Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.28 KB | None | 0 0
  1. <?php
  2. /* This Class is the basic user model */
  3. class User {
  4.      // Constants
  5.      const NICKNAME_MAXLENGTH = 20;
  6.      const NICKNAME_MINLENGTH = 3;
  7.      const PASSWORD_MAXLENGTH = 30;
  8.      const PASSWORD_MINLENGTH = 5;
  9.      const LANG_USABILITY = 1;
  10.      const LANG_EXISTS = 0;
  11.      const TRAINER_DEFAULT_ICON = 1;
  12.      const TRAINER_MSG_MAXLENGTH = 255;
  13.      const USER_LEVEL = 0;
  14.      const MODERATOR_LEVEL = 1;
  15.      const ADMINISTRATOR_LEVEL = 2;
  16.      const OWNER_LEVEL = 3;
  17.      
  18.      // Main user atributes
  19.      private $user_id;
  20.      private $nickname;
  21.      private $password;
  22.      // Profile attributes
  23.      private $language = DEFAULT_LANGUAGE;
  24.      private $trainer_icon = self::TRAINER_DEFAULT_ICON;
  25.      private $trainer_msg;
  26.      private $color;
  27.      private $timezone;
  28.      private $IP;
  29.      private $last_visit;
  30.      private $subscribed;
  31.      private $template;
  32.      // User authority attribute
  33.      private $auth_level = self::USER_LEVEL;
  34.      
  35.      public function __construct($nickname = '', $password = '') {
  36.          global $_SERVER;
  37.          $this->nickname = $nickname;
  38.          $this->password = $password;
  39.          $this->IP = $_SERVER['REMOTE_ADDR'];
  40.          $this->last_visit = time();
  41.          $this->subscribed = time();
  42.          $this->template = $this->GetDefaultTemplate();
  43.      }
  44.      
  45.      public function SetNickname($nickname) {
  46.          return $this->nickname = $nickname;
  47.      }
  48.      
  49.      public function SetPassword($password) {
  50.          return $this->password = $password;
  51.      }
  52.      
  53.      public function SetLanguage($lang) {
  54.          global $UF;
  55.          if($this->VerifyUserLanguage($UF->SecureData($lang)))
  56.          {
  57.              $this->language = $UF->SecureData($lang);
  58.          }
  59.      }
  60.      
  61.      public function SetTrainerIcon($icon) {
  62.          $this->trainer_icon = $icon;
  63.      }
  64.      
  65.      public function GetNickname() {
  66.          return $this->nickname;
  67.      }
  68.      
  69.      public function GetPassword() {
  70.          return $this->password;
  71.      }
  72.      
  73.      public function GetLanguage() {
  74.          return $this->language;
  75.      }
  76.      
  77.      public function GetDefaultTemplate() {
  78.          global $PA;
  79.          $query = $PA->query('SELECT '.TABLE_TEMPLATES.'.template_id FROM '.TABLE_TEMPLATES.' WHERE '.TABLE_TEMPLATES.'.template_default=1') or die($PA->error);
  80.          $default = $query->fetch();
  81.          $query = NULL;
  82.          return $default['template_id'];
  83.      }
  84.      
  85.      // Verify if the nickname is already taken
  86.      public function VerifyNickAvailability($nick) {
  87.          global $PA;
  88.          global $UF;
  89.          
  90.          // We start by verifying the nickname's syntax
  91.          if(!$this->VerifyLogins($nick, '', 1, 0))
  92.          {
  93.              return false;
  94.              break;
  95.          }
  96.          
  97.          // We look for any primary or secondary nickname identifal to the login
  98.          $query = $PA->prepare('SELECT '.TABLE_USERS_USERID.' FROM '.TABLE_USERS.' WHERE '.TABLE_USERS_NICKNAME.'=:nickname') or die($PA->error);
  99.          $query->execute(array('nickname'=>$UF->SecureData($nick)));
  100.          if($query->rowCount() != 0) // If it's taken as a primary nickname
  101.          {
  102.              $query = NULL;
  103.              return false;
  104.              break;
  105.          }
  106.          else
  107.          {
  108.              $query = NULL;
  109.              // We verify if it it's used as a secondary nickname
  110.              $query = $PA->prepare('SELECT '.TABLE_SECONDARIES_NICK.'.user_id FROM '.TABLE_SECONDARIES_NICK.' WHERE '.TABLE_SECONDARIES_NICK.'.user_nickname=:nickname') or die($PA->error);
  111.              $query->execute(array('nickname'=>$UF->SecureData($nick)));
  112.              if($query->rowCount() != 0) // If it's taken as a secondary nickname
  113.              {
  114.                  $query = NULL;
  115.                  return false;
  116.              }
  117.              else
  118.              {
  119.                  $query = NULL;
  120.                  return true;
  121.              }
  122.          }
  123.      }
  124.      
  125.      public function VerifyLogins($login, $password, $VerifyLogin = 1, $VerifyPassword = 1) { // Verify the format of the login/password
  126.          global $UF; // We need the utility Function
  127.          $errors = '';
  128.          
  129.          // We start by verifying the nickname
  130.          if(($VerifyLogin == 1) && (empty($login) || !preg_match("#^[^".$UF->EspapeRegexData('*+\\/"\'')."]{".self::NICKNAME_MINLENGTH.",".self::NICKNAME_MAXLENGTH."}$#", $login)))
  131.          {
  132.              $errors .= 'The nickname is incorrect.<br />';
  133.          }
  134.          // We verify the password
  135.          if(($VerifyPassword == 1) && (empty($password) || !preg_match("#^[^(\n)]{".self::PASSWORD_MINLENGTH.",".self::PASSWORD_MAXLENGTH."}$#", $password)))
  136.          {
  137.              $errors .= 'The password is incorrect.<br />';
  138.          }
  139.          // Now if the $errors variable is not empty then there is an error
  140.          if($errors == '')
  141.          {
  142.              return true;
  143.          }
  144.          else
  145.          {
  146.              return false;
  147.          }
  148.      }
  149.      
  150.      public function AddUser($login, $password) { // Add a new user to the database
  151.          global $PA; // To access the PDO variable
  152.          global $UF;
  153.          
  154.          // First we need to verify the logins
  155.          if($this->VerifyLogins($login, $password) && $this->VerifyNickAvailability($login) && $this->VerifyUserIP($this->IP) && $this->VerifyUserLanguage($this->language, self::LANG_USABILITY))
  156.          {
  157.              // Now that we are sure about the validity of the logins, we can add the user to the dabase
  158.              $query = $PA->prepare('INSERT INTO '.TABLE_USERS.'('.TABLE_USERS_NICKNAME.', '.TABLE_USERS_PASSWORD.') VALUES(:nickname, :password)') or die($PA->error);
  159.              $query->execute(array('nickname'=>$UF->SecureData($this->nickname), 'password'=>md5($this->password)));
  160.              $query = NULL;
  161.              $this->user_id = $PA->lastInsertId(); // We recover the ID given to the user
  162.              // Now we need to create the rest of the tables that are in relation with the users table
  163.              $query = $PA->prepare('INSERT INTO '.TABLE_USERS_AUTH.'(user_id, user_auth_level) VALUES(:id, :auth)') or die($PA->error);
  164.              $query->execute(array('id'=>$this->user_id, 'auth'=>$this->auth_level));
  165.              $query = NULL;
  166.              // Now we add the user profile
  167.              $query = $PA->prepare('INSERT INTO '.TABLE_USERS_PROFILES.'(user_id, user_lang, user_color, user_ip, user_last_visit, user_subscribed, user_template) VALUES(:id, :language, :color, :ip, :timestamp, :timestamp2, :template)') or die($PA->error);
  168.              $query->execute(array('id'=>$this->user_id, 'language'=>$this->language, 'color'=>$UF->RandomColor(), 'ip'=>$this->IP, 'timestamp'=>$this->last_visit, 'timestamp2'=>$this->subscribed, 'template'=>$this->template));
  169.              $query = NULL;
  170.              // Now we add the user status
  171.              $query = $PA->prepare('INSERT INTO '.TABLE_USERS_STATUS.'(user_id, user_tempban) VALUES(:id, :tempban)') or die($PA->error);
  172.              $query->execute(array('id'=>$this->user_id, ':tempban'=>time()));
  173.              return true; // The user was successfully added
  174.          }
  175.          else
  176.          {
  177.              return false;
  178.          }
  179.      }
  180.      
  181.      /* Methodes concerning the user profile */
  182.      
  183.      // Verify if a language exists in the database and if it is usable depending on the $option parametre
  184.      public function VerifyUserLanguage($lang, $option) {
  185.          global $PA;
  186.          global $UF;
  187.          
  188.          if(preg_match("#^[a-zA-Z0-9_]$#", $lang))
  189.          {
  190.              if($option == self::LANG_EXISTS) // Will only verify if it exists
  191.              {
  192.                  $query = $PA->prepare('SELECT lang_name FROM '.TABLE_LANGUAGES.' WHERE '.TABLE_LANGUAGES.'.lang_name=:langname') or die($PA->error);
  193.                  $query->execute(array('langname'=>$UF->SecureData($lang)));
  194.                  if($query->rowCount() == 0) // No existing language with the same name
  195.                  {
  196.                      $query = NULL;
  197.                      return false;
  198.                      break;
  199.                  }
  200.                  else
  201.                  {
  202.                      $query = NULL;
  203.                      return true;
  204.                      break;
  205.                  }
  206.              }
  207.              elseif($option == self::LANG_USABILITY) // Will verify if we can use it(existence and status)
  208.              {
  209.                  $query = $PA->prepare('SELECT lang_name FROM '.TABLE_LANGUAGES.' WHERE '.TABLE_LANGUAGES.'.lang_name=:langname AND '.TABLE_LANGUAGES.'.lang_status=1') or die($PA->error);
  210.                  $query->execute(array('langname'=>$UF->SecureData($lang)));
  211.                  if($query->rowCount() == 0) // No existing language with the same name/width 1 as status
  212.                  {
  213.                      $query = NULL;
  214.                      return false;
  215.                  }
  216.                  else
  217.                  {
  218.                      $query = NULL;
  219.                      return true;
  220.                  }
  221.              }
  222.          }
  223.      }
  224.      
  225.      // Verify if a trainer icon exists
  226.      public function VerifyTrainerIcon($icon) {
  227.          global $PA;
  228.          
  229.          if(preg_match("#^[0-9]{1,3}$#", $icon)) // We need to make sure it's an ID
  230.          {
  231.              // Now we look for the ID in the database
  232.              $query = $PA->prepare('SELECT trainer_id FROM '.TABLE_TRAINERS_ICONS.' WHERE '.TABLE_TRAINERS_ICONS.'.trainer_id=:icon') or die($PA->error);
  233.              $query->execute(array('icon'=>$icon));
  234.              if($query->rowCount() != 0) // Exists
  235.              {
  236.                  return true;
  237.              }
  238.              else
  239.              {
  240.                  return false;
  241.              }
  242.              $query = NULL;
  243.          }
  244.      }
  245.      
  246.      // Verify the trainer message
  247.      public function VerifyTrainerMsg($msg) {
  248.          if(mb_strlen($msg) <= self::TRAINER_MSG_MAXLENGTH)
  249.          {
  250.              return true;
  251.          }
  252.          else
  253.          {
  254.              return false;
  255.          }
  256.      }
  257.      
  258.      // Verify the syntaxt of a color
  259.      public function VerifyUserColor($color) {
  260.          if(preg_match("#^\#[A-F0-9]{6}$#i", $color))
  261.          {
  262.              return true;
  263.          }
  264.          else
  265.          {
  266.              return false;
  267.          }
  268.      }
  269.      
  270.      // Verify the syntaxt of the inputed timezone
  271.      public function VerifyUserTimezone($timezone) {
  272.          if($timezone >= -12 && $timezone <= +12)
  273.          {
  274.              return true;
  275.          }
  276.          else
  277.          {
  278.              return false;
  279.          }
  280.      }
  281.      
  282.      // Verify the validity of an IP adress
  283.      public function VerifyUserIP($ip) {
  284.          if(preg_match("#\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b#", $ip))
  285.          {
  286.              return true;
  287.          }
  288.          else
  289.          {
  290.              return false;
  291.          }
  292.      }
  293.      
  294.      // Verify the syntaxt of the timestamp
  295.      public function VerifyUserTimestamp($timestamp) {
  296.          if(preg_match("#[0-9]{10}#", $timestamp))
  297.          {
  298.              return true;
  299.          }
  300.          else
  301.          {
  302.              return false;
  303.          }
  304.      }
  305.      
  306.      // Return an array containing all the informations about the user
  307.      public function GetUserInformations($username) {
  308.          global $PA;
  309.          global $UF;
  310.          
  311.          $query = $PA->prepare('SELECT '.TABLE_USERS_USERID.', '.TABLE_USERS_NICKNAME.', '.TABLE_USERS_PASSWORD.',
  312.          '.TABLE_USERS_AUTH.'.user_auth_level, '.TABLE_USERS_PROFILES.'.user_lang, '.TABLE_USERS_PROFILES.'.user_trainer_icon,
  313.          '.TABLE_USERS_PROFILES.'.user_trainer_msg, '.TABLE_USERS_PROFILES.'.user_color, '.TABLE_USERS_PROFILES.'.user_timezone,
  314.          '.TABLE_USERS_PROFILES.'.user_ip, '.TABLE_USERS_PROFILES.'.user_last_visit, '.TABLE_USERS_PROFILES.'.user_subscribed,
  315.          '.TABLE_USERS_PROFILES.'.user_template FROM '.TABLE_USERS.' LEFT JOIN '.TABLE_USERS_AUTH.' ON '.TABLE_USERS_USERID.'='.TABLE_USERS_AUTH.'.user_id
  316.          LEFT JOIN '.TABLE_USERS_PROFILES.' ON '.TABLE_USERS_USERID.'='.TABLE_USERS_PROFILES.'.user_id WHERE '.TABLE_USERS_NICKNAME.'=:username') or die($PA->error);
  317.          $query->execute(array('username'=>$UF->SecureData($username)));
  318.          if($query->rowCount() == 1)
  319.          {
  320.              $infos = $query->fetch();
  321.              $query = NULL;
  322.              return $infos;
  323.          }
  324.          else
  325.          {
  326.              $query = NULL;
  327.              return 0;
  328.          }
  329.      }
  330.      
  331.      // Load User Default Language
  332.      
  333.      public function LoadUserLanguage() {
  334.          global $_COOKIE;
  335.          global $UF;
  336.          
  337.          if(!empty($_COOKIE['language']) && $this->VerifyUserLanguage($_COOKIE['language'], self::LANG_USABILITY)) // If there is a cookie containing the name of the user language and if it's usable
  338.          {
  339.              require(SERVER_ROOT.'/langs/'.strtolower($_COOKIE['language']).".php");
  340.          }
  341.          else // If not, we use the default language
  342.          {
  343.              require(SERVER_ROOT.'/langs/'.strtolower(DEFAULT_LANGUAGE).".php");
  344.          }
  345.      }
  346.      
  347.      // Verify if someone is connected
  348.      public function VerifyUserIsConnected() {
  349.          global $_SESSION;
  350.          
  351.          if(!empty($_SESSION['id']) && !empty($_SESSION['username']) && !empty($_SESSION['auth_level']))
  352.          {
  353.              return true;
  354.          }
  355.          else
  356.          {
  357.              return false;
  358.          }
  359.      }
  360.      
  361.      // Connect the user to the server
  362.      public function ConnectUser($username, $password, $lang = DEFAULT_LANGUAGE, $save_login = 'off') {
  363.          global $_COOKIE;
  364.          
  365.          if(!$this->VerifyUserIsConnected() && $this->VerifyLogins($username, $password)) // If the logins have a correct format and he isn't connected
  366.          {
  367.              $UserInfos = $this->GetUserInformations($username);
  368.              if($UserInfos[ROW_USERS_PASSWORD] == md5($password))
  369.              {
  370.                  // We identify the user
  371.                  $_SESSION['id'] == $UserInfos[ROW_USERS_USERID];
  372.                  $_SESSION['username'] == $UserInfos[ROW_USERS_NICKNAME];
  373.                  $_SESSION['auh_level'] == $UserInfos['user_auth_level'];
  374.                  setcookie('language', $UF->SecureData($lang), time()+60*60*24*30*12);
  375.                  if($save_login == 'on')
  376.                  {
  377.                      setcookie('nickname', $_SESSION['username'], time()+60*60*24*30*12);
  378.                      setcookie('password', md5($UserInfos['user_password']), time()+60*60*24*30*12);
  379.                  }
  380.              }       
  381.          }
  382.          elseif(!$this->VerifyUserIsConnected() && $this->VerifyLogins($_COOKIE['nickname'], $_COOKIE['password']))
  383.          {
  384.              $UserInfos = $this->GetUserInformations($_COOKIE['nickname']);
  385.              if($UserInfos[ROW_USERS_PASSWORD] == md5($_COOKIE['password']))
  386.              {
  387.                  // We identify the user
  388.                  $_SESSION['id'] == $UserInfos[ROW_USERS_USERID];
  389.                  $_SESSION['username'] == $UserInfos[ROW_USERS_NICKNAME];
  390.                  $_SESSION['auh_level'] == $UserInfos['user_auth_level'];
  391.                  setcookie('language', $UF->SecureData($lang), time()+60*60*24*30*12);
  392.                  if($save_login == 'on')
  393.                  {
  394.                      setcookie('nickname', $_SESSION['username'], time()+60*60*24*30*12);
  395.                      setcookie('password', md5($UserInfos['user_password']), time()+60*60*24*30*12);
  396.                  }
  397.              }
  398.          }
  399.          else
  400.          {
  401.              return false;
  402.          }
  403.      }
  404.      
  405.      public function UpdateUserInformations($ArrayInfos) {
  406.          global $PA;
  407.          $NewUserInfos = array();
  408.          
  409.          // First of all, we need to verify the nickname syntax, if it's not correct we can't continue our verification
  410.          if(!$this->VerifyLogins($ArrayInfos[ROWS_USERS_NICKNAME], '', 1, 0))
  411.          {
  412.              return false;
  413.              break;
  414.          }
  415.          // We extract the actual user informations
  416.          $UserInfos = $this->GetUserInformations($ArrayInfos[ROW_USERS_NICKNAME]);
  417.          foreach($UserInfos as $key=>$info) // We will create an array with the updated informations
  418.          {
  419.              if(!isset($ArrayInfos[$key]))
  420.              {
  421.                  $NewUserInfos[$key] = $UserInfos[$key];
  422.              }
  423.              elseif($key == ROW_USERS_USERID)
  424.              {
  425.                  $NewUserInfos[ROW_USERS_USERID] = $UserInfos[ROW_USERS_USERID];
  426.              }
  427.              elseif($key == ROW_USERS_NICKNAME && !$this->VerifyNickAvailability($ArrayInfos[ROW_USERS_NICKNAME]))
  428.              {
  429.                  $NewUserInfos[ROW_USERS_NICKNAME] = $UserInfos[ROW_USERS_NICKNAME];
  430.              }
  431.              elseif($key == ROW_USERS_PASSWORD && !$this->VerifyLogins('', $ArrayInfos[ROW_USERS_PASSWORD], 0, 1))
  432.              {
  433.                  $NewUserInfos[ROW_USERS_PASSWORD] = $UserInfos[ROW_USERS_PASSWORD];
  434.              }
  435.              elseif($key == ROW_USERS_PASSWORD && $this->VerifyLogins('', $ArrayInfos[ROW_USERS_PASSWORD], 0, 1))
  436.              {
  437.                  $NewUserInfos[ROW_USERS_PASSWORD] = md5($ArrayInfos[ROW_USERS_PASSWORD]);
  438.              }
  439.              elseif($key == 'user_lang' && !$this->VerifyUserLanguage($ArrayInfos['user_lang'], self::LANG_USABILITY))
  440.              {
  441.                  $NewUserInfos['user_lang'] = $UserInfos['user_lang'];
  442.              }
  443.              elseif($key == 'user_trainer_icon' && !$this->VerifyTrainerIcon($ArrayInfos['user_trainer_icon']))
  444.              {
  445.                  $NewUserInfos['user_trainer_icon'] = $UserInfos['user_trainer_icon'];
  446.              }
  447.              elseif($key == 'user_trainer_msg' && !$this->VerifyTrainerMsg($ArrayInfos['user_trainer_msg']))
  448.              {
  449.                  $NewUserInfos['user_trainer_msg'] = $UserInfos['user_trainer_msg'];
  450.              }
  451.              elseif($key == 'user_color' && !$this->VerifyUserColor($ArrayInfos['user_color']))
  452.              {
  453.                  $NewUserInfos['user_color'] = $UserInfos['user_color'];
  454.              }
  455.              elseif($key == 'user_timezone' && !$this->VerifyUserTimezone($ArrayInfos['user_timezone']))
  456.              {
  457.                  $NewUserInfos['user_timezone'] = $UserInfos['user_timezone'];
  458.              }
  459.              elseif($key = 'user_ip' && !$this->VerifyUserIP($ArrayInfos['user_ip']))
  460.              {
  461.                  $NewUserInfos['user_ip'] = $UserInfos['user_ip'];
  462.              }
  463.              elseif($key == 'user_last_visit' && !$this->VerifyUserTimestamp($ArrayInfos['user_last_visit']))
  464.              {
  465.                  $NewUserInfos['user_last_visit'] = $UserInfos['user_last_visit'];
  466.              }
  467.              elseif($key == 'user_subscribed' && !$this->VerifyUserTimestamp($ArrayInfos['user_subscribed']))
  468.              {
  469.                  $NewUserInfos['user_subscribed'] = $UserInfos['user_subscribed'];
  470.              }
  471.              elseif($key == 'user_ban' && !in_array($ArrayInfos['user_auth_level'], array(0, 1)))
  472.              {
  473.                  $NewUserInfos['user_ban'] = $UserInfos['user_ban'];
  474.              }
  475.              elseif($key == 'user_tempban' && !$this->VerifyUserTimestamp($ArrayInfos['user_tempban']))
  476.              {
  477.                  $NewUserInfos['user_tempban'] = $UserInfos['user_tempban'];
  478.              }
  479.              elseif($key == 'user_auth_level' && !in_array($ArrayInfos['user_auth_level'], array(self::USER_LEVEL, self::MODERATOR_LEVEL, self::ADMINISTRATOR_LEVEL, self::OWNER_LEVEL)))
  480.              {
  481.                  $NewUserInfos['user_auth_level'] = $UserInfos['user_auth_level'];
  482.              }
  483.              else
  484.              {
  485.                  $NewUserInfos[$key] = $ArrayInfos[$key];
  486.              }
  487.          }
  488.          // Now that the array has been created, we update the database
  489.          $query = $PA->prepare('UPDATE') or die($PA->error);
  490.          $query->execute($NewUserInfos);
  491.          if($query->rowCount != 0)
  492.          {
  493.              $query = NULL;
  494.              return true;
  495.          }
  496.          else
  497.          {
  498.              $query = NULL;
  499.              return false;
  500.          }
  501.      }
  502. }
  503. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement