Advertisement
Guest User

Untitled

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