Album.php 22 KB

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