SHARE
TWEET

data

a guest May 22nd, 2017 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class DatabaseWrapper {
  4.     private $dbo;
  5.    
  6.     function __construct() {
  7.         try {
  8.             $this->dbo = new PDO('mysql:host=localhost;dbname=thwitter', 'root', '');
  9.         } catch (Exception $e) {
  10.             echo "Konnte keine Verbindung zur Datenbank herstellen!";
  11.             exit();
  12.         }
  13.     }
  14.    
  15.     public function createUser($user, $mail, $pass) {
  16.         $pass = sha1($pass);
  17.         echo $pass;
  18.        
  19.         $stmt = $this->dbo->prepare('INSERT INTO users (Username, Password, Mail) VALUES (:name, :pass, :mail)');
  20.         $stmt->bindParam(':name', $user);
  21.         $stmt->bindParam(':pass', $pass);
  22.         $stmt->bindParam(':mail', $mail);
  23.        
  24.         return $stmt->execute() == 1;
  25.     }
  26.    
  27.     public function isUserFree($user) {
  28.         $stmt = $this->dbo->prepare('SELECT COUNT(*) AS Anz FROM users WHERE Username LIKE :name');
  29.         $stmt->bindParam(':name', $user);
  30.         $stmt->execute();
  31.        
  32.         return $stmt->fetch()['Anz'] == 0;
  33.     }
  34.    
  35.     public function isLoginValid($user, $pass) {
  36.         $stmt = $this->dbo->prepare('SELECT COUNT(*) AS Anz, ID, Password FROM users WHERE Username LIKE :name');
  37.         $stmt->bindParam(':name', $user);
  38.         $stmt->execute();
  39.        
  40.         $result = $stmt->fetch();
  41.        
  42.         if($result['Anz'] > 0) {
  43.             if(sha1($pass) == $result['Password']) {
  44.                 return $result['ID'];
  45.             }
  46.         } else {
  47.             return false;
  48.         }
  49.     }
  50.    
  51.     public function createPost($userId, $text) {
  52.         $time = time();
  53.        
  54.         $stmt = $this->dbo->prepare('INSERT INTO posts (User, Timestamp, Text) VALUES (:user, :time, :text)');
  55.         $stmt->bindParam(':user', $userId);
  56.         $stmt->bindParam(':time', $time);
  57.         $stmt->bindParam(':text', $text);
  58.        
  59.         return $stmt->execute() == 1;
  60.     }
  61.    
  62.     public function deletePost($postId) {
  63.         $stmt = $this->dbo->prepare('DELETE FROM posts WHERE ID = :id');
  64.         $stmt->bindParam(':id', $postId);
  65.        
  66.         return $stmt->execute() == 1;
  67.     }
  68.    
  69.     public function getPosts() {
  70.         $prepString = '
  71.             SELECT u.Username AS User, p.Timestamp as Timestamp, p.Text as Text
  72.             FROM posts p
  73.                 INNER JOIN users u
  74.                     ON p.User = u.ID
  75.             ORDER BY p.Timestamp DESC';
  76.                
  77.         $stmt = $this->dbo->prepare($prepString);  
  78.         $stmt->execute();
  79.        
  80.         return $stmt->fetchAll();
  81.     }
  82.    
  83.     public function getLikes($postId) {
  84.         $stmt = $this->dbo->prepare('SELECT COUNT(*) AS Likes FROM likes WHERE Post = :post');
  85.         $stmt->bindParam(':post', $postId);
  86.         $stmt->execute();
  87.        
  88.         return $stmt->fetch()['Likes'];
  89.     }
  90.    
  91.     public function addLike($userId, $postId) {
  92.         $stmt = $this->dbo->prepare('INSERT INTO likes (User, Post) VALUES (:user, :post)');
  93.         $stmt->bindParam(':user', $userId);
  94.         $stmt->bindParam(':post', $postId);
  95.        
  96.         return $stmt->execute() == 1;
  97.     }
  98.    
  99.     public function removeLike($userId, $postId) {
  100.         $stmt = $this->dbo->prepare('DELETE FROM likes WHERE User = :user AND Post = :post');
  101.         $stmt->bindParam(':user', $userId);
  102.         $stmt->bindParam(':post', $postId);
  103.        
  104.         return $stmt->execute() == 1;
  105.     }
  106. }
  107.  
  108. $db = new DatabaseWrapper();
  109.  
  110. if($db->isUserFree("Testuser")) {
  111.     echo "Testuser frei! <br>";
  112. } else {
  113.     echo "Testuser belegt! <br>";
  114. }
  115. if($db->isUserFree("Bla")) {
  116.     echo "Bla frei!";
  117.     $db->createUser("Bla","abc","124");
  118. } else {
  119.     echo "Bla belegt <br>";
  120. }
  121.  
  122. echo $db->isLoginValid("Bla", "124");
  123. echo "<br>";
  124. print_r( $db->getPosts());
  125. echo "<br>";
  126. echo $db->getLikes(1);
  127. $db->removeLike(1, 1);
  128. echo "<br>".$db->getLikes(1);
  129. $db->addLike(1, 1);
  130. echo "<br>".$db->getLikes(1);
  131.  
  132.  
  133. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top