Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.23 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Revolution;
  4. if(!defined('IN_INDEX')) { die('Sorry, you cannot access this file.'); }
  5. if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  6.   $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
  7. }
  8. class users implements iUsers
  9. {  
  10.    
  11.     /*-------------------------------Authenticate-------------------------------------*/
  12.    
  13.     final public function isLogged()
  14.     {
  15.         if(isset($_SESSION['user']['id']))
  16.         {
  17.             return true;
  18.         }
  19.        
  20.         return false;
  21.     }
  22.    
  23.     /*-------------------------------Checking of submitted data-------------------------------------*/
  24.    
  25.         final public function validName($username)  
  26. {
  27.     if(strlen($username) <= 25 && preg_match("/^[a-zA-Z0-9]+$/", $username))  
  28.     {          
  29.            return true;        
  30.     }              
  31.  
  32.         return false;    
  33.     }      
  34.          
  35.     final public function validEmail($email)    
  36.     {      
  37.         return preg_match("/^[a-z0-9_\.-]+@([a-z0-9]+([\-]+[a-z0-9]+)*\.)+[a-z]{2,7}$/i", $email);  
  38.     }      
  39.    
  40.     final public function validSecKey($seckey)
  41.     {
  42.         if(is_numeric($seckey) && strlen($seckey) == 4)
  43.         {
  44.             return true;
  45.         }
  46.        
  47.         return false;
  48.     }
  49.    
  50.     final public function nameTaken($username)  
  51.     {      
  52.         global $engine;        
  53.        
  54.         if($engine->num_rows("SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1") > 0)
  55.         {
  56.             return true;
  57.         }  
  58.        
  59.         return false;
  60.     }
  61.    
  62.     final public function emailTaken($email)
  63.     {
  64.         global $engine;
  65.        
  66.         if($engine->num_rows("SELECT * FROM users WHERE mail = '" . $email . "' LIMIT 1") > 0)
  67.         {
  68.             return true;
  69.         }
  70.        
  71.         return false;
  72.     }
  73.        
  74.     final public function userValidation($username, $password)
  75.     {      
  76.         global $engine;
  77.         if($engine->num_rows("SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . $password . "' LIMIT 1") > 0)
  78.         {
  79.             return true;
  80.         }  
  81.          
  82.         return false;
  83.     }    
  84.    
  85.     /*-------------------------------Stuff related to bans-------------------------------------*/
  86.    
  87.     final public function isBanned($value)
  88.     {
  89.         global $engine;
  90.          
  91.         if ($engine->num_rows("SELECT * FROM bans WHERE value = '" . $value . "' AND expire >= '" . time() . "' ") > 0)
  92.         {
  93.             return true;
  94.         }
  95.  
  96.         return false;  
  97.     }  
  98.    
  99.     final public function getReason($value)
  100.     {
  101.         global $engine;
  102.         return $engine->result("SELECT reason FROM bans WHERE value = '" . $value . "' LIMIT 1");
  103.     }
  104.    
  105.     final public function hasClones($ip)
  106.     {
  107.         global $engine;
  108.         if($engine->num_rows("SELECT * FROM users WHERE ip_last = '" . $_SERVER['REMOTE_ADDR'] . "'") >= 3)
  109.         {
  110.             return true;
  111.         }
  112.        
  113.         return false;
  114.     }
  115.    
  116.     /*-------------------------------Login or Register user-------------------------------------*/
  117.    
  118.     final public function register()
  119.     {
  120.         global $core, $template, $_CONFIG;
  121.        
  122.         if(isset($_POST['register']))
  123.         {
  124.             unset($template->form->error);
  125.            
  126.             $template->form->setData();
  127.                
  128.             if($this->validName($template->form->reg_username))
  129.             {
  130.                 if(!$this->nameTaken($template->form->reg_username))
  131.                 {
  132.                     if($this->validEmail($template->form->reg_email))
  133.                     {
  134.                         if(!$this->emailTaken($template->form->reg_email))
  135.                         {
  136.                             if(strlen($template->form->reg_password) > 6)
  137.                             {
  138.                                 if($template->form->reg_password == $template->form->reg_rep_password)
  139.                                 {
  140.                                     if(isset($template->form->reg_seckey))
  141.                                     {
  142.                                         if($this->validSecKey($template->form->reg_seckey))
  143.                                         {
  144.                                             //Continue
  145.                                         }
  146.                                         else
  147.                                         {
  148.                                             $template->form->error = 'Secret key must only have 4 numbers';
  149.                                             return;
  150.                                         }
  151.                                     }
  152.                                     if($this->isBanned($_SERVER['REMOTE_ADDR']) == false)
  153.                                     {
  154.                                         if(!$this->hasClones($_SERVER['REMOTE_ADDR']))
  155.                                         {                                                                                                              
  156.                                            
  157.                                                 if(!isset($template->form->reg_gender)) { $template->form->reg_gender = 'M'; }
  158.                                                 if(!isset($template->form->reg_figure)) { $template->form->reg_figure = $_CONFIG['hotel']['figure']; }
  159.                                            
  160.                                                 $this->addUser($template->form->reg_username, $core->hashed($template->form->reg_password), $template->form->reg_email, $_CONFIG['hotel']['motto'], $_CONFIG['hotel']['credits'], $_CONFIG['hotel']['pixels'], 1, $template->form->reg_figure, $template->form->reg_gender, $core->hashed($template->form->reg_key));
  161.                                
  162.                                                 $this->turnOn($template->form->reg_username);
  163.                                        
  164.                                                 if(isset($template->form->reg_referrer) && $template->form->reg_referrer != "") {
  165.                                                     $this->referUser($template->form->reg_referrer);
  166.                                                 }
  167.                                        
  168.                                                 header('Location: ' . $_CONFIG['hotel']['url'] . '/me');
  169.                                                 exit;
  170.                                                                                    
  171.                                            
  172.                                         }
  173.                                         else
  174.                                         {
  175.                                             $template->form->error = 'Извините, но вы не можете зарегистрировать более чем в 3 раза!';
  176.                                         }
  177.                                     }
  178.                                     else
  179.                                     {
  180.                                         $template->form->error = 'Извините, кажется Вы забанены по IP<br />';
  181.                                         $template->form->error .= 'Причина: ' . $this->getReason($_SERVER['REMOTE_ADDR']);
  182.                                         return;
  183.                                     }
  184.                                 }
  185.                                 else   
  186.                                 {
  187.                                     $template->form->error = 'Пороли не совпадают. Попробуйте еще раз';
  188.                                     return;
  189.                                 }
  190.  
  191.                             }
  192.                             else
  193.                             {
  194.                                 $template->form->error = 'Пароль должен содержать не менее 6 цифр/букв';
  195.                                 return;
  196.                             }
  197.                         }
  198.                         else
  199.                         {
  200.                             $template->form->error = 'Электронная почта: <b>' . $template->form->reg_email . '</b>  уже зарегистрирована';
  201.                             return;
  202.                         }
  203.                     }
  204.                     else
  205.                     {
  206.                         $template->form->error = 'Электронная почта не действительна';
  207.                         return;
  208.                     }
  209.                 }
  210.                 else
  211.                 {
  212.                     $template->form->error = 'Имя пользователя уже зарегистрировано';
  213.                     return;
  214.                 }
  215.             }
  216.             else
  217.             {
  218.                 $template->form->error = 'Имя пользователя не действительно';
  219.                 return;
  220.             }
  221.         }
  222.     }  
  223.    
  224.     final public function login()
  225.     {
  226.         global $template, $_CONFIG, $core;
  227.        
  228.         if(isset($_POST['login']))
  229.         {
  230.             $template->form->setData();
  231.             unset($template->form->error);
  232.            
  233.             if($this->nameTaken($template->form->log_username))
  234.             {
  235.                 if($this->isBanned($_SERVER['REMOTE_ADDR']) == false)
  236.     {
  237.         if($this->isBanned($template->form->log_username) == false)
  238.         {
  239.        
  240.                 if($this->userValidation($template->form->log_username, $core->hashed($template->form->log_password)))
  241.                 {
  242.                     $this->turnOn($template->form->log_username);
  243.                     $this->updateUser($_SESSION['user']['id'], 'ip_last', $_SERVER['REMOTE_ADDR']);
  244.                     $template->form->unsetData();
  245.                     header('Location: ' . $_CONFIG['hotel']['url'] . '/me');
  246.                     exit;
  247.                 }
  248.                 else
  249.                 {
  250.                     $template->form->error = 'Детали не совпадают';
  251.                     return;
  252.                 }
  253.            
  254.         }
  255.         else
  256.         {
  257.             $template->form->error = 'Извините, кажется этот пользователь забанен<br />';
  258.             $template->form->error .= 'Причина: ' . $this->getReason($template->form->log_username);
  259.             return;
  260.         }
  261.     }
  262.     else
  263.     {
  264.         $template->form->error = 'Извините, кажется что этот IP забанен<br />';
  265.         $template->form->error .= 'Причина: ' . $this->getReason($_SERVER['REMOTE_ADDR']);
  266.         return;
  267.     }  
  268.             }
  269.             else
  270.             {
  271.                 $template->form->error = 'Имя пользователя не существует';
  272.                 return;
  273.             }
  274.         }
  275.     }
  276.    
  277.     final public function loginHK()
  278.     {
  279.         global $template, $_CONFIG, $core;
  280.        
  281.         if(isset($_POST['login']))
  282.         {  
  283.             $template->form->setData();
  284.             unset($template->form->error);
  285.            
  286.             if(isset($template->form->username) && isset($template->form->password))
  287.             {
  288.                 if($this->nameTaken($template->form->username))
  289.                 {    
  290.                     if($this->userValidation($template->form->username, $core->hashed($template->form->password)))
  291.                     {
  292.                         if(($this->getInfo($_SESSION['user']['id'], 'rank', true)) >= 8)
  293.                         {
  294.                             $_SESSION["in_hk"] = true;
  295.                             header("Location:".$_CONFIG['hotel']['url']."/ase/dash");
  296.                             exit;
  297.                         }
  298.                         else
  299.                         {
  300.                             $template->form->error = 'Incorrect access level';
  301.                             return;
  302.                         }
  303.                     }
  304.                     else
  305.                     {
  306.                         $template->form->error = 'Incorrect password';
  307.                         return;
  308.                     }      
  309.                 }
  310.                 else
  311.                 {
  312.                     $template->form->error = 'User does not exist';
  313.                     return;
  314.                 }
  315.             }
  316.    
  317.             $template->form->unsetData();
  318.         }
  319.     }  
  320.    
  321.     final public function help()
  322.     {
  323.         global $template, $_CONFIG;
  324.         $template->form->setData();
  325.        
  326.         if(isset($template->form->help))
  327.         {
  328.             $to = $_CONFIG['hotel']['email'];
  329.             $subject = "Help from RevCMS user - " . $this->getInfo($_SESSION['user']['id'], 'username');
  330.             $body = $template->form->question;
  331.                
  332.             if (mail($to, $subject, $body))
  333.             {
  334.                 $template->form->error = 'Message successfully sent! We will answer you shortly!';
  335.             }
  336.             else
  337.             {
  338.                  $template->form->error = 'Message delivery failed';
  339.             }
  340.         }
  341.     }
  342.  
  343.     /*-------------------------------Account settings-------------------------------------*/
  344.    
  345.     final public function updateAccount()
  346.     {
  347.         global $template, $_CONFIG, $core, $engine;
  348.        
  349.         if(isset($_POST['account']))
  350.         {  
  351.             if(!empty($_POST['acc_old_password']) && !empty($_POST['acc_new_password']))
  352.             {
  353.                 if($this->userValidation($this->getInfo($_SESSION['user']['id'], 'username'), $core->hashed($_POST['acc_old_password'])))
  354.                 {              
  355.                      if($_POST['acc_new_password'] != $_POST['acc_rep_password']) {
  356.                      $template->form->error = 'The passwords you typed do not match!';
  357.                         return;
  358.                     }                  
  359.                     if(strlen($_POST['acc_new_password']) >= 6)
  360.                     {
  361.                         $template->form->error = 'Your password has been successfully updated';
  362.                         $this->updateUser($_SESSION['user']['id'], 'password', $core->hashed($_POST['acc_new_password']));
  363.                         header('Location: '.$_CONFIG['hotel']['url'].'/logout');
  364.                         exit;
  365.                     }
  366.                     else
  367.                     {
  368.                         $template->form->error = 'The password you entered is too short';
  369.                         return;
  370.                     }
  371.                 }
  372.                 else
  373.                 {
  374.                     $template->form->error = 'The password you entered is incorrect';
  375.                     return;
  376.                 }
  377.             }
  378.         }      
  379.     }
  380.        
  381.        
  382.     final public function turnOn($k)
  383.     {  
  384.         $j = $this->getID($k);
  385.         $this->createSSO($j);
  386.         $_SESSION['user']['id'] = $j;  
  387.         $this->cacheUser($j);  
  388.         unset($j);
  389.     }
  390.    
  391.     /*-------------------------------Create SSO auth_ticket-------------------------------------*/
  392.    
  393.     final public function createSSO($k)
  394. {  
  395.   global $engine;
  396.   $sessionKey = 'RevCMS-' . rand(9, 9999999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  397.  
  398.   if($engine->num_rows("SELECT * FROM user_auth_ticket WHERE user_id = '" . $k . "' LIMIT 1") > 0) {
  399.    $engine->query("UPDATE user_auth_ticket SET auth_ticket = '" . $sessionKey . "' WHERE user_id = '" . $k . "'");
  400.   } else {
  401.    $engine->query("INSERT INTO user_auth_ticket (user_id, auth_ticket) VALUES ('" . $k . "', '" . $sessionKey ."')");
  402.   }
  403.   return $sessionKey;
  404.   unset($sessionKey);
  405. }      
  406.     /*-------------------------------Adding/Updating/Deleting users-------------------------------------*/
  407.    
  408.     final public function addUser($username, $password, $email, $motto, $credits, $pixels, $rank, $figure, $gender, $seckey)    
  409.     {      
  410.         global $engine;                                
  411.         $sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  412.         $engine->query("INSERT INTO users (username, password, mail, motto, credits, activity_points, rank, look, gender, seckey, ip_last, ip_reg, account_created, last_online, auth_ticket) VALUES('" . $username . "', '" . $password . "', '" . $email . "', '" . $motto . "', '" . $credits . "', '" . $pixels . "', '" . $rank . "', '" . $figure . "', '" . $gender . "', '" . $seckey . "', '" . $_SERVER['REMOTE_ADDR'] . "', '" . $_SERVER['REMOTE_ADDR'] . "', '" . time() . "', '" . time() . "', '" . $sessionKey . "')");    
  413.        
  414.  
  415.         unset($sessionKey);
  416.                      
  417.     }              
  418.          
  419.     final public function deleteUser($k)    
  420.     {      
  421.         global $engine;                
  422.         $engine->query("DELETE FROM users WHERE id = '" . $k . "' LIMIT 1");       
  423.         $engine->query("DELETE FROM items WHERE userid = '" . $k . "' LIMIT 1");       
  424.         $engine->query("DELETE FROM rooms WHERE ownerid = '" . $k . "' LIMIT 1");  
  425.     }  
  426.        
  427.     final public function updateUser($k, $key, $value)  
  428.     {      
  429.         global $engine;                
  430.         $engine->query("UPDATE users SET " . $key . " = '" . $engine->secure($value) . "' WHERE id = '" . $k . "' LIMIT 1");
  431.         $_SESSION['user'][$key] = $engine->secure($value);     
  432.     }
  433.    
  434.     /*-------------------------------Handling user information-------------------------------------*/    
  435.    
  436.     final public function cacheUser($k)
  437.     {
  438.         global $engine;            
  439.         $userInfo = $engine->fetch_assoc("SELECT username, rank, motto, mail, credits, activity_points, look, auth_ticket, ip_last FROM users WHERE id = '" . $k . "' LIMIT 1");
  440.        
  441.         foreach($userInfo as $key => $value)
  442.         {
  443.             $this->setInfo($key, $value);
  444.         }
  445.     }  
  446.    
  447.     final public function setInfo($key, $value)
  448.     {
  449.         global $engine;
  450.         $_SESSION['user'][$key] = $engine->secure($value);
  451.     }
  452.  
  453.     final public function getInfo($k, $key, $straight = false)
  454.     {
  455.         global $engine;
  456.         if($straight == true || !isset($_SESSION['user'][$key]))
  457.         {
  458.             $value = $engine->result("SELECT $key FROM users WHERE id = '" . $engine->secure($k) . "' LIMIT 1");
  459.             if($value != null)
  460.             {          
  461.                 $this->setInfo($key, $value);
  462.             }
  463.         }
  464.            
  465.         return $_SESSION['user'][$key];
  466.     }
  467.    
  468.    
  469.    
  470.     /*-------------------------------Get user ID or Username-------------------------------------*/
  471.    
  472.     final public function getID($k)    
  473.     {      
  474.         global $engine;        
  475.         return $engine->result("SELECT id FROM users WHERE username = '" . $engine->secure($k) . "' LIMIT 1");  
  476.     }      
  477.    
  478.     final public function getUsername($k)
  479.     {
  480.         global $engine;
  481.         return $this->getInfo($_SESSION['user']['id'], 'username');
  482.     }
  483.    
  484. }
  485. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement