Advertisement
BobbiMonsta

home

Jan 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.46 KB | None | 0 0
  1. // Persist
  2.  
  3. function persist($db) {
  4.     try {
  5.         if ($this->id) {
  6.             $sqlSelect = "UPDATE user SET username=:username, password=:password WHERE id = " . $this->id;
  7.             $sthSqlSelect = $db->prepare($sqlSelect);
  8.             $result = $sthSqlSelect->execute([":username" => $this->username, ":password" => $this->password]);
  9.         } else {
  10.             $sqlSelect = "INSERT INTO user (username, password) VALUES (:username, :password)";
  11.             $sthSqlSelect = $db->prepare($sqlSelect);
  12.             $result = $sthSqlSelect->execute([":username" => $this->username, ":password" => $this->password]);
  13.             if ($result)
  14.                 $this->id = (int) $db->lastInsertId();
  15.         }
  16.         return $result;
  17.     } catch (PDOException $e) {
  18.         throw new Exception("Username already exists.");
  19.     }
  20. }
  21.  
  22.  
  23. // Iterador
  24.  
  25. // Se resetea el iterador para empezar por el primero
  26. $matchday->getMatches()->resetIterator();
  27.  
  28. // Se recorre la colección de partidas
  29. while ($match = $matchday->getMatches()->iterate()) {
  30.     //
  31. }
  32.  
  33.  
  34. // Fetch All
  35.  
  36. $sql = 'SELECT * FROM `match` WHERE id_matchday = :id_matchday';
  37. $sthSql = $db->prepare($sql);
  38. $sthSql->execute([':id_matchday' => $id]);
  39. $sthSql->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Match');
  40. $result = $sthSql->fetchAll();
  41. return $result;
  42.  
  43.  
  44. // Get User
  45.  
  46. static function getUserByCredentials($db, $username, $password) {
  47.     $sqlSelect = 'SELECT * FROM user WHERE username=:username AND password=:password';
  48.     $sthSqlSelect = $db->prepare($sqlSelect);
  49.     $sthSqlSelect->execute([":username" => $username, ":password" => $password]);
  50.     $sthSqlSelect->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'User');
  51.     $user = $sthSqlSelect->fetch();
  52.     if ($user) {
  53.         $matchdays = Matchday::getLeague($db);
  54.         foreach ($matchdays as $matchday) {
  55.             $user->getLeague()->add($matchday);
  56.         }
  57.     }
  58.     return $user;
  59. }
  60.  
  61.  
  62. // DB in index.php
  63.  
  64. try {
  65.     $db = Database::getConnection();
  66. } catch (Exception $e) {
  67.     $type = 'danger';
  68.     $output = $e->getMessage();
  69.     include('views/login.php');
  70.     die();
  71. }
  72.  
  73. // Database.php CLASS
  74.  
  75. class Database {
  76.     private $host = 'localhost';
  77.     private $user = 'root';
  78.     private $pass = '';
  79.     private $dbname = 'sport';
  80.     protected static $bd = null;
  81.    
  82.     private function __construct() {
  83.         try {
  84.             self::$bd = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->pass);
  85.             self::$bd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  86.         } catch (PDOException $e) {
  87.             switch($e->getCode()) {
  88.                 case 2002:
  89.                     $msg = "Can't connect to the database, try again later.";
  90.                     break;
  91.                 default:
  92.                     $msg = "Unknown error, try again later.";
  93.                     break;
  94.             }
  95.             throw new Exception($msg);
  96.         }
  97.     }
  98.  
  99.     public static function getConnection() {
  100.         if (!self::$bd) {
  101.             new Database();
  102.         }
  103.         return self::$bd;
  104.     }
  105.  
  106. }
  107.  
  108.  
  109. // Auth.php CLASS
  110.  
  111. class Auth {
  112.    
  113.     const SESSION_NAME = 'user';
  114.     protected static $auth = null;
  115.    
  116.     function __construct() {
  117.         if (session_status() === PHP_SESSION_NONE) {
  118.             session_start();
  119.         }
  120.     }
  121.    
  122.     public static function getAuth() {
  123.         if (!self::$auth) {
  124.             self::$auth = new Auth();
  125.         }
  126.         return self::$auth;
  127.     }
  128.    
  129.     function check() {
  130.         return isset ($_SESSION[self::SESSION_NAME]);
  131.     }
  132.    
  133.     function login($user) {
  134.         $_SESSION[self::SESSION_NAME] = $user;
  135.     }
  136.    
  137.     function loggedUser() {
  138.         return $_SESSION[self::SESSION_NAME];
  139.     }
  140.    
  141.     function logout() {
  142.         session_unset();
  143.         session_destroy();
  144.     }
  145. }
  146.  
  147.  
  148. // Example of view
  149.  
  150. <!DOCTYPE html>
  151. <html>
  152.     <head>
  153.         <meta charset="UTF-8">
  154.         <title>Sports - Login</title>
  155.         <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  156.         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  157.         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
  158.         <style>
  159.             .link-lookalike {
  160.                 background: none;
  161.                 border: none;
  162.                 color: blue;
  163.                 text-decoration: underline;
  164.                 cursor: pointer;
  165.             }
  166.         </style>
  167.         <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  168.         <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  169.     </head>
  170.     <body>
  171.         <div class="container">
  172.             <div class="row main col-sm-4 col-md-offset-4">
  173.                 <div class="panel-heading">
  174.                     <div class="panel-title text-left">
  175.                         <h1 class="title">Login to access</h1>
  176.                         <hr />
  177.                     </div>
  178.                 </div>
  179.                 <?php
  180.                 if (isset($output)) {
  181.                     ?>
  182.                     <div class="alert alert-<?php echo $type ?> text-center">
  183.                         <?php echo $output ?>
  184.                     </div>
  185.                     <?php
  186.                 }
  187.                 ?>
  188.                 <form action="index.php" name="login-form" method="POST">
  189.                     <span class="input-group text-center">
  190.                         <input type="text" class="form-control" placeholder="Username" name="username" required autofocus />
  191.                         <span class="input-group-btn" style="width:0px; height:0px;"></span>
  192.                         <input type="password" class="form-control" placeholder="Password" name="password" required />
  193.                         <span class="input-group-btn">
  194.                             <input class="btn btn-primary" type="submit" value="Login" name='login'>
  195.                         </span>
  196.                     </span>
  197.                 </form>
  198.                 <form action="index.php" name="login-form" method="POST">
  199.                     <p><h6>Don't have an account? <input class="link-lookalike" value="Register here" type="submit" name="register-form"></h6></p>
  200.                 </form>
  201.             </div>
  202.         </div>
  203.     </body>
  204. </html>
  205.  
  206.  
  207. // Intro example controlador
  208.  
  209. <?php
  210.  
  211. // Importación de clases requeridas
  212. require_once('class/Database.php');
  213. require_once('class/Collection.php');
  214.  
  215. // Arrancamos la sesión desde la clase singleton Auth
  216. $auth = Auth::getAuth();
  217.  
  218. // Se intenta la conexión con la BD.
  219. // En caso de error se le mostrará al usuario
  220. // la vista login.php con un mensaje.
  221. try {
  222.     $db = Database::getConnection();
  223. } catch (Exception $e) {
  224.     $type = 'danger';
  225.     $output = $e->getMessage();
  226.     include('views/login.php');
  227.     die();
  228. }
  229.  
  230. // Controlador de la aplicación
  231. if ($auth->check()) {
  232.  
  233. } else {
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement