Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.22 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_MSG_MAXLENGTH = 255;
  12.      
  13.      // Main user atributes
  14.      private $user_id;
  15.      private $nickname;
  16.      private $password;
  17.      // Profile attributes
  18.      private $language = DEFAULT_LANGUAGE;
  19.      private $trainer_icon = 1;
  20.      private $trainer_msg;
  21.      private $color;
  22.      private $timetable;
  23.      private $IP;
  24.      private $last_visit;
  25.      private $subscribed;
  26.      // User authority attribute
  27.      private $auth_level = 0;
  28.      
  29.      public function __construct($nickname, $password) {
  30.          global $_SERVER;
  31.          $this->nickname = $nickname;
  32.          $this->password = $password;
  33.          $this->IP = $_SERVER['REMOTE_ADDR'];
  34.          $this->last_visit = time();
  35.          $this->subscribed = time();
  36.      }
  37.      
  38.      public function SetLanguage($lang) {
  39.          global $UF;
  40.          if($this->VerifyUserLanguage($UF->SecureData($lang)))
  41.          {
  42.              $this->language = $UF->SecureData($lang);
  43.          }
  44.      }
  45.      
  46.      public function SetTrainerIcon($icon) {
  47.          $this->trainer_icon = $icon;
  48.      }
  49.      
  50.      public function GetNickname() {
  51.          return $this->nickname;
  52.      }
  53.      
  54.      public function GetPassword() {
  55.          return $this->password;
  56.      }
  57.      
  58.      public function GetLanguage() {
  59.          return $this->language;
  60.      }
  61.      
  62.      // Verify if the nickname is already taken
  63.      public function VerifyNickAvailability($nick) {
  64.          global $PA;
  65.          global $UF;
  66.          
  67.          // We look for any primary or secondary nickname identifal to the login
  68.          $query = $PA->prepare('SELECT '.TABLE_USERS_USERID.' FROM '.TABLE_USERS.' WHERE '.TABLE_USERS_NICKNAME.'=:nickname') or die($PA->error);
  69.          $query->execute(array('nickname'=>$UF->SecureData($nick)));
  70.          if($query->rowCount() != 0) // If it's taken as a primary nickname
  71.          {
  72.              $query = NULL;
  73.              return false;
  74.              break;
  75.          }
  76.          else
  77.          {
  78.              $query = NULL;
  79.              // We verify if it it's used as a secondary nickname
  80.              $query = $PA->prepare('SELECT '.TABLE_SECONDARIES_NICK.'.user_id FROM '.TABLE_SECONDARIES_NICK.' WHERE '.TABLE_SECONDARIES_NICK.'.user_nickname=:nickname') or die($PA->error);
  81.              $query->execute(array('nickname'=>$UF->SecureData($nick)));
  82.              if($query->rowCount() != 0) // If it's taken as a secondary nickname
  83.              {
  84.                  $query = NULL;
  85.                  return false;
  86.              }
  87.              else
  88.              {
  89.                  $query = NULL;
  90.                  return true;
  91.              }
  92.          }
  93.      }
  94.      
  95.      public function VerifyLogins($login, $password) { // Verify the format of the login/password
  96.          global $UF; // We need the utility Function
  97.          $errors = '';
  98.          
  99.          // We start by verifying the nickname
  100.          if(empty($login) || !preg_match("#^[^".$UF->EspapeRegexData('*+\\/"\'')."]{".self::NICKNAME_MINLENGTH.",".self::NICKNAME_MAXLENGTH."}$#", $login))
  101.          {
  102.              $errors .= 'The nickname is incorrect.<br /c';
  103.          }
  104.          // We verify the password
  105.          if(empty($password) || !preg_match("#^[^(\n)]{".self::PASSWORD_MINLENGTH.",".self::PASSWORD_MAXLENGTH."}$#", $password))
  106.          {
  107.              $errors .= 'The password is incorrect.<br />';
  108.          }
  109.          // Now if the $errors variable is not empty then there is an error
  110.          if($errors == '')
  111.          {
  112.              return true;
  113.          }
  114.          else
  115.          {
  116.              return false;
  117.          }
  118.      }
  119.      
  120.      public function AddUser($login, $password) { // Add a new user to the database
  121.          global $PA; // To access the PDO variable
  122.          global $UF;
  123.          
  124.          // First we need to verify the logins
  125.          if($this->VerifyLogins($login, $password) && $this->VerifyNickAvailability($login) && $this->VerifyUserIP($this->IP) && $this->VerifyUserLanguage($this->language, self::LANG_USABILITY))
  126.          {
  127.              // Now that we are sure about the validity of the logins, we can add the user to the dabase
  128.              $query = $PA->prepare('INSERT INTO '.TABLE_USERS.'('.TABLE_USERS_NICKNAME.', '.TABLE_USERS_PASSWORD.') VALUES(:nickname, :password)') or die($PA->error);
  129.              $query->execute(array('nickname'=>$UF->SecureData($this->nickname), 'password'=>md5($this->password)));
  130.              $query = NULL;
  131.              $this->user_id = $PA->lastInsertId(); // We recover the ID given to the user
  132.              // Now we need to create the rest of the tables that are in relation with the users table
  133.              $query = $PA->prepare('INSERT INTO '.TABLE_USERS_AUTH.'(user_id, user_auth_level) VALUES(:id, :auth)') or die($PA->error);
  134.              $query->execute(array('id'=>$this->user_id, 'auth'=>$this->auth_level));
  135.              $query = NULL;
  136.              // Now we add the user profile
  137.              $query = $PA->prepare('INSERT INTO '.TABLE_USERS_PROFILES.'(user_id, user_lang, user_color, user_ip, user_last_visit, user_subscribed) VALUES(:id, :language, :color, :ip, :timestamp, :timestamp2)') or die($PA->error);
  138.              $query->execute(array('id'=>$this->user_id, 'language'=>$this->language, 'color'=>$UF->RandomColor(), 'ip'=>$this->IP, 'timestamp'=>$this->last_visit, 'timestamp2'=>$this->subscribed));
  139.              $query = NULL;
  140.              // Now we add the user status
  141.              $query = $PA->prepare('INSERT INTO '.TABLE_USERS_STATUS.'(user_id, user_tempban) VALUES(:id, :tempban)') or die($PA->error);
  142.              $query->execute(array('id'=>$this->user_id, ':tempban'=>time()));
  143.              return true; // The user was successfully added
  144.          }
  145.          else
  146.          {
  147.              return false;
  148.          }
  149.      }
  150.      
  151.      /* Methodes concerning the user profile */
  152.      
  153.      // Verify if a language exists in the database and if it is usable depending on the $option parametre
  154.      public function VerifyUserLanguage($lang, $option) {
  155.          global $PA;
  156.          global $UF;
  157.          
  158.          if($option == self::LANG_EXISTS) // Will only verify if it exists
  159.          {
  160.              $query = $PA->prepare('SELECT lang_name FROM '.TABLE_LANGUAGES.' WHERE '.TABLE_LANGUAGES.'.lang_name=:langname') or die($PA->error);
  161.              $query->execute(array('langname'=>$UF->SecureData($lang)));
  162.              if($query->rowCount() == 0) // No existing language with the same name
  163.              {
  164.                  $query = NULL;
  165.                  return false;
  166.                  break;
  167.              }
  168.              else
  169.              {
  170.                  $query = NULL;
  171.                  return true;
  172.                  break;
  173.              }
  174.          }
  175.          elseif($option == self::LANG_USABILITY) // Will verify if we can use it(existence and status)
  176.          {
  177.              $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);
  178.              $query->execute(array('langname'=>$UF->SecureData($lang)));
  179.              if($query->rowCount() == 0) // No existing language with the same name/width 1 as status
  180.              {
  181.                  $query = NULL;
  182.                  return false;
  183.              }
  184.              else
  185.              {
  186.                  $query = NULL;
  187.                  return true;
  188.              }
  189.          }
  190.      }
  191.      
  192.      // Verify if a trainer icon exists
  193.      public function VerifyTrainerIcon($icon) {
  194.          global $PA;
  195.          if(preg_match("#^[0-9]{1,3}$#", $icon)) // We need to make sure it's an ID
  196.          {
  197.              // Now we look for the ID in the database
  198.              $query = $PA->prepare('SELECT trainer_id FROM '.TABLE_TRAINERS_ICONS.' WHERE '.TABLE_TRAINERS_ICONS.'.trainer_id=:icon') or die($PA->error);
  199.              $query->execute(array('icon'=>$icon));
  200.              if($query->rowCount() != 0) // Exists
  201.              {
  202.                  return true;
  203.              }
  204.              else
  205.              {
  206.                  return false;
  207.              }
  208.              $query = NULL;
  209.          }
  210.      }
  211.      
  212.      // Verify the trainer message
  213.      public function VerifyTrainerMsg($msg) {
  214.          if(mb_strlen($msg) <= self::TRAINER_MSG_MAXLENGTH)
  215.          {
  216.              return true;
  217.          }
  218.          else
  219.          {
  220.              return false;
  221.          }
  222.      }
  223.      
  224.      // Verify the syntaxt of a color
  225.      public function VerifyUserColor($color) {
  226.          if(preg_match("#^\#[A-F0-9]{6}$#i", $color))
  227.          {
  228.              return true;
  229.          }
  230.          else
  231.          {
  232.              return false;
  233.          }
  234.      }
  235.      
  236.      // Verify the syntaxt of the inputed timetable
  237.      public function VerifyUserTimetable($timetable) {
  238.          if($timetable >= -12 && $timetable <= +12)
  239.          {
  240.              return true;
  241.          }
  242.          else
  243.          {
  244.              return false;
  245.          }
  246.      }
  247.      
  248.      // Verify the validity of an IP adress
  249.      public function VerifyUserIP($ip) {
  250.          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))
  251.          {
  252.              return true;
  253.          }
  254.          else
  255.          {
  256.              return false;
  257.          }
  258.      }
  259. }
  260. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement