Album.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. * @return array|false Returns an array of albums or false on failure.
  131. */
  132. public function getAll($public = true) {
  133. // Call plugins
  134. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  135. // Initialize return var
  136. $return = array(
  137. 'smartalbums' => null,
  138. 'albums' => null,
  139. 'num' => 0
  140. );
  141. // Get SmartAlbums
  142. if ($public===false) $return['smartalbums'] = $this->getSmartInfo();
  143. // Albums query
  144. if ($public===false) $query = Database::prepare(Database::get(), 'SELECT id, title, public, sysstamp, password FROM ? ' . Settings::get()['sortingAlbums'], array(LYCHEE_TABLE_ALBUMS));
  145. else $query = Database::prepare(Database::get(), 'SELECT id, title, public, sysstamp, password FROM ? WHERE public = 1 AND visible <> 0 ' . Settings::get()['sortingAlbums'], array(LYCHEE_TABLE_ALBUMS));
  146. // Execute query
  147. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  148. if ($albums===false) return false;
  149. // For each album
  150. while ($album = $albums->fetch_assoc()) {
  151. // Turn data from the database into a front-end friendly format
  152. $album = Album::prepareData($album);
  153. // Thumbs
  154. if (($public===true&&$album['password']==='0')||
  155. ($public===false)) {
  156. // Execute query
  157. $query = Database::prepare(Database::get(), "SELECT thumbUrl FROM ? WHERE album = '?' ORDER BY star DESC, " . substr(Settings::get()['sortingPhotos'], 9) . " LIMIT 3", array(LYCHEE_TABLE_PHOTOS, $album['id']));
  158. $thumbs = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  159. if ($thumbs===false) return false;
  160. // For each thumb
  161. $k = 0;
  162. while ($thumb = $thumbs->fetch_object()) {
  163. $album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
  164. $k++;
  165. }
  166. }
  167. // Add to return
  168. $return['albums'][] = $album;
  169. }
  170. // Num of albums
  171. $return['num'] = $albums->num_rows;
  172. // Call plugins
  173. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  174. return $return;
  175. }
  176. /**
  177. * @return array|false Returns an array of smart albums or false on failure.
  178. */
  179. private function getSmartInfo() {
  180. // Initialize return var
  181. $return = array(
  182. 'unsorted' => null,
  183. 'public' => null,
  184. 'starred' => null,
  185. 'recent' => null
  186. );
  187. /**
  188. * Unsorted
  189. */
  190. $query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE album = 0 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  191. $unsorted = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  192. $i = 0;
  193. if ($unsorted===false) return false;
  194. $return['unsorted'] = array(
  195. 'thumbs' => array(),
  196. 'num' => $unsorted->num_rows
  197. );
  198. while($row = $unsorted->fetch_object()) {
  199. if ($i<3) {
  200. $return['unsorted']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row->thumbUrl;
  201. $i++;
  202. } else break;
  203. }
  204. /**
  205. * Starred
  206. */
  207. $query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE star = 1 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  208. $starred = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  209. $i = 0;
  210. if ($starred===false) return false;
  211. $return['starred'] = array(
  212. 'thumbs' => array(),
  213. 'num' => $starred->num_rows
  214. );
  215. while($row3 = $starred->fetch_object()) {
  216. if ($i<3) {
  217. $return['starred']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
  218. $i++;
  219. } else break;
  220. }
  221. /**
  222. * Public
  223. */
  224. $query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE public = 1 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  225. $public = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  226. $i = 0;
  227. if ($public===false) return false;
  228. $return['public'] = array(
  229. 'thumbs' => array(),
  230. 'num' => $public->num_rows
  231. );
  232. while($row2 = $public->fetch_object()) {
  233. if ($i<3) {
  234. $return['public']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row2->thumbUrl;
  235. $i++;
  236. } else break;
  237. }
  238. /**
  239. * Recent
  240. */
  241. $query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  242. $recent = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  243. $i = 0;
  244. if ($recent===false) return false;
  245. $return['recent'] = array(
  246. 'thumbs' => array(),
  247. 'num' => $recent->num_rows
  248. );
  249. while($row3 = $recent->fetch_object()) {
  250. if ($i<3) {
  251. $return['recent']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
  252. $i++;
  253. } else break;
  254. }
  255. // Return SmartAlbums
  256. return $return;
  257. }
  258. /**
  259. * Starts a download of an album.
  260. * @return resource|boolean Sends a ZIP-file or returns false on failure.
  261. */
  262. public function getArchive() {
  263. // Check dependencies
  264. Validator::required(isset($this->albumIDs), __METHOD__);
  265. // Call plugins
  266. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  267. // Illicit chars
  268. $badChars = array_merge(
  269. array_map('chr', range(0,31)),
  270. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  271. );
  272. // Photos query
  273. switch($this->albumIDs) {
  274. case 's':
  275. $photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE public = 1', array(LYCHEE_TABLE_PHOTOS));
  276. $zipTitle = 'Public';
  277. break;
  278. case 'f':
  279. $photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE star = 1', array(LYCHEE_TABLE_PHOTOS));
  280. $zipTitle = 'Starred';
  281. break;
  282. case 'r':
  283. $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));
  284. $zipTitle = 'Recent';
  285. break;
  286. default:
  287. $photos = Database::prepare(Database::get(), "SELECT title, url FROM ? WHERE album = '?'", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  288. $zipTitle = 'Unsorted';
  289. }
  290. // Get title from database when album is not a SmartAlbum
  291. if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) {
  292. $query = Database::prepare(Database::get(), "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  293. $album = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  294. if ($album===false) return false;
  295. // Get album object
  296. $album = $album->fetch_object();
  297. // Photo not found
  298. if ($album===null) {
  299. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
  300. return false;
  301. }
  302. // Set title
  303. $zipTitle = $album->title;
  304. }
  305. // Escape title
  306. $zipTitle = str_replace($badChars, '', $zipTitle);
  307. $filename = LYCHEE_DATA . $zipTitle . '.zip';
  308. // Create zip
  309. $zip = new ZipArchive();
  310. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  311. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive');
  312. return false;
  313. }
  314. // Execute query
  315. $photos = Database::execute(Database::get(), $photos, __METHOD__, __LINE__);
  316. if ($album===null) return false;
  317. // Check if album empty
  318. if ($photos->num_rows==0) {
  319. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive without images');
  320. return false;
  321. }
  322. // Parse each path
  323. $files = array();
  324. while ($photo = $photos->fetch_object()) {
  325. // Parse url
  326. $photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
  327. // Parse title
  328. $photo->title = str_replace($badChars, '', $photo->title);
  329. if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
  330. // Check if readable
  331. if (!@is_readable($photo->url)) continue;
  332. // Get extension of image
  333. $extension = getExtension($photo->url);
  334. // Set title for photo
  335. $zipFileName = $zipTitle . '/' . $photo->title . $extension;
  336. // Check for duplicates
  337. if (!empty($files)) {
  338. $i = 1;
  339. while (in_array($zipFileName, $files)) {
  340. // Set new title for photo
  341. $zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . $extension;
  342. $i++;
  343. }
  344. }
  345. // Add to array
  346. $files[] = $zipFileName;
  347. // Add photo to zip
  348. $zip->addFile($photo->url, $zipFileName);
  349. }
  350. // Finish zip
  351. $zip->close();
  352. // Send zip
  353. header("Content-Type: application/zip");
  354. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  355. header("Content-Length: " . filesize($filename));
  356. readfile($filename);
  357. // Delete zip
  358. unlink($filename);
  359. // Call plugins
  360. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  361. return true;
  362. }
  363. /**
  364. * @return boolean Returns true when successful.
  365. */
  366. public function setTitle($title = 'Untitled') {
  367. // Check dependencies
  368. Validator::required(isset($this->albumIDs), __METHOD__);
  369. // Call plugins
  370. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  371. // Execute query
  372. $query = Database::prepare(Database::get(), "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $title, $this->albumIDs));
  373. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  374. // Call plugins
  375. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  376. if ($result===false) return false;
  377. return true;
  378. }
  379. /**
  380. * @return boolean Returns true when successful.
  381. */
  382. public function setDescription($description = '') {
  383. // Check dependencies
  384. Validator::required(isset($this->albumIDs), __METHOD__);
  385. // Call plugins
  386. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  387. // Execute query
  388. $query = Database::prepare(Database::get(), "UPDATE ? SET description = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $description, $this->albumIDs));
  389. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  390. // Call plugins
  391. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  392. if ($result===false) return false;
  393. return true;
  394. }
  395. /**
  396. * @return boolean Returns true when the album is public.
  397. */
  398. public function getPublic() {
  399. // Check dependencies
  400. Validator::required(isset($this->albumIDs), __METHOD__);
  401. // Call plugins
  402. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  403. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
  404. // Execute query
  405. $query = Database::prepare(Database::get(), "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  406. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  407. if ($albums===false) return false;
  408. // Get album object
  409. $album = $albums->fetch_object();
  410. // Call plugins
  411. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  412. if ($album->public==1) return true;
  413. return false;
  414. }
  415. /**
  416. * @return boolean Returns true when the album is downloadable.
  417. */
  418. public function getDownloadable() {
  419. // Check dependencies
  420. Validator::required(isset($this->albumIDs), __METHOD__);
  421. // Call plugins
  422. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  423. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f'||$this->albumIDs==='r') return false;
  424. // Execute query
  425. $query = Database::prepare(Database::get(), "SELECT downloadable FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  426. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  427. if ($albums===false) return false;
  428. // Get album object
  429. $album = $albums->fetch_object();
  430. // Call plugins
  431. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  432. if ($album->downloadable==1) return true;
  433. return false;
  434. }
  435. /**
  436. * @return boolean Returns true when successful.
  437. */
  438. public function setPublic($public, $password, $visible, $downloadable) {
  439. // Check dependencies
  440. Validator::required(isset($this->albumIDs), __METHOD__);
  441. // Call plugins
  442. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  443. // Convert values
  444. $public = ($public==='1' ? 1 : 0);
  445. $visible = ($visible==='1' ? 1 : 0);
  446. $downloadable = ($downloadable==='1' ? 1 : 0);
  447. // Set public
  448. $query = Database::prepare(Database::get(), "UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $public, $visible, $downloadable, $this->albumIDs));
  449. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  450. if ($result===false) return false;
  451. // Reset permissions for photos
  452. if ($public===1) {
  453. $query = Database::prepare(Database::get(), "UPDATE ? SET public = 0 WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  454. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  455. if ($result===false) return false;
  456. }
  457. // Call plugins
  458. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  459. // Set password
  460. if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
  461. return true;
  462. }
  463. /**
  464. * @return boolean Returns true when successful.
  465. */
  466. private function setPassword($password) {
  467. // Check dependencies
  468. Validator::required(isset($this->albumIDs), __METHOD__);
  469. // Call plugins
  470. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  471. if (strlen($password)>0) {
  472. // Get hashed password
  473. $password = getHashedString($password);
  474. // Set hashed password
  475. // Do not prepare $password because it is hashed and save
  476. // Preparing (escaping) the password would destroy the hash
  477. $query = Database::prepare(Database::get(), "UPDATE ? SET password = '$password' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  478. } else {
  479. // Unset password
  480. $query = Database::prepare(Database::get(), "UPDATE ? SET password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  481. }
  482. // Execute query
  483. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  484. // Call plugins
  485. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  486. if ($result===false) return false;
  487. return true;
  488. }
  489. /**
  490. * @return boolean Returns when album is public.
  491. */
  492. public function checkPassword($password) {
  493. // Check dependencies
  494. Validator::required(isset($this->albumIDs), __METHOD__);
  495. // Call plugins
  496. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  497. // Execute query
  498. $query = Database::prepare(Database::get(), "SELECT password FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  499. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  500. if ($albums===false) return false;
  501. // Get album object
  502. $album = $albums->fetch_object();
  503. // Call plugins
  504. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  505. // Check if password is correct
  506. if ($album->password=='') return true;
  507. if ($album->password===crypt($password, $album->password)) return true;
  508. return false;
  509. }
  510. /**
  511. * @return boolean Returns true when successful.
  512. */
  513. public function merge() {
  514. // Check dependencies
  515. Validator::required(isset($this->albumIDs), __METHOD__);
  516. // Call plugins
  517. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  518. // Convert to array
  519. $albumIDs = explode(',', $this->albumIDs);
  520. // Get first albumID
  521. $albumID = array_splice($albumIDs, 0, 1);
  522. $albumID = $albumID[0];
  523. $query = Database::prepare(Database::get(), "UPDATE ? SET album = ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->albumIDs));
  524. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  525. if ($result===false) return false;
  526. // $albumIDs contains all IDs without the first albumID
  527. // Convert to string
  528. $filteredIDs = implode(',', $albumIDs);
  529. $query = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $filteredIDs));
  530. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  531. // Call plugins
  532. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  533. if ($result===false) return false;
  534. return true;
  535. }
  536. /**
  537. * @return boolean Returns true when successful.
  538. */
  539. public function delete() {
  540. // Check dependencies
  541. Validator::required(isset($this->albumIDs), __METHOD__);
  542. // Call plugins
  543. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  544. // Init vars
  545. $photoIDs = array();
  546. // Execute query
  547. $query = Database::prepare(Database::get(), "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  548. $photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  549. if ($photos===false) return false;
  550. // Only delete photos when albums contain photos
  551. if ($photos->num_rows>0) {
  552. // Add each id to photoIDs
  553. while ($row = $photos->fetch_object()) $photoIDs[] = $row->id;
  554. // Convert photoIDs to a string
  555. $photoIDs = implode(',', $photoIDs);
  556. // Delete all photos
  557. $photo = new Photo($photoIDs);
  558. if ($photo->delete()!==true) return false;
  559. }
  560. // Delete albums
  561. $query = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  562. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  563. // Call plugins
  564. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  565. if ($result===false) return false;
  566. return true;
  567. }
  568. }
  569. ?>