Album.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. <?php
  2. ###
  3. # @name Album Module
  4. # @copyright 2015 by Tobias Reich
  5. ###
  6. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  7. class Album extends Module {
  8. private $database = null;
  9. private $settings = null;
  10. private $albumIDs = null;
  11. public function __construct($database, $plugins, $settings, $albumIDs) {
  12. # Init vars
  13. $this->database = $database;
  14. $this->plugins = $plugins;
  15. $this->settings = $settings;
  16. $this->albumIDs = $albumIDs;
  17. return true;
  18. }
  19. public function add($title = 'Untitled', $public = 0, $visible = 1) {
  20. # Check dependencies
  21. self::dependencies(isset($this->database));
  22. # Call plugins
  23. $this->plugins(__METHOD__, 0, func_get_args());
  24. # Parse
  25. if (strlen($title)>50) $title = substr($title, 0, 50);
  26. # Database
  27. $sysstamp = time();
  28. $query = Database::prepare($this->database, "INSERT INTO ? (title, sysstamp, public, visible) VALUES ('?', '?', '?', '?')", array(LYCHEE_TABLE_ALBUMS, $title, $sysstamp, $public, $visible));
  29. $result = $this->database->query($query);
  30. # Call plugins
  31. $this->plugins(__METHOD__, 1, func_get_args());
  32. if (!$result) {
  33. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  34. return false;
  35. }
  36. return $this->database->insert_id;
  37. }
  38. public static function prepareData($data) {
  39. # This function requires the following album-attributes and turns them
  40. # into a front-end friendly format: id, title, public, sysstamp, password
  41. # Note that some attributes remain unchanged
  42. # Check dependencies
  43. self::dependencies(isset($data));
  44. # Init
  45. $album = null;
  46. # Set unchanged attributes
  47. $album['id'] = $data['id'];
  48. $album['title'] = $data['title'];
  49. $album['public'] = $data['public'];
  50. # Additional attributes
  51. # Only part of $album when available
  52. if (isset($data['description'])) $album['description'] = $data['description'];
  53. if (isset($data['visible'])) $album['visible'] = $data['visible'];
  54. if (isset($data['downloadable'])) $album['downloadable'] = $data['downloadable'];
  55. # Parse date
  56. $album['sysdate'] = date('F Y', $data['sysstamp']);
  57. # Parse password
  58. $album['password'] = ($data['password']=='' ? '0' : '1');
  59. # Parse thumbs or set default value
  60. $album['thumbs'] = (isset($data['thumbs']) ? explode(',', $data['thumbs']) : array());
  61. return $album;
  62. }
  63. public function get() {
  64. # Check dependencies
  65. self::dependencies(isset($this->database, $this->settings, $this->albumIDs));
  66. # Call plugins
  67. $this->plugins(__METHOD__, 0, func_get_args());
  68. # Get album information
  69. switch ($this->albumIDs) {
  70. case 'f': $return['public'] = '0';
  71. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE star = 1 " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  72. break;
  73. case 's': $return['public'] = '0';
  74. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE public = 1 " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  75. break;
  76. case 'r': $return['public'] = '0';
  77. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  78. break;
  79. case '0': $return['public'] = '0';
  80. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = 0 " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  81. break;
  82. default: $query = Database::prepare($this->database, "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  83. $albums = $this->database->query($query);
  84. $return = $albums->fetch_assoc();
  85. $return = Album::prepareData($return);
  86. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = '?' " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  87. break;
  88. }
  89. # Get photos
  90. $photos = $this->database->query($query);
  91. $previousPhotoID = '';
  92. while ($photo = $photos->fetch_assoc()) {
  93. # Turn data from the database into a front-end friendly format
  94. $photo = Photo::prepareData($photo);
  95. # Set previous and next photoID for navigation purposes
  96. $photo['previousPhoto'] = $previousPhotoID;
  97. $photo['nextPhoto'] = '';
  98. # Set current photoID as nextPhoto of previous photo
  99. if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id'];
  100. $previousPhotoID = $photo['id'];
  101. # Add to return
  102. $return['content'][$photo['id']] = $photo;
  103. }
  104. if ($photos->num_rows===0) {
  105. # Album empty
  106. $return['content'] = false;
  107. } else {
  108. # Enable next and previous for the first and last photo
  109. $lastElement = end($return['content']);
  110. $lastElementId = $lastElement['id'];
  111. $firstElement = reset($return['content']);
  112. $firstElementId = $firstElement['id'];
  113. if ($lastElementId!==$firstElementId) {
  114. $return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
  115. $return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
  116. }
  117. }
  118. $return['id'] = $this->albumIDs;
  119. $return['num'] = $photos->num_rows;
  120. # Call plugins
  121. $this->plugins(__METHOD__, 1, func_get_args());
  122. return $return;
  123. }
  124. public function getAll($public) {
  125. # Check dependencies
  126. self::dependencies(isset($this->database, $this->settings, $public));
  127. # Call plugins
  128. $this->plugins(__METHOD__, 0, func_get_args());
  129. # Initialize return var
  130. $return = array(
  131. 'smartalbums' => null,
  132. 'albums' => null,
  133. 'num' => 0
  134. );
  135. # Get SmartAlbums
  136. if ($public===false) $return['smartalbums'] = $this->getSmartInfo();
  137. # Albums query
  138. if ($public===false) $query = Database::prepare($this->database, 'SELECT a.id, a.title, a.public, a.sysstamp, a.password, p.thumbs FROM (SELECT album, substring_index(GROUP_CONCAT(thumbUrl ORDER BY star DESC, ' . substr($this->settings['sortingPhotos'], 9) . '), ",", 3) AS thumbs FROM ? GROUP BY album) AS p RIGHT JOIN ? a ON p.album = a.id ' . $this->settings['sortingAlbums'], array(LYCHEE_TABLE_PHOTOS, LYCHEE_TABLE_ALBUMS));
  139. else $query = Database::prepare($this->database, 'SELECT a.id, a.title, a.public, a.sysstamp, a.password, p.thumbs FROM (SELECT album, substring_index(GROUP_CONCAT(thumbUrl ORDER BY star DESC, ' . substr($this->settings['sortingPhotos'], 9) . '), ",", 3) AS thumbs FROM ? GROUP BY album) AS p RIGHT JOIN ? a ON p.album = a.id WHERE public = 1 AND visible <> 0 ' . $this->settings['sortingAlbums'], array(LYCHEE_TABLE_PHOTOS, LYCHEE_TABLE_ALBUMS));
  140. # Execute query
  141. $albums = $this->database->query($query);
  142. if (!$albums) {
  143. Log::error($this->database, __METHOD__, __LINE__, 'Could not get all albums (' . $this->database->error . ')');
  144. exit('Error: ' . $this->database->error);
  145. }
  146. # For each album
  147. while ($album = $albums->fetch_assoc()) {
  148. # Turn data from the database into a front-end friendly format
  149. $album = Album::prepareData($album);
  150. # Thumbs
  151. if (($public===true&&$album['password']==='0')||
  152. ($public===false)) {
  153. $k = 0;
  154. foreach ($album['thumbs'] as $thumb) {
  155. $album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb;
  156. $k++;
  157. }
  158. }
  159. # Add to return
  160. $return['albums'][] = $album;
  161. }
  162. # Num of albums
  163. $return['num'] = $albums->num_rows;
  164. # Call plugins
  165. $this->plugins(__METHOD__, 1, func_get_args());
  166. return $return;
  167. }
  168. private function getSmartInfo() {
  169. # Check dependencies
  170. self::dependencies(isset($this->database, $this->settings));
  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($this->database, 'SELECT thumbUrl FROM ? WHERE album = 0 ' . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  182. $unsorted = $this->database->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($this->database, 'SELECT thumbUrl FROM ? WHERE star = 1 ' . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  198. $starred = $this->database->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($this->database, 'SELECT thumbUrl FROM ? WHERE public = 1 ' . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  214. $public = $this->database->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($this->database, 'SELECT thumbUrl FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) ' . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  230. $recent = $this->database->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. self::dependencies(isset($this->database, $this->albumIDs));
  248. # Call plugins
  249. $this->plugins(__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($this->database, 'SELECT title, url FROM ? WHERE public = 1', array(LYCHEE_TABLE_PHOTOS));
  259. $zipTitle = 'Public';
  260. break;
  261. case 'f':
  262. $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE star = 1', array(LYCHEE_TABLE_PHOTOS));
  263. $zipTitle = 'Starred';
  264. break;
  265. case 'r':
  266. $photos = Database::prepare($this->database, '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($this->database, "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($this->database, "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  276. $album = $this->database->query($query);
  277. # Error in database query
  278. if (!$album) {
  279. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  280. return false;
  281. }
  282. # Fetch object
  283. $album = $album->fetch_object();
  284. # Photo not found
  285. if ($album===null) {
  286. Log::error($this->database, __METHOD__, __LINE__, 'Album not found. Cannot start download.');
  287. return false;
  288. }
  289. # Set title
  290. $zipTitle = $album->title;
  291. }
  292. # Escape title
  293. $zipTitle = str_replace($badChars, '', $zipTitle);
  294. $filename = LYCHEE_DATA . $zipTitle . '.zip';
  295. # Create zip
  296. $zip = new ZipArchive();
  297. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  298. Log::error($this->database, __METHOD__, __LINE__, 'Could not create ZipArchive');
  299. return false;
  300. }
  301. # Execute query
  302. $photos = $this->database->query($photos);
  303. # Check if album empty
  304. if ($photos->num_rows==0) {
  305. Log::error($this->database, __METHOD__, __LINE__, 'Could not create ZipArchive without images');
  306. return false;
  307. }
  308. # Parse each path
  309. $files = array();
  310. while ($photo = $photos->fetch_object()) {
  311. # Parse url
  312. $photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
  313. # Parse title
  314. $photo->title = str_replace($badChars, '', $photo->title);
  315. if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
  316. # Check if readable
  317. if (!@is_readable($photo->url)) continue;
  318. # Get extension of image
  319. $extension = getExtension($photo->url);
  320. # Set title for photo
  321. $zipFileName = $zipTitle . '/' . $photo->title . $extension;
  322. # Check for duplicates
  323. if (!empty($files)) {
  324. $i = 1;
  325. while (in_array($zipFileName, $files)) {
  326. # Set new title for photo
  327. $zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . $extension;
  328. $i++;
  329. }
  330. }
  331. # Add to array
  332. $files[] = $zipFileName;
  333. # Add photo to zip
  334. $zip->addFile($photo->url, $zipFileName);
  335. }
  336. # Finish zip
  337. $zip->close();
  338. # Send zip
  339. header("Content-Type: application/zip");
  340. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  341. header("Content-Length: " . filesize($filename));
  342. readfile($filename);
  343. # Delete zip
  344. unlink($filename);
  345. # Call plugins
  346. $this->plugins(__METHOD__, 1, func_get_args());
  347. return true;
  348. }
  349. public function setTitle($title = 'Untitled') {
  350. # Check dependencies
  351. self::dependencies(isset($this->database, $this->albumIDs));
  352. # Call plugins
  353. $this->plugins(__METHOD__, 0, func_get_args());
  354. # Execute query
  355. $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $title, $this->albumIDs));
  356. $result = $this->database->query($query);
  357. # Call plugins
  358. $this->plugins(__METHOD__, 1, func_get_args());
  359. if (!$result) {
  360. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  361. return false;
  362. }
  363. return true;
  364. }
  365. public function setDescription($description = '') {
  366. # Check dependencies
  367. self::dependencies(isset($this->database, $this->albumIDs));
  368. # Call plugins
  369. $this->plugins(__METHOD__, 0, func_get_args());
  370. # Execute query
  371. $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $description, $this->albumIDs));
  372. $result = $this->database->query($query);
  373. # Call plugins
  374. $this->plugins(__METHOD__, 1, func_get_args());
  375. if (!$result) {
  376. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  377. return false;
  378. }
  379. return true;
  380. }
  381. public function getPublic() {
  382. # Check dependencies
  383. self::dependencies(isset($this->database, $this->albumIDs));
  384. # Call plugins
  385. $this->plugins(__METHOD__, 0, func_get_args());
  386. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
  387. # Execute query
  388. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  389. $albums = $this->database->query($query);
  390. $album = $albums->fetch_object();
  391. # Call plugins
  392. $this->plugins(__METHOD__, 1, func_get_args());
  393. if ($album->public==1) return true;
  394. return false;
  395. }
  396. public function getDownloadable() {
  397. # Check dependencies
  398. self::dependencies(isset($this->database, $this->albumIDs));
  399. # Call plugins
  400. $this->plugins(__METHOD__, 0, func_get_args());
  401. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f'||$this->albumIDs==='r') return false;
  402. # Execute query
  403. $query = Database::prepare($this->database, "SELECT downloadable FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  404. $albums = $this->database->query($query);
  405. $album = $albums->fetch_object();
  406. # Call plugins
  407. $this->plugins(__METHOD__, 1, func_get_args());
  408. if ($album->downloadable==1) return true;
  409. return false;
  410. }
  411. public function setPublic($public, $password, $visible, $downloadable) {
  412. # Check dependencies
  413. self::dependencies(isset($this->database, $this->albumIDs));
  414. # Call plugins
  415. $this->plugins(__METHOD__, 0, func_get_args());
  416. # Convert values
  417. $public = ($public==='1' ? 1 : 0);
  418. $visible = ($visible==='1' ? 1 : 0);
  419. $downloadable = ($downloadable==='1' ? 1 : 0);
  420. # Set public
  421. $query = Database::prepare($this->database, "UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $public, $visible, $downloadable, $this->albumIDs));
  422. $result = $this->database->query($query);
  423. if (!$result) {
  424. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  425. return false;
  426. }
  427. # Reset permissions for photos
  428. if ($public===1) {
  429. $query = Database::prepare($this->database, "UPDATE ? SET public = 0 WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  430. $result = $this->database->query($query);
  431. if (!$result) {
  432. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  433. return false;
  434. }
  435. }
  436. # Call plugins
  437. $this->plugins(__METHOD__, 1, func_get_args());
  438. # Set password
  439. if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
  440. return true;
  441. }
  442. private function setPassword($password) {
  443. # Check dependencies
  444. self::dependencies(isset($this->database, $this->albumIDs));
  445. # Call plugins
  446. $this->plugins(__METHOD__, 0, func_get_args());
  447. if (strlen($password)>0) {
  448. # Get hashed password
  449. $password = getHashedString($password);
  450. # Set hashed password
  451. # Do not prepare $password because it is hashed and save
  452. # Preparing (escaping) the password would destroy the hash
  453. $query = Database::prepare($this->database, "UPDATE ? SET password = '$password' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  454. } else {
  455. # Unset password
  456. $query = Database::prepare($this->database, "UPDATE ? SET password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  457. }
  458. # Execute query
  459. $result = $this->database->query($query);
  460. # Call plugins
  461. $this->plugins(__METHOD__, 1, func_get_args());
  462. if (!$result) {
  463. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  464. return false;
  465. }
  466. return true;
  467. }
  468. public function checkPassword($password) {
  469. # Check dependencies
  470. self::dependencies(isset($this->database, $this->albumIDs));
  471. # Call plugins
  472. $this->plugins(__METHOD__, 0, func_get_args());
  473. # Execute query
  474. $query = Database::prepare($this->database, "SELECT password FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  475. $albums = $this->database->query($query);
  476. $album = $albums->fetch_object();
  477. # Call plugins
  478. $this->plugins(__METHOD__, 1, func_get_args());
  479. if ($album->password=='') return true;
  480. else if ($album->password===crypt($password, $album->password)) return true;
  481. return false;
  482. }
  483. public function merge() {
  484. # Check dependencies
  485. self::dependencies(isset($this->database, $this->albumIDs));
  486. # Call plugins
  487. $this->plugins(__METHOD__, 0, func_get_args());
  488. # Convert to array
  489. $albumIDs = explode(',', $this->albumIDs);
  490. # Get first albumID
  491. $albumID = array_splice($albumIDs, 0, 1);
  492. $albumID = $albumID[0];
  493. $query = Database::prepare($this->database, "UPDATE ? SET album = ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->albumIDs));
  494. $result = $this->database->query($query);
  495. if (!$result) {
  496. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  497. return false;
  498. }
  499. # $albumIDs contains all IDs without the first albumID
  500. # Convert to string
  501. $filteredIDs = implode(',', $albumIDs);
  502. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $filteredIDs));
  503. $result = $this->database->query($query);
  504. # Call plugins
  505. $this->plugins(__METHOD__, 1, func_get_args());
  506. if (!$result) {
  507. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  508. return false;
  509. }
  510. return true;
  511. }
  512. public function delete() {
  513. # Check dependencies
  514. self::dependencies(isset($this->database, $this->albumIDs));
  515. # Call plugins
  516. $this->plugins(__METHOD__, 0, func_get_args());
  517. # Init vars
  518. $error = false;
  519. # Execute query
  520. $query = Database::prepare($this->database, "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  521. $photos = $this->database->query($query);
  522. # For each album delete photo
  523. while ($row = $photos->fetch_object()) {
  524. $photo = new Photo($this->database, $this->plugins, null, $row->id);
  525. if (!$photo->delete($row->id)) $error = true;
  526. }
  527. # Delete albums
  528. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  529. $result = $this->database->query($query);
  530. # Call plugins
  531. $this->plugins(__METHOD__, 1, func_get_args());
  532. if ($error) return false;
  533. if (!$result) {
  534. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  535. return false;
  536. }
  537. return true;
  538. }
  539. }
  540. ?>