| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
|---|
| 5 | |
|---|
| 6 | // +-------------------------------------------------------------------+ |
|---|
| 7 | // | WiFiDog Authentication Server | |
|---|
| 8 | // | ============================= | |
|---|
| 9 | // | | |
|---|
| 10 | // | The WiFiDog Authentication Server is part of the WiFiDog captive | |
|---|
| 11 | // | portal suite. | |
|---|
| 12 | // +-------------------------------------------------------------------+ |
|---|
| 13 | // | PHP version 5 required. | |
|---|
| 14 | // +-------------------------------------------------------------------+ |
|---|
| 15 | // | Homepage: http://www.wifidog.org/ | |
|---|
| 16 | // | Source Forge: http://sourceforge.net/projects/wifidog/ | |
|---|
| 17 | // +-------------------------------------------------------------------+ |
|---|
| 18 | // | This program is free software; you can redistribute it and/or | |
|---|
| 19 | // | modify it under the terms of the GNU General Public License as | |
|---|
| 20 | // | published by the Free Software Foundation; either version 2 of | |
|---|
| 21 | // | the License, or (at your option) any later version. | |
|---|
| 22 | // | | |
|---|
| 23 | // | This program is distributed in the hope that it will be useful, | |
|---|
| 24 | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|---|
| 25 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|---|
| 26 | // | GNU General Public License for more details. | |
|---|
| 27 | // | | |
|---|
| 28 | // | You should have received a copy of the GNU General Public License | |
|---|
| 29 | // | along with this program; if not, contact: | |
|---|
| 30 | // | | |
|---|
| 31 | // | Free Software Foundation Voice: +1-617-542-5942 | |
|---|
| 32 | // | 59 Temple Place - Suite 330 Fax: +1-617-542-2652 | |
|---|
| 33 | // | Boston, MA 02111-1307, USA gnu@gnu.org | |
|---|
| 34 | // | | |
|---|
| 35 | // +-------------------------------------------------------------------+ |
|---|
| 36 | |
|---|
| 37 | /** This file contains the code for the MainUI class, as well as GUI exception handling. |
|---|
| 38 | * @package WiFiDogAuthServer |
|---|
| 39 | * @author Benoit Grégoire <bock@step.polymtl.ca> |
|---|
| 40 | * @copyright 2005-2007 Benoit Grégoire, Technologies Coeus inc. |
|---|
| 41 | * @version Subversion $Id: MainUI.php 1246 2007-07-03 16:35:09Z benoitg $ |
|---|
| 42 | * @link http://www.wifidog.org/ |
|---|
| 43 | */ |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Load required files |
|---|
| 47 | */ |
|---|
| 48 | |
|---|
| 49 | require_once('classes/SmartyWifidog.php'); |
|---|
| 50 | |
|---|
| 51 | define('MENU_ITEM_GROUPING','MENU_ITEM_GROUPING'); |
|---|
| 52 | /** |
|---|
| 53 | * Singleton class for managing menu. |
|---|
| 54 | * |
|---|
| 55 | * @package WiFiDogAuthServer |
|---|
| 56 | * @author Benoit Grégoire <bock@step.polymtl.ca> |
|---|
| 57 | * @copyright 2005-2006 Benoit Grégoire, Technologies Coeus inc. |
|---|
| 58 | */ |
|---|
| 59 | class Menu { |
|---|
| 60 | /** holder for the singleton */ |
|---|
| 61 | private static $object; |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Object for Smarty class |
|---|
| 65 | */ |
|---|
| 66 | private $smarty; |
|---|
| 67 | /** The array from which the menu is generated */ |
|---|
| 68 | private $menuArray; |
|---|
| 69 | /** |
|---|
| 70 | * Get the MainUI object |
|---|
| 71 | * @return object The MainUI object |
|---|
| 72 | */ |
|---|
| 73 | public static function &getObject() { |
|---|
| 74 | if (self :: $object == null) { |
|---|
| 75 | self :: $object = new self(); |
|---|
| 76 | } |
|---|
| 77 | return self :: $object; |
|---|
| 78 | } |
|---|
| 79 | /** |
|---|
| 80 | * Contructor |
|---|
| 81 | * |
|---|
| 82 | * @return void |
|---|
| 83 | * |
|---|
| 84 | * @access public |
|---|
| 85 | */ |
|---|
| 86 | private function __construct() { |
|---|
| 87 | //$db = AbstractDb :: getObject(); |
|---|
| 88 | // Init Smarty |
|---|
| 89 | $this->smarty = SmartyWifidog :: getObject(); |
|---|
| 90 | |
|---|
| 91 | // Init the menu array |
|---|
| 92 | $this->_menuArray = array('childrens'=>array()); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** Builds an HTML menu, appends it to $userData*/ |
|---|
| 96 | static private function buildHtmlMenuItemCallback($menuItemArray, $menuItemIndex, &$userData) { |
|---|
| 97 | //echo "buildHtmlMenuItemCallback for: ";pretty_print_r($menuItemArray); |
|---|
| 98 | $html = ''; |
|---|
| 99 | //echo "previous_level: {$userData['previous_level']} current_level: {$menuItemArray['level']}<br/>"; |
|---|
| 100 | if(isset($userData['previous_level']) && $userData['previous_level']>$menuItemArray['level']) { |
|---|
| 101 | $html .= "</ul>\n</li>\n"; |
|---|
| 102 | } |
|---|
| 103 | !empty($menuItemArray['title'])?$title=$menuItemArray['title']:$title=$menuItemArray['path']; |
|---|
| 104 | if(!empty($menuItemArray['url'])) { |
|---|
| 105 | $html .= "<li><a href='{$menuItemArray['url']}'>{$menuItemArray['title']}</a>\n"; |
|---|
| 106 | } |
|---|
| 107 | else if(!empty($menuItemArray['childrens'])){ |
|---|
| 108 | $html .= "<li><a href='#'>{$menuItemArray['title']}</a>\n"; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | if(!empty($menuItemArray['childrens'])) { |
|---|
| 112 | $html .= "<ul>\n"; |
|---|
| 113 | } else { |
|---|
| 114 | $html .= "\n</li>\n"; |
|---|
| 115 | } |
|---|
| 116 | $userData['previous_level'] = $menuItemArray['level']; |
|---|
| 117 | isset($userData['html'])?$userData['html'].=$html:$userData['html']=$html; |
|---|
| 118 | } |
|---|
| 119 | /** Takes an array_walk_recursive compatible callback. Will be called for each menu item */ |
|---|
| 120 | private static function menuArrayWalkRecursiveReal($menuArray, $menuItemIdx, $funcname, &$userdata = null) { |
|---|
| 121 | //echo "menuArrayWalkRecursive called with menuArray:"; pretty_print_r($menuArray); |
|---|
| 122 | $retval = true; |
|---|
| 123 | if(isset($menuArray['path'])){ |
|---|
| 124 | //Only call if we are in a real menu item (and, among other, not at the root) |
|---|
| 125 | //echo "menuArrayWalkRecursive(): Calling callback.<br/>"; |
|---|
| 126 | $retval = call_user_func($funcname, $menuArray, $menuItemIdx, &$userdata); |
|---|
| 127 | } |
|---|
| 128 | foreach ($menuArray['childrens'] as $menuItemIdx => $menuItem) { |
|---|
| 129 | //pretty_print_r($menuItem); |
|---|
| 130 | //echo "Recusively calling for $menuItemIdx<br/>"; |
|---|
| 131 | $retval = $retval & self::menuArrayWalkRecursiveReal($menuItem, $menuItemIdx, $funcname, &$userdata); |
|---|
| 132 | } |
|---|
| 133 | return $retval; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** Takes an array_walk_recursive compatible callback. Will be called for each menu item */ |
|---|
| 137 | public function menuArrayWalkRecursive($funcname, &$userdata = null) { |
|---|
| 138 | return self::menuArrayWalkRecursiveReal($this->_menuArray, 0, $funcname, &$userdata); |
|---|
| 139 | } |
|---|
| 140 | /** Compare menu items according to it's title output passed to strcoll(). The toString |
|---|
| 141 | * output is converted to ISO-8859-1 before sorting to allow strcoll() to be used |
|---|
| 142 | * for locale-specific sorting. |
|---|
| 143 | */ |
|---|
| 144 | public static function titlestrcoll($object1, $object2) |
|---|
| 145 | { |
|---|
| 146 | //echo "CMP: ".$object1['title']." vs ". $object2['title']."<br/>\n"; |
|---|
| 147 | return strcoll ( utf8_decode($object1['title']), utf8_decode($object2['title']) ); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** Sort the menu using a user defined sort function */ |
|---|
| 151 | private static function menuArraySort(&$menuArray, $funcname, &$userdata = null) { |
|---|
| 152 | //echo "menuArraySort called with menuArray:"; pretty_print_r($menuArray); |
|---|
| 153 | $retval = true; |
|---|
| 154 | //Sort childrens |
|---|
| 155 | $retval = uasort($menuArray['childrens'], $funcname); |
|---|
| 156 | foreach ($menuArray['childrens'] as $menuItemIdx => &$menuItem) { |
|---|
| 157 | //Recursive call |
|---|
| 158 | //pretty_print_r($menuItem); |
|---|
| 159 | //echo "Recusively calling for $menuItemIdx<br/>"; |
|---|
| 160 | $retval = $retval & self::menuArraySort($menuItem, $funcname, &$userdata); |
|---|
| 161 | } |
|---|
| 162 | return $retval; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | public function processHookMenu($classname) { |
|---|
| 166 | require_once("classes/$classname.php"); |
|---|
| 167 | $hookArray = call_user_func(array($classname, 'hookMenu')); |
|---|
| 168 | //pretty_print_r($hookArray); |
|---|
| 169 | foreach($hookArray as $hookArrayItem) { |
|---|
| 170 | |
|---|
| 171 | $parts = explode('/', $hookArrayItem['path']); |
|---|
| 172 | $menuItem=&$this->_menuArray;//This is just for initialisation, the foreach will set it to a leaf of menuitem. |
|---|
| 173 | foreach ($parts as $k => $part) { |
|---|
| 174 | //echo "$k => $part<br/>"; |
|---|
| 175 | if (!isset($menuItem['childrens'][$part])){ |
|---|
| 176 | //echo "$part does not exist, setting it<br/>"; |
|---|
| 177 | $menuItem['childrens'][$part]=array("childrens"=>array(), "level"=>$k); |
|---|
| 178 | } |
|---|
| 179 | $menuItem=&$menuItem['childrens'][$part]; |
|---|
| 180 | } |
|---|
| 181 | $menuItem = array_merge($menuItem, $hookArrayItem); |
|---|
| 182 | //echo "Setting menuItem to hookArrayItem of path {$hookArrayItem['path']}, new _menuArray:<br/>"; |
|---|
| 183 | // pretty_print_r($this->_menuArray); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | public function initMenu() { |
|---|
| 187 | $this->processHookMenu('Server'); |
|---|
| 188 | $this->processHookMenu('Node'); |
|---|
| 189 | $this->processHookMenu('Network'); |
|---|
| 190 | $this->processHookMenu('User'); |
|---|
| 191 | $this->processHookMenu('Content'); |
|---|
| 192 | $this->processHookMenu('NodeList'); |
|---|
| 193 | $this->processHookMenu('Role'); |
|---|
| 194 | $this->processHookMenu('VirtualHost'); |
|---|
| 195 | $this->processHookMenu('ContentTypeFilter'); |
|---|
| 196 | $this->processHookMenu('ProfileTemplate'); |
|---|
| 197 | self::menuArraySort($this->_menuArray, array('Menu','titlestrcoll')); |
|---|
| 198 | //pretty_print_r($this->_menuArray); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | public function getUserUI() |
|---|
| 202 | { |
|---|
| 203 | $this->initMenu(); |
|---|
| 204 | $html = ''; |
|---|
| 205 | //Deal with internet explorer's baindeadness. From http://www.htmldog.com/articles/suckerfish/dropdowns/example/vertical.html |
|---|
| 206 | $html .= <<<EOT |
|---|
| 207 | <script type="text/javascript"><!--//--><![CDATA[//><!-- |
|---|
| 208 | |
|---|
| 209 | sfHover = function() { |
|---|
| 210 | var sfEls = document.getElementById("nav").getElementsByTagName("LI"); |
|---|
| 211 | for (var i=0; i<sfEls.length; i++) { |
|---|
| 212 | sfEls[i].onmouseover=function() { |
|---|
| 213 | this.className+=" sfhover"; |
|---|
| 214 | } |
|---|
| 215 | sfEls[i].onmouseout=function() { |
|---|
| 216 | this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | if (window.attachEvent) window.attachEvent("onload", sfHover); |
|---|
| 221 | |
|---|
| 222 | //--><!]]></script> |
|---|
| 223 | EOT; |
|---|
| 224 | $html .= "<ul id='nav'>\n"; |
|---|
| 225 | $userData=null; |
|---|
| 226 | self::menuArrayWalkRecursive(array('Menu','buildHtmlMenuItemCallback'), $userData); |
|---|
| 227 | $html .= $userData['html']; |
|---|
| 228 | if(isset($userData['previous_level']) && $userData['previous_level']>0) { |
|---|
| 229 | $html .= "</ul>\n"; |
|---|
| 230 | } |
|---|
| 231 | $html .= "</ul>\n"; |
|---|
| 232 | //echo htmlspecialchars($userData['html']); |
|---|
| 233 | return $html; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /* |
|---|
| 238 | * Local variables: |
|---|
| 239 | * tab-width: 4 |
|---|
| 240 | * c-basic-offset: 4 |
|---|
| 241 | * c-hanging-comment-ender-p: nil |
|---|
| 242 | * End: |
|---|
| 243 | */ |
|---|