123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- require_once('comment.php');
- $pdo = null;
- $dsn = 'mysql:host=' . $config['host'] . ';dbname=' . $config['db'];
- $time = time();
- try {
- /* PDO object creation */
- $pdo = new PDO($dsn, $config['user'], $config['passwd']);
- /* Enable exceptions on errors */
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- /* If there is an error an exception is thrown */
- echo 'Connection failed<br>';
- echo 'Error number: ' . $e->getCode() . '<br>';
- echo 'Error message: ' . $e->getMessage() . '<br>';
- die();
- }
- function get_dir_info($dir) {
- global $pdo;
- $sql = 'SELECT * FROM dirs WHERE path = :dir';
- $values = array(
- ':dir' => $dir
- );
- try {
- $res = $pdo->prepare($sql);
- $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- if(!($row = $res->fetch(PDO::FETCH_ASSOC))) {
- return null;
- }
- return $row;
- }
- function get_dir_list() {
- global $pdo;
- $dir = [];
- $query = 'SELECT * FROM dirs ORDER BY date DESC';
- try {
- $res = $pdo->query($query);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- if ($row['hidden'])
- continue;
- $dir[] = $row;
- }
- return $dir;
- }
- function get_dir_count($dir) {
- global $pdo;
- $sql = 'SELECT id FROM dirs WHERE path = :dir';
- $values = array(
- ':dir' => $dir
- );
- try {
- $res = $pdo->prepare($sql);
- $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- if(!($row = $res->fetch(PDO::FETCH_ASSOC))) {
- return 0;
- }
- $dirid = $row['id'];
- $sql = 'SELECT count(*) AS count FROM images WHERE dir = :dirid';
- $values = array(
- ':dirid' => $dirid
- );
- try {
- $res = $pdo->prepare($sql);
- $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- if(!($row = $res->fetch(PDO::FETCH_ASSOC))) {
- return 0;
- }
- return $row['count'];
- }
- function update_dir_count($dirpath, $count) {
- global $pdo;
- $sql = 'UPDATE dirs SET count = :count WHERE path = :path';
- $values = array(
- ':path' => $dirpath,
- ':count' => $count,
- );
- try {
- $res = $pdo->prepare($sql);
- return $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- }
- function get_dir_images($dir, $mode = '') {
- global $pdo;
- $images = [];
- $sql = 'SELECT images.id, images.name, images.comment, images.views, images.likes, images.tags, images.path, dirs.name AS dir_name, dirs.comment AS dir_comment, dirs.views AS dir_views, dirs.likes AS dir_likes, dirs.tags AS dir_tags, dirs.path AS dir_path FROM images INNER JOIN dirs ON dirs.id=images.dir WHERE dirs.path = :path ORDER BY images.date ASC, images.path ASC';
- $values = array(
- ':path' => $dir
- );
- try {
- $res = $pdo->prepare($sql);
- $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- if ($mode == 'viewcomments') {
- if (count_comments($row['id'], 'image') == 0)
- continue;
- }
- $images[] = $row;
- $dirname = $row['dir_name']; // probably should do this only once
- }
- return $images;
- }
- function make_dir_path($dir) {
- global $config;
- return $config['basepath'] . '/' . $dir['path'] . '/';
- }
- function create_db_dir($path, $name, $comment, $tags, $date) {
- global $pdo;
- $sql = 'INSERT INTO dirs (name, comment, tags, path, date, lastmodified) VALUES (:name, :comment, :tags, :path, :date, :currentdate)';
- $values = array(
- ':name' => $name,
- ':comment' => $comment,
- ':tags' => $tags,
- ':path' => $path,
- ':date' => $date,
- ':currentdate' => time()
- );
- try {
- $res = $pdo->prepare($sql);
- return $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- }
- function update_db_dir($path, $name, $comment, $tags, $date) {
- global $pdo;
- $sql = 'UPDATE dirs SET name = :name, comment = :comment, tags = :tags, date = :date, lastmodified = :currentdate WHERE path = :path';
- $values = array(
- ':name' => $name,
- ':comment' => $comment,
- ':tags' => $tags,
- ':path' => $path,
- ':date' => $date,
- ':currentdate' => time()
- );
- try {
- $res = $pdo->prepare($sql);
- return $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- }
- function add_db_image($dirid, $name, $comment, $tags, $path, $date) {
- global $pdo;
- $sql = 'INSERT INTO images (dir, name, comment, tags, path, date, lastmodified) VALUES (:dirid, :name, :comment, :tags, :path, :date, :currentdate)';
- $values = array(
- ':dirid' => $dirid,
- ':name' => $name,
- ':comment' => $comment,
- ':tags' => $tags,
- ':path' => $path,
- ':date' => $date,
- ':currentdate' => time()
- );
- try {
- $res = $pdo->prepare($sql);
- return $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- }
- ?>
|