db.php 5.2 KB

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