Advertisement
Guest User

Untitled

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