Source for file OCSP_USER.phpclass

Documentation is available at OCSP_USER.phpclass

  1. <?php
  2. /**
  3.  * Class file OCSP_USER.phpclass
  4.  *
  5.  * @project    Open CSP-Management
  6.  * @package    user
  7.  *
  8.  * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  9.  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  *
  11.  * @version    pk-03-12-30
  12.  * @version    $Id: OCSP_USER.phpclass,v 1.28 2008/11/24 23:30:34 pitlinz Exp $
  13.  */
  14.  
  15. if (!defined('_OCSP_USR_SESSION_TTL_')) define('_OCSP_USR_SESSION_TTL_',600);
  16.  
  17. if (!class_exists('DBMS_TABLEOBJ'))
  18.  
  19.  
  20. if (!class_exists('DBMS_TABLEOBJ'))
  21. {
  22.     require_once __OCSP_PHPINCPATH__ "db" _OCSP_DIRSEP_ "DBMS_TABLEOBJ.phpclass";
  23. }
  24.  
  25. if (!class_exists('OCSP_GROUPTREE'))
  26. {
  27.     require_once dirname(__FILE___OCSP_DIRSEP_ "OCSP_GROUPTREE.phpclass";
  28. }
  29.  
  30.  
  31. /**
  32.  * handels  users
  33.  *
  34.  * @project    Open CSP-Management
  35.  * @package    user
  36.  *
  37.  * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  38.  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  39.  *
  40.  *  Version History:
  41.  *  ------------------------------------------------
  42.  * @version pk-03-12-30
  43.  * @version pk-08-03-12 (unit of work)
  44.  * @version    $Id: OCSP_USER.phpclass,v 1.28 2008/11/24 23:30:34 pitlinz Exp $
  45.  *
  46.  */
  47. class OCSP_USER
  48.     extends DBMS_TABLEOBJ
  49. {
  50.  
  51.     /*** class constants  --------------------------------------------- */
  52.  
  53.     /**
  54.      * @constant string CLASS_SRC_FILE
  55.      *
  56.      * @abstract
  57.      */
  58.     const CLASS_SRC_FILE = __FILE__;
  59.  
  60.     /*** class variables  --------------------------------------------- */
  61.  
  62.     /**
  63.      * list of already loaded users
  64.      * (unit of work) used in foactory classes
  65.      *
  66.      * @staticvar array $usrList 
  67.      */
  68.     static $usrList array();
  69.  
  70.     /*** class functions  --------------------------------------------- */
  71.     
  72.     /**
  73.      * outputs the propper login form
  74.      *
  75.      * tries to get the proper method to output a login form
  76.      * 
  77.      * - $_FILES
  78.      *         if on or more file uploads are found getHttpLoginForm is used
  79.      * 
  80.      * - SEC_REQ[CONTENTTYPE]
  81.      *        if isset an ajax call is supposed and the propper
  82.      *      auth form method is chosen by the content type
  83.      * 
  84.      * - $_GET['SecReq']
  85.      *         if isset and $_GET['inToDiv'] an jOCSP.load(...) is supposed
  86.      *         else jOCSP.getScript is supposed
  87.      * 
  88.      * NOTE to avoid missinterpretion always set SEC_REQ['CONTENTTYPE']
  89.      * in ajax answers
  90.      * 
  91.      * NOTE do not set SEC_REQ['CONTENTTYPE'] if you have a normal non ajax call
  92.      * 
  93.      * 
  94.      * 
  95.      * @param array $secReq 
  96.      * 
  97.      * @return boolean 
  98.      * 
  99.      */
  100.     static function echoLoginForm($secReq)
  101.     {
  102.         if (OCSP_CONF::getInstance()->getValue('LOGIN_WWW-AUTH'))
  103.         {
  104.             return self::getHttpLoginForm();
  105.         }
  106.         
  107.         if (sizeof($_FILES)) 
  108.         {
  109.             // we have an upload -> send http auth to not have to handle 
  110.             // the upload file
  111.             return self::getHttpLoginForm();
  112.         }
  113.         
  114.         if (isset($_SERVER['HTTP_X_REQUESTED_WITH']&& ($_SERVER['HTTP_X_REQUESTED_WITH'== 'XMLHttpRequest'))
  115.         {
  116.             if (!isset($secReq['CONTENTTYPE']))
  117.             {
  118.                 if (isset($_GET['inToDiv']&& !empty($_GET['inToDiv']))
  119.                 {
  120.                     $secReq['CONTENTTYPE']='text/html';        
  121.                 else {
  122.                     $secReq['CONTENTTYPE']='text/javascript';
  123.                 }
  124.             }
  125.                 
  126.             if (isset($secReq['CONTENTTYPE']))
  127.             {
  128.                 switch($secReq['CONTENTTYPE'])
  129.                 {
  130.                     case "text/javascript":
  131.                     case "text/html":
  132.                     default
  133.                         return self::getHttpLoginForm();
  134.                 }            
  135.             }
  136.         }
  137.             
  138.         $str_loginUrl OCSP_OBJ::getConf('LOGINURL');
  139.         if (empty($str_loginUrl)) $str_loginUrl OCSP_OBJ::getConf('SYSTEMURL'"/tools/login.php";
  140.         
  141.         OCSP_SESSION::getInstance()->setDefaultValue('LOGIN_POST',$_POST);
  142.         if (isset($_SERVER['HTTP_REFERER'])) OCSP_SESSION::getInstance()->setDefaultValue('LOGIN_REFERER',$_SERVER['HTTP_REFERER']);
  143.         OCSP_SESSION::getInstance()->setValue('LOGIN_ACTION',$_SERVER['REQUEST_URI']);
  144.         
  145.         $str_loginUrl pcf_HTML_changeURI_GetValue($str_loginUrl,'LOGIN',Null);
  146.         
  147.           if ($debugechoDebugLine(__FILE__,__LINE__,"<a href=\"$str_loginUrl\">$str_loginUrl</a><pre>".print_r($_SESSION,TRUE)."</pre>");
  148.         header("Location$str_loginUrl");        
  149.             
  150.     }
  151.     
  152.     /**
  153.      * sets the headers for a http authentication
  154.      * 
  155.      * NOTE: if you use this method logout is currently not available
  156.      * 
  157.      * @return boolean 
  158.      */
  159.     static function getHttpLoginForm()
  160.     {
  161.         $str_realm OCSP_CONF::getInstance()->getValue('LOGIN_REALM');
  162.         $str_realm (empty($str_realm"openCSP Login" $str_realm);
  163.            header("WWW-AuthenticateBasic realm=\"{$str_realm}\"");
  164.         header("HTTP/1.0 401 Unauthorized");
  165.  
  166.         return True;
  167.     }
  168.     
  169.     static function getJavaScriptLoginForm()
  170.     {
  171.         // Bust cache in the head
  172.         header ("Expires: Mon, 26 Feb 2007 00:00:00 GMT");    // Date in the past
  173.         header ("Last-Modified: " gmdate("D, d M Y H:i:s"" GMT");
  174.         // always modified
  175.         header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
  176.         header ("Pragma: no-cache");                          // HTTP/1.0
  177.         // set content type
  178.         header('Content-Type: text/javascript; charset=utf-8');
  179.  
  180.         
  181.  
  182.     }
  183.     
  184.     
  185.     /**
  186.      * compositon ----------------------------------------------------------------
  187.      */
  188.  
  189.     /**
  190.      * array of groups the user belongs to directly
  191.      * (stored in T_SYS_GROUPMEMBERS);
  192.      * 
  193.      * @var array $myGroups 
  194.      */
  195.     protected $myGroups = array();
  196.     
  197.     
  198.     /**
  199.      * attributes ----------------------------------------------------------------
  200.      */
  201.  
  202.     /**
  203.      * array holding security requirements for the current function
  204.      * @var array $curSecReq 
  205.      */
  206.     protected $curSecReq = array();
  207.     
  208.     // ---------------- DB Attributes -------------------------
  209.  
  210.  
  211.     /**
  212.      * @staticvar string $myTable name of the db table
  213.      */
  214.     var $myTable="T_SYS_USER";
  215.  
  216.     /**
  217.      * @var string $dbConffile 
  218.      * @access protected
  219.      */
  220.     protected $dbConffile = "";
  221.     
  222.     /**
  223.      * @var int $dbConfSeekLoop count loops in self::getDBConfFile()
  224.      */
  225.     private $dbConfSeekLoop = 0;
  226.     
  227.     // ---------------- Client Attributes -------------------------
  228.     /**
  229.      * @var int $myCliId the client id the user belongs to
  230.      * @since pk-06-04-19
  231.      ***/
  232.     protected $myCliId=0;
  233.  
  234.     /**
  235.      * @var CLIENT $myClient the client object
  236.      * @since pk-06-07-21
  237.      * @deprecated since pk-08-03-27 (CLIENT -> unit of work)
  238.      ***/
  239.     protected $myClient=NULL;    
  240.     
  241.     /**
  242.      * array of client id's the user is assigned
  243.      *
  244.      * @var array $myClientLst 
  245.      * @since pk-08-03-27
  246.      */
  247.     protected $myClientLst = array();
  248.         
  249.     
  250.     /**
  251.      * @var int $myCtyId the client type of the users client
  252.      * @since pk-06-07-21
  253.      ***/
  254.     protected $myCtyId=0;
  255.  
  256.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  257.     // constructor / factory
  258.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  259.  
  260.     /**
  261.      * constructor
  262.      *
  263.      * @param boolean $debug 
  264.      */
  265.     function __construct($debug=False)
  266.     {
  267.         //$this->init($debug);
  268.     }
  269.  
  270.     /**
  271.      * factory a userObject
  272.      *
  273.      * @param int $aId 
  274.      * @param boolean $debug 
  275.      *
  276.      * @return OCSP_USER 
  277.      */
  278.     static function &factoryFromId($aId,$debug=False)
  279.     {
  280.         $obj_ret Null;
  281.         if ($debugechoDebugMethod(__FILE__,"static","OCSP_USER::factoryFromId($aId)");
  282.         if ($aId intval($aId))
  283.         {
  284.             if (pcf_is_instance_of(OCSP_USER::$usrList[$aId],'OCSP_USER'))
  285.             {
  286.                 if ($debugechoDebugLine(__FILE__,__LINE__,"returning already loaded user for {$aId}");
  287.                 return OCSP_USER::$usrList[$aId];
  288.             }
  289.  
  290.             if ($debugechoDebugLine(__FILE__,__LINE__,"generating new user");
  291.             $obj_ret new OCSP_USER();
  292.             $obj_ret->db_SetKey('USR_ID',$aId,$debug);
  293.             if ($obj_ret->dbPopulate($debug))
  294.             {
  295.                 OCSP_USER::$usrList[$aId]=$obj_ret;
  296.                 return OCSP_USER::$usrList[$aId];
  297.             }
  298.             return $obj_ret;
  299.         }
  300.          
  301.         return $obj_ret;
  302.     }
  303.     
  304.     /**
  305.      * factory a userObject
  306.      *
  307.      * @param string $lName 
  308.      * @param boolean $debug 
  309.      *
  310.      * @return OCSP_USER 
  311.      */    
  312.     static function factoryFromLoginName($lName,$debug=False)
  313.     {
  314.         if (is_array(OCSP_USER::$usrList))
  315.         {
  316.             foreach(OCSP_USER::$usrList as $aId => &$obj_user)
  317.             {
  318.                 if ($obj_user->getName(== $lName)
  319.                 {
  320.                     return $obj_user;
  321.                 }
  322.             }
  323.         }
  324.         
  325.         $obj_db OCSP_OBJ::defaultDBObj('r');
  326.         $str_query "SELECT * FROM " $this->myTable;
  327.         $str_query.= " WHERE USR_LOGIN = " $obj_db->qs_getSlashedValue($lName);
  328.         
  329.         if ($arr_usr $obj_db->quickQuery($str_query))
  330.         {
  331.             OCSP_USER::$usrList[$arr_usr['USR_ID']] new OCSP_USER();
  332.             OCSP_USER::$usrList[$arr_usr['USR_ID']]->setDBRow($arr_user);
  333.             return OCSP_USER::$usrList[$arr_usr['USR_ID']];
  334.         
  335.         return NULL;
  336.     }
  337.     
  338.  
  339.     /**
  340.      * factory a user object from the session
  341.      *
  342.      * @param boolean $debug 
  343.      * 
  344.      * @return OCSP_USER 
  345.      */
  346.     static function &factoryCurrentFromSession($debug=False)
  347.     {        
  348.         if ($debugechoDebugMethod(__FILE__,"static","OCSP_USER::factoryCurrentFromSession()");
  349.         
  350.         pcf_require_class('OCSP_SESSION',__OCSP_PHPINCPATH__ "common" _OCSP_DIRSEP_ "OCSP_SESSION.phpclass");
  351.         
  352.         if ($str_class OCSP_SESSION::getInstance(True)->getValue('USRCLASS'))
  353.         {
  354.             if (!class_exists($str_class&& ($str_src OCSP_SESSION::getInstance(True)->getValue('USRCLASSSRC'))) 
  355.             {
  356.                 require_once $str_src;
  357.             else {
  358.                 throw new Exception(_OCSP_EXCEP_CLASSNOTFOUND_ ": " $str_class);
  359.             }
  360.             
  361.             $str_cmd "\$obj_user = new " $str_class "();";
  362.             eval($str_cmd);
  363.         else {
  364.             $obj_user new OCSP_USER();
  365.         }
  366.  
  367.         if ($debugechoDebugLine(__FILE__,__LINE__,"USER: <pre> ".print_r($obj_user,True)."</pre>");
  368.         
  369.         $obj_user->sessPopulate($debug);
  370.         if (!$obj_user->isPublic())
  371.         {
  372.             OCSP_USER::$usrList[$obj_user->getId()$obj_user;            
  373.         }
  374.         
  375.         self::$global_currentUser=$obj_user;        
  376.         return $obj_user;
  377.     }
  378.  
  379.     /**
  380.      * factory a user object from login + password
  381.      *
  382.      * @param string $login 
  383.      * @param string $passwd 
  384.      * @param string $className 
  385.      * @param boolean $debug 
  386.      *
  387.      * @return unknown 
  388.      */
  389.     static function factoryCurrentFromLoginPasswd($login,$passwd,$className=NULL,$debug=False)
  390.     {
  391.         if ($debugechoDebugMethod(__FILE__,"static","OCSP_USER::factoryCurrentFromLoginPasswd()");
  392.         if (empty($login))
  393.         {
  394.             return NULL;
  395.         }
  396.  
  397.         if (!empty($className&& ($className != 'OCSP_USER'))
  398.         {
  399.             if (!class_exists($className,True))
  400.             {
  401.                 throw new Exception(_OCSP_EXCEP_CLASSNOTFOUND_ ":" $className);
  402.             }
  403.                 
  404.             $str_cmd "\$obj_user = new " $className "();";
  405.             eval($str_cmd);
  406.         else {
  407.             $obj_user new OCSP_USER();
  408.         }
  409.  
  410.         if ($debugechoDebugLine(__FILE__,__LINE__,"<pre>" print_r($obj_user,True"</pre>");
  411.         if (!$obj_user->checkPassword(NULL,$login,$passwd,$debug))
  412.         {
  413.             return NULL;
  414.         }
  415.  
  416.         if ($obj_user->isValid($debug&& ($obj_user->isEnabled()))
  417.         {
  418.             if (!$obj_user->isPublic())
  419.             {
  420.                 OCSP_USER::$usrList[$obj_user->getId()$obj_user;            
  421.             }            
  422.             self::$global_currentUser $obj_user;
  423.             global $OCSP_OBJ;
  424.             if (is_array($OCSP_OBJ))
  425.             {
  426.                 $OCSP_OBJ['USER'&self::$global_currentUser;
  427.             }
  428.                 
  429.             return self::$global_currentUser;
  430.         }
  431.  
  432.         return NULL;
  433.  
  434.     }
  435.  
  436.     // ---------------------------------------------------------
  437.     // getter/setter
  438.     // ---------------------------------------------------------
  439.  
  440.     /**
  441.      * returns the user id
  442.      *
  443.      * @return int 
  444.      */
  445.     public function getId()
  446.     {
  447.         return intval($this->getDBField('USR_ID'));
  448.     }
  449.  
  450.     /**
  451.      * returns the user naem
  452.      *
  453.      * @return string 
  454.      */
  455.     public function getName()
  456.     {
  457.         if ($this->getId())
  458.         {
  459.             $str_ret $this->getDBField('USR_NAME');
  460.             if (empty($str_ret))
  461.             {
  462.                 return $this->getDBField('USR_LOGIN');
  463.             else {
  464.                 return $str_ret;
  465.             }
  466.         else if ($this->isValid()) {
  467.             return "PUBLIC";
  468.         }        
  469.     }
  470.  
  471.     
  472.     /**
  473.      * returns the language id for the user
  474.      * 
  475.      * @return string 
  476.      * 
  477.      * @since pk-08-06-02
  478.      */
  479.     public function getLang()
  480.     {
  481.         return $this->getDBField('USR_LANG');
  482.     }
  483.     
  484.     /**
  485.      * disable a user
  486.      *
  487.      * @param boolean $debug 
  488.      * @since pk-05-06-27
  489.      *
  490.      ***/
  491.     function disable($debug=False
  492.     {
  493.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::disable()","USR_ID: ".$this->getId());
  494.  
  495.         if (!$this->isConnected(True))
  496.         {
  497.             throw new Exception(_OCSP_EXEP_NODBCONN_);
  498.         }
  499.         
  500.         $cmd="UPDATE T_SYS_USER SET USR_ENABLED=0 WHERE USR_ID=".$this->getId();
  501.         $this->myDBObj->executeCmd($cmd);
  502.         
  503.         if ($this->isCurrent())
  504.         {
  505.             $this->setDBField('USR_ENABLED',0);
  506.             $this->sessSave();
  507.         }        
  508.     }
  509.  
  510.     /**
  511.      * enables a user
  512.      *
  513.      * @param boolean $debug 
  514.      * @since pk-05-06-27
  515.      *
  516.      ***/
  517.     function enable($debug=False
  518.     {
  519.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::enable()","USR_ID: ".$this->getId());
  520.  
  521.         if (!$this->isConnected(True))
  522.         {
  523.             throw new Exception(_OCSP_EXEP_NODBCONN_);
  524.         }
  525.         
  526.         $cmd="UPDATE T_SYS_USER SET USR_ENABLED=1 WHERE USR_ID=".$this->getId();
  527.         $this->myDBObj->executeCmd($cmd);
  528.         
  529.         if ($this->isCurrent())
  530.         {
  531.             $this->setDBField('USR_ENABLED',1);
  532.             $this->sessSave();
  533.         }            
  534.     }    
  535.     
  536.     // ---------------------------------------------------------
  537.     // db connection methods
  538.     // ---------------------------------------------------------
  539.  
  540.     /**
  541.      * returns the db-Conffile for the user
  542.      *
  543.      * @param boolean $useEnv (if True it is tried to find one from the environment)
  544.      * @param boolean $debug 
  545.      *
  546.      *
  547.      * @return string 
  548.      * @access public
  549.      *
  550.      * @since pk-07-06-14
  551.      * @version pk-07-06-15
  552.      * @version pk-08-06-06
  553.      *
  554.      * 
  555.      */
  556.     function getDBConfFile($useEnv=False,$debug=False)
  557.     {
  558.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::getDBConfFile()",$this->dbConffile);
  559.         
  560.         if (!empty($this->dbConffile))
  561.         {
  562.             if (file_exists(OCSP_CONF::getInstance()->getProjectPath($this->dbConffile))
  563.             {
  564.                 return OCSP_CONF::getProjectPath($this->dbConffile;
  565.             }            
  566.             if (file_exists($this->dbConffile))
  567.             
  568.                 return $this->dbConffile;
  569.             }
  570.             
  571.             if (file_exists(OCSP_CONF::getInstance()->getProjectConfPath($this->dbConffile))
  572.             {
  573.                 return OCSP_CONF::getInstance()->getProjectConfPath($this->dbConffile;
  574.             }
  575.         }
  576.         
  577.         if ($useEnv
  578.         {
  579.             pcf_require_class('OCSP_DB',__OCSP_PHPINCPATH__."db/OCSP_DB.phpclass");
  580.             if ($str_confFile OCSP_DB::getMyConfFile(False,True,$debug))
  581.             {
  582.                 $this->dbConffile = str_replace(OCSP_CONF::getInstance()->getProjectPath(),"",$str_confFile);
  583.                 return $str_confFile;
  584.             }
  585.         }
  586.         
  587.         return "";
  588.     }
  589.  
  590.  
  591.     /**
  592.      * sets the database config file
  593.      *
  594.      * @param string $confFile 
  595.      *
  596.      * @global array $OCSP_CONF 
  597.      *
  598.      * @since pk-07-09-06
  599.      *
  600.      * @access public
  601.      */
  602.     function setDBConfFile($confFile)
  603.     {
  604.         $this->dbConffile=str_replace(OCSP_CONF::getInstance()->getValue('PROJECTPATH'),"",$confFile);
  605.     }
  606.  
  607.     /**
  608.       * returns if the dbObj is connected
  609.       *
  610.       * @param boolean $autoConnect try to connect
  611.       * @param boolean $debug 
  612.       *
  613.       * @return boolean 
  614.       *
  615.       * @since pk-07-05-31
  616.       */
  617.     function isConnected($autoConnect=False,$debug=False)
  618.     {
  619.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::isConnected()");
  620.         
  621.         if (!pcf_is_instance_of($this->myDBObj,'OCSP_DB'))
  622.         {
  623.             if ($autoConnect)
  624.             {
  625.                 try {
  626.                     $this->dbConnect();
  627.                     return True;
  628.                 catch(Exception $e{
  629.                     if ($debugechoDebugLine(__FILE__,__LINE__,print_r($e,True));
  630.                     return False;
  631.                 }
  632.             }
  633.         else if (!$this->myDBObj->isConnected()) {
  634.               if ($autoConnect)
  635.               {
  636.                   $this->getMyDBObj(False,"",False,$debug);
  637.                   //$this->myDBObj->connect($this->getDBConfFile(),$this->isPublic(),False,$debug);               
  638.               }
  639.         else {
  640.             return True;
  641.         }
  642.         
  643.         return False;
  644.     }
  645.     
  646.     /**
  647.      * connects to the db
  648.      *
  649.      * @param  boolean $asAdmin 
  650.      * @param  boolean $debug 
  651.      *
  652.      * @global $OCSP_CONF 
  653.      *
  654.      * @return OCSP_DB 
  655.      *
  656.      * @version pk-04-09-28
  657.      * @requires __OCSP_PHPINCPATH__."common/pcf_templates.phpinc"
  658.      *
  659.      ***/
  660.     function &dbConnect($asAdmin=False,$debug=False{
  661.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::dbConnect()");
  662.  
  663.         global $OCSP_CONF;
  664.  
  665.         if (!$this->getMyDBObj(True,"",True,$debug))
  666.         {
  667.             throw new Exception(_OCSP_EXCEP_NODBCONN_);
  668.         }
  669.  
  670.         if (!$this->myDBObj->connect($this->getDBConfFile(),$this->isPublic(),$asAdmin,$debug))
  671.         {
  672.             throw new Exception(_OCSP_EXCEP_NODBCONN_);
  673.         }
  674.  
  675.         return $this->myDBObj;
  676.     }
  677.  
  678.  
  679.     /**
  680.      * @param OCSP_DB $aDBObj 
  681.      * @access public
  682.      * @since pk-07-08-20
  683.      */
  684.     function setMyDBObj(&$aDBObj)
  685.     {
  686.         $this->myDBObj=$aDBObj;
  687.     }
  688.  
  689.  
  690.     /**
  691.      * loading the db object checks dbtype in the following order
  692.      *
  693.      * 1st: $OCSP_CONF['DBTYPE']
  694.      * 2nd: $_SERVER['DBTYPE']
  695.      * 3rd: $GLOBALS['OCSP']['DBTYPE']
  696.      * 4th: set to mySQL
  697.      *
  698.      * and sets $OCSP_CONF['DBTYPE'] if not isset or is empty
  699.      *
  700.      * @param boolean $withoutConnect (only return the database object)
  701.      * @param string $dbConffile 
  702.      * @param boolean $useGlobal 
  703.      * @param boolean $debug 
  704.      *
  705.      * @global array $OCSP_CONF 
  706.      * @global array $OCSP_OBJ 
  707.      *
  708.      * @return OCSP_DB 
  709.      *
  710.      * @access public
  711.      *
  712.      * @since pk-07-08-20
  713.      * @version pk-08-03-14
  714.      *
  715.      * @todo replace global arrays
  716.      */
  717.     function &getMyDBObj($withoutConnect=False,$dbConffile="",$useGlobal=True,$debug=False)
  718.     {        
  719.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::getMyDBObj()",($useGlobal "use global" "DON'T use global"));
  720.         global $OCSP_CONF,$OCSP_OBJ;
  721.  
  722.         if (!pcf_is_instance_of($this->myDBObj,'OCSP_DB'))
  723.         {
  724.             if ($useGlobal && pcf_is_instance_of(OCSP_OBJ::defaultDBObj('rw',False),'OCSP_DB'))
  725.             {
  726.                 $this->myDBObj=OCSP_OBJ::defaultDBObj();
  727.             else {
  728.                 // $debug=True;echoDebugLine(__FILE__,__LINE__,"debug on");                    
  729.                 require $this->getDBConfFile(True,$debug);
  730.                 
  731.                 if (isset($DBCONF['CLASS']&& !empty($DBCONF['CLASS']))
  732.                 {
  733.                     if (!class_exists($DBCONF['CLASS']))
  734.                     {
  735.                         if (isset($DBCONF['INCLUDE']&& !empty($DBCONF['INCLUDE']))
  736.                         {
  737.                             if (!function_exists('pcf_tmpl_parse')) require_once __OCSP_PHPINCPATH__ "common" _OCSP_DIRSEP_ "pcf_templates.phpinc";
  738.                             $str_include pcf_tmpl_parse($DBCONF['INCLUDE'],array(),$debug);
  739.                             if ($debugechoDebugLine(__FILE__,__LINE__,"DB include: " $str_include);
  740.                             require_once $str_include;
  741.                         }
  742.                     }
  743.                     $str_dbClassName $DBCONF['CLASS'];                    
  744.                 else {
  745.                     $str_dbClassName "OCSP_DB_"OCSP_CONF::getDBType();
  746.                     pcf_require_class($str_dbClassName,__OCSP_PHPINCPATH__ "db/" $str_dbClassName ".phpclass");
  747.                 }
  748.                 
  749.                 $str_objCmd "\$this->myDBObj = new " $str_dbClassName "();";
  750.                 if ($debug)
  751.                 {
  752.                     echoDebugLine(__FILE__,__LINE__,"DB-Obj cmd$str_objCmd");
  753.                     eval($str_objCmd);
  754.                 else {
  755.                     @eval($str_objCmd);
  756.                 }
  757.                 if (!is_object($this->myDBObj)) {
  758.                     ocsp_logError(__FILE__,__LINE__,"Could not create DB object OCSP_DB_".$OCSP_CONF['DBTYPE'],E_ERROR);
  759.                     die();
  760.                 }
  761.                 //if ($useGlobal) $OCSP_OBJ['USRDB']=&$this->myDBObj;
  762.             }
  763.         }
  764.  
  765.         if (!$withoutConnect && !$this->myDBObj->isConnected())
  766.         {
  767.             $this->dbConnect(False,$debug);
  768.         }
  769.         return $this->myDBObj;
  770.     }
  771.      
  772.  
  773.     // ---------------------------------------------------------
  774.     // db row methods
  775.     // ---------------------------------------------------------
  776.  
  777.     /**
  778.      * touches the user
  779.      *
  780.      * @param boolean $toDB 
  781.      * @param boolean $debug 
  782.      */
  783.     public function touch($toDB=False,$debug=False)
  784.     {
  785.         if ($int_id intval($this->getId()))
  786.         {
  787.             if ($toDB)
  788.             {
  789.                 if (!$this->isConnected(True,$debug))
  790.                 {
  791.                     throw new Exception(_OCSP_EXCEP_NODBCONN_);
  792.                 }
  793.     
  794.                 $str_cmd "UPDATE T_SYS_USER SET USR_LASTLOGIN=" .$this->myDBObj->qs_getNowStmt();
  795.                 $str_cmd.= " WHERE USR_ID=" $this->getId();
  796.                 $this->myDBObj->executeCmd($str_cmd);
  797.             }
  798.             $this->sessTouch($debug);
  799.         }
  800.     }
  801.  
  802.     /**
  803.      * sets a database row to the object fields
  804.      *
  805.      * sets each key (=columname) value
  806.      *
  807.      * if $row['__OCSP_OBJ_VALS__'] isset and an array this values are set as object values
  808.      * else if $asPopulated object values are loaded
  809.      *
  810.      * @param  array $row            the row form a select * from DBMS_TABLEOBJ::myTable
  811.      * @param  boolean $asPopulated    set populateTS as if the object has been populated
  812.      * @param  boolean $debug 
  813.      *
  814.      * @returns bool
  815.      *
  816.      * @since pk-08-03-14
  817.      */
  818.     function setDBRow($row,$asPopulated=True,$debug=False)
  819.     {
  820.         if ($debugechoDebugMethod(__FILE__,get_class($this),'OCSP_USER::setDBRow()');
  821.  
  822.         if (pcf_is_instance_of($this,'DBMS_TABLEOBJ'))
  823.         {
  824.             $bol_ret parent::setDBRow($row,$asPopulated,$debug);
  825.             $this->isValid     $asPopulated;
  826.         else {
  827.             $bol_ret False;
  828.         }
  829.                  
  830.         return $bol_ret;
  831.     }
  832.  
  833.     /**
  834.      * only saves none public user
  835.      *
  836.      * @param boolean $debug 
  837.      * 
  838.      * @return boolean 
  839.      */
  840.     function dbSave($debug=False)
  841.     {
  842.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::dbSave()");
  843.         if (!$this->isPublic())
  844.         {
  845.             return parent::dbSave($debug);
  846.         else {
  847.             return False;
  848.         }
  849.     }
  850.  
  851.     /**
  852.       * inserts a new row to the table
  853.       *
  854.       * @param boolean   $debug  show debug info
  855.       *
  856.       * @returns int   returns the autoIncFld ID if exists or 1 on success
  857.       * 
  858.       * @since pk-08-06-04
  859.       */
  860.     function dbInsert($debug=False
  861.     {    
  862.         $this->setDBField('USR_CREATION',"#NOW");
  863.         $this->setDBField('USR_LASTCHANGE',"#NOW");
  864.         $this->setDBField('USR_CREATOR',OCSP_OBJ::currentUser()->getId());
  865.         
  866.         return parent::dbInsert();
  867.     }
  868.     
  869.     // ---------------------------------------------------------
  870.     // db row getter / setter
  871.     // ---------------------------------------------------------
  872.     
  873.     /**
  874.      * changes the password
  875.      *
  876.      * @param string $newPassword 
  877.      */
  878.     function setPassword($newPassword)
  879.     {
  880.         $this->setDBField('USR_MD5PWD',md5($newPassword));
  881.         
  882.         $str_pwd OCSP_SESSION::getInstance(False)->encrypt($newPassword,md5($this->getId().$this->getName()));
  883.         $this->setDBField('USR_PWD',md5($str_pwd));
  884.     }
  885.     
  886.     // ---------------------------------------------------------
  887.     // session methods
  888.     // ---------------------------------------------------------    
  889.  
  890.     /**
  891.      * populates a user from the session
  892.      *
  893.      * @param boolean $debug 
  894.      */
  895.     protected function sessPopulate($debug=False)
  896.     {        
  897.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::sessPopulate()");
  898.         
  899.         if (!class_exists('OCSP_SESSION'))
  900.         {
  901.             require_once __OCSP_PHPINCPATH__ "common" _OCSP_DIR_ "OCSP_SESSION.phpclass";
  902.         }
  903.         
  904.         if ($str_usrData OCSP_SESSION::getInstance(True)->getValue('CURRENTUSER'))
  905.         {
  906.             if (md5($str_usrData!= OCSP_SESSION::getInstance(True)->getValue('CURRENTUSERCHK'))
  907.             {
  908.                 throw new Exception(_OCSP_SESSION_ERROR_ ": User session check failed" );
  909.             }
  910.             $arr_usrData unserialize($str_usrData);
  911.             
  912.             if ($debugechoDebugLine(__FILE__,__LINE__,"\$arr_usrData: <pre>" print_r($arr_usrData,True"</pre>");
  913.             
  914.             if ($arr_usrData['HTTP_USER_AGENT'!= $_SERVER['HTTP_USER_AGENT'])
  915.             {
  916.                 throw new Exception(_OCSP_SESSION_ERROR_ ": User session check failed" );
  917.             }
  918.             
  919.             if (!isset($arr_usrData['ROW']|| !is_array($arr_usrData['ROW']))
  920.             {
  921.                 throw new Exception(_OCSP_SESSION_ERROR_ ": User data not ok");
  922.             }
  923.             
  924.             if (intval($arr_usrData['ROW']['USR_ID']))
  925.             {
  926.                 foreach($arr_usrData['ROW'as $str_col => $mix_val)
  927.                 {
  928.                     $this->{$str_col$mix_val;
  929.                 }
  930.                 
  931.                 $int_ttl intval(OCSP_CONF::getInstance()->getValue('SESSION_TTL'));
  932.                 if ($int_ttl _OCSP_USR_SESSION_TTL_)
  933.                 {
  934.                     $int_ttl _OCSP_USR_SESSION_TTL_;
  935.                 }
  936.                 
  937.                 if ($arr_usrData['LOADTIME'(time(_OCSP_USR_SESSION_TTL_))
  938.                 {
  939.                     if ($debugechoDebugLine(__FILE__,__LINE__,'Session Timed Out');
  940.                                         
  941.                     $this->dbConnect("",False,$debug);
  942.                     $this->dbPopulate($debug);
  943.                     $this->populateMyGroups($debug);
  944.                     //$this->sessSave($debug);
  945.                 else {
  946.  
  947.                     $this->populateTS=$arr_usrData['LOADTIME'];
  948.                     $this->myGroups=$arr_usrData['GROUPS'];
  949.                     $this->myCliId=$arr_usrData['CLI_ID'];
  950.                     $this->myCtyId=$arr_usrData['CTY_ID'];
  951.                 }
  952.             else {
  953.                 $this->setDBField('USR_ID',0,$debug);            
  954.             }
  955.             
  956.         else {
  957.             $this->setDBField('USR_ID',0,$debug);
  958.         }
  959.     }
  960.     
  961.     /**
  962.      * stores the user to the session
  963.      *
  964.      * @param boolean $debug 
  965.      * 
  966.      * @version pk-08-06-11 store only if $this is current user
  967.      */
  968.     public function sessSave($debug=False)
  969.     {
  970.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::sessSave()");
  971.         
  972.         if ($this->getId(== OCSP_OBJ::currentUser()->getId())
  973.         {        
  974.             $arr_usrData array(
  975.                 'ROW' => $this->getDBRow(),
  976.                 'LOADTIME' => $this->populateTS,
  977.                 'GROUPS' => $this->myGroups,
  978.                 'CLI_ID' => $this->myCliId,
  979.                 'CTY_ID' => $this->myCtyId,
  980.                 //'REMOTE_ADDR' => $_SERVER["REMOTE_ADDR"],
  981.                 //'REMOTE_PORT' => $_SERVER["REMOTE_PORT"],
  982.                 'HTTP_USER_AGENT' => $_SERVER["HTTP_USER_AGENT"]
  983.             );
  984.             
  985.             $str_usrData serialize($arr_usrData);
  986.             
  987.             OCSP_SESSION::getInstance()->setValue('CURRENTUSER',$str_usrData);
  988.             OCSP_SESSION::getInstance()->setValue('CURRENTUSERCHK',md5($str_usrData));
  989.             $this->sessTouch($debug);
  990.         }            
  991.     }
  992.     
  993.     /**
  994.      * sets session user timestamp
  995.      *
  996.      * @param boolean $debug 
  997.      */
  998.     protected function sessTouch($debug=False)
  999.     {
  1000.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::sessTouch()");
  1001.         if (intval($this->getId()) && ($this->getId(== OCSP_OBJ::currentUser()->getId()))
  1002.         {
  1003.             OCSP_SESSION::getInstance(True)->setValue('CURRENTUSERTS',time());
  1004.         }
  1005.     }
  1006.  
  1007.     // ---------------------------------------------------------
  1008.     // group methods
  1009.     // ---------------------------------------------------------    
  1010.     
  1011.     /**
  1012.      * populates the groups from the database (T_SYS_GROUPMEMBER)
  1013.      *
  1014.      * @param boolean $debug 
  1015.      */
  1016.     public function populateMyGroups($debug)
  1017.     {
  1018.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::populateMyGroups();");
  1019.         
  1020.         if (!$this->isConnected(True))
  1021.         {
  1022.             throw new Exception(_OCSP_EXCEP_NODBCONN_);
  1023.         }
  1024.         
  1025.         if (!$this->isPublic($debug))
  1026.         {
  1027.             $str_query "SELECT * FROM T_SYS_GROUPMEMBER WHERE USR_ID=" $this->getId();
  1028.             if ($obj_cursor $this->myDBObj->query($str_query))
  1029.             {
  1030.                 while($arr_grp $obj_cursor->fetchArrayFld())
  1031.                 {
  1032.                     $this->myGroups[$arr_grp['GRP_ID']] $arr_grp;
  1033.                 }    
  1034.             }             
  1035.         else {
  1036.             $this->myGroups[0]=array('GRP_ID' =>0,'GRP_NAME' => "Public");
  1037.         }
  1038.     }
  1039.     
  1040.     /**
  1041.      * returns the default group of the user
  1042.      * (GRP_ID in T_SYS_USER)
  1043.      *
  1044.      * @return int 
  1045.      *
  1046.      * @since pk-06-11-14
  1047.      *
  1048.      ***/
  1049.     public function getDefaultGroupId(
  1050.     {
  1051.         return intval($this->getDBField('GRP_ID'));
  1052.     }    
  1053.     
  1054.     /**
  1055.      * returns an array with group id's direct assigned to the user
  1056.      *
  1057.      * @param boolean $forceReload (reload the groups from the db)
  1058.      * @param boolean $debug 
  1059.      *
  1060.      * @return array 
  1061.      *
  1062.      * @since pk-08-03-12
  1063.      */
  1064.     public function getDirectAssignedGroups($forceReload=False,$debug=False)
  1065.     {
  1066.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::getDirectAssignedGroups()");
  1067.         if ($forceReload || !(sizeof($this->myGroups)))
  1068.         {
  1069.             $this->populateMyGroups($debug);            
  1070.         }
  1071.          
  1072.         if ($debugechoDebugLine(__FILE__,__LINE__,"OCSP_USER->groups: <pre>" print_r($this->myGroups,True"</pre>");
  1073.          
  1074.         $arr_ret array();
  1075.         $arr_ret[0$this->getDefaultGroupId();
  1076.         foreach($this->myGroups as $arr_grp)
  1077.         {
  1078.             if (intval($arr_grp['GRP_ID']&& ($arr_grp['GRP_ID'!= $arr_ret[0]))
  1079.             {
  1080.                 $arr_ret[$arr_grp['GRP_ID'];
  1081.             }
  1082.         }
  1083.         if ($debugechoDebugLine(__FILE__,__LINE__,"returns: <pre>" print_r($arr_ret,True"</pre>");
  1084.         return $arr_ret;
  1085.     }    
  1086.     
  1087.     /**
  1088.      * returns if the user is a member of $aGroup
  1089.      *
  1090.      * @param int $aGrpId 
  1091.      * @param boolean $debug 
  1092.      *
  1093.      * @return boolean 
  1094.      *
  1095.      * @version pk-08-07-22
  1096.      */
  1097.     public function isGroupMember($aGrpId,$debug=False
  1098.     {
  1099.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::isGroupMember({$aGrpId})");
  1100.  
  1101.         switch($aGrpId)
  1102.         {
  1103.             case _OCSP_GROUP_LOGGEDIN_:
  1104.                 return ($this->isPublic(False True);
  1105.             case _OCSP_GROUP_PUBLIC_:
  1106.                 return True;
  1107.             case _OCSP_GROUP_NOTLOGGEDIN_:
  1108.                 return $this->isPublic();
  1109.             default:
  1110.                 return OCSP_GROUPTREE::getInstance()->userIsMember($aGrpId,$this->getId(),True,$debug);
  1111.         }         
  1112.     }    
  1113.     
  1114.     /**
  1115.      * returns a string containing group id's
  1116.      *
  1117.      * @param char $sep 
  1118.      * @param boolean $forceReload 
  1119.      * @param boolean $debug 
  1120.      *
  1121.      * @return string 
  1122.      *
  1123.      * @version pk-05-03-01
  1124.      * @version pk-05-09-01
  1125.      *
  1126.      * @var  $s_Ids 
  1127.      ***/
  1128.     public function getGroups($sep=",",$forceReload=False,$debug=False{
  1129.         if ($debugecho "<p><b>USER::getGroups($sep,....)</b> (".get_class($this).")</p><blockquote>\n";
  1130.  
  1131.         if ($this->isPublic()) {
  1132.             // user is not logged in no need to check
  1133.             if ($debugecho "<p>(NOT LOGGEDIN) Returning: ".$GLOBALS['OCSP_GROUPS']['PUBLIC'].$sep.$GLOBALS['OCSP_GROUPS']['NOTLOGGEDIN']."</p></blockquote>";
  1134.             return _OCSP_GROUP_PUBLIC_.$sep._OCSP_GROUP_NOTLOGGEDIN;
  1135.         }
  1136.  
  1137.         if ($forceReload || (!is_array($this->myGroups)) || (!sizeof($this->myGroups))) {
  1138.             $this->populateMyGroups($debug);
  1139.         }
  1140.  
  1141.         if ($debugecho "<pre>".print_r($this->myGroups,True)."</pre>";
  1142.  
  1143.         if (is_array($this->myGroups)) // <pk-05-09-20>
  1144.             $s_ret=implode($sep,array_keys($this->myGroups));
  1145.             $s_ret.=$sep._OCSP_GROUP_LOGGEDIN_;
  1146.         else {
  1147.             $s_ret=_OCSP_GROUP_LOGGEDIN_;
  1148.         // </pk-05-09-20>
  1149.         
  1150.         
  1151.         if (!isset($this->myGroups[$this->getDefaultGroupId()]))
  1152.         {
  1153.             $s_ret .= $sep $this->getDefaultGroupId();
  1154.         }
  1155.  
  1156.         if ($debugecho "<p>Returns$s_ret<p></blockquote>";
  1157.         return $s_ret;
  1158.     }
  1159.  
  1160.     /**
  1161.      * returns an array with group id's direct assigned to the user where he is admin
  1162.      *
  1163.      * @param boolean $forceReload (reload the groups from the db)
  1164.      * @param boolean $debug 
  1165.      *
  1166.      * @return array 
  1167.      *
  1168.      * @since pk-08-03-12
  1169.      */
  1170.     public function getDirectAssignedAdminGroups($forceReload=False,$debug=False)
  1171.     {
  1172.         if ($forceReload || !(sizeof($this->myGroups)))
  1173.         {
  1174.             $this->populateMyGroups($debug);
  1175.         }
  1176.          
  1177.         $arr_ret array();
  1178.         foreach($this->myGroups as $arr_grp)
  1179.         {
  1180.             if (intval($arr_grp['GRP_ISADMIN']))
  1181.             {
  1182.                 $arr_ret[$arr_grp['GRP_ID'];
  1183.             }
  1184.         }
  1185.  
  1186.         return $arr_ret;
  1187.     }    
  1188.     
  1189.     /**
  1190.      * returns if a user is admin
  1191.      *
  1192.      * @return boolean 
  1193.      *
  1194.      * @since pk-04-08-13
  1195.      ***/
  1196.     public function isAdmin($debug=False{
  1197.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::isAdmin()");
  1198.         return $this->isGroupMember(_OCSP_GROUP_ADMIN_,$debug);
  1199.     }
  1200.  
  1201.  
  1202.     /**
  1203.      * returns if the user is group admin
  1204.      *
  1205.      * @param int $aGrpId 
  1206.      * @param boolean $debug 
  1207.      *
  1208.      * @return boolean 
  1209.      *
  1210.      * @since pk-05-07-07
  1211.      * @version pk-06-01-18
  1212.      * @version pk-08-06-12
  1213.      */
  1214.     function isGroupAdmin($aGrpId,$debug=False{
  1215.         if ($debugechoDebugMethod(__FILE__,get_class($this),"USER::isGroupAdmin($aGroup)");
  1216.         
  1217.         if ($aGrpId 1)             return False;
  1218.         if ($this->isPublic())         return False;
  1219.         if ($this->isAdmin($debug))    return True;
  1220.  
  1221.         return OCSP_GROUPTREE::getInstance()->userIsAdmin($aGrpId,$this->getId(),$debug);
  1222.     }    
  1223.  
  1224.     /**
  1225.      * adds the user to $aGrpId
  1226.      *
  1227.      * @note this method does only work for self selectable groups
  1228.      *
  1229.      * @param int $aGrpId 
  1230.      * @param boolean $debug 
  1231.      *
  1232.      * @return boolean 
  1233.      *
  1234.      * @since pk-05-03-02
  1235.      *
  1236.      ***/
  1237.     function addToGroup($aGrpId,$ro=False,$asAdmin=False,$debug=False{
  1238.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::addToGroup($aGrpId)");
  1239.  
  1240.         return OCSP_GROUPTREE::getInstance()->addUserToGroup($this->getId(),$aGrpId,$ro,$asAdmin,$debug);;
  1241.     }
  1242.  
  1243.     /**
  1244.      * remove the user from $aGrpId
  1245.      *
  1246.      * @note this method does only work for self selectable groups
  1247.      *
  1248.      * @param int $aGrpId 
  1249.      * @param boolean $debug 
  1250.      *
  1251.      * @return boolean 
  1252.      *
  1253.      * @since pk-05-03-02
  1254.      *
  1255.      ***/
  1256.     function removeFromGroup($aGrpId,$debug=False{
  1257.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::removeFromGroup($aGrpId,....)");
  1258.         
  1259.         return OCSP_GROUPTREE::getInstance()->removeUserFromGroup($this->getId(),$aGrpId);
  1260.     }    
  1261.     
  1262.     // ---------------------------------------------------------
  1263.     // user check methods
  1264.     // ---------------------------------------------------------
  1265.  
  1266.     /**
  1267.      * checks username and password and setts $this->user if ok
  1268.      *
  1269.      * @param DBOBJ $dbObj 
  1270.      * @param string $aName 
  1271.      * @param string $aPasswd 
  1272.      * @param boolean $debug 
  1273.      *
  1274.      * @return boolean 
  1275.      *
  1276.      * @since pk-06-03-15
  1277.      * @version pk-07-02-22 check table version
  1278.      * @version pk-07-06-15
  1279.      * @version pk-08-03-14
  1280.      */
  1281.     function checkPassword($dbObj,$aName,$aPasswd,$debug=False)
  1282.     {
  1283.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::checkPassword()");
  1284.  
  1285.         if (!pcf_is_instance_of($dbObj,'OCSP_DB'))
  1286.         {
  1287.             if (!$this->isConnected(True,$debug))
  1288.             {
  1289.                 throw new Exception(_OCSP_EXCEP_NODBCONN_ ": for checking username and password");
  1290.             }
  1291.             $dbObj $this->myDBObj;
  1292.         else {
  1293.             if (!$dbObj->isConnected(&& (!$dbObj->connect($this->getDBConfFile(True,$debug),False,False,$debug)))
  1294.             {
  1295.                 throw new Exception(_OCSP_EXCEP_NODBCONN_ ": for checking username and password");
  1296.             }
  1297.         }
  1298.  
  1299.         $str_query "SELECT * FROM T_SYS_USER ";
  1300.         $str_query.= " WHERE TRIM(UPPER(USR_LOGIN)) = ".$dbObj->qs_getSlashedValue(strtoupper(trim($aName)));
  1301.         $str_query.= "   AND USR_MD5PWD = ".$dbObj->qs_getSlashedValue(md5($aPasswd));
  1302.  
  1303.         // if ($debug) echoDebugLine(__FILE__,__LINE__,"<p>" . $str_query . "</p>");
  1304.  
  1305.         if ($arr_row $dbObj->quickQuery($str_query))
  1306.         {
  1307.             $this->setDBRow($arr_row,True,$debug);
  1308.             $this->sessSave($debug);            
  1309.             return True;
  1310.         }
  1311.  
  1312.         if (pcf_is_instance_of($dbObj,'OCSP_DB_mySQL'))
  1313.         {
  1314.             // convert old style mysql passwords
  1315.             $str_query "SELECT * FROM T_SYS_USER ";
  1316.             $str_query.= " WHERE TRIM(UPPER(USR_LOGIN) = ".$dbObj->qs_getSlashedValue(strtoupper(trim($aName)));
  1317.             $str_query.= "   AND USR_MD5PWD <> ".$dbObj->qs_getSlashedValue(md5($aPasswd));
  1318.             $str_query.= "   AND (USR_PWD = PASSWORD(" $dbObj->qs_getSlashedValue($aPasswd")";
  1319.              
  1320.             if ($arr_row $dbObj->quickQuery($str_query))
  1321.             {
  1322.                 $str_cmd "UPDATE T_SYS_USER SET USR_MD5PWD = ".$dbObj->qs_getSlashedValue(md5($aPasswd));
  1323.                 $str_cmd.= " WHERE USR_ID=".intval($arr_row['USR_ID']);
  1324.                 $dbObj->executeCmd($str_cmd);
  1325.                 $this->setDBRow($arr_row,True,$debug);
  1326.                 $this->sessSave($debug);            
  1327.                 return True;
  1328.             }
  1329.         }
  1330.         return False;
  1331.     }
  1332.  
  1333.     /**
  1334.      * returns if the user is public or is a logined user
  1335.      *
  1336.      * as this function is called durring the dbConnection cal
  1337.      * you MUST not use $this->getDBField() method here this
  1338.      * will lead to an endless loop.
  1339.      * 
  1340.      * a user is public if no login name is set
  1341.      * @param boolean $debug 
  1342.      * 
  1343.      * @return boolean 
  1344.      *
  1345.      * @since pk-05-07-28
  1346.      * @version pk-08-06-02
  1347.      * @version pk-08-06-11 don't use $this->getDBField() => endless loop
  1348.      */
  1349.     function isPublic($debug=False{
  1350.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::isPublic()");
  1351.  
  1352.         $str_login=(isset($this->USR_LOGIN$this->USR_LOGIN "");
  1353.         if(empty($str_login))
  1354.         {
  1355.             return True;
  1356.         }
  1357.         if (isset($this->USR_ID&& intval($this->USR_ID))
  1358.         {
  1359.             return False;
  1360.         else {
  1361.             return True;
  1362.         }
  1363.     }    
  1364.     
  1365.     /**
  1366.      * @return boolean 
  1367.      */
  1368.     function isValid($debug=False)
  1369.     {
  1370.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::isValid()","Return: ".($this->isValid ? "True" "False"));
  1371.         if ($this->isPublic($debug))
  1372.         {
  1373.             return True;
  1374.         }
  1375.         return ($this->populateTS 0);
  1376.     }
  1377.  
  1378.     /**
  1379.      * returns if the user is enabled by default
  1380.      * 
  1381.      * NOTE: does not check current page settings
  1382.      *
  1383.      * @param  $debug 
  1384.      * @return boolean 
  1385.      */
  1386.     function isEnabled($debug=False)
  1387.     {
  1388.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::isEnabled()");
  1389.         if ($this->isPublic($debug))
  1390.         {
  1391.             return True;
  1392.         }
  1393.         return (intval($this->getDBField('USR_ENABLED')) True False);
  1394.     }
  1395.  
  1396.     /**
  1397.      * merges $secReq with $this->curSecReq
  1398.      *
  1399.      * @param array $secReq 
  1400.      */
  1401.     function setSecRequireArr($secReq)
  1402.     {
  1403.         if (is_array($secReq))
  1404.         {
  1405.             $this->curSecReq array_merge($this->curSecReq,$secReq);
  1406.         }
  1407.     }
  1408.     
  1409.     /**
  1410.      * checks the user against a merge of $this->curSecReq and $addSecReq
  1411.      * 
  1412.      * @param array $addSecReq 
  1413.      *
  1414.      * @return boolean 
  1415.      *
  1416.      * @since pk-06-08-15
  1417.      *
  1418.      */
  1419.     function isAllowed($addSecReq=NULL,$debug=False
  1420.     {
  1421.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::isAllowed()");
  1422.         
  1423.         if (is_array($addSecReq))
  1424.         {
  1425.             $arr_secReq array_merge($this->curSecReq,$addSecReq);
  1426.         else {
  1427.             $arr_secReq $this->curSecReq;
  1428.         }
  1429.         
  1430.         if (isset($arr_secReq['PUBLIC']&& $arr_secReq['PUBLIC'])
  1431.         {
  1432.             return True;
  1433.         else if ($this->isPublic($debug)) {
  1434.             return False
  1435.         }
  1436.         
  1437.         if (!$this->isEnabled($debug))
  1438.         {
  1439.             return False;
  1440.         }
  1441.         
  1442.         if (isset($arr_secReq['USER']&& intval($arr_secReq['USER']))
  1443.         {
  1444.             if ($this->getId(!= intval($arr_secReq['USER']))
  1445.             {
  1446.                 return False;
  1447.             }
  1448.         }
  1449.                 
  1450.         if (isset($arr_secReq['DEFAULTGROUP']&& intval($arr_secReq['DEFAULTGROUP']))
  1451.         {
  1452.             if (intval($arr_secReq['DEFAULTGROUP']!= $this->getDefaultGroupId())
  1453.             {
  1454.                 return False;
  1455.             }
  1456.         }
  1457.         
  1458.         if (isset($arr_secReq['GROUP']))
  1459.         {
  1460.             $obj_grpTree OCSP_GROUPTREE::getInstance();
  1461.             if (!$obj_grpTree->userIsMember(intval($arr_secReq['GROUP']),$this->getId(),True,$debug))
  1462.             {            
  1463.                 return False;
  1464.             }
  1465.         }
  1466.         
  1467.         if (isset($arr_secReq['GROUPADMIN']))
  1468.         {
  1469.             $obj_grpTree OCSP_GROUPTREE::getInstance();
  1470.             if (!$obj_grpTree->userIsAdmin(intval($arr_secReq['GROUP']),$this->getId(),$debug))
  1471.             {            
  1472.                 return False;
  1473.             }            
  1474.         }
  1475.  
  1476.         if (isset($arr_secReq['GROUPWRITE']))
  1477.         {
  1478.             $obj_grpTree OCSP_GROUPTREE::getInstance();
  1479.             if (!$obj_grpTree->userIsAdmin(intval($arr_secReq['GROUPWRITE']),$this->getId(),$debug))
  1480.             {    
  1481.                 if (!isset($this->myGroups[intval($arr_secReq['GROUPWRITE'])]))
  1482.                 {
  1483.                     return False;
  1484.                 else {
  1485.                     if (intval($this->myGroups[intval($arr_secReq['GROUPWRITE'])]['GRP_READONLY']))
  1486.                     {
  1487.                         return False;                    
  1488.                     }
  1489.                 }
  1490.             }                                            
  1491.         }    
  1492.         
  1493.         if (isset($arr_secReq['IP']&& !empty($arr_secReq['IP']))
  1494.         {
  1495.             if ($_SERVER['REMOTE_ADDR'!= $arr_secReq['IP'])
  1496.             {
  1497.                 return False;
  1498.             }
  1499.         }
  1500.  
  1501.         // all checks passed successfully
  1502.         return True;
  1503.     }
  1504.  
  1505.  
  1506.  
  1507.     /**#@+
  1508.      * old methods
  1509.      * @dprecated since pk-08-03-14
  1510.      */
  1511.  
  1512.  
  1513.  
  1514.  
  1515.     /**
  1516.      * deletes a user
  1517.      *
  1518.      * @param boolean $debug 
  1519.      *
  1520.      * @return boolean False if the current user has not enough rights to do this
  1521.      *
  1522.      * @since pk-06-10-04
  1523.      *
  1524.      ***/
  1525.     function deleteUser($debug=False{
  1526.         if ($debugechoDebugMethod(__FILE__,get_class($this),"USER::deleteUser(".$this->userId.")");
  1527.  
  1528.         if ($this->userId == $$GLOBALS['OCSP_OBJ']['USER']->getId()) return False// user can not delete himself
  1529.  
  1530.         if (!$$GLOBALS['OCSP_OBJ']['USER']->isAdmin()) {
  1531.             if (!$$GLOBALS['OCSP_OBJ']['USER']->canEditUser($this->getId())) return False;
  1532.         }
  1533.  
  1534.         $s_cmd="DELETE FROM T_CLI_USER WHERE USR_ID=".$this->getId();
  1535.         if ($debugecho "<p>$s_cmd</p>";
  1536.         $GLOBALS['OCSP_OBJ']['USRDB']->executeCmd($s_cmd);
  1537.         $s_cmd="DELETE FROM T_SYS_GROUPMEMBER WHERE USR_ID=".$this->getId();
  1538.         if ($debugecho "<p>$s_cmd</p>";
  1539.         $GLOBALS['OCSP_OBJ']['USRDB']->executeCmd($s_cmd);
  1540.         $s_cmd="DELETE FROM T_SYS_USER WHERE USR_ID=".$this->getId();
  1541.         if ($debugecho "<p>$s_cmd</p>";
  1542.         $GLOBALS['OCSP_OBJ']['USRDB']->executeCmd($s_cmd);
  1543.  
  1544.         return True;
  1545.     }
  1546.  
  1547.  
  1548.  
  1549.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1550.     //
  1551.     // user / group checks
  1552.     //
  1553.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1554.  
  1555.  
  1556.  
  1557.  
  1558.     /**
  1559.      * returns an array of GRP_ID's where the user is admin
  1560.      *
  1561.      * @param boolean $strict only direct assigned groups
  1562.      * @param boolean $idsOnly return an array with group ids only
  1563.      * @param boolean $debug 
  1564.      *
  1565.      * @return array 
  1566.      * 
  1567.      * @version pk-08-06-05
  1568.      */
  1569.     function getAdminGroups($strict=False,$keysOnly=True,$debug=False)
  1570.     {
  1571.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::getAdminGroups()");
  1572.         
  1573.         $obj_grpTree OCSP_GROUPTREE::getInstance();            
  1574.         $arr_grps $obj_grpTree->getUserGroupTreeArray($debug);
  1575.         
  1576.         $arr_ret array();
  1577.         foreach($arr_grps as $int_grpId => $arr_grpNode)
  1578.         {
  1579.             if ($arr_grpNode['ADMIN'])
  1580.             {
  1581.                 if (!isset($arr_ret[$int_grpId]))
  1582.                 {
  1583.                     $arr_ret[$int_grpId$arr_grpNode['ROW'];
  1584.                     if ($arr_children $obj_grpTree->getAllChildGroups($int_grpId,$arr_ret,$debug))
  1585.                     {
  1586.                         foreach($arr_children as $int_childId => $arr_row)
  1587.                         {
  1588.                             $arr_ret[$int_childId$arr_row;
  1589.                         }
  1590.                     }
  1591.                 }
  1592.             }
  1593.         }
  1594.                 
  1595.         if ($keysOnly)
  1596.         {
  1597.             return array_keys($arr_ret);
  1598.         else {
  1599.             return $arr_ret;
  1600.         }
  1601.     }
  1602.     
  1603.     
  1604.     
  1605.     
  1606.  
  1607.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1608.     //
  1609.     // Database Methods
  1610.     //
  1611.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.     /**
  1619.      * opens a connection to the database and checks username and password
  1620.      *
  1621.      * if username and password don't match the connection is closed
  1622.      * else the connection is assigned to $GLOBALS['OCSP_OBJ'][$this->gDBIDX]
  1623.      *
  1624.      * sets:
  1625.      *     - isValid
  1626.      *     - isAdmin
  1627.      *
  1628.      * @params string $aName
  1629.      * @params string $aPasswd (clear text)
  1630.      * @params string $aConffile (database conf file to include)
  1631.      * @params boolean $asAdmin (user is admin)
  1632.      * @params boolean $debug
  1633.      *
  1634.      * @return boolean 
  1635.      *
  1636.      * @version pk-04-09-29
  1637.      * @version pk-05-06-27
  1638.      * @version pk-05-10-28 $GLOBALS['OCSP']['USR_LOGIN_SHORTENTO'] added
  1639.      *
  1640.      * @deprecated since pk-08-03-14
  1641.      */
  1642.     function login($aName,$aPasswd,$aConffile=NULL,$asAdmin=False,$debug=False{
  1643.          
  1644.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::login($aName,*********,$aConffile,$asAdmin,$debug)");
  1645.         $this->isValid=False;
  1646.         $this->isAdmin=False;
  1647.         $this->isCurrentUser=True// <pk-07-06-01 />
  1648.  
  1649.         if (!empty($aConffile)) {$this->dbConffile=$aConffile;}
  1650.         if (!($dbObj=$this->dbConnect($asAdmin,$debug))) {
  1651.             if (!empty($GLOBALS['OCSP']['LOGINLOGFILE'])) {
  1652.                 if ($fp=fopen($GLOBALS['OCSP']['LOGINLOGFILE'],"a")) {
  1653.                     fwrite($fp,date("y-m-d H:i:s",time())." --->NO DB<---\n");
  1654.                     fclose($fp);
  1655.                 }
  1656.             }
  1657.             if ($debugocsp_logError(__FILE__,__LINE__,"NO Database Conffile");
  1658.             return False;
  1659.         }
  1660.  
  1661.         if (isset($GLOBALS['OCSP']['USR_LOGIN_SHORTENTO']&& intval($GLOBALS['OCSP']['USR_LOGIN_SHORTENTO'])) // <pk-05-10-28>
  1662.             // this is usefull if some other db fields like EMAIL are used to
  1663.             // identify the user
  1664.             // if this is set $aName's largen then $GLOBALS['OCSP']['USR_LOGIN_SHORTENTO']
  1665.             // are cut at the max position
  1666.             $aName=substr($aName,0,$GLOBALS['OCSP']['USR_LOGIN_SHORTENTO']);
  1667.         // </pk-05-10-28>
  1668.  
  1669.  
  1670.         if (!$this->checkPassword($dbObj,$aName,$aPasswd,$debug)) {
  1671.             $query2 ="SELECT *, PASSWORD(".$dbObj->qs_getPassword($aPasswd).") AS PWD,OLD_PASSWORD(".$dbObj->qs_getSlashedValue($aPasswd).") AS OLD_PWD FROM T_SYS_USER ";
  1672.             $query2.=" WHERE UPPER(USR_LOGIN) LIKE UPPER(".$dbObj->qs_getSlashedValue(strtoupper($aName)).")";
  1673.  
  1674.             if ($debugechoDebug(__FILE__,"<p>Login Query:".str_replace($aPasswd,"*******",$query2)."</p>\n");
  1675.  
  1676.             if ($user=$dbObj->quickQuery($query2)) {
  1677.                 if ($user['PWD'!= $user['USR_PWD']{
  1678.                     if (!empty($GLOBALS['OCSP']['LOGINLOGFILE'])) {
  1679.                         if ($fp=fopen($GLOBALS['OCSP']['LOGINLOGFILE'],"a")) {
  1680.                             fwrite($fp,date("y-m-d H:i:s",time())." --->".$query."<---\n");
  1681.                             fwrite($fp,"\t\tDBCONFFILE: ".$this->dbConffile."\n");
  1682.                             fwrite($fp,"\t\tDATABASE:   ".$dbObj->myDB."\n");
  1683.                             fwrite($fp,"\t\t$query2\n");
  1684.                             fwrite($fp,"\t\tQ2_PWD:     -->".$user['PWD']."<-->".$user['USR_PWD']."<--\n");
  1685.                             fclose($fp);
  1686.                         }
  1687.                     }
  1688.                     $dbObj->disconnect();
  1689.                     return False;
  1690.                 else {
  1691.                     $this->user=$user;
  1692.                 }
  1693.             else {
  1694.                 if (!empty($GLOBALS['OCSP']['LOGINLOGFILE'])) {
  1695.                     if ($fp=fopen($GLOBALS['OCSP']['LOGINLOGFILE'],"a")) {
  1696.                         fwrite($fp,date("y-m-d H:i:s",time())." --->".$query."<---\n");
  1697.                         fwrite($fp,"\t\tDBCONFFILE: ".$this->dbConffile."\n");
  1698.                         fwrite($fp,"\t\tDATABASE:   ".$dbObj->myDB."\n");
  1699.                         fwrite($fp,"\t\t$query2\n");
  1700.                         fwrite($fp,"\t\tQ2_PWD:     -->".$user['PWD']."<-->".$user['USR_PWD']."<--\n");
  1701.                         fclose($fp);
  1702.                     }
  1703.                 }
  1704.                 $dbObj->disconnect();
  1705.                 return False;
  1706.             }
  1707.         }
  1708.  
  1709.         if ($debugechoDebugLine(__FILE__,__LINE__,"User is ok");
  1710.  
  1711.         $this->userName=$this->user['USR_LOGIN'];
  1712.         $this->userId  =$this->user['USR_ID'];
  1713.         $this->dbConffile $this->user['USR_DBCONFFILE'];
  1714.         $this->isValid =True;
  1715.  
  1716.  
  1717.         if (($asAdmin&& (!$this->isAdmin)) {
  1718.             if ($debug{echo "<pre>";print_r($this->groups);echo"</pre>";}
  1719.             // reconnect to not grant admin right to the scripts
  1720.             // if user ist not an admin
  1721.             $dbObj->disconnect();
  1722.             if (!($dbObj=$this->dbConnect())) return False;
  1723.         }
  1724.  
  1725.  
  1726.  
  1727.         if (!$this->loadGroups($debug)) return False;
  1728.         $this->isAdmin=(is_array($this->groups[_OCSP_GROUP_ADMIN_]True False);
  1729.  
  1730.         if ($this->populateUser()) {
  1731.             //$this->isValid=True;
  1732.             $cmd="UPDATE T_SYS_USER SET USR_LASTLOGIN=".$dbObj->qs_getNowStmt();
  1733.             if (isset($user['USR_LASTCHANGE']&& (doubleval($user['USR_LASTCHANGE']))) {
  1734.                 // don't overwrite last change column
  1735.                 $cmd.=" ,USR_LASTCHANGE=".$dbObj->qs_getSlashedValue($user['USR_LASTCHANGE']);
  1736.             }
  1737.             $cmd.=" WHERE USR_ID=".$this->userId;
  1738.             $dbObj->executeCmd($cmd);
  1739.             $this->setSession($debug);
  1740.         else {
  1741.             $this->isValid=False;
  1742.         }
  1743.         if ($debugechoDebug(__FILE__,"<p>Returning ".($this->isValid ? "True" "False" )." Line:".__LINE__."</p>");
  1744.         return $this->isValid;
  1745.     }
  1746.  
  1747.     /**
  1748.      * logsout the current user
  1749.      *
  1750.      *
  1751.      @param boolean $debug
  1752.      *
  1753.      * @since pk-04-09-28
  1754.      *
  1755.      ***/
  1756.     function logout($debug=False{
  1757.         if ($debugecho "<p><b>USER::logout(...)</b> (".get_class($this).")</p>";
  1758.  
  1759.         $this->userName=NULL;
  1760.         $this->setDBField('USR_ID',0);        
  1761.         $this->isAdmin=False;
  1762.         $this->clearSession($debug);
  1763.     }
  1764.  
  1765.  
  1766.  
  1767.  
  1768.     ####################
  1769.  
  1770.  
  1771.     ####################
  1772.  
  1773.     /**
  1774.      * shows the login screen and returns to the current page
  1775.      *
  1776.      * @param int $method (OCSP_AUTH_xxxx)
  1777.      * @param string $form 
  1778.      *
  1779.      * @todo only OCSP_AUTH_HTTP implemented
  1780.      *
  1781.      */
  1782.     function loginScreen($method=OCSP_AUTH_HTTP,$form=""{
  1783.         if (empty($GLOBALS['OCSP']['REALM'])) $GLOBALS['OCSP']['REALM'$_SERVER['SERVER_NAME']." ".$_SERVER['REQUEST_URI'];
  1784.         switch($method)
  1785.         {
  1786.             case OCSP_AUTH_HTTP:
  1787.             default:
  1788.                 header("WWW-Authenticate: Basic realm=\""$GLOBALS['OCSP']['REALM']."\"");
  1789.                 header("HTTP/1.0 401 Unauthorized");
  1790.                 break;
  1791.         }
  1792.     }
  1793.  
  1794.  
  1795.     function checkGroup($aGroup,$withWrite=False,$isAdmin=False{
  1796.         if ($isAdmin{
  1797.             if ($aGroup==0return $this->isGroupMember($GLOBALS['OCSP']['GROUPS']['ADMIN']);
  1798.             else return $this->groups[$aGroup]['GRP_ISADMIN'];
  1799.         else if ($withWrite{
  1800.             if ($aGroup==0return True;
  1801.             else return ($this->groups[$aGroup]['GRP_READONLY'False True);
  1802.         else {
  1803.             return $this->isGroupMember($aGroup);
  1804.         }
  1805.     }
  1806.  
  1807.     function checkRights($rights{
  1808.         if (!is_array($this->groups)) return False;
  1809.         reset($this->groups);
  1810.         while(list($key,$val)=each($this->groups)) {
  1811.             if ($val['GRP_RIGHTS'>= $rights)
  1812.             return True;
  1813.         }
  1814.         return False;
  1815.     }
  1816.  
  1817.  
  1818.     /**
  1819.      * checks security requirements
  1820.      *
  1821.      * @param array $SEC_REQ 
  1822.      * @param bool $debug 
  1823.      *
  1824.      * @return bool 
  1825.      *
  1826.      * @version pk-04-09-28
  1827.      * @version pk-04-10-14
  1828.      * @version pk-05-03-17
  1829.      * @version pk-06-07-03
  1830.      * @version pk-06-07-23 checking SEC_REQ[GROUP|GROUPWRITE] changed to avoid warnings
  1831.      * @version pk-06-08-15 $SEC_REQ['USER'] added
  1832.      *
  1833.      ***/
  1834.     function checkSEC_REQ($SEC_REQ,$debug=False{
  1835.  
  1836.     }
  1837.  
  1838.     /**
  1839.      * not final use override this method to populate the additionl
  1840.      * user data
  1841.      *
  1842.      * @param bool $debug 
  1843.      *
  1844.      * @return bool 
  1845.      *
  1846.      ***/
  1847.     function populateUser($debug=False{
  1848.         return True;
  1849.     }
  1850.  
  1851.  
  1852.     // ----------------------------------------------------------------------------
  1853.     // other user functions
  1854.     // ----------------------------------------------------------------------------
  1855.  
  1856.     /**
  1857.      * returns if this user can see an other user (is in the same group)
  1858.      *
  1859.      * @param int $usrId 
  1860.      * @param boolean $debug 
  1861.      *
  1862.      * @return boolean 
  1863.      *
  1864.      * @since pk-06-12-17
  1865.      *
  1866.      ***/
  1867.     function canShowUser($usrId,$debug=False{
  1868.         if ($debugechoDebugMethod(__FILE__,get_class($this),"USER::canShowUser($usrId)");
  1869.  
  1870.         if (!intval($usrId)) return False// nobody kann edit public as it is (should not be) not stored in the db
  1871.         if ($this->isAdmin()) return True// admin is always allowed
  1872.  
  1873.         $s_query ="SELECT COUNT(*) FROM T_SYS_GROUPMEMBER g1 JOIN T_SYS_GROUPMEMBER g2 USING(GRP_ID)";
  1874.         $s_query.=" WHERE g1.USR_ID=".intval($usrId);
  1875.         $s_query.="   AND g2.USR_ID=".$this->getId();
  1876.  
  1877.         if (intval($GLOBALS['OCSP_OBJ']['USRDB']->quickQuery($s_query,0))) {
  1878.             return True;
  1879.         else {
  1880.             return False;
  1881.         }
  1882.     }
  1883.  
  1884.     /**
  1885.      * returns if this user has the right to edit another user
  1886.      * with id $usrId
  1887.      *
  1888.      * @param mixed $user (integer or OCSP_USER)
  1889.      * @param boolean $debug 
  1890.      *
  1891.      * @return boolean 
  1892.      *
  1893.      * @since pk-06-01-18
  1894.      * @version pk-08-03-12
  1895.      *
  1896.      */
  1897.     function canEditUser($user,$debug=False{
  1898.         if ($debugechoDebugMethod(__FILE__,get_class($this),"USER::canEditUser($usrId)");
  1899.  
  1900.         if (pcf_is_instance_of($user,'OCSP_USER'))
  1901.         {
  1902.             $usrId $user->getId();
  1903.         else {
  1904.             $usrId intval($user);
  1905.         }
  1906.  
  1907.         if (!intval($usrId)) return False// nobody kann edit public as it is (should not be) not stored in the db
  1908.  
  1909.         if ($this->isAdmin()) return True// admin is always allowed
  1910.  
  1911.         $s_query ="SELECT COUNT(*) FROM T_SYS_GROUPMEMBER g1 JOIN T_SYS_GROUPMEMBER g2 USING(GRP_ID)";
  1912.         $s_query.=" WHERE g1.USR_ID=".intval($usrId);
  1913.         $s_query.="   AND g2.USR_ID=".$this->getId();
  1914.         $s_query.="   And g2.GRP_ISADMIN=1";
  1915.  
  1916.         if (intval($this->myDBObj->quickQuery($s_query,0))) {
  1917.             return True;
  1918.         else {
  1919.             return False;
  1920.         }
  1921.     }
  1922.  
  1923.     // ----------------------------------------------------------------------------
  1924.     // client functions
  1925.     // ----------------------------------------------------------------------------
  1926.  
  1927.     /**
  1928.      * returns if myCliIDTS is ok (means not outdated)
  1929.      *
  1930.      * @return boolean 
  1931.      *
  1932.      * @since pk-06-07-21
  1933.      *
  1934.      ***/
  1935.     function check_myCliIDTS({
  1936.         return ($this->myCliIDTS (time()-ini_get('max_execution_time')));
  1937.     }
  1938.  
  1939.     
  1940.     /**
  1941.      * returns an array of client id's the user is assigned to
  1942.      * 
  1943.      * @param int $ctyId 
  1944.      * @param boolean $forceReload 
  1945.      * @param boolean $debug 
  1946.      * 
  1947.      * @return array 
  1948.      */
  1949.     public function getmyClientIds($ctyId=0,$forceReload=False,$debug=False)
  1950.     {
  1951.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::getmyCleintLst(" $ctyId ")");
  1952.         
  1953.         if (!$forceReload && sizeof($this->myClientLst))
  1954.         {
  1955.             if (!intval($ctyId)) 
  1956.             {
  1957.                 return array_keys($this->myClientLst);                
  1958.             else {
  1959.                 $arr_ret array();
  1960.                 foreach($this->myClientLst as $int_id => $arr_data)
  1961.                 {
  1962.                     if (intval($arr_data['CTY_ID']== intval($ctyId))
  1963.                     {
  1964.                         $arr_ret[]=$int_id;
  1965.                     }
  1966.                 }
  1967.                 return $arr_ret;
  1968.             }
  1969.         }
  1970.         
  1971.         if (!$this->isConnected(True))
  1972.         {
  1973.             throw new Exception(_OCSP_EXCEP_NODBCONN_);
  1974.         }
  1975.         
  1976.         $arr_filter array(
  1977.                 '?CLI_ID' => "CLI_ID IN (SELECT CLI_ID FROM T_CLI_USER WHERE USR_ID=" $this->getId(.")"    
  1978.             );
  1979.             
  1980.         $this->myClientLst array();
  1981.         if ($arr_clients $this->myDBObj->getArray('T_CLI_CLIENT',$arr_filter))
  1982.         {
  1983.             foreach($arr_clients as $arr_row)
  1984.             {
  1985.                 $this->myClientLst[$arr_row['CLI_ID']] array(
  1986.                         'CLI_NAME1' => $arr_row['CLI_NAME1'],
  1987.                         'CTY_ID' => $arr_row['CTY_ID'],
  1988.                         'CLS_ID' => $arr_row['CLS_ID']                    
  1989.                     );
  1990.             }
  1991.             
  1992.             if (!intval($ctyId)) 
  1993.             {
  1994.                 return array_keys($this->myClientLst);                
  1995.             else {
  1996.                 return $this->getmyClientIds($ctyId,False,$debug);    
  1997.             }
  1998.         }
  1999.         
  2000.         return array();
  2001.     }
  2002.     
  2003.     
  2004.     /**
  2005.      * returns if the user has the right to view a client's data
  2006.      *
  2007.      * @param int $cliId 
  2008.      * @param boolean $debug 
  2009.      *
  2010.      * @return boolean 
  2011.      *
  2012.      * @since pk-06-09-08
  2013.      ***/
  2014.     function canShowClient($cliId,$debug=False{
  2015.         if ($debugecho "<p><b>USER::canShowClient($cliId)</b> (".get_class($this).")</p>\n";
  2016.         if ($this->getCliId(== $cliIdreturn True;
  2017.         return $this->isGroupMember($GLOBALS['OCSP_GROUPS']['CLIENT-ADMIN']);
  2018.     }
  2019.  
  2020.     /**
  2021.      * returns if the user has the right to edit a client
  2022.      *
  2023.      * @param int $cliId 
  2024.      * @param boolean $debug 
  2025.      *
  2026.      * @return boolean 
  2027.      *
  2028.      * @since pk-05-09-23
  2029.      ***/
  2030.     function canEditClient($cliId,$debug=False{
  2031.         if ($debugecho "<p><b>USER::canEditClient($cliId)</b> (".get_class($this).")</p>\n";
  2032.  
  2033.         if ($this->getCliId(== $cliIdreturn True;
  2034.         if ($this->isGroupMember($GLOBALS['OCSP_GROUPS']['CLIENT-ADMIN'])) return True;
  2035.  
  2036.         $s_query="SELECT CLU_ISADMIN FROM T_CLI_USER WHERE CLI_ID=".intval($cliId)." AND USR_ID=".$this->getId();
  2037.         if (intval($GLOBALS['OCSP_OBJ']['USRDB']->quickQuery($s_query,0))) return True;
  2038.  
  2039.         return False;
  2040.  
  2041.     }
  2042.     
  2043.     
  2044.  
  2045.     /**
  2046.      * returns the client id from T_CLI_USER
  2047.      *
  2048.      * @param boolean $debug 
  2049.      * @param boolean $forceReload 
  2050.      *
  2051.      * @return int 
  2052.      *
  2053.      * @since pk-04-10-14
  2054.      * @version pk-05-12-11 bugfix returns null -> 0
  2055.      * @version pk-06-04-19 use $this->myCliId[TS]
  2056.      *
  2057.      ***/
  2058.     function getCliId($debug=False,$forceReload=False{
  2059.         if ($debugechoDebugMethod(__FILE__,get_class($this),"USER::getCliId",($forceReload "DO RELOAD" ""),0);
  2060.  
  2061.         if (!$forceReload && (intval($this->myCliId)) && ($this->check_myCliIDTS())) {
  2062.             return $this->myCliId;
  2063.         }
  2064.  
  2065.         $query="SELECT CLI_ID FROM T_CLI_USER WHERE USR_ID=".$this->getId();
  2066.         if ($debugecho "<blockquote>$query</blockquote>";
  2067.         if ($this->myCliId=intval(OCSP_OBJ::defaultReadDBObj()->quickQuery($query,0,0))) {
  2068.             $this->myCliIDTS=time();
  2069.         }
  2070.         if (is_object($this->myClient)) // we have to repopulate the clientobject to ensure it's the right one
  2071.             $this->myClient->setId($this->myCliId)// ensure we have the right ID
  2072.             $this->myClient->populate();            // reload the object values from the database
  2073.         }
  2074.         return $this->myCliId;
  2075.     }
  2076.  
  2077.  
  2078.     /**
  2079.      * sets the client id and generates a row in T_CLI_USER
  2080.      *
  2081.      * @param int $cliId 
  2082.      * @param boolean $overwrite remove all rows for the user in T_CLI_USER
  2083.      * @param boolean $debug 
  2084.      *
  2085.      * @since pk-05-10-19
  2086.      *
  2087.      * @version pk-06-04-19
  2088.      *
  2089.      ***/
  2090.     function setCliId($cliId,$overwrite=True,$debug=False{
  2091.         if($debugecho "<p><b>USER::setCliId($cliId,".($overwrite "REPLACE" "INSERT").")</b> (".get_class($this).")</p>";
  2092.  
  2093.         if (!intval($cliId)) return False;
  2094.         if ($overwrite{
  2095.             $s_cmd="DELETE FROM T_CLI_USER WHERE USR_ID=".$this->getId();
  2096.             if ($debugecho "<blockquote><p>$s_cmd</p></blockquote>";
  2097.             $GLOBALS['OCSP_OBJ']['USRDB']->executeCmd($s_cmd);
  2098.         }
  2099.  
  2100.         $s_cmd="INSERT INTO T_CLI_USER (USR_ID,CLI_ID) VALUES(".$this->getId().",".intval($cliId).")";
  2101.         if ($GLOBALS['OCSP_OBJ']['USRDB']->executeCmd($s_cmd)) {
  2102.             // <pk-06-04-19 />
  2103.             $this->myCliId=$cliId;
  2104.             $this->myCliIDTS=time();
  2105.             return True;
  2106.         }
  2107.         return False;
  2108.     }
  2109.  
  2110.     /**
  2111.      * returns a client object for the first client found for the user
  2112.      * @param bool $debug 
  2113.      *
  2114.      * @global array $OCSP_CONF 
  2115.      *
  2116.      * @return CLIENT 
  2117.      *
  2118.      * @version pk-06-04-19
  2119.      *
  2120.      ***/
  2121.     function &getClient($debug=False{
  2122.         if ($debugecho "<p><b>USER::getClient($debug)</b></p>";
  2123.  
  2124.         if (is_object($this->myClient&& ($this->myClient->getId()==$this->myCliId)) // we already have the object loaded
  2125.             return $this->myClient;
  2126.         }
  2127.  
  2128.         if ($cliId=$this->getCliId($debug)) {
  2129.             require_once __OCSP_DEFAULTCONFPATH__."client.conf.phpinc";
  2130.             require_once $GLOBALS['CLIENT']['PHPINCPATH']."CLIENT.phpclass";
  2131.             $this->myClient=new CLIENT(intval($cliId),$debug);
  2132.  
  2133.             if (!$this->myClient->getPopulateTS()) {
  2134.                 return NULL;
  2135.             else {
  2136.                 $this->myCtyId=$this->myClient->getTypeId();
  2137.                 return $this->myClient;
  2138.             }
  2139.         }
  2140.         return NULL;
  2141.     }
  2142.  
  2143.     /**
  2144.      * returns the staff assigned to the user
  2145.      *
  2146.      * @param boolean $debug 
  2147.      *
  2148.      * @return CLI_STAFF 
  2149.      *
  2150.      * @since pk-08-03-02
  2151.      */
  2152.     function getStaff($debug=False)
  2153.     {
  2154.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_USER::getStaff()");
  2155.  
  2156.         // @todo inline SQL
  2157.         $str_query "SELECT * FROM T_CLI_STAFF WHERE CST_ID IN (SELECT CST_ID FROM T_CLI_USER WHERE USR_ID=" $this->getId(")";
  2158.         if ($arr_staffRow $this->myDBObj->quickQuery($str_query))
  2159.         {
  2160.             if (!class_exists('CLI_STAFF'))
  2161.             {
  2162.                 require_once __OCSP_PHPINCPATH__ "client" _OCSP_DIRSEP_ "CLI_STAFF.phpclass";
  2163.             }
  2164.  
  2165.             return CLI_STAFF::factoryFromRow($arr_staffRow,$debug);
  2166.         }
  2167.  
  2168.         return NULL;
  2169.     }
  2170.  
  2171.     // client type functions -------------------------------------
  2172.  
  2173.     /**
  2174.      * returns the client type id of the users client
  2175.      * or False if no client could be found
  2176.      *
  2177.      * @param boolean $debug 
  2178.      * @param boolean $foreachReload or use cache (session) if available
  2179.      *
  2180.      * @since pk-06-07-21
  2181.      *
  2182.      ***/
  2183.     function getCliTypeId($debug=False,$forceReload=False{
  2184.         if ($debugechoDebugMethod(__FILE__,get_class($this),"USER::getCliTypeId()");
  2185.  
  2186.         if ($forceReload || (!intval($this->myCliId))) // load the client
  2187.             $this->getCliId($debug,$forceReload);
  2188.         }
  2189.  
  2190.         // check if we already have a clientobject loaded
  2191.         if (is_object($this->myClient&& ($this->myClient->getId()==$this->myCliId)) {
  2192.             $this->myCtyId=$this->myClient->getTypeId();
  2193.             return $this->myCtyId;
  2194.         }
  2195.  
  2196.         if ($this->check_myCliIDTS(&& intval($this->myCtyId)) // we asume the current value as ok
  2197.             return $this->myCtyId;
  2198.         }
  2199.  
  2200.         // get the clientId without forceing a reload as this is done earlier
  2201.         if (!intval($this->getCliId($debug,False))) {
  2202.             // the user belongs to no client return False
  2203.             return False;
  2204.         }
  2205.  
  2206.         $s_query="SELECT CTY_ID FROM T_CLI_CLIENT WHERE CLI_ID=".$this->myCliId;
  2207.         $this->myCtyId=$GLOBALS[$this->get_gDBIDX()]->quickQuery($s_query);
  2208.         return $this->myCtyId;
  2209.  
  2210.  
  2211.  
  2212.     }
  2213.  
  2214.     /**
  2215.      * checks if the user can show users of a type
  2216.      * returns always True to the type the users client is
  2217.      *
  2218.      * if the type exists the columns
  2219.      *
  2220.      * checks if the user is in the show or admin group of the clienttype
  2221.      *
  2222.      * @param int $ctyId 
  2223.      * @param boolean $debug 
  2224.      *
  2225.      * @return boolean 
  2226.      ***/
  2227.     function canListClientType($ctyId,$debug=False{
  2228.         if ($debugechoDebugMethod(__FILE__,get_class($this),"USER::canListClientType()");
  2229.  
  2230.         if ($this->isAdmin()) return True// no need to check because isGroupMember will return True
  2231.  
  2232.         $s_query="SELECT FROM T_CLI_TYPE WHERE CTY_ID=".$ctyId;
  2233.         if ($a_cty=$GLOBALS[$this->get_gDBIDX()]->query($s_query)) {
  2234.             if ($this->isGroupMember($a_cty['CTY_ADMIN']))      return True;
  2235.             if ($this->isGroupMember($a_cty['CTY_EDITGROUP']))  return True;
  2236.             if ($this->isGroupMember($a_cty['CTY_SHOWGROUP']))  return True;
  2237.             return False// user is not member of relevant group and we do not want users can see other clients of theire group
  2238.         else {
  2239.             return ($ctyId==0)// return True for none specified type
  2240.         }
  2241.     }
  2242.  
  2243.     /**
  2244.      * checks if the user has admin rights for a client
  2245.      * User is group member of CLIENT_ADMIN or T_CLI_USER CLU_ISADMIN is True
  2246.      *
  2247.      * uses the array ($this->isCliAdminBuffer to buffer results)
  2248.      *
  2249.      * @param int $cliId if 0 $this->getCliId() is called
  2250.      * @param boolean $debug 
  2251.      *
  2252.      * @return boolen 
  2253.      *
  2254.      * @since pk-06-09-15
  2255.      *
  2256.      ***/
  2257.     function isClientAdmin($cliId=0,$debug=False{
  2258.         if ($debugechoDebugMethod(__FILE__,get_class($this),"USER::isClientAdmin");
  2259.  
  2260.         if ($this->isAdmin())                       return True;
  2261.         if ($this->isGroupMember('CLIENT-ADMIN'))   return True;
  2262.  
  2263.         if (!intval($cliId)) $cliId=$this->getCliId();
  2264.         if (!isset($isCliAdminBuffer[$clientId])) {
  2265.             $s_query ="SELECT CLU_ISADMIN FROM T_CLI_USER ";
  2266.             $s_query.=" WHERE CLI_ID=".intval($cliId)." AND USR_ID=".$this->getId();
  2267.             if ($s_res=$GLOBALS[$this->get_gDBIDX()]->queryArray($s_query,0)) {
  2268.                 $isCliAdminBuffer[intval($cliId)]=(intval($s_resTrue False);
  2269.             else {
  2270.                 $isCliAdminBuffer[intval($cliId)]=False;
  2271.             }
  2272.         }
  2273.         return $isCliAdminBuffer[intval($cliId)];
  2274.     }
  2275.  
  2276.     /**
  2277.      * checks if the user is staff of a client
  2278.      *
  2279.      *
  2280.      * uses the array ($this->isCliAdminBuffer to buffer results)
  2281.      *
  2282.      * @param int $cliId if 0 $this->getCliId() is called
  2283.      * @param boolean $debug 
  2284.      *
  2285.      * @return boolen 
  2286.      *
  2287.      * @since pk-06-10-11
  2288.      *
  2289.      ***/
  2290.     function isClientStaff($cliId=0,$debug=False{
  2291.         if ($debugechoDebugMethod(__FILE__,get_class($this),"USER::isClientStaff()");
  2292.  
  2293.         if (!intval($cliId)) {
  2294.             return (intval($this->getCliId()) True False);
  2295.         }
  2296.  
  2297.         if ($this->getCliId()==$clientIdreturn True;
  2298.  
  2299.         $s_query="SELECT COUNT(*) FROM T_CLI_USER WHERE USR_ID=".$this->getId()." AND CLI_ID=".intval($cliId);
  2300.         return (intval($GLOBALS['OCSP_OBJ']['USRDB']->quickQuery($s_query,0)) True False);
  2301.  
  2302.     }
  2303.  
  2304.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  2305.     /*  Profile methods                                                     */
  2306.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  2307.  
  2308.     /**
  2309.      * stores profile data to the database
  2310.      * calls addProfileObj() for each value array
  2311.      *
  2312.      * @param int $clpId 
  2313.      * @param array $valArr 
  2314.      * @param boolean $multiArr    if the profile allows multiple entries is $valArr a single entry or an array of entries?
  2315.      * @param boolean $dataCheck   check the data by calling
  2316.      * @param boolean $debug 
  2317.      *
  2318.      * @return int     number of rows added
  2319.      *
  2320.      * @since pk-05-08-24
  2321.      *
  2322.      ***/
  2323.     function addProfile($clpId,$valArr,$multiArr=False,$dataCheck=True,$debug=False{
  2324.         if ($debugecho "<p><b>USER::addProfile($clpId,....)</b> (".get_class($this).")</p>";
  2325.  
  2326.         if (!$this->getId())    return False;
  2327.         if (!intval($clpId))    return False;
  2328.         if (!is_array($valArr)) return False;
  2329.  
  2330.         require_once $GLOBALS['OCSP']['DEFAULTCONFPATH']."client.conf.phpinc";
  2331.         require_once $GLOBALS['CLIENT']['PHPINCPATH']."CLI_PROFILE.phpclass";
  2332.  
  2333.         if ($debugecho "<blockquote>";
  2334.         $noRows=0;
  2335.         if ($profile=new CLI_PROFILE(0,$clpId,0)) {
  2336.             if ((!$profile->isMultiple()) || (!$multiArr)) {
  2337.                 // signle data row processing
  2338.                 if ($this->addProfileObj($profile,$valArr,$dataCheck,$debug)) {
  2339.                     $noRows=1;
  2340.                 }
  2341.             else {
  2342.                 foreach($valArr as $profileData{
  2343.                     if ($this->addProfileObj($profile,$profileData,$dataCheck,$debug)) {
  2344.                         $noRows++;
  2345.                     }
  2346.                 }
  2347.             }
  2348.         }
  2349.  
  2350.         if ($debugecho "<p>Returns: <b>$noRows</b></p></blockquote>";
  2351.         return $noRows;
  2352.     }
  2353.  
  2354.     /**
  2355.      * stores profile data to the database
  2356.      * processes only one data row
  2357.      *
  2358.      * @param CLP_PROFILE $profile 
  2359.      * @param array $valArr 
  2360.      * @param boolean $dataCheck   check the data by calling
  2361.      * @param boolean $debug 
  2362.      *
  2363.      * @return boolean 
  2364.      *
  2365.      * @since pk-05-08-24
  2366.      *
  2367.      ***/
  2368.     function addProfileObj($profile,$valArr,$dataCheck=True,$debug=False{
  2369.         if ($debugecho "<p><b>USER::addProfileObj(...)</b> (".get_class($this).")</p>";
  2370.  
  2371.         if ($debugecho "<blockquote><pre>".print_r($valArr,True)."</pre></blockquote>";
  2372.         if (!$profile->getId()) return False;   // empty profile
  2373.         if (!is_array($valArr)) return False;   // no data
  2374.  
  2375.         $valArr['USR_ID']=$this->getId();
  2376.         if (($dataCheck&& (!$profile->checkEntryRowArr($valArr,$debug))) {
  2377.             return False;
  2378.         }
  2379.  
  2380.         if ($profile->isMultiple()) {
  2381.             return $profile->insertEntryRow($valArr,$debug);
  2382.         else {
  2383.             return $profile->updateEntryRow($valArr,True,$debug);
  2384.         }
  2385.     }
  2386.  
  2387.  
  2388.     /**
  2389.      * deletes all profile entries for the user
  2390.      *
  2391.      * @param CLP_PROFILE $profile 
  2392.      * @param boolean $debug 
  2393.      *
  2394.      * @since pk-05-09-23
  2395.      *
  2396.      ***/
  2397.     function clearProfileObj(&$profile,$debug=False{
  2398.         if ($debugecho "<p><b>USER::clearProfileObj(".$profile->getId().")</b> (".get_class($this).")</p>";
  2399.  
  2400.         if ($profile->getKeyColName(== "USR_ID"{
  2401.             $profile->deleteEntryRow($this->getId(),0,$debug);
  2402.         else {
  2403.             if ($o_client=$this->getClient()) {
  2404.                 $o_client->clearProfileData($profile,False,$debug);
  2405.             }
  2406.         }
  2407.  
  2408.     }
  2409.  
  2410.  
  2411.     /**#@-*/
  2412. }
  2413. ?>

Documentation generated on Thu, 08 Jan 2009 17:47:32 +0100 by phpDocumentor 1.4.0a2