album.php 9.7 KB

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