Advertisement
Guest User

class.user.php

a guest
Sep 25th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.03 KB | None | 0 0
  1. <?php
  2.     /* Copyright (C) 2011  Joopie
  3.  
  4.      * This program is free software: you can redistribute it and/or modify
  5.      * it under the terms of the GNU General Public License as published by
  6.      * the Free Software Foundation, either version 3 of the License, or
  7.      * (at your option) any later version.
  8.  
  9.      * This program is distributed in the hope that it will be useful,
  10.      * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.      * GNU General Public License for more details.
  13.  
  14.      * You should have received a copy of the GNU General Public License
  15.      * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.      */
  17.  
  18.     class User
  19.     {
  20.         private static $data;
  21.        
  22.         public static function LoggedIn()
  23.         {
  24.             return @$_SESSION['user']['loggedin'];
  25.         }
  26.        
  27.         public static function Login($login, $password)
  28.         {
  29.             $result = MySQL::Query('SELECT `id` FROM `users` WHERE (`username` = "'.$login.'" OR `email` = "'.$login.'") AND `password` = "'.$password.'" ORDER BY `last_alive` DESC LIMIT 1');
  30.             if ($result->num_rows >= 1)
  31.             {
  32.                 $id = $result->Result();
  33.                 $_SESSION['user']['loggedin'] = true;
  34.                 $_SESSION['user']['login'] = $login;
  35.                 $_SESSION['user']['password'] = $password;
  36.                 $_SESSION['user']['id'] = $id;
  37.                
  38.                 self::UpdateUserData($id);
  39.                 new MultiUser(true);
  40.                
  41.                 return true;   
  42.             }
  43.            
  44.             return false;
  45.         }
  46.        
  47.         public static function Logout()
  48.         {
  49.             unset($_SESSION['user']);
  50.             Core::CleanSessions();
  51.         }
  52.        
  53.         public static function UpdateSecure()
  54.         {
  55.             $_SESSION['user']['multi'][USER_ID]['last_update'] = time();
  56.         }
  57.        
  58.         public static function NeedCheckSecure()
  59.         {
  60.             return (@$_SESSION['user']['multi'][USER_ID]['last_update'] +450 <= time()) ? true : false;
  61.         }
  62.        
  63.         public static function UpdateUserData($id, $allowCache = false)
  64.         {
  65.             if ($allowCache && isset($_SESSION['user']['data']))
  66.             {
  67.                 self::$data = $_SESSION['user']['data'];
  68.                
  69.                 return;
  70.             }
  71.            
  72.             self::$data = $_SESSION['user']['data'] = MySQL::Query('SELECT * FROM `users` WHERE `id` = "'.$id.'" LIMIT 1')->NextRecord();
  73.            
  74.             $_SESSION['user']['login'] = self::$data['username'];
  75.         }
  76.        
  77.         public static function GetUserData($key, $id = 0)
  78.         {
  79.             if ($id == 0 || self::$data['id'] == $id)
  80.             {
  81.                 return self::$data[$key];
  82.             }
  83.            
  84.             return MySQL::Query('SELECT `'.$key.'` FROM `users` WHERE `id` = "'.$id.'" LIMIT 1')->Result();
  85.         }
  86.        
  87.         public static function SetUserData($key, $value, $id = 0, $onlyCache = false)
  88.         {
  89.             if ($id == 0 || self::$data['id'] == $id)
  90.             {
  91.                 $id = self::$data['id'];
  92.                 self::$data[$key] = $_SESSION['user']['data'][$key] = $value;              
  93.             }
  94.            
  95.             if ($onlyCache)
  96.             {
  97.                 return 1;
  98.             }
  99.            
  100.             return MySQL::Query('UPDATE `users` SET `'.$key.'` = "'.$value.'" WHERE `id` = "'.$id.'" LIMIT 1')->affected_rows;
  101.         }
  102.        
  103.         public static function SSOTicket($id = 0)
  104.         {
  105.             if($id == 0 || self::$data['id'] == $id)
  106.             {
  107.                 $id = self::$data['id'];
  108.             }
  109.            
  110.             $username = self::GetUserData('username', $id);
  111.             $userid = self::GetUserData('id', $id);
  112.            
  113.             $sso = 'BrickPHP-'.Core::Hash($username.rand(100, 999)).'-'.rand($userid, $userid + 100);
  114.            
  115.             self::SetUserData('sso_hash', $sso, $id);
  116.            
  117.             return $sso;
  118.         }
  119.        
  120.         // if is
  121.        
  122.         public static function IsAdmin($id = 0)
  123.         {
  124.             return (self::GetUserData('rank', $id) >= 2) ? true : false;
  125.         }
  126.        
  127.         //Habbo Club/VIP
  128.         public static function IsBasicOrVip($id = 0)
  129.         {
  130.             if ($id == 0 || self::$data['id'] == $id)
  131.             {
  132.                 $id = self::$data['id'];
  133.                
  134.                 if (isset($_SESSION['user']['multi'][$id]['ismembership']))
  135.                 {
  136.                     return $_SESSION['user']['multi'][$id]['ismembership'];
  137.                 }
  138.             }
  139.            
  140.             $_SESSION['user']['multi'][$id]['ismembership'] = $ismember = (MySQL::Query('SELECT null FROM `user_memberships` WHERE `user_id` = "'.$id.'" LIMIT 1')->num_rows >= 1) ? true : false;
  141.            
  142.             return $ismember;
  143.         }
  144.        
  145.         public static function GetMemberShip($id = 0)
  146.         {
  147.             if ($id == 0 || self::$data['id'] == $id)
  148.             {
  149.                 $id = self::$data['id'];
  150.                
  151.                 if (isset($_SESSION['user']['multi'][$id]['membership']))
  152.                 {
  153.                     return $_SESSION['user']['multi'][$id]['membership'];
  154.                 }
  155.             }
  156.            
  157.             $_SESSION['user']['multi'][$id]['membership'] = $membership = (MySQL::Query('SELECT `member_scaler` FROM `user_memberships` WHERE `user_id` = "'.$id.'" LIMIT 1')->Result() == 0) ? 'Habbo Club' : 'Habbo Vip';
  158.            
  159.             return $membership;
  160.         }
  161.        
  162.         public static function DaysLeft($id = 0)
  163.         {
  164.             if ($id == 0 || self::$data['id'] == $id)
  165.             {
  166.                 $id = self::$data['id'];
  167.                
  168.                 if (isset($_SESSION['user']['multi'][$id]['daysleft']))
  169.                 {                  
  170.                     return $_SESSION['user']['multi'][$id]['daysleft'];
  171.                 }
  172.             }
  173.            
  174.             $now = time();
  175.             $result = MySQL::Query('SELECT `activated_datetime`,`month_amount` FROM `user_memberships` WHERE `user_id` = "'.$id.'" LIMIT 1');
  176.            
  177.             if ($result->num_rows < 1)
  178.             {
  179.                 return 0;
  180.             }
  181.            
  182.             $start = Core::DateToTime($result->Result());
  183.             $ends = strtotime('+'.$result->Result(0, 1).' month', $start);
  184.            
  185.             $_SESSION['user']['multi'][$id]['daysleft'] = $days = ceil(($ends -$now) /60 /60 /24);
  186.            
  187.             return $days;
  188.         }
  189.        
  190.         public static function ClubClean()
  191.         {
  192.             $id = self::$data['id'];
  193.            
  194.             unset($_SESSION['user']['multi'][$id]['ismembership']);
  195.             unset($_SESSION['user']['multi'][$id]['membership']);
  196.             unset($_SESSION['user']['multi'][$id]['daysleft']);
  197.         }
  198.        
  199.         //Controle shit
  200.        
  201.         public static function ValidName($username)
  202.         {
  203.             if(preg_match('/^[a-zA-Z0-9._:,-]+$/i', $username) && !preg_match('/mod-/i', $username))
  204.             {
  205.                 return true;
  206.             }
  207.            
  208.             return false;
  209.         }
  210.        
  211.         public static function NameTaken($username)
  212.         {
  213.             return (MySQL::Query("SELECT null FROM `users` WHERE `username` = '".$username."'")->num_rows > 0) ? true : false;
  214.         }
  215.        
  216.         public static function ValidMail($mail)
  217.         {
  218.             return preg_match("/^[a-z0-9_\.-]+@([a-z0-9]+([\-]+[a-z0-9]+)*\.)+[a-z]{2,7}$/i", $mail);
  219.         }
  220.        
  221.         public static function MailTaken($mail)
  222.         {
  223.             return (MySQL::Query("SELECT * FROM `users` WHERE `email` = '".$mail."' LIMIT 1")->num_rows > 0) ? true : false;
  224.         }
  225.        
  226.         //Ban
  227.        
  228.         public static function AddBan($user_id, $user_ip, $ip_ban, $time, $reason)
  229.         {
  230.             return MySQL::Query('INSERT INTO `user_bans` (user_id, user_ip, ip_ban, given_datetime, end_datetime, given_mod_id, reason) VALUES ("'.$user_id.'", "'.$user_ip.'", "'.$ip_ban.'", "'.time().'", "'.time() +$time.'", "'.USER_ID.'", "'.$reason.'")')->affected_rows;
  231.         }
  232.        
  233.         public static function IsBanned($id = 0)
  234.         {
  235.             if ($id == 0)
  236.             {
  237.                 $id = self::$data['id'];
  238.             }
  239.            
  240.             return (MySQL::Query("SELECT null FROM `user_bans` WHERE (`user_id` = '".$id."' OR (`user_ip` = '".USER_IP."' AND `ip_ban` = '1')) AND `end_datetime` > '".time()."' LIMIT 1")->num_rows >= 1) ? true : false;       
  241.         }
  242.          
  243.         //Converter blabla
  244.        
  245.         public static function Name2Id($username)
  246.         {
  247.             return MySQL::Query('SELECT `id` FROM `users` WHERE `username` = "'.$username.'" LIMIT 1')->Result();
  248.         }
  249.        
  250.         public static function Id2Name($id)
  251.         {
  252.             return MySQL::Query('SELECT `username` FROM `users` WHERE `id` = "'.$id.'" LIMIT 1')->Result();
  253.         }
  254.        
  255.         //Register
  256.        
  257.        
  258.         public static function Register($username, $password, $email, $gender = 'm', $figure = 'hd-180-1.ch-210-66.lg-270-82.sh-290-91.hr-100', $no_activation = false)
  259.         {      
  260.             $activate = ($no_activation) ? 1 : 0;
  261.                 $time = date("d-m-Y");
  262.                 $time2 = date("d-m-Y");
  263.             MySQL::Query("INSERT INTO users (username, password, email, look, gender, user_hash, register_ip, activated_email, registered_datetime, last_alive) VALUES ('".$username."', '".$password."', '".$email."', '".$figure."', '".$gender."', '".md5($username)."', '".USER_IP."', '".$activate."', '".$time."', '".$time2."')");
  264.             return mysql_insert_id(MySQL::$link);
  265.         }
  266.     }
  267.    
  268.     class MultiUser
  269.     {      
  270.         public static $data;
  271.        
  272.         public static function IsValid($id = 0) //Safety control if it is right user
  273.         {
  274.             if (!User::LoggedIn())
  275.             {
  276.                 return false;
  277.             }
  278.            
  279.             foreach (self::$data as $value)
  280.             {
  281.                 if ($value['id'] == $id)
  282.                 {
  283.                     return true;
  284.                 }      
  285.             }
  286.            
  287.             return false;          
  288.         }
  289.        
  290.         public static function Togo()
  291.         {
  292.             return 50 -count(self::$data);
  293.         }
  294.        
  295.         public static function SwitchUser($id)
  296.         {
  297.             if (!self::IsValid($id))
  298.             {
  299.                 return false;              
  300.             }            
  301.            
  302.             User::UpdateUserData($id, false);
  303.            
  304.             return true;             
  305.         }
  306.        
  307.         function __Construct($allowCache = false)
  308.         {
  309.             if (!User::LoggedIn())
  310.             {
  311.                 return;
  312.             }
  313.            
  314.             if ($allowCache && isset($_SESSION['user']['multi']))
  315.             {              
  316.                 self::$data = $_SESSION['user']['multi'];
  317.                
  318.                 return;
  319.             }
  320.            
  321.             $result = MySQL::Query('SELECT `id`,`username`,`look`,`last_alive` FROM `users` WHERE `email` = "'.User::GetUserData('email').'"');
  322.  
  323.             while ($row = $result->NextRecord())
  324.             {
  325.                 self::$data[$row['id']] = $row;
  326.             }
  327.            
  328.             $_SESSION['user']['multi'] = self::$data;
  329.         }    
  330.     }
  331. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement