Album.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. ###
  3. # @name Album Module
  4. # @author Tobias Reich
  5. # @copyright 2014 by Tobias Reich
  6. ###
  7. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  8. class Album extends Module {
  9. private $database = null;
  10. private $settings = null;
  11. private $albumIDs = null;
  12. public function __construct($database, $plugins, $settings, $albumIDs) {
  13. # Init vars
  14. $this->database = $database;
  15. $this->plugins = $plugins;
  16. $this->settings = $settings;
  17. $this->albumIDs = $albumIDs;
  18. return true;
  19. }
  20. public function add($title = 'Untitled', $public = 0, $visible = 1) {
  21. if (!isset($this->database)) return false;
  22. # Call plugins
  23. $this->plugins(__METHOD__, 0, func_get_args());
  24. # Parse
  25. if (strlen($title)>50) $title = substr($title, 0, 50);
  26. # Database
  27. $sysstamp = time();
  28. $result = $this->database->query("INSERT INTO lychee_albums (title, sysstamp, public, visible) VALUES ('$title', '$sysstamp', '$public', '$visible');");
  29. # Call plugins
  30. $this->plugins(__METHOD__, 1, func_get_args());
  31. if (!$result) return false;
  32. return $this->database->insert_id;
  33. }
  34. public function get() {
  35. if (!isset($this->database, $this->settings, $this->albumIDs)) return false;
  36. # Call plugins
  37. $this->plugins(__METHOD__, 0, func_get_args());
  38. # Get album information
  39. switch($this->albumIDs) {
  40. case 'f': $return['public'] = false;
  41. $query = "SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE star = 1 " . $this->settings['sorting'];
  42. break;
  43. case 's': $return['public'] = false;
  44. $query = "SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE public = 1 " . $this->settings['sorting'];
  45. break;
  46. case '0': $return['public'] = false;
  47. $query = "SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE album = 0 " . $this->settings['sorting'];
  48. break;
  49. default: $albums = $this->database->query("SELECT * FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  50. $return = $albums->fetch_assoc();
  51. $return['sysdate'] = date('d M. Y', $return['sysstamp']);
  52. $return['password'] = ($return['password']=='' ? false : true);
  53. $query = "SELECT id, title, tags, public, star, album, thumbUrl FROM lychee_photos WHERE album = '$this->albumIDs' " . $this->settings['sorting'];
  54. break;
  55. }
  56. # Get photos
  57. $photos = $this->database->query($query);
  58. $previousPhotoID = '';
  59. while($photo = $photos->fetch_assoc()) {
  60. # Parse
  61. $photo['sysdate'] = date('d F Y', substr($photo['id'], 0, -4));
  62. $photo['previousPhoto'] = $previousPhotoID;
  63. $photo['nextPhoto'] = '';
  64. if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id'];
  65. $previousPhotoID = $photo['id'];
  66. # Add to return
  67. $return['content'][$photo['id']] = $photo;
  68. }
  69. if ($photos->num_rows===0) {
  70. # Album empty
  71. $return['content'] = false;
  72. } else {
  73. # Enable next and previous for the first and last photo
  74. $lastElement = end($return['content']);
  75. $lastElementId = $lastElement['id'];
  76. $firstElement = reset($return['content']);
  77. $firstElementId = $firstElement['id'];
  78. if ($lastElementId!==$firstElementId) {
  79. $return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
  80. $return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
  81. }
  82. }
  83. $return['id'] = $this->albumIDs;
  84. $return['num'] = $photos->num_rows;
  85. # Call plugins
  86. $this->plugins(__METHOD__, 1, func_get_args());
  87. return $return;
  88. }
  89. public function getAll($public) {
  90. if (!isset($this->database, $this->settings, $public)) return false;
  91. # Call plugins
  92. $this->plugins(__METHOD__, 0, func_get_args());
  93. # Get SmartAlbums
  94. if ($public===false) $return = $this->getSmartInfo();
  95. # Albums query
  96. $query = 'SELECT id, title, public, sysstamp, password FROM lychee_albums WHERE public = 1 AND visible <> 0';
  97. if ($public===false) $query = 'SELECT id, title, public, sysstamp, password FROM lychee_albums';
  98. # Execute query
  99. $albums = $this->database->query($query) OR exit('Error: ' . $this->database->error);
  100. # For each album
  101. while ($album = $albums->fetch_assoc()) {
  102. # Parse info
  103. $album['sysdate'] = date('F Y', $album['sysstamp']);
  104. $album['password'] = ($album['password'] != '');
  105. # Thumbs
  106. if (($public===true&&$album['password']===false)||($public===false)) {
  107. # Execute query
  108. $thumbs = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE album = '" . $album['id'] . "' ORDER BY star DESC, " . substr($this->settings['sorting'], 9) . " LIMIT 3");
  109. # For each thumb
  110. $k = 0;
  111. while ($thumb = $thumbs->fetch_object()) {
  112. $album["thumb$k"] = $thumb->thumbUrl;
  113. $k++;
  114. }
  115. }
  116. # Add to return
  117. $return['content'][$album['id']] = $album;
  118. }
  119. # Num of albums
  120. $return['num'] = $albums->num_rows;
  121. # Call plugins
  122. $this->plugins(__METHOD__, 1, func_get_args());
  123. return $return;
  124. }
  125. private function getSmartInfo() {
  126. if (!isset($this->database, $this->settings)) return false;
  127. # Unsorted
  128. $unsorted = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE album = 0 " . $this->settings['sorting']);
  129. $i = 0;
  130. while($row = $unsorted->fetch_object()) {
  131. if ($i<3) {
  132. $return["unsortedThumb$i"] = $row->thumbUrl;
  133. $i++;
  134. } else break;
  135. }
  136. $return['unsortedNum'] = $unsorted->num_rows;
  137. # Public
  138. $public = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE public = 1 " . $this->settings['sorting']);
  139. $i = 0;
  140. while($row2 = $public->fetch_object()) {
  141. if ($i<3) {
  142. $return["publicThumb$i"] = $row2->thumbUrl;
  143. $i++;
  144. } else break;
  145. }
  146. $return['publicNum'] = $public->num_rows;
  147. # Starred
  148. $starred = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE star = 1 " . $this->settings['sorting']);
  149. $i = 0;
  150. while($row3 = $starred->fetch_object()) {
  151. if ($i<3) {
  152. $return["starredThumb$i"] = $row3->thumbUrl;
  153. $i++;
  154. } else break;
  155. }
  156. $return['starredNum'] = $starred->num_rows;
  157. return $return;
  158. }
  159. public function getArchive() {
  160. if (!isset($this->database, $this->albumIDs)) return false;
  161. # Call plugins
  162. $this->plugins(__METHOD__, 0, func_get_args());
  163. # Photos query
  164. switch($this->albumIDs) {
  165. case 's':
  166. $photos = "SELECT url FROM lychee_photos WHERE public = '1';";
  167. $zipTitle = 'Public';
  168. break;
  169. case 'f':
  170. $photos = "SELECT url FROM lychee_photos WHERE star = '1';";
  171. $zipTitle = 'Starred';
  172. break;
  173. default:
  174. $photos = "SELECT url FROM lychee_photos WHERE album = '$this->albumIDs';";
  175. $zipTitle = 'Unsorted';
  176. }
  177. # Execute query
  178. $photos = $this->database->query($photos);
  179. # Init vars
  180. $zip = new ZipArchive();
  181. $files = array();
  182. $i = 0;
  183. # Parse each url
  184. while ($photo = $photos->fetch_object()) {
  185. $files[$i] = __DIR__ . '/../../uploads/big/' . $photo->url;
  186. $i++;
  187. }
  188. # Set title
  189. $album = $this->database->query("SELECT title FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  190. if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) $zipTitle = $album->fetch_object()->title;
  191. # Create zip
  192. $filename = __DIR__ . "/../../data/$zipTitle.zip";
  193. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false;
  194. # Add each photo
  195. foreach ($files AS $file) {
  196. $newFile = explode('/', $file);
  197. $newFile = array_reverse($newFile);
  198. $zip->addFile($file, $zipTitle . '/' . $newFile[0]);
  199. }
  200. # Finish zip
  201. $zip->close();
  202. # Send zip
  203. header("Content-Type: application/zip");
  204. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  205. header("Content-Length: ".filesize($filename));
  206. readfile($filename);
  207. # Delete zip
  208. unlink($filename);
  209. # Call plugins
  210. $this->plugins(__METHOD__, 1, func_get_args());
  211. return true;
  212. }
  213. public function setTitle($title = 'Untitled') {
  214. if (!isset($this->database, $this->albumIDs)) return false;
  215. # Call plugins
  216. $this->plugins(__METHOD__, 0, func_get_args());
  217. # Parse
  218. if (strlen($title)>50) $title = substr($title, 0, 50);
  219. # Execute query
  220. $result = $this->database->query("UPDATE lychee_albums SET title = '$title' WHERE id IN ($this->albumIDs);");
  221. # Call plugins
  222. $this->plugins(__METHOD__, 1, func_get_args());
  223. if (!$result) return false;
  224. return true;
  225. }
  226. public function setDescription($description = '') {
  227. if (!isset($this->database, $this->albumIDs)) return false;
  228. # Call plugins
  229. $this->plugins(__METHOD__, 0, func_get_args());
  230. # Parse
  231. $description = htmlentities($description);
  232. if (strlen($description)>1000) return false;
  233. # Execute query
  234. $result = $this->database->query("UPDATE lychee_albums SET description = '$description' WHERE id IN ($this->albumIDs);");
  235. # Call plugins
  236. $this->plugins(__METHOD__, 1, func_get_args());
  237. if (!$result) return false;
  238. return true;
  239. }
  240. public function getPublic() {
  241. if (!isset($this->database, $this->albumIDs)) return false;
  242. # Call plugins
  243. $this->plugins(__METHOD__, 0, func_get_args());
  244. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
  245. # Execute query
  246. $albums = $this->database->query("SELECT public FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  247. $album = $albums->fetch_object();
  248. # Call plugins
  249. $this->plugins(__METHOD__, 1, func_get_args());
  250. if ($album->public==1) return true;
  251. return false;
  252. }
  253. public function setPublic($password) {
  254. if (!isset($this->database, $this->albumIDs)) return false;
  255. # Call plugins
  256. $this->plugins(__METHOD__, 0, func_get_args());
  257. # Get public
  258. $albums = $this->database->query("SELECT id, public FROM lychee_albums WHERE id IN ('$this->albumIDs');");
  259. while ($album = $albums->fetch_object()) {
  260. # Invert public
  261. $public = ($album->public=='0' ? 1 : 0);
  262. # Set public
  263. $result = $this->database->query("UPDATE lychee_albums SET public = '$public', password = NULL WHERE id = '$album->id';");
  264. if (!$result) return false;
  265. # Reset permissions for photos
  266. if ($public===1) {
  267. $result = $this->database->query("UPDATE lychee_photos SET public = 0 WHERE album = '$album->id';");
  268. if (!$result) return false;
  269. }
  270. }
  271. # Call plugins
  272. $this->plugins(__METHOD__, 1, func_get_args());
  273. # Set password
  274. if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
  275. return true;
  276. }
  277. public function setPassword($password) {
  278. if (!isset($this->database, $this->albumIDs)) return false;
  279. # Call plugins
  280. $this->plugins(__METHOD__, 0, func_get_args());
  281. # Execute query
  282. $result = $this->database->query("UPDATE lychee_albums SET password = '$password' WHERE id IN ('$this->albumIDs');");
  283. # Call plugins
  284. $this->plugins(__METHOD__, 1, func_get_args());
  285. if (!$result) return false;
  286. return true;
  287. }
  288. public function checkPassword($password) {
  289. if (!isset($this->database, $this->albumIDs)) return false;
  290. # Call plugins
  291. $this->plugins(__METHOD__, 0, func_get_args());
  292. # Execute query
  293. $albums = $this->database->query("SELECT password FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  294. $album = $albums->fetch_object();
  295. # Call plugins
  296. $this->plugins(__METHOD__, 1, func_get_args());
  297. if ($album->password=='') return true;
  298. else if ($album->password===$password) return true;
  299. return false;
  300. }
  301. public function delete($albumIDs) {
  302. if (!isset($this->database, $this->albumIDs)) return false;
  303. # Call plugins
  304. $this->plugins(__METHOD__, 0, func_get_args());
  305. # Init vars
  306. $error = false;
  307. # Execute query
  308. $photos = $this->database->query("SELECT id FROM lychee_photos WHERE album IN ($albumIDs);");
  309. # For each album delete photo
  310. while ($row = $photos->fetch_object()) {
  311. $photo = new Photo($this->database, $this->plugins, null, $row->id);
  312. if (!$photo->delete($row->id)) $error = true;
  313. }
  314. # Delete albums
  315. $result = $this->database->query("DELETE FROM lychee_albums WHERE id IN ($albumIDs);");
  316. # Call plugins
  317. $this->plugins(__METHOD__, 1, func_get_args());
  318. if ($error||!$result) return false;
  319. return true;
  320. }
  321. }