Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.11 KB | None | 0 0
  1. <?php
  2. class user {
  3.     private static $conf;
  4.     public static $_i;
  5.     private function __construct() {
  6.         self::$conf = conf('user');
  7.     }
  8.     public function __clone() { }
  9.     public static function instance() {
  10.         if(self::$_i instanceof self) {
  11.             return self::$_i;
  12.         } else {
  13.             self::$_i = new self;
  14.             return self::$_i;
  15.         }
  16.     }
  17.     public function isSignedIn() {
  18.         $s = $_SESSION['getGuestlist_User'];
  19.         if(isset($s['id']) && isset($s['code'])) {
  20.             $q = c('database')->select("SELECT * FROM `user_account` WHERE id = ".addslashes(trim($s['id']))." AND code = '".addslashes(trim($s['code']))."' AND ip = '".$_SERVER['REMOTE_ADDR']."'");
  21.             if($q->error() === false && $q->rowCount() === 1) {
  22.                 return true;
  23.             }
  24.         }
  25.         return false;
  26.     }
  27.     public function isRegistered($v, $t) {
  28.         if(!empty($v) && !empty($t)) {
  29.             switch($t) {
  30.                 case 'email': $f = 'email'; break;
  31.                 case 'username': $f = 'username'; break;
  32.                 case 'mobile': $f = 'mobile'; break;
  33.             }
  34.             if(isset($f)) {
  35.                 $q = c('database')->select("SELECT id FROM `user_account` WHERE ".$f." = '".addslashes(trim($v))."'");
  36.                 if($q->error() === false && $q->rowCount() === 1) {
  37.                     return true;
  38.                 }
  39.             }
  40.         }
  41.         return false;
  42.     }
  43.     public function isUser($user) {
  44.         if(!empty($user)) {
  45.             $user = addslashes(trim($user));
  46.             $q = c('database')->select("SELECT id FROM `user_account` WHERE (id = '".$user."' OR username = '".$user."')");
  47.             if($q->error() === false && $q->rowCount() === 1) {
  48.                 return true;
  49.             }
  50.         }
  51.         return false;
  52.     }
  53.     public function isEmail($email) {
  54.         if(!empty($email)) {
  55.             $user = addslashes(trim($email));
  56.             $q = c('database')->select("SELECT id FROM `user_account` WHERE email = '".$email."'");
  57.             if($q->error() === false && $q->rowCount() === 1) {
  58.                 return true;
  59.             }
  60.         }
  61.         return false;
  62.     }
  63.     public function isPromoter($user) {
  64.         if(!empty($user)) {
  65.             $user = addslashes(trim($user));
  66.             $q = c('database')->select("SELECT id FROM `user_account` WHERE (id = '".$user."' OR username = '".$user."') AND type = '1'");
  67.             if($q->error() === false && $q->rowCount() === 1) {
  68.                 return true;
  69.             }
  70.         }
  71.         return false;
  72.     }
  73.     public function isStaff($user) {
  74.         if(!empty($user)) {
  75.             $user = addslashes(trim($user));
  76.             $q = c('database')->select("SELECT id FROM `user_account` WHERE (id = '".$user."' OR username = '".$user."') AND type = '3'");
  77.             if($q->error() === false && $q->rowCount() === 1) {
  78.                 return true;
  79.             }
  80.         }
  81.         return false;
  82.     }
  83.     public function isAdmin($user) {
  84.         if(!empty($user)) {
  85.             $user = addslashes(trim($user));
  86.             $q = c('database')->select("SELECT id FROM `user_account` WHERE (id = '".$user."' OR username = '".$user."') AND type = '4'");
  87.             if($q->error() === false && $q->rowCount() === 1) {
  88.                 return true;
  89.             }
  90.         }
  91.         return false;
  92.     }
  93.     public function signIn($username, $password) {
  94.         if(!empty($username) && !empty($password)) {
  95.             $username = addslashes(trim($username));
  96.             $password = addslashes(trim($password));
  97.             $q = c('database')->select("SELECT id FROM `user_account` WHERE username = '".$username."' AND password = '".$password."'");
  98.             if($q->error() === false && $q->rowCount() === 1) {
  99.                 $user = $q->row('assoc');
  100.                 if($user !== false) {
  101.                     $user['code'] = $this->generateUserCode();
  102.                     if($user['code'] !== false) {
  103.                         if($this->update('code', $user['code'], $user['id']) === true) {
  104.                             $this->update('ip', $_SERVER['REMOTE_ADDR'], $user['id']);
  105.                             $_SESSION['getGuestlist_User'] = array('id' => $user['id'], 'code' => $user['code']);
  106.                             return $user['id'];
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.         return false;
  113.     }
  114.     public function signOut() {
  115.         if($this->isSignedIn() === true) {
  116.             $s = $_SESSION['getGuestlist_User'];
  117.             $q = c('database')->update("UPDATE `user_account` SET code = '', ip = '' WHERE id = ".addslashes(trim($s['id'])));
  118.             if($q->error() === false && $q->rowCount() === 1) {
  119.                 $_SESSION['getGuestlist_User'] = NULL;
  120.                 session_destroy();
  121.                 return true;
  122.             }
  123.         }
  124.         return false;
  125.     }
  126.     public function register($username, $email, $password) {
  127.         if(!empty($username) && !empty($email) && !empty($password)) {
  128.             $q = c('database')->insert("INSERT INTO `user_account` (username, password, email, registered, status) VALUES (?, ?, ?, ?, ?);", array($username, $password, $email, time(), '0'));
  129.             if($q->error() === false && $q->rowCount() === 1) {
  130.                 return $q->insertId();
  131.             }
  132.         }
  133.         return false;
  134.     }
  135.     public function passwd($password) { return crypt($password, self::$conf['salt']); }
  136.     public function generateUserCode() { return System::randomString(25); }
  137.     public function update($field, $value, $user) {
  138.         if(!empty($field) && $value !== '' && !is_null($value) && !empty($user)) {
  139.             if($this->isUser($user) === true) {
  140.                 $field = addslashes(trim($field));
  141.                 $value = addslashes(trim($value));
  142.                 $user = addslashes(trim($user));
  143.                 $q = c('database')->select("SHOW COLUMNS IN `user_account`");
  144.                 if($q->error() === false && $q->rowCount() > 0) {
  145.                     $fields = $q->rows('assoc');
  146.                     for($i=0;$i<count($fields);$i++) {
  147.                         if($fields[$i]['Field'] === $field) {
  148.                             $field = array('name' => $fields[$i]['Field'], 'type' => $fields[$i]['Type']);
  149.                             break;
  150.                         }
  151.                     }
  152.                 }
  153.                 if(is_array($field) && count($field) > 0) {
  154.                     if(preg_match("/varchar\(([0-9]*)\)/", $field['type'], $matches) !== 0) {
  155.                         if(strlen($value) <= $matches[1]) {
  156.                             $q = c('database')->update("UPDATE `user_account` SET ".$field['name']." = '".$value."' WHERE id = '".$user."'");
  157.                             if($q->error() === false && $q->rowCount() === 1) {
  158.                                 return true;
  159.                             }
  160.                         }
  161.                     } elseif(preg_match("/^(bigint|int|tinyint)\(([0-9]*)\)$/", $field['type'], $matches) !== 0) {
  162.                         if(is_int($value)) {
  163.                             $q = c('database')->update("UPDATE `user_account` SET ".$field['name']." = '".$value."' WHERE id = '".$user."'");
  164.                             if($q->error() === false && $q->rowCount() === 1) {
  165.                                 return true;
  166.                             }
  167.                         }
  168.                     } elseif(preg_match("/^float\(([0-9\,]*)\)$/", $field['type'], $matches) !== 0) {
  169.                         $dec = explode(',', $matches[1]);
  170.                         if(is_float($value)) {
  171.                             $q = c('database')->update("UPDATE `user_account` SET ".$field['name']." = '".$value."' WHERE id = '".$user."'");
  172.                             if($q->error() === false && $q->rowCount() === 1) {
  173.                                 return true;
  174.                             }
  175.                         }
  176.                     } elseif(preg_match("/^(text|longtext)$/", $field['type']) !== 0) {
  177.                         $value = htmlentities($value);
  178.                         $q = c('database')->update("UPDATE `user_account` SET ".$field['name']." = '".$value."' WHERE id = '".$user."'");
  179.                         if($q->error() === false && $q->rowCount() === 1) {
  180.                             return true;
  181.                         }
  182.                     } elseif(preg_match("/^(enum|set)\(([0-9a-zA-Z\,\']*)\)$/", $field['type'], $matches) !== 0) {
  183.                         $opts = explode(',', str_replace("'", '', $matches[2]));
  184.                         if(in_array($value, $opts)) {
  185.                             $q = c('database')->update("UPDATE `user_account` SET ".$field['name']." = '".$value."' WHERE id = '".$user."'");
  186.                             if($q->error() === false && $q->rowCount() === 1) {
  187.                                 return true;
  188.                             }
  189.                         }
  190.                     }
  191.                 }
  192.             }
  193.         }
  194.         return false;
  195.     }
  196.     public function updatePromoter($field, $value, $user) {
  197.         if(!empty($field) && $value !== '' && !is_null($value) && !empty($user)) {
  198.             if($this->isUser($user) === true) {
  199.                 $field = addslashes(trim($field));
  200.                 $value = addslashes(trim($value));
  201.                 $user = addslashes(trim($user));
  202.                 $q = c('database')->select("SHOW COLUMNS IN `user_promoter_information`");
  203.                 if($q->error() === false && $q->rowCount() > 0) {
  204.                     $fields = $q->rows('assoc');
  205.                     for($i=0;$i<count($fields);$i++) {
  206.                         if($fields[$i]['Field'] === $field) {
  207.                             $field = array('name' => $fields[$i]['Field'], 'type' => $fields[$i]['Type']);
  208.                             break;
  209.                         }
  210.                     }
  211.                 }
  212.                 if(is_array($field) && count($field) > 0) {
  213.                     if(preg_match("/varchar\(([0-9]*)\)/", $field['type'], $matches) !== 0) {
  214.                         if(strlen($value) <= $matches[1]) {
  215.                             $q = c('database')->update("UPDATE `user_promoter_information` SET ".$field['name']." = '".$value."' WHERE uid = '".$user."'");
  216.                             if($q->error() === false && $q->rowCount() === 1) {
  217.                                 return true;
  218.                             }
  219.                         }
  220.                     } elseif(preg_match("/^(bigint|int|tinyint)\(([0-9]*)\)$/", $field['type'], $matches) !== 0) {
  221.                         if(is_int($value)) {
  222.                             $q = c('database')->update("UPDATE `user_promoter_information` SET ".$field['name']." = '".$value."' WHERE uid = '".$user."'");
  223.                             if($q->error() === false && $q->rowCount() === 1) {
  224.                                 return true;
  225.                             }
  226.                         }
  227.                     } elseif(preg_match("/^float\(([0-9\,]*)\)$/", $field['type'], $matches) !== 0) {
  228.                         $dec = explode(',', $matches[1]);
  229.                         if(is_float($value)) {
  230.                             $q = c('database')->update("UPDATE `user_promoter_information` SET ".$field['name']." = '".$value."' WHERE uid = '".$user."'");
  231.                             if($q->error() === false && $q->rowCount() === 1) {
  232.                                 return true;
  233.                             }
  234.                         }
  235.                     } elseif(preg_match("/^(text|longtext)$/", $field['type']) !== 0) {
  236.                         $value = htmlentities($value);
  237.                         $q = c('database')->update("UPDATE `user_promoter_information` SET ".$field['name']." = '".$value."' WHERE uid = '".$user."'");
  238.                         if($q->error() === false && $q->rowCount() === 1) {
  239.                             return true;
  240.                         }
  241.                     } elseif(preg_match("/^(enum|set)\(([0-9a-zA-Z\,\']*)\)$/", $field['type'], $matches) !== 0) {
  242.                         $opts = explode(',', str_replace("'", '', $matches[2]));
  243.                         if(in_array($value, $opts)) {
  244.                             $q = c('database')->update("UPDATE `user_promoter_information` SET ".$field['name']." = '".$value."' WHERE uid = '".$user."'");
  245.                             if($q->error() === false && $q->rowCount() === 1) {
  246.                                 return true;
  247.                             }
  248.                         }
  249.                     }
  250.                 }
  251.             }
  252.         }
  253.         return false;
  254.     }
  255.     public function promoterLogo($logo, $user) {
  256.         if(!empty($logo) && !empty($user) && $this->isUser($user)) {
  257.             if(is_uploaded_file($logo['tmp_name'])) {
  258.                 $data = @exif_read_data($logo['tmp_name']);
  259.                 if(!empty($data)) {
  260.                     $max = (1024*1024)*2;
  261.                     $types = array('image/x-png', 'image/png', 'image/jpeg', 'image/jpg', 'image/pjpeg');
  262.                     if(!in_array($data['MimeType'], $types)) {
  263.                         return 1;
  264.                     }
  265.                     if($data['FileSize'] > $max) {
  266.                         return 2;
  267.                     }
  268.                     $new = $user;
  269.                     $dir = getcwd().'/data/uploads/logos/';
  270.                     if(is_file($dir.$new)) {
  271.                         unlink($dir.$new);
  272.                     }
  273.                     if(move_uploaded_file($logo['tmp_name'], $dir.$new)) {
  274.                         $mw = self::$conf['logoWidth'];
  275.                         $mh = self::$conf['logoHeight'];
  276.                         $w = $data['COMPUTED']['Width'];
  277.                         $h = $data['COMPUTED']['Height'];
  278.                         if($w > $mw || $h > $mh) {
  279.                             if($w > $h) {
  280.                                 $p = $mw / $w;
  281.                             } else {
  282.                                 $p = $mh / $h;
  283.                             }
  284.                             $nw = round($w * $p);
  285.                             $nh = round($h * $p);
  286.                             $image = imagecreatetruecolor($nw, $nh);
  287.                             if($data['MimeType'] === 'image/x-png' || $data['MimeType'] === 'image/png') {
  288.                                 $im = imagecreatefrompng($dir.$new);
  289.                             } elseif($data['MimeType'] === 'image/jpeg' || $data['MimeType'] === 'image/jpg' || $data['MimeType'] === 'image/pjpeg') {
  290.                                 $im = imagecreatefromjpeg($dir.$new);
  291.                             }
  292.                             imagecopyresampled($image, $im, 0, 0, 0, 0, $nw, $nh, $w, $h);
  293.                             unlink($dir.$new);
  294.                             imagepng($image, $dir.$new);
  295.                             $data['MimeType'] = 'image/png';
  296.                         }
  297.                         return $this->updatePromoter('logo', $data['MimeType'], $user);
  298.                     }
  299.                 }
  300.             }
  301.         }
  302.         return false;
  303.     }
  304.     public function get($field, $user) {
  305.         $field = addslashes(trim($field));
  306.         $user = addslashes(trim($user));
  307.         if(!empty($field) && !empty($user)) {
  308.             $q = c('database')->select("SELECT ".$field." FROM user_account WHERE id = '".$user."'");
  309.             if($q->error() === false && $q->rowCount() === 1) {
  310.                 $r = $q->row('num');
  311.                 return $r[0];
  312.             }
  313.         }
  314.         return false;
  315.     }
  316.     public function getPromoter($field, $user) {
  317.         $field = addslashes(trim($field));
  318.         $user = addslashes(trim($user));
  319.         if(!empty($field) && !empty($user)) {
  320.             $q = c('database')->select("SELECT ".$field." FROM user_promoter_information WHERE uid = '".$user."'");
  321.             if($q->error() === false && $q->rowCount() === 1) {
  322.                 $r = $q->row('num');
  323.                 return $r[0];
  324.             }
  325.         }
  326.         return false;
  327.     }
  328.     public function getUserProfile($id = NULL) {
  329.         if($this->isSignedIn() === true) {
  330.             if(is_null($user)) {
  331.                 $id = addslashes(trim($_SESSION['getGuestlist_User']['id']));
  332.             }
  333.             if(!empty($id) && $this->isUser($id) === true) {
  334.                 $q = c('database')->select("SELECT id, first_name, last_name, email, password, mobile, student, location, uni FROM user_account WHERE id = '".$id."'");
  335.                 if($q->error() === false && $q->rowCount() === 1) {
  336.                     $user = $q->row('assoc');
  337.                 }
  338.                 if($this->isPromoter($id) === true) {
  339.                     $q = c('database')->select("SELECT * FROM user_promoter_information WHERE uid = '".$id."'");
  340.                     if($q->error() === false && $q->rowCount() === 1) {
  341.                         $user['promoter'] = $q->row('assoc');
  342.                     }
  343.                 }
  344.                 if(is_array($user) && count($user) > 0) {
  345.                     return $user;
  346.                 }
  347.             }
  348.         }
  349.         return false;
  350.     }
  351.     public function getId() {
  352.         if($this->isSignedIn() === true) {
  353.             $s = $_SESSION['getGuestlist_User'];
  354.             $id = addslashes(trim($s['id']));
  355.             return $id;
  356.         }
  357.         return false;
  358.     }
  359. }
  360. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement