public function get()
 {
     $bdd = new Database('home');
     $get = $bdd->getBdd()->prepare('SELECT id, name FROM categories WHERE active = 1');
     $get->execute();
     $all = $get->fetchAll(\PDO::FETCG_ASSOC);
     return json_encode($all);
 }
 public function getByPost($id)
 {
     $bdd = new Database('home');
     $get = $bdd->getBdd()->prepare('SELECT * FROM media WHERE post_id = :post_id');
     $get->bindParam(':post_id', $id);
     $get->execute();
     $medias = $get->fetchAll(\PDO::FETCH_ASSOC);
     return $medias;
 }
 public function create($blog_id, $title, $content)
 {
     $bdd = new Database('home');
     $create = $bdd->getBdd()->prepare('INSERT INTO mails (blog_id, title, content, user_id) VALUES (:blog_id, :title, :content, :user_id)');
     $create->bindParam(':blog_id', $blog_id);
     $create->bindParam(':title', $title);
     $create->bindParam(':content', $content);
     $create->bindParam(':user_id', $_SESSION['id']);
     $create->execute();
     return true;
 }
Exemple #4
0
 public static function retrieve($username = null, $hashed_password = null, $token = null)
 {
     $sql = 'SELECT username, address, password, salt, is_admin, ' . 'token, token_expiration ' . 'FROM users';
     if (!is_null($username)) {
         $sql .= sprintf(' WHERE password="******" AND username="******"', $hashed_password, $username);
     } else {
         if (!is_null($token)) {
             $sql .= sprintf(' WHERE token="%s" AND token_expiration>="%s"', $token, Database::now());
         }
     }
     $result = HackableDatabase::select($sql);
     $users = array();
     foreach ($result as $r) {
         $is_admin = $r['is_admin'] === '1' ? true : false;
         $users[] = new HackableUser($r['username'], $r['address'], $r['password'], $r['salt'], $is_admin, $r['token'], $r['token_expiration']);
     }
     if (!is_null($username)) {
         if (empty($users)) {
             throw new \Lib\Exceptions\NotFoundException();
         }
         return $users[0];
     }
     if (!is_null($token)) {
         if (empty($users)) {
             throw new \Lib\Exceptions\UnauthorizedException();
         }
         return $users[0];
     }
     return $users;
 }
 public function setMinusComment($comment_id)
 {
     $bdd = new Database('home');
     $get_comment = $bdd->getBdd()->prepare('SELECT * FROM comments WHERE id = :id');
     $get_comment->bindParam(':id', $comment_id, \PDO::PARAM_INT);
     $get_comment->execute();
     $comment = $get_comment->fetch(\PDO::FETCH_ASSOC);
     if ($comment === false) {
         $comment = array('error' => 'comment id invalid');
         return json_encode($comment);
     } else {
         $current_comment_vote = $comment['vote'];
         $new_vote = $current_comment_vote - 1;
         $vote_minus = $bdd->getBdd()->prepare('UPDATE comments SET vote = ' . $new_vote . ' WHERE id = :id');
         $vote_minus->bindParam(':id', $comment_id, \PDO::PARAM_INT);
         $vote_minus->execute();
         return true;
     }
 }
 /**
  * Expect full path of folder which will be parse
  * @param string $dir_path
  */
 public function __construct($dir_path = '.')
 {
     $dir_path = \models\Database::validateData($dir_path, 'string');
     if (!empty($dir_path) && is_dir($dir_path)) {
         $this->_dir_path = $dir_path;
         $this->_is_directory = true;
     } else {
         echo 'Given $dir_path [' . $dir_path . '] for \\models\\Parser instance is not directory!';
     }
 }
 /**
  * get what eat
  * @return string
  */
 public function getEat()
 {
     return \models\Database::validateData($this->_eat, 'string|specialchars|strip_tags');
 }
<?php

