Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.91 KB | None | 0 0
  1. <?php
  2.  
  3. class User extends Model
  4. {
  5.     public function Login($Username, $Password, $Remember = null)
  6.     {
  7.         if (empty($Username) || strlen($Username) == 0)
  8.             echo 'Intet brugernavn indtastet';
  9.         elseif (empty($Password) || strlen($Password) == 0)
  10.             echo 'Intet kodeord indtastet';
  11.         else
  12.         {
  13.             // Check if the username exists in the database
  14.             $SqlString = sprintf("SELECT `UserId`, `Username`, `Password` FROM `users` WHERE `Username` = '%s'", mysql_real_escape_string($Username));
  15.             $Result = $this->Database->Query($SqlString);
  16.             if ($Result->NumRows() == 0)
  17.                 echo 'Forkert brugernavn';
  18.             else
  19.             {
  20.                 // Check if the password matches
  21.                 $SqlString .= sprintf(" AND `Password` = '%s'", mysql_real_escape_string($Password));
  22.                 $Result = $this->Database->Query($SqlString);
  23.                 if ($Result->NumRows() == 0)
  24.                     echo 'Forkert kodeord';
  25.                 else
  26.                 {
  27.                     $User = $Result->Fetch();
  28.                    
  29.                     // Update LastLogin
  30.                     $SqlString = sprintf("UPDATE `users` SET `LastLogin` = '%s' WHERE `UserId` = '%s'", time(), $User['UserId']);
  31.                     $this->Database->Query($SqlString);
  32.                    
  33.                     Session::Set('UserId', $User['UserId']);
  34.                    
  35.                     if (is_null($Remember))
  36.                     {
  37.                         $CookieData = $User['UserId'] . '|' . $User['Username'] . '|' . $User['Password'];
  38.                         Cookie::Set('DODSDK_KAGE', $CookieData);
  39.                     }
  40.                    
  41.                     if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
  42.                         echo 'Success';
  43.                     else
  44.                     {
  45.                         header('Location: ' . $_SERVER['REQUEST_URI']);
  46.                         exit;
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.     }
  52.    
  53.     public function Logout()
  54.     {
  55.         Session::Destroy();
  56.         Cookie::Delete('DODSDK_KAGE');
  57.         echo 'Success';
  58.     }
  59.    
  60.     public function Create($Username, $Password, $PasswordAgain)
  61.     {
  62.         if (empty($Username) || strlen($Username) == 0)
  63.             echo 'Intet brugernavn valgt!';
  64.         elseif (empty($Password) || strlen($Password) == 0)
  65.             echo 'Intet kodeord valgt!';
  66.         elseif (empty($PasswordAgain) || strlen($PasswordAgain) == 0)
  67.             echo 'Du skal indtaste kodeordet 2 gange!';
  68.         elseif ($Password != $PasswordAgain)
  69.             echo 'De 2 indtastede kodeord var ikke ens!';
  70.         else
  71.         {
  72.             $SqlString = sprintf("SELECT `Username` FROM `users` WHERE `Username` = '%s'", mysql_real_escape_string($Username));
  73.             $Result = $this->Database->Query($SqlString);
  74.             if ($Result->NumRows() != 0)
  75.                 echo 'Brugernavnet findes allerede i systemet!';
  76.             else
  77.             {
  78.                 $Password = md5($Password);
  79.                
  80.                 $SqlString = sprintf("INSERT INTO `users` (`CreatedDate`, `Username`, `Password`) VALUES('%s', '%s', '%s')", time(), mysql_real_escape_string($Username), mysql_real_escape_string($Password));
  81.                 $Result = $this->Database->Query($SqlString);
  82.                 $LastUserId = $Result->InsertId();
  83.                
  84.                 $SqlString = sprintf("INSERT INTO `users_roles` (`UserId`, `RoleId`) VALUES('%s', '%s')", $LastUserId, 6);
  85.                 $this->Database->Query($SqlString);
  86.                
  87.                 echo 'Success'; // Return ajax 'call'
  88.             }
  89.         }
  90.     }
  91.    
  92.     public function Delete($UserId)
  93.     {
  94.         $SqlString = sprintf("DELETE FROM `users` WHERE `UserId` = '%s'", $UserId);
  95.         $this->Database->Query($SqlString);
  96.         $SqlString = sprintf("DELETE FROM `users_roles` WHERE `UserId` = '%s'", $UserId);
  97.         $this->Database->Query($SqlString);
  98.     }
  99.    
  100.     public function GetById($UserId)
  101.     {
  102.         $SqlString = sprintf("SELECT `UserId`, `Username`, `Password` FROM `users` WHERE `UserId` = '%s'", $UserId);
  103.         $Result = $this->Database->Query($SqlString);
  104.         return $Result->Fetch();
  105.     }
  106.    
  107.     public function GetAll()
  108.     {
  109.         $SqlString = "SELECT `UserId`, `Username`, `FirstName`, `SurName` FROM `users` ORDER BY `Username` ASC";
  110.         $Result = $this->Database->Query($SqlString);
  111.         return $Result->FetchAll();
  112.     }
  113.    
  114.     public function GetAllByCreatedDate()
  115.     {
  116.         $SqlString = "SELECT `UserId`, `Username`, `FirstName`, `SurName` FROM `users` ORDER BY `CreatedDate` DESC LIMIT 5";
  117.         $Result = $this->Database->Query($SqlString);
  118.         return $Result->FetchAll();
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement