FOSSology  4.4.0
Open Source License Compliance by Open Source Software
Menu.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2011 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\UI\Component;
10 
12 use Twig\Environment;
13 
14 class Menu
15 {
16  const FULL_MENU_DEBUG = 'fullmenudebug';
21  const BANNER_COOKIE = 'close_banner';
22  var $MenuTarget = "treenav";
23  protected $renderer;
24 
25  public function __construct(Environment $renderer)
26  {
27  // Add default menus (with no actions linked to plugins)
28  menu_insert("Main::Upload", 70);
29  menu_insert("Main::Jobs", 60);
30  menu_insert("Main::Organize", 50);
31  menu_insert("Main::Help", -1);
32  menu_insert("Main::Help::Documentation", 0, NULL, NULL, NULL, "<a href='https://github.com/fossology/fossology/wiki'>Documentation</a>");
33  $this->renderer = $renderer;
34  }
35 
39  function menu_html(&$menu, $indent)
40  {
41  if (empty($menu)) {
42  return;
43  }
44  $output = "<!--[if lt IE 7]><table><tr><td><![endif]-->\n";
45  $output .= "<ul id='menu-$indent'>\n";
46 
47  foreach ($menu as $menuEntry) {
48  $output .= '<li>';
49 
50  if (!empty($menuEntry->HTML)) {
51  $output .= $menuEntry->HTML;
52  } else { /* create HTML */
53  $output .= $this->createHtmlFromMenuEntry($menuEntry, $indent);
54  }
55 
56  if (!empty($menuEntry->SubMenu)) {
57  $output .= $this->menu_html($menuEntry->SubMenu, $indent + 1);
58  }
59  }
60  $output .= "</ul>\n";
61  $output .= "<!--[if lt IE 7]></td></tr></table></a><![endif]-->\n";
62  return preg_replace("|<li><a href=\"#\"><font color(.*)*?$|m", '', $output);
63  }
64 
65  function createHtmlFromMenuEntry(\menu $menuEntry, $indent)
66  {
67  $isFullMenuDebug = array_key_exists(self::FULL_MENU_DEBUG, $_SESSION) && $_SESSION[self::FULL_MENU_DEBUG] == 1;
68  $output = "";
69  if (!empty($menuEntry->URI)) {
70  $output .= '<a id="'. htmlentities($menuEntry->FullName) .'" href="' . Traceback_uri() . "?mod=" . $menuEntry->URI;
71  if (empty($menuEntry->Target) || ($menuEntry->Target == "")) {
72  $output .= '">';
73  } else {
74  $output .= '" target="' . $menuEntry->Target . '">';
75  }
76  if ($isFullMenuDebug) {
77  $output .= $menuEntry->FullName . "(" . $menuEntry->Order . ")";
78  } else {
79  $output .= $menuEntry->Name;
80  }
81  } else {
82  $output .= '<a id="'. htmlentities($menuEntry->FullName) .'" href="#">';
83  if (empty($menuEntry->SubMenu)) {
84  $output .= "<font color='#C0C0C0'>";
85  if ($isFullMenuDebug) {
86  $output .= $menuEntry->FullName . "(" . $menuEntry->Order . ")";
87  } else {
88  $output .= '';
89  }
90  $output .= "</font>";
91  } else {
92  if ($isFullMenuDebug) {
93  $output .= $menuEntry->FullName . "(" . $menuEntry->Order . ")";
94  } else {
95  $output .= $menuEntry->Name;
96  }
97  }
98  }
99 
100  if (!empty($menuEntry->SubMenu) && ($indent > 0)) {
101  $output .= " <span>&raquo;</span>";
102  }
103  $output .= "</a>\n";
104  return $output;
105  }
106 
110  function OutputCSS()
111  {
112 
113  $output = "<style type=\"text/css\">\n";
114  /* Depth 0 is special: position is relative, colors are blue */
115  $depth = 0;
116  $label = "";
117  $Menu = menu_find("Main", $MenuDepth);
118  $cssBorder = "border-color:#bee5eb #bee5eb #bee5eb #bee5eb; border-width:1px 1px 1px 1px; border-radius:3px;";
119  $cssPadding = "padding:4px 0px 4px 4px;";
120  $FOSScolor1 = "#c50830";
121  $FOSSbg = "white";
122 
123  $FOSSfg1 = "black";
124  $FOSSbg1 = "white";
125  $FOSSfg1h = $FOSScolor1; // highlight colors
126  $FOSSbg1h = "#d1ecf1";
127 
128  $FOSSfg2 = "#0c5460";
129  $FOSSbg2 = "#d1ecf1";
130  $FOSSfg2h = $FOSScolor1; // highlight colors
131  $FOSSbg2h = "#d1ecf1";
132 
133  $FOSSfg3 = "#0c5460";
134  $FOSSbg3 = "#d1ecf1";
135  $FOSSfg3h = $FOSScolor1; // highlight colors
136  $FOSSbg3h = "#d1ecf1";
137 
138  if ($depth < $MenuDepth) {
140  $output .= "\n/* CSS for Depth $depth */\n";
141  $label = "ul#menu-" . $depth;
142  $output .= $label . "\n";
143  $output .= " { z-index:0; margin:0; padding:0px; list-style:none; background:$FOSSbg1; width:100%; height:24px; font:normal 14px verdana, arial, helvetica; font-weight: bold; }\n";
144  $label .= " li";
145  $output .= $label . "\n";
146  $output .= " { float:left; margin:0; padding:0px; display:block; position:relative; width:auto; border:0px solid #000; }\n";
147  $output .= $label . " a:link,\n";
148  $output .= $label . " a:visited\n";
149  $output .= " { float:left; padding:4px 10px; text-decoration:none; color:$FOSSfg1; background:$FOSSbg1; width:auto; display:block; }\n";
150  $output .= $label . ":hover a,\n";
151  $output .= $label . " a:hover,\n";
152  $output .= $label . " a:active\n";
153  $output .= " { float:left; padding:4px 10px; color:$FOSSfg1h; background:$FOSSbg1h; $cssBorder width:auto; display:block; }\n";
154  $output .= $label . " a span\n";
155  $output .= " { float:left; position:absolute; top:0; left:135px; font-size:12pt; }\n";
156  $depth++;
157  }
158 
159  /* Depth 1 is special: position is absolute. Left is 0, top is 24 */
160  if ($depth < $MenuDepth) {
161  $output .= "\n/* CSS for Depth $depth */\n";
162  $output .= $label . " ul#menu-" . $depth . "\n";
163  $output .= " { margin:0; padding:0px 0; list-style:none; display:none; visibility:hidden; left:0px; width:150px; position:absolute; top:24px; font-weight: bold; }\n";
164  $output .= $label . ":hover ul#menu-" . $depth . "\n";
165  $output .= " { float:left; display:block; visibility:visible; }\n";
166  $label .= " ul#menu-" . $depth . " li";
167  $output .= $label . "\n";
168  $output .= " { z-index:$depth; margin:0; padding:0; display:block; visibility:visible; position:relative; width:150px; }\n";
169  $output .= $label . " a:link,\n";
170  $output .= $label . " a:visited\n";
171  $output .= " { z-index:$depth; $cssPadding color:$FOSSfg2; background:$FOSSbg2; border:1px solid #000; $cssBorder width:150px; display:block; visibility:visible; }\n";
172  $output .= $label . ":hover a,\n";
173  $output .= $label . " a:active,\n";
174  $output .= $label . " a:hover\n";
175  $output .= " { z-index:$depth; $cssPadding color:$FOSSfg2h; background:$FOSSbg2h; width:150px; display:block; visibility:visible; }\n";
176  $output .= $label . " a span\n";
177  $output .= " { text-align:left; }\n";
178  $depth++;
179  }
180 
181  /* Depth 2+ is recursive: position is absolute. Left is 150*(Depth-1), top is 0 */
182  for (; $depth < $MenuDepth; $depth++) {
183  $output .= "\n/* CSS for Depth $depth */\n";
184  $output .= $label . " ul#menu-" . $depth . "\n";
185  $output .= " { margin:0; padding:1px 0; list-style:none; display:none; visibility:hidden; left:156px; width:150px; position:absolute; top:-1px; font-weight: bold; }\n";
186  $output .= $label . ":hover ul#menu-" . $depth . "\n";
187  $output .= " { float:left; display:block; visibility:visible; }\n";
188  $label .= " ul#menu-" . $depth . " li";
189  $output .= $label . "\n";
190  $output .= " { z-index:$depth; margin:0; padding:0; display:block; visibility:visible; position:relative; width:150px; margin-left:-6px; }\n";
191  $output .= $label . " a:link,\n";
192  $output .= $label . " a:visited\n";
193  $output .= " { z-index:$depth; $cssPadding color:$FOSSfg3; background:$FOSSbg2h; border:1px solid #000; $cssBorder width:150px; display:block; }\n";
194  $output .= $label . ":hover a,\n";
195  $output .= $label . " a:active,\n";
196  $output .= $label . " a:hover\n";
197  $output .= " { z-index:$depth; $cssPadding color:$FOSSfg3h; background:$FOSSbg3h; width:150px; display:block; visibility:visible; }\n";
198  $output .= $label . " a span\n";
199  $output .= " { text-align:left; }\n";
200  }
201  $output .= "</style>\n";
202 
203  /* For IE's screwed up CSS: this defines "hover". */
204  $output .= "<!--[if lt IE 8]>\n";
205  $output .= "<style type='text/css' media='screen'>\n";
207  $output .= "body { behavior:url(csshover.htc); }\n";
209  for ($i = 1; $i < $MenuDepth; $i++) {
210  $output .= "#menu-$i table {height:0px; border-collapse:collapse; margin:0; padding:0; }\n";
211  $output .= "#menu-$i td {height:0px; border:none; margin:0; padding:0; }\n";
212  }
213  $output .= "</style>\n";
214  $output .= "<![endif]-->\n";
215 
216  $this->_CSSdone = 1;
217  return ($output);
218  } // OutputCSS()
219 
223  function Output($title = NULL)
224  {
225  global $SysConf;
226  $sysConfig = $SysConf['SYSCONFIG'];
227 
228  $hide_banner = (array_key_exists(self::BANNER_COOKIE, $_COOKIE)
229  && $_COOKIE[self::BANNER_COOKIE] == 1);
230 
231  $vars = array();
232  $vars['title'] = empty($title) ? _("Welcome to FOSSology") : $title;
233  if ($hide_banner) {
234  $vars['bannerMsg'] = "";
235  $vars['systemLoad'] = "";
236  } else {
237  $vars['bannerMsg'] = @$sysConfig['BannerMsg'];
238  $vars['systemLoad'] = get_system_load_average().'<br/>';
239  }
240  $vars['logoLink'] = $sysConfig['LogoLink']?: 'http://fossology.org';
241  $vars['logoImg'] = $sysConfig['LogoImage']?: 'images/fossology-logo.gif';
242 
243  if ( array_key_exists('SupportEmailLabel',$sysConfig) && !empty($sysConfig['SupportEmailLabel'])
244  && array_key_exists('SupportEmailAddr',$sysConfig) && !empty($sysConfig['SupportEmailAddr'])) {
245  $menuItem = '<a href="mailto:'.$sysConfig['SupportEmailAddr'].'?subject='.@$sysConfig['SupportEmailSubject'].'">'.$sysConfig['SupportEmailLabel'].'</a>';
246  menu_insert("Main::Help::".$sysConfig['SupportEmailLabel'], 0, NULL, NULL, NULL, $menuItem);
247  }
248 
249  $menu = menu_find("Main", $MenuDepth);
250  $vars['mainMenu'] = $this->menu_html($menu, 0);
251  $vars['uri'] = Traceback_uri();
252 
253  /* Handle login information */
254  $vars['isLoggedOut'] = ((empty($_SESSION[Auth::USER_NAME])) or ($_SESSION[Auth::USER_NAME] == "Default User"));
255  $vars['isLoginPage'] = GetParm("mod", PARM_STRING)=='auth';
256 
257  global $SysConf;
258  if (array_key_exists('BUILD', $SysConf)) {
259  $vars['versionInfo'] = array(
260  'version' => $SysConf['BUILD']['VERSION'],
261  'buildDate' => $SysConf['BUILD']['BUILD_DATE'],
262  'commitHash' => $SysConf['BUILD']['COMMIT_HASH'],
263  'commitDate' => $SysConf['BUILD']['COMMIT_DATE'],
264  'branchName' => $SysConf['BUILD']['BRANCH']
265  );
266  }
267 
268  if (!$vars['isLoggedOut']) {
269  $this->mergeUserLoginVars($vars);
270  }
271 
272  return $this->renderer->load('components/menu.html.twig')->render($vars);
273  }
274 
275  private function mergeUserLoginVars(&$vars)
276  {
277  global $container;
278  $dbManager = $container->get("db.manager");
279 
280  $vars['logOutUrl'] = Traceback_uri() . '?mod=' . ((plugin_find_id('auth')>=0) ? 'auth' : 'smauth');
281  $vars['userName'] = $_SESSION[Auth::USER_NAME];
282 
283  $sql = 'SELECT group_pk, group_name FROM group_user_member LEFT JOIN groups ON group_fk=group_pk WHERE user_fk=$1';
284  $stmt = __METHOD__ . '.availableGroups';
285  $dbManager->prepare($stmt, $sql);
286  $res = $dbManager->execute($stmt, array($_SESSION['UserId']));
287  $allAssignedGroups = array();
288  while ($row = $dbManager->fetchArray($res)) {
289  $allAssignedGroups[$row['group_pk']] = $row['group_name'];
290  }
291  $dbManager->freeResult($res);
292  if (count($allAssignedGroups) > 1) {
293  $vars['backtraceUri'] = Traceback_uri() . "?mod=" . Traceback_parm();
294  $vars['groupId'] = $_SESSION[Auth::GROUP_ID];
295  $vars['allAssignedGroups'] = $allAssignedGroups;
296  } else {
297  $vars['singleGroup'] = @$_SESSION['GroupName'];
298  }
299  }
300 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
menu_html(&$menu, $indent)
Recursively generate the menu in HTML.
Definition: Menu.php:39
Output($title=NULL)
Create the output.
Definition: Menu.php:223
OutputCSS()
Create the output CSS.
Definition: Menu.php:110
Code for creating a menu list (2D linked list) from a set of plugins.
Definition: common-menu.php:20
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
menu_find($Name, &$MaxDepth, $Menu=NULL)
Given a top-level menu name, find the list of sub-menus below it and max depth of menu.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
Traceback_parm($ShowMod=1)
Get the URI query to this location.
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
get_system_load_average()
Get system load average.