1<?php 2/* 3 FusionPBX 4 Version: MPL 1.1 5 6 The contents of this file are subject to the Mozilla Public License Version 7 1.1 (the "License"); you may not use this file except in compliance with 8 the License. You may obtain a copy of the License at 9 http://www.mozilla.org/MPL/ 10 11 Software distributed under the License is distributed on an "AS IS" basis, 12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 for the specific language governing rights and limitations under the 14 License. 15 16 The Original Code is FusionPBX 17 18 The Initial Developer of the Original Code is 19 Mark J Crane <markjcrane@fusionpbx.com> 20 Copyright (C) 2010 - 2014 21 All Rights Reserved. 22 23 Contributor(s): 24 Mark J Crane <markjcrane@fusionpbx.com> 25*/ 26 27//define the menu class 28if (!class_exists('menu')) { 29 class menu { 30 //define the variables 31 public $menu_uuid; 32 public $menu_language; 33 34 //delete items in the menu that are not protected 35 public function delete() { 36 //set the variable 37 $db = $this->db; 38 //remove existing menu languages 39 $sql = "delete from v_menu_languages "; 40 $sql .= "where menu_uuid = '".$this->menu_uuid."' "; 41 $sql .= "and menu_item_uuid in ( "; 42 $sql .= " select menu_item_uuid "; 43 $sql .= " from v_menu_items "; 44 $sql .= " where menu_uuid = '".$this->menu_uuid."' "; 45 $sql .= " and ( "; 46 $sql .= " menu_item_protected <> 'true' "; 47 $sql .= " or menu_item_protected is null "; 48 $sql .= " ) "; 49 $sql .= ");"; 50 $db->exec(check_sql($sql)); 51 //remove existing unprotected menu item groups 52 $sql = "delete from v_menu_item_groups "; 53 $sql .= "where menu_uuid = '".$this->menu_uuid."' "; 54 $sql .= "and menu_item_uuid in ( "; 55 $sql .= " select menu_item_uuid "; 56 $sql .= " from v_menu_items "; 57 $sql .= " where menu_uuid = '".$this->menu_uuid."' "; 58 $sql .= " and ( "; 59 $sql .= " menu_item_protected <> 'true' "; 60 $sql .= " or menu_item_protected is null "; 61 $sql .= " ) "; 62 $sql .= ");"; 63 $db->exec(check_sql($sql)); 64 //remove existing unprotected menu items 65 $sql = "delete from v_menu_items "; 66 $sql .= "where menu_uuid = '".$this->menu_uuid."' "; 67 $sql .= "and (menu_item_protected <> 'true' "; 68 $sql .= "or menu_item_protected is null);"; 69 $db->exec(check_sql($sql)); 70 } 71 72 //restore the menu 73 public function restore() { 74 75 //set the variables 76 $db = $this->db; 77 78 //get the $apps array from the installed apps from the core and mod directories 79 $config_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_menu.php"); 80 $x = 0; 81 if (is_array($config_list)) { 82 foreach ($config_list as &$config_path) { 83 $app_path = dirname($config_path); 84 $app_path = preg_replace('/\A.*(\/.*\/.*)\z/', '$1', $app_path); 85 $y = 0; 86 try { 87 //echo "[".$x ."] ".$config_path."\n"; 88 include($config_path); 89 $x++; 90 } 91 catch (Exception $e) { 92 echo 'exception caught: ' . $e->getMessage() . "\n"; 93 exit; 94 } 95 } 96 } 97 98 //begin the transaction 99 if ($db_type == "sqlite") { 100 $db->beginTransaction(); 101 } 102 103 //get the list of languages 104 $language = new text; 105 106 //use the app array to restore the default menu 107 if (is_array($apps)) { 108 foreach ($apps as $row) { 109 if (is_array($row['menu'])) { 110 foreach ($row['menu'] as $menu) { 111 //set the variables 112 if (strlen($menu['title'][$this->menu_language]) > 0) { 113 $menu_item_title = $menu['title'][$this->menu_language]; 114 } 115 else { 116 $menu_item_title = $menu['title']['en-us']; 117 } 118 $menu_item_uuid = $menu['uuid']; 119 $menu_item_parent_uuid = $menu['parent_uuid']; 120 $menu_item_category = $menu['category']; 121 $menu_item_icon = $menu['icon']; 122 $menu_item_path = $menu['path']; 123 $menu_item_order = $menu['order']; 124 $menu_item_description = $menu['desc']; 125 126 //menu found set the default 127 $menu_item_exists = true; 128 129 //if the item uuid is not currently in the db then add it 130 $sql = "select * from v_menu_items "; 131 $sql .= "where menu_uuid = '".$this->menu_uuid."' "; 132 $sql .= "and menu_item_uuid = '".$menu_item_uuid."' "; 133 $prep_statement = $db->prepare(check_sql($sql)); 134 if ($prep_statement) { 135 $prep_statement->execute(); 136 $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC); 137 if (count($result) == 0) { 138 139 //menu found the menu 140 $menu_item_exists = false; 141 142 //insert the default menu into the database 143 $sql = "insert into v_menu_items "; 144 $sql .= "("; 145 $sql .= "menu_item_uuid, "; 146 $sql .= "menu_uuid, "; 147 $sql .= "menu_item_title, "; 148 $sql .= "menu_item_link, "; 149 $sql .= "menu_item_category, "; 150 $sql .= "menu_item_icon, "; 151 if (strlen($menu_item_order) > 0) { 152 $sql .= "menu_item_order, "; 153 } 154 if (strlen($menu_item_parent_uuid) > 0) { 155 $sql .= "menu_item_parent_uuid, "; 156 } 157 $sql .= "menu_item_description "; 158 $sql .= ") "; 159 $sql .= "values "; 160 $sql .= "("; 161 $sql .= "'".$menu_item_uuid."', "; 162 $sql .= "'".$this->menu_uuid."', "; 163 $sql .= "'".check_str($menu_item_title)."', "; 164 $sql .= "'$menu_item_path', "; 165 $sql .= "'$menu_item_category', "; 166 $sql .= "'$menu_item_icon', "; 167 if (strlen($menu_item_order) > 0) { 168 $sql .= "'$menu_item_order', "; 169 } 170 if (strlen($menu_item_parent_uuid) > 0) { 171 $sql .= "'$menu_item_parent_uuid', "; 172 } 173 $sql .= "'$menu_item_description' "; 174 $sql .= ")"; 175 if ($menu_item_uuid == $menu_item_parent_uuid) { 176 //echo $sql."<br />\n"; 177 } 178 else { 179 $db->exec(check_sql($sql)); 180 } 181 unset($sql); 182 } 183 } 184 185 //set the menu languages 186 if (!$menu_item_exists and is_array($language->languages)) { 187 foreach ($language->languages as $menu_language) { 188 $menu_item_title = $menu["title"][$menu_language]; 189 if(strlen($menu_item_title) == 0) { 190 $menu_item_title = $menu["title"]['en-us']; 191 } 192 $menu_language_uuid = uuid(); 193 $sql = "insert into v_menu_languages "; 194 $sql .= "("; 195 $sql .= "menu_language_uuid, "; 196 $sql .= "menu_item_uuid, "; 197 $sql .= "menu_uuid, "; 198 $sql .= "menu_language, "; 199 $sql .= "menu_item_title "; 200 $sql .= ") "; 201 $sql .= "values "; 202 $sql .= "("; 203 $sql .= "'".$menu_language_uuid."', "; 204 $sql .= "'".$menu_item_uuid."', "; 205 $sql .= "'".$this->menu_uuid."', "; 206 $sql .= "'".$menu_language."', "; 207 $sql .= "'".check_str($menu_item_title)."' "; 208 $sql .= ")"; 209 $db->exec(check_sql($sql)); 210 unset($sql); 211 } 212 } 213 214 } 215 } 216 } 217 } 218 219 //make sure the default user groups exist 220 $group = new groups; 221 $group->defaults(); 222 223 //get default global group_uuids 224 $sql = "select group_uuid, group_name from v_groups "; 225 $sql .= "where domain_uuid is null "; 226 $sql .= "and ( "; 227 $sql .= " group_name = 'public' "; 228 $sql .= " or group_name = 'user' "; 229 $sql .= " or group_name = 'admin' "; 230 $sql .= " or group_name = 'superadmin' "; 231 $sql .= " or group_name = 'agent' "; 232 $sql .= ") "; 233 $prep_statement = $db->prepare(check_sql($sql)); 234 $prep_statement->execute(); 235 $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC); 236 if (is_array($result)) { 237 foreach ($result as $row) { 238 $group_uuids[$row['group_name']] = $row['group_uuid']; 239 } 240 } 241 unset($sql, $prep_statement, $result); 242 243 //if there are no groups listed in v_menu_item_groups under menu_item_uuid then add the default groups 244 if (is_array($apps)) { 245 foreach($apps as $app) { 246 if (is_array($apps)) { 247 foreach ($app['menu'] as $sub_row) { 248 if (isset($sub_row['groups'])) foreach ($sub_row['groups'] as $group) { 249 $sql = "select count(*) as count from v_menu_item_groups "; 250 $sql .= "where menu_item_uuid = '".$sub_row['uuid']."' "; 251 $sql .= "and menu_uuid = '".$this->menu_uuid."' "; 252 $sql .= "and group_name = '".$group."' "; 253 $sql .= "and group_uuid = '".$group_uuids[$group]."' "; 254 //echo $sql."<br>"; 255 $prep_statement = $db->prepare($sql); 256 $prep_statement->execute(); 257 $sub_result = $prep_statement->fetch(PDO::FETCH_ASSOC); 258 unset ($prep_statement); 259 if ($sub_result['count'] == 0) { 260 //no menu item groups found add the defaults 261 $sql = "insert into v_menu_item_groups "; 262 $sql .= "( "; 263 $sql .= "menu_item_group_uuid, "; 264 $sql .= "menu_uuid, "; 265 $sql .= "menu_item_uuid, "; 266 $sql .= "group_name, "; 267 $sql .= "group_uuid "; 268 $sql .= ") "; 269 $sql .= "values "; 270 $sql .= "( "; 271 $sql .= "'".uuid()."', "; 272 $sql .= "'".$this->menu_uuid."', "; 273 $sql .= "'".$sub_row['uuid']."', "; 274 $sql .= "'".$group."', "; 275 $sql .= "'".$group_uuids[$group]."' "; 276 $sql .= ") "; 277 //echo $sql."<br>"; 278 $db->exec(check_sql($sql)); 279 unset($sql); 280 } 281 } 282 } 283 } 284 } 285 } 286 287 //commit the transaction 288 if ($db_type == "sqlite") { 289 $db->commit(); 290 } 291 } //end function 292 293 294 //create the menu 295 public function build_html($menu_item_level = 0) { 296 297 $db = $this->db; 298 $menu_html_full = ''; 299 300 $menu_array = $this->menu_array(); 301 302 if (!isset($_SESSION['groups'])) { 303 $_SESSION['groups'][0]['group_name'] = 'public'; 304 } 305 306 if (is_array($menu_array)) { 307 foreach($menu_array as $menu_field) { 308 //set the variables 309 $menu_item_link = $menu_field['menu_item_link']; 310 $menu_item_category = $menu_field['menu_item_category']; 311 $menu_items = $menu_field['menu_items']; 312 313 //prepare the protected menus 314 $menu_item_title = ($menu_field['menu_item_protected'] == "true") ? $menu_field['menu_item_title'] : $menu_field['menu_language_title']; 315 316 //prepare the menu_tags according to the category 317 $menu_tags = ''; 318 switch ($menu_item_category) { 319 case "internal": 320 $menu_tags = "href='".PROJECT_PATH.$submenu_item_link."'"; 321 break; 322 case "external": 323 if (substr($submenu_item_link, 0,1) == "/") { 324 $submenu_item_link = PROJECT_PATH.$submenu_item_link; 325 } 326 $menu_tags = "href='".$submenu_item_link."' target='_blank'"; 327 break; 328 case "email": 329 $menu_tags = "href='mailto:".$submenu_item_link."'"; 330 break; 331 } 332 333 if ($menu_item_level == 0) { 334 $menu_html = "<ul class='menu_main'>\n"; 335 $menu_html .= "<li>\n"; 336 if (!isset($_SESSION["username"])) { 337 $_SESSION["username"] = ''; 338 } 339 if (strlen($_SESSION["username"]) == 0) { 340 $menu_html .= "<a $menu_tags style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n"; 341 } 342 else { 343 if ($submenu_item_link == "/login.php" || $submenu_item_link == "/users/signup.php") { 344 //hide login and sign-up when the user is logged in 345 } 346 else { 347 if (strlen($submenu_item_link) == 0) { 348 $menu_html .= "<h2 align='center' style=''>".$menu_item_title."</h2>\n"; 349 } 350 else { 351 $menu_html .= "<a ".$menu_tags." style='padding: 0px 0px; border-style: none; background: none;'><h2 align='center' style=''>".$menu_item_title."</h2></a>\n"; 352 } 353 } 354 } 355 } 356 357 if (is_array($menu_field['menu_items']) && count($menu_field['menu_items']) > 0) { 358 $menu_html .= $this->build_child_html($menu_item_level, $menu_field['menu_items']); 359 } 360 361 if ($menu_item_level == 0) { 362 $menu_html .= "</li>\n"; 363 $menu_html .= "</ul>\n\n"; 364 } 365 366 $menu_html_full .= $menu_html; 367 } //end for each 368 } 369 370 return $menu_html_full; 371 } //end function 372 373 //create the sub menus 374 private function build_child_html($menu_item_level, $submenu_array) { 375 376 $db = $this->db; 377 $menu_item_level = $menu_item_level+1; 378 379 if (count($_SESSION['groups']) == 0) { 380 $_SESSION['groups'][0]['group_name'] = 'public'; 381 } 382 383 if (is_array($submenu_array)) { 384 //child menu found 385 $submenu_html = "<ul class='menu_sub'>\n"; 386 387 foreach($submenu_array as $submenu_field) { 388 //set the variables 389 $menu_item_link = $submenu_field['menu_item_link']; 390 $menu_item_category = $submenu_field['menu_item_category']; 391 $menu_items = $submenu_field['menu_items']; 392 393 //prepare the protected menus 394 $menu_item_title = ($submenu_field['menu_item_protected'] == "true") ? $submenu_field['menu_item_title'] : $submenu_field['menu_language_title']; 395 396 //prepare the menu_tags according to the category 397 switch ($menu_item_category) { 398 case "internal": 399 $menu_tags = "href='".PROJECT_PATH.$menu_item_link."'"; 400 break; 401 case "external": 402 if (substr($menu_item_link, 0,1) == "/") { 403 $menu_item_link = PROJECT_PATH.$menu_item_link; 404 } 405 $menu_tags = "href='".$menu_item_link."' target='_blank'"; 406 break; 407 case "email": 408 $menu_tags = "href='mailto:".$menu_item_link."'"; 409 break; 410 } 411 412 $submenu_html .= "<li>"; 413 414 //get sub menu for children 415 if (is_array($menu_items) && count($menu_items) > 0) { 416 $str_child_menu = $this->build_child_html($menu_item_level, $menu_items); 417 } 418 419 if (strlen($str_child_menu) > 1) { 420 $submenu_html .= "<a ".$menu_tags.">".$menu_item_title."</a>"; 421 $submenu_html .= $str_child_menu; 422 unset($str_child_menu); 423 } 424 else { 425 $submenu_html .= "<a ".$menu_tags.">".$menu_item_title."</a>"; 426 } 427 $submenu_html .= "</li>\n"; 428 } 429 unset($submenu_array); 430 431 $submenu_html .="</ul>\n"; 432 433 return $submenu_html; 434 } 435 } //end function 436 437 //create the menu array 438 public function menu_array($sql = '', $menu_item_level = 0) { 439 440 //get the database connnection 441 $db = $this->db; 442 443 //database object does not exist return immediately 444 if (!$db) { return Array(); } 445 446 //if there are no groups then set the public group 447 if (!isset($_SESSION['groups'][0]['group_name'])) { 448 $_SESSION['groups'][0]['group_name'] = 'public'; 449 } 450 451 //get the menu from the database 452 if (strlen($sql) == 0) { 453 $sql = "select i.menu_item_link, l.menu_item_title as menu_language_title, ". 454 $sql .= "i.menu_item_title, i.menu_item_protected, i.menu_item_category, "; 455 $sql .= "i.menu_item_icon, i.menu_item_uuid, i.menu_item_parent_uuid "; 456 $sql .= "from v_menu_items as i, v_menu_languages as l "; 457 $sql .= "where i.menu_item_uuid = l.menu_item_uuid "; 458 $sql .= "and l.menu_language = '".$_SESSION['domain']['language']['code']."' "; 459 $sql .= "and l.menu_uuid = '".$this->menu_uuid."' "; 460 $sql .= "and i.menu_uuid = '".$this->menu_uuid."' "; 461 $sql .= "and i.menu_item_parent_uuid is null "; 462 $sql .= "and i.menu_item_uuid in "; 463 $sql .= "(select menu_item_uuid from v_menu_item_groups where menu_uuid = '".$this->menu_uuid."' "; 464 $sql .= "and ( "; 465 $x = 0; 466 foreach($_SESSION['groups'] as $row) { 467 if ($x == 0) { 468 $sql .= "group_name = '".$row['group_name']."' "; 469 } 470 else { 471 $sql .= "or group_name = '".$row['group_name']."' "; 472 } 473 $x++; 474 } 475 $sql .= ") "; 476 $sql .= "and menu_item_uuid is not null "; 477 $sql .= ") "; 478 $sql .= "order by i.menu_item_order asc "; 479 } 480 $prep_statement = $db->prepare(check_sql($sql)); 481 $prep_statement->execute(); 482 $result = $prep_statement->fetchAll(PDO::FETCH_NAMED); 483 484 //save the menu into an array 485 $x = 0; 486 $a = Array(); 487 if (is_array($result)) { 488 foreach($result as $row) { 489 //add the row to the array 490 $a[$x] = $row; 491 492 //add the sub menus to the array 493 $menu_item_level = 0; 494 if (strlen($row['menu_item_uuid']) > 0) { 495 $a[$x]['menu_items'] = $this->menu_child_array($menu_item_level, $row['menu_item_uuid']); 496 } 497 498 //increment the row number 499 $x++; 500 } //end for each 501 } 502 503 //unset the variables 504 unset($prep_statement, $sql, $result); 505 506 //return the array 507 return $a; 508 } //end function 509 510 //create the sub menus 511 private function menu_child_array($menu_item_level, $menu_item_uuid) { 512 513 //get the database connnection 514 $db = $this->db; 515 516 //database ojbect does not exist return immediately 517 if (!$db) { return; } 518 519 //set the level 520 $menu_item_level = $menu_item_level+1; 521 522 //if there are no groups then set the public group 523 if (!isset($_SESSION['groups'][0]['group_name'])) { 524 $_SESSION['groups'][0]['group_name'] = 'public'; 525 } 526 527 //get the child menu from the database 528 $sql = "select i.menu_item_link, l.menu_item_title as menu_language_title, i.menu_item_title, i.menu_item_protected, i.menu_item_category, i.menu_item_icon, i.menu_item_uuid, i.menu_item_parent_uuid "; 529 $sql .= "from v_menu_items as i, v_menu_languages as l "; 530 $sql .= "where i.menu_item_uuid = l.menu_item_uuid "; 531 $sql .= "and l.menu_language = '".$_SESSION['domain']['language']['code']."' "; 532 $sql .= "and l.menu_uuid = '".$this->menu_uuid."' "; 533 $sql .= "and i.menu_uuid = '".$this->menu_uuid."' "; 534 $sql .= "and i.menu_item_parent_uuid = '$menu_item_uuid' "; 535 $sql .= "and i.menu_item_uuid in "; 536 $sql .= "(select menu_item_uuid from v_menu_item_groups where menu_uuid = '".$this->menu_uuid."' "; 537 $sql .= "and ( "; 538 $x = 0; 539 foreach($_SESSION['groups'] as $row) { 540 if ($x == 0) { 541 $sql .= "group_name = '".$row['group_name']."' "; 542 } 543 else { 544 $sql .= "or group_name = '".$row['group_name']."' "; 545 } 546 $x++; 547 } 548 $sql .= ") "; 549 $sql .= ") "; 550 $sql .= "order by l.menu_item_title, i.menu_item_order asc "; 551 $sub_prep_statement = $db->prepare($sql); 552 $sub_prep_statement->execute(); 553 $sub_result = $sub_prep_statement->fetchAll(PDO::FETCH_NAMED); 554 555 //save the child menu into an array 556 if (is_array($sub_result)) { 557 foreach($sub_result as $row) { 558 //set the variables 559 $menu_item_link = $row['menu_item_link']; 560 $menu_item_category = $row['menu_item_category']; 561 $menu_item_icon = $row['menu_item_icon']; 562 $menu_item_uuid = $row['menu_item_uuid']; 563 $menu_item_parent_uuid = $row['menu_item_parent_uuid']; 564 565 //add the row to the array 566 $a[$x] = $row; 567 568 //prepare the protected menus 569 if ($row['menu_item_protected'] == "true") { 570 $a[$x]['menu_item_title'] = $row['menu_item_title']; 571 } 572 else { 573 $a[$x]['menu_item_title'] = $row['menu_language_title']; 574 } 575 576 //get sub menu for children 577 if (strlen($menu_item_uuid) > 0) { 578 $a[$x]['menu_items'] = $this->menu_child_array($menu_item_level, $menu_item_uuid); 579 //$str_child_menu = 580 } 581 582 //increment the row 583 $x++; 584 } 585 unset($sql, $sub_result); 586 return $a; 587 } 588 unset($sub_prep_statement, $sql); 589 } //end function 590 591 //add the default menu when no menu exists 592 public function menu_default() { 593 //set the default menu_uuid 594 $this->menu_uuid = 'b4750c3f-2a86-b00d-b7d0-345c14eca286'; 595 //check to see if any menu exists 596 $sql = "select count(*) as count from v_menus "; 597 $sql .= "where menu_uuid = '".$this->menu_uuid."' "; 598 $prep_statement = $this->db->prepare(check_sql($sql)); 599 $prep_statement->execute(); 600 $result = $prep_statement->fetch(PDO::FETCH_NAMED); 601 unset($sql, $prep_statement); 602 if ($result['count'] == 0) { 603 //set the menu variables 604 $menu_name = 'default'; 605 $menu_language = 'en-us'; 606 $menu_description = 'Default Menu'; 607 608 //add the menu 609 $sql = "insert into v_menus "; 610 $sql .= "("; 611 $sql .= "menu_uuid, "; 612 $sql .= "menu_name, "; 613 $sql .= "menu_language, "; 614 $sql .= "menu_description "; 615 $sql .= ") "; 616 $sql .= "values "; 617 $sql .= "("; 618 $sql .= "'".$this->menu_uuid."', "; 619 $sql .= "'$menu_name', "; 620 $sql .= "'$menu_language', "; 621 $sql .= "'$menu_description' "; 622 $sql .= ");"; 623 $this->db->exec($sql); 624 625 //add the menu items 626 $this->restore(); 627 } 628 } //end function 629 } 630} 631 632?> 633