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

Source for file DB_Simple.php

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

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