Advertisement
ByMsx

Untitled

Jun 4th, 2014
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.75 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. Tasks:
  6. Id Student Teacher Mark Completed Content Title Date
  7.  
  8. Users
  9. Id Access E-mail Password Teacher Name
  10.  
  11. */
  12.  
  13. class CUser
  14. {
  15.     private $db;
  16.     public $Error;
  17.     public function __construct( $_db )
  18.     {
  19.         $this->db = $_db;
  20.     }
  21.     public function Login( $login, $password )
  22.     {
  23.         $query = $this->db->prepare("SELECT `Id`, `Access`, `E-mail` FROM `Users` WHERE `E-mail` = :login AND `Password` = :pass LIMIT 1");
  24.         $query->bindParam(':login', $login, PDO::PARAM_STR);
  25.         $query->bindParam(':pass', $password, PDO::PARAM_STR);
  26.         $query->execute();
  27.         $result = $query->fetch(PDO::FETCH_ASSOC);
  28.         if( !empty($result) )
  29.         {
  30.             foreach ($result as $key => $value) {
  31.                 $_SESSION[$key] = $value;
  32.             }
  33.             return 1;
  34.         }
  35.         else
  36.         {
  37.             $Error = "Неверный логин/пароль.";
  38.             return 2;
  39.         }
  40.     }
  41.     public function RegisterStudent( $params, $TeacherId )
  42.     {
  43.         $UID = $this->GetMaxUserId();
  44.         $Access = 0;
  45.         if( array_key_exists('Password', $params) && $params["Password"] == "" )
  46.             $params["Password"] = chr( rand( 97,122 ) ) . chr( rand( 97,122 ) ) . chr( rand( 97,122 ) ) . chr( rand( 97,122 ) );
  47.         $query = $this->db->prepare( "INSERT INTO `Users` (`Id`, `Name`, `E-mail`, `Password`, `Access`, `Teacher`) VALUES (:Id, :Name, :Email, :Password, :Access, :TeacherId);" );
  48.         $query->bindParam( ':Id', $UID, PDO::PARAM_INT );
  49.         $query->bindParam( ':Name', $params["Name"], PDO::PARAM_STR );
  50.         $query->bindParam( ':Email', $params["E-mail"], PDO::PARAM_STR );
  51.         $query->bindParam( ':Password', $params["Password"], PDO::PARAM_STR );
  52.         $query->bindParam( ':Access', $Access, PDO::PARAM_INT );
  53.         $query->bindParam( ':TeacherId', $TeacherId, PDO::PARAM_INT );
  54.         return $query->execute();
  55.     }
  56.     public function OldRegisterStudent( $params, $Id )
  57.     {
  58.         $params["Teacher"]  = $Id;
  59.         $params["Id"]       = $this->GetMaxUserId();
  60.         $params["Access"]   = 0;
  61.         if( $params["Password"] == "" )
  62.             $params["Password"] = chr( rand( 97,122 ) ) . chr( rand( 97,122 ) ) . chr( rand( 97,122 ) ) . chr( rand( 97,122 ) );
  63.  
  64.         $tQuery = $this->db->prepare("DESCRIBE `Users`;");
  65.         $tQuery->execute();
  66.         $tResult = $tQuery->fetchAll(PDO::FETCH_ASSOC);
  67.         $arTypes = array();
  68.         foreach ($tResult as $key => $value) {
  69.             if($value['Null'] == 'NO')
  70.             {
  71.                 $type = preg_replace('/^(\w+)(\(.*\))?$/', '$1', $value['Type']);
  72.                 /* if(strpos($value['Type'], 'varchar') !== false) $arTypes[$value['Field']] = PDO::PARAM_STR;
  73.                 elseif(strpos($value['Type'], 'int') !== false) $arTypes[$value['Field']] = PDO::PARAM_INT; */
  74.                 switch( $type )
  75.                 {
  76.                     case 'int':
  77.                     case 'decimal':
  78.                         $arTypes[ $value['Field'] ] = PDO::PARAM_INT;
  79.                         break;
  80.                     default:
  81.                         $arTypes[ $value['Field'] ] = PDO::PARAM_STR;
  82.                         break;
  83.                 }
  84.             }
  85.         }
  86.         $tQuery->closeCursor();
  87.         $arFields = "";
  88.         $arFieldsBool = false;
  89.         $arVals = "";
  90.         foreach ($arTypes as $key => $value) {
  91.             if( !array_key_exists( $key, $params ) )
  92.             {
  93.                 $this->Error = "Missing key: " . $key;
  94.             }
  95.             else
  96.             {
  97.                 if($arFieldsBool === false)
  98.                 {
  99.                     $arFields = "`" . $key . "`";
  100.                     $arVals = ":" . $key;
  101.                     $arFieldsBool = true;
  102.                 }
  103.                 else
  104.                 {
  105.                     $arFields .= ", `" . $key . "`";
  106.                     $arVals .= ", :" . $key;
  107.                 }
  108.             }
  109.         }
  110.         $dquery = $this->db->prepare("INSERT INTO `Users` (" . $arFields . ") VALUES (" . $arVals . ");");
  111.         foreach($arTypes as $key => $value)
  112.         {
  113.             $dquery->bindValue(':' . $key, $params[ $key ], $value);
  114.         }
  115.         $dquery->execute();
  116.         return 0;
  117.     }
  118.     public function GetTask( $Id )
  119.     {
  120.         return $this->GetCommonById( '`Id`', '`Tasks`', '`Id`', $Id );
  121.     }
  122.     public function GetTasksForStudent( $Id )
  123.     {
  124.         return $this->GetCommonById( '`Id`', '`Tasks`', '`Student`', $Id );
  125.     }
  126.     public function GetTasksForStudentA( $Id )
  127.     {
  128.         return $this->GetCommonById( '*', '`Tasks`', '`Student`', $Id );
  129.     }
  130.     public function GetTasksForTeacher( $Id )
  131.     {
  132.         return $this->GetCommonById( '`Id`', '`Tasks`', '`Teacher`', $Id );
  133.     }
  134.     public function GetStudentsForTeacher( $Id )
  135.     {
  136.         return $this->GetCommonById( '`Id`, `Name`, `E-mail`', '`Users`', '`Teacher`', $Id );
  137.     }
  138.     public function GetMaxUserId()
  139.     {
  140.         $query = $this->db->prepare("SELECT MAX(Id) FROM `Users`;");
  141.         $query->execute();
  142.         $res = $query->fetch(PDO::FETCH_ASSOC);
  143.         $id = $res['MAX(Id)'];
  144.         $query->closeCursor();
  145.         ++$id;
  146.         return $id;
  147.     }
  148.     public function GetTasksAll()
  149.     {
  150.         $query = $this->db->prepare("SELECT * FROM `Tasks`;");
  151.         $query->execute();
  152.         return $query->fetchAll();
  153.     }
  154.     public function GetCommonById( $SelectKeys, $TableName, $IdName, $Id )
  155.     {
  156.         $query = $this->db->prepare("SELECT " . $SelectKeys . " FROM " . $TableName . " WHERE " . $IdName . " = :id");
  157.         $query->bindParam(':id', $Id, PDO::PARAM_INT);
  158.         $query->execute();
  159.         return $query->fetchAll();
  160.     }
  161. }
  162.  
  163. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement