_album.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * @name Album Module
  4. * @author Philipp Maurer
  5. * @author Tobias Reich
  6. * @copyright 2014 by Philipp Maurer, Tobias Reich
  7. */
  8. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  9. function addAlbum($title = '', $public = 0, $visible = 1) {
  10. global $database;
  11. if (strlen($title)>50) $title = substr($title, 0, 50);
  12. $sysdate = date('d.m.Y');
  13. $result = $database->query("INSERT INTO lychee_albums (title, sysdate, public, visible) VALUES ('$title', '$sysdate', '$public', '$visible');");
  14. if (!$result) return false;
  15. return $database->insert_id;
  16. }
  17. function getAlbums($public) {
  18. global $database, $settings;
  19. // Smart Albums
  20. if ($public===false) $return = getSmartInfo();
  21. // Albums
  22. $query = "SELECT id, title, public, sysdate, password FROM lychee_albums WHERE public = 1 AND visible <> 0";
  23. if ($public===false) $query = "SELECT id, title, public, sysdate, password FROM lychee_albums";
  24. $result = $database->query($query) OR exit('Error: ' . $database->error);
  25. while ($row = $result->fetch_object()) {
  26. $album = array();
  27. // Info
  28. $album['id'] = $row->id;
  29. $album['title'] = $row->title;
  30. $album['public'] = $row->public;
  31. $album['sysdate'] = date('F Y', strtotime($row->sysdate));
  32. // Password
  33. $album['password'] = ($row->password != '');
  34. // Thumbs
  35. if (($public&&$row->password=='')||(!$public)) {
  36. $albumID = $row->id;
  37. $result2 = $database->query("SELECT thumbUrl FROM lychee_photos WHERE album = '$albumID' ORDER BY star DESC, " . substr($settings['sorting'], 9) . " LIMIT 0, 3");
  38. $k = 0;
  39. while ($row2 = $result2->fetch_object()) {
  40. $album["thumb$k"] = $row2->thumbUrl;
  41. $k++;
  42. }
  43. }
  44. $return['content'][$row->id] = $album;
  45. }
  46. $return['num'] = $result->num_rows;
  47. return $return;
  48. }
  49. function getSmartInfo() {
  50. global $database, $settings;
  51. // Unsorted
  52. $result = $database->query("SELECT thumbUrl FROM lychee_photos WHERE album = 0 " . $settings['sorting']);
  53. $i = 0;
  54. while($row = $result->fetch_object()) {
  55. if ($i<3) {
  56. $return["unsortedThumb$i"] = $row->thumbUrl;
  57. $i++;
  58. } else break;
  59. }
  60. $return['unsortedNum'] = $result->num_rows;
  61. // Public
  62. $result2 = $database->query("SELECT thumbUrl FROM lychee_photos WHERE public = 1 " . $settings['sorting']);
  63. $i = 0;
  64. while($row2 = $result2->fetch_object()) {
  65. if ($i<3) {
  66. $return["publicThumb$i"] = $row2->thumbUrl;
  67. $i++;
  68. } else break;
  69. }
  70. $return['publicNum'] = $result2->num_rows;
  71. // Starred
  72. $result3 = $database->query("SELECT thumbUrl FROM lychee_photos WHERE star = 1 " . $settings['sorting']);
  73. $i = 0;
  74. while($row3 = $result3->fetch_object()) {
  75. if ($i<3) {
  76. $return["starredThumb$i"] = $row3->thumbUrl;
  77. $i++;
  78. } else break;
  79. }
  80. $return['starredNum'] = $result3->num_rows;
  81. return $return;
  82. }
  83. function getAlbum($albumID) {
  84. global $database, $settings;
  85. // Get album information
  86. switch($albumID) {
  87. case 'f': $return['public'] = false;
  88. $query = "SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE star = 1 " . $settings['sorting'];
  89. break;
  90. case 's': $return['public'] = false;
  91. $query = "SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE public = 1 " . $settings['sorting'];
  92. break;
  93. case '0': $return['public'] = false;
  94. $query = "SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE album = 0 " . $settings['sorting'];
  95. break;
  96. default: $result = $database->query("SELECT * FROM lychee_albums WHERE id = '$albumID';");
  97. $row = $result->fetch_object();
  98. $return['title'] = $row->title;
  99. $return['description'] = $row->description;
  100. $return['sysdate'] = date('d M. Y', strtotime($row->sysdate));
  101. $return['public'] = $row->public;
  102. $return['password'] = ($row->password=='' ? false : true);
  103. $query = "SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE album = '$albumID' " . $settings['sorting'];
  104. break;
  105. }
  106. // Get photos
  107. $result = $database->query($query);
  108. $previousPhotoID = '';
  109. while($row = $result->fetch_assoc()) {
  110. $return['content'][$row['id']]['id'] = $row['id'];
  111. $return['content'][$row['id']]['title'] = $row['title'];
  112. $return['content'][$row['id']]['sysdate'] = date('d F Y', strtotime($row['sysdate']));
  113. $return['content'][$row['id']]['public'] = $row['public'];
  114. $return['content'][$row['id']]['star'] = $row['star'];
  115. $return['content'][$row['id']]['tags'] = $row['tags'];
  116. $return['content'][$row['id']]['album'] = $row['album'];
  117. $return['content'][$row['id']]['thumbUrl'] = $row['thumbUrl'];
  118. $return['content'][$row['id']]['previousPhoto'] = $previousPhotoID;
  119. $return['content'][$row['id']]['nextPhoto'] = '';
  120. if ($previousPhotoID!="") $return['content'][$previousPhotoID]['nextPhoto'] = $row['id'];
  121. $previousPhotoID = $row['id'];
  122. }
  123. if ($result->num_rows===0) {
  124. // Empty album
  125. $return['content'] = false;
  126. } else {
  127. // Enable next and previous for the first and last photo
  128. $lastElement = end($return['content']);
  129. $lastElementId = $lastElement['id'];
  130. $firstElement = reset($return['content']);
  131. $firstElementId = $firstElement['id'];
  132. if ($lastElementId!==$firstElementId) {
  133. $return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
  134. $return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
  135. }
  136. }
  137. $return['id'] = $albumID;
  138. $return['num'] = $result->num_rows;
  139. return $return;
  140. }
  141. function setAlbumTitle($albumIDs, $title) {
  142. global $database;
  143. if (strlen($title)>50) $title = substr($title, 0, 50);
  144. $result = $database->query("UPDATE lychee_albums SET title = '$title' WHERE id IN ($albumIDs);");
  145. if (!$result) return false;
  146. return true;
  147. }
  148. function setAlbumDescription($albumID, $description) {
  149. global $database;
  150. $description = htmlentities($description);
  151. if (strlen($description)>1000) return false;
  152. $result = $database->query("UPDATE lychee_albums SET description = '$description' WHERE id = '$albumID';");
  153. if (!$result) return false;
  154. return true;
  155. }
  156. function deleteAlbum($albumIDs) {
  157. global $database;
  158. $error = false;
  159. $result = $database->query("SELECT id FROM lychee_photos WHERE album IN ($albumIDs);");
  160. // Delete photos
  161. while ($row = $result->fetch_object())
  162. if (!deletePhoto($row->id)) $error = true;
  163. // Delete album
  164. $result = $database->query("DELETE FROM lychee_albums WHERE id IN ($albumIDs);");
  165. if ($error||!$result) return false;
  166. return true;
  167. }
  168. function getAlbumArchive($albumID) {
  169. global $database;
  170. switch($albumID) {
  171. case 's':
  172. $query = "SELECT url FROM lychee_photos WHERE public = '1';";
  173. $zipTitle = 'Public';
  174. break;
  175. case 'f':
  176. $query = "SELECT url FROM lychee_photos WHERE star = '1';";
  177. $zipTitle = 'Starred';
  178. break;
  179. default:
  180. $query = "SELECT url FROM lychee_photos WHERE album = '$albumID';";
  181. $zipTitle = 'Unsorted';
  182. }
  183. $zip = new ZipArchive();
  184. $result = $database->query($query);
  185. $files = array();
  186. $i = 0;
  187. while($row = $result->fetch_object()) {
  188. $files[$i] = '../uploads/big/' . $row->url;
  189. $i++;
  190. }
  191. $result = $database->query("SELECT title FROM lychee_albums WHERE id = '$albumID' LIMIT 1;");
  192. $row = $result->fetch_object();
  193. if ($albumID!=0&&is_numeric($albumID)) $zipTitle = $row->title;
  194. $filename = "../data/$zipTitle.zip";
  195. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  196. return false;
  197. }
  198. foreach($files AS $zipFile) {
  199. $newFile = explode('/',$zipFile);
  200. $newFile = array_reverse($newFile);
  201. $zip->addFile($zipFile, $zipTitle . '/' . $newFile[0]);
  202. }
  203. $zip->close();
  204. header("Content-Type: application/zip");
  205. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  206. header("Content-Length: ".filesize($filename));
  207. readfile($filename);
  208. unlink($filename);
  209. return true;
  210. }
  211. function setAlbumPublic($albumID, $password) {
  212. global $database;
  213. $result = $database->query("SELECT public FROM lychee_albums WHERE id = '$albumID' LIMIT 1;");
  214. $row = $result->fetch_object();
  215. $public = ($row->public=='0' ? 1 : 0);
  216. $result = $database->query("UPDATE lychee_albums SET public = '$public', password = NULL WHERE id = '$albumID';");
  217. if (!$result) return false;
  218. if ($public==1) {
  219. $result = $database->query("UPDATE lychee_photos SET public = 0 WHERE album = '$albumID';");
  220. if (!$result) return false;
  221. }
  222. if (strlen($password)>0) return setAlbumPassword($albumID, $password);
  223. return true;
  224. }
  225. function setAlbumPassword($albumID, $password) {
  226. global $database;
  227. $result = $database->query("UPDATE lychee_albums SET password = '$password' WHERE id = '$albumID';");
  228. if (!$result) return false;
  229. return true;
  230. }
  231. function checkAlbumPassword($albumID, $password) {
  232. global $database;
  233. $result = $database->query("SELECT password FROM lychee_albums WHERE id = '$albumID';");
  234. $row = $result->fetch_object();
  235. if ($row->password=='') return true;
  236. else if ($row->password==$password) return true;
  237. return false;
  238. }
  239. function isAlbumPublic($albumID) {
  240. global $database;
  241. if ($albumID==='0'||$albumID==='s'||$albumID==='f') return false;
  242. $result = $database->query("SELECT public FROM lychee_albums WHERE id = '$albumID';");
  243. $row = $result->fetch_object();
  244. if ($row->public==1) return true;
  245. return false;
  246. }
  247. ?>