Album.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <?php
  2. namespace Lychee\Modules;
  3. use ZipArchive;
  4. final class Album {
  5. private $albumIDs = null;
  6. /**
  7. * @return boolean Returns true when successful.
  8. */
  9. public function __construct($albumIDs) {
  10. // Init vars
  11. $this->albumIDs = $albumIDs;
  12. return true;
  13. }
  14. /**
  15. * @return string|false ID of the created album.
  16. */
  17. public function add($title = 'Untitled') {
  18. // Call plugins
  19. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  20. // Properties
  21. $id = generateID();
  22. $sysstamp = time();
  23. $public = 0;
  24. $visible = 1;
  25. // Database
  26. $query = Database::prepare(Database::get(), "INSERT INTO ? (id, title, sysstamp, public, visible) VALUES ('?', '?', '?', '?', '?')", array(LYCHEE_TABLE_ALBUMS, $id, $title, $sysstamp, $public, $visible));
  27. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  28. // Call plugins
  29. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  30. if ($result===false) return false;
  31. return $id;
  32. }
  33. /**
  34. * Rurns album-attributes into a front-end friendly format. Note that some attributes remain unchanged.
  35. * @return array Returns album-attributes in a normalized structure.
  36. */
  37. public static function prepareData(array $data) {
  38. // This function requires the following album-attributes and turns them
  39. // into a front-end friendly format: id, title, public, sysstamp, password
  40. // Note that some attributes remain unchanged
  41. // Init
  42. $album = null;
  43. // Set unchanged attributes
  44. $album['id'] = $data['id'];
  45. $album['title'] = $data['title'];
  46. $album['public'] = $data['public'];
  47. // Additional attributes
  48. // Only part of $album when available
  49. if (isset($data['description'])) $album['description'] = $data['description'];
  50. if (isset($data['visible'])) $album['visible'] = $data['visible'];
  51. if (isset($data['downloadable'])) $album['downloadable'] = $data['downloadable'];
  52. // Parse date
  53. $album['sysdate'] = strftime('%B %Y', $data['sysstamp']);
  54. // Parse password
  55. $album['password'] = ($data['password']=='' ? '0' : '1');
  56. // Parse thumbs or set default value
  57. $album['thumbs'] = (isset($data['thumbs']) ? explode(',', $data['thumbs']) : array());
  58. return $album;
  59. }
  60. /**
  61. * @return array|false Returns an array of photos and album information or false on failure.
  62. */
  63. public function get() {
  64. // Check dependencies
  65. Validator::required(isset($this->albumIDs), __METHOD__);
  66. // Call plugins
  67. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  68. // Get album information
  69. switch ($this->albumIDs) {
  70. case 'f':
  71. $return['public'] = '0';
  72. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE star = 1 " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  73. break;
  74. case 's':
  75. $return['public'] = '0';
  76. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE public = 1 " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  77. break;
  78. case 'r':
  79. $return['public'] = '0';
  80. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  81. break;
  82. case '0':
  83. $return['public'] = '0';
  84. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE album = 0 " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  85. break;
  86. default:
  87. $query = Database::prepare(Database::get(), "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  88. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  89. $return = $albums->fetch_assoc();
  90. $return = Album::prepareData($return);
  91. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE album = '?' " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  92. break;
  93. }
  94. // Get photos
  95. $photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  96. $previousPhotoID = '';
  97. if ($photos===false) return false;
  98. while ($photo = $photos->fetch_assoc()) {
  99. // Turn data from the database into a front-end friendly format
  100. $photo = Photo::prepareData($photo);
  101. // Set previous and next photoID for navigation purposes
  102. $photo['previousPhoto'] = $previousPhotoID;
  103. $photo['nextPhoto'] = '';
  104. // Set current photoID as nextPhoto of previous photo
  105. if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id'];
  106. $previousPhotoID = $photo['id'];
  107. // Add to return
  108. $return['content'][$photo['id']] = $photo;
  109. }
  110. if ($photos->num_rows===0) {
  111. // Album empty
  112. $return['content'] = false;
  113. } else {
  114. // Enable next and previous for the first and last photo
  115. $lastElement = end($return['content']);
  116. $lastElementId = $lastElement['id'];
  117. $firstElement = reset($return['content']);
  118. $firstElementId = $firstElement['id'];
  119. if ($lastElementId!==$firstElementId) {
  120. $return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
  121. $return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
  122. }
  123. }
  124. $return['id'] = $this->albumIDs;
  125. $return['num'] = $photos->num_rows;
  126. // Call plugins
  127. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  128. return $return;
  129. }
  130. /**
  131. * Starts a download of an album.
  132. * @return resource|boolean Sends a ZIP-file or returns false on failure.
  133. */
  134. public function getArchive() {
  135. // Check dependencies
  136. Validator::required(isset($this->albumIDs), __METHOD__);
  137. // Call plugins
  138. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  139. // Illicit chars
  140. $badChars = array_merge(
  141. array_map('chr', range(0,31)),
  142. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  143. );
  144. // Photos query
  145. switch($this->albumIDs) {
  146. case 's':
  147. $photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE public = 1', array(LYCHEE_TABLE_PHOTOS));
  148. $zipTitle = 'Public';
  149. break;
  150. case 'f':
  151. $photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE star = 1', array(LYCHEE_TABLE_PHOTOS));
  152. $zipTitle = 'Starred';
  153. break;
  154. case 'r':
  155. $photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY checksum', array(LYCHEE_TABLE_PHOTOS));
  156. $zipTitle = 'Recent';
  157. break;
  158. default:
  159. $photos = Database::prepare(Database::get(), "SELECT title, url FROM ? WHERE album = '?'", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  160. $zipTitle = 'Unsorted';
  161. }
  162. // Get title from database when album is not a SmartAlbum
  163. if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) {
  164. $query = Database::prepare(Database::get(), "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  165. $album = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  166. if ($album===false) return false;
  167. // Get album object
  168. $album = $album->fetch_object();
  169. // Album not found?
  170. if ($album===null) {
  171. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
  172. return false;
  173. }
  174. // Set title
  175. $zipTitle = $album->title;
  176. }
  177. // Escape title
  178. $zipTitle = str_replace($badChars, '', $zipTitle);
  179. $filename = LYCHEE_DATA . $zipTitle . '.zip';
  180. // Create zip
  181. $zip = new ZipArchive();
  182. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  183. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive');
  184. return false;
  185. }
  186. // Execute query
  187. $photos = Database::execute(Database::get(), $photos, __METHOD__, __LINE__);
  188. // Check if album empty
  189. if ($photos->num_rows==0) {
  190. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive without images');
  191. return false;
  192. }
  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. $photo->title = str_replace($badChars, '', $photo->title);
  200. if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
  201. // Check if readable
  202. if (!@is_readable($photo->url)) continue;
  203. // Get extension of image
  204. $extension = getExtension($photo->url, false);
  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. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  232. return true;
  233. }
  234. /**
  235. * @return boolean Returns true when successful.
  236. */
  237. public function setTitle($title = 'Untitled') {
  238. // Check dependencies
  239. Validator::required(isset($this->albumIDs), __METHOD__);
  240. // Call plugins
  241. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  242. // Execute query
  243. $query = Database::prepare(Database::get(), "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $title, $this->albumIDs));
  244. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  245. // Call plugins
  246. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  247. if ($result===false) return false;
  248. return true;
  249. }
  250. /**
  251. * @return boolean Returns true when successful.
  252. */
  253. public function setDescription($description = '') {
  254. // Check dependencies
  255. Validator::required(isset($this->albumIDs), __METHOD__);
  256. // Call plugins
  257. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  258. // Execute query
  259. $query = Database::prepare(Database::get(), "UPDATE ? SET description = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $description, $this->albumIDs));
  260. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  261. // Call plugins
  262. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  263. if ($result===false) return false;
  264. return true;
  265. }
  266. /**
  267. * @return boolean Returns true when the album is public.
  268. */
  269. public function getPublic() {
  270. // Check dependencies
  271. Validator::required(isset($this->albumIDs), __METHOD__);
  272. // Call plugins
  273. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  274. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
  275. // Execute query
  276. $query = Database::prepare(Database::get(), "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  277. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  278. if ($albums===false) return false;
  279. // Get album object
  280. $album = $albums->fetch_object();
  281. // Album not found?
  282. if ($album===null) {
  283. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
  284. return false;
  285. }
  286. // Call plugins
  287. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  288. if ($album->public==1) return true;
  289. return false;
  290. }
  291. /**
  292. * @return boolean Returns true when the album is downloadable.
  293. */
  294. public function getDownloadable() {
  295. // Check dependencies
  296. Validator::required(isset($this->albumIDs), __METHOD__);
  297. // Call plugins
  298. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  299. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f'||$this->albumIDs==='r') return false;
  300. // Execute query
  301. $query = Database::prepare(Database::get(), "SELECT downloadable FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  302. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  303. if ($albums===false) return false;
  304. // Get album object
  305. $album = $albums->fetch_object();
  306. // Album not found?
  307. if ($album===null) {
  308. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
  309. return false;
  310. }
  311. // Call plugins
  312. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  313. if ($album->downloadable==1) return true;
  314. return false;
  315. }
  316. /**
  317. * @return boolean Returns true when successful.
  318. */
  319. public function setPublic($public, $password, $visible, $downloadable) {
  320. // Check dependencies
  321. Validator::required(isset($this->albumIDs), __METHOD__);
  322. // Call plugins
  323. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  324. // Convert values
  325. $public = ($public==='1' ? 1 : 0);
  326. $visible = ($visible==='1' ? 1 : 0);
  327. $downloadable = ($downloadable==='1' ? 1 : 0);
  328. // Set public
  329. $query = Database::prepare(Database::get(), "UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $public, $visible, $downloadable, $this->albumIDs));
  330. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  331. if ($result===false) return false;
  332. // Reset permissions for photos
  333. if ($public===1) {
  334. $query = Database::prepare(Database::get(), "UPDATE ? SET public = 0 WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  335. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  336. if ($result===false) return false;
  337. }
  338. // Call plugins
  339. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  340. // Set password
  341. if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
  342. return true;
  343. }
  344. /**
  345. * @return boolean Returns true when successful.
  346. */
  347. private function setPassword($password) {
  348. // Check dependencies
  349. Validator::required(isset($this->albumIDs), __METHOD__);
  350. // Call plugins
  351. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  352. if (strlen($password)>0) {
  353. // Get hashed password
  354. $password = getHashedString($password);
  355. // Set hashed password
  356. // Do not prepare $password because it is hashed and save
  357. // Preparing (escaping) the password would destroy the hash
  358. $query = Database::prepare(Database::get(), "UPDATE ? SET password = '$password' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  359. } else {
  360. // Unset password
  361. $query = Database::prepare(Database::get(), "UPDATE ? SET password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  362. }
  363. // Execute query
  364. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  365. // Call plugins
  366. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  367. if ($result===false) return false;
  368. return true;
  369. }
  370. /**
  371. * @return boolean Returns when album is public.
  372. */
  373. public function checkPassword($password) {
  374. // Check dependencies
  375. Validator::required(isset($this->albumIDs), __METHOD__);
  376. // Call plugins
  377. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  378. // Execute query
  379. $query = Database::prepare(Database::get(), "SELECT password FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  380. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  381. if ($albums===false) return false;
  382. // Get album object
  383. $album = $albums->fetch_object();
  384. // Album not found?
  385. if ($album===null) {
  386. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
  387. return false;
  388. }
  389. // Call plugins
  390. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  391. // Check if password is correct
  392. if ($album->password=='') return true;
  393. if ($album->password===crypt($password, $album->password)) return true;
  394. return false;
  395. }
  396. /**
  397. * @return boolean Returns true when successful.
  398. */
  399. public function merge() {
  400. // Check dependencies
  401. Validator::required(isset($this->albumIDs), __METHOD__);
  402. // Call plugins
  403. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  404. // Convert to array
  405. $albumIDs = explode(',', $this->albumIDs);
  406. // Get first albumID
  407. $albumID = array_splice($albumIDs, 0, 1);
  408. $albumID = $albumID[0];
  409. $query = Database::prepare(Database::get(), "UPDATE ? SET album = ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->albumIDs));
  410. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  411. if ($result===false) return false;
  412. // $albumIDs contains all IDs without the first albumID
  413. // Convert to string
  414. $filteredIDs = implode(',', $albumIDs);
  415. $query = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $filteredIDs));
  416. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  417. // Call plugins
  418. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  419. if ($result===false) return false;
  420. return true;
  421. }
  422. /**
  423. * @return boolean Returns true when successful.
  424. */
  425. public function delete() {
  426. // Check dependencies
  427. Validator::required(isset($this->albumIDs), __METHOD__);
  428. // Call plugins
  429. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  430. // Init vars
  431. $photoIDs = array();
  432. // Execute query
  433. $query = Database::prepare(Database::get(), "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  434. $photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  435. if ($photos===false) return false;
  436. // Only delete photos when albums contain photos
  437. if ($photos->num_rows>0) {
  438. // Add each id to photoIDs
  439. while ($row = $photos->fetch_object()) $photoIDs[] = $row->id;
  440. // Convert photoIDs to a string
  441. $photoIDs = implode(',', $photoIDs);
  442. // Delete all photos
  443. $photo = new Photo($photoIDs);
  444. if ($photo->delete()!==true) return false;
  445. }
  446. // Delete albums
  447. $query = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  448. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  449. // Call plugins
  450. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  451. if ($result===false) return false;
  452. return true;
  453. }
  454. }
  455. ?>