Advertisement
Guest User

RevCMS Captcha

a guest
Aug 2nd, 2014
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 27.21 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['REMOTE_ADDR'] . "'") == 1)
  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_seckey))
  137.                                                                         {
  138.                                                                                 if($this->validSecKey($template->form->reg_seckey))
  139.                                                                                 {
  140.                                                                                         //Continue
  141.                                                                                 }
  142.                                                                                 else
  143.                                                                                 {
  144.                                                                                         $template->form->error = 'Secret key must only have 4 numbers';
  145.                                                                                         return;
  146.                                                                                 }
  147.                                                                         }
  148.                                                                         if($this->isBanned($_SERVER['REMOTE_ADDR']) == false)
  149.                                                                         {
  150.                                                                                 if(!$this->hasClones($_SERVER['REMOTE_ADDR']))
  151.                                                                                 {
  152.                                                                                   if (@$_POST['captcha'] == $_SESSION['security_number'])
  153.                                             {
  154.                                                                                         if(!isset($template->form->reg_gender)) { $template->form->reg_gender = 'M'; }
  155.                                                                                         if(!isset($template->form->reg_figure)) { $template->form->reg_figure = $_CONFIG['hotel']['figure']; }
  156.                                                                                
  157.                                                                                         $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));
  158.                                                        
  159.                                                                                         $this->turnOn($template->form->reg_username);
  160.                                                                        
  161.                                                                                         header('Location: ' . $_CONFIG['hotel']['url'] . '/me');
  162.                                                                                         exit;
  163.                                                                                 }
  164.                                                                                 else
  165.                                                                                     {
  166.                                                                                      $template->form->error = 'Wrong captcha.';
  167.                                                                                     }
  168.                                                                                     else
  169.                                                                                 {
  170.                                                                                         $template->form->error = 'Sorry, but you cannot register twice';
  171.                                                                                 }
  172.                                                                         }
  173.                                                                         else
  174.                                                                         {
  175.                                                                                 $template->form->error = 'Sorry, it appears you are IP banned.<br />';
  176.                                                                                 $template->form->error .= 'Reason: ' . $this->getReason($_SERVER['REMOTE_ADDR']);
  177.                                                                                 return;
  178.                                                                         }
  179.                                                                 }
  180.                                                                 else  
  181.                                                                 {
  182.                                                                         $template->form->error = 'Password does not match repeated password';
  183.                                                                         return;
  184.                                                                 }
  185.  
  186.                                                         }
  187.                                                         else
  188.                                                         {
  189.                                                                 $template->form->error = 'Password must have more than 6 characters';
  190.                                                                 return;
  191.                                                         }
  192.                                                 }
  193.                                                 else
  194.                                                 {
  195.                                                         $template->form->error = 'Email: <b>' . $template->form->reg_email . '</b> is already registered';
  196.                                                         return;
  197.                                                 }
  198.                                         }
  199.                                         else
  200.                                         {
  201.                                                 $template->form->error = 'Email is not valid';
  202.                                                 return;
  203.                                         }
  204.                                 }
  205.                                 else
  206.                                 {
  207.                                         $template->form->error = 'Username is already registered';
  208.                                         return;
  209.                                 }
  210.                         }
  211.                         else
  212.                         {
  213.                                 $template->form->error = 'Username is invalid';
  214.                                 return;
  215.                         }
  216.                 }
  217.         }      
  218.        
  219.         final public function login()
  220.         {
  221.                 global $template, $_CONFIG, $core;
  222.                
  223.                 if(isset($_POST['login']))
  224.                 {
  225.                         $template->form->setData();
  226.                         unset($template->form->error);
  227.                        
  228.                         if($this->nameTaken($template->form->log_username))
  229.                         {
  230.                                 if($this->isBanned($template->form->log_username) == false || $this->isBanned($_SERVER['REMOTE_ADDR']) == false)
  231.                                 {
  232.                                         if($this->userValidation($template->form->log_username, $core->hashed($template->form->log_password)))
  233.                                         {
  234.                                                 $this->turnOn($template->form->log_username);
  235.                                                 $this->updateUser($_SESSION['user']['id'], 'ip_last', $_SERVER['REMOTE_ADDR']);
  236.                                                 $template->form->unsetData();
  237.                                                 header('Location: ' . $_CONFIG['hotel']['url'] . '/me');
  238.                                                 exit;
  239.                                         }
  240.                                         else
  241.                                         {
  242.                                                 $template->form->error = 'Details do not match';
  243.                                                 return;
  244.                                         }
  245.                                 }
  246.                                 else
  247.                                 {
  248.                                         $template->form->error = 'Sorry, it appears this user is banned<br />';
  249.                                         $template->form->error .= 'Reason: ' . $this->getReason($template->form->log_username);
  250.                                         return;
  251.                                 }
  252.                         }
  253.                         else
  254.                         {
  255.                                 $template->form->error = 'Username does not exist';
  256.                                 return;
  257.                         }
  258.                 }
  259.         }
  260.        
  261.         final public function loginHK()
  262.         {
  263.                 global $template, $_CONFIG, $core;
  264.                
  265.                 if(isset($_POST['login']))
  266.                 {      
  267.                         $template->form->setData();
  268.                         unset($template->form->error);
  269.                        
  270.                         if(isset($template->form->username) && isset($template->form->password))
  271.                         {
  272.                                 if($this->nameTaken($template->form->username))
  273.                                 {        
  274.                                         if($this->userValidation($template->form->username, $core->hashed($template->form->password)))
  275.                                         {
  276.                                                 if(($this->getInfo($_SESSION['user']['id'], 'rank')) >= 4)
  277.                                                 {
  278.                                                         $_SESSION["in_hk"] = true;
  279.                                                         header("Location:".$_CONFIG['hotel']['url']."/ase/dash");
  280.                                                         exit;
  281.                                                 }
  282.                                                 else
  283.                                                 {
  284.                                                         $template->form->error = 'Incorrect access level.';
  285.                                                         return;
  286.                                                 }
  287.                                         }
  288.                                         else
  289.                                         {
  290.                                                 $template->form->error = 'Incorrect password.';
  291.                                                 return;
  292.                                         }              
  293.                                 }
  294.                                 else
  295.                                 {
  296.                                         $template->form->error = 'User does not exist.';
  297.                                         return;
  298.                                 }
  299.                         }
  300.        
  301.                         $template->form->unsetData();
  302.                 }
  303.         }      
  304.        
  305.         final public function help()
  306.         {
  307.                 global $template, $_CONFIG;
  308.                 $template->form->setData();
  309.                
  310.                 if(isset($template->form->help))
  311.                 {
  312.                         $to = $_CONFIG['hotel']['email'];
  313.                         $subject = "Help from RevCMS user - " . $this->getInfo($_SESSION['user']['id'], 'username');
  314.                         $body = $template->form->question;
  315.                                
  316.                         if (mail($to, $subject, $body))
  317.                         {
  318.                                 $template->form->error = 'Message successfully sent! We will answer you shortly!';
  319.                         }
  320.                         else
  321.                         {
  322.                                  $template->form->error = 'Message delivery failed.';
  323.                         }
  324.                 }
  325.         }
  326.  
  327.         /*-------------------------------Account settings-------------------------------------*/
  328.        
  329.         final public function updateAccount()
  330.         {
  331.                 global $template, $_CONFIG, $core, $engine;
  332.                
  333.                 if(isset($_POST['account']))
  334.                 {
  335.                
  336.                         if(isset($_POST['acc_motto']) && strlen($_POST['acc_motto']) < 30 && $_POST['acc_motto'] != $this->getInfo($_SESSION['user']['id'], 'motto'))
  337.                         {
  338.                                 $this->updateUser($_SESSION['user']['id'], 'motto', $engine->secure($_POST['acc_motto']));
  339.                                 header('Location: '.$_CONFIG['hotel']['url'].'/account');
  340.                                 exit;
  341.                         }
  342.                         else
  343.                         {
  344.                                 $template->form->error = 'Motto is invalid.';
  345.                         }
  346.                        
  347.                         if(isset($_POST['acc_email']) && $_POST['acc_email'] != $this->getInfo($_SESSION['user']['id'], 'mail'))
  348.                         {
  349.                                 if($this->validEmail($_POST['acc_email']))
  350.                                 {
  351.                                         $this->updateUser($_SESSION['user']['id'], 'mail', $engine->secure($_POST['acc_email']));
  352.                                         header('Location: '.$_CONFIG['hotel']['url'].'/account');
  353.                                         exit;
  354.                                 }
  355.                                 else
  356.                                 {
  357.                                         $template->form->error = 'Email is not valid';
  358.                                         return;
  359.                                 }
  360.                         }
  361.                        
  362.                         if(!empty($_POST['acc_old_password']) && !empty($_POST['acc_new_password']))
  363.                         {
  364.                                 if($this->userValidation($this->getInfo($_SESSION['user']['id'], 'username'), $core->hashed($_POST['acc_old_password'])))
  365.                                 {
  366.                                         if(strlen($_POST['acc_new_password']) >= 8)
  367.                                         {
  368.                                                 $this->updateUser($_SESSION['user']['id'], 'password', $core->hashed($_POST['acc_new_password']));
  369.                                                 header('Location: '.$_CONFIG['hotel']['url'].'/me');
  370.                                                 exit;
  371.                                         }
  372.                                         else
  373.                                         {
  374.                                                 $template->form->error = 'New password is too short';
  375.                                                 return;
  376.                                         }
  377.                                 }
  378.                                 else
  379.                                 {
  380.                                         $template->form->error = 'Current password is wrong';
  381.                                         return;
  382.                                 }
  383.                         }
  384.                 }              
  385.         }
  386.                
  387.                
  388.         final public function turnOn($k)
  389.         {      
  390.                 $j = $this->getID($k);
  391.                 $this->createSSO($j);
  392.                 $_SESSION['user']['id'] = $j;  
  393.                 $this->cacheUser($j);  
  394.                 unset($j);
  395.         }
  396.        
  397.         /*-------------------------------Loggin forgotten-------------------------------------*/        
  398.        
  399.         final public function forgotten()
  400.         {
  401.                 global $template, $_CONFIG, $core;
  402.                
  403.                 if(isset($_POST['forgot']))
  404.                 {
  405.                
  406.                         $template->form->setData();
  407.                         unset($template->form->error);
  408.                        
  409.                         if($this->nameTaken($template->form->for_username))
  410.                         {
  411.                                 if(strlen($template->form->for_password) > 6)
  412.                                 {
  413.                                         if($this->getInfo($this->getID($template->form->for_username), 'seckey') == $core->hashed($template->form->for_key))
  414.                                         {
  415.                                                 $this->updateUser($this->getID($template->form->for_username), 'password', $core->hashed($template->form->for_password));
  416.                                                 $template->form->error = 'Account recovered! Go <b><a href="index">here</a></b> to login!';
  417.                                                 return;
  418.                                         }
  419.                                         else
  420.                                         {
  421.                                                 $template->form->error = 'Secret key is incorrect';
  422.                                                 return;
  423.                                         }
  424.                                 }
  425.                                 else
  426.                                 {
  427.                                         $template->form->error = 'Password must have more than 6 characters.';
  428.                                         return;
  429.                                 }
  430.                         }
  431.                         else
  432.                         {
  433.                                 $template->form->error = 'Username does not exist';
  434.                                 return;
  435.                         }
  436.                 }
  437.         }
  438.        
  439.         /*-------------------------------Create SSO auth_ticket-------------------------------------*/
  440.        
  441.         final public function createSSO($k)    
  442.         {              
  443.                 $sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  444.                
  445.                 $this->updateUser($k, 'auth_ticket', $sessionKey);
  446.                
  447.                 unset($sessionKey);
  448.         }        
  449.                
  450.         /*-------------------------------Adding/Updating/Deleting users-------------------------------------*/
  451.        
  452.         final public function addUser($username, $password, $email, $motto, $credits, $pixels, $rank, $figure, $gender, $seckey)        
  453.         {              
  454.                 global $engine;                                                                
  455.                 $sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33);
  456.                 $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 . "')");        
  457.                 unset($sessionKey);    
  458.                                          
  459.         }                              
  460.                  
  461.         final public function deleteUser($k)    
  462.         {              
  463.                 global $engine;                                
  464.                 $engine->query("DELETE FROM users WHERE id = '" . $k . "' LIMIT 1");          
  465.                 $engine->query("DELETE FROM items WHERE userid = '" . $k . "' LIMIT 1");              
  466.                 $engine->query("DELETE FROM rooms WHERE ownerid = '" . $k . "' LIMIT 1");      
  467.         }      
  468.                
  469.         final public function updateUser($k, $key, $value)      
  470.         {              
  471.                 global $engine;                                
  472.                 $engine->query("UPDATE users SET " . $key . " = '" . $engine->secure($value) . "' WHERE id = '" . $k . "' LIMIT 1");
  473.                 $_SESSION['user'][$key] = $engine->secure($value);            
  474.         }
  475.        
  476.         /*-------------------------------Handling user information-------------------------------------*/        
  477.        
  478.         final public function cacheUser($k)
  479.         {
  480.                 global $engine;                        
  481.                 $userInfo = $engine->fetch_assoc("SELECT username, rank, motto, mail, credits, activity_points, look, auth_ticket, ip_last FROM users WHERE id = '" . $k . "' LIMIT 1");
  482.                
  483.                 foreach($userInfo as $key => $value)
  484.                 {
  485.                         $this->setInfo($key, $value);
  486.                 }
  487.         }      
  488.        
  489.         final public function setInfo($key, $value)
  490.         {
  491.                 global $engine;
  492.                 $_SESSION['user'][$key] = $engine->secure($value);
  493.         }
  494.  
  495.         final public function getInfo($k, $key)
  496.         {
  497.                 global $engine;
  498.                 if(!isset($_SESSION['user'][$key]))
  499.                 {
  500.                         $value = $engine->result("SELECT $key FROM users WHERE id = '" . $engine->secure($k) . "' LIMIT 1");
  501.                         if($value != null)
  502.                         {                      
  503.                                 $this->setInfo($key, $value);
  504.                         }
  505.                 }
  506.                        
  507.                 return $_SESSION['user'][$key];
  508.         }
  509.        
  510.        
  511.        
  512.         /*-------------------------------Get user ID or Username-------------------------------------*/
  513.        
  514.         final public function getID($k)        
  515.         {              
  516.                 global $engine;                
  517.                 return $engine->result("SELECT id FROM users WHERE username = '" . $engine->secure($k) . "' LIMIT 1");  
  518.         }              
  519.        
  520.         final public function getUsername($k)
  521.         {
  522.                 global $engine;
  523.                 return $this->getInfo($_SESSION['user']['id'], 'username');
  524.         }
  525.        
  526. }
  527. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement