LiveUser
[ class tree: LiveUser ] [ index: LiveUser ] [ all elements ]

Source for file MDB_Simple.php

Documentation is available at MDB_Simple.php

  1. <?php
  2. // LiveUser: A framework for authentication and authorization in PHP applications
  3. // Copyright (C) 2002-2003 Markus Wolff
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. /**
  20.  * Include parent class
  21.  */
  22. require_once 'LiveUser/Admin/Perm/Common.php';
  23. require_once 'MDB.php';
  24.  
  25. /**
  26.  * Container for simple rights managements.
  27.  *
  28.  * With it you can only assign rights to users. For advanced uses like
  29.  * groups assignements and more see DB_Medium and DB_Complex.
  30.  *
  31.  * @category authentication
  32.  * @version $Id: MDB_Simple.php,v 1.46 2004/08/28 15:46:37 lsmith Exp $
  33.  * @package LiveUser
  34.  */
  35. {
  36.     /**
  37.      * Constructor
  38.      *
  39.      * @access protected
  40.      * @param  array  full liveuser configuration array
  41.      * @return void 
  42.      * @see    LiveUser::factory()
  43.      */
  44.     function LiveUser_Admin_Perm_Container_MDB_Simple(&$connectOptions)
  45.     {
  46.         $this->LiveUser_Admin_Perm_Common($connectOptions);
  47.         if (is_array($connectOptions)) {
  48.             foreach ($connectOptions as $key => $value{
  49.                 if (isset($this->$key)) {
  50.                     $this->$key $value;
  51.                 }
  52.             }
  53.             if (isset($connectOptions['connection']&&
  54.                     MDB::isConnection($connectOptions['connection'])
  55.             {
  56.                 $this->dbc     &$connectOptions['connection'];
  57.                 $this->init_ok = true;
  58.             elseif (isset($connectOptions['dsn'])) {
  59.                 $this->dsn $connectOptions['dsn'];
  60.                 $function = null;
  61.                 if (isset($connectOptions['function'])) {
  62.                     $function $connectOptions['function'];
  63.                 }
  64.                 $options = null;
  65.                 if (isset($connectOptions['options'])) {
  66.                     $options $connectOptions['options'];
  67.                 }
  68.                 $options['optimize''portability';
  69.                 if ($function == 'singleton'{
  70.                     $this->dbc =MDB::singleton($connectOptions['dsn']$options);
  71.                 else {
  72.                     $this->dbc =MDB::connect($connectOptions['dsn']$options);
  73.                 }
  74.                 if (!MDB::isError($this->dbc)) {
  75.                     $this->init_ok = true;
  76.                 }
  77.             }
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * Gets the perm ID of a user.
  83.      *
  84.      * @access  public
  85.      * @param   string  Auth user ID.
  86.      * @param   string  Auth container name.
  87.      * @return  mixed   Permission ID or MDB error.
  88.      */
  89.     function getPermUserId($authId$authName)
  90.     {
  91.         $query '
  92.             SELECT
  93.                 perm_user_id
  94.             FROM
  95.                 ' $this->prefix . 'perm_users
  96.             WHERE
  97.                 auth_user_id = '.$this->dbc->getValue('text'$authId).'
  98.             AND
  99.                 auth_container_name = '.$this->dbc->getValue('text'$authName);
  100.         $permId $this->dbc->queryOne($query);
  101.  
  102.         return $permId;
  103.     // end func _getPermUserId
  104.  
  105.     function getAuthUserId($permId$id_only = false)
  106.     {
  107.         $query '
  108.             SELECT
  109.                 auth_user_id, auth_container_name
  110.             FROM
  111.                 ' $this->prefix . 'perm_users
  112.             WHERE
  113.                 perm_user_id = '.$this->dbc->getValue('text'$permId);
  114.                 $authId $this->dbc->queryRow($queryarray('text''text')MDB_FETCHMODE_ASSOC);
  115.             if ($id_only{
  116.                 return $authId['auth_user_id'];
  117.             }
  118.             return $authId;
  119.     // end func _getAuthUserId
  120.  
  121.     /**
  122.      * Reads all languages from databases and stores them in private variable
  123.      *
  124.      * $this->_langs is filled with this array structure:
  125.      *     two_letter_code => language_id
  126.      *
  127.      * @access private
  128.      * @return mixed boolean or MDB Error object
  129.      */
  130.     function _getLanguages()
  131.     {
  132.         if (sizeof($this->_langs< 1{
  133.             $query 'SELECT two_letter_name, language_id FROM ' $this->prefix . 'languages';
  134.             $langs $this->dbc->queryAll($querynullMDB_FETCHMODE_ASSOCtrue);
  135.             if (MDB::isError($langs)) {
  136.                 return $langs;
  137.             }
  138.             $this->_langs $langs;
  139.         }
  140.         return true;
  141.     }
  142.  
  143.     /**
  144.      * Set current language
  145.      *
  146.      * Returns false if the language is not known
  147.      *
  148.      * @access public
  149.      * @param  string language short name
  150.      * @return mixed   boolean or MDB Error object or false
  151.      */
  152.     function setCurrentLanguage($language)
  153.     {
  154.         // Get all language ids
  155.         if (MDB::isError($result $this->_getLanguages())) {
  156.             return $result;
  157.         }
  158.  
  159.         // Check if language is a known one
  160.         if (!isset($this->_langs[$language])) {
  161.             return false;
  162.         }
  163.  
  164.         $this->_language $language;
  165.  
  166.         return true;
  167.     }
  168.  
  169.     /**
  170.      * Get current language
  171.      *
  172.      * @access public
  173.      * @return string name of the current language
  174.      */
  175.     function getCurrentLanguage()
  176.     {
  177.         return $this->_language;
  178.     }
  179.  
  180.     /**
  181.      * Set current application
  182.      *
  183.      * @access public
  184.      * @param  integer  id of application
  185.      * @return boolean always true
  186.      */
  187.     function setCurrentApplication($applicationId)
  188.     {
  189.         $this->_application $applicationId;
  190.  
  191.         return true;
  192.     }
  193.  
  194.     /**
  195.      * Get current application
  196.      *
  197.      * @access public
  198.      * @return string name of the current application
  199.      */
  200.     function getCurrentApplication()
  201.     {
  202.         return $this->_application;
  203.     }
  204.  
  205.     /**
  206.      * Assigns name (and description) in specified language to a section
  207.      *
  208.      * @access public
  209.      * @param integer id of [section]
  210.      * @param integer type of section
  211.      * @param string  language (two letter code) of name/description
  212.      * @param string  name of [section]
  213.      * @param string  description of [section]
  214.      * @return mixed boolean or MDB Error object
  215.      */
  216.     function addTranslation($sectionId$section_type$language$name$description = null)
  217.     {
  218.         // Get all language ids
  219.         if (MDB::isError($result $this->_getLanguages())) {
  220.             return $result;
  221.         }
  222.  
  223.         // Check if language is a known one
  224.         if (!isset($this->_langs[$language])) {
  225.             return false;
  226.         }
  227.  
  228.         // Register translation
  229.         $query 'INSERT INTO
  230.                   ' $this->prefix . 'translations
  231.                   (section_id, section_type, language_id, name, description)
  232.                 VALUES
  233.                   (
  234.                     ' $this->dbc->getValue('integer'$sectionId',
  235.                     ' $this->dbc->getValue('integer'$section_type',
  236.                     ' $this->dbc->getValue('integer'$this->_langs[$language]',
  237.                     ' $this->dbc->getValue('text'$name',
  238.                     ' $this->dbc->getValue('text'$description'
  239.                   )';
  240.  
  241.         $result $this->dbc->query($query);
  242.  
  243.         if (MDB::isError($result)) {
  244.             return $result;
  245.         }
  246.  
  247.         // name (and description) added ...
  248.         return true;
  249.     }
  250.  
  251.     /**
  252.      * Updates name (and description) of [section] in specified language
  253.      *
  254.      * @access public
  255.      * @param  integer id of [section]
  256.      * @param  integer type of section
  257.      * @param  string  language (two letter code) of name/description
  258.      * @param  string  name of [section]
  259.      * @param  string  description of [section]
  260.      * @return mixed boolean or MDB Error object
  261.      */
  262.     function updateTranslation($sectionId$section_type$language$name$description = null)
  263.     {
  264.         // Get all language ids
  265.         if (MDB::isError($result $this->_getLanguages())) {
  266.             return $result;
  267.         }
  268.  
  269.         // Check if language is a known one
  270.         if (!isset($this->_langs[$language])) {
  271.             return false;
  272.         }
  273.  
  274.         // Update translation
  275.         $query 'UPDATE
  276.                   ' $this->prefix . 'translations
  277.                 SET
  278.                   name        = ' $this->dbc->getValue('text'$name',
  279.                   description = ' $this->dbc->getValue('text'$description'
  280.                 WHERE
  281.                   section_id    = ' $this->dbc->getValue('integer'$sectionId' AND
  282.                   section_type  = ' $this->dbc->getValue('integer'$section_type' AND
  283.                   language_id   = ' $this->dbc->getValue('integer'$this->_langs[$language]);
  284.  
  285.         $result $this->dbc->query($query);
  286.  
  287.         if (MDB::isError($result)) {
  288.             return $result;
  289.         }
  290.  
  291.         // Translation name (and description) updated ...
  292.         return true;
  293.     }
  294.  
  295.     /**
  296.      * Remove name (and description) of the [section] in specified language
  297.      *
  298.      * @access public
  299.      * @param  integer id of [section]
  300.      * @param  integer type of section
  301.      * @param  string  language (two letter code) of name/description
  302.      * @param  boolean recursive delete of all translations
  303.      * @return mixed boolean or MDB Error object
  304.      */
  305.     function removeTranslation($sectionId$section_type$language$recursive = false)
  306.     {
  307.         // Get all language ids
  308.         if (MDB::isError($result $this->_getLanguages())) {
  309.             return $result;
  310.         }
  311.  
  312.         // Check if language is a known one
  313.         if (!isset($this->_langs[$language])) {
  314.             return false;
  315.         }
  316.  
  317.         // Remove translation
  318.         $query 'DELETE FROM
  319.                   ' $this->prefix . 'translations
  320.                 WHERE
  321.                   section_id    = ' $this->dbc->getValue('integer'$sectionId' AND
  322.                   section_type  = ' $this->dbc->getValue('integer'$section_type);
  323.         if (!$recursive{
  324.             $query .= ' AND language_id = ' $this->dbc->getValue('integer'$this->_langs[$language]);
  325.         }
  326.  
  327.         $result $this->dbc->query($query);
  328.  
  329.         if (MDB::isError($result)) {
  330.             return $result;
  331.         }
  332.  
  333.         // Translation name (and description) removed ...
  334.         return true;
  335.     }
  336.  
  337.     /**
  338.      * Get name (and description) of the [section] in specified language
  339.      *
  340.      * @access public
  341.      * @param  integer id of [section]
  342.      * @param  integer type of section
  343.      * @return mixed array or MDB Error object
  344.      */
  345.     function getTranslation($sectionId$section_type)
  346.     {
  347.         // get translation
  348.         $query 'SELECT
  349.                   translations.name        AS name,
  350.                   translations.description AS description
  351.                 FROM
  352.                   ' $this->prefix . 'translations translations
  353.                 WHERE
  354.                   section_id    = ' $this->dbc->getValue('integer'$sectionId' AND
  355.                   section_type  = ' $this->dbc->getValue('integer'$section_type);
  356.         $translation $this->dbc->queryRow($querynullMDB_FETCHMODE_ASSOC);
  357.         if (MDB::isError($translation)) {
  358.             return $translation;
  359.         }
  360.  
  361.         if (!is_array($translation)) {
  362.             return array();
  363.         }
  364.         // Translation name (and description) removed ...
  365.         return $translation;
  366.     }
  367.  
  368.     /**
  369.      * Add a new language
  370.      *
  371.      * @access public
  372.      * @param  string two letter code of language
  373.      * @param  string name of language
  374.      * @param  string description of language
  375.      * @return mixed integer (language_id) or MDB Error object
  376.      */
  377.     function addLanguage($two_letter_code$language_name$language_description = null)
  378.     {
  379.         // Get next language id
  380.         $languageId $this->dbc->nextId($this->prefix . 'languages'true);
  381.  
  382.         if (MDB::isError($languageId)) {
  383.             return $languageId;
  384.         }
  385.  
  386.         // Add language
  387.         $query 'INSERT INTO
  388.                   ' $this->prefix . 'languages
  389.                   (language_id, two_letter_name)
  390.                 VALUES
  391.                   (
  392.                     ' $languageId ',
  393.                     ' $this->dbc->getValue('text'$two_letter_code'
  394.                   )';
  395.  
  396.         $result $this->dbc->query($query);
  397.  
  398.         if (MDB::isError($result)) {
  399.             return $result;
  400.         }
  401.  
  402.         // force language reload in case it is the first language we create
  403.         $this->_getLanguages();
  404.  
  405.         if (sizeof($this->_langs== 1{
  406.             $lang $two_letter_code;
  407.         else {
  408.             $lang $this->getCurrentLanguage();
  409.         }
  410.  
  411.         // Insert Language translation into Translations table
  412.         $result $this->addTranslation(
  413.             $languageId,
  414.             LIVEUSER_SECTION_LANGUAGE,
  415.             $lang,
  416.             $language_name,
  417.             $language_description
  418.         );
  419.  
  420.         if (MDB::isError($result)) {
  421.             return $result;
  422.         }
  423.  
  424.         // Clear language cache
  425.         unset($this->_langs);
  426.  
  427.         // Job done ...
  428.         return $languageId;
  429.     }
  430.  
  431.     /**
  432.      * Remove a language
  433.      *
  434.      * @access public
  435.      * @param  integer language (two letter code)
  436.      * @return mixed   boolean or MDB Error object
  437.      */
  438.     function removeLanguage($language)
  439.     {
  440.         // Get all language ids
  441.         if (MDB::isError($result $this->_getLanguages())) {
  442.             return $result;
  443.         }
  444.  
  445.         // Check if language is a known one
  446.         if (!isset($this->_langs[$language])) {
  447.             return false;
  448.         }
  449.  
  450.         // Delete language
  451.         $query 'DELETE FROM
  452.                   ' $this->prefix . 'languages
  453.                 WHERE
  454.                   language_id = ' $this->dbc->getValue('integer'$this->_langs[$language]);
  455.  
  456.         $result $this->dbc->query($query);
  457.  
  458.         if (MDB::isError($result)) {
  459.             return $result;
  460.         }
  461.  
  462.         // Delete language translations
  463.         $result $this->removeTranslation($this->_langs[$language]LIVEUSER_SECTION_LANGUAGE$this->getCurrentLanguage()true);
  464.  
  465.         if (MDB::isError($result)) {
  466.             return $result;
  467.         }
  468.  
  469.         // Clear language cache
  470.         unset($this->_langs);
  471.  
  472.         // Job done ...
  473.         return true;
  474.     }
  475.  
  476.     /**
  477.      * Update language
  478.      *
  479.      * @access public
  480.      * @param  integer language (two letter code)
  481.      * @param  string  name of language
  482.      * @param  string  description of language
  483.      * @return mixed   boolean or MDB Error object
  484.      */
  485.     function updateLanguage($language$language_name$language_description = null)
  486.     {
  487.         // Get all language ids
  488.         if (MDB::isError($result $this->_getLanguages())) {
  489.             return $result;
  490.         }
  491.  
  492.         // Check if language is a known one
  493.         if (!isset($this->_langs[$language])) {
  494.             return false;
  495.         }
  496.  
  497.         // Update Language translation into Translations table
  498.         $result $this->updateTranslation(
  499.             $this->_langs[$language],
  500.             LIVEUSER_SECTION_LANGUAGE,
  501.             $this->getCurrentLanguage(),
  502.             $language_name,
  503.             $langauge_description
  504.         );
  505.  
  506.         if (MDB::isError($result)) {
  507.             return $result;
  508.         }
  509.  
  510.         // Clear language cache
  511.         unset($this->_langs);
  512.  
  513.         // Job done ...
  514.         return true;
  515.     }
  516.  
  517.     /**
  518.      * Add an application
  519.      *
  520.      * @access public
  521.      * @param  string name of application constant
  522.      * @param  string name of application
  523.      * @param  string description of application
  524.      * @return mixed  integer (application_id) or MDB Error object
  525.      */
  526.     function addApplication($define_name = null$application_name = null,
  527.         $application_description = null)
  528.     {
  529.         // Get next application id
  530.         $applicationId $this->dbc->nextId($this->prefix . 'applications'true);
  531.         if (MDB::isError($applicationId)) {
  532.             return $applicationId;
  533.         }
  534.  
  535.         // Register new application
  536.         $query 'INSERT INTO
  537.                   ' $this->prefix . 'applications
  538.                   (application_id, application_define_name)
  539.                 VALUES
  540.                   (
  541.                     ' $this->dbc->getValue('integer'$applicationId',
  542.                     ' $this->dbc->getValue('text'(is_null($define_name$define_name $applicationId)) '
  543.                   )';
  544.  
  545.         $result $this->dbc->query($query);
  546.  
  547.         if (MDB::isError($result)) {
  548.             return $result;
  549.         }
  550.  
  551.         // Insert Application translation into Translations table
  552.         $result $this->addTranslation(
  553.             $applicationId,
  554.             LIVEUSER_SECTION_APPLICATION,
  555.             $this->getCurrentLanguage(),
  556.             $application_name,
  557.             $application_description
  558.         );
  559.  
  560.         if (MDB::isError($result)) {
  561.             return $result;
  562.         }
  563.  
  564.         return $applicationId;
  565.     }
  566.  
  567.     /**
  568.      * Add an area
  569.      *
  570.      * @access public
  571.      * @param  string id of application
  572.      * @param  string name of area constant
  573.      * @param  string name of area
  574.      * @param  string description of area
  575.      * @return mixed  integer (area_id) or MDB Error object
  576.      */
  577.     function addArea($applicationId$define_name = null$area_name = null,
  578.         $area_description = null)
  579.     {
  580.         // Get next area id
  581.         $areaId $this->dbc->nextId($this->prefix . 'areas'true);
  582.  
  583.         if (MDB::isError($areaId)) {
  584.             return $areaId;
  585.         }
  586.  
  587.         // Register new area
  588.         $query 'INSERT INTO
  589.                   ' $this->prefix . 'areas
  590.                   (area_id, area_define_name, application_id)
  591.                 VALUES
  592.                   (
  593.                     ' $this->dbc->getValue('integer'$areaId',
  594.                     ' $this->dbc->getValue('text'(is_null($define_name$define_name $areaId)) ',
  595.                     ' $this->dbc->getValue('integer'$applicationId'
  596.                   )';
  597.  
  598.         $result $this->dbc->query($query);
  599.  
  600.         if (MDB::isError($result)) {
  601.             return $result;
  602.         }
  603.  
  604.         // Insert Area translation into Translations table
  605.         $result $this->addTranslation(
  606.             $areaId,
  607.             LIVEUSER_SECTION_AREA,
  608.             $this->getCurrentLanguage(),
  609.             $area_name,
  610.             $area_description
  611.         );
  612.  
  613.         if (MDB::isError($result)) {
  614.             return $result;
  615.         }
  616.  
  617.         return $areaId;
  618.     }
  619.  
  620.     /**
  621.      * Delete an application
  622.      *
  623.      * @access public
  624.      * @param  integer id of application
  625.      * @return mixed   boolean or MDB Error object or false
  626.      */
  627.     function removeApplication($applicationId)
  628.     {
  629.         if (!is_numeric($applicationId)) {
  630.             return false;
  631.         }
  632.  
  633.         // Get all areas within the application, no matter what language
  634.         $query '
  635.             SELECT
  636.                 area_id
  637.             FROM
  638.             ' $this->prefix . 'areas
  639.             WHERE
  640.                 application_id=' $this->dbc->getValue('integer'$applicationId);
  641.  
  642.         $areas $this->dbc->queryAll($querynullMDB_FETCHMODE_ASSOC);
  643.  
  644.         if (MDB::isError($areas)) {
  645.             return $areas;
  646.         }
  647.  
  648.         // Delete all areas within the application
  649.         if ($areas{
  650.             foreach($areas as $area{
  651.                 $res $this->removeArea($area['area_id']);
  652.                 if (MDB::isError($res)) {
  653.                     return $res;
  654.                 }
  655.             }
  656.         }
  657.  
  658.         // Delete application translations
  659.         $result $this->removeTranslation($applicationIdLIVEUSER_SECTION_APPLICATION$this->getCurrentLanguage()true);
  660.  
  661.         if (MDB::isError($result)) {
  662.             return $result;
  663.         }
  664.  
  665.         // Delete application itself
  666.         $query 'DELETE FROM
  667.                   ' $this->prefix . 'applications
  668.                 WHERE
  669.                   application_id = ' $applicationId;
  670.  
  671.         $result $this->dbc->query($query);
  672.  
  673.         if (MDB::isError($result)) {
  674.             return $result;
  675.         }
  676.  
  677.         return true;
  678.     }
  679.  
  680.     /**
  681.      * Delete an area
  682.      *
  683.      * @access public
  684.      * @param  integer id of area
  685.      * @return mixed   boolean or MDB Error object or false
  686.      */
  687.     function removeArea($areaId)
  688.     {
  689.         if (!is_numeric($areaId)) {
  690.             return false;
  691.         }
  692.  
  693.         // Delete all rights in this area
  694.         $query 'SELECT
  695.                   right_id
  696.                 FROM
  697.                   ' $this->prefix . 'rights
  698.                 WHERE
  699.                   area_id = ' $areaId;
  700.  
  701.         $result $this->dbc->queryCol($query);
  702.  
  703.         if (MDB::isError($result)) {
  704.             return $result;
  705.         }
  706.  
  707.         if (is_array($result)) {
  708.             foreach($result as $rightId{
  709.                 $this->removeRight($rightId);
  710.             }
  711.         }
  712.  
  713.         // Delete area admins
  714.         $query '
  715.             DELETE FROM
  716.                 ' $this->prefix . 'area_admin_areas
  717.             WHERE
  718.                 area_id=' $areaId '
  719.         ';
  720.  
  721.         $result $this->dbc->query($query);
  722.  
  723.         if (MDB::isError($result)) {
  724.             return $result;
  725.         }
  726.  
  727.         // Delete area itself
  728.         $query 'DELETE FROM
  729.                   ' $this->prefix . 'areas
  730.                 WHERE
  731.                   area_id = ' $this->dbc->getValue('integer'$areaId);
  732.  
  733.         $result $this->dbc->query($query);
  734.  
  735.         if (MDB::isError($result)) {
  736.             return $result;
  737.         }
  738.  
  739.         // Delete area translations
  740.         $result $this->removeTranslation($areaIdLIVEUSER_SECTION_AREA$this->getCurrentLanguage()true);
  741.  
  742.         if (MDB::isError($result)) {
  743.             return $result;
  744.         }
  745.  
  746.         return true;
  747.     }
  748.  
  749.     /**
  750.      * Update an application
  751.      *
  752.      * @access public
  753.      * @param  integer id of application
  754.      * @param  string  name of application constant
  755.      * @param  string  name of application
  756.      * @param  string  description of application
  757.      * @return mixed   boolean or MDB Error object
  758.      */
  759.     function updateApplication($applicationId$define_name = null,
  760.         $application_name = null$application_description = null)
  761.     {
  762.         if (!is_null($define_name)) {
  763.             $query 'UPDATE
  764.                       ' $this->prefix . 'applications
  765.                     SET
  766.                       application_define_name = ' $this->dbc->getValue('text'$define_name'
  767.                     WHERE
  768.                       application_id = ' $this->dbc->getValue('integer'$applicationId);
  769.  
  770.             $result $this->dbc->query($query);
  771.             if (MDB::isError($result)) {
  772.                 return $result;
  773.             }
  774.         }
  775.  
  776.         // Update Application translation into Translations table
  777.         $result $this->updateTranslation(
  778.             $applicationId,
  779.             LIVEUSER_SECTION_APPLICATION,
  780.             $this->getCurrentLanguage(),
  781.             $application_name,
  782.             $application_description
  783.         );
  784.  
  785.         if (MDB::isError($result)) {
  786.             return $result;
  787.         }
  788.  
  789.         return true;
  790.     }
  791.  
  792.     /**
  793.      * Update an area
  794.      *
  795.      * @access public
  796.      * @param  integer id of area
  797.      * @param  int     id of application
  798.      * @param  string  name of area constant
  799.      * @param  string  name of area
  800.      * @param  string  description of area
  801.      * @return mixed   boolean or MDB Error object or false
  802.      */
  803.     function updateArea($areaId$applicationId$define_name = null,
  804.         $area_name = null$area_description = null)
  805.     {
  806.         if (!is_numeric($areaId)) {
  807.             return false;
  808.         }
  809.  
  810.         $query 'UPDATE
  811.                   ' $this->prefix . 'areas
  812.                 SET
  813.                   application_id   = ' $this->dbc->getValue('integer'$applicationId',
  814.                   area_define_name = ' $this->dbc->getValue('text'(is_null($define_name$define_name $applicationId)) '
  815.                 WHERE
  816.                   area_id = ' $this->dbc->getValue('integer'$areaId);
  817.  
  818.         $result $this->dbc->query($query);
  819.  
  820.         if (MDB::isError($result)) {
  821.             return $result;
  822.         }
  823.  
  824.         // Update Area translation into Translations table
  825.         $result $this->updateTranslation(
  826.             $areaId,
  827.             LIVEUSER_SECTION_AREA,
  828.             $this->getCurrentLanguage(),
  829.             $area_name,
  830.             $area_description
  831.         );
  832.         if (MDB::isError($result)) {
  833.             return $result;
  834.         }
  835.  
  836.         return true;
  837.     }
  838.  
  839.     /**
  840.      * Add a right in special area
  841.      *
  842.      * @access public
  843.      * @param  integer id of area
  844.      * @param  string  name of right constant
  845.      * @param  string  name of right
  846.      * @param  string  description of right
  847.      * @return mixed   integer (right_id) or MDB Error object
  848.      */
  849.     function addRight($areaId$define_name = null$right_name = null,
  850.         $right_description = null)
  851.     {
  852.         // Get next right id
  853.         $rightId $this->dbc->nextId($this->prefix . 'rights'true);
  854.  
  855.         if (MDB::isError($rightId)) {
  856.             return $rightId;
  857.         }
  858.  
  859.         // Register right
  860.         $query 'INSERT INTO
  861.                   ' $this->prefix . 'rights
  862.                   (right_id, area_id, right_define_name)
  863.                 VALUES
  864.                   (
  865.                     ' $this->dbc->getValue('integer'$rightId',
  866.                     ' $this->dbc->getValue('integer'$areaId',
  867.                     ' $this->dbc->getValue('text'(is_null($define_name$define_name $rightId)) '
  868.                   )';
  869.  
  870.         $result $this->dbc->query($query);
  871.  
  872.         if (MDB::isError($result)) {
  873.             return $result;
  874.         }
  875.  
  876.         // Insert Right translation into Translations table
  877.         $result $this->addTranslation(
  878.             $rightId,
  879.             LIVEUSER_SECTION_RIGHT,
  880.             $this->getCurrentLanguage(),
  881.             $right_name,
  882.             $right_description
  883.         );
  884.  
  885.         if (MDB::isError($result)) {
  886.             return $result;
  887.         }
  888.  
  889.         // Job done ...
  890.         return $rightId;
  891.     }
  892.  
  893.     /**
  894.      * Delete a right
  895.      *
  896.      * @access public
  897.      * @param  integer id of right
  898.      * @return boolean true on success or false on failure
  899.      */
  900.     function removeRight($rightId)
  901.     {
  902.         // Delete userright
  903.         $query 'DELETE FROM
  904.                   ' $this->prefix . 'userrights
  905.                 WHERE
  906.                   right_id = ' $this->dbc->getValue('integer'$rightId);
  907.  
  908.         $result $this->dbc->query($query);
  909.  
  910.         if (MDB::isError($result)) {
  911.             return false;
  912.         }
  913.  
  914.         // Delete right translations
  915.         $result $this->removeTranslation($rightIdLIVEUSER_SECTION_RIGHT$this->getCurrentLanguage()true);
  916.  
  917.         if (MDB::isError($result)) {
  918.             return $result;
  919.         }
  920.  
  921.         // Delete right itself
  922.         $query 'DELETE FROM
  923.                   ' $this->prefix . 'rights
  924.                 WHERE
  925.                   right_id = ' $this->dbc->getValue('integer'$rightId);
  926.  
  927.         $result $this->dbc->query($query);
  928.  
  929.         if (MDB::isError($result)) {
  930.             return false;
  931.         }
  932.  
  933.         // Job done ...
  934.         return true;
  935.  
  936.     }
  937.  
  938.     /**
  939.      * Update a right
  940.      *
  941.      * @access public
  942.      * @param  integer id of right
  943.      * @param  integer id of area
  944.      * @param  string  name of right constant
  945.      * @param  string  name of right
  946.      * @param  string  description of right
  947.      * @return mixed   boolean or MDB Error object
  948.      */
  949.     function updateRight($rightId$areaId$define_name = null,
  950.         $right_name = null$right_description = null)
  951.     {
  952.         $query 'UPDATE
  953.                   ' $this->prefix . 'rights
  954.                 SET
  955.                   area_id           = ' $this->dbc->getValue('integer'$areaId',
  956.                   right_define_name = ' $this->dbc->getValue('text'(is_null($define_name$define_name $areaId)) '
  957.                 WHERE
  958.                   right_id = ' $this->dbc->getValue('integer'$rightId);
  959.  
  960.         $result $this->dbc->query($query);
  961.  
  962.         if (MDB::isError($result)) {
  963.             return $result;
  964.         }
  965.  
  966.         // Update Right translation into Translations table
  967.         $result $this->updateTranslation(
  968.             $rightId,
  969.             LIVEUSER_SECTION_RIGHT,
  970.             $this->getCurrentLanguage(),
  971.             $right_name,
  972.             $right_description
  973.         );
  974.  
  975.         if (MDB::isError($result)) {
  976.             return $result;
  977.         }
  978.  
  979.         // Job done ...
  980.         return true;
  981.     }
  982.  
  983.     /**
  984.      * Add a user
  985.      *
  986.      * @access  public
  987.      * @param   string   $authId    Auth user ID of the user that should be added.
  988.      * @param   string   $authname  Auth container name.
  989.      * @param   int      $type      User type (constants defined in Perm/Common.php) (optional).
  990.      * @param   mixed    $permId    If specificed no new ID will be automatically generated instead
  991.      * @return mixed   string (perm_user_id) or MDB Error object
  992.      */
  993.     function addUser($authId$authName$type = LIVEUSER_USER_TYPE_ID$permId = null)
  994.     {
  995.         if (!$this->init_ok{
  996.             return false;
  997.         }
  998.  
  999.         if (is_null($permId)) {
  1000.             $permId $this->dbc->nextId($this->prefix . 'perm_users'true);
  1001.         }
  1002.  
  1003.         $query '
  1004.             INSERT INTO
  1005.                 ' $this->prefix . 'perm_users
  1006.                 (perm_user_id, auth_user_id, perm_type, auth_container_name)
  1007.             VALUES
  1008.                 (
  1009.                 ' $this->dbc->getValue('integer'$permId',
  1010.                 ' $this->dbc->getValue('text'$authId',
  1011.                 ' $this->dbc->getValue('integer'$type',
  1012.                 ' $this->dbc->getValue('text'$authName'
  1013.                 )';
  1014.  
  1015.         $result $this->dbc->query($query);
  1016.  
  1017.         if (MDB::isError($result)) {
  1018.             return $result;
  1019.         }
  1020.  
  1021.         return $permId;
  1022.     }
  1023.  
  1024.  
  1025.     /**
  1026.      * Updates auth_user_id in the mapping table.
  1027.      *
  1028.      * @access  public
  1029.      * @param   int     perm_user_id of the user
  1030.      * @param   mixed   new Auth user ID
  1031.      * @param   mixed   new Auth Container name
  1032.      * @param   mixed   new perm type
  1033.      * @return  mixed   true or MDB Error object or false if there was an error
  1034.      *                   to begin with
  1035.      */
  1036.     function updateUser($permId$authId = false$authName = false$type = false)
  1037.     {
  1038.         if (!$this->init_ok{
  1039.             return false;
  1040.         }
  1041.  
  1042.         $update = array();
  1043.         if ($authId !== false{
  1044.             $update[' auth_user_id=' $this->dbc->getValue('text'$authId);
  1045.         }
  1046.         if ($authName !== false{
  1047.             $update[' auth_container_name=' $this->dbc->getValue('text'$authName);
  1048.         }
  1049.         if ($type !== false{
  1050.             $update[' perm_type=' $this->dbc->getValue('text'$type);
  1051.         }
  1052.  
  1053.         if (!empty($update)) {
  1054.             $update implode(','$update);
  1055.             $query '
  1056.                 UPDATE
  1057.                     ' $this->prefix . 'perm_users
  1058.                 SET ' $update '
  1059.                 WHERE
  1060.                     perm_user_id=' $this->dbc->getValue('integer'$permId);
  1061.  
  1062.             $result $this->dbc->query($query);
  1063.  
  1064.             if (MDB::isError($result)) {
  1065.                 return $result;
  1066.             }
  1067.         }
  1068.  
  1069.         return true;
  1070.     }
  1071.  
  1072.  
  1073.     /**
  1074.      * Delete user
  1075.      *
  1076.      * @access public
  1077.      * @param  string  id of user
  1078.      * @return mixed   boolean or MDB Error object
  1079.      */
  1080.     function removeUser($permId)
  1081.     {
  1082.         if (!$this->init_ok{
  1083.             return false;
  1084.         }
  1085.  
  1086.         // Delete group assignments
  1087.         $query 'DELETE FROM
  1088.                   ' $this->prefix . 'groupusers
  1089.                 WHERE
  1090.                   perm_user_id = ' $permId;
  1091.  
  1092.         $result $this->dbc->query($query);
  1093.  
  1094.         if (MDB::isError($result)) {
  1095.             return $result;
  1096.         }
  1097.  
  1098.         // Delete right assignments
  1099.         $query 'DELETE FROM
  1100.                   ' $this->prefix . 'userrights
  1101.                 WHERE
  1102.                   perm_user_id = ' $permId;
  1103.  
  1104.         $result $this->dbc->query($query);
  1105.  
  1106.         if (MDB::isError($result)) {
  1107.             return $result;
  1108.         }
  1109.  
  1110.         // remove user area admin relation
  1111.         $result $this->removeUserAreaAdmin($permId);
  1112.  
  1113.         if (MDB::isError($result)) {
  1114.             return $result;
  1115.         }
  1116.  
  1117.         // Delete user from perm table (Perm/MDB)
  1118.         $query '
  1119.             DELETE FROM
  1120.                 ' $this->prefix . 'perm_users
  1121.             WHERE
  1122.                 perm_user_id = ' $permId;
  1123.  
  1124.         $result $this->dbc->query($query);
  1125.  
  1126.         if (MDB::isError($result)) {
  1127.             return $result;
  1128.         }
  1129.  
  1130.         return true;
  1131.     }
  1132.  
  1133.     /**
  1134.      * Grant right to user
  1135.      *
  1136.      * @access public
  1137.      * @param  string  id of user
  1138.      * @param  integer id of right
  1139.      * @return mixed   boolean or MDB Error object
  1140.      */
  1141.     function grantUserRight($permId$rightId)
  1142.     {
  1143.         //return if this user already has right
  1144.         $query 'SELECT
  1145.                   count(*)
  1146.                 FROM
  1147.                   ' $this->prefix . 'userrights
  1148.                 WHERE
  1149.                   perm_user_id = ' $this->dbc->getValue('integer'$rightId'
  1150.                 AND
  1151.                   right_id     = ' $this->dbc->getValue('integer'$rightId);
  1152.  
  1153.         $count $this->dbc->queryOne($query);
  1154.  
  1155.         if (MDB::isError($count|| $count != 0{
  1156.             return false;
  1157.         }
  1158.  
  1159.         $query 'INSERT INTO
  1160.                   ' $this->prefix . 'userrights
  1161.                   (perm_user_id, right_id, right_level)
  1162.                 VALUES
  1163.                   (
  1164.                     ' $this->dbc->getValue('integer'$permId',
  1165.                     ' $this->dbc->getValue('integer'$rightId', '.LIVEUSER_MAX_LEVEL.'
  1166.                   )';
  1167.  
  1168.         $result $this->dbc->query($query);
  1169.  
  1170.         if (MDB::isError($result)) {
  1171.             return $result;
  1172.         }
  1173.  
  1174.         // Job done ...
  1175.         return true;
  1176.     }
  1177.  
  1178.     /**
  1179.      * Update right level of userRight
  1180.      *
  1181.      * @access public
  1182.      * @param  string  id of user
  1183.      * @param  integer id of right
  1184.      * @param  integer right level
  1185.      * @return mixed   boolean or MDB Error object
  1186.      */
  1187.     function updateUserRight($permId$rightId$right_level)
  1188.     {
  1189.         $query 'UPDATE
  1190.                   ' $this->prefix . 'userrights
  1191.                 SET
  1192.                   right_level = ' $this->dbc->getValue('integer'$right_level'
  1193.                 WHERE
  1194.                   perm_user_id = ' $this->dbc->getValue('integer'$permId'
  1195.                 AND
  1196.                   right_id = ' $this->dbc->getValue('integer'$rightId);
  1197.         $result $this->dbc->query($query);
  1198.  
  1199.         if (MDB::isError($result)) {
  1200.             return $result;
  1201.         }
  1202.  
  1203.         // Job done ...
  1204.         return true;
  1205.     }
  1206.  
  1207.     /**
  1208.      * Revoke right from user
  1209.      *
  1210.      * @access public
  1211.      * @param  string  id of user
  1212.      * @param  integer id of right
  1213.      * @return mixed   boolean or MDB Error object
  1214.      */
  1215.     function revokeUserRight($permId$rightId = null)
  1216.     {
  1217.         $query 'DELETE FROM
  1218.                   ' $this->prefix . 'userrights
  1219.                 WHERE
  1220.                   perm_user_id = ' $this->dbc->getValue('integer'$permId);
  1221.         if (!is_null($rightId)) {
  1222.             $query .= ' AND
  1223.               right_id = ' $this->dbc->getValue('integer'$rightId);
  1224.         }
  1225.  
  1226.         $result $this->dbc->query($query);
  1227.  
  1228.         if (MDB::isError($result)) {
  1229.             return $result;
  1230.         }
  1231.  
  1232.         // Job done ...
  1233.         return true;
  1234.     }
  1235.  
  1236.     /**
  1237.      * Get list of all applications
  1238.      *
  1239.      * This method accepts the following options...
  1240.      *  'where_application_id' = [APPLICATION_ID]
  1241.      *
  1242.      * @access public
  1243.      * @param  array an array determining which fields and conditions to use
  1244.      * @return mixed array or MDB Error object
  1245.      */
  1246.     function getApplications($options = null)
  1247.     {
  1248.         $query 'SELECT
  1249.                   applications.application_id          AS application_id,
  1250.                   applications.application_define_name AS define_name,
  1251.                   translations.name                    AS name,
  1252.                   translations.description             AS description
  1253.                 FROM
  1254.                   ' $this->prefix . 'applications applications,
  1255.                   ' $this->prefix . 'translations translations
  1256.                 WHERE';
  1257.  
  1258.         if (isset($options['where_application_id'])
  1259.                 && is_numeric($options['where_application_id'])) {
  1260.             $query .= ' applications.application_id = '
  1261.                 . $this->dbc->getValue('integer'$options['where_application_id']' AND ';
  1262.         }
  1263.  
  1264.         $query .= ' applications.application_id = translations.section_id AND
  1265.                   translations.section_type = '
  1266.                     . LIVEUSER_SECTION_APPLICATION . ' AND
  1267.                   translations.language_id = '
  1268.                     . $this->dbc->getValue('integer'$this->_langs[$this->getCurrentLanguage()]'
  1269.                 ORDER BY
  1270.                   applications.application_id ASC';
  1271.  
  1272.         $applications $this->dbc->queryAll($querynullMDB_FETCHMODE_ASSOC);
  1273.  
  1274.         if (MDB::isError($applications)) {
  1275.             return $applications;
  1276.         }
  1277.  
  1278.         return $applications;
  1279.     }
  1280.  
  1281.     /**
  1282.      * Get list of all areas within a given application
  1283.      *
  1284.      * This method accepts the following options...
  1285.      *  'where_area_id' = [AREA_ID],
  1286.      *  'where_application_id' = [APPLICATION_ID],
  1287.      *  'with_applications' = [BOOLEAN]
  1288.      *
  1289.      * @access public
  1290.      * @param  array an array determining which fields and conditions to use
  1291.      * @return mixed array or MDB Error object
  1292.      */
  1293.     function getAreas($options = null)
  1294.     {
  1295.         $query 'SELECT
  1296.                   areas.area_id            AS area_id,
  1297.                   areas.application_id     AS application_id,
  1298.                   translations.name        AS name,
  1299.                   translations.description AS description,
  1300.                   areas.area_define_name   AS define_name
  1301.                 FROM
  1302.                   ' $this->prefix . 'areas areas,
  1303.                   ' $this->prefix . 'translations translations
  1304.                 WHERE';
  1305.  
  1306.         if (isset($options['where_area_id'])
  1307.                 && is_numeric($options['where_area_id'])) {
  1308.                   $query .= ' areas.area_id=' $this->dbc->getValue('integer'$options['where_area_id']' AND';
  1309.         }
  1310.  
  1311.         if (isset($options['where_application_id'])
  1312.                 && is_numeric($options['where_application_id'])) {
  1313.                   $query .= ' areas.application_id=' $this->dbc->getValue('integer'$options['where_application_id']' AND';
  1314.         }
  1315.  
  1316.         $query .= ' areas.area_id = translations.section_id AND
  1317.                   translations.section_type = '.LIVEUSER_SECTION_AREA . ' AND
  1318.                   translations.language_id = ' $this->dbc->getValue('integer'$this->_langs[$this->getCurrentLanguage()]'
  1319.                 ORDER BY
  1320.                   areas.area_id ASC';
  1321.  
  1322.         $areas $this->dbc->queryAll($querynullMDB_FETCHMODE_ASSOC);
  1323.  
  1324.         if (MDB::isError($areas)) {
  1325.             return $areas;
  1326.         }
  1327.  
  1328.         $_areas = array();
  1329.         if (is_array($areas)) {
  1330.             foreach($areas as $key => $value{
  1331.                 $id $value['area_id'];
  1332.                 $_areas[$id$value;
  1333.  
  1334.                 if (isset($options['with_applications'])) {
  1335.                     $_areas[$id]['application'$this->getTranslation($value['application_id']LIVEUSER_SECTION_APPLICATION);
  1336.                     if (MDB::isError($_areas[$id]['application'])) {
  1337.                         return $_areas[$id]['application'];
  1338.                     }
  1339.                 }
  1340.             }
  1341.         }
  1342.  
  1343.         return $_areas;
  1344.     }
  1345.  
  1346.     /**
  1347.      * Get list of all languages
  1348.      *
  1349.      * This method accepts the following options...
  1350.      *  'where_language_id' = [LANGUAGE_ID],
  1351.      *  'with_translations' = [BOOLEAN]
  1352.      *
  1353.      * @access public
  1354.      * @param  array an array determining which fields and conditions to use
  1355.      * @return mixed array or MDB Error object
  1356.      */
  1357.     function getLanguages($options = null)
  1358.     {
  1359.         $query 'SELECT
  1360.                   languages.language_id     AS language_id,
  1361.                   languages.two_letter_name AS two_letter_code
  1362.                 FROM
  1363.                   ' $this->prefix . 'languages languages';
  1364.  
  1365.         if (isset($options['where_language_id'])
  1366.                 && is_numeric($options['where_language_id'])) {
  1367.             $query .= ' WHERE languages.language_id = ' $this->dbc->getValue('integer'$options['where_language_id']);
  1368.         }
  1369.  
  1370.         $langs $this->dbc->queryAll($querynullMDB_FETCHMODE_ASSOC);
  1371.  
  1372.         if (MDB::isError($langs)) {
  1373.             return $langs;
  1374.         }
  1375.  
  1376.         if (!is_array($langs)) {
  1377.             return array();
  1378.         }
  1379.  
  1380.         if (isset($options['with_translations'])
  1381.                 && $options['with_translations']
  1382.         {
  1383.             $query '
  1384.                     SELECT
  1385.                         translations.section_id       AS section_id,
  1386.                         translations.language_id      AS language_id,
  1387.                         languages.two_letter_name     AS two_letter_code,
  1388.                         translations.name             AS name
  1389.                     FROM
  1390.                         ' $this->prefix . 'languages languages,
  1391.                         ' $this->prefix . 'translations translations
  1392.                     WHERE
  1393.                         languages.language_id = translations.language_id
  1394.                         AND translations.section_type = ' LIVEUSER_SECTION_LANGUAGE;
  1395.  
  1396.             $trans $this->dbc->queryAll($querynullMDB_FETCHMODE_ASSOC);
  1397.  
  1398.             if (MDB::isError($trans)) {
  1399.                 return $trans;
  1400.             }
  1401.         }
  1402.  
  1403.         foreach($langs as $key => $value{
  1404.             unset($langs[$key]);
  1405.             $code $value['two_letter_code'];
  1406.             unset($value['two_letter_code']);
  1407.             $langs[$code$value;
  1408.  
  1409.             if ($options['with_translations'== true && is_array($trans)) {
  1410.                 foreach($trans as $translation{
  1411.                     if ($translation['section_id'== $value['language_id']{
  1412.                         $langs[$code]['name'$translation['name'];
  1413.                     }
  1414.                 }
  1415.             }
  1416.         }
  1417.  
  1418.         return $langs;
  1419.     }
  1420.  
  1421.     /**
  1422.      * Get list of all rights
  1423.      *
  1424.      * This method accepts the following options...
  1425.      *  'where_user_id' = [AUTH_USER_ID],
  1426.      *  'where_group_id' = [GROUP_ID],
  1427.      *  'where_right_id' = [RIGHT_ID],
  1428.      *  'where_area_id' = [AREA_ID],
  1429.      *  'where_application_id' = [APPLICATION_ID],
  1430.      *  'with_areas' = [BOOLEAN],
  1431.      *  'with_applications' = [BOOLEAN]
  1432.      *
  1433.      * @access public
  1434.      * @param  array an array determining which fields and conditions to use
  1435.      * @return mixed array or MDB Error object
  1436.      */
  1437.     function getRights($options = null)
  1438.     {
  1439.         $types = array('integer''integer''integer''text''boolean''boolean''boolean''text''text');
  1440.         $query 'SELECT
  1441.                   rights.right_id      AS right_id,
  1442.                   rights.area_id       AS area_id,
  1443.                   areas.application_id AS application_id,
  1444.                   rights.right_define_name AS define_name,
  1445.                   rights.has_implied       AS has_implied,
  1446.                   rights.has_level         AS has_level,
  1447.                   rights.has_scope         AS has_scope,
  1448.                   translations.name        AS name,
  1449.                   translations.description AS description';
  1450.  
  1451.         if (isset($options['where_user_id'])) {
  1452.             $query .= ', userrights.perm_user_id AS user_id';
  1453.             $type['integer';
  1454.         else if (isset($options['where_group_id'])
  1455.                 && is_numeric($options['where_group_id'])) {
  1456.             $query .= ', grouprights.group_id AS group_id';
  1457.             $type['integer';
  1458.         }
  1459.  
  1460.         $query .= '
  1461.                 FROM
  1462.                   ' $this->prefix . 'rights rights,
  1463.                   ' $this->prefix . 'areas areas,
  1464.                   ' $this->prefix . 'applications applications,';
  1465.  
  1466.         if (isset($options['where_user_id'])) {
  1467.             $query .= ' ' $this->prefix . 'userrights userrights,';
  1468.         }
  1469.  
  1470.         if (isset($options['where_group_id'])
  1471.                 && is_numeric($options['where_group_id'])) {
  1472.             $query .= ' ' $this->prefix . 'grouprights grouprights,';
  1473.         }
  1474.  
  1475.         $query .= ' ' $this->prefix . 'translations translations
  1476.                 WHERE';
  1477.  
  1478.         if (isset($options['where_right_id'])
  1479.                 && is_numeric($options['where_right_id'])) {
  1480.             $query .= ' rights.right_id = '
  1481.                 . $this->dbc->getValue('integer'$options['where_right_id']' AND';
  1482.         }
  1483.  
  1484.         if (isset($options['where_area_id'])
  1485.                 && is_numeric($options['where_area_id'])) {
  1486.             $query .= ' rights.area_id = '
  1487.                 . $this->dbc->getValue('integer'$options['where_area_id']' AND';
  1488.         }
  1489.  
  1490.         if (isset($options['where_application_id'])
  1491.                 && is_numeric($options['where_application_id'])) {
  1492.             $query .= ' areas.application_id = '
  1493.                 . $this->dbc->getValue('integer'$options['where_application_id']' AND';
  1494.         }
  1495.  
  1496.         if (isset($options['where_user_id'])) {
  1497.             $query .= ' userrights.perm_user_id = '
  1498.                 . $this->dbc->getValue('integer'$options['where_user_id']' AND
  1499.                       userrights.right_id = rights.right_id AND';
  1500.         }
  1501.  
  1502.         if (isset($options['where_group_id'])
  1503.                 && is_numeric($options['where_group_id'])) {
  1504.             $query .= ' grouprights.' $this->groupTableCols['required']['group_id']['name'' = '
  1505.                 . $this->dbc->getValue('integer'$options['where_group_id']' AND
  1506.                       grouprights.right_id = rights.right_id AND';
  1507.         }
  1508.  
  1509.         $query .= ' rights.area_id = areas.area_id AND
  1510.                   rights.right_id = translations.section_id AND
  1511.                   translations.section_type = ' LIVEUSER_SECTION_RIGHT . ' AND
  1512.                   translations.language_id = '
  1513.                     . $this->dbc->getValue('integer'$this->_langs[$this->getCurrentLanguage()]'
  1514.                 GROUP BY
  1515.                   rights.right_id, rights.area_id, areas.application_id';
  1516.  
  1517.         if (isset($options['where_user_id'])) {
  1518.             $query .= ',userrights.perm_user_id';
  1519.         }
  1520.  
  1521.         if (isset($options['where_group_id'])
  1522.                 && is_numeric($options['where_group_id'])) {
  1523.             $query .= ',grouprights.group_id';
  1524.         }
  1525.  
  1526.         $query .= '
  1527.                   ,rights.right_define_name, rights.has_implied,
  1528.                   rights.has_level, rights.has_scope,
  1529.                   translations.name, translations.description
  1530.                 ORDER BY
  1531.                   rights.area_id ASC';
  1532.  
  1533.         $rights $this->dbc->queryAll($query$typesMDB_FETCHMODE_ASSOC);
  1534.  
  1535.         if (MDB::isError($rights)) {
  1536.             return $rights;
  1537.         }
  1538.  
  1539.         $_rights = array();
  1540.         if (is_array($rights)) {
  1541.             foreach($rights as $key => $value)
  1542.             {
  1543.                 $id $value['right_id'];
  1544.                 $_rights[$id$value;
  1545.  
  1546.                 if (isset($options['with_areas'])) {
  1547.                     // Add area
  1548.                     $filter = array('where_area_id' => $value['area_id']);
  1549.                     $_rights[$id]['area'=
  1550.                         array_shift($this->getAreas($filter));
  1551.  
  1552.                     if (MDB::isError($_rights[$id]['area'])) {
  1553.                         return $_rights[$id]['area'];
  1554.                     }
  1555.  
  1556.                     if (isset($options['with_applications'])) {
  1557.                         // Add application
  1558.                         $filter = array('where_application_id' => $value['application_id']);
  1559.                         $_rights[$id]['application'=
  1560.                             array_shift($this->getApplications($filter));
  1561.  
  1562.                         if (MDB::isError($_rights[$id]['application'])) {
  1563.                             return $_rights[$id]['application'];
  1564.                         }
  1565.                     }
  1566.                 }
  1567.             }
  1568.         }
  1569.  
  1570.         return $_rights;
  1571.     }
  1572.  
  1573.     /**
  1574.      * Make a user an admin of a given area.
  1575.      *
  1576.      * @access public
  1577.      * @param  mixed  user identifier
  1578.      * @param  int    area identifier
  1579.      * @return mixed  true on success, MDB Error object or false
  1580.      */
  1581.     function addUserAreaAdmin($permId$areaId)
  1582.     {
  1583.         $query '
  1584.             INSERT INTO
  1585.                 ' $this->prefix . 'area_admin_areas
  1586.                 (perm_user_id, area_id)
  1587.             VALUES (
  1588.                 ' $permId ', ' $this->dbc->getValue('integer'$areaId'
  1589.             )
  1590.         ';
  1591.  
  1592.         $result $this->dbc->query($query);
  1593.  
  1594.         if (MDB::isError($result)) {
  1595.             return $result;
  1596.         }
  1597.  
  1598.         return true;
  1599.     }
  1600.  
  1601.     /**
  1602.      * Remove the privilege of being an admin.
  1603.      *
  1604.      * If no area_id is provided the user will be removed asan admin
  1605.      * from all areas he was an admin for.
  1606.      *
  1607.      * @access public
  1608.      * @param  mixed  user identifier
  1609.      * @param  int    area identifier
  1610.      * @return mixed  true on success, MDB Error object or false
  1611.      */
  1612.     function removeUserAreaAdmin($permId$areaId = null)
  1613.     {
  1614.         $query '
  1615.             DELETE FROM
  1616.                 ' $this->prefix . 'area_admin_areas
  1617.             WHERE
  1618.                 perm_user_id=' $permId;
  1619.  
  1620.         if (!is_null($areaId&& is_numeric($areaId)) {
  1621.             $query .= '
  1622.             AND
  1623.                 area_id= ' $this->dbc->getValue('integer'$areaId);
  1624.         }
  1625.  
  1626.         $result $this->dbc->query($query);
  1627.  
  1628.         if (MDB::isError($result)) {
  1629.             return $result;
  1630.         }
  1631.  
  1632.         return true;
  1633.     }
  1634.  
  1635.     /**
  1636.      * Fetch users from the database.
  1637.      *
  1638.      * The only supported filter is perm_user_id => 'value'
  1639.      *
  1640.      * The array will look like this:
  1641.      * <code>
  1642.      * $userData[0]['perm_user_id'] = 1;
  1643.      *             ['type']         = 1;
  1644.      *             ['container']    = '';
  1645.      *             ['rights']       = array(); // the array returned by getRights()
  1646.      * </code>
  1647.      *
  1648.      * @access  public
  1649.      * @param   array   filters to apply to fetched data
  1650.      * @param   boolean  If true the rights for each user will be retrieved.
  1651.      * @param   boolean will return an associative array with the auth_user_id
  1652.      *                   as the key by using the $rekey param in MDB::fetchAll()
  1653.      * @return  mixed    Array with user data or error object.
  1654.      * @see     LiveUser_Admin_Perm_DB_Common::getRights()
  1655.      */
  1656.     function getUsers($filters = array()$options = array()$rekey = false)
  1657.     {
  1658.         $query 'SELECT
  1659.                       users.perm_user_id        AS perm_user_id,
  1660.                       users.auth_user_id        AS auth_user_id,
  1661.                       users.perm_type                AS type,
  1662.                       users.auth_container_name AS container
  1663.                   FROM
  1664.                   ' $this->prefix . 'perm_users users';
  1665.  
  1666.         if (isset($filters['group_id'])) {
  1667.             $query .= ', ' $this->prefix . 'groupusers groupusers';
  1668.         }
  1669.  
  1670.         if (isset($filters['group_id'])) {
  1671.             $filter_array['groupusers.perm_user_id=users.perm_user_id';
  1672.             $filter_array['groupusers.group_id IN (' implode(', '$filters['group_id']')';
  1673.         }
  1674.  
  1675.         if (isset($filters['perm_user_id'])) {
  1676.             $filter_array['users.perm_user_id=' $filters['perm_user_id'];
  1677.         }
  1678.  
  1679.         if (isset($filter_array&& count($filter_array)) {
  1680.             $query .= ' WHERE '.implode(' AND '$filter_array);
  1681.         }
  1682.  
  1683.         $types = array('integer''text''integer''text');
  1684.         $res $this->dbc->queryAll($query$typesMDB_FETCHMODE_ASSOC$rekey);
  1685.  
  1686.         if (is_array($res)) {
  1687.             foreach ($res as $k => $v{
  1688.                 if (isset($options['with_rights'])) {
  1689.                     $res[$k]['rights'$this->getRights(array('where_user_id' => $v['perm_user_id']));
  1690.                 }
  1691.                 if (isset($options['with_groups'])) {
  1692.                     $res[$k]['groups'$this->getGroups(array('where_user_id' => $v['perm_user_id']));
  1693.                 }
  1694.             }
  1695.         else if (!MDB::isError($res)) {
  1696.             $res = array();
  1697.         }
  1698.         return $res;
  1699.     }
  1700. }
  1701. ?>

Documentation generated on Mon, 11 Mar 2019 13:56:19 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.