Album.php 21 KB

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