Album.php 13 KB

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