require_once '../config.php';
require_once ROOT_PATH . '/models/Database.php';
require_once ROOT_PATH . '/models/Animal.php';
if (!empty($_POST['searched_text'])) {
    //filter and validate searched data
    $searched_text = \models\Database::validateData($_POST['searched_text'], 'string|specialchars|strip_tags');
    $params = array('search_name' => $searched_text);
    $searched_animals = \models\Animal::getAnimals($params, true);
    $search_result = '';
    if (!empty($searched_animals)) {
        foreach ($searched_animals as $key => $data) {
            $search_result .= '<tr>';
            $search_result .= '<td>' . $searched_animals[$key]['id'] . '</td>';
            $search_result .= '<td>' . $searched_animals[$key]['category'] . '</td>';
            $search_result .= '<td>' . $searched_animals[$key]['subcategory'] . '</td>';
            $search_result .= '<td>' . $searched_animals[$key]['name'] . '</td>';
            $search_result .= '<td>' . $searched_animals[$key]['description'] . '</td>';
            $search_result .= '<td>' . $searched_animals[$key]['eat'] . '</td>';
            $search_result .= '<td>' . $searched_animals[$key]['date_created'] . '</td>';
            $search_result .= '</tr>';
        }
    } else {
        $search_result = '<div class="alert alert-danger">Search results not found.</div>';
    }
    echo json_encode($search_result);
} else {
    throw new Exception('Empty string given for seach');
}
 public function readPost($id, $json = 0)
 {
     $bdd = new Database('home');
     $read = $bdd->getBdd()->prepare('SELECT * FROM posts WHERE id = :id AND active = 1');
     $read->bindParam(':id', $id);
     $read->execute();
     $post = $read->fetch(\PDO::FETCH_ASSOC);
     if ($json == 0) {
         $this->setPost($post);
         return true;
     } else {
         if (empty($post)) {
             $post = array('error' => 'post id invalid');
         } else {
             $medias = new MediasController();
             $nb_comments = $bdd->getBdd()->prepare('SELECT COUNT(id) AS nb_comments FROM comments WHERE post_id = :post_id');
             $nb_comments->bindParam(':post_id', $post['id'], \PDO::PARAM_INT);
             $nb_comments->execute();
             $nb_comments = $nb_comments->fetch(\PDO::FETCH_ASSOC);
             $post['comments'] = array();
             if ($nb_comments["nb_comments"] !== 0) {
                 $all_post_comments = $bdd->getBdd()->prepare('SELECT users.id AS "user_id", comments.id AS "comment_id", users.name AS "user_name", title, content, score, vote FROM comments LEFT JOIN users ON users.id = comments.user_id WHERE post_id = :post_id');
                 $all_post_comments->bindParam(':post_id', $post['id'], \PDO::PARAM_INT);
                 $all_post_comments->execute();
                 $all_post_comments = $all_post_comments->fetchAll();
                 foreach ($all_post_comments as $comment) {
                     $post['comments']['comment_id'][] = $comment['comment_id'];
                     $post['comments']['user_id'][] = $comment['user_id'];
                     $post['comments']['user_name'][] = $comment['user_name'];
                     $post['comments']['title'][] = $comment['title'];
                     $post['comments']['content'][] = $comment['content'];
                     $post['comments']['score'][] = $comment['score'];
                     $post['comments']['vote'][] = $comment['vote'];
                 }
             }
             $post['nb_comments'] = $nb_comments['nb_comments'];
             $post['medias'] = $medias->getByPost($post['id']);
         }
         return json_encode($post);
     }
 }
 private function _updateCheckEmail($email)
 {
     $bdd = new Database('home');
     $id = isset($_SESSION['id']) ? $_SESSION['id'] : 0;
     $check = $bdd->getBdd()->prepare('SELECT email FROM users WHERE email = :email AND id != :id AND active = 1');
     $check->bindParam(':email', $email, \PDO::PARAM_STR, 60);
     $check->bindParam(':id', $id);
     $check->execute();
     $user = $check->fetch(\PDO::FETCH_ASSOC);
     if ($user) {
         return false;
     }
     return true;
 }
 public function getMyBlogs()
 {
     $bdd = new Database('home');
     $get = $bdd->getBdd()->prepare('SELECT id, name FROM blogs WHERE user_id = :user_id AND active = 1');
     $get->bindParam(':user_id', $_SESSION['id']);
     $get->execute();
     $all = $get->fetchAll(\PDO::FETCH_ASSOC);
     if (empty($all)) {
         $this->setError('You have no blog');
     }
     $this->_blogs = $all;
 }