Album.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. $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'] = date('F 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 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 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 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 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 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. if ($album===null) return false;
  189. // Check if album empty
  190. if ($photos->num_rows==0) {
  191. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive without images');
  192. return false;
  193. }
  194. // Parse each path
  195. $files = array();
  196. while ($photo = $photos->fetch_object()) {
  197. // Parse url
  198. $photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
  199. // Parse title
  200. $photo->title = str_replace($badChars, '', $photo->title);
  201. if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
  202. // Check if readable
  203. if (!@is_readable($photo->url)) continue;
  204. // Get extension of image
  205. $extension = getExtension($photo->url);
  206. // Set title for photo
  207. $zipFileName = $zipTitle . '/' . $photo->title . $extension;
  208. // Check for duplicates
  209. if (!empty($files)) {
  210. $i = 1;
  211. while (in_array($zipFileName, $files)) {
  212. // Set new title for photo
  213. $zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . $extension;
  214. $i++;
  215. }
  216. }
  217. // Add to array
  218. $files[] = $zipFileName;
  219. // Add photo to zip
  220. $zip->addFile($photo->url, $zipFileName);
  221. }
  222. // Finish zip
  223. $zip->close();
  224. // Send zip
  225. header("Content-Type: application/zip");
  226. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  227. header("Content-Length: " . filesize($filename));
  228. readfile($filename);
  229. // Delete zip
  230. unlink($filename);
  231. // Call plugins
  232. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  233. return true;
  234. }
  235. /**
  236. * @return boolean Returns true when successful.
  237. */
  238. public function setTitle($title = 'Untitled') {
  239. // Check dependencies
  240. Validator::required(isset($this->albumIDs), __METHOD__);
  241. // Call plugins
  242. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  243. // Execute query
  244. $query = Database::prepare(Database::get(), "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $title, $this->albumIDs));
  245. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  246. // Call plugins
  247. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  248. if ($result===false) return false;
  249. return true;
  250. }
  251. /**
  252. * @return boolean Returns true when successful.
  253. */
  254. public function setDescription($description = '') {
  255. // Check dependencies
  256. Validator::required(isset($this->albumIDs), __METHOD__);
  257. // Call plugins
  258. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  259. // Execute query
  260. $query = Database::prepare(Database::get(), "UPDATE ? SET description = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $description, $this->albumIDs));
  261. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  262. // Call plugins
  263. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  264. if ($result===false) return false;
  265. return true;
  266. }
  267. /**
  268. * @return boolean Returns true when the album is public.
  269. */
  270. public function getPublic() {
  271. // Check dependencies
  272. Validator::required(isset($this->albumIDs), __METHOD__);
  273. // Call plugins
  274. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  275. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
  276. // Execute query
  277. $query = Database::prepare(Database::get(), "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  278. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  279. if ($albums===false) return false;
  280. // Get album object
  281. $album = $albums->fetch_object();
  282. // Album not found?
  283. if ($album===null) {
  284. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
  285. return false;
  286. }
  287. // Call plugins
  288. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  289. if ($album->public==1) return true;
  290. return false;
  291. }
  292. /**
  293. * @return boolean Returns true when the album is downloadable.
  294. */
  295. public function getDownloadable() {
  296. // Check dependencies
  297. Validator::required(isset($this->albumIDs), __METHOD__);
  298. // Call plugins
  299. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  300. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f'||$this->albumIDs==='r') return false;
  301. // Execute query
  302. $query = Database::prepare(Database::get(), "SELECT downloadable FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  303. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  304. if ($albums===false) return false;
  305. // Get album object
  306. $album = $albums->fetch_object();
  307. // Album not found?
  308. if ($album===null) {
  309. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
  310. return false;
  311. }
  312. // Call plugins
  313. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  314. if ($album->downloadable==1) return true;
  315. return false;
  316. }
  317. /**
  318. * @return boolean Returns true when successful.
  319. */
  320. public function setPublic($public, $password, $visible, $downloadable) {
  321. // Check dependencies
  322. Validator::required(isset($this->albumIDs), __METHOD__);
  323. // Call plugins
  324. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  325. // Convert values
  326. $public = ($public==='1' ? 1 : 0);
  327. $visible = ($visible==='1' ? 1 : 0);
  328. $downloadable = ($downloadable==='1' ? 1 : 0);
  329. // Set public
  330. $query = Database::prepare(Database::get(), "UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $public, $visible, $downloadable, $this->albumIDs));
  331. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  332. if ($result===false) return false;
  333. // Reset permissions for photos
  334. if ($public===1) {
  335. $query = Database::prepare(Database::get(), "UPDATE ? SET public = 0 WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  336. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  337. if ($result===false) return false;
  338. }
  339. // Call plugins
  340. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  341. // Set password
  342. if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
  343. return true;
  344. }
  345. /**
  346. * @return boolean Returns true when successful.
  347. */
  348. private function setPassword($password) {
  349. // Check dependencies
  350. Validator::required(isset($this->albumIDs), __METHOD__);
  351. // Call plugins
  352. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  353. if (strlen($password)>0) {
  354. // Get hashed password
  355. $password = getHashedString($password);
  356. // Set hashed password
  357. // Do not prepare $password because it is hashed and save
  358. // Preparing (escaping) the password would destroy the hash
  359. $query = Database::prepare(Database::get(), "UPDATE ? SET password = '$password' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  360. } else {
  361. // Unset password
  362. $query = Database::prepare(Database::get(), "UPDATE ? SET password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  363. }
  364. // Execute query
  365. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  366. // Call plugins
  367. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  368. if ($result===false) return false;
  369. return true;
  370. }
  371. /**
  372. * @return boolean Returns when album is public.
  373. */
  374. public function checkPassword($password) {
  375. // Check dependencies
  376. Validator::required(isset($this->albumIDs), __METHOD__);
  377. // Call plugins
  378. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  379. // Execute query
  380. $query = Database::prepare(Database::get(), "SELECT password FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  381. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  382. if ($albums===false) return false;
  383. // Get album object
  384. $album = $albums->fetch_object();
  385. // Album not found?
  386. if ($album===null) {
  387. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
  388. return false;
  389. }
  390. // Call plugins
  391. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  392. // Check if password is correct
  393. if ($album->password=='') return true;
  394. if ($album->password===crypt($password, $album->password)) return true;
  395. return false;
  396. }
  397. /**
  398. * @return boolean Returns true when successful.
  399. */
  400. public function merge() {
  401. // Check dependencies
  402. Validator::required(isset($this->albumIDs), __METHOD__);
  403. // Call plugins
  404. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  405. // Convert to array
  406. $albumIDs = explode(',', $this->albumIDs);
  407. // Get first albumID
  408. $albumID = array_splice($albumIDs, 0, 1);
  409. $albumID = $albumID[0];
  410. $query = Database::prepare(Database::get(), "UPDATE ? SET album = ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->albumIDs));
  411. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  412. if ($result===false) return false;
  413. // $albumIDs contains all IDs without the first albumID
  414. // Convert to string
  415. $filteredIDs = implode(',', $albumIDs);
  416. $query = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $filteredIDs));
  417. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  418. // Call plugins
  419. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  420. if ($result===false) return false;
  421. return true;
  422. }
  423. /**
  424. * @return boolean Returns true when successful.
  425. */
  426. public function delete() {
  427. // Check dependencies
  428. Validator::required(isset($this->albumIDs), __METHOD__);
  429. // Call plugins
  430. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  431. // Init vars
  432. $photoIDs = array();
  433. // Execute query
  434. $query = Database::prepare(Database::get(), "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  435. $photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  436. if ($photos===false) return false;
  437. // Only delete photos when albums contain photos
  438. if ($photos->num_rows>0) {
  439. // Add each id to photoIDs
  440. while ($row = $photos->fetch_object()) $photoIDs[] = $row->id;
  441. // Convert photoIDs to a string
  442. $photoIDs = implode(',', $photoIDs);
  443. // Delete all photos
  444. $photo = new Photo($photoIDs);
  445. if ($photo->delete()!==true) return false;
  446. }
  447. // Delete albums
  448. $query = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  449. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  450. // Call plugins
  451. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  452. if ($result===false) return false;
  453. return true;
  454. }
  455. }
  456. ?>