Album.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 title, url FROM lychee_photos WHERE public = '1';";
  167. $zipTitle = 'Public';
  168. break;
  169. case 'f':
  170. $photos = "SELECT title, url FROM lychee_photos WHERE star = '1';";
  171. $zipTitle = 'Starred';
  172. break;
  173. default:
  174. $photos = "SELECT title, url FROM lychee_photos WHERE album = '$this->albumIDs';";
  175. $zipTitle = 'Unsorted';
  176. }
  177. # Set title
  178. $album = $this->database->query("SELECT title FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  179. if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) $zipTitle = $album->fetch_object()->title;
  180. $filename = LYCHEE_DATA . $zipTitle . '.zip';
  181. # Create zip
  182. $zip = new ZipArchive();
  183. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false;
  184. # Execute query
  185. $photos = $this->database->query($photos);
  186. # Check if album empty
  187. if ($photos->num_rows==0) return false;
  188. # Parse each path
  189. $files = array();
  190. while ($photo = $photos->fetch_object()) {
  191. # Parse url
  192. $photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
  193. # Parse title
  194. $badChars = array_merge(
  195. array_map('chr', range(0,31)),
  196. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  197. );
  198. $photo->title = str_replace($badChars, '', $photo->title);
  199. if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
  200. # Check if readable
  201. if (!@is_readable($photo->url)) continue;
  202. # Get extension of image
  203. $extension = array_reverse(explode('.', $photo->url));
  204. $extension = $extension[0];
  205. # Set title for photo
  206. $zipFileName = $zipTitle . '/' . $photo->title . '.' . $extension;
  207. # Check for duplicates
  208. if (!empty($files)) {
  209. $i = 1;
  210. while (in_array($zipFileName, $files)) {
  211. # Set new title for photo
  212. $zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . '.' . $extension;
  213. $i++;
  214. }
  215. }
  216. # Add to array
  217. $files[] = $zipFileName;
  218. # Add photo to zip
  219. $zip->addFile($photo->url, $zipFileName);
  220. }
  221. # Finish zip
  222. $zip->close();
  223. # Send zip
  224. header("Content-Type: application/zip");
  225. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  226. header("Content-Length: ".filesize($filename));
  227. readfile($filename);
  228. # Delete zip
  229. unlink($filename);
  230. # Call plugins
  231. $this->plugins(__METHOD__, 1, func_get_args());
  232. return true;
  233. }
  234. public function setTitle($title = 'Untitled') {
  235. if (!isset($this->database, $this->albumIDs)) return false;
  236. # Call plugins
  237. $this->plugins(__METHOD__, 0, func_get_args());
  238. # Parse
  239. if (strlen($title)>50) $title = substr($title, 0, 50);
  240. # Execute query
  241. $result = $this->database->query("UPDATE lychee_albums SET title = '$title' WHERE id IN ($this->albumIDs);");
  242. # Call plugins
  243. $this->plugins(__METHOD__, 1, func_get_args());
  244. if (!$result) return false;
  245. return true;
  246. }
  247. public function setDescription($description = '') {
  248. if (!isset($this->database, $this->albumIDs)) return false;
  249. # Call plugins
  250. $this->plugins(__METHOD__, 0, func_get_args());
  251. # Parse
  252. $description = htmlentities($description);
  253. if (strlen($description)>1000) return false;
  254. # Execute query
  255. $result = $this->database->query("UPDATE lychee_albums SET description = '$description' WHERE id IN ($this->albumIDs);");
  256. # Call plugins
  257. $this->plugins(__METHOD__, 1, func_get_args());
  258. if (!$result) return false;
  259. return true;
  260. }
  261. public function getPublic() {
  262. if (!isset($this->database, $this->albumIDs)) return false;
  263. # Call plugins
  264. $this->plugins(__METHOD__, 0, func_get_args());
  265. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
  266. # Execute query
  267. $albums = $this->database->query("SELECT public FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  268. $album = $albums->fetch_object();
  269. # Call plugins
  270. $this->plugins(__METHOD__, 1, func_get_args());
  271. if ($album->public==1) return true;
  272. return false;
  273. }
  274. public function setPublic($password) {
  275. if (!isset($this->database, $this->albumIDs)) return false;
  276. # Call plugins
  277. $this->plugins(__METHOD__, 0, func_get_args());
  278. # Get public
  279. $albums = $this->database->query("SELECT id, public FROM lychee_albums WHERE id IN ('$this->albumIDs');");
  280. while ($album = $albums->fetch_object()) {
  281. # Invert public
  282. $public = ($album->public=='0' ? 1 : 0);
  283. # Set public
  284. $result = $this->database->query("UPDATE lychee_albums SET public = '$public', password = NULL WHERE id = '$album->id';");
  285. if (!$result) return false;
  286. # Reset permissions for photos
  287. if ($public===1) {
  288. $result = $this->database->query("UPDATE lychee_photos SET public = 0 WHERE album = '$album->id';");
  289. if (!$result) return false;
  290. }
  291. }
  292. # Call plugins
  293. $this->plugins(__METHOD__, 1, func_get_args());
  294. # Set password
  295. if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
  296. return true;
  297. }
  298. public function setPassword($password) {
  299. if (!isset($this->database, $this->albumIDs)) return false;
  300. # Call plugins
  301. $this->plugins(__METHOD__, 0, func_get_args());
  302. # Execute query
  303. $result = $this->database->query("UPDATE lychee_albums SET password = '$password' WHERE id IN ('$this->albumIDs');");
  304. # Call plugins
  305. $this->plugins(__METHOD__, 1, func_get_args());
  306. if (!$result) return false;
  307. return true;
  308. }
  309. public function checkPassword($password) {
  310. if (!isset($this->database, $this->albumIDs)) return false;
  311. # Call plugins
  312. $this->plugins(__METHOD__, 0, func_get_args());
  313. # Execute query
  314. $albums = $this->database->query("SELECT password FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  315. $album = $albums->fetch_object();
  316. # Call plugins
  317. $this->plugins(__METHOD__, 1, func_get_args());
  318. if ($album->password=='') return true;
  319. else if ($album->password===$password) return true;
  320. return false;
  321. }
  322. public function delete($albumIDs) {
  323. if (!isset($this->database, $this->albumIDs)) return false;
  324. # Call plugins
  325. $this->plugins(__METHOD__, 0, func_get_args());
  326. # Init vars
  327. $error = false;
  328. # Execute query
  329. $photos = $this->database->query("SELECT id FROM lychee_photos WHERE album IN ($albumIDs);");
  330. # For each album delete photo
  331. while ($row = $photos->fetch_object()) {
  332. $photo = new Photo($this->database, $this->plugins, null, $row->id);
  333. if (!$photo->delete($row->id)) $error = true;
  334. }
  335. # Delete albums
  336. $result = $this->database->query("DELETE FROM lychee_albums WHERE id IN ($albumIDs);");
  337. # Call plugins
  338. $this->plugins(__METHOD__, 1, func_get_args());
  339. if ($error||!$result) return false;
  340. return true;
  341. }
  342. }