db.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. require_once('comment.php');
  3. $pdo = null;
  4. $dsn = 'mysql:host=' . $config['host'] . ';dbname=' . $config['db'];
  5. $time = time();
  6. try {
  7. /* PDO object creation */
  8. $pdo = new PDO($dsn, $config['user'], $config['passwd']);
  9. /* Enable exceptions on errors */
  10. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. } catch (PDOException $e) {
  12. /* If there is an error an exception is thrown */
  13. echo 'Connection failed<br>';
  14. echo 'Error number: ' . $e->getCode() . '<br>';
  15. echo 'Error message: ' . $e->getMessage() . '<br>';
  16. die();
  17. }
  18. function get_dir_info($dir) {
  19. global $pdo;
  20. $sql = 'SELECT * FROM dirs WHERE path = :dir';
  21. $values = array(
  22. ':dir' => $dir
  23. );
  24. try {
  25. $res = $pdo->prepare($sql);
  26. $res->execute($values);
  27. } catch (PDOException $e) {
  28. echo 'Query error: ' . $e->getMessage();
  29. die();
  30. }
  31. if(!($row = $res->fetch(PDO::FETCH_ASSOC))) {
  32. return null;
  33. }
  34. return $row;
  35. }
  36. function get_dir_list() {
  37. global $pdo;
  38. $dir = [];
  39. $query = 'SELECT * FROM dirs ORDER BY date DESC';
  40. try {
  41. $res = $pdo->query($query);
  42. } catch (PDOException $e) {
  43. echo 'Query error: ' . $e->getMessage();
  44. die();
  45. }
  46. while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
  47. if ($row['hidden'])
  48. continue;
  49. $dir[] = $row;
  50. }
  51. return $dir;
  52. }
  53. function get_dir_count($dir) {
  54. global $pdo;
  55. $sql = 'SELECT id FROM dirs WHERE path = :dir';
  56. $values = array(
  57. ':dir' => $dir
  58. );
  59. try {
  60. $res = $pdo->prepare($sql);
  61. $res->execute($values);
  62. } catch (PDOException $e) {
  63. echo 'Query error: ' . $e->getMessage();
  64. die();
  65. }
  66. if(!($row = $res->fetch(PDO::FETCH_ASSOC))) {
  67. return 0;
  68. }
  69. $dirid = $row['id'];
  70. $sql = 'SELECT count(*) AS count FROM images WHERE dir = :dirid';
  71. $values = array(
  72. ':dirid' => $dirid
  73. );
  74. try {
  75. $res = $pdo->prepare($sql);
  76. $res->execute($values);
  77. } catch (PDOException $e) {
  78. echo 'Query error: ' . $e->getMessage();
  79. die();
  80. }
  81. if(!($row = $res->fetch(PDO::FETCH_ASSOC))) {
  82. return 0;
  83. }
  84. return $row['count'];
  85. }
  86. function update_dir_count($dirpath, $count) {
  87. global $pdo;
  88. $sql = 'UPDATE dirs SET count = :count WHERE path = :path';
  89. $values = array(
  90. ':path' => $dirpath,
  91. ':count' => $count,
  92. );
  93. try {
  94. $res = $pdo->prepare($sql);
  95. return $res->execute($values);
  96. } catch (PDOException $e) {
  97. echo 'Query error: ' . $e->getMessage();
  98. die();
  99. }
  100. }
  101. function get_dir_images($dir, $mode = '') {
  102. global $pdo;
  103. $images = [];
  104. $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';
  105. $values = array(
  106. ':path' => $dir
  107. );
  108. try {
  109. $res = $pdo->prepare($sql);
  110. $res->execute($values);
  111. } catch (PDOException $e) {
  112. echo 'Query error: ' . $e->getMessage();
  113. die();
  114. }
  115. while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
  116. if ($mode == 'viewcomments') {
  117. if (count_comments($row['id'], 'image') == 0)
  118. continue;
  119. }
  120. $images[] = $row;
  121. $dirname = $row['dir_name']; // probably should do this only once
  122. }
  123. return $images;
  124. }
  125. function make_dir_path($dir) {
  126. global $config;
  127. return $config['basepath'] . '/' . $dir['path'] . '/';
  128. }
  129. function create_db_dir($path, $name, $comment, $tags, $date) {
  130. global $pdo;
  131. $sql = 'INSERT INTO dirs (name, comment, tags, path, date, lastmodified) VALUES (:name, :comment, :tags, :path, :date, :currentdate)';
  132. $values = array(
  133. ':name' => $name,
  134. ':comment' => $comment,
  135. ':tags' => $tags,
  136. ':path' => $path,
  137. ':date' => $date,
  138. ':currentdate' => time()
  139. );
  140. try {
  141. $res = $pdo->prepare($sql);
  142. return $res->execute($values);
  143. } catch (PDOException $e) {
  144. echo 'Query error: ' . $e->getMessage();
  145. die();
  146. }
  147. }
  148. function update_db_dir($path, $name, $comment, $tags, $date) {
  149. global $pdo;
  150. $sql = 'UPDATE dirs SET name = :name, comment = :comment, tags = :tags, date = :date, lastmodified = :currentdate WHERE path = :path';
  151. $values = array(
  152. ':name' => $name,
  153. ':comment' => $comment,
  154. ':tags' => $tags,
  155. ':path' => $path,
  156. ':date' => $date,
  157. ':currentdate' => time()
  158. );
  159. try {
  160. $res = $pdo->prepare($sql);
  161. return $res->execute($values);
  162. } catch (PDOException $e) {
  163. echo 'Query error: ' . $e->getMessage();
  164. die();
  165. }
  166. }
  167. function add_db_image($dirid, $name, $comment, $tags, $path, $date) {
  168. global $pdo;
  169. $sql = 'INSERT INTO images (dir, name, comment, tags, path, date, lastmodified) VALUES (:dirid, :name, :comment, :tags, :path, :date, :currentdate)';
  170. $values = array(
  171. ':dirid' => $dirid,
  172. ':name' => $name,
  173. ':comment' => $comment,
  174. ':tags' => $tags,
  175. ':path' => $path,
  176. ':date' => $date,
  177. ':currentdate' => time()
  178. );
  179. try {
  180. $res = $pdo->prepare($sql);
  181. return $res->execute($values);
  182. } catch (PDOException $e) {
  183. echo 'Query error: ' . $e->getMessage();
  184. die();
  185. }
  186. }
  187. ?>