album.php 9.2 KB

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