Ignore:
Timestamp:
Apr 6, 2020, 11:17:40 PM (5 years ago)
Author:
chronos
Message:
  • Modified: Improved code format.
File:
1 edited

Legend:

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

    r828 r873  
    3030  function Hash($Password, $Salt)
    3131  {
    32     return(sha1(sha1($Password).$Salt));
     32    return (sha1(sha1($Password).$Salt));
    3333  }
    3434
    3535  function Verify($Password, $Salt, $StoredHash)
    3636  {
    37     return($this->Hash($Password, $Salt) == $StoredHash);
     37    return ($this->Hash($Password, $Salt) == $StoredHash);
    3838  }
    3939
     
    7171    // Lookup user record
    7272    $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
    73     if($Query->num_rows > 0)
     73    if ($Query->num_rows > 0)
    7474    {
    7575      // Refresh time of last access
     
    8181
    8282    // Logged permanently?
    83     if(array_key_exists('LoginHash', $_COOKIE))
     83    if (array_key_exists('LoginHash', $_COOKIE))
    8484    {
    8585      $DbResult = $this->Database->query('SELECT * FROM `UserOnline` WHERE `User`='.$_COOKIE['LoginUserId'].
    8686        ' AND `StayLogged`=1 AND SessionId!="'.$SID.'"');
    87       if($DbResult->num_rows > 0)
     87      if ($DbResult->num_rows > 0)
    8888      {
    8989        $DbRow = $DbResult->fetch_assoc();
    90         if(sha1($_COOKIE['LoginUserId'].$DbRow['StayLoggedHash']) == $_COOKIE['LoginHash'])
     90        if (sha1($_COOKIE['LoginUserId'].$DbRow['StayLoggedHash']) == $_COOKIE['LoginHash'])
    9191        {
    9292          $this->Database->query('DELETE FROM `UserOnline` WHERE `SessionId`="'.$SID.'"');
     
    9999    $Query = $this->Database->select('UserOnline', '*', '`SessionId`="'.$SID.'"');
    100100    $Row = $Query->fetch_assoc();
    101     if($Row['User'] != '')
     101    if ($Row['User'] != '')
    102102    {
    103103      $Query = $this->Database->query('SELECT `User`.*, `UserCustomerRel`.`Customer` AS `Member` FROM `User` '.
     
    114114    // Remove nonactive users
    115115    $DbResult = $this->Database->select('UserOnline', '`Id`, `User`', '(`ActivityTime` < DATE_SUB(NOW(), INTERVAL '.$this->OnlineStateTimeout.' SECOND)) AND (`StayLogged` = 0)');
    116     while($DbRow = $DbResult->fetch_array())
     116    while ($DbRow = $DbResult->fetch_array())
    117117    {
    118118      $this->Database->delete('UserOnline', 'Id='.$DbRow['Id']);
    119       if($DbRow['User'] != null) $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Logout');
     119      if ($DbRow['User'] != null) $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Logout');
    120120    }
    121121    //$this->LoadPermission($this->User['Role']);
     
    127127  function Register($Login, $Password, $Password2, $Email, $Name)
    128128  {
    129     if(($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '')  || ($Name == '')) $Result = DATA_MISSING;
    130     else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
     129    if (($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '')  || ($Name == '')) $Result = DATA_MISSING;
     130    else if ($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
    131131    else
    132132    {
    133133      // Is user registred yet?
    134134      $Query = $this->Database->select('User', '*', 'Login = "'.$Login.'"');
    135       if($Query->num_rows > 0) $Result = LOGIN_USED;
     135      if ($Query->num_rows > 0) $Result = LOGIN_USED;
    136136      else
    137137      {
    138138        $Query = $this->Database->select('User', '*', 'Name = "'.$Name.'"');
    139         if($Query->num_rows > 0) $Result = NAME_USED;
     139        if ($Query->num_rows > 0) $Result = NAME_USED;
    140140        else
    141141        {
    142142          $Query = $this->Database->select('User', '*', 'Email = "'.$Email.'"');
    143           if($Query->num_rows > 0) $Result = EMAIL_USED;
     143          if ($Query->num_rows > 0) $Result = EMAIL_USED;
    144144          else
    145145          {
     
    177177      }
    178178    }
    179     return($Result);
     179    return ($Result);
    180180  }
    181181
     
    183183  {
    184184    $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
    185     if($DbResult->num_rows > 0)
     185    if ($DbResult->num_rows > 0)
    186186    {
    187187      $Row = $DbResult->fetch_array();
    188188      $NewPassword = substr(sha1(strtoupper($Row['Login'])), 0, 7);
    189       if($Hash == $NewPassword)
     189      if ($Hash == $NewPassword)
    190190      {
    191191        $this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0));
     
    195195      } else $Output = PASSWORDS_UNMATCHED;
    196196    } else $Output = USER_NOT_FOUND;
    197     return($Output);
     197    return ($Output);
    198198  }
    199199
    200200  function Login($Login, $Password, $StayLogged = false)
    201201  {
    202     if($StayLogged) $StayLogged = 1; else $StayLogged = 0;
     202    if ($StayLogged) $StayLogged = 1; else $StayLogged = 0;
    203203    $SID = session_id();
    204204    $Query = $this->Database->select('User', '*', 'Login="'.$Login.'"');
    205     if($Query->num_rows > 0)
     205    if ($Query->num_rows > 0)
    206206    {
    207207      $Row = $Query->fetch_assoc();
    208208      $PasswordHash = new PasswordHash();
    209       if(!$PasswordHash->Verify($Password, $Row['Salt'], $Row['Password'])) $Result = BAD_PASSWORD;
    210       else if($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED;
     209      if (!$PasswordHash->Verify($Password, $Row['Salt'], $Row['Password'])) $Result = BAD_PASSWORD;
     210      else if ($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED;
    211211      else
    212212      {
     
    217217        $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array(
    218218          'User' => $Row['Id'], 'StayLogged' => $StayLogged, 'StayLoggedHash' => $StayLoggedSalt));
    219         if($StayLogged)
     219        if ($StayLogged)
    220220        {
    221221          setcookie('LoginUserId', $Row['Id'], time()+365*24*60*60, $this->System->Link('/'));
     
    231231      }
    232232    } else $Result = USER_NOT_REGISTRED;
    233     return($Result);
     233    return ($Result);
    234234  }
    235235
     
    240240    $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Logout', $this->User['Login']);
    241241    $this->Check();
    242     return(USER_LOGGED_OUT);
     242    return (USER_LOGGED_OUT);
    243243  }
    244244
     
    247247    $this->Roles = array();
    248248    $DbResult = $this->Database->select('UserRole', '*');
    249     while($DbRow = $DbResult->fetch_array())
     249    while ($DbRow = $DbResult->fetch_array())
    250250      $this->Roles[] = $DbRow;
    251251  }
     
    255255    $this->User['Permission'] = array();
    256256    $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role);
    257     if($DbResult->num_rows > 0)
    258     while($DbRow = $DbResult->fetch_array())
     257    if ($DbResult->num_rows > 0)
     258    while ($DbRow = $DbResult->fetch_array())
    259259      $this->User['Permission'][$DbRow['Operation']] = $DbRow;
    260260  }
     
    264264    $Result = array();
    265265    $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description`, `UserRole`.`Title` FROM `UserRolePermission` LEFT JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` LEFT JOIN `UserRole` ON `UserRole`.`Id` = `UserRolePermission`.`Role`');
    266     while($DbRow = $DbResult->fetch_array())
     266    while ($DbRow = $DbResult->fetch_array())
    267267    {
    268268      $Value = '';
    269       if($DbRow['Read']) $Value .= 'R';
    270       if($DbRow['Write']) $Value .= 'W';
     269      if ($DbRow['Read']) $Value .= 'R';
     270      if ($DbRow['Write']) $Value .= 'W';
    271271      $Result[$DbRow['Description']][$DbRow['Title']] = $Value;
    272272    }
    273     return($Result);
     273    return ($Result);
    274274  }
    275275
     
    278278    $PermissionExists = false;
    279279    // First try to check cache group-group relation
    280     if(array_key_exists($GroupId, $this->PermissionGroupCache))
     280    if (array_key_exists($GroupId, $this->PermissionGroupCache))
    281281    {
    282282      $PermissionExists = true;
     
    287287        '") AND (`AssignedGroup` IS NOT NULL)');
    288288      $DbRow = array();
    289       while($DbRow[] = $DbResult->fetch_array());
     289      while ($DbRow[] = $DbResult->fetch_array());
    290290        $this->PermissionGroupCache[$GroupId] = $DbRow;
    291291      $PermissionExists = true;
    292292    }
    293     if($PermissionExists)
    294     {
    295       foreach($this->PermissionGroupCache[$GroupId] as $DbRow)
    296       {
    297         if($DbRow['AssignedGroup'] != '')
    298         if($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return(true);
     293    if ($PermissionExists)
     294    {
     295      foreach ($this->PermissionGroupCache[$GroupId] as $DbRow)
     296      {
     297        if ($DbRow['AssignedGroup'] != '')
     298        if ($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return (true);
    299299      }
    300300    }
    301301
    302302    // Check group-operation relation
    303     if(array_key_exists($GroupId.','.$OperationId, $this->PermissionGroupCacheOp))
     303    if (array_key_exists($GroupId.','.$OperationId, $this->PermissionGroupCacheOp))
    304304    {
    305305      $PermissionExists = true;
     
    308308      // If no permission combination exists in cache, do new check of database items
    309309      $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `AssignedOperation`="'.$OperationId.'"');
    310       if($DbResult->num_rows > 0) $this->PermissionGroupCacheOp[$GroupId.','.$OperationId] = true;
     310      if ($DbResult->num_rows > 0) $this->PermissionGroupCacheOp[$GroupId.','.$OperationId] = true;
    311311        else $this->PermissionGroupCacheOp[$GroupId.','.$OperationId] = false;
    312312      $PermissionExists = true;
    313313    }
    314     if($PermissionExists)
    315     {
    316       return($this->PermissionGroupCacheOp[$GroupId.','.$OperationId]);
    317     }
    318     return(false);
     314    if ($PermissionExists)
     315    {
     316      return ($this->PermissionGroupCacheOp[$GroupId.','.$OperationId]);
     317    }
     318    return (false);
    319319  }
    320320
     
    323323    // Get module id
    324324    $DbResult = $this->Database->select('Module', 'Id', '`Name`="'.$Module.'"');
    325     if($DbResult->num_rows > 0)
     325    if ($DbResult->num_rows > 0)
    326326    {
    327327      $DbRow = $DbResult->fetch_assoc();
    328328      $ModuleId = $DbRow['Id'];
    329     } else return(false);
     329    } else return (false);
    330330
    331331    // First try to check cache
    332     if(in_array(array($Module, $Operation, $ItemType, $ItemType), $this->PermissionCache))
     332    if (in_array(array($Module, $Operation, $ItemType, $ItemType), $this->PermissionCache))
    333333    {
    334334      $OperationId = array_search(array($Module, $Operation, $ItemType, $ItemIndex), $this->PermissionCache);
     
    339339      $DbResult = $this->Database->select('PermissionOperation', 'Id', '(`Module`="'.$ModuleId.
    340340        '") AND (`Item`="'.$ItemType.'") AND (`ItemId`='.$ItemIndex.') AND (`Operation`="'.$Operation.'")');
    341       if($DbResult->num_rows > 0)
     341      if ($DbResult->num_rows > 0)
    342342      {
    343343        $DbRow = $DbResult->fetch_array();
     
    352352    }
    353353
    354     if($PermissionExists)
    355     {
    356       if($this->User['Id'] == null) $UserCondition = '(`User` IS NULL)';
     354    if ($PermissionExists)
     355    {
     356      if ($this->User['Id'] == null) $UserCondition = '(`User` IS NULL)';
    357357        else $UserCondition = '(`User`="'.$this->User['Id'].'")';
    358358      // Check user-operation relation
    359359      $DbResult = $this->Database->select('PermissionUserAssignment', '*', $UserCondition.' AND (`AssignedOperation`="'.$OperationId.'")');
    360       if($DbResult->num_rows > 0) return(true);
     360      if ($DbResult->num_rows > 0) return (true);
    361361
    362362      // Check user-group relation
    363363      $DbResult = $this->Database->select('PermissionUserAssignment', 'AssignedGroup', $UserCondition);
    364       while($DbRow = $DbResult->fetch_array())
    365       {
    366        if($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return(true);
    367       }
    368       return(false);
    369     } else return(false);
     364      while ($DbRow = $DbResult->fetch_array())
     365      {
     366       if ($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return (true);
     367      }
     368      return (false);
     369    } else return (false);
    370370  }
    371371
     
    373373  {
    374374    $DbResult = $this->Database->select('User', 'Login, Name, Id, Email, Password', '`Login`="'.$Login.'" AND `Email`="'.$Email.'"');
    375     if($DbResult->num_rows > 0)
     375    if ($DbResult->num_rows > 0)
    376376    {
    377377      $Row = $DbResult->fetch_array();
     
    394394      $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email);
    395395    } else $Output = USER_PASSWORD_RECOVERY_FAIL;
    396     return($Output);
     396    return ($Output);
    397397  }
    398398
     
    400400  {
    401401    $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
    402     if($DbResult->num_rows > 0)
     402    if ($DbResult->num_rows > 0)
    403403    {
    404404      $Row = $DbResult->fetch_array();
    405405      $NewPassword2 = substr(sha1(strtoupper($Row['Login'])), 0, 7);
    406       if(($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))
     406      if (($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))
    407407      {
    408408        $PasswordHash = new PasswordHash();
     
    414414      } else $Output = PASSWORDS_UNMATCHED;
    415415    } else $Output = USER_NOT_FOUND;
    416     return($Output);
     416    return ($Output);
    417417  }
    418418
     
    420420  {
    421421    $DbResult = $this->Database->select('APIToken', 'User', '`Token`="'.$Token.'"');
    422     if($DbResult->num_rows > 0)
     422    if ($DbResult->num_rows > 0)
    423423    {
    424424      $DbRow = $DbResult->fetch_assoc();
    425425      $User = new User($this->System);
    426426      $User->User = array('Id' => $DbRow['User']);
    427       return($User->CheckPermission($Module, $Operation));
    428     } else return(false);
     427      return ($User->CheckPermission($Module, $Operation));
     428    } else return (false);
    429429  }
    430430}
Note: See TracChangeset for help on using the changeset viewer.