Advertisement
rav1989

Untitled

Apr 25th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.00 KB | None | 0 0
  1. <?php
  2.  
  3. define("MODE_GUEST",0);
  4. define("MODE_USER",1);
  5. define("MODE_ADMIN",2);
  6.  
  7. class User{
  8.     private $id;
  9.     private $email;
  10.     private $haslo;
  11.     private $construct;
  12.  
  13.     private $nazwa;
  14.     private $opis;
  15.     private $avatar;
  16.     private $mode;
  17.  
  18.     public function __construct($anonim = true){
  19.         if($anonim == true)
  20.         {
  21.             $this -> id = 0;
  22.             $this -> login = '';
  23.             $this -> password = '';
  24.             $this -> nazwa = 'Anonim';
  25.             $this -> opis = '';
  26.             $this -> avatar = '';
  27.             $this -> mode = MODE_GUEST;
  28.         }
  29.         $this -> construct = true;
  30.     }
  31.  
  32.     public function isAnonymous()
  33.     {
  34.         return ($this -> id == MODE_GUEST ? true : false);
  35.     }
  36.  
  37.     public function isAdmin()
  38.     {
  39.         return ($this -> mode == MODE_ADMIN ? true : false);
  40.     }
  41.  
  42.     public function getId()
  43.     {
  44.         return $this -> id;
  45.     }
  46.  
  47.     public function getEmail()
  48.     {
  49.         return $this -> email;
  50.     }
  51.  
  52.     public function getHaslo()
  53.     {
  54.         return $this -> haslo;
  55.     }
  56.  
  57.     public function getOpis()
  58.     {
  59.         return $this -> opis;
  60.     }
  61.  
  62.     public function getAvatar()
  63.     {
  64.         return $this -> avatar;
  65.     }
  66.  
  67.     public function getNazwa()
  68.     {
  69.         return $this -> nazwa;
  70.     }
  71.  
  72.     public function getMode()
  73.     {
  74.         return $this -> mode;
  75.     }
  76.  
  77.     public function __set($name, $value)
  78.     {
  79.         global $pdo;
  80.         if($this -> construct)
  81.         {
  82.             $this -> $name = $value;
  83.             $stmt = $pdo -> prepare('UPDATE '.MySql_TPrefix.'users SET
  84.                 '.$name.' = :value WHERE id = :id');
  85.             $stmt -> bindValue(':value', $value, PDO::PARAM_STR);
  86.             $stmt -> bindValue(':id', $this->getId(), PDO::PARAM_INT);
  87.             $stmt -> execute();
  88.         }
  89.     }
  90.  
  91.     static public function Sprawdz($email, $password)
  92.     {
  93.         global $pdo;
  94.         $stmt = $pdo -> prepare('SELECT id, email, nazwa, haslo, opis, avatar, mode FROM '.MySql_TPrefix.'users WHERE email=:email AND haslo=:haslo');
  95.         $stmt -> bindValue(':email', $email, PDO::PARAM_STR);
  96.         $stmt -> bindValue(':haslo', sha1(md5('@TuJestBardzo_').md5($password).md5('_TrudnaSol@')), PDO::PARAM_STR);
  97.         $stmt -> execute();
  98.         $stmt -> setFetchMode(PDO::FETCH_CLASS, 'User', array(0 => false));
  99.         if($user = $stmt -> fetch())
  100.         {
  101.             // Jezeli uzytkownik o takim loginie i hasle
  102.             // istnieje, zwroc jego rekord w postaci obiektu
  103.             $stmt -> closeCursor();
  104.             return $user;
  105.         }
  106.         else
  107.         {
  108.             $stmt -> closeCursor();
  109.             // Bledy w loginie/hasle zglaszamy zerem
  110.             return 0;
  111.         }
  112.     }
  113.     static public function Rejestruj ($email, $password, $nazwa)
  114.     {
  115.         global $pdo;
  116.         $result = User::Sprawdz($email, $password);
  117.         if($result  instanceof user){ return false; }
  118.         $stmt = $pdo -> prepare('INSERT INTO '.MySql_TPrefix.'users (email, haslo, mode, nazwa) VALUES (:email, :haslo, :mode, :nazwa)');
  119.         $stmt -> bindValue(':email', $email, PDO::PARAM_STR);
  120.         $stmt -> bindValue(':haslo', sha1(md5('@TuJestBardzo_').md5($password).md5('_TrudnaSol@')), PDO::PARAM_STR);
  121.         $stmt -> bindValue(':mode', MODE_USER, PDO::PARAM_INT);
  122.         $stmt -> bindValue(':nazwa', $nazwa, PDO::PARAM_STR);
  123.         $ile = $stmt -> execute();
  124.         if($ile > 0)
  125.         {
  126.             return true;
  127.         }
  128.         else
  129.         {
  130.             return false;
  131.         }
  132.     }
  133. }
  134.  
  135.  
  136.  
  137. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement