Advertisement
ItsWidee

Untitled

Feb 19th, 2021
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.75 KB | None | 0 0
  1. <?php
  2. function debug($variable){
  3.  
  4.     echo '<pre class="error">' . print_r($variable, true) . '</pre>';
  5.  
  6. }
  7.  
  8. function str_random($length){
  9.     $alphabet = "0123456789azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN";
  10.     return substr(str_shuffle(str_repeat($alphabet, $length)), 0, $length);
  11. }
  12.  
  13. function logged_only(){
  14.     if(!isset($_SESSION['auth'])){
  15.         $_SESSION['flash']['danger'] = "Vous n'avez pas le droit d'accéder à cette page";
  16.         header('Location: /connexion.php');
  17.         exit();
  18.     }
  19. }
  20.  
  21. function is_connected(){
  22.     if(isset($_SESSION['auth'])){
  23.         return true;
  24.     }else{
  25.         return false;
  26.     }
  27. }
  28.  
  29. function reconnect_from_cookie(){
  30.     if(session_status() == PHP_SESSION_NONE){
  31.         session_start();
  32.     }
  33.     if(isset($_COOKIE['remember']) && !isset($_SESSION['auth'])){
  34.         require_once 'db.php';
  35.         if(!isset($pdo)){
  36.             global $pdo;
  37.         }
  38.         $remember_token = $_COOKIE['remember'];
  39.         $parts = explode('==', $remember_token);
  40.         $user_id = $parts['0'];
  41.         $req = $pdo->prepare('SELECT * FROM users WHERE id = ?');
  42.         $req->execute([$user_id]);
  43.         $user = $req->fetch();
  44.         if($user){
  45.             $expected = $user_id . '==' . $user['remember_token'] . sha1($user_id . 'ratonlaveurs');
  46.             if($expected == $remember_token){
  47.                 $_SESSION['auth'] = $user;
  48.                 unset($_SESSION['flash']);
  49.                 setcookie('remember', $remember_token, time() + 60 * 60 * 24 * 7);
  50.             }else{
  51.                 setcookie('remember', NULL, -1);
  52.             }
  53.         }else{
  54.             setcookie('remember', NULL, -1);
  55.         }
  56.     }
  57. }
  58.  
  59. // Récupérer la liste d'histoires
  60. function get_last_stories(){
  61.     require_once 'db.php';
  62.     $req = $pdo->prepare('SELECT id, titre, auteur, histoire_resume, category, DATE_FORMAT(date_publication, \'%d/%m/%Y à %Hh%i (Heure française)\') AS good_date_publication FROM histoires ORDER BY id DESC LIMIT 0, 10');
  63.     $req->execute();
  64.     $data = $req->fetchAll();
  65.     return $data;
  66.     $req->closeCursor();
  67. }
  68. // Récupérer une histoire
  69. function get_story($id){
  70.     require 'db.php';
  71.     $req = $pdo->prepare('SELECT *, DATE_FORMAT(date_publication, \'%d/%m/%Y à %Hh%i (Heure française)\') AS good_date_publication FROM histoires WHERE id = ?');
  72.     $req->execute([$id]);
  73.     if($req->rowCount() == 1){
  74.         $data = $req->fetch();
  75.         return $data;
  76.     } else {
  77.         header('Location: index.php');
  78.     }
  79. }
  80. // Rechercher une histoire
  81. function search($content){
  82.     require_once 'db.php';
  83.     $valid_content = strip_tags($content);
  84.     $req = $pdo->prepare("SELECT id, titre, auteur, histoire_resume, category, DATE_FORMAT(date_publication, '%d/%m/%Y à %Hh%i (UTC+1)') AS good_date_publication FROM histoires WHERE titre LIKE '%" . $valid_content . "%'");
  85.     $req->execute();
  86.     $data = $req->fetchAll();
  87.     return $data;
  88. }
  89.  
  90. function search_inside($content){
  91.     require_once 'db.php';
  92.     $valid_content = strip_tags($content);
  93.     $req = $pdo->prepare("SELECT id, titre, histoire_resume, category, DATE_FORMAT(date_publication, '%d/%m/%Y à %Hh%i (UTC+1)') AS good_date_publication FROM histoires WHERE titre LIKE '%" . $valid_content . "%' OR id LIKE '%" . $valid_content . "%'");
  94.     $req->execute();
  95.     $data = $req->fetchAll();
  96.     return $data;
  97. }
  98.  
  99. function search_category($content){
  100.     require_once 'db.php';
  101.     $valid_content = strip_tags($content);
  102.     $req = $pdo->prepare("SELECT id, titre, auteur, histoire_resume, category, DATE_FORMAT(date_publication, '%d/%m/%Y à %Hh%i (UTC+1)') AS good_date_publication FROM histoires WHERE category = ?");
  103.     $req->execute([$valid_content]);
  104.     $data = $req->fetchAll();
  105.     return $data;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement