album.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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) {
  10. global $database;
  11. if (strlen($title)<1||strlen($title)>50) return false;
  12. $sysdate = date("d.m.Y");
  13. $result = $database->query("INSERT INTO lychee_albums (title, sysdate) VALUES ('$title', '$sysdate');");
  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) $return = getSmartInfo();
  21. // Albums
  22. if ($public) $query = "SELECT id, title, public, sysdate, password FROM lychee_albums WHERE public = 1";
  23. else $query = "SELECT id, title, public, sysdate, password FROM lychee_albums";
  24. $result = $database->query($query) OR exit("Error: $result <br>".$database->error);
  25. $i = 0;
  26. while($row = $result->fetch_object()) {
  27. // Info
  28. $return["content"][$row->id]['id'] = $row->id;
  29. $return["content"][$row->id]['title'] = $row->title;
  30. $return["content"][$row->id]['public'] = $row->public;
  31. $return["content"][$row->id]['sysdate'] = date('F Y', strtotime($row->sysdate));
  32. // Password
  33. if ($row->password=="") $return["content"][$row->id]['password'] = false;
  34. else $return["content"][$row->id]['password'] = true;
  35. // Thumbs
  36. if (($public&&$row->password=="")||(!$public)) {
  37. $albumID = $row->id;
  38. $result2 = $database->query("SELECT thumbUrl FROM lychee_photos WHERE album = '$albumID' ORDER BY star DESC, " . substr($settings['sorting'], 9) . " LIMIT 0, 3");
  39. $k = 0;
  40. while($row2 = $result2->fetch_object()){
  41. $return["content"][$row->id]["thumb$k"] = $row2->thumbUrl;
  42. $k++;
  43. }
  44. if (!isset($return["content"][$row->id]["thumb0"])) $return["content"][$row->id]["thumb0"] = "";
  45. if (!isset($return["content"][$row->id]["thumb1"])) $return["content"][$row->id]["thumb1"] = "";
  46. if (!isset($return["content"][$row->id]["thumb2"])) $return["content"][$row->id]["thumb2"] = "";
  47. }
  48. // Album count
  49. $i++;
  50. }
  51. $return["num"] = $i;
  52. return $return;
  53. }
  54. function getSmartInfo() {
  55. global $database, $settings;
  56. // Unsorted
  57. $result = $database->query("SELECT thumbUrl FROM lychee_photos WHERE album = 0 " . $settings['sorting']);
  58. $i = 0;
  59. while($row = $result->fetch_object()) {
  60. if ($i<3) $return["unsortedThumb$i"] = $row->thumbUrl;
  61. $i++;
  62. }
  63. $return['unsortedNum'] = $i;
  64. // Public
  65. $result2 = $database->query("SELECT thumbUrl FROM lychee_photos WHERE public = 1 " . $settings['sorting']);
  66. $i = 0;
  67. while($row2 = $result2->fetch_object()) {
  68. if ($i<3) $return["publicThumb$i"] = $row2->thumbUrl;
  69. $i++;
  70. }
  71. $return['publicNum'] = $i;
  72. // Starred
  73. $result3 = $database->query("SELECT thumbUrl FROM lychee_photos WHERE star = 1 " . $settings['sorting']);
  74. $i = 0;
  75. while($row3 = $result3->fetch_object()) {
  76. if ($i<3) $return["starredThumb$i"] = $row3->thumbUrl;
  77. $i++;
  78. }
  79. $return['starredNum'] = $i;
  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. $i = 0;
  109. while($row = $result->fetch_array()) {
  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. $i++;
  123. }
  124. if ($i==0) {
  125. // Empty album
  126. $return['content'] = false;
  127. } else {
  128. // Enable next and previous for the first and last photo
  129. $lastElement = end($return['content']);
  130. $lastElementId = $lastElement['id'];
  131. $firstElement = reset($return['content']);
  132. $firstElementId = $firstElement['id'];
  133. if ($lastElementId!==$firstElementId) {
  134. $return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
  135. $return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
  136. }
  137. }
  138. $return['id'] = $albumID;
  139. $return['num'] = $i;
  140. return $return;
  141. }
  142. function setAlbumTitle($albumIDs, $title) {
  143. global $database;
  144. if (strlen($title)<1||strlen($title)>50) return false;
  145. $result = $database->query("UPDATE lychee_albums SET title = '$title' WHERE id IN ($albumIDs);");
  146. if (!$result) return false;
  147. return true;
  148. }
  149. function setAlbumDescription($albumID, $description) {
  150. global $database;
  151. $description = htmlentities($description);
  152. if (strlen($description)>1000) return false;
  153. $result = $database->query("UPDATE lychee_albums SET description = '$description' WHERE id = '$albumID';");
  154. if (!$result) return false;
  155. return true;
  156. }
  157. function deleteAlbum($albumIDs) {
  158. global $database;
  159. $error = false;
  160. $result = $database->query("SELECT id FROM lychee_photos WHERE album IN ($albumIDs);");
  161. // Delete photos
  162. while ($row = $result->fetch_object())
  163. if (!deletePhoto($row->id)) $error = true;
  164. // Delete album
  165. $result = $database->query("DELETE FROM lychee_albums WHERE id IN ($albumIDs);");
  166. if ($error||!$result) return false;
  167. return true;
  168. }
  169. function getAlbumArchive($albumID) {
  170. global $database;
  171. switch($albumID) {
  172. case 's':
  173. $query = "SELECT url FROM lychee_photos WHERE public = '1';";
  174. $zipTitle = "Public";
  175. break;
  176. case 'f':
  177. $query = "SELECT url FROM lychee_photos WHERE star = '1';";
  178. $zipTitle = "Starred";
  179. break;
  180. default:
  181. $query = "SELECT url FROM lychee_photos WHERE album = '$albumID';";
  182. $zipTitle = "Unsorted";
  183. }
  184. $zip = new ZipArchive();
  185. $result = $database->query($query);
  186. $files = array();
  187. $i = 0;
  188. while($row = $result->fetch_object()) {
  189. $files[$i] = "../uploads/big/".$row->url;
  190. $i++;
  191. }
  192. $result = $database->query("SELECT title FROM lychee_albums WHERE id = '$albumID' LIMIT 1;");
  193. $row = $result->fetch_object();
  194. if ($albumID!=0&&is_numeric($albumID)) $zipTitle = $row->title;
  195. $filename = "../data/$zipTitle.zip";
  196. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  197. return false;
  198. }
  199. foreach($files AS $zipFile) {
  200. $newFile = explode("/",$zipFile);
  201. $newFile = array_reverse($newFile);
  202. $zip->addFile($zipFile, $zipTitle."/".$newFile[0]);
  203. }
  204. $zip->close();
  205. header("Content-Type: application/zip");
  206. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  207. header("Content-Length: ".filesize($filename));
  208. readfile($filename);
  209. unlink($filename);
  210. return true;
  211. }
  212. function setAlbumPublic($albumID, $password) {
  213. global $database;
  214. $result = $database->query("SELECT public FROM lychee_albums WHERE id = '$albumID';");
  215. $row = $result->fetch_object();
  216. $public = ($row->public=='0' ? 1 : 0);
  217. $result = $database->query("UPDATE lychee_albums SET public = '$public', password = NULL WHERE id = '$albumID';");
  218. if (!$result) return false;
  219. if ($public==1) {
  220. $result = $database->query("UPDATE lychee_photos SET public = 0 WHERE album = '$albumID';");
  221. if (!$result) return false;
  222. }
  223. if (strlen($password)>0) return setAlbumPassword($albumID, $password);
  224. return true;
  225. }
  226. function setAlbumPassword($albumID, $password) {
  227. global $database;
  228. $result = $database->query("UPDATE lychee_albums SET password = '$password' WHERE id = '$albumID';");
  229. if (!$result) return false;
  230. return true;
  231. }
  232. function checkAlbumPassword($albumID, $password) {
  233. global $database;
  234. $result = $database->query("SELECT password FROM lychee_albums WHERE id = '$albumID';");
  235. $row = $result->fetch_object();
  236. if ($row->password=="") return true;
  237. else if ($row->password==$password) return true;
  238. return false;
  239. }
  240. function isAlbumPublic($albumID) {
  241. global $database;
  242. $result = $database->query("SELECT public FROM lychee_albums WHERE id = '$albumID';");
  243. $row = $result->fetch_object();
  244. if ($albumID==='0'||$albumID==='s'||$albumID==='f') return false;
  245. if ($row->public==1) return true;
  246. return false;
  247. }
  248. ?>