Album.php 22 KB

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