Source for file gacl_api.class.php
Documentation is available at gacl_api.class.php
* @author Mambo Foundation Inc see README.php
* @copyright Mambo Foundation Inc.
* See COPYRIGHT.php for copyright notices and details.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see
* Mambo is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2 of the
* phpGACL - Generic Access Control List
* Copyright (C) 2002,2003 Mike Benoit
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* For questions, help, comments, discussion, etc., please join the
* phpGACL mailing list. http://sourceforge.net/mail/?group_id=57103
* You may contact the author of phpGACL by e-mail at:
* The latest version of phpGACL can be obtained from:
* http://phpgacl.sourceforge.net/
/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
* == 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. ==
* $gacl_api = new gacl_api;
* $section_id = $gacl_api->get_aco_section_id('System');
* $aro_id= $gacl_api->add_aro($section_id, 'John Doe', 10);
* For more examples, see the Administration interface, as it makes use of nearly every API Call.
* Administration interface settings
/*======================================================================*\
Purpose: Dump all contents of an array in HTML (kinda).
\*======================================================================*/
/*======================================================================*\
Function: $gacl_api->return_page()
Purpose: Sends the user back to a passed URL, unless debug is enabled, then we don't redirect.
If no URL is passed, try the REFERER
\*======================================================================*/
if (empty($url) AND !empty($_SERVER[HTTP_REFERER])) {
$this->debug_text("return_page(): URL not set, using referer!");
$url =
$_SERVER[HTTP_REFERER];
if (!$debug OR $debug==
0) {
$this->debug_text("return_page(): URL: $url -- Referer: $_SERVER[HTTP_REFERRER]");
/*======================================================================*\
Function: get_paging_data()
Purpose: Creates a basic array for Smarty to deal with paging large recordsets.
Pass it the ADODB recordset.
\*======================================================================*/
'prevpage' =>
$rs->absolutepage() -
1,
'currentpage' =>
$rs->absolutepage(),
'nextpage' =>
$rs->absolutepage() +
1,
'atfirstpage' =>
$rs->atfirstpage(),
'atlastpage' =>
$rs->atlastpage(),
'lastpageno' =>
$rs->lastpageno()
/*======================================================================*\
Purpose: Recursively counts elements in an array and sub-arrays.
The returned count is a count of all scalar elements found.
This is different from count($arg, COUNT_RECURSIVE)
in PHP >= 4.2.0, which includes sub-arrays in the count.
\*======================================================================*/
// call recursively for all elements of $arg
/*======================================================================*\
Purpose: Grabs phpGACL version from the database.
\*======================================================================*/
/*======================================================================*\
Function: get_schema_version()
Purpose: Grabs phpGACL schema version from the database.
\*======================================================================*/
/*======================================================================*\
Function: consolidated_edit_acl()
Purpose: Add's an ACL but checks to see if it can consolidate it with another one first.
This ONLY works with ACO's and ARO's. Groups, and AXO are excluded.
As well this function is designed for handling ACLs with return values,
and consolidating on the return_value, in hopes of keeping the ACL count to a minimum.
A return value of false must _always_ be handled outside this function.
As this function will remove AROs from ACLs and return false, in most cases
you will need to a create a completely new ACL on a false return.
\*======================================================================*/
/*======================================================================*\
Purpose: Opposite of append_acl(). Removes objects from a specific ACL. (named after PHP's array_shift())
\*======================================================================*/
/*======================================================================*\
\*======================================================================*/
/*======================================================================*\
Function: is_conflicting_acl()
Purpose: Checks for conflicts when adding a specific ACL.
\*======================================================================*/
/*======================================================================*\
Purpose: Add's an ACL. ACO_IDS, ARO_IDS, GROUP_IDS must all be arrays.
\*======================================================================*/
/*======================================================================*\
Purpose: Edit's an ACL, ACO_IDS, ARO_IDS, GROUP_IDS must all be arrays.
\*======================================================================*/
/*======================================================================*\
Purpose: Deletes a given ACL
\*======================================================================*/
/*======================================================================*\
Purpose: Grabs all the groups from the database doing preliminary grouping by parent
\*======================================================================*/
/*======================================================================*\
Function: format_groups()
Purpose: Takes the array returned by sort_groups() and formats for human consumption.
\*======================================================================*/
/*======================================================================*\
Purpose: Gets the group_id given the name.
Will only return one group id, so if there are duplicate names, it will return false.
\*======================================================================*/
$this->debug_text("get_group_id(): name ($name) is empty, this is required");
$this->db->setQuery( "SELECT group_id FROM $table WHERE name='$name'" );
$rows =
$this->db->loadRowList();
if ($this->db->getErrorNum()) {
$row_count =
count( $rows );
$this->debug_text("get_group_id(): Returned $row_count rows, can only return one. Please make your names unique.");
$this->debug_text("get_group_id(): Returned $row_count rows");
/*======================================================================*\
Function: get_group_name()
Purpose: Gets the name given the group_id.
Will only return one group id, so if there are duplicate names, it will return false.
\*======================================================================*/
$this->debug_text("get_group_name(): ID: $group_id");
$group_id =
intval($group_id);
$this->debug_text("get_group_name(): group_id ($group_id) is empty, this is required");
$this->db->setQuery( "SELECT name FROM $table WHERE group_id='$group_id'" );
$rows =
$this->db->loadRowList();
if ($this->db->getErrorNum()) {
$row_count =
count( $rows );
$this->debug_text("get_group_name(): Returned $row_count rows, can only return one. Please make your names unique.");
$this->debug_text("get_group_name(): Returned $row_count rows");
/*======================================================================*\
Function: get_group_children()
Purpose: Gets a groups child IDs
\*======================================================================*/
$this->debug_text("get_group_children(): Group_ID: $group_id Group Type: $group_type Recurse: $recurse");
$this->debug_text("get_group_children(): ID ($group_id) is empty, this is required");
//FIXME-mikeb: Why is group_id in quotes?
LEFT JOIN '.
$table .
' g2 ON g2.lft<g1.lft AND g2.rgt>g1.rgt
WHERE g2.group_id='.
$group_id;
WHERE g1.parent_id='.
$group_id;
$this->db->setQuery( $query );
return $this->db->loadResultArray();
/*======================================================================*\
Function: get_group_data()
Purpose: Gets the group data given the GROUP_ID.
\*======================================================================*/
/*======================================================================*\
Function: get_group_parent_id()
Purpose: Grabs the parent_id of a given group
\*======================================================================*/
/*======================================================================*\
Function: get_group_children()
Purpose: Gets a groups child IDs
\*======================================================================*/
function get_group_parents($group_id, $group_type =
'ARO', $recurse =
'NO_RECURSE') {
$this->debug_text("get_group_parents(): Group_ID: $group_id Group Type: $group_type Recurse: $recurse");
$this->debug_text("get_group_parents(): ID ($group_id) is empty, this is required");
//FIXME-mikeb: Why is group_id in quotes?
LEFT JOIN '.
$table .
' g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt
WHERE g1.group_id='.
$group_id;
LEFT JOIN '.
$table .
' g2 ON g1.lft >= g2.lft AND g1.lft <= g2.rgt
WHERE g1.group_id='.
$group_id;
WHERE g1.parent_id='.
$group_id;
$this->db->setQuery( $query );
return $this->db->loadResultArray();
/*======================================================================*\
Function: get_root_group_id ()
Purpose: Grabs the id of the root group for the specified tree
\*======================================================================*/
/*======================================================================*\
Function: map_path_to_root()
Purpose: Maps a unique path to root to a specific group. Each group can only have
\*======================================================================*/
\*======================================================================*/
\*======================================================================*/
\*======================================================================*/
\*======================================================================*/
function add_group($name, $parent_id=
0, $group_type=
'ARO') {
$this->debug_text("add_group(): Name: $name Parent ID: $parent_id Group Type: $group_type");
$this->debug_text("add_group(): name ($name) OR parent id ($parent_id) is empty, this is required");
//This has to be outside the transaction, because the first time it is run, it will say the sequence
//doesn't exist. Then try to create it, but the transaction will already by aborted by then.
//$insert_id = $this->db->GenID($this->_db_table_prefix.$group_type.'_groups_id_seq',10);
$this->db->setQuery( "SELECT MAX(group_id)+1 FROM $table" );
$insert_id =
intval( $this->db->loadResult() );
// <mos> $this->db->BeginTrans();
// special case for root group
// check a root group is not already defined
$this->db->setQuery( 'SELECT group_id FROM '.
$table .
' WHERE parent_id=0' );
$rs =
$this->db->loadResultArray();
$this->db->RollBackTrans();
$this->debug_text('add_group (): A root group already exists.');
// <mos> $this->db->RollBackTrans();
$this->debug_text("add_group (): parent id ($parent_id) is empty, this is required");
// grab parent details from database
$this->db->setQuery( 'SELECT group_id, lft, rgt FROM '.
$table .
' WHERE group_id='.
$parent_id );
$rows =
$this->db->loadRowList();
if (!is_array($rows) OR $this->db->getErrorNum() >
0) {
// <mos> $this->db->RollBackTrans();
$this->debug_text('add_group (): Parent ID: '.
$parent_id .
' not found.');
// <mos> $this->db->RollBackTrans();
// make room for the new group
$this->db->setQuery( 'UPDATE '.
$table .
' SET rgt=rgt+2 WHERE rgt>='.
$parent_rgt );
$rs =
$this->db->query();
$this->debug_db('add_group: make room for the new group - right');
// <mos> $this->db->RollBackTrans();
$this->db->setQuery( 'UPDATE '.
$table .
' SET lft=lft+2 WHERE lft>'.
$parent_rgt );
$rs =
$this->db->query();
$this->debug_db('add_group: make room for the new group - left');
// <mos> $this->db->RollBackTrans();
$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) .
')' );
$rs =
$this->db->query();
$this->debug_db('add_group: insert record');
// <mos> $this->db->RollBackTrans();
// <mos> $this->db->CommitTrans();
$this->debug_text('add_group (): Added group as ID: '.
$insert_id);
/*======================================================================*\
Function: get_group_objects()
Purpose: Gets all objects assigned to a group.
If $option == 'RECURSE' it will get all objects in child groups as well.
defaults to omit child groups.
\*======================================================================*/
$this->debug_text("get_group_objects(): Group ID: $group_id");
$this->debug_text("get_group_objects(): Group ID: ($group_id) is empty, this is required");
SELECT o.section_value,o.value
FROM '.
$object_table .
' o
LEFT JOIN '.
$map_table .
' gm ON o.'.
$group_type .
'_id=gm.'.
$group_type .
'_id';
if ($option ==
'RECURSE') {
LEFT JOIN '.
$group_table .
' g1 ON g1.group_id=gm.group_id
LEFT JOIN '.
$group_table .
' g2 ON g2.lft<=g1.lft AND g2.rgt>=g1.rgt
WHERE g2.group_id='.
$group_id;
WHERE gm.group_id='.
$group_id;
$this->db->setQuery( $query );
$rs =
$this->db->loadRowList();
$this->debug_text("get_group_objects(): Got group objects, formatting array.");
$retarr[$section][] =
$value;
/*======================================================================*\
Function: add_group_object()
Purpose: Assigns an Object to a group
\*======================================================================*/
function add_group_object($group_id, $object_section_value, $object_value, $group_type=
'ARO') {
$this->debug_text("add_group_object(): Group ID: $group_id, Section Value: $object_section_value, Value: $object_value, Group Type: $group_type");
$object_section_value =
trim($object_section_value);
$object_value =
trim($object_value);
if (empty($group_id) OR empty($object_value) OR empty($object_section_value)) {
$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");
// test to see if object & group exist and if object is already a member
SELECT g.group_id,o.'.
$group_type .
'_id,gm.group_id AS member
FROM '.
$object_table .
' o
LEFT JOIN '.
$group_table .
' g ON g.group_id='.
$group_id .
'
LEFT JOIN '.
$table .
' gm ON (gm.group_id=g.group_id AND gm.'.
$group_type .
'_id=o.'.
$group_type .
'_id)
WHERE (o.section_value=\''.
$this->db->getEscaped($object_section_value) .
'\' AND o.value=\''.
$this->db->getEscaped($object_value) .
'\')'
$rows =
$this->db->loadRowList();
if ($this->db->getErrorNum()) {
if (count( $rows ) !=
1) {
$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?");
$this->debug_text("add_group_object (): Object: $object_value is already a member of Group ID: $group_id");
//Object is already assigned to group. Return true.
$this->db->setQuery( 'INSERT INTO '.
$table .
' (group_id,'.
$group_type .
'_id) VALUES ('.
$group_id .
','.
$object_id .
')' );
if (!$this->db->query()) {
$this->debug_text('add_group_object(): Added Object: '.
$object_id .
' to Group ID: '.
$group_id);
$this->Cache_Lite->clean('default');
/*======================================================================*\
Function: del_group_object()
Purpose: Removes an Object from a group.
\*======================================================================*/
function del_group_object($group_id, $object_section_value, $object_value, $group_type=
'ARO') {
$this->debug_text("del_group_object(): Group ID: $group_id Section value: $object_section_value Value: $object_value");
$object_section_value =
trim($object_section_value);
$object_value =
trim($object_value);
if (empty($group_id) OR empty($object_value) OR empty($object_section_value)) {
$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");
if (!$object_id =
$this->get_object_id($object_section_value, $object_value, $group_type)) {
$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?");
$this->db->setQuery( 'DELETE FROM '.
$table .
' WHERE group_id='.
$group_id .
' AND '.
$group_type .
'_id='.
$object_id );
if ($this->db->getErrorNum()) {
$this->debug_text("del_group_object(): Deleted Value: $object_value to Group ID: $group_id assignment");
$this->Cache_Lite->clean('default');
/*======================================================================*\
\*======================================================================*/
/*======================================================================*\
Function: rebuild_tree ()
Purpose: rebuilds the group tree for the given type
\*======================================================================*/
/*======================================================================*\
Purpose: deletes a given group
\*======================================================================*/
function del_group($group_id, $reparent_children=
TRUE, $group_type=
'ARO') {
$this->debug_text("del_group(): ID: $group_id Reparent Children: $reparent_children Group Type: $group_type");
$this->debug_text("del_group(): Group ID ($group_id) is empty, this is required");
// Get details of this group
$this->db->setQuery( 'SELECT group_id, parent_id, name, lft, rgt FROM '.
$table .
' WHERE group_id='.
$group_id );
$group_details =
$this->db->loadRow($query);
$this->debug_db('del_group: get group details');
$parent_id =
$group_details[1];
$left =
$group_details[3];
$right =
$group_details[4];
// <mos> $this->db->BeginTrans();
// grab list of all children
// prevent deletion of root group & reparent of children if it has more than one immediate child
$this->db->setQuery( 'SELECT count(*) FROM '.
$table .
' WHERE parent_id='.
$group_id );
$child_count =
$this->db->loadResult($query);
if ($child_count >
1 &&
$reparent_children) {
$this->debug_text ('del_group (): You cannot delete the root group and reparent children, this would create multiple root groups.');
// there are no child groups, just delete group
case count($children_ids) ==
0:
/* Reapply when ACL's implemented
$this->db->setQuery( 'DELETE FROM '. $groups_map_table .' WHERE group_id='. $group_id );
$rs = $this->db->Execute($query);
// remove group object maps
$this->db->setQuery( 'DELETE FROM '.
$groups_object_map_table .
' WHERE group_id='.
$group_id );
$rs =
$this->db->query();
$this->db->setQuery( 'DELETE FROM '.
$table .
' WHERE group_id='.
$group_id );
$rs =
$this->db->query();
// move all groups right of deleted group left by width of deleted group
$this->db->setQuery( 'UPDATE '.
$table .
' SET lft=lft-'.
($right-
$left+
1) .
' WHERE lft>'.
$right );
$rs =
$this->db->query();
$this->db->setQuery( 'UPDATE '.
$table .
' SET rgt=rgt-'.
($right-
$left+
1) .
' WHERE rgt>'.
$right );
$rs =
$this->db->query();
case $reparent_children ==
TRUE:
/* Reapply when ACL's implemented
$query = 'DELETE FROM '. $groups_map_table .' WHERE group_id='. $group_id;
$rs = $this->db->Execute($query);
// remove group object maps
$this->db->setQuery( 'DELETE FROM '.
$groups_object_map_table .
' WHERE group_id='.
$group_id );
$rs =
$this->db->query();
$this->db->setQuery( 'DELETE FROM '.
$table .
' WHERE group_id='.
$group_id );
$rs =
$this->db->query();
// set parent of immediate children to parent group
$this->db->setQuery( 'UPDATE '.
$table .
' SET parent_id='.
$parent_id .
' WHERE parent_id='.
$group_id );
$rs =
$this->db->query();
// move all children left by 1
$this->db->setQuery( 'UPDATE '.
$table .
' SET lft=lft-1, rgt=rgt-1 WHERE lft>'.
$left .
' AND rgt<'.
$right );
$rs =
$this->db->query();
// move all groups right of deleted group left by 2
$this->db->setQuery( 'UPDATE '.
$table .
' SET lft=lft-2 WHERE lft>'.
$right );
$rs =
$this->db->query();
$this->db->setQuery( 'UPDATE '.
$table .
' SET rgt=rgt-2 WHERE rgt>'.
$right );
$rs =
$this->db->query();
// make list of group and all children
$group_ids =
$children_ids;
$group_ids[] =
$group_id;
/* Reapply when ACL's implemented
$query = 'DELETE FROM '. $groups_map_table .' WHERE group_id IN ('. implode (',', $group_ids) .')';
$rs = $this->db->Execute($query);
// remove group object maps
$this->db->setQuery( 'DELETE FROM '.
$groups_object_map_table .
' WHERE group_id IN ('.
implode (',', $group_ids) .
')' );
$rs =
$this->db->query();
$this->db->setQuery( 'DELETE FROM '.
$table .
' WHERE group_id IN ('.
implode (',', $group_ids) .
')' );
$rs =
$this->db->query();
// move all groups right of deleted group left by width of deleted group
$this->db->setQuery( 'UPDATE '.
$table .
' SET lft=lft-'.
($right -
$left +
1) .
' WHERE lft>'.
$right );
$rs =
$this->db->query();
$this->db->setQuery( 'UPDATE '.
$table .
' SET rgt=rgt-'.
($right -
$left +
1) .
' WHERE rgt>'.
$right );
$rs =
$this->db->query();
// if the delete failed, rollback the trans and return false
$this->db->RollBackTrans();
$this->debug_text("del_group(): deleted group ID: $group_id");
// <mos> $this->db->CommitTrans();
$this->Cache_Lite->clean('default');
/*======================================================================*\
Purpose: Grabs all Objects's in the database, or specific to a section_value
\*======================================================================*/
function get_object($section_value =
null, $return_hidden=
1, $object_type=
NULL) {
$this->debug_text('get_object(): Invalid Object Type: '.
$object_type);
$this->debug_text("get_object(): Section Value: $section_value Object Type: $object_type");
$
$this->db->setQuery( 'SELECT '.
$object_type .
'_id FROM '.
$table );
if (!empty($section_value)) {
$where[] =
'section_value='.
$this->db->getEscaped($section_value);
$query .=
' WHERE '.
implode(' AND ', $where);
$rs =
$this->db->loadResultArray();
/*======================================================================*\
Purpose: Grabs all Objects in the database, or specific to a section_value
returns format suitable for add_acl and is_conflicting_acl
\*======================================================================*/
/*======================================================================*\
Function: get_object_data()
Purpose: Gets all data pertaining to a specific Object.
\*======================================================================*/
/*======================================================================*\
Function: get_object_groups()
Purpose: Gets the group_id's for the given the section_value AND value
\*======================================================================*/
function get_object_groups($object_section_value, $object_value, $object_type=
NULL) {
$this->debug_text('get_object_groups(): Invalid Object Type: '.
$object_type);
$this->debug_text("get_object_groups(): Section Value: $object_section_value Value: $object_value Object Type: $object_type");
$object_section_value =
trim($object_section_value);
$object_value =
trim($object_value);
if (empty($object_section_value) AND empty($object_value) ) {
$this->debug_text("get_object_groups(): Section Value ($object_section_value) AND value ($object_value) is empty, this is required");
if (empty($object_type) ) {
$this->debug_text("get_object_groups(): Object Type ($object_type) is empty, this is required");
// SELECT g.group_id,o.'. $group_type .'_id,(gm.group_id IS NOT NULL) AS member
SELECT g.group_id,o.'.
$group_type .
'_id,(gm.group_id IS NOT NULL) AS member
FROM '.
$group_table .
' g
LEFT JOIN '.
$table .
' gm ON gm.group_id=g.group_id
LEFT JOIN '.
$object_table .
' o ON o.'.
$group_type .
'_id = gm.'.
$group_type .
'_id
WHERE (o.section_value=\''.
$this->db->getEscaped($object_section_value) .
'\' AND o.value=\''.
$this->db->getEscaped($object_value) .
'\')'
$rs =
$this->db->loadResultArray();
if ($this->db->getErrorNum()) {
//Return the array of group id's
/*======================================================================*\
Function: get_object_id()
Purpose: Gets the object_id given the section_value AND value of the object.
\*======================================================================*/
function get_object_id($section_value, $value, $object_type=
NULL) {
$this->debug_text('get_object_id(): Invalid Object Type: '.
$object_type);
$this->debug_text("get_object_id(): Section Value: $section_value Value: $value Object Type: $object_type");
$section_value =
trim($section_value);
if (empty($section_value) AND empty($value) ) {
$this->debug_text("get_object_id(): Section Value ($value) AND value ($value) is empty, this is required");
if (empty($object_type) ) {
$this->debug_text("get_object_id(): Object Type ($object_type) is empty, this is required");
$this->db->setQuery( 'SELECT '.
$object_type .
'_id FROM '.
$table .
' WHERE section_value=\''.
$this->db->getEscaped($section_value) .
'\' AND value=\''.
$this->db->getEscaped($value) .
'\''
$rs =
$this->db->loadRowList();
if ($this->db->getErrorNum()) {
$row_count =
count( $rs );
$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.");
$this->debug_text("get_object_id(): Returned $row_count rows");
/*======================================================================*\
Function: get_object_section_value()
Purpose: Gets the object_section_value given object id
\*======================================================================*/
/*======================================================================*\
Function: get_object_groups()
Purpose: Gets all groups an object is a member of.
If $option == 'RECURSE' it will get all ancestor groups.
defaults to only get direct parents.
\*======================================================================*/
/*======================================================================*\
Purpose: Inserts a new object
\*======================================================================*/
function add_object($section_value, $name, $value=
0, $order=
0, $hidden=
0, $object_type=
NULL) {
$this->debug_text('add_object(): Invalid Object Type: '.
$object_type);
$this->debug_text("add_object(): Section Value: $section_value Value: $value Order: $order Name: $name Object Type: $object_type");
$section_value =
trim($section_value);
if ($order ==
NULL OR $order ==
'') {
if (empty($name) OR empty($section_value) ) {
$this->debug_text("add_object(): name ($name) OR section value ($section_value) is empty, this is required");
$this->debug_text("add_object(): name ($name) OR value ($value) is too long.");
if (empty($object_type) ) {
$this->debug_text("add_object(): Object Type ($object_type) is empty, this is required");
// Test to see if the section is invalid or object already exists.
SELECT (o.'.
$object_type .
'_id IS NOT NULL) AS object_exists
FROM '.
$object_sections_table .
' s
LEFT JOIN '.
$table .
' o ON (s.value=o.section_value AND o.value=\''.
$this->db->getEscaped($value) .
'\')
WHERE s.value=\''.
$this->db->getEscaped($section_value).
'\''
$rows =
$this->db->loadRowList();
if ($this->db->getErrorNum()) {
if (count( $rows ) !=
1) {
$this->debug_text("add_object(): Section Value: $section_value Object Type ($object_type) does not exist, this is required");
//Object is already created.
$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')" );
if (!$this->db->query()) {
$insert_id =
$this->db->insertid();
$this->debug_text("add_object(): Added object as ID: $insert_id");
/*======================================================================*\
Purpose: Edits a given Object
\*======================================================================*/
function edit_object($object_id, $section_value, $name, $value=
0, $order=
0, $hidden=
0, $object_type=
NULL) {
$object_map_table =
'aco_map';
$object_map_table =
'aro_map';
$object_map_table =
'axo_map';
$this->debug_text("edit_object(): ID: $object_id, Section Value: $section_value, Value: $value, Order: $order, Name: $name, Object Type: $object_type");
$section_value =
trim($section_value);
if (empty($object_id) OR empty($section_value) ) {
$this->debug_text("edit_object(): Object ID ($object_id) OR Section Value ($section_value) is empty, this is required");
$this->debug_text("edit_object(): name ($name) is empty, this is required");
if (empty($object_type) ) {
$this->debug_text("edit_object(): Object Type ($object_type) is empty, this is required");
//Get old value incase it changed, before we do the update.
$this->db->setQuery( 'SELECT value, section_value FROM '.
$table .
' WHERE '.
$object_type .
'_id='.
$object_id );
$old =
$this->db->loadRow();
SET section_value=\''.
$this->db->getEscaped($section_value) .
'\',
value='.
$this->db->getEscaped($value) .
',
order_value='.
$this->db->getEscaped($order) .
',
name=\''.
$this->db->getEscaped($name) .
'\',
WHERE '.
$object_type .
'_id='.
$object_id
if (!$this->db->getErrorNum()) {
if ($old[0] !=
$value OR $old[1] !=
$section_value) {
$this->debug_text("edit_object(): Value OR Section Value Changed, update other tables.");
UPDATE '.
$object_map_table .
'
SET value=\''.
$this->db->getEscaped($value) .
'\',
section_value=\''.
$this->db->getEscaped($section_value) .
'\'
WHERE section_value=\''.
$this->db->getEscaped($old[1]) .
'\'
AND value='.
$this->db->getEscaped($old[0])
if (!$this->db->getErrorNum()) {
$this->debug_text ('edit_object(): Modified Map Value: '.
$value .
' Section Value: '.
$section_value);
/*======================================================================*\
Purpose: Deletes a given Object and, if instructed to do so,
erase all referencing objects
ERASE feature by: Martino Piccinato
\*======================================================================*/
function del_object($object_id, $object_type=
NULL, $erase=
FALSE) {
$this->debug_text('del_object(): Invalid Object Type: '.
$object_type);
$this->debug_text("del_object(): ID: $object_id Object Type: $object_type, Erase all referencing objects: $erase");
if (empty($object_id) ) {
$this->debug_text("del_object(): Object ID ($object_id) is empty, this is required");
if (empty($object_type) ) {
$this->debug_text("del_object(): Object Type ($object_type) is empty, this is required");
// <mos> $this->db->BeginTrans();
// Get Object section_value/value (needed to look for referencing objects)
$this->db->setQuery( 'SELECT section_value,value FROM '.
$table .
' WHERE '.
$object_type .
'_id='.
$object_id );
$object =
$this->db->loadRow();
$this->debug_text('del_object(): The specified object ('.
strtoupper($object_type) .
' ID: '.
$object_id .
') could not be found.<br />SQL = '.
$this->db->stderr());
$section_value =
$object[0];
// Get ids of acl referencing the Object (if any)
$this->db->setQuery( "SELECT acl_id FROM $object_map_table WHERE value='$value' AND section_value='$section_value'" );
$acl_ids =
$this->db->loadResultArray();
// We were asked to erase all acl referencing it
$this->debug_text("del_object(): Erase was set to TRUE, delete all referencing objects");
if ($object_type ==
"aro" OR $object_type ==
"axo") {
// The object can be referenced in groups_X_map tables
// in the future this branching may become useless because
// ACO might me "groupable" too
// Get rid of groups_map referencing the Object
$this->db->setQuery( 'DELETE FROM '.
$object_group_table .
' WHERE '.
$object_type .
'_id='.
$object_id );
$rs =
$this->db->query();
// <mos> $this->db->RollBackTrans();
//There are acls actually referencing the object
if ($object_type ==
'aco') {
// I know it's extremely dangerous but
// if asked to really erase an ACO
// we should delete all acl referencing it
// Do this below this branching
// where it uses $orphan_acl_ids as
// the array of the "orphaned" acl
// in this case all referenced acl are
$orphan_acl_ids =
$acl_ids;
// The object is not an ACO and might be referenced
// in still valid acls regarding also other object.
// In these cases the acl MUST NOT be deleted
// Get rid of $object_id map referencing erased objects
$this->db->setQuery( "DELETE FROM $object_map_table WHERE section_value='$section_value' AND value='$value'" );
$rs =
$this->db->query();
$this->db->RollBackTrans();
// Find the "orphaned" acl. I mean acl referencing the erased Object (map)
// not referenced anymore by other objects
$sql_acl_ids =
implode(",", $acl_ids);
LEFT JOIN '.
$object_map_table .
' b ON a.id=b.acl_id
'.
/* <mos return for full acl stuff> LEFT JOIN '. $groups_map_table .' c ON a.id=c.acl_id*/'
AND section_value IS NULL
AND a.id in ('.
$sql_acl_ids .
')');
$orphan_acl_ids =
$this->db->loadResultArray();
} // End of else section of "if ($object_type == "aco")"
// If there are orphaned acls get rid of them
foreach ($orphan_acl_ids as $acl) {
} // End of if ($acl_ids)
// Finally delete the Object itself
$this->db->setQuery( "DELETE FROM $table WHERE {$object_type}_id='$object_id'" );
$rs =
$this->db->query();
// <mos> $this->db->RollBackTrans();
// <mos> $this->db->CommitTrans();
} // End of "if ($erase)"
if ($object_type ==
'axo' OR $object_type ==
'aro') {
// If the object is "groupable" (may become unnecessary,
// Get id of groups where the object is assigned:
// you must explicitly remove the object from its groups before
// deleting it (don't know if this is really needed, anyway it's safer ;-)
$this->db->setQuery( 'SELECT group_id FROM '.
$object_group_table .
' WHERE '.
$object_type .
'_id='.
$object_id );
$groups_ids =
$this->db->loadResultArray();
if ( ( isset
($acl_ids) AND $acl_ids !==
FALSE ) OR ( isset
($groups_ids) AND $groups_ids !==
FALSE) ) {
// The Object is referenced somewhere (group or acl), can't delete it
$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,",").
")");
// The Object is NOT referenced anywhere, delete it
$this->db->setQuery( "DELETE FROM $table WHERE {$object_type}_id='$object_id'" );
if ( $this->db->getErrorNum() ) {
// <mos> $this->db->RollBackTrans();
// <mos> $this->db->CommitTrans();
/*======================================================================*\
Function: get_object_section_section_id()
Purpose: Gets the object_section_id given the name AND/OR value of the section.
Will only return one section id, so if there are duplicate names it will return false.
\*======================================================================*/
/*======================================================================*\
Function: add_object_section()
Purpose: Inserts an object Section
\*======================================================================*/
/*======================================================================*\
Function: edit_object_section()
Purpose: Edits a given Object Section
\*======================================================================*/
/*======================================================================*\
Function: del_object_section()
Purpose: Deletes a given Object Section and, if explicitly
asked, all the section objects
ERASE feature by: Martino Piccinato
\*======================================================================*/
/*======================================================================*\
Function: has_group_parent
Purpose: Checks whether the 'source' group is a child of the 'target'
\*======================================================================*/
$this->debug_text("has_group_parent(): Source=$grp_src, Target=$grp_tgt, Type=$group_type");
$this->db->setQuery( "SELECT COUNT(*)"
.
"\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt"
.
"\nWHERE g1.group_id=$grp_src AND g2.group_id=$grp_tgt"
$this->db->setQuery( "SELECT COUNT(*)"
.
"\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt"
.
"\nWHERE g1.name='$grp_src' AND g2.name='$grp_tgt'"
$this->db->setQuery( "SELECT COUNT(*)"
.
"\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt"
.
"\nWHERE g1.group_id='$grp_src' AND g2.name='$grp_tgt'"
$this->db->setQuery( "SELECT COUNT(*)"
.
"\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt"
.
"\nWHERE g1.name=$grp_src AND g2.group_id='$grp_tgt'"
return $this->db->loadResult();
$database->setQuery( "SELECT g.*"
.
"\nFROM #__core_acl_{$type}_groups AS g"
.
"\nINNER JOIN #__core_acl_groups_{$type}_map AS gm ON gm.group_id = g.group_id"
.
"\nINNER JOIN #__core_acl_{$type} AS ao ON ao.{$type}_id = gm.{$type}_id"
.
"\nWHERE ao.value='$value'"
$database->loadObject( $obj );
function _getBelow( $table, $fields, $groupby=
null, $root_id=
null, $root_name=
null, $inclusive=
true ) {
$database->setQuery( "SELECT lft, rgt FROM $table WHERE name='$root_name'" );
$database->loadObject( $root );
if ($root->lft+
$root->rgt <>
0) {
$where =
"WHERE g1.lft BETWEEN $root->lft AND $root->rgt";
$where =
"WHERE g1.lft BETWEEN $root->lft+1 AND $root->rgt-1";
$database->setQuery( "SELECT $fields"
.
"\nINNER JOIN $table AS g2 ON g1.lft BETWEEN g2.lft AND g2.rgt"
.
($groupby ?
"\nGROUP BY $groupby" :
"")
//echo $database->getQuery();
return $database->loadObjectList();
'g1.group_id, g1.name, COUNT(g2.name) AS level',
$root_id, $root_name, $inclusive );
// first pass get level limits
for ($i=
0; $i <
$n; $i++
) {
$min =
min( $min, $tree[$i]->level );
$max =
max( $max, $tree[$i]->level );
foreach (range( $min, $max ) as $i) {
$indents[$i] =
' ';
// correction for first indent
for ($i=
$n-
1; $i >=
0; $i--
) {
foreach (range( $min, $tree[$i]->level ) as $j) {
if (@$indents[$tree[$i]->level+
1] ==
'. ') {
//$list[$i] = $tree[$i]->level.$shim.$twist.$tree[$i]->name;
$list[$i] =
mosHTML::makeOption( $tree[$i]->group_id, $shim.
$twist.
$tree[$i]->name );
if ($tree[$i]->level <
@$tree[$i-
1]->level) {
$indents[$tree[$i]->level+
1] =
'. ';
/** @var int Primary key */
$this->mosDBTable( '#__core_acl_aro', 'aro_id', $db );
* Utility function for returning groups
/** @var int Primary key */
$this->mosDBTable( '#__core_acl_aro_groups', 'group_id', $db );
Documentation generated on Mon, 05 May 2008 16:19:56 +0400 by phpDocumentor 1.4.0