Ignore:
Timestamp:
Mar 31, 2014, 9:10:41 AM (10 years ago)
Author:
chronos
Message:
  • Fixed: Better registration form values validation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/User/User.php

    r791 r801  
    1818                $this->Dependencies = array();
    1919        }
    20        
     20
    2121        function Start()
    2222        {
     
    3838      T('Translators'), array('Name'), '`User`', $this->System->Link('/userlist.php?search='));
    3939        }
    40        
     40
    4141        function ShowOnlineList()
    4242        {
    4343                $Output = T('Online translators').':<br />';
    44                 $DbResult = $this->System->Database->query('SELECT * FROM ('. 
     44                $DbResult = $this->System->Database->query('SELECT * FROM ('.
    4545'SELECT `User`.`Name`, `User`.`ID` FROM `UserOnline` '.
    4646                                'JOIN `User` ON `User`.`ID` = `UserOnline`.`User` '.
     
    5353                }
    5454                return($Output);
    55         }       
     55        }
    5656}
    5757
     
    6161        {
    6262                $Output = '<form action="'.$this->System->Link('/?action=login').'" method="post">'.
    63                         '<fieldset><legend>'.T('Login').'</legend>                     
     63                        '<fieldset><legend>'.T('Login').'</legend>
    6464                        <table>
    6565                        <tr>
     
    9999  var $OnlineStateTimeout;
    100100  var $PreferredVersion = 0;
    101  
     101
    102102  function __construct($System)
    103103  {
     
    105105    $this->Database = &$System->Database;
    106106    $this->OnlineStateTimeout = 600; // in seconds
    107     if(isset($_SESSION)) $this->Check();   
    108   }
    109  
     107    if(isset($_SESSION)) $this->Check();
     108  }
     109
    110110  function __destroy()
    111111  {
    112112  }
    113  
     113
    114114  function Login($Name, $Password, $StayLogged = false)
    115115  {
     
    120120    {
    121121      $User = $DbResult->fetch_assoc();
    122       $this->Id = $User['ID']; 
    123      
    124       // Prepare cookies for permanent login     
     122      $this->Id = $User['ID'];
     123
     124      // Prepare cookies for permanent login
    125125      $StayLoggedSalt = $this->GetPasswordSalt();
    126126      $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array(
    127127        'User' => $User['ID'], 'StayLogged' => $StayLogged, 'StayLoggedHash' => $StayLoggedSalt));
    128       if($StayLogged) 
     128      if($StayLogged)
    129129      {
    130130        setcookie('LoginUserId', $User['ID'], time()+365*24*60*60);
     
    133133        setcookie('LoginUserId', '', time() - 3600);
    134134        setcookie('LoginHash', '', time() - 3600);
    135       }     
    136      
     135      }
     136
    137137      $this->Database->query('UPDATE `UserTrace` SET '.
    138138        '`LastLogin` = NOW(), '.
     
    144144    };
    145145  }
    146  
     146
    147147  function Logout()
    148148  {
     
    157157    }
    158158  }
    159  
     159
    160160  function Load()
    161161  {
     
    180180    } else $this->SetAnonymous();
    181181  }
    182  
     182
    183183  function SetAnonymous()
    184184  {
     
    187187    $this->Role = LICENCE_ANONYMOUS;
    188188    $this->Language = NULL;
    189     $this->Redirecting = 1; 
     189    $this->Redirecting = 1;
    190190    $this->Team = '';
    191191    $this->Email = '';
    192192  }
    193  
     193
    194194  function Licence($Licence)
    195195  {
    196196    if(!isset($_SERVER['REMOTE_ADDR'])) return(true); // Execution from command line
    197     else return($this->Role >= $Licence);   
    198   }
    199  
     197    else return($this->Role >= $Licence);
     198  }
     199
    200200  function CheckToken($Licence, $Token)
    201201  {
     
    207207      $DbRow2 = $DbResult2->fetch_assoc();
    208208      return($DbRow2['GM'] >= $Licence);
    209     } else return(false);   
     209    } else return(false);
    210210  }
    211211
     
    214214    return(substr(sha1(mt_rand()), 0, 8));
    215215  }
    216  
     216
    217217  function CryptPasswordSQL($Password, $Salt)
    218218  {
    219219    return('SHA1(CONCAT(SHA1('.$Password.'), '.$Salt.'))');
    220   } 
    221  
     220  }
     221
    222222  function Check()
    223223  {
     
    229229      // Refresh time of last access
    230230      $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('ActivityTime' => 'NOW()'));
    231     } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 
    232       'User' => null, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 
    233       'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress()), 
     231    } else $this->Database->insert('UserOnline', array('SessionId' => $SID,
     232      'User' => null, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()',
     233      'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress()),
    234234      'ScriptName' => $_SERVER['REQUEST_URI']));
    235      
    236     // Logged permanently? 
    237           if(array_key_exists('LoginHash', $_COOKIE)) 
    238           { 
    239             $DbResult = $this->Database->query('SELECT * FROM `UserOnline` WHERE `User`='.$_COOKIE['LoginUserId']. 
    240               ' AND `StayLogged`=1 AND SessionId!="'.$SID.'"'); 
    241             if($DbResult->num_rows > 0) 
    242             { 
    243               $DbRow = $DbResult->fetch_assoc(); 
    244               if(sha1($_COOKIE['LoginUserId'].$DbRow['StayLoggedHash']) == $_COOKIE['LoginHash']) 
    245               {               
    246                 $this->Database->query('DELETE FROM `UserOnline` WHERE `SessionId`="'.$SID.'"'); 
    247                 $this->Database->query('UPDATE `UserOnline` SET `SessionId`="'.$SID.'" WHERE `Id`='.$DbRow['Id']); 
    248               } 
    249             } 
    250     } 
    251        
     235
     236    // Logged permanently?
     237          if(array_key_exists('LoginHash', $_COOKIE))
     238          {
     239            $DbResult = $this->Database->query('SELECT * FROM `UserOnline` WHERE `User`='.$_COOKIE['LoginUserId'].
     240              ' AND `StayLogged`=1 AND SessionId!="'.$SID.'"');
     241            if($DbResult->num_rows > 0)
     242            {
     243              $DbRow = $DbResult->fetch_assoc();
     244              if(sha1($_COOKIE['LoginUserId'].$DbRow['StayLoggedHash']) == $_COOKIE['LoginHash'])
     245              {
     246                $this->Database->query('DELETE FROM `UserOnline` WHERE `SessionId`="'.$SID.'"');
     247                $this->Database->query('UPDATE `UserOnline` SET `SessionId`="'.$SID.'" WHERE `Id`='.$DbRow['Id']);
     248              }
     249            }
     250    }
     251
    252252          // Check login
    253253    $Query = $this->Database->select('UserOnline', '*', '`SessionId`="'.$SID.'"');
    254254    $Row = $Query->fetch_assoc();
    255     if($Row['User'] != '') 
     255    if($Row['User'] != '')
    256256    {
    257257        $this->Id = $Row['User'];
    258258      $this->Load();
    259     } else 
     259    } else
    260260    {
    261261      $this->SetAnonymous();
    262262    }
    263    
     263
    264264    // Remove nonactive users
    265265    $DbResult = $this->Database->select('UserOnline', '`Id`, `User`', '(`ActivityTime` < DATE_SUB(NOW(), INTERVAL '.$this->OnlineStateTimeout.' SECOND)) AND (`StayLogged` = 0)');
     
    269269    }
    270270  }
    271  
     271
    272272  function Register($UserName, $Password, $Email, $Language, $Team, $PreferredVersion)
    273273  {
    274274    $Salt = $this->GetPasswordSalt();
     275    if($Team == null) $Team = 'NULL';
     276    if($PreferredVersion == null) $PreferredVersion = 'NULL';
    275277    $this->Database->query('INSERT INTO `User` '.
    276278      '(`Name` , `Pass` , `Salt`, `Email` , `Language` , `Team` , `NeedUpdate`, `RegistrationTime`, `PreferredVersion` ) '.
Note: See TracChangeset for help on using the changeset viewer.