Album.php 21 KB

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