Source for file mambo.php

Documentation is available at mambo.php

  1. <?php
  2. /**
  3. * Main file for Mambo
  4. @package Mambo
  5. @author Mambo Foundation Inc see README.php
  6. @copyright Mambo Foundation Inc.
  7. *  See COPYRIGHT.php for copyright notices and details.
  8. @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see
  9. *  LICENSE.php
  10. *  Mambo is free software; you can redistribute it and/or
  11. *  modify it under the terms of the GNU General Public License
  12. *  as published by the Free Software Foundation; version 2 of the
  13. *  License.
  14. */
  15. defined'_VALID_MOS' or die'Direct Access to this location is not allowed.' );
  16.  
  17. class mosAuthoriser {
  18.     var $connection = '';
  19.     var $perm_found;
  20.     var $permissions = array();
  21.     var $assignments = array();
  22.  
  23.     function mosAuthoriser ({
  24.         $this->connection =mamboDatabase::getInstance();
  25.     }
  26.  
  27.     function &getInstance ({
  28.         static $instance;
  29.         if (!is_object($instance)) {
  30.             $instance =new mosAuthoriser();
  31.         }
  32.         return $instance;
  33.     }
  34.     
  35.     function doSQL ($sql{
  36.         $this->connection->setQuery($sql);
  37.         if (!$this->connection->query()) {
  38.             echo "<script> alert('".$this->connection->getErrorMsg()."'); window.history.go(-1); </script>\n";
  39.             exit();
  40.         }
  41.     }
  42.  
  43.     function getAccessorData ($type$id{
  44.         if (isset($this->perm_found[$type][$id])) return;
  45.         $sql "SELECT a.*, p.control, p.action, p.subject_type, p.subject_id, p.system FROM #__assignments AS a LEFT JOIN #__permissions AS p ON p.role=a.role WHERE a.access_type='$type'";
  46.         if (isset($this->perm_found[$type])) $sql .= " AND a.access_id='$id'";
  47.         else $sql .= " AND (a.access_id='$id' OR a.access_id='*' OR a.access_id='+')";
  48.         $this->doSQL($sql);
  49.         $new_permissions $this->connection->loadObjectList();
  50.         if ($new_permissions$this->permissions = array_merge($this->permissions$new_permissions);
  51.         $this->perm_found[$type][$id1;
  52.     }
  53.  
  54.     function &getRoles ($type$id{
  55.         $this->getAccessorData ($type$id);
  56.         $rolenames array();
  57.         foreach ($this->permissions as $role{
  58.             if (strcasecmp($role->access_type$type== 0
  59.             AND ($role->access_id == $id OR $role->access_id == '*' OR ($role->access_id == '+' AND $id != 0))
  60.             AND !in_array($role->role,$rolenames)) $rolenames[$role->role;
  61.         }
  62.         return $rolenames;
  63.     }
  64.  
  65.     function accessorPermissionOrControl  ($mask$a_type$a_id$action$s_type='*'$s_id='*'{
  66.         $this->getAccessorData ($a_type$a_id);
  67.         foreach ($this->permissions as $permission{
  68.             if ((strcasecmp($permission->access_type,$a_type== OR $permission->access_type == '*')
  69.             AND (strcasecmp($permission->access_id,$a_id== OR $permission->access_id == '*')
  70.             AND (strcasecmp($permission->action,$action)==OR $permission->action == '*' OR $action == '*')
  71.             AND (strcasecmp($permission->subject_type,$s_type)==OR $s_type=='*')
  72.             AND (strcasecmp($permission->subject_id,$s_id)==OR $permission->subject_id == '*')
  73.             AND ($permission->control&$mask)) return 1;
  74.         }
  75.         return 0;
  76.     }
  77.     
  78.     function checkPermission ($a_type$a_id$action$s_type='*'$s_id='*'{
  79.         return $this->accessorPermissionOrControl(2$a_type$a_id$action$s_type='*'$s_id='*');
  80.     }
  81.     
  82.     function checkControl ($a_type$a_id$action$s_type='*'$s_id='*'{
  83.         return $this->accessorPermissionOrControl(1$a_type$a_id$action$s_type='*'$s_id='*');
  84.     }
  85.     
  86.     function checkGrant ($a_type$a_id$action$s_type='*'$s_id='*'{
  87.         return $this->accessorPermissionOrControl(4$a_type$a_id$action$s_type='*'$s_id='*');
  88.     }
  89.  
  90.     function rolePermissionOrControl ($mask$role$action$s_type$s_id{
  91.         $sql "SELECT * FROM #__permissions WHERE role='$role'";
  92.         $this->connection->setQuery($sql);
  93.         $permissions $this->connection->loadObjectList();
  94.         if ($permissions{
  95.             foreach ($permissions as $permission{
  96.                 if (strcasecmp($permission->role,$role== 0
  97.                 AND (strcasecmp($permission->action,$action)==OR $permission->action == '*')
  98.                 AND (strcasecmp($permission->subject_type,$s_type)==0)
  99.                 AND (strcasecmp($permission->subject_id,$s_id)==OR $permission->subject_id == '*')
  100.                 AND ($permission->control&$mask)) return 1;
  101.             }
  102.         }
  103.         return 0;
  104.     }
  105.  
  106.     function checkRolePermission  ($role$action$s_type$s_id{
  107.         return $this->rolePermissionOrControl(2$role$action$s_type$s_id);
  108.     }
  109.  
  110.     function checkRoleControl  ($role$action$s_type$s_id{
  111.         return $this->rolePermissionOrControl(1$role$action$s_type$s_id);
  112.     }
  113.  
  114.     function checkRoleGrant  ($role$action$s_type$s_id{
  115.         return $this->rolePermissionOrControl(4$role$action$s_type$s_id);
  116.     }
  117.  
  118.     function &listPermissions ($a_type$a_id$action$property{
  119.         $this->getAccessorData ($a_type$a_id);
  120.         $results array();
  121.         foreach ($this->permissions as $permission{
  122.             if (strcasecmp($permission->access_type,$a_type== 0
  123.             AND (strcasecmp($permission->access_id,$a_id== OR $permission->access_id == '*' OR ($permission->access_id == '+' AND $a_id != 0))
  124.             AND (strcasecmp($permission->action,$action== OR $permission->action == '*')
  125.             AND $permission->subject_type != null AND $permission->subject_id != null{
  126.                 $results[$permission->$property;
  127.             }
  128.         }
  129.         return $results;
  130.     }
  131.  
  132. }
  133.  
  134.     var $connection;
  135.     var $roles = array();
  136.  
  137.     function mosAuthorisationAdmin ({
  138.         $this->connection =mamboDatabase::getInstance();
  139.     }
  140.  
  141.     function &getInstance ({
  142.         static $instance;
  143.         if (!is_object($instance)) {
  144.             $instance =new mosAuthorisationAdmin();
  145.         }
  146.         return $instance;
  147.     }
  148.  
  149.     function doSQL ($sql{
  150.         $this->connection->setQuery($sql);
  151.         if (!$this->connection->query()) {
  152.             echo "<script> alert('".$this->connection->getErrorMsg()."'); window.history.go(-1); </script>\n";
  153.             exit();
  154.         }
  155.     }
  156.     
  157.     function getRoles ({
  158.         if (count($this->roles== 0{
  159.             $sql "SELECT DISTINCT role FROM #__assignments";
  160.             $this->connection->setQuery($sql);
  161.             $this->roles = $this->connection->loadResultArray();
  162.             $sql "SELECT DISTINCT role FROM #__permissions";
  163.             $this->connection->setQuery($sql);
  164.             $more $this->connection->loadResultArray();
  165.             foreach ($more as $role$this->addRole($role);
  166.         }
  167.         return $this->roles;
  168.     }
  169.     
  170.     function addRole ($role{
  171.         if (!in_array($role$this->roles)) $this->roles[$role;
  172.     }
  173.     
  174.     function removeRole ($role{
  175.         $key array_search($role$this->roles);
  176.         if ($key !== falseunset($this->roles[$key]);
  177.     }
  178.     
  179.     function &permissionHolders ($subject_type$subject_id{
  180.         $sql "SELECT role, action, control FROM #__permissions";
  181.         if ($subject_type != '*'$where["(subject_type='$subject_type' OR subject_type='*')";
  182.         if ($subject_id != '*'$where["(subject_id='$subject_id' OR subject_id='*')";
  183.         if (isset($where)) $sql .= " WHERE ".implode(' AND '$where);
  184.         $this->connection->setQuery($sql);
  185.         $result $this->connection->loadObjectList();
  186.         if (!$result$result array();
  187.         return $result;
  188.     }
  189.     
  190.     function &nonLocalPermissionHolders ($subject_type$subject_id{
  191.         $sql "SELECT role, action, control FROM #__permissions WHERE (action='*' OR subject_type='*' OR subject_id='*') AND ((subject_type='$subject_type' OR subject_type='*') AND (subject_id='$subject_id' OR subject_id='*'))";
  192.         $this->connection->setQuery($sql);
  193.         $result $this->connection->loadObjectList();
  194.         if (!$result$result array();
  195.         return $result;
  196.     }
  197.     
  198.     function permitSQL ($role$control$action$subject_type$subject_id{
  199.         $sql "REPLACE INTO #__permissions (role, control, action, subject_type, subject_id) VALUES ('$role', '$control', '$action', '$subject_type', '$subject_id');";
  200.         return $sql;
  201.     }
  202.  
  203.     function permit ($role$control$action$subject_type$subject_id{
  204.         $sql $this->permitSQL($role$control$action$subject_type$subject_id);
  205.         $this->doSQL($sql);
  206.         $this->addRole($role);
  207.     }
  208.  
  209.     function assign ($role$access_type$access_id{
  210.         $sql "REPLACE INTO #__assignments (role, access_type, access_id) VALUES ('$role', '$access_type', '$access_id')";
  211.         $this->doSQL($sql);
  212.         $this->addRole($role);
  213.     }
  214.  
  215.     function dropAccess ($access_type$access_id{
  216.         $sql "DELETE FROM #__assignments WHERE access_type='$access_type' AND access_id='$access_id'";
  217.         $this->doSQL($sql);
  218.     }
  219.  
  220.     function &getControllingRoles ($access_type$access_id$action$subject_type$subject_id{
  221.         $sql "SELECT a.role FROM #__permissions AS p, #__assignments AS a WHERE a.access_type='$access_type'"
  222.         ." AND a.access_id='$access_id' AND a.role=p.role AND (p.control&1)"
  223.         ." AND p.action='$action' AND p.subject_type='$subject_type' AND p.subject_id='$subject_id'";
  224.         $this->doSQL($sql);
  225.         $roles $this->connection->loadResultArray();
  226.         return $roles;
  227.     }
  228.  
  229.     function &getMyPermissions ($access_type$access_id{
  230.         $sql 'SELECT p.action, p.subject_type, p.subject_id, control FROM #__permissions AS p, #__assignments AS a'
  231.         . " WHERE p.role=a.role AND a.access_type='$access_type' AND (a.access_id='$access_id' OR a.access_id='*')"
  232.         . ' AND (p.control&1)';
  233.         $this->doSQL($sql);
  234.         $permissions =$this->connection->loadObjectList();
  235.         return $permissions;
  236.     }
  237.  
  238.     function getJointPermissions ($access_type$access_id$role{
  239.         $sql "SELECT p2.control AS hiscontrol, p1.control AS mycontrol, p1.action, p1.subject_type, p1.subject_id"
  240.         ." FROM `#__assignments` AS a, `#__permissions` AS p1 LEFT JOIN `#__permissions` AS p2"
  241.         ." ON (p2.role='$role' AND p1.action=p2.action AND p1.subject_type=p2.subject_type AND p1.subject_id=p2.subject_id)"
  242.         ." WHERE  (p1.control&1) AND p1.role=a.role AND a.access_type='$access_type' AND (a.access_id='$access_id' OR a.access_id='*')";
  243.         $this->doSQL($sql);
  244.         $permissions =$this->connection->loadObjectList();
  245.         return $permissions;
  246.     }
  247.     
  248.     function getAccessLists ($access_type$access_id$action$subject_type$subject_id{
  249.         $authoriser =mosAuthoriser::getInstance();
  250.         if ($authoriser->checkControl($access_type$access_id$action$subject_type$subject_id)) {
  251.             $cangrant $authoriser->checkGrant($access_type$access_id$action$subject_type$subject_id);
  252.             $permissions $this->permissionHolders($subject_type$subject_id);
  253.             $allroles $this->getRoles();
  254.             foreach ($allroles as $role{
  255.                 $itemc[$optionc mosHTML::makeOption($role$role);
  256.                 $itema[$optiona mosHTML::makeOption($role$role);
  257.                 if ($cangrant$itemg[$optiong mosHTML::makeOption($role$role);
  258.                 foreach ($permissions as $permission{
  259.                     if (($permission->action == '*' OR $permission->action == $actionAND $permission->role == $role{
  260.                         if ($permission->control 1$cselected[$optionc;
  261.                         if ($permission->control 2$aselected[$optiona;
  262.                         if ($cangrant AND $permission->control 4$gselected[$optiong;
  263.                     }
  264.                 }
  265.             }
  266.             $results[mosHTML::selectList($itema$action.'_arole[]''multiple="multiple"''value''text'$aselected);
  267.             $results[mosHTML::selectList($itemc$action.'_crole[]''multiple="multiple"''value''text'$cselected);
  268.             if ($cangrant$results[mosHTML::selectList($itemg$action.'_grole[]''multiple="multiple"''value''text'$gselected);
  269.         }
  270.         else $results array();
  271.         return $results;
  272.     }
  273.     
  274.     function resetPermissions ($action$subject_type$subject_id{
  275.         $control_types array ('crole''arole''grole');
  276.         $control_values array (1,2,4);
  277.         $permissions $this->nonLocalPermissionHolders($subject_type$subject_id);
  278.         $this->dropPermissions($action$subject_type$subject_id);
  279.         foreach ($control_types as $i=>$type{
  280.             $key $action.'_'.$type;
  281.             if (isset($_POST[$key])) {
  282.                 foreach ($_POST[$keyas $role{
  283.                     $value = isset($newpermits[$role]$newpermits[$role0;
  284.                     $newpermits[$role$value $control_values[$i];
  285.                 }
  286.             }
  287.         }
  288.         $sql '';
  289.         foreach ($newpermits as $role=>$value{
  290.             $needed true;
  291.             foreach ($permissions as $permission{
  292.                 if (($permission->action == '*' OR $permission->action == $actionAND $permission->role == $role{
  293.                     if (($value $permission->control=== $value{
  294.                         $needed false;
  295.                         break;
  296.                     }
  297.                 }
  298.             }
  299.             if ($needed$sql .= $this->permitSQL ($role$value$action$subject_type$subject_id);
  300.         }
  301.         if ($sql$this->doSQL($sql);
  302.     }
  303.  
  304.     function roleExists ($role{
  305.         $sql "SELECT COUNT(role) FROM #__permissions WHERE role='$role' GROUP BY role";
  306.         $this->doSQL($sql);
  307.         if ($this->connection->loadResult()) return true;
  308.         $sql "SELECT COUNT(role) FROM #__assignments WHERE role='$role' GROUP BY role";
  309.         $this->doSQL($sql);
  310.         if ($this->connection->loadResult()) return true;
  311.         else return false;
  312.     }
  313.  
  314.     function dropRole ($role{
  315.         $sql "DELETE FROM #__permissions WHERE action='administer' AND subject_type='$role' AND system=0";
  316.         $this->doSQL($sql);
  317.         $sql "DELETE a FROM #__assignments AS a LEFT JOIN #__permissions AS p ON a.role=p.role WHERE a.role='$role' AND (p.system=0 OR p.system=NULL)";
  318.         $this->doSQL($sql);
  319.         $this->dropRolePermissions($role);
  320.         $this->removeRole($role);
  321.     }
  322.  
  323.     function dropRolePermissions ($role{
  324.         $sql "DELETE FROM #__permissions WHERE role='$role' AND system=0";
  325.         $this->doSQL($sql);
  326.         $this->roles = array();
  327.     }
  328.  
  329.     function dropPermissions ($action$subject_type$subject_id{
  330.         $sql "DELETE FROM #__permissions WHERE action='$action' AND subject_type='$subject_type'AND subject_id='$subject_id' AND system=0";
  331.         $this->doSQL($sql);
  332.         $this->roles = array();
  333.     }
  334.  
  335. }
  336.  
  337.  
  338.  
  339.  
  340. // ----- NO MORE CLASSES OR FUNCTIONS PASSED THIS POINT -----
  341. // Post class declaration initialisations
  342. // some version of PHP don't allow the instantiation of classes
  343. // before they are defined
  344.  
  345. ?>

Documentation generated on Mon, 05 May 2008 16:20:58 +0400 by phpDocumentor 1.4.0