Album.php 14 KB

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