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

Source for file MDB_Simple.php

Documentation is available at MDB_Simple.php

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

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