| [ PHPXref.com ] | [ Generated: Sun Jul 20 18:59:55 2008 ] | [ NS Mamu 1.0.0 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @version $Id: gacl_api.class.php, v1.0 2006/01/20 07:52 NS Mamu Team Exp $ 4 * @package NS Mamu 5 * @Copyright (C) 2003 - 2006 NS Network Organization. All right reserved. 6 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL 7 * NS Mamu is Free Software 8 */ 9 10 /* 11 * phpGACL - Generic Access Control List 12 * Copyright (C) 2002,2003 Mike Benoit 13 * 14 * This library is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU Lesser General Public 16 * License as published by the Free Software Foundation; either 17 * version 2.1 of the License, or (at your option) any later version. 18 * 19 * This library is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 * Lesser General Public License for more details. 23 * 24 * You should have received a copy of the GNU Lesser General Public 25 * License along with this library; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 27 * 28 * For questions, help, comments, discussion, etc., please join the 29 * phpGACL mailing list. http://sourceforge.net/mail/?group_id=57103 30 * 31 * You may contact the author of phpGACL by e-mail at: 32 * ipso@snappymail.ca 33 * 34 * The latest version of phpGACL can be obtained from: 35 * http://phpgacl.sourceforge.net/ 36 * 37 */ 38 39 /** ensure this file is being included by a parent file */ 40 defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); 41 42 /* 43 * 44 * 45 * == If you find a feature may be missing from this API, please email me: ipso@snappymail.ca and I will be happy to add it. == 46 * 47 * 48 * Example: 49 * $gacl_api = new gacl_api; 50 * 51 * $section_id = $gacl_api->get_aco_section_id('System'); 52 * $aro_id= $gacl_api->add_aro($section_id, 'John Doe', 10); 53 * 54 * For more examples, see the Administration interface, as it makes use of nearly every API Call. 55 * 56 */ 57 58 class gacl_api extends gacl { 59 /* 60 * Administration interface settings 61 */ 62 var $_items_per_page = 100; 63 var $_max_select_box_items = 100; 64 var $_max_search_return_items = 100; 65 66 /* 67 * 68 * Misc helper functions. 69 * 70 */ 71 72 /*======================================================================*\ 73 Function: showarray() 74 Purpose: Dump all contents of an array in HTML (kinda). 75 \*======================================================================*/ 76 function showarray($array) { 77 echo "<br><pre>\n"; 78 var_dump($array); 79 echo "</pre><br>\n"; 80 } 81 82 /*======================================================================*\ 83 Function: $gacl_api->return_page() 84 Purpose: Sends the user back to a passed URL, unless debug is enabled, then we don't redirect. 85 If no URL is passed, try the REFERER 86 \*======================================================================*/ 87 function return_page($url="") { 88 global $_SERVER, $debug; 89 90 if (empty($url) AND !empty($_SERVER[HTTP_REFERER])) { 91 $this->debug_text("return_page(): URL not set, using referer!"); 92 $url = $_SERVER[HTTP_REFERER]; 93 } 94 95 if (!$debug OR $debug==0) { 96 header("Location: $url\n\n"); 97 } else { 98 $this->debug_text("return_page(): URL: $url -- Referer: $_SERVER[HTTP_REFERRER]"); 99 } 100 } 101 102 /*======================================================================*\ 103 Function: get_paging_data() 104 Purpose: Creates a basic array for Smarty to deal with paging large recordsets. 105 Pass it the ADODB recordset. 106 \*======================================================================*/ 107 function get_paging_data($rs) { 108 return array( 109 'prevpage' => $rs->absolutepage() - 1, 110 'currentpage' => $rs->absolutepage(), 111 'nextpage' => $rs->absolutepage() + 1, 112 'atfirstpage' => $rs->atfirstpage(), 113 'atlastpage' => $rs->atlastpage(), 114 'lastpageno' => $rs->lastpageno() 115 ); 116 } 117 118 /*======================================================================*\ 119 Function: count_all() 120 Purpose: Recursively counts elements in an array and sub-arrays. 121 The returned count is a count of all scalar elements found. 122 123 This is different from count($arg, COUNT_RECURSIVE) 124 in PHP >= 4.2.0, which includes sub-arrays in the count. 125 \*======================================================================*/ 126 function count_all($arg = NULL) { 127 switch (TRUE) { 128 case is_scalar($arg): 129 case is_object($arg): 130 // single object 131 return 1; 132 case is_array($arg): 133 // call recursively for all elements of $arg 134 $count = 0; 135 foreach ($arg as $val) { 136 $count += $this->count_all($val); 137 } 138 return $count; 139 } 140 return FALSE; 141 } 142 143 /*======================================================================*\ 144 Function: get_version() 145 Purpose: Grabs phpGACL version from the database. 146 \*======================================================================*/ 147 148 149 /*======================================================================*\ 150 Function: get_schema_version() 151 Purpose: Grabs phpGACL schema version from the database. 152 \*======================================================================*/ 153 /* 154 * 155 * ACL 156 * 157 */ 158 159 /*======================================================================*\ 160 Function: consolidated_edit_acl() 161 Purpose: Add's an ACL but checks to see if it can consolidate it with another one first. 162 This ONLY works with ACO's and ARO's. Groups, and AXO are excluded. 163 As well this function is designed for handling ACLs with return values, 164 and consolidating on the return_value, in hopes of keeping the ACL count to a minimum. 165 166 A return value of false must _always_ be handled outside this function. 167 As this function will remove AROs from ACLs and return false, in most cases 168 you will need to a create a completely new ACL on a false return. 169 \*======================================================================*/ 170 171 /*======================================================================*\ 172 Function: shift_acl() 173 Purpose: Opposite of append_acl(). Removes objects from a specific ACL. (named after PHP's array_shift()) 174 \*======================================================================*/ 175 176 /*======================================================================*\ 177 Function: get_acl() 178 Purpose: Grabs ACL data. 179 \*======================================================================*/ 180 181 /*======================================================================*\ 182 Function: is_conflicting_acl() 183 Purpose: Checks for conflicts when adding a specific ACL. 184 \*======================================================================*/ 185 186 /*======================================================================*\ 187 Function: add_acl() 188 Purpose: Add's an ACL. ACO_IDS, ARO_IDS, GROUP_IDS must all be arrays. 189 \*======================================================================*/ 190 191 /*======================================================================*\ 192 Function: edit_acl() 193 Purpose: Edit's an ACL, ACO_IDS, ARO_IDS, GROUP_IDS must all be arrays. 194 \*======================================================================*/ 195 196 /*======================================================================*\ 197 Function: del_acl() 198 Purpose: Deletes a given ACL 199 \*======================================================================*/ 200 201 202 /* 203 * 204 * Groups 205 * 206 */ 207 208 /*======================================================================*\ 209 Function: sort_groups() 210 Purpose: Grabs all the groups from the database doing preliminary grouping by parent 211 \*======================================================================*/ 212 213 /*======================================================================*\ 214 Function: format_groups() 215 Purpose: Takes the array returned by sort_groups() and formats for human consumption. 216 \*======================================================================*/ 217 218 /*======================================================================*\ 219 Function: get_group_id() 220 Purpose: Gets the group_id given the name. 221 Will only return one group id, so if there are duplicate names, it will return false. 222 \*======================================================================*/ 223 function get_group_id($name = null, $group_type = 'ARO') { 224 225 $this->debug_text("get_group_id(): Name: $name"); 226 227 switch(strtolower(trim($group_type))) { 228 case 'axo': 229 $table = $this->_db_table_prefix .'axo_groups'; 230 break; 231 default: 232 $table = $this->_db_table_prefix .'aro_groups'; 233 break; 234 } 235 236 $name = trim($name); 237 238 if (empty($name) ) { 239 $this->debug_text("get_group_id(): name ($name) is empty, this is required"); 240 return false; 241 } 242 243 $this->db->setQuery( "SELECT group_id FROM $table WHERE name='$name'" ); 244 245 $rows = $this->db->loadRowList(); 246 if ($this->db->getErrorNum()) { 247 $this->debug_db('get_group_id'); 248 return false; 249 } 250 251 $row_count = count( $rows ); 252 253 if ($row_count > 1) { 254 $this->debug_text("get_group_id(): Returned $row_count rows, can only return one. Please make your names unique."); 255 return false; 256 } 257 258 if ($row_count == 0) { 259 $this->debug_text("get_group_id(): Returned $row_count rows"); 260 return false; 261 } 262 263 $row = $rows[0]; 264 265 //Return the ID. 266 return $row[0]; 267 } 268 269 /*======================================================================*\ 270 Function: get_group_name() 271 Purpose: Gets the name given the group_id. 272 Will only return one group id, so if there are duplicate names, it will return false. 273 \*======================================================================*/ 274 function get_group_name($group_id = null, $group_type = 'ARO') { 275 276 $this->debug_text("get_group_name(): ID: $group_id"); 277 278 switch(strtolower(trim($group_type))) { 279 case 'axo': 280 $table = $this->_db_table_prefix .'axo_groups'; 281 break; 282 default: 283 $table = $this->_db_table_prefix .'aro_groups'; 284 break; 285 } 286 287 $group_id = intval($group_id); 288 289 if (!$group_id) { 290 $this->debug_text("get_group_name(): group_id ($group_id) is empty, this is required"); 291 return false; 292 } 293 294 $this->db->setQuery( "SELECT name FROM $table WHERE group_id='$group_id'" ); 295 296 $rows = $this->db->loadRowList(); 297 if ($this->db->getErrorNum()) { 298 $this->debug_db('get_group_name'); 299 return false; 300 } 301 302 $row_count = count( $rows ); 303 304 if ($row_count > 1) { 305 $this->debug_text("get_group_name(): Returned $row_count rows, can only return one. Please make your names unique."); 306 return false; 307 } 308 309 if ($row_count == 0) { 310 $this->debug_text("get_group_name(): Returned $row_count rows"); 311 return false; 312 } 313 314 $row = $rows[0]; 315 316 //Return the ID. 317 return $row[0]; 318 } 319 320 /*======================================================================*\ 321 Function: get_group_children() 322 Purpose: Gets a groups child IDs 323 \*======================================================================*/ 324 function get_group_children($group_id, $group_type = 'ARO', $recurse = 'NO_RECURSE') { 325 $this->debug_text("get_group_children(): Group_ID: $group_id Group Type: $group_type Recurse: $recurse"); 326 327 switch (strtolower(trim($group_type))) { 328 case 'axo': 329 $group_type = 'axo'; 330 $table = $this->_db_table_prefix .'axo_groups'; 331 break; 332 default: 333 $group_type = 'aro'; 334 $table = $this->_db_table_prefix .'aro_groups'; 335 } 336 337 if (empty($group_id)) { 338 $this->debug_text("get_group_children(): ID ($group_id) is empty, this is required"); 339 return FALSE; 340 } 341 342 $query = ' 343 SELECT g1.group_id 344 FROM '. $table .' g1'; 345 346 //FIXME-mikeb: Why is group_id in quotes? 347 switch (strtoupper($recurse)) { 348 case 'RECURSE': 349 $query .= ' 350 LEFT JOIN '. $table .' g2 ON g2.lft<g1.lft AND g2.rgt>g1.rgt 351 WHERE g2.group_id='. $group_id; 352 break; 353 default: 354 $query .= ' 355 WHERE g1.parent_id='. $group_id; 356 } 357 358 $query .= ' 359 ORDER BY g1.name'; 360 361 362 $this->db->setQuery( $query ); 363 return $this->db->loadResultArray(); 364 } 365 366 /*======================================================================*\ 367 Function: get_group_data() 368 Purpose: Gets the group data given the GROUP_ID. 369 \*======================================================================*/ 370 371 /*======================================================================*\ 372 Function: get_group_parent_id() 373 Purpose: Grabs the parent_id of a given group 374 \*======================================================================*/ 375 376 /*======================================================================*\ 377 Function: get_group_children() 378 Purpose: Gets a groups child IDs 379 \*======================================================================*/ 380 function get_group_parents($group_id, $group_type = 'ARO', $recurse = 'NO_RECURSE') { 381 $this->debug_text("get_group_parents(): Group_ID: $group_id Group Type: $group_type Recurse: $recurse"); 382 383 switch (strtolower(trim($group_type))) { 384 case 'axo': 385 $group_type = 'axo'; 386 $table = $this->_db_table_prefix .'axo_groups'; 387 break; 388 default: 389 $group_type = 'aro'; 390 $table = $this->_db_table_prefix .'aro_groups'; 391 } 392 393 if (empty($group_id)) { 394 $this->debug_text("get_group_parents(): ID ($group_id) is empty, this is required"); 395 return FALSE; 396 } 397 398 $query = ' 399 SELECT g2.group_id 400 FROM '. $table .' g1'; 401 402 //FIXME-mikeb: Why is group_id in quotes? 403 switch (strtoupper($recurse)) { 404 case 'RECURSE': 405 $query .= ' 406 LEFT JOIN '. $table .' g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt 407 WHERE g1.group_id='. $group_id; 408 break; 409 case 'RECURSE_INCL': 410 // inclusive resurse 411 $query .= ' 412 LEFT JOIN '. $table .' g2 ON g1.lft >= g2.lft AND g1.lft <= g2.rgt 413 WHERE g1.group_id='. $group_id; 414 break; 415 default: 416 $query .= ' 417 WHERE g1.parent_id='. $group_id; 418 } 419 420 $query .= ' 421 ORDER BY g2.lft'; 422 423 424 $this->db->setQuery( $query ); 425 return $this->db->loadResultArray(); 426 } 427 428 /*======================================================================*\ 429 Function: get_root_group_id () 430 Purpose: Grabs the id of the root group for the specified tree 431 \*======================================================================*/ 432 433 /*======================================================================*\ 434 Function: map_path_to_root() 435 Purpose: Maps a unique path to root to a specific group. Each group can only have 436 one path to root. 437 \*======================================================================*/ 438 /** REMOVED **/ 439 /*======================================================================*\ 440 Function: put_path_to_root() 441 Purpose: Writes the unique path to root to the database. There should really only be 442 one path to root for each level "deep" the groups go. If the groups are branched 443 10 levels deep, there should only be 10 unique path to roots. These of course 444 overlap each other more and more the closer to the root/trunk they get. 445 \*======================================================================*/ 446 /** REMOVED **/ 447 /*======================================================================*\ 448 Function: clean_path_to_root() 449 Purpose: Cleans up any paths that are not being used. 450 \*======================================================================*/ 451 /** REMOVED **/ 452 /*======================================================================*\ 453 Function: get_path_to_root() 454 Purpose: Generates the path to root for a given group. 455 \*======================================================================*/ 456 /** REMOVED **/ 457 458 /*======================================================================*\ 459 Function: add_group() 460 Purpose: Inserts a group, defaults to be on the "root" branch. 461 \*======================================================================*/ 462 function add_group($name, $parent_id=0, $group_type='ARO') { 463 464 switch(strtolower(trim($group_type))) { 465 case 'axo': 466 $group_type = 'axo'; 467 $table = $this->_db_table_prefix .'axo_groups'; 468 break; 469 default: 470 $group_type = 'aro'; 471 $table = $this->_db_table_prefix .'aro_groups'; 472 break; 473 } 474 475 $this->debug_text("add_group(): Name: $name Parent ID: $parent_id Group Type: $group_type"); 476 477 $name = trim($name); 478 479 if (empty($name)) { 480 $this->debug_text("add_group(): name ($name) OR parent id ($parent_id) is empty, this is required"); 481 return false; 482 } 483 484 //This has to be outside the transaction, because the first time it is run, it will say the sequence 485 //doesn't exist. Then try to create it, but the transaction will already by aborted by then. 486 //$insert_id = $this->db->GenID($this->_db_table_prefix.$group_type.'_groups_id_seq',10); 487 $this->db->setQuery( "SELECT MAX(group_id)+1 FROM $table" ); 488 $insert_id = intval( $this->db->loadResult() ); 489 490 // <mos> $this->db->BeginTrans(); 491 492 // special case for root group 493 if ($parent_id == 0) { 494 // check a root group is not already defined 495 $$this->db->setQuery( 'SELECT group_id FROM '. $table .' WHERE parent_id=0' ); 496 $rs = $this->db->loadResultArray(); 497 498 if (!is_array( $rs )) { 499 $this->debug_db('add_group'); 500 $this->db->RollBackTrans(); 501 return FALSE; 502 } 503 504 if (count( $rs ) > 0) { 505 $this->debug_text('add_group (): A root group already exists.'); 506 // <mos> $this->db->RollBackTrans(); 507 return FALSE; 508 } 509 510 $parent_lft = 0; 511 $parent_rgt = 1; 512 } else { 513 if (empty($parent_id)) { 514 $this->debug_text("add_group (): parent id ($parent_id) is empty, this is required"); 515 return FALSE; 516 } 517 518 // grab parent details from database 519 $this->db->setQuery( 'SELECT group_id, lft, rgt FROM '. $table .' WHERE group_id='. $parent_id ); 520 $rows = $this->db->loadRowList(); 521 522 if (!is_array($rows) OR $this->db->getErrorNum() > 0) { 523 $this->debug_db('add_group'); 524 // <mos> $this->db->RollBackTrans(); 525 return FALSE; 526 } 527 528 if (empty($rows)) { 529 $this->debug_text('add_group (): Parent ID: '. $parent_id .' not found.'); 530 // <mos> $this->db->RollBackTrans(); 531 return FALSE; 532 } 533 $row = $rows[0]; 534 $parent_lft = &$row[1]; 535 $parent_rgt = &$row[2]; 536 537 // make room for the new group 538 $this->db->setQuery( 'UPDATE '. $table .' SET rgt=rgt+2 WHERE rgt>='. $parent_rgt ); 539 $rs = $this->db->query(); 540 541 if (!$rs) { 542 $this->debug_db('add_group: make room for the new group - right'); 543 // <mos> $this->db->RollBackTrans(); 544 return FALSE; 545 } 546 547 $this->db->setQuery( 'UPDATE '. $table .' SET lft=lft+2 WHERE lft>'. $parent_rgt ); 548 $rs = $this->db->query($query); 549 550 if (!$rs) { 551 $this->debug_db('add_group: make room for the new group - left'); 552 // <mos> $this->db->RollBackTrans(); 553 return FALSE; 554 } 555 } 556 557 $this->db->setQuery( 'INSERT INTO '. $table .' (group_id,parent_id,name,lft,rgt) VALUES ('. $insert_id .','. $parent_id .',\''. $this->db->getEscaped($name) .'\','. $parent_rgt .','. ($parent_rgt + 1) .')' ); 558 $rs = $this->db->query($query); 559 560 if (!$rs) { 561 $this->debug_db('add_group: insert record'); 562 // <mos> $this->db->RollBackTrans(); 563 return FALSE; 564 } 565 566 // <mos> $this->db->CommitTrans(); 567 568 $this->debug_text('add_group (): Added group as ID: '. $insert_id); 569 return $insert_id; 570 } 571 572 /*======================================================================*\ 573 Function: get_group_objects() 574 Purpose: Gets all objects assigned to a group. 575 If $option == 'RECURSE' it will get all objects in child groups as well. 576 defaults to omit child groups. 577 \*======================================================================*/ 578 function get_group_objects($group_id, $group_type='ARO', $option='NO_RECURSE') { 579 580 switch(strtolower(trim($group_type))) { 581 case 'axo': 582 $group_type = 'axo'; 583 $object_table = $this->_db_table_prefix .'axo'; 584 $group_table = $this->_db_table_prefix .'axo_groups'; 585 $map_table = $this->_db_table_prefix .'groups_axo_map'; 586 break; 587 default: 588 $group_type = 'aro'; 589 $object_table = $this->_db_table_prefix .'aro'; 590 $group_table = $this->_db_table_prefix .'aro_groups'; 591 $map_table = $this->_db_table_prefix .'groups_aro_map'; 592 break; 593 } 594 595 $this->debug_text("get_group_objects(): Group ID: $group_id"); 596 597 if (empty($group_id)) { 598 $this->debug_text("get_group_objects(): Group ID: ($group_id) is empty, this is required"); 599 return false; 600 } 601 602 $query = ' 603 SELECT o.section_value,o.value 604 FROM '. $object_table .' o 605 LEFT JOIN '. $map_table .' gm ON o.'. $group_type .'_id=gm.'. $group_type .'_id'; 606 607 if ($option == 'RECURSE') { 608 $query .= ' 609 LEFT JOIN '. $group_table .' g1 ON g1.group_id=gm.group_id 610 LEFT JOIN '. $group_table .' g2 ON g2.lft<=g1.lft AND g2.rgt>=g1.rgt 611 WHERE g2.group_id='. $group_id; 612 } else { 613 $query .= ' 614 WHERE gm.group_id='. $group_id; 615 } 616 617 $this->db->setQuery( $query ); 618 619 $rs = $this->db->loadRowList(); 620 621 if (!is_array( $rs )) { 622 $this->debug_db('get_group_objects'); 623 return false; 624 } 625 626 $this->debug_text("get_group_objects(): Got group objects, formatting array."); 627 628 $retarr = array(); 629 630 //format return array. 631 foreach ($rs as $row) { 632 $section = &$row[0]; 633 $value = &$row[1]; 634 635 $retarr[$section][] = $value; 636 } 637 638 return $retarr; 639 } 640 641 /*======================================================================*\ 642 Function: add_group_object() 643 Purpose: Assigns an Object to a group 644 \*======================================================================*/ 645 function add_group_object($group_id, $object_section_value, $object_value, $group_type='ARO') { 646 647 switch(strtolower(trim($group_type))) { 648 case 'axo': 649 $group_type = 'axo'; 650 $table = $this->_db_table_prefix .'groups_axo_map'; 651 $object_table = $this->_db_table_prefix .'axo'; 652 $group_table = $this->_db_table_prefix .'axo_groups'; 653 break; 654 default: 655 $group_type = 'aro'; 656 $table = $this->_db_table_prefix .'groups_aro_map'; 657 $object_table = $this->_db_table_prefix .'aro'; 658 $group_table = $this->_db_table_prefix .'aro_groups'; 659 break; 660 } 661 662 $this->debug_text("add_group_object(): Group ID: $group_id, Section Value: $object_section_value, Value: $object_value, Group Type: $group_type"); 663 664 $object_section_value = trim($object_section_value); 665 $object_value = trim($object_value); 666 667 if (empty($group_id) OR empty($object_value) OR empty($object_section_value)) { 668 $this->debug_text("add_group_object(): Group ID: ($group_id) OR Value ($object_value) OR Section value ($object_section_value) is empty, this is required"); 669 return false; 670 } 671 672 // test to see if object & group exist and if object is already a member 673 $this->db->setQuery( ' 674 SELECT g.group_id,o.'. $group_type .'_id,gm.group_id AS member 675 FROM '. $object_table .' o 676 LEFT JOIN '. $group_table .' g ON g.group_id='. $group_id .' 677 LEFT JOIN '. $table .' gm ON (gm.group_id=g.group_id AND gm.'. $group_type .'_id=o.'. $group_type .'_id) 678 WHERE (o.section_value=\''. $this->db->getEscaped($object_section_value) .'\' AND o.value=\''. $this->db->getEscaped($object_value) .'\')' 679 ); 680 681 $rows = $this->db->loadRowList(); 682 if ($this->db->getErrorNum()) { 683 $this->debug_db('add_group_object'); 684 return FALSE; 685 } 686 687 if (count( $rows ) != 1) { 688 $this->debug_text("add_group_object (): Group ID ($group_id) OR Value ($object_value) OR Section value ($object_section_value) is invalid. Does this object exist?"); 689 return FALSE; 690 } 691 692 $row = $rows[0]; 693 694 if ($row[2] == 1) { 695 $this->debug_text("add_group_object (): Object: $object_value is already a member of Group ID: $group_id"); 696 //Object is already assigned to group. Return true. 697 return true; 698 } 699 700 $object_id = $row[1]; 701 702 $this->db->setQuery( 'INSERT INTO '. $table .' (group_id,'. $group_type .'_id) VALUES ('. $group_id .','. $object_id .')' ); 703 704 if (!$this->db->query()) { 705 $this->debug_db('add_group_object'); 706 return FALSE; 707 } 708 709 $this->debug_text('add_group_object(): Added Object: '. $object_id .' to Group ID: '. $group_id); 710 711 if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE) { 712 //Expire all cache. 713 $this->Cache_Lite->clean('default'); 714 } 715 716 return TRUE; 717 } 718 719 /*======================================================================*\ 720 Function: del_group_object() 721 Purpose: Removes an Object from a group. 722 \*======================================================================*/ 723 function del_group_object($group_id, $object_section_value, $object_value, $group_type='ARO') { 724 725 switch(strtolower(trim($group_type))) { 726 case 'axo': 727 $group_type = 'axo'; 728 $table = $this->_db_table_prefix .'groups_axo_map'; 729 break; 730 default: 731 $group_type = 'aro'; 732 $table = $this->_db_table_prefix .'groups_aro_map'; 733 break; 734 } 735 736 $this->debug_text("del_group_object(): Group ID: $group_id Section value: $object_section_value Value: $object_value"); 737 738 $object_section_value = trim($object_section_value); 739 $object_value = trim($object_value); 740 741 if (empty($group_id) OR empty($object_value) OR empty($object_section_value)) { 742 $this->debug_text("del_group_object(): Group ID: ($group_id) OR Section value: $object_section_value OR Value ($object_value) is empty, this is required"); 743 return false; 744 } 745 746 if (!$object_id = $this->get_object_id($object_section_value, $object_value, $group_type)) { 747 $this->debug_text ("del_group_object (): Group ID ($group_id) OR Value ($object_value) OR Section value ($object_section_value) is invalid. Does this object exist?"); 748 return FALSE; 749 } 750 751 $this->db->setQuery( 'DELETE FROM '. $table .' WHERE group_id='. $group_id .' AND '. $group_type .'_id='. $object_id ); 752 $this->db->query(); 753 754 if ($this->db->getErrorNum()) { 755 $this->debug_db('del_group_object'); 756 return false; 757 } 758 759 $this->debug_text("del_group_object(): Deleted Value: $object_value to Group ID: $group_id assignment"); 760 761 if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE) { 762 //Expire all cache. 763 $this->Cache_Lite->clean('default'); 764 } 765 766 return true; 767 } 768 769 /*======================================================================*\ 770 Function: edit_group() 771 Purpose: Edits a group 772 \*======================================================================*/ 773 774 /*======================================================================*\ 775 Function: rebuild_tree () 776 Purpose: rebuilds the group tree for the given type 777 \*======================================================================*/ 778 779 /*======================================================================*\ 780 Function: del_group() 781 Purpose: deletes a given group 782 \*======================================================================*/ 783 function del_group($group_id, $reparent_children=TRUE, $group_type='ARO') { 784 785 switch(strtolower(trim($group_type))) { 786 case 'axo': 787 $group_type = 'axo'; 788 $table = $this->_db_table_prefix .'axo_groups'; 789 $groups_map_table = $this->_db_table_prefix .'axo_groups_map'; 790 $groups_object_map_table = $this->_db_table_prefix .'groups_axo_map'; 791 break; 792 default: 793 $group_type = 'aro'; 794 $table = $this->_db_table_prefix .'aro_groups'; 795 $groups_map_table = $this->_db_table_prefix .'aro_groups_map'; 796 $groups_object_map_table = $this->_db_table_prefix .'groups_aro_map'; 797 break; 798 } 799 800 $this->debug_text("del_group(): ID: $group_id Reparent Children: $reparent_children Group Type: $group_type"); 801 802 if (empty($group_id) ) { 803 $this->debug_text("del_group(): Group ID ($group_id) is empty, this is required"); 804 return false; 805 } 806 807 // Get details of this group 808 $this->db->setQuery( 'SELECT group_id, parent_id, name, lft, rgt FROM '. $table .' WHERE group_id='. $group_id ); 809 $group_details = $this->db->loadRow($query); 810 811 if (!is_array($group_details)) { 812 $this->debug_db('del_group: get group details'); 813 return false; 814 } 815 816 $parent_id = $group_details[1]; 817 818 $left = $group_details[3]; 819 $right = $group_details[4]; 820 821 // <mos> $this->db->BeginTrans(); 822 823 // grab list of all children 824 $children_ids = $this->get_group_children($group_id, $group_type, 'RECURSE'); 825 826 // prevent deletion of root group & reparent of children if it has more than one immediate child 827 if ($parent_id == 0) { 828 $this->db->setQuery( 'SELECT count(*) FROM '. $table .' WHERE parent_id='. $group_id ); 829 $child_count = $this->db->loadResult($query); 830 831 if ($child_count > 1 && $reparent_children) { 832 $this->debug_text ('del_group (): You cannot delete the root group and reparent children, this would create multiple root groups.'); 833 return FALSE; 834 } 835 } 836 837 $success = FALSE; 838 839 /* 840 * Handle children here. 841 */ 842 switch (TRUE) { 843 // there are no child groups, just delete group 844 case !is_array($children_ids): 845 case count($children_ids) == 0: 846 // remove acl maps 847 /* Reapply when ACL's implemented 848 $this->db->setQuery( 'DELETE FROM '. $groups_map_table .' WHERE group_id='. $group_id ); 849 $rs = $this->db->Execute($query); 850 851 if (!is_object($rs)) { 852 break; 853 }*/ 854 855 // remove group object maps 856 $this->db->setQuery( 'DELETE FROM '. $groups_object_map_table .' WHERE group_id='. $group_id ); 857 $rs = $this->db->query(); 858 859 if (!$rs) { 860 break; 861 } 862 863 // remove group 864 $this->db->setQuery( 'DELETE FROM '. $table .' WHERE group_id='. $group_id ); 865 $rs = $this->db->query(); 866 867 if (!$rs) { 868 break; 869 } 870 871 // move all groups right of deleted group left by width of deleted group 872 $this->db->setQuery( 'UPDATE '. $table .' SET lft=lft-'. ($right-$left+1) .' WHERE lft>'. $right ); 873 $rs = $this->db->query(); 874 875 if (!$rs) { 876 break; 877 } 878 879 $this->db->setQuery( 'UPDATE '. $table .' SET rgt=rgt-'. ($right-$left+1) .' WHERE rgt>'. $right ); 880 $rs = $this->db->query(); 881 882 if (!$rs) { 883 break; 884 } 885 886 $success = TRUE; 887 break; 888 case $reparent_children == TRUE: 889 // remove acl maps 890 /* Reapply when ACL's implemented 891 $query = 'DELETE FROM '. $groups_map_table .' WHERE group_id='. $group_id; 892 $rs = $this->db->Execute($query); 893 894 if (!is_object($rs)) { 895 break; 896 }*/ 897 898 // remove group object maps 899 $this->db->setQuery( 'DELETE FROM '. $groups_object_map_table .' WHERE group_id='. $group_id ); 900 $rs = $this->db->query(); 901 902 if (!$rs) { 903 break; 904 } 905 906 // remove group 907 $this->db->setQuery( 'DELETE FROM '. $table .' WHERE group_id='. $group_id ); 908 $rs = $this->db->query(); 909 910 if (!$rs) { 911 break; 912 } 913 914 // set parent of immediate children to parent group 915 $this->db->setQuery( 'UPDATE '. $table .' SET parent_id='. $parent_id .' WHERE parent_id='. $group_id ); 916 $rs = $this->db->query(); 917 918 if (!$rs) { 919 break; 920 } 921 922 // move all children left by 1 923 $this->db->setQuery( 'UPDATE '. $table .' SET lft=lft-1, rgt=rgt-1 WHERE lft>'. $left .' AND rgt<'. $right ); 924 $rs = $this->db->query(); 925 926 if (!$rs) { 927 break; 928 } 929 930 // move all groups right of deleted group left by 2 931 $this->db->setQuery( 'UPDATE '. $table .' SET lft=lft-2 WHERE lft>'. $right ); 932 $rs = $this->db->query(); 933 934 if (!$rs) { 935 break; 936 } 937 938 $this->db->setQuery( 'UPDATE '. $table .' SET rgt=rgt-2 WHERE rgt>'. $right ); 939 $rs = $this->db->query(); 940 941 if (!$rs) { 942 break; 943 } 944 945 $success = TRUE; 946 break; 947 default: 948 // make list of group and all children 949 $group_ids = $children_ids; 950 $group_ids[] = $group_id; 951 952 // remove acl maps 953 /* Reapply when ACL's implemented 954 $query = 'DELETE FROM '. $groups_map_table .' WHERE group_id IN ('. implode (',', $group_ids) .')'; 955 $rs = $this->db->Execute($query); 956 957 if (!is_object($rs)) { 958 break; 959 }*/ 960 961 // remove group object maps 962 $this->db->setQuery( 'DELETE FROM '. $groups_object_map_table .' WHERE group_id IN ('. implode (',', $group_ids) .')' ); 963 $rs = $this->db->query(); 964 965 if (!$rs) { 966 break; 967 } 968 969 // remove groups 970 $this->db->setQuery( 'DELETE FROM '. $table .' WHERE group_id IN ('. implode (',', $group_ids) .')' ); 971 $rs = $this->db->query(); 972 973 if (!$rs) { 974 break; 975 } 976 977 // move all groups right of deleted group left by width of deleted group 978 $this->db->setQuery( 'UPDATE '. $table .' SET lft=lft-'. ($right - $left + 1) .' WHERE lft>'. $right ); 979 $rs = $this->db->query(); 980 981 if (!$rs) { 982 break; 983 } 984 985 $this->db->setQuery( 'UPDATE '. $table .' SET rgt=rgt-'. ($right - $left + 1) .' WHERE rgt>'. $right ); 986 $rs = $this->db->query(); 987 988 if (!$rs) { 989 break; 990 } 991 992 $success = TRUE; 993 } 994 995 // if the delete failed, rollback the trans and return false 996 if (!$success) { 997 998 $this->debug_db('del_group'); 999 $this->db->RollBackTrans(); 1000 return false; 1001 } 1002 1003 $this->debug_text("del_group(): deleted group ID: $group_id"); 1004 // <mos> $this->db->CommitTrans(); 1005 1006 if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE) { 1007 //Expire all cache. 1008 $this->Cache_Lite->clean('default'); 1009 } 1010 1011 return true; 1012 1013 } 1014 1015 1016 /* 1017 * 1018 * Objects (ACO/ARO/AXO) 1019 * 1020 */ 1021 /*======================================================================*\ 1022 Function: get_object() 1023 Purpose: Grabs all Objects's in the database, or specific to a section_value 1024 \*======================================================================*/ 1025 function get_object($section_value = null, $return_hidden=1, $object_type=NULL) { 1026 1027 switch(strtolower(trim($object_type))) { 1028 case 'aco': 1029 $object_type = 'aco'; 1030 $table = $this->_db_table_prefix .'aco'; 1031 break; 1032 case 'aro': 1033 $object_type = 'aro'; 1034 $table = $this->_db_table_prefix .'aro'; 1035 break; 1036 case 'axo': 1037 $object_type = 'axo'; 1038 $table = $this->_db_table_prefix .'axo'; 1039 break; 1040 default: 1041 $this->debug_text('get_object(): Invalid Object Type: '. $object_type); 1042 return FALSE; 1043 } 1044 1045 $this->debug_text("get_object(): Section Value: $section_value Object Type: $object_type"); 1046 1047 $$this->db->setQuery( 'SELECT '. $object_type .'_id FROM '. $table ); 1048 1049 $where = array(); 1050 1051 if (!empty($section_value)) { 1052 $where[] = 'section_value='. $this->db->getEscaped($section_value); 1053 } 1054 1055 if ($return_hidden==0) { 1056 $where[] = 'hidden=0'; 1057 } 1058 1059 if (!empty($where)) { 1060 $query .= ' WHERE '. implode(' AND ', $where); 1061 } 1062 1063 $rs = $this->db->loadResultArray(); 1064 1065 if (!is_array($rs)) { 1066 $this->debug_db('get_object'); 1067 return false; 1068 } 1069 1070 // Return Object IDs 1071 return $rs; 1072 } 1073 1074 /*======================================================================*\ 1075 Function: get_objects () 1076 Purpose: Grabs all Objects in the database, or specific to a section_value 1077 returns format suitable for add_acl and is_conflicting_acl 1078 \*======================================================================*/ 1079 1080 /*======================================================================*\ 1081 Function: get_object_data() 1082 Purpose: Gets all data pertaining to a specific Object. 1083 \*======================================================================*/ 1084 1085 /*======================================================================*\ 1086 Function: get_object_groups() 1087 Purpose: Gets the group_id's for the given the section_value AND value 1088 of the object. 1089 \*======================================================================*/ 1090 function get_object_groups($object_section_value, $object_value, $object_type=NULL) { 1091 1092 switch(strtolower(trim($object_type))) { 1093 case 'aro': 1094 $group_type = 'aro'; 1095 $table = $this->_db_table_prefix .'groups_aro_map'; 1096 $object_table = $this->_db_table_prefix .'aro'; 1097 $group_table = $this->_db_table_prefix .'aro_groups'; 1098 break; 1099 case 'axo': 1100 $group_type = 'axo'; 1101 $table = $this->_db_table_prefix .'groups_axo_map'; 1102 $object_table = $this->_db_table_prefix .'axo'; 1103 $group_table = $this->_db_table_prefix .'axo_groups'; 1104 break; 1105 default: 1106 $this->debug_text('get_object_groups(): Invalid Object Type: '. $object_type); 1107 return FALSE; 1108 } 1109 1110 $this->debug_text("get_object_groups(): Section Value: $object_section_value Value: $object_value Object Type: $object_type"); 1111 1112 $object_section_value = trim($object_section_value); 1113 $object_value = trim($object_value); 1114 1115 if (empty($object_section_value) AND empty($object_value) ) { 1116 $this->debug_text("get_object_groups(): Section Value ($object_section_value) AND value ($object_value) is empty, this is required"); 1117 return false; 1118 } 1119 1120 if (empty($object_type) ) { 1121 $this->debug_text("get_object_groups(): Object Type ($object_type) is empty, this is required"); 1122 return false; 1123 } 1124 // SELECT g.group_id,o.'. $group_type .'_id,(gm.group_id IS NOT NULL) AS member 1125 1126 $this->db->setQuery( ' 1127 SELECT g.group_id,o.'. $group_type .'_id,(gm.group_id IS NOT NULL) AS member 1128 FROM '. $group_table .' g 1129 LEFT JOIN '. $table .' gm ON gm.group_id=g.group_id 1130 LEFT JOIN '. $object_table .' o ON o.'. $group_type .'_id = gm.'. $group_type .'_id 1131 WHERE (o.section_value=\''. $this->db->getEscaped($object_section_value) .'\' AND o.value=\''. $this->db->getEscaped($object_value) .'\')' 1132 ); 1133 $rs = $this->db->loadResultArray(); 1134 1135 if ($this->db->getErrorNum()) { 1136 $this->debug_db('get_object_id'); 1137 return false; 1138 } 1139 1140 //Return the array of group id's 1141 return $rs; 1142 } 1143 1144 /*======================================================================*\ 1145 Function: get_object_id() 1146 Purpose: Gets the object_id given the section_value AND value of the object. 1147 \*======================================================================*/ 1148 function get_object_id($section_value, $value, $object_type=NULL) { 1149 1150 switch(strtolower(trim($object_type))) { 1151 case 'aco': 1152 $object_type = 'aco'; 1153 $table = $this->_db_table_prefix .'aco'; 1154 break; 1155 case 'aro': 1156 $object_type = 'aro'; 1157 $table = $this->_db_table_prefix .'aro'; 1158 break; 1159 case 'axo': 1160 $object_type = 'axo'; 1161 $table = $this->_db_table_prefix .'axo'; 1162 break; 1163 default: 1164 $this->debug_text('get_object_id(): Invalid Object Type: '. $object_type); 1165 return FALSE; 1166 } 1167 1168 $this->debug_text("get_object_id(): Section Value: $section_value Value: $value Object Type: $object_type"); 1169 1170 $section_value = trim($section_value); 1171 $value = trim($value); 1172 1173 if (empty($section_value) AND empty($value) ) { 1174 $this->debug_text("get_object_id(): Section Value ($value) AND value ($value) is empty, this is required"); 1175 return false; 1176 } 1177 1178 if (empty($object_type) ) { 1179 $this->debug_text("get_object_id(): Object Type ($object_type) is empty, this is required"); 1180 return false; 1181 } 1182 1183 $this->db->setQuery( 'SELECT '. $object_type .'_id FROM '. $table .' WHERE section_value=\''. $this->db->getEscaped($section_value) .'\' AND value=\''. $this->db->getEscaped($value) .'\'' 1184 ); 1185 $rs = $this->db->loadRowList(); 1186 1187 if ($this->db->getErrorNum()) { 1188 $this->debug_db('get_object_id'); 1189 return false; 1190 } 1191 1192 $row_count = count( $rs ); 1193 1194 if ($row_count > 1) { 1195 $this->debug_text("get_object_id(): Returned $row_count rows, can only return one. This should never happen, the database may be missing a unique key."); 1196 return false; 1197 } 1198 1199 if ($row_count == 0) { 1200 $this->debug_text("get_object_id(): Returned $row_count rows"); 1201 return false; 1202 } 1203 1204 $row = $rs[0]; 1205 1206 //Return the ID. 1207 return $row[0]; 1208 } 1209 1210 /*======================================================================*\ 1211 Function: get_object_section_value() 1212 Purpose: Gets the object_section_value given object id 1213 \*======================================================================*/ 1214 1215 /*======================================================================*\ 1216 Function: get_object_groups() 1217 Purpose: Gets all groups an object is a member of. 1218 If $option == 'RECURSE' it will get all ancestor groups. 1219 defaults to only get direct parents. 1220 \*======================================================================*/ 1221 1222 /*======================================================================*\ 1223 Function: add_object() 1224 Purpose: Inserts a new object 1225 \*======================================================================*/ 1226 function add_object($section_value, $name, $value=0, $order=0, $hidden=0, $object_type=NULL) { 1227 1228 switch(strtolower(trim($object_type))) { 1229 case 'aco': 1230 $object_type = 'aco'; 1231 $table = $this->_db_table_prefix .'aco'; 1232 $object_sections_table = $this->_db_table_prefix .'aco_sections'; 1233 break; 1234 case 'aro': 1235 $object_type = 'aro'; 1236 $table = $this->_db_table_prefix .'aro'; 1237 $object_sections_table = $this->_db_table_prefix .'aro_sections'; 1238 break; 1239 case 'axo': 1240 $object_type = 'axo'; 1241 $table = $this->_db_table_prefix .'axo'; 1242 $object_sections_table = $this->_db_table_prefix .'axo_sections'; 1243 break; 1244 default: 1245 $this->debug_text('add_object(): Invalid Object Type: '. $object_type); 1246 return FALSE; 1247 } 1248 1249 $this->debug_text("add_object(): Section Value: $section_value Value: $value Order: $order Name: $name Object Type: $object_type"); 1250 1251 $section_value = trim($section_value); 1252 $name = trim($name); 1253 $value = trim($value); 1254 $order = trim($order); 1255 1256 if ($order == NULL OR $order == '') { 1257 $order = 0; 1258 } 1259 1260 if (empty($name) OR empty($section_value) ) { 1261 $this->debug_text("add_object(): name ($name) OR section value ($section_value) is empty, this is required"); 1262 return false; 1263 } 1264 1265 if (strlen($name) >= 255 OR strlen($value) >= 230 ) { 1266 $this->debug_text("add_object(): name ($name) OR value ($value) is too long."); 1267 return false; 1268 } 1269 1270 if (empty($object_type) ) { 1271 $this->debug_text("add_object(): Object Type ($object_type) is empty, this is required"); 1272 return false; 1273 } 1274 1275 // Test to see if the section is invalid or object already exists. 1276 $this->db->setQuery( ' 1277 SELECT (o.'. $object_type .'_id IS NOT NULL) AS object_exists 1278 FROM '. $object_sections_table .' s 1279 LEFT JOIN '. $table .' o ON (s.value=o.section_value AND o.value=\''. $this->db->getEscaped($value) .'\') 1280 WHERE s.value=\''. $this->db->getEscaped($section_value). '\'' 1281 ); 1282 1283 $rows = $this->db->loadRowList(); 1284 if ($this->db->getErrorNum()) { 1285 $this->debug_db('add_object'); 1286 return FALSE; 1287 } 1288 1289 if (count( $rows ) != 1) { 1290 // Section is invalid 1291 $this->debug_text("add_object(): Section Value: $section_value Object Type ($object_type) does not exist, this is required"); 1292 return false; 1293 } 1294 1295 $row = $rows[0]; 1296 1297 if ($row[0] == 1) { 1298 //Object is already created. 1299 return true; 1300 } 1301 1302 $insert_id = $this->db->GenID($this->_db_table_prefix.$object_type.'_seq',10); 1303 $this->db->setQuery( "INSERT INTO $table ({$object_type}_id,section_value,value,order_value,name,hidden) VALUES($insert_id,'$section_value','$value','$order','$name','$hidden')" ); 1304 1305 if (!$this->db->query()) { 1306 $this->debug_db('add_object'); 1307 return false; 1308 } 1309 1310 $insert_id = $this->db->insertid(); 1311 $this->debug_text("add_object(): Added object as ID: $insert_id"); 1312 return $insert_id; 1313 } 1314 /*======================================================================*\ 1315 Function: edit_object() 1316 Purpose: Edits a given Object 1317 \*======================================================================*/ 1318 function edit_object($object_id, $section_value, $name, $value=0, $order=0, $hidden=0, $object_type=NULL) { 1319 1320 switch(strtolower(trim($object_type))) { 1321 case 'aco': 1322 $object_type = 'aco'; 1323 $table = $this->_db_table_prefix .'aco'; 1324 $object_map_table = 'aco_map'; 1325 break; 1326 case 'aro': 1327 $object_type = 'aro'; 1328 $table = $this->_db_table_prefix .'aro'; 1329 $object_map_table = 'aro_map'; 1330 break; 1331 case 'axo': 1332 $object_type = 'axo'; 1333 $table = $this->_db_table_prefix .'axo'; 1334 $object_map_table = 'axo_map'; 1335 break; 1336 } 1337 1338 $this->debug_text("edit_object(): ID: $object_id, Section Value: $section_value, Value: $value, Order: $order, Name: $name, Object Type: $object_type"); 1339 1340 $section_value = trim($section_value); 1341 $name = trim($name); 1342 $value = trim($value); 1343 $order = trim($order); 1344 1345 if (empty($object_id) OR empty($section_value) ) { 1346 $this->debug_text("edit_object(): Object ID ($object_id) OR Section Value ($section_value) is empty, this is required"); 1347 return false; 1348 } 1349 1350 if (empty($name) ) { 1351 $this->debug_text("edit_object(): name ($name) is empty, this is required"); 1352 return false; 1353 } 1354 1355 if (empty($object_type) ) { 1356 $this->debug_text("edit_object(): Object Type ($object_type) is empty, this is required"); 1357 return false; 1358 } 1359 1360 //Get old value incase it changed, before we do the update. 1361 $this->db->setQuery( 'SELECT value, section_value FROM '. $table .' WHERE '. $object_type .'_id='. $object_id ); 1362 $old = $this->db->loadRow(); 1363 1364 $this->db->setQuery( ' 1365 UPDATE '. $table .' 1366 SET section_value=\''. $this->db->getEscaped($section_value) .'\', 1367 value='. $this->db->getEscaped($value) .', 1368 order_value='. $this->db->getEscaped($order) .', 1369 name=\''. $this->db->getEscaped($name) .'\', 1370 hidden='. $hidden .' 1371 WHERE '. $object_type .'_id='. $object_id 1372 ); 1373 $this->db->query(); 1374 1375 if (!$this->db->getErrorNum()) { 1376 $this->debug_db('edit_object'); 1377 return false; 1378 } 1379 1380 $this->debug_text('edit_object(): Modified '. strtoupper($object_type) .' ID: '. $object_id); 1381 1382 if ($old[0] != $value OR $old[1] != $section_value) { 1383 $this->debug_text("edit_object(): Value OR Section Value Changed, update other tables."); 1384 1385 $this->db->setQuery( ' 1386 UPDATE '. $object_map_table .' 1387 SET value=\''. $this->db->getEscaped($value) .'\', 1388 section_value=\''. $this->db->getEscaped($section_value) .'\' 1389 WHERE section_value=\''. $this->db->getEscaped($old[1]) .'\' 1390 AND value='. $this->db->getEscaped($old[0]) 1391 ); 1392 $this->db->query(); 1393 1394 if (!$this->db->getErrorNum()) { 1395 $this->debug_db('edit_object'); 1396 return FALSE; 1397 } 1398 1399 $this->debug_text ('edit_object(): Modified Map Value: '. $value .' Section Value: '. $section_value); 1400 } 1401 1402 return TRUE; 1403 } 1404 1405 /*======================================================================*\ 1406 Function: del_object() 1407 Purpose: Deletes a given Object and, if instructed to do so, 1408 erase all referencing objects 1409 ERASE feature by: Martino Piccinato 1410 \*======================================================================*/ 1411 function del_object($object_id, $object_type=NULL, $erase=FALSE) { 1412 1413 switch(strtolower(trim($object_type))) { 1414 case 'aco': 1415 $object_type = 'aco'; 1416 $table = $this->_db_table_prefix .'aco'; 1417 $object_map_table = $this->_db_table_prefix .'aco_map'; 1418 break; 1419 case 'aro': 1420 $object_type = 'aro'; 1421 $table = $this->_db_table_prefix .'aro'; 1422 $object_map_table = $this->_db_table_prefix .'aro_map'; 1423 $groups_map_table = $this->_db_table_prefix .'aro_groups_map'; 1424 $object_group_table = $this->_db_table_prefix .'groups_aro_map'; 1425 break; 1426 case 'axo': 1427 $object_type = 'axo'; 1428 $table = $this->_db_table_prefix .'axo'; 1429 $object_map_table = $this->_db_table_prefix .'axo_map'; 1430 $groups_map_table = $this->_db_table_prefix .'axo_groups_map'; 1431 $object_group_table = $this->_db_table_prefix .'groups_axo_map'; 1432 break; 1433 default: 1434 $this->debug_text('del_object(): Invalid Object Type: '. $object_type); 1435 return FALSE; 1436 } 1437 1438 $this->debug_text("del_object(): ID: $object_id Object Type: $object_type, Erase all referencing objects: $erase"); 1439 1440 if (empty($object_id) ) { 1441 $this->debug_text("del_object(): Object ID ($object_id) is empty, this is required"); 1442 return false; 1443 } 1444 1445 if (empty($object_type) ) { 1446 $this->debug_text("del_object(): Object Type ($object_type) is empty, this is required"); 1447 return false; 1448 } 1449 1450 // <mos> $this->db->BeginTrans(); 1451 1452 // Get Object section_value/value (needed to look for referencing objects) 1453 $this->db->setQuery( 'SELECT section_value,value FROM '. $table .' WHERE '. $object_type .'_id='. $object_id ); 1454 $object = $this->db->loadRow(); 1455 1456 if (empty($object)) { 1457 $this->debug_text('del_object(): The specified object ('. strtoupper($object_type) .' ID: '. $object_id .') could not be found.<br />SQL = '.$this->db->stderr()); 1458 return FALSE; 1459 } 1460 1461 $section_value = $object[0]; 1462 $value = $object[1]; 1463 1464 // Get ids of acl referencing the Object (if any) 1465 $this->db->setQuery( "SELECT acl_id FROM $object_map_table WHERE value='$value' AND section_value='$section_value'" ); 1466 $acl_ids = $this->db->loadResultArray(); 1467 1468 if ($erase) { 1469 // We were asked to erase all acl referencing it 1470 1471 $this->debug_text("del_object(): Erase was set to TRUE, delete all referencing objects"); 1472 1473 if ($object_type == "aro" OR $object_type == "axo") { 1474 // The object can be referenced in groups_X_map tables 1475 // in the future this branching may become useless because 1476 // ACO might me "groupable" too 1477 1478 // Get rid of groups_map referencing the Object 1479 $this->db->setQuery( 'DELETE FROM '. $object_group_table .' WHERE '. $object_type .'_id='. $object_id ); 1480 $rs = $this->db->query(); 1481 1482 if (!$rs) { 1483 $this->debug_db('edit_object'); 1484 // <mos> $this->db->RollBackTrans(); 1485 return false; 1486 } 1487 } 1488 1489 if ($acl_ids) { 1490 //There are acls actually referencing the object 1491 1492 if ($object_type == 'aco') { 1493 // I know it's extremely dangerous but 1494 // if asked to really erase an ACO 1495 // we should delete all acl referencing it 1496 // (and relative maps) 1497 1498 // Do this below this branching 1499 // where it uses $orphan_acl_ids as 1500 // the array of the "orphaned" acl 1501 // in this case all referenced acl are 1502 // orhpaned acl 1503 1504 $orphan_acl_ids = $acl_ids; 1505 } else { 1506 // The object is not an ACO and might be referenced 1507 // in still valid acls regarding also other object. 1508 // In these cases the acl MUST NOT be deleted 1509 1510 // Get rid of $object_id map referencing erased objects 1511 $this->db->setQuery( "DELETE FROM $object_map_table WHERE section_value='$section_value' AND value='$value'" ); 1512 $rs = $this->db->query($query); 1513 1514 if (!$rs) { 1515 $this->debug_db('edit_object'); 1516 $this->db->RollBackTrans(); 1517 return false; 1518 } 1519 1520 // Find the "orphaned" acl. I mean acl referencing the erased Object (map) 1521 // not referenced anymore by other objects 1522 1523 $sql_acl_ids = implode(",", $acl_ids); 1524 1525 $this->db->setQuery( ' 1526 SELECT a.id 1527 FROM '. $this->_db_table_prefix .'acl a 1528 LEFT JOIN '. $object_map_table .' b ON a.id=b.acl_id 1529 './* <mos return for full acl stuff> LEFT JOIN '. $groups_map_table .' c ON a.id=c.acl_id*/' 1530 WHERE value IS NULL 1531 AND section_value IS NULL 1532 AND group_id IS NULL 1533 AND a.id in ('. $sql_acl_ids .')'); 1534 $orphan_acl_ids = $this->db->loadResultArray(); 1535 1536 } // End of else section of "if ($object_type == "aco")" 1537 1538 if ($orphan_acl_ids) { 1539 // If there are orphaned acls get rid of them 1540 1541 foreach ($orphan_acl_ids as $acl) { 1542 $this->del_acl($acl); 1543 } 1544 } 1545 1546 } // End of if ($acl_ids) 1547 1548 // Finally delete the Object itself 1549 $this->db->setQuery( "DELETE FROM $table WHERE {$object_type}_id='$object_id'" ); 1550 $rs = $this->db->query(); 1551 1552 if (!$rs) { 1553 $this->debug_db('edit_object'); 1554 // <mos> $this->db->RollBackTrans(); 1555 return false; 1556 } 1557 1558 // <mos> $this->db->CommitTrans(); 1559 return true; 1560 1561 } // End of "if ($erase)" 1562 1563 $groups_ids = FALSE; 1564 1565 if ($object_type == 'axo' OR $object_type == 'aro') { 1566 // If the object is "groupable" (may become unnecessary, 1567 // see above 1568 1569 // Get id of groups where the object is assigned: 1570 // you must explicitly remove the object from its groups before 1571 // deleting it (don't know if this is really needed, anyway it's safer ;-) 1572 1573 $this->db->setQuery( 'SELECT group_id FROM '. $object_group_table .' WHERE '. $object_type .'_id='. $object_id ); 1574 $groups_ids = $this->db->loadResultArray(); 1575 } 1576 1577 if ( ( isset($acl_ids) AND $acl_ids !== FALSE ) OR ( isset($groups_ids) AND $groups_ids !== FALSE) ) { 1578 // The Object is referenced somewhere (group or acl), can't delete it 1579 1580 $this->debug_text("del_object(): Can't delete the object as it is being referenced by GROUPs (".@implode($group_ids).") or ACLs (".@implode($acl_ids,",").")"); 1581 1582 return false; 1583 } else { 1584 // The Object is NOT referenced anywhere, delete it 1585 1586 $this->db->setQuery( "DELETE FROM $table WHERE {$object_type}_id='$object_id'" ); 1587 $this->db->query(); 1588 1589 if ( $this->db->getErrorNum() ) { 1590 $this->debug_db('edit_object'); 1591 // <mos> $this->db->RollBackTrans(); 1592 return false; 1593 } 1594 1595 // <mos> $this->db->CommitTrans(); 1596 return true; 1597 } 1598 1599 return false; 1600 } 1601 1602 /* 1603 * 1604 * Object Sections 1605 * 1606 */ 1607 1608 /*======================================================================*\ 1609 Function: get_object_section_section_id() 1610 Purpose: Gets the object_section_id given the name AND/OR value of the section. 1611 Will only return one section id, so if there are duplicate names it will return false. 1612 \*======================================================================*/ 1613 1614 /*======================================================================*\ 1615 Function: add_object_section() 1616 Purpose: Inserts an object Section 1617 \*======================================================================*/ 1618 1619 /*======================================================================*\ 1620 Function: edit_object_section() 1621 Purpose: Edits a given Object Section 1622 \*======================================================================*/ 1623 1624 /*======================================================================*\ 1625 Function: del_object_section() 1626 Purpose: Deletes a given Object Section and, if explicitly 1627 asked, all the section objects 1628 ERASE feature by: Martino Piccinato 1629 \*======================================================================*/ 1630 1631 /* 1632 * 1633 * Mambo Utility Methods 1634 * 1635 */ 1636 1637 /*======================================================================*\ 1638 Function: has_group_parent 1639 Purpose: Checks whether the 'source' group is a child of the 'target' 1640 \*======================================================================*/ 1641 function is_group_child_of( $grp_src, $grp_tgt, $group_type='ARO' ) { 1642 $this->debug_text("has_group_parent(): Source=$grp_src, Target=$grp_tgt, Type=$group_type"); 1643 1644 switch(strtolower(trim($group_type))) { 1645 case 'axo': 1646 $table = $this->_db_table_prefix .'axo_groups'; 1647 break; 1648 default: 1649 $table = $this->_db_table_prefix .'aro_groups'; 1650 break; 1651 } 1652 1653 if (is_int( $grp_src ) && is_int($grp_tgt)) { 1654 $this->db->setQuery( "SELECT COUNT(*)" 1655 . "\nFROM $table AS g1" 1656 . "\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt" 1657 . "\nWHERE g1.group_id=$grp_src AND g2.group_id=$grp_tgt" 1658 ); 1659 } else if (is_string( $grp_src ) && is_string($grp_tgt)) { 1660 $this->db->setQuery( "SELECT COUNT(*)" 1661 . "\nFROM $table AS g1" 1662 . "\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt" 1663 . "\nWHERE g1.name='$grp_src' AND g2.name='$grp_tgt'" 1664 ); 1665 } else if (is_int( $grp_src ) && is_string($grp_tgt)) { 1666 $this->db->setQuery( "SELECT COUNT(*)" 1667 . "\nFROM $table AS g1" 1668 . "\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt" 1669 . "\nWHERE g1.group_id='$grp_src' AND g2.name='$grp_tgt'" 1670 ); 1671 } else { 1672 $this->db->setQuery( "SELECT COUNT(*)" 1673 . "\nFROM $table AS g1" 1674 . "\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt" 1675 . "\nWHERE g1.name=$grp_src AND g2.group_id='$grp_tgt'" 1676 ); 1677 } 1678 1679 return $this->db->loadResult(); 1680 } 1681 1682 function getAroGroup( $value ) { 1683 return $this->_getGroup( 'aro', $value ); 1684 } 1685 1686 function _getGroup( $type, $value ) { 1687 global $database; 1688 1689 $database->setQuery( "SELECT g.*" 1690 . "\nFROM #__core_acl_{$type}_groups AS g" 1691 . "\nINNER JOIN #__core_acl_groups_{$type}_map AS gm ON gm.group_id = g.group_id" 1692 . "\nINNER JOIN #__core_acl_{$type} AS ao ON ao.{$type}_id = gm.{$type}_id" 1693 . "\nWHERE ao.value='$value'" 1694 ); 1695 $obj = null; 1696 $database->loadObject( $obj ); 1697 return $obj; 1698 } 1699 1700 function _getAbove() { 1701 } 1702 1703 function _getBelow( $table, $fields, $groupby=null, $root_id=null, $root_name=null, $inclusive=true ) { 1704 global $database; 1705 1706 $root = new stdClass(); 1707 $root->lft = 0; 1708 $root->rgt = 0; 1709 1710 if ($root_id) { 1711 } else if ($root_name) { 1712 $database->setQuery( "SELECT lft, rgt FROM $table WHERE name='$root_name'" ); 1713 $database->loadObject( $root ); 1714 } 1715 1716 $where = ''; 1717 if ($root->lft+$root->rgt <> 0) { 1718 if ($inclusive) { 1719 $where = "WHERE g1.lft BETWEEN $root->lft AND $root->rgt"; 1720 } else { 1721 $where = "WHERE g1.lft BETWEEN $root->lft+1 AND $root->rgt-1"; 1722 } 1723 } 1724 1725 $database->setQuery( "SELECT $fields" 1726 . "\nFROM $table AS g1" 1727 . "\nINNER JOIN $table AS g2 ON g1.lft BETWEEN g2.lft AND g2.rgt" 1728 . "\n$where" 1729 . ($groupby ? "\nGROUP BY $groupby" : "") 1730 . "\nORDER BY g1.lft" 1731 ); 1732 1733 //echo $database->getQuery(); 1734 return $database->loadObjectList(); 1735 } 1736 1737 function get_group_children_tree( $root_id=null, $root_name=null, $inclusive=true ) { 1738 global $database; 1739 1740 $tree = gacl_api::_getBelow( '#__core_acl_aro_groups', 1741 'g1.group_id, g1.name, COUNT(g2.name) AS level', 1742 'g1.name', 1743 $root_id, $root_name, $inclusive ); 1744 1745 // first pass get level limits 1746 $n = count( $tree ); 1747 $min = $tree[0]->level; 1748 $max = $tree[0]->level; 1749 for ($i=0; $i < $n; $i++) { 1750 $min = min( $min, $tree[$i]->level ); 1751 $max = max( $max, $tree[$i]->level ); 1752 } 1753 1754 $indents = array(); 1755 foreach (range( $min, $max ) as $i) { 1756 $indents[$i] = ' '; 1757 } 1758 // correction for first indent 1759 $indents[$min] = ''; 1760 1761 $list = array(); 1762 for ($i=$n-1; $i >= 0; $i--) { 1763 $shim = ''; 1764 foreach (range( $min, $tree[$i]->level ) as $j) { 1765 $shim .= $indents[$j]; 1766 } 1767 1768 if (@$indents[$tree[$i]->level+1] == '. ') { 1769 $twist = ' '; 1770 } else { 1771 $twist = "- "; 1772 } 1773 1774 //$list[$i] = $tree[$i]->level.$shim.$twist.$tree[$i]->name; 1775 $list[$i] = mosHTML::makeOption( $tree[$i]->group_id, $shim.$twist.$tree[$i]->name ); 1776 if ($tree[$i]->level < @$tree[$i-1]->level) { 1777 $indents[$tree[$i]->level+1] = '. '; 1778 } 1779 } 1780 1781 ksort($list); 1782 return $list; 1783 } 1784 } 1785 1786 class mosARO extends mosDBTable { 1787 /** @var int Primary key */ 1788 var $aro_id=null; 1789 var $section_value=null; 1790 var $value=null; 1791 var $order_value=null; 1792 var $name=null; 1793 var $hidden=null; 1794 1795 function mosARO( &$db ) { 1796 $this->mosDBTable( '#__core_acl_aro', 'aro_id', $db ); 1797 } 1798 1799 /** 1800 * Utility function for returning groups 1801 */ 1802 1803 } 1804 1805 class mosAroGroup extends mosDBTable { 1806 /** @var int Primary key */ 1807 var $group_id=null; 1808 var $parent_id=null; 1809 var $name=null; 1810 var $lft=null; 1811 var $rgt=null; 1812 1813 function mosAroGroup( &$db ) { 1814 $this->mosDBTable( '#__core_acl_aro_groups', 'group_id', $db ); 1815 } 1816 } 1817 1818 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |