Dotclear


Ignore:
Timestamp:
11/17/13 20:25:53 (12 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
2.6
Children:
2567:6c11245cbf04, 2568:61c67a7d17fa
Message:

Add some people in CREDITS, remove trailing spaces and tabs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.auth.php

    r2311 r2566  
    1616* @nosubgrouping 
    1717* @brief Authentication and user credentials management 
    18 *  
     18* 
    1919* dcAuth is a class used to handle everything related to user authentication 
    2020* and credentials. Object is provided by dcCore $auth property. 
     
    2626     /** @var connection Database connection object */ 
    2727     protected $con; 
    28       
     28 
    2929     /** @var string User table name */ 
    3030     protected $user_table; 
    3131     /** @var string Perm table name */ 
    3232     protected $perm_table; 
    33       
     33 
    3434     /** @var string Current user ID */ 
    3535     protected $user_id; 
     
    5050     /** @var integer Count of user blogs */ 
    5151     public $blog_count = null; 
    52       
     52 
    5353     /** @var array Permission types */ 
    5454     protected $perm_types; 
    55       
     55 
    5656     /** @var dcPrefs dcPrefs object */ 
    5757     public $user_prefs; 
    58       
     58 
    5959     /** 
    6060     * Class constructor. Takes dcCore object as single argument. 
    61      *  
     61     * 
    6262     * @param dcCore     $core          dcCore object 
    6363     */ 
     
    6969          $this->user_table = $core->prefix.'user'; 
    7070          $this->perm_table = $core->prefix.'permissions'; 
    71            
     71 
    7272          $this->perm_types = array( 
    7373               'admin' => __('administrator'), 
     
    8181          ); 
    8282     } 
    83       
     83 
    8484     /// @name Credentials and user permissions 
    8585     //@{ 
     
    8888     * while you may need to check user without password. This method will create 
    8989     * credentials and populate all needed object properties. 
    90      *  
     90     * 
    9191     * @param string     $user_id       User ID 
    9292     * @param string     $pwd           User password 
     
    104104                    'FROM '.$this->con->escapeSystem($this->user_table).' '. 
    105105                    "WHERE user_id = '".$this->con->escape($user_id)."' "; 
    106            
     106 
    107107          try { 
    108108               $rs = $this->con->select($strReq); 
     
    110110               $err = $e->getMessage(); 
    111111               return false; 
    112           }          
    113            
     112          } 
     113 
    114114          if ($rs->isEmpty()) { 
    115115               sleep(rand(2,5)); 
    116116               return false; 
    117117          } 
    118            
     118 
    119119          $rs->extend('rsExtUser'); 
    120            
     120 
    121121          if ($pwd != '') 
    122122          { 
     
    132132               } 
    133133          } 
    134            
     134 
    135135          $this->user_id = $rs->user_id; 
    136136          $this->user_change_pwd = (boolean) $rs->user_change_pwd; 
    137137          $this->user_admin = (boolean) $rs->user_super; 
    138            
     138 
    139139          $this->user_info['user_pwd'] = $rs->user_pwd; 
    140140          $this->user_info['user_name'] = $rs->user_name; 
     
    149149          $this->user_info['user_creadt'] = $rs->user_creadt; 
    150150          $this->user_info['user_upddt'] = $rs->user_upddt; 
    151            
     151 
    152152          $this->user_info['user_cn'] = dcUtils::getUserCN($rs->user_id, $rs->user_name, 
    153153          $rs->user_firstname, $rs->user_displayname); 
    154            
     154 
    155155          $this->user_options = array_merge($this->core->userDefaults(),$rs->options()); 
    156            
     156 
    157157          $this->user_prefs = new dcPrefs($this->core,$this->user_id); 
    158            
     158 
    159159          # Get permissions on blogs 
    160160          if ($check_blog && ($this->findUserBlog() === false)) { 
     
    163163          return true; 
    164164     } 
    165       
     165 
    166166     /** 
    167167     * This method only check current user password. 
    168      *  
     168     * 
    169169     * @param string     $pwd           User password 
    170170     * @return boolean 
     
    175175               return $pwd == $this->user_info['user_pwd']; 
    176176          } 
    177            
     177 
    178178          return false; 
    179179     } 
    180       
     180 
    181181     /** 
    182182     * This method checks if user session cookie exists 
    183      *  
     183     * 
    184184     * @return boolean 
    185185     */ 
     
    188188          return isset($_COOKIE[DC_SESSION_NAME]); 
    189189     } 
    190       
     190 
    191191     /** 
    192192     * This method checks user session validity. 
    193      *  
     193     * 
    194194     * @return boolean 
    195195     */ 
     
    197197     { 
    198198          $this->core->session->start(); 
    199            
     199 
    200200          # If session does not exist, logout. 
    201201          if (!isset($_SESSION['sess_user_id'])) { 
     
    203203               return false; 
    204204          } 
    205            
     205 
    206206          # Check here for user and IP address 
    207207          $this->checkUser($_SESSION['sess_user_id']); 
    208208          $uid = $uid ? $uid : http::browserUID(DC_MASTER_KEY); 
    209            
     209 
    210210          $user_can_log = $this->userID() !== null && $uid == $_SESSION['sess_browser_uid']; 
    211            
     211 
    212212          if (!$user_can_log) { 
    213213               $this->core->session->destroy(); 
    214214               return false; 
    215215          } 
    216            
     216 
    217217          return true; 
    218218     } 
    219       
     219 
    220220     /** 
    221221     * Checks if user must change his password in order to login. 
     
    227227          return $this->user_change_pwd; 
    228228     } 
    229       
     229 
    230230     /** 
    231231     * Checks if user is super admin 
    232      *  
     232     * 
    233233     * @return boolean 
    234234     */ 
     
    237237          return $this->user_admin; 
    238238     } 
    239       
     239 
    240240     /** 
    241241     * Checks if user has permissions given in <var>$permissions</var> for blog 
    242242     * <var>$blog_id</var>. <var>$permissions</var> is a coma separated list of 
    243243     * permissions. 
    244      *  
     244     * 
    245245     * @param string     $permissions   Permissions list 
    246246     * @param string     $blog_id       Blog ID 
     
    252252               return true; 
    253253          } 
    254            
     254 
    255255          $p = explode(',',$permissions); 
    256256          $b = $this->getPermissions($blog_id); 
    257            
     257 
    258258          if ($b != false) 
    259259          { 
     
    261261                    return true; 
    262262               } 
    263                 
     263 
    264264               foreach ($p as $v) 
    265265               { 
     
    269269               } 
    270270          } 
    271            
     271 
    272272          return false; 
    273273     } 
    274       
     274 
    275275     /** 
    276276     * Returns true if user is allowed to change its password. 
    277      *  
     277     * 
    278278     * @return boolean 
    279279     */ 
     
    283283     } 
    284284     //@} 
    285       
     285 
    286286     /// @name User code handlers 
    287287     //@{ 
     
    293293          return bin2hex($code); 
    294294     } 
    295       
     295 
    296296     public function checkUserCode($code) 
    297297     { 
    298298          $code = @pack('H*',$code); 
    299            
     299 
    300300          $user_id = trim(@pack('a32',substr($code,0,32))); 
    301301          $pwd = @unpack('H40hex',substr($code,32,40)); 
    302            
     302 
    303303          if ($user_id === false || $pwd === false) { 
    304304               return false; 
    305305          } 
    306            
     306 
    307307          $pwd = $pwd['hex']; 
    308            
     308 
    309309          $strReq = 'SELECT user_id, user_pwd '. 
    310310                    'FROM '.$this->user_table.' '. 
    311311                    "WHERE user_id = '".$this->con->escape($user_id)."' "; 
    312            
     312 
    313313          $rs = $this->con->select($strReq); 
    314            
     314 
    315315          if ($rs->isEmpty()) { 
    316316               return false; 
    317317          } 
    318            
     318 
    319319          if (crypt::hmac(DC_MASTER_KEY,$rs->user_pwd) != $pwd) { 
    320320               return false; 
    321321          } 
    322            
     322 
    323323          return $rs->user_id; 
    324324     } 
    325325     //@} 
    326       
    327       
     326 
     327 
    328328     /// @name Sudo 
    329329     //@{ 
     
    331331     * Calls $f function with super admin rights. 
    332332     * Returns the function result. 
    333      *  
     333     * 
    334334     * @param callback   $f             Callback function 
    335335     * @return mixed 
     
    340340               throw new Exception($f.' function doest not exist'); 
    341341          } 
    342            
     342 
    343343          $args = func_get_args(); 
    344344          array_shift($args); 
    345            
     345 
    346346          if ($this->user_admin) { 
    347347               $res = call_user_func_array($f,$args); 
     
    356356               } 
    357357          } 
    358            
     358 
    359359          return $res; 
    360360     } 
    361361     //@} 
    362       
     362 
    363363     /// @name User information and options 
    364364     //@{ 
    365365     /** 
    366366     * Returns user permissions for a blog as an array which looks like: 
    367      *  
     367     * 
    368368     *  - [blog_id] 
    369369     *    - [permission] => true 
    370370     *    - ... 
    371      *  
     371     * 
    372372     * @param string     $blog_id       Blog ID 
    373373     * @return array 
     
    384384                    "WHERE blog_id = '".$this->con->escape($blog_id)."' "; 
    385385               $rs = $this->con->select($strReq); 
    386                 
     386 
    387387               $this->blogs[$blog_id] = $rs->isEmpty() ? false : array('admin' => true); 
    388                 
     388 
    389389               return $this->blogs[$blog_id]; 
    390390          } 
    391            
     391 
    392392          $strReq = 'SELECT permissions '. 
    393393                    'FROM '.$this->perm_table.' '. 
     
    396396                    "AND (permissions LIKE '%|usage|%' OR permissions LIKE '%|admin|%' OR permissions LIKE '%|contentadmin|%') "; 
    397397          $rs = $this->con->select($strReq); 
    398            
     398 
    399399          $this->blogs[$blog_id] = $rs->isEmpty() ? false : $this->parsePermissions($rs->permissions); 
    400            
     400 
    401401          return $this->blogs[$blog_id]; 
    402402     } 
     
    409409        return $this->blog_count; 
    410410    } 
    411       
     411 
    412412     public function findUserBlog($blog_id=null) 
    413413     { 
     
    434434                              $this->con->limit(1); 
    435435               } 
    436                 
     436 
    437437               $rs = $this->con->select($strReq); 
    438438               if (!$rs->isEmpty()) { 
     
    440440               } 
    441441          } 
    442            
     442 
    443443          return false; 
    444444     } 
    445       
     445 
    446446     /** 
    447447     * Returns current user ID 
    448      *  
     448     * 
    449449     * @return string 
    450450     */ 
     
    453453          return $this->user_id; 
    454454     } 
    455       
     455 
    456456     /** 
    457457     * Returns information about a user . 
    458      *  
     458     * 
    459459     * @param string     $n             Information name 
    460460     * @return string 
     
    465465               return $this->user_info[$n]; 
    466466          } 
    467            
     467 
    468468          return null; 
    469469     } 
    470       
     470 
    471471     /** 
    472472     * Returns a specific user option 
    473      *  
     473     * 
    474474     * @param string     $n             Option name 
    475475     * @return string 
     
    482482          return null; 
    483483     } 
    484       
     484 
    485485     /** 
    486486     * Returns all user options in an associative array. 
    487      *  
     487     * 
    488488     * @return array 
    489489     */ 
     
    493493     } 
    494494     //@} 
    495       
     495 
    496496     /// @name Permissions 
    497497     //@{ 
    498498     /** 
    499499     * Returns an array with permissions parsed from the string <var>$level</var> 
    500      *  
     500     * 
    501501     * @param string     $level         Permissions string 
    502502     * @return array 
     
    506506          $level = preg_replace('/^\|/','',$level); 
    507507          $level = preg_replace('/\|$/','',$level); 
    508            
     508 
    509509          $res = array(); 
    510510          foreach (explode('|',$level) as $v) { 
     
    513513          return $res; 
    514514     } 
    515       
     515 
    516516     /** 
    517517     * Returns <var>perm_types</var> property content. 
    518      *  
     518     * 
    519519     * @return array 
    520520     */ 
     
    523523          return $this->perm_types; 
    524524     } 
    525       
     525 
    526526     /** 
    527527     * Adds a new permission type. 
    528      *  
     528     * 
    529529     * @param string     $name          Permission name 
    530530     * @param string     $title         Permission title 
     
    535535     } 
    536536     //@} 
    537       
     537 
    538538     /// @name Password recovery 
    539539     //@{ 
     
    541541     * Add a recover key to a specific user identified by its email and 
    542542     * password. 
    543      *  
     543     * 
    544544     * @param string     $user_id       User ID 
    545545     * @param string     $user_email    User Email 
     
    552552                    "WHERE user_id = '".$this->con->escape($user_id)."' ". 
    553553                    "AND user_email = '".$this->con->escape($user_email)."' "; 
    554            
     554 
    555555          $rs = $this->con->select($strReq); 
    556            
     556 
    557557          if ($rs->isEmpty()) { 
    558558               throw new Exception(__('That user does not exist in the database.')); 
    559559          } 
    560            
     560 
    561561          $key = md5(uniqid()); 
    562            
     562 
    563563          $cur = $this->con->openCursor($this->user_table); 
    564564          $cur->user_recover_key = $key; 
    565            
     565 
    566566          $cur->update("WHERE user_id = '".$this->con->escape($user_id)."'"); 
    567            
     567 
    568568          return $key; 
    569569     } 
    570       
     570 
    571571     /** 
    572572     * Creates a new user password using recovery key. Returns an array: 
    573      *  
     573     * 
    574574     * - user_email 
    575575     * - user_id 
    576576     * - new_pass 
    577      *  
     577     * 
    578578     * @param string     $recover_key   Recovery key 
    579579     * @return array 
     
    584584                    'FROM '.$this->user_table.' '. 
    585585                    "WHERE user_recover_key = '".$this->con->escape($recover_key)."' "; 
    586            
     586 
    587587          $rs = $this->con->select($strReq); 
    588            
     588 
    589589          if ($rs->isEmpty()) { 
    590590               throw new Exception(__('That key does not exist in the database.')); 
    591591          } 
    592            
     592 
    593593          $new_pass = crypt::createPassword(); 
    594            
     594 
    595595          $cur = $this->con->openCursor($this->user_table); 
    596596          $cur->user_pwd = crypt::hmac(DC_MASTER_KEY,$new_pass); 
    597597          $cur->user_recover_key = null; 
    598            
     598 
    599599          $cur->update("WHERE user_recover_key = '".$this->con->escape($recover_key)."'"); 
    600            
     600 
    601601          return array('user_email' => $rs->user_email, 'user_id' => $rs->user_id, 'new_pass' => $new_pass); 
    602602     } 
    603603     //@} 
    604       
     604 
    605605     /** @name User management callbacks 
    606606     This 3 functions only matter if you extend this class and use 
     
    611611     */ 
    612612     //@{ 
    613       
     613 
    614614     /** 
    615615     * Called after core->addUser 
     
    618618     */ 
    619619     public function afterAddUser($cur) {} 
    620       
     620 
    621621     /** 
    622622     * Called after core->updUser 
     
    626626     */ 
    627627     public function afterUpdUser($id,$cur) {} 
    628       
     628 
    629629     /** 
    630630     * Called after core->delUser 
     
    635635     //@} 
    636636} 
    637 ?> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map