phpDocumentor lodel-logic
[ class tree: lodel-logic ] [ index: lodel-logic ] [ all elements ]

Source for file class.users.php

Documentation is available at class.users.php

  1. <?php
  2. /**    
  3.  * Logique des utilisateurs
  4.  *
  5.  * PHP versions 4 et 5
  6.  *
  7.  * LODEL - Logiciel d'Edition ELectronique.
  8.  *
  9.  * Home page: http://www.lodel.org
  10.  * E-Mail: lodel@lodel.org
  11.  *
  12.  * All Rights Reserved
  13.  *
  14.  * This program is free software; you can redistribute it and/or modify
  15.  * it under the terms of the GNU General Public License as published by
  16.  * the Free Software Foundation; either version 2 of the License, or
  17.  * (at your option) any later version.
  18.  *
  19.  * This program is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  * GNU General Public License for more details.
  23.  *
  24.  * You should have received a copy of the GNU General Public License
  25.  * along with this program; if not, write to the Free Software
  26.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27.  *
  28.  * @package lodel/logic
  29.  * @author Ghislain Picard
  30.  * @author Jean Lamy
  31.  * @copyright 2001-2002, Ghislain Picard, Marin Dacos
  32.  * @copyright 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  33.  * @copyright 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  34.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  35.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  36.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  37.  * @licence http://www.gnu.org/copyleft/gpl.html
  38.  * @since Fichier ajouté depuis la version 0.8
  39.  * @version CVS:$Id: class.users.php 4361 2008-06-17 09:14:18Z cenou $
  40.  */
  41.  
  42.  
  43. /**
  44.  * Classe de logique des utilisateurs
  45.  * 
  46.  * @package lodel/logic
  47.  * @author Ghislain Picard
  48.  * @author Jean Lamy
  49.  * @copyright 2001-2002, Ghislain Picard, Marin Dacos
  50.  * @copyright 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  51.  * @copyright 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  52.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  53.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  54.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  55.  * @licence http://www.gnu.org/copyleft/gpl.html
  56.  * @since Classe ajouté depuis la version 0.8
  57.  * @see logic.php
  58.  */
  59. class UsersLogic extends Logic 
  60. {
  61.  
  62.     /** Constructor
  63.     */
  64.     function UsersLogic({
  65.         $this->Logic('users');
  66.     }
  67.  
  68.     /**
  69.     *  Indique si un objet est protégé en suppression
  70.     *
  71.     * Cette méthode indique si un objet, identifié par son identifiant numérique et
  72.     * éventuellement son status, ne peut pas être supprimé. Dans le cas où un objet ne serait
  73.     * pas supprimable un message est retourné indiquant la cause. Sinon la méthode renvoit le
  74.     * booleen false.
  75.     *
  76.     * @param integer $id identifiant de l'objet
  77.     * @param integer $status status de l'objet
  78.     * @return false si l'objet n'est pas protégé en suppression, un message sinon
  79.     */
  80.     function isdeletelocked($id,$status=0
  81.     {
  82.         global $lodeluser;
  83.         if ($lodeluser['id']==$id && 
  84.     ( ($GLOBALS['site'&& $lodeluser['rights']<LEVEL_ADMINLODEL||
  85.         (!$GLOBALS['site'&& $lodeluser['rights']==LEVEL_ADMINLODEL))) {
  86.             return getlodeltextcontents("cannot_delete_current_user","common");
  87.         else {
  88.             return false;
  89.         }
  90.         //) { $error["error_has_entities"]=$count; return "_back"; }
  91.     }
  92.  
  93.  
  94.     /**
  95.      * Suppression du log des sessions d'un utilisateur
  96.      *
  97.      * Cette action permet de supprimer soit :
  98.      * - toutes les sessions d'un utilisateur, do=deletesession&lo=users&id=xx
  99.      * - une session particulière : do=deletesession&lo=users&session=xx
  100.      *
  101.      * @param array $context le contexte passé par référence
  102.      * @param array $error les erreur éventuelles par référence
  103.      */
  104.     function deletesessionAction(&$context&$error)
  105.     {
  106.         global $db;
  107.         require_once 'func.php';
  108.         $id intval($context['id']);
  109.         $session     intval($context['session']);
  110.         usemaindb()// les sessions sont stockés dans la base principale
  111.         $ids array();
  112.         if ($id//suppression de toutes les sessions
  113.             $result $db->execute(lq("SELECT id FROM #_MTP_session WHERE iduser='".$id."'")) or dberror();
  114.             while(!$result->EOF{
  115.                 $ids[$result->fields['id'];
  116.                 $result->MoveNext();
  117.             }
  118.         elseif ($session//suppression d'une session
  119.             $ids[$session;
  120.         else {
  121.             die ('ERROR: unknown operation');
  122.         }
  123.         
  124.         if ($ids{
  125.             $idstr join(','$ids);
  126.             // remove the session
  127.             $db->execute(lq("DELETE FROM #_MTP_session WHERE id IN ($idstr)")) or dberror();
  128.             // remove the url related to the session
  129.             $db->execute(lq("DELETE FROM #_MTP_urlstack WHERE idsession IN ($idstr)")) or dberror();
  130.         }
  131.  
  132.         usecurrentdb();
  133.         update();
  134.         return '_back';
  135.     }
  136.  
  137.     /**
  138.      * Permet de régler la langue ou le mode traduction d'un utilisateur
  139.      *
  140.      * Pour changer la langue d'un utilisateur : lo=users&do=set&lang=fr
  141.      * Pour changer le mode traduction : lo=users&do=set&translationmode=off
  142.      * @param array $context le contexte passé par référence
  143.      * @param array $error les erreur éventuelles par référence
  144.      */
  145.     function setAction(&$context&$error)
  146.     {
  147.         global $db;
  148.         $lang $context['lang'];
  149.         $translationmode $context['translationmode'];
  150.         if ($lang{
  151.             if (!preg_match("/^\w\w(-\w\w)?$/",$lang)) {
  152.                 die("ERROR: invalid lang");
  153.             }
  154.             $db->execute(lq("UPDATE #_TP_users SET lang='$lang'")) or dberror();
  155.             $this->_setcontext('lang''setvalue'$lang);
  156.         }
  157.  
  158.         if ($translationmode{
  159.             switch($translationmode{
  160.             case 'off':
  161.                 $this->_setcontext('translationmode''clear');
  162.                 break;
  163.             case 'site':
  164.             case 'interface':
  165.                 $this->_setcontext('translationmode''setvalue'$translationmode);
  166.                 break;
  167.             }
  168.         }
  169.  
  170.     update();
  171.     return '_back';
  172.     }
  173.  
  174.  
  175.  
  176.     /**
  177.         * make the select for this logic
  178.         */
  179.     function makeSelect(&$context,$var)
  180.  
  181.     {
  182.         switch($var{
  183.         case 'usergroups' :
  184.             $dao=&getDAO("usergroups");
  185.             $list=$dao->findMany("status>0","rank,name","id,name");
  186.             $arr=array();
  187.             foreach($list as $group{
  188.     $arr[$group->id]=$group->name;
  189.             }
  190.             if (!$arr$arr[1'--';
  191.             renderOptions($arr,$context['usergroups']);
  192.             break;
  193.         case 'gui_user_complexity' :
  194.             require_once 'commonselect.php';
  195.             makeSelectGuiUserComplexity($context['gui_user_complexity']);
  196.             break;
  197.         case 'userrights':
  198.             require_once 'commonselect.php';
  199.             makeSelectUserRights($context['userrights'],!$GLOBALS['site'|| SINGLESITE);
  200.             break;
  201.         case "lang" :
  202.             // get the language available in the interface
  203.             
  204.             $dao=&getDAO("translations");
  205.             $list=$dao->findMany("status>0 AND textgroups='interface'","rank,lang","lang,title");
  206.             $arr=array();
  207.             foreach($list as $lang{
  208.     $arr[$lang->lang]=$lang->title;
  209.             }
  210.             if (!$arr$arr['fr']="Francais";
  211.             renderOptions($arr,$context['lang']);
  212.         }
  213.     }
  214.  
  215.     /*---------------------------------------------------------------*/
  216.     //! Private or protected from this point
  217.     /**
  218.         * @private
  219.         */
  220.     /**
  221.      *
  222.      *
  223.      */
  224.     function _setcontext($var$operation$value '')
  225.     {
  226.         global $db;
  227.         usemaindb();
  228.         $where "name='"addslashes($_COOKIE[$GLOBALS['sessionname']])"' AND iduser='"$GLOBALS['lodeluser']['id']"'";
  229.         $context $db->getOne(lq("SELECT context FROM $GLOBALS[tp]session WHERE ".$where));
  230.         if ($db->errorno()) {
  231.             dberror();
  232.         }
  233.         $arr unserialize($context);
  234.         switch ($operation{
  235.         case 'toggle' :
  236.             $arr[$var$arr[$var1// toggle
  237.             break;
  238.         case 'setvalue' :
  239.             $arr[$var$value;  // set
  240.             break;
  241.         case 'clear' :
  242.             unset($arr[$var]);  // clear
  243.             break;
  244.         }
  245.     
  246.         $db->execute(lq("UPDATE #_MTP_session SET context='"addslashes(serialize($arr))"' WHERE ".$where)) or dberror();
  247.         usecurrentdb();
  248.     }
  249.  
  250.     /**
  251.     * Préparation de l'action Edit
  252.     *
  253.     * @access private
  254.     * @param object $dao la DAO utilisée
  255.     * @param array &$context le context passé par référence
  256.     */
  257.     function _prepareEdit($dao,&$context
  258.     {
  259.         // encode the password
  260.         if ($context['passwd']$context['passwd']=md5($context['passwd'].$context['username']);
  261.     }
  262.  
  263.  
  264.  
  265.     function _populateContextRelatedTables(&$vo,&$context)
  266.  
  267.     {
  268.         if ($vo->userrights<=LEVEL_EDITOR{
  269.             $dao=&getDAO("users_usergroups");
  270.             $list=$dao->findMany("iduser='"$vo->id."'""""idgroup");
  271.             $context['usergroups']=array();
  272.             foreach($list as $relationobj{
  273.                 $context['usergroups'][$relationobj->idgroup;
  274.             }
  275.         }
  276.     }
  277.     /**
  278.     * Sauve des données dans des tables liées éventuellement
  279.     *
  280.     * Appelé par editAction pour effectuer des opérations supplémentaires de sauvegarde.
  281.     *
  282.     * @param object $vo l'objet qui a été créé
  283.     * @param array $context le contexte
  284.     */
  285.     function _saveRelatedTables($vo,$context
  286.  
  287.     {
  288.         global $db;
  289.         if ($vo->userrights<=LEVEL_EDITOR{
  290.             if (!$context['usergroups']$context['usergroups']=array(1);
  291.  
  292.             // change the usergroups     
  293.             // first delete the group
  294.             $this->_deleteRelatedTables($vo->id);
  295.             // now add the usergroups
  296.             foreach ($context['usergroups'as $usergroup{
  297.     $usergroup=intval($usergroup);
  298.     $db->execute(lq("INSERT INTO #_TP_users_usergroups (idgroupiduserVALUES  ('$usergroup','$id')")) or dberror();
  299.             }
  300.         }
  301.     }
  302.  
  303.     function _deleteRelatedTables($id{
  304.         global $db;
  305.         if ($GLOBALS['site']// only in the site table
  306.             $db->execute(lq("DELETE FROM #_TP_users_usergroups WHERE iduser='$id'")) or dberror();
  307.         }
  308.     }
  309.  
  310.  
  311.     function validateFields(&$context,&$error{
  312.         global $db,$lodeluser;
  313.  
  314.         if (!Logic::validateFields($context,$error)) return false;
  315.  
  316.         // check the user has the right equal or higher to the new user
  317.         if ($lodeluser['rights']<$context['userrights']die("ERROR: You don't have the right to create a user with rights higher than yours");
  318.  
  319.         // Check the user is not duplicated in the main table...
  320.         if (!usemaindb()) return true// use the main db, return if it is the same as the current one.
  321.  
  322.         $ret=$db->getOne("SELECT 1 FROM ".lq("#_TP_".$this->maintable)." WHERE status>-64 AND id!='".$context['id']."' AND username='".$context['username']."'");
  323.         if ($db->errorno()) die($this->errormsg());
  324.         usecurrentdb();
  325.  
  326.         // check the passwd is given for new user.
  327.         if (!$context['id'&& !trim($context['passwd'])) {
  328.             $error['passwd']=1;
  329.             return false;
  330.         }
  331.  
  332.         if ($ret{
  333.             $error['username']="1"// report the error on the first field
  334.             return false;
  335.         else {
  336.             return true;
  337.         }
  338.     }
  339.  
  340.  
  341.     // begin{publicfields} automatic generation  //
  342.  
  343.     /**
  344.      * Retourne la liste des champs publics
  345.      * @access private
  346.      */
  347.     function _publicfields(
  348.     {
  349.         return array('username' => array('username''+'),
  350.                                     'passwd' => array('passwd'''),
  351.                                     'lastname' => array('text'''),
  352.                                     'firstname' => array('text'''),
  353.                                     'email' => array('email''+'),
  354.                                     'lang' => array('lang''+'),
  355.                                     'userrights' => array('select''+'),
  356.                                     'gui_user_complexity' => array('select''+'),
  357.                                     'nickname' => array('text'''),
  358.                                     'biography' => array('longtext'''),
  359.                                     'photo' => array('image'''),
  360.                                     'professional_website' => array('text'''),
  361.                                     'url_professional_website' => array('url'''),
  362.                                     'rss_professional_website' => array('url'''),
  363.                                     'personal_website' => array('text'''),
  364.                                     'url_personal_website' => array('url'''),
  365.                                     'rss_personal_website' => array('url'''),
  366.                                     'pgp_key' => array('longtext'''),
  367.                                     'alternate_email' => array('email'''),
  368.                                     'phonenumber' => array('text'''),
  369.                                     'im_identifier' => array('text'''),
  370.                                     'im_name' => array('text'''));
  371.     }
  372.     // end{publicfields} automatic generation  //
  373.  
  374.     // begin{uniquefields} automatic generation  //
  375.  
  376.     /**
  377.      * Retourne la liste des champs uniques
  378.      * @access private
  379.      */
  380.     function _uniqueFields(
  381.     
  382.         return array(array('username'));
  383.     }
  384.     // end{uniquefields} automatic generation  //
  385.  
  386.     /**
  387.      * Bloque un compte utilisateur tant que celui-ci n'a pas modifié son mot de passe
  388.      */
  389.  
  390.     function suspendAction()
  391.     {
  392.          global $db$id$site$lodeluser;
  393.  
  394.         //on vérifie qu'on est bien administrateur
  395.         if($lodeluser['rights'>= 40{
  396.             $prefixe ($site != '' && $site != "tous les sites""#_TP_" "#_MTP_";
  397.     
  398.             $status $db->getOne(lq("SELECT status FROM ".$prefixe."users WHERE id = '".$id."'"));
  399.     
  400.             if($status != -40 && $status != 32)
  401.                 $stat 10;
  402.             else
  403.                 $stat 11;
  404.             
  405.             $db->execute(lq("UPDATE ".$prefixe."users SET status = ".$stat." WHERE id = '".$id."'"));
  406.         else
  407.             die("ERROR : You don't have permissions to suspend this user. Contact your administrator.");
  408.         
  409.         return "_back";
  410.     }
  411.  
  412. // class 
  413.  
  414.  
  415. /*-----------------------------------*/
  416. /* loops                             */
  417.  
  418. ?>

Documentation generated on Thu, 24 Jul 2008 05:08:23 +0200 by phpDocumentor 1.4.0a2