Album.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 integer|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. $public = 0;
  22. $visible = 1;
  23. // Database
  24. $sysstamp = time();
  25. $query = Database::prepare(Database::get(), "INSERT INTO ? (title, sysstamp, public, visible) VALUES ('?', '?', '?', '?')", array(LYCHEE_TABLE_ALBUMS, $title, $sysstamp, $public, $visible));
  26. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  27. // Call plugins
  28. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  29. if ($result===false) return false;
  30. return Database::get()->insert_id;
  31. }
  32. /**
  33. * Rurns album-attributes into a front-end friendly format. Note that some attributes remain unchanged.
  34. * @return array Returns album-attributes in a normalized structure.
  35. */
  36. public static function prepareData(array $data) {
  37. // This function requires the following album-attributes and turns them
  38. // into a front-end friendly format: id, title, public, sysstamp, password
  39. // Note that some attributes remain unchanged
  40. // Init
  41. $album = null;
  42. // Set unchanged attributes
  43. $album['id'] = $data['id'];
  44. $album['title'] = $data['title'];
  45. $album['public'] = $data['public'];
  46. // Additional attributes
  47. // Only part of $album when available
  48. if (isset($data['description'])) $album['description'] = $data['description'];
  49. if (isset($data['visible'])) $album['visible'] = $data['visible'];
  50. if (isset($data['downloadable'])) $album['downloadable'] = $data['downloadable'];
  51. // Parse date
  52. $album['sysdate'] = date('F Y', $data['sysstamp']);
  53. // Parse password
  54. $album['password'] = ($data['password']=='' ? '0' : '1');
  55. // Parse thumbs or set default value
  56. $album['thumbs'] = (isset($data['thumbs']) ? explode(',', $data['thumbs']) : array());
  57. return $album;
  58. }
  59. /**
  60. * @return array|false Returns an array of photos and album information or false on failure.
  61. */
  62. public function get() {
  63. // Check dependencies
  64. Validator::required(isset($this->albumIDs), __METHOD__);
  65. // Call plugins
  66. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  67. // Get album information
  68. switch ($this->albumIDs) {
  69. case 'f':
  70. $return['public'] = '0';
  71. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE star = 1 " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  72. break;
  73. case 's':
  74. $return['public'] = '0';
  75. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE public = 1 " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  76. break;
  77. case 'r':
  78. $return['public'] = '0';
  79. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  80. break;
  81. case '0':
  82. $return['public'] = '0';
  83. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = 0 " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  84. break;
  85. default:
  86. $query = Database::prepare(Database::get(), "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  87. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  88. $return = $albums->fetch_assoc();
  89. $return = Album::prepareData($return);
  90. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = '?' " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  91. break;
  92. }
  93. // Get photos
  94. $photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  95. $previousPhotoID = '';
  96. if ($photos===false) return false;
  97. while ($photo = $photos->fetch_assoc()) {
  98. // Turn data from the database into a front-end friendly format
  99. $photo = Photo::prepareData($photo);
  100. // Set previous and next photoID for navigation purposes
  101. $photo['previousPhoto'] = $previousPhotoID;
  102. $photo['nextPhoto'] = '';
  103. // Set current photoID as nextPhoto of previous photo
  104. if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id'];
  105. $previousPhotoID = $photo['id'];
  106. // Add to return
  107. $return['content'][$photo['id']] = $photo;
  108. }
  109. if ($photos->num_rows===0) {
  110. // Album empty
  111. $return['content'] = false;
  112. } else {
  113. // Enable next and previous for the first and last photo
  114. $lastElement = end($return['content']);
  115. $lastElementId = $lastElement['id'];
  116. $firstElement = reset($return['content']);
  117. $firstElementId = $firstElement['id'];
  118. if ($lastElementId!==$firstElementId) {
  119. $return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
  120. $return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
  121. }
  122. }
  123. $return['id'] = $this->albumIDs;
  124. $return['num'] = $photos->num_rows;
  125. // Call plugins
  126. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  127. return $return;
  128. }
  129. /**
  130. * Starts a download of an album.
  131. * @return resource|boolean Sends a ZIP-file or returns false on failure.
  132. */
  133. public function getArchive() {
  134. // Check dependencies
  135. Validator::required(isset($this->albumIDs), __METHOD__);
  136. // Call plugins
  137. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  138. // Illicit chars
  139. $badChars = array_merge(
  140. array_map('chr', range(0,31)),
  141. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  142. );
  143. // Photos query
  144. switch($this->albumIDs) {
  145. case 's':
  146. $photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE public = 1', array(LYCHEE_TABLE_PHOTOS));
  147. $zipTitle = 'Public';
  148. break;
  149. case 'f':
  150. $photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE star = 1', array(LYCHEE_TABLE_PHOTOS));
  151. $zipTitle = 'Starred';
  152. break;
  153. case 'r':
  154. $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));
  155. $zipTitle = 'Recent';
  156. break;
  157. default:
  158. $photos = Database::prepare(Database::get(), "SELECT title, url FROM ? WHERE album = '?'", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  159. $zipTitle = 'Unsorted';
  160. }
  161. // Get title from database when album is not a SmartAlbum
  162. if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) {
  163. $query = Database::prepare(Database::get(), "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  164. $album = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  165. if ($album===false) return false;
  166. // Get album object
  167. $album = $album->fetch_object();
  168. // Album not found?
  169. if ($album===null) {
  170. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
  171. return false;
  172. }
  173. // Set title
  174. $zipTitle = $album->title;
  175. }
  176. // Escape title
  177. $zipTitle = str_replace($badChars, '', $zipTitle);
  178. $filename = LYCHEE_DATA . $zipTitle . '.zip';
  179. // Create zip
  180. $zip = new ZipArchive();
  181. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  182. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive');
  183. return false;
  184. }
  185. // Execute query
  186. $photos = Database::execute(Database::get(), $photos, __METHOD__, __LINE__);
  187. if ($album===null) return false;
  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);
  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. ?>