db.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. $dir[] = $row;
  48. }
  49. return $dir;
  50. }
  51. function get_dir_count($dir) {
  52. global $pdo;
  53. $sql = 'SELECT id FROM dirs WHERE path = :dir';
  54. $values = array(
  55. ':dir' => $dir
  56. );
  57. try {
  58. $res = $pdo->prepare($sql);
  59. $res->execute($values);
  60. } catch (PDOException $e) {
  61. echo 'Query error: ' . $e->getMessage();
  62. die();
  63. }
  64. if(!($row = $res->fetch(PDO::FETCH_ASSOC))) {
  65. return 0;
  66. }
  67. $dirid = $row['id'];
  68. $sql = 'SELECT count(*) AS count FROM images WHERE dir = :dirid';
  69. $values = array(
  70. ':dirid' => $dirid
  71. );
  72. try {
  73. $res = $pdo->prepare($sql);
  74. $res->execute($values);
  75. } catch (PDOException $e) {
  76. echo 'Query error: ' . $e->getMessage();
  77. die();
  78. }
  79. if(!($row = $res->fetch(PDO::FETCH_ASSOC))) {
  80. return 0;
  81. }
  82. return $row['count'];
  83. }
  84. function update_dir_count($dirpath, $count) {
  85. global $pdo;
  86. $sql = 'UPDATE dirs SET count = :count WHERE path = :path';
  87. $values = array(
  88. ':path' => $dirpath,
  89. ':count' => $count,
  90. );
  91. try {
  92. $res = $pdo->prepare($sql);
  93. return $res->execute($values);
  94. } catch (PDOException $e) {
  95. echo 'Query error: ' . $e->getMessage();
  96. die();
  97. }
  98. }
  99. function get_dir_images($dir, $mode = '') {
  100. global $pdo;
  101. $images = [];
  102. $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';
  103. $values = array(
  104. ':path' => $dir
  105. );
  106. try {
  107. $res = $pdo->prepare($sql);
  108. $res->execute($values);
  109. } catch (PDOException $e) {
  110. echo 'Query error: ' . $e->getMessage();
  111. die();
  112. }
  113. while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
  114. if ($mode == 'viewcomments') {
  115. if (count_comments($row['id'], 'image') == 0)
  116. continue;
  117. }
  118. $images[] = $row;
  119. $dirname = $row['dir_name']; // probably should do this only once
  120. }
  121. return $images;
  122. }
  123. function make_dir_path($dir) {
  124. global $config;
  125. return $config['basepath'] . '/' . $dir['path'] . '/';
  126. }
  127. function create_db_dir($path, $name, $comment, $tags, $date) {
  128. global $pdo;
  129. $sql = 'INSERT INTO dirs (name, comment, tags, path, date, lastmodified) VALUES (:name, :comment, :tags, :path, :date, :currentdate)';
  130. $values = array(
  131. ':name' => $name,
  132. ':comment' => $comment,
  133. ':tags' => $tags,
  134. ':path' => $path,
  135. ':date' => $date,
  136. ':currentdate' => time()
  137. );
  138. try {
  139. $res = $pdo->prepare($sql);
  140. return $res->execute($values);
  141. } catch (PDOException $e) {
  142. echo 'Query error: ' . $e->getMessage();
  143. die();
  144. }
  145. }
  146. function update_db_dir($path, $name, $comment, $tags, $date) {
  147. global $pdo;
  148. $sql = 'UPDATE dirs SET name = :name, comment = :comment, tags = :tags, date = :date, lastmodified = :currentdate WHERE path = :path';
  149. $values = array(
  150. ':name' => $name,
  151. ':comment' => $comment,
  152. ':tags' => $tags,
  153. ':path' => $path,
  154. ':date' => $date,
  155. ':currentdate' => time()
  156. );
  157. try {
  158. $res = $pdo->prepare($sql);
  159. return $res->execute($values);
  160. } catch (PDOException $e) {
  161. echo 'Query error: ' . $e->getMessage();
  162. die();
  163. }
  164. }
  165. function add_db_image($dirid, $name, $comment, $tags, $path, $date) {
  166. global $pdo;
  167. $sql = 'INSERT INTO images (dir, name, comment, tags, path, date, lastmodified) VALUES (:dirid, :name, :comment, :tags, :path, :date, :currentdate)';
  168. $values = array(
  169. ':dirid' => $dirid,
  170. ':name' => $name,
  171. ':comment' => $comment,
  172. ':tags' => $tags,
  173. ':path' => $path,
  174. ':date' => $date,
  175. ':currentdate' => time()
  176. );
  177. try {
  178. $res = $pdo->prepare($sql);
  179. return $res->execute($values);
  180. } catch (PDOException $e) {
  181. echo 'Query error: ' . $e->getMessage();
  182. die();
  183. }
  184. }
  185. ?>