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

Documentation generated on Mon, 11 Mar 2019 10:16:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.