db.php 5.2 KB

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