Album.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. ###
  3. # @name Album Module
  4. # @author Tobias Reich
  5. # @copyright 2014 by Tobias Reich
  6. ###
  7. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  8. class Album extends Module {
  9. private $database = null;
  10. private $settings = null;
  11. private $albumIDs = null;
  12. public function __construct($database, $plugins, $settings, $albumIDs) {
  13. # Init vars
  14. $this->database = $database;
  15. $this->plugins = $plugins;
  16. $this->settings = $settings;
  17. $this->albumIDs = $albumIDs;
  18. return true;
  19. }
  20. public function add($title = 'Untitled', $public = 0, $visible = 1) {
  21. # Check dependencies
  22. self::dependencies(isset($this->database));
  23. # Call plugins
  24. $this->plugins(__METHOD__, 0, func_get_args());
  25. # Parse
  26. if (strlen($title)>50) $title = substr($title, 0, 50);
  27. # Database
  28. $sysstamp = time();
  29. $query = Database::prepare($this->database, "INSERT INTO ? (title, sysstamp, public, visible) VALUES ('?', '?', '?', '?')", [LYCHEE_TABLE_ALBUMS, $title, $sysstamp, $public, $visible]);
  30. $result = $this->database->query($query);
  31. # Call plugins
  32. $this->plugins(__METHOD__, 1, func_get_args());
  33. if (!$result) {
  34. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  35. return false;
  36. }
  37. return $this->database->insert_id;
  38. }
  39. public function get() {
  40. # Check dependencies
  41. self::dependencies(isset($this->database, $this->settings, $this->albumIDs));
  42. # Call plugins
  43. $this->plugins(__METHOD__, 0, func_get_args());
  44. # Get album information
  45. switch ($this->albumIDs) {
  46. case 'f': $return['public'] = false;
  47. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp FROM ? WHERE star = 1 " . $this->settings['sorting'], [LYCHEE_TABLE_PHOTOS]);
  48. break;
  49. case 's': $return['public'] = false;
  50. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp FROM ? WHERE public = 1 " . $this->settings['sorting'], [LYCHEE_TABLE_PHOTOS]);
  51. break;
  52. case 'r': $return['public'] = false;
  53. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) " . $this->settings['sorting'], [LYCHEE_TABLE_PHOTOS]);
  54. break;
  55. case '0': $return['public'] = false;
  56. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp FROM ? WHERE album = 0 " . $this->settings['sorting'], [LYCHEE_TABLE_PHOTOS]);
  57. break;
  58. default: $query = Database::prepare($this->database, "SELECT * FROM ? WHERE id = '?' LIMIT 1", [LYCHEE_TABLE_ALBUMS, $this->albumIDs]);
  59. $albums = $this->database->query($query);
  60. $return = $albums->fetch_assoc();
  61. $return['sysdate'] = date('d M. Y', $return['sysstamp']);
  62. $return['password'] = ($return['password']=='' ? false : true);
  63. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp FROM ? WHERE album = '?' " . $this->settings['sorting'], [LYCHEE_TABLE_PHOTOS, $this->albumIDs]);
  64. break;
  65. }
  66. # Get photos
  67. $photos = $this->database->query($query);
  68. $previousPhotoID = '';
  69. while ($photo = $photos->fetch_assoc()) {
  70. # Parse
  71. $photo['sysdate'] = date('d F Y', substr($photo['id'], 0, -4));
  72. $photo['previousPhoto'] = $previousPhotoID;
  73. $photo['nextPhoto'] = '';
  74. $photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $photo['thumbUrl'];
  75. if (isset($photo['takestamp'])&&$photo['takestamp']!=='0') {
  76. $photo['cameraDate'] = 1;
  77. $photo['sysdate'] = date('d F Y', $photo['takestamp']);
  78. }
  79. if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id'];
  80. $previousPhotoID = $photo['id'];
  81. # Add to return
  82. $return['content'][$photo['id']] = $photo;
  83. }
  84. if ($photos->num_rows===0) {
  85. # Album empty
  86. $return['content'] = false;
  87. } else {
  88. # Enable next and previous for the first and last photo
  89. $lastElement = end($return['content']);
  90. $lastElementId = $lastElement['id'];
  91. $firstElement = reset($return['content']);
  92. $firstElementId = $firstElement['id'];
  93. if ($lastElementId!==$firstElementId) {
  94. $return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
  95. $return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
  96. }
  97. }
  98. $return['id'] = $this->albumIDs;
  99. $return['num'] = $photos->num_rows;
  100. # Call plugins
  101. $this->plugins(__METHOD__, 1, func_get_args());
  102. return $return;
  103. }
  104. public function getAll($public) {
  105. # Check dependencies
  106. self::dependencies(isset($this->database, $this->settings, $public));
  107. # Call plugins
  108. $this->plugins(__METHOD__, 0, func_get_args());
  109. # Get SmartAlbums
  110. if ($public===false) $return = $this->getSmartInfo();
  111. # Albums query
  112. $query = Database::prepare($this->database, 'SELECT id, title, public, sysstamp, password FROM ? WHERE public = 1 AND visible <> 0', [LYCHEE_TABLE_ALBUMS]);
  113. if ($public===false) $query = Database::prepare($this->database, 'SELECT id, title, public, sysstamp, password FROM ?', [LYCHEE_TABLE_ALBUMS]);
  114. # Execute query
  115. $albums = $this->database->query($query);
  116. if (!$albums) {
  117. Log::error($database, __METHOD__, __LINE__, 'Could not get all albums (' . $database->error . ')');
  118. exit('Error: ' . $this->database->error);
  119. }
  120. # For each album
  121. while ($album = $albums->fetch_assoc()) {
  122. # Parse info
  123. $album['sysdate'] = date('F Y', $album['sysstamp']);
  124. $album['password'] = ($album['password'] != '');
  125. # Thumbs
  126. if (($public===true&&$album['password']===false)||($public===false)) {
  127. # Execute query
  128. $query = Database::prepare($this->database, "SELECT thumbUrl FROM ? WHERE album = '?' ORDER BY star DESC, " . substr($this->settings['sorting'], 9) . " LIMIT 3", [LYCHEE_TABLE_PHOTOS, $album['id']]);
  129. $thumbs = $this->database->query($query);
  130. # For each thumb
  131. $k = 0;
  132. while ($thumb = $thumbs->fetch_object()) {
  133. $album["thumb$k"] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
  134. $k++;
  135. }
  136. }
  137. # Add to return
  138. $return['content'][$album['id']] = $album;
  139. }
  140. # Num of albums
  141. $return['num'] = $albums->num_rows;
  142. # Call plugins
  143. $this->plugins(__METHOD__, 1, func_get_args());
  144. return $return;
  145. }
  146. private function getSmartInfo() {
  147. # Check dependencies
  148. self::dependencies(isset($this->database, $this->settings));
  149. # Unsorted
  150. $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE album = 0 ' . $this->settings['sorting'], [LYCHEE_TABLE_PHOTOS]);
  151. $unsorted = $this->database->query($query);
  152. $i = 0;
  153. while($row = $unsorted->fetch_object()) {
  154. if ($i<3) {
  155. $return["unsortedThumb$i"] = LYCHEE_URL_UPLOADS_THUMB . $row->thumbUrl;
  156. $i++;
  157. } else break;
  158. }
  159. $return['unsortedNum'] = $unsorted->num_rows;
  160. # Public
  161. $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE public = 1 ' . $this->settings['sorting'], [LYCHEE_TABLE_PHOTOS]);
  162. $public = $this->database->query($query);
  163. $i = 0;
  164. while($row2 = $public->fetch_object()) {
  165. if ($i<3) {
  166. $return["publicThumb$i"] = LYCHEE_URL_UPLOADS_THUMB . $row2->thumbUrl;
  167. $i++;
  168. } else break;
  169. }
  170. $return['publicNum'] = $public->num_rows;
  171. # Starred
  172. $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE star = 1 ' . $this->settings['sorting'], [LYCHEE_TABLE_PHOTOS]);
  173. $starred = $this->database->query($query);
  174. $i = 0;
  175. while($row3 = $starred->fetch_object()) {
  176. if ($i<3) {
  177. $return["starredThumb$i"] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
  178. $i++;
  179. } else break;
  180. }
  181. $return['starredNum'] = $starred->num_rows;
  182. # Recent
  183. $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) ' . $this->settings['sorting'], [LYCHEE_TABLE_PHOTOS]);
  184. $recent = $this->database->query($query);
  185. $i = 0;
  186. while($row3 = $recent->fetch_object()) {
  187. if ($i<3) {
  188. $return["recentThumb$i"] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
  189. $i++;
  190. } else break;
  191. }
  192. $return['recentNum'] = $recent->num_rows;
  193. return $return;
  194. }
  195. public function getArchive() {
  196. # Check dependencies
  197. self::dependencies(isset($this->database, $this->albumIDs));
  198. # Call plugins
  199. $this->plugins(__METHOD__, 0, func_get_args());
  200. # Illicit chars
  201. $badChars = array_merge(
  202. array_map('chr', range(0,31)),
  203. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  204. );
  205. # Photos query
  206. switch($this->albumIDs) {
  207. case 's':
  208. $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE public = 1', [LYCHEE_TABLE_PHOTOS]);
  209. $zipTitle = 'Public';
  210. break;
  211. case 'f':
  212. $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE star = 1', [LYCHEE_TABLE_PHOTOS]);
  213. $zipTitle = 'Starred';
  214. break;
  215. case 'r':
  216. $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY checksum', [LYCHEE_TABLE_PHOTOS]);
  217. $zipTitle = 'Recent';
  218. break;
  219. default:
  220. $photos = Database::prepare($this->database, "SELECT title, url FROM ? WHERE album = '?'", [LYCHEE_TABLE_PHOTOS, $this->albumIDs]);
  221. $zipTitle = 'Unsorted';
  222. }
  223. # Set title
  224. if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) {
  225. $query = Database::prepare($this->database, "SELECT title FROM ? WHERE id = '?' LIMIT 1", [LYCHEE_TABLE_ALBUMS, $this->albumIDs]);
  226. $album = $this->database->query($query);
  227. $zipTitle = $album->fetch_object()->title;
  228. }
  229. # Escape title
  230. $zipTitle = str_replace($badChars, '', $zipTitle);
  231. $filename = LYCHEE_DATA . $zipTitle . '.zip';
  232. # Create zip
  233. $zip = new ZipArchive();
  234. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  235. Log::error($this->database, __METHOD__, __LINE__, 'Could not create ZipArchive');
  236. return false;
  237. }
  238. # Execute query
  239. $photos = $this->database->query($photos);
  240. # Check if album empty
  241. if ($photos->num_rows==0) {
  242. Log::error($this->database, __METHOD__, __LINE__, 'Could not create ZipArchive without images');
  243. return false;
  244. }
  245. # Parse each path
  246. $files = array();
  247. while ($photo = $photos->fetch_object()) {
  248. # Parse url
  249. $photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
  250. # Parse title
  251. $photo->title = str_replace($badChars, '', $photo->title);
  252. if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
  253. # Check if readable
  254. if (!@is_readable($photo->url)) continue;
  255. # Get extension of image
  256. $extension = getExtension($photo->url);
  257. # Set title for photo
  258. $zipFileName = $zipTitle . '/' . $photo->title . $extension;
  259. # Check for duplicates
  260. if (!empty($files)) {
  261. $i = 1;
  262. while (in_array($zipFileName, $files)) {
  263. # Set new title for photo
  264. $zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . $extension;
  265. $i++;
  266. }
  267. }
  268. # Add to array
  269. $files[] = $zipFileName;
  270. # Add photo to zip
  271. $zip->addFile($photo->url, $zipFileName);
  272. }
  273. # Finish zip
  274. $zip->close();
  275. # Send zip
  276. header("Content-Type: application/zip");
  277. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  278. header("Content-Length: " . filesize($filename));
  279. readfile($filename);
  280. # Delete zip
  281. unlink($filename);
  282. # Call plugins
  283. $this->plugins(__METHOD__, 1, func_get_args());
  284. return true;
  285. }
  286. public function setTitle($title = 'Untitled') {
  287. # Check dependencies
  288. self::dependencies(isset($this->database, $this->albumIDs));
  289. # Call plugins
  290. $this->plugins(__METHOD__, 0, func_get_args());
  291. # Parse
  292. if (strlen($title)>50) $title = substr($title, 0, 50);
  293. # Execute query
  294. $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", [LYCHEE_TABLE_ALBUMS, $title, $this->albumIDs]);
  295. $result = $this->database->query($query);
  296. # Call plugins
  297. $this->plugins(__METHOD__, 1, func_get_args());
  298. if (!$result) {
  299. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  300. return false;
  301. }
  302. return true;
  303. }
  304. public function setDescription($description = '') {
  305. # Check dependencies
  306. self::dependencies(isset($this->database, $this->albumIDs));
  307. # Call plugins
  308. $this->plugins(__METHOD__, 0, func_get_args());
  309. # Parse
  310. $description = htmlentities($description, ENT_COMPAT | ENT_HTML401, 'UTF-8');
  311. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  312. # Execute query
  313. $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN (?)", [LYCHEE_TABLE_ALBUMS, $description, $this->albumIDs]);
  314. $result = $this->database->query($query);
  315. # Call plugins
  316. $this->plugins(__METHOD__, 1, func_get_args());
  317. if (!$result) {
  318. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  319. return false;
  320. }
  321. return true;
  322. }
  323. public function getPublic() {
  324. # Check dependencies
  325. self::dependencies(isset($this->database, $this->albumIDs));
  326. # Call plugins
  327. $this->plugins(__METHOD__, 0, func_get_args());
  328. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
  329. # Execute query
  330. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", [LYCHEE_TABLE_ALBUMS, $this->albumIDs]);
  331. $albums = $this->database->query($query);
  332. $album = $albums->fetch_object();
  333. # Call plugins
  334. $this->plugins(__METHOD__, 1, func_get_args());
  335. if ($album->public==1) return true;
  336. return false;
  337. }
  338. public function getDownloadable() {
  339. # Check dependencies
  340. self::dependencies(isset($this->database, $this->albumIDs));
  341. # Call plugins
  342. $this->plugins(__METHOD__, 0, func_get_args());
  343. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f'||$this->albumIDs==='r') return false;
  344. # Execute query
  345. $query = Database::prepare($this->database, "SELECT downloadable FROM ? WHERE id = '?' LIMIT 1", [LYCHEE_TABLE_ALBUMS, $this->albumIDs]);
  346. $albums = $this->database->query($query);
  347. $album = $albums->fetch_object();
  348. # Call plugins
  349. $this->plugins(__METHOD__, 1, func_get_args());
  350. if ($album->downloadable==1) return true;
  351. return false;
  352. }
  353. public function setPublic($password, $visible, $downloadable) {
  354. # Check dependencies
  355. self::dependencies(isset($this->database, $this->albumIDs));
  356. # Call plugins
  357. $this->plugins(__METHOD__, 0, func_get_args());
  358. # Get public
  359. $query = Database::prepare($this->database, "SELECT id, public FROM ? WHERE id IN (?)", [LYCHEE_TABLE_ALBUMS, $this->albumIDs]);
  360. $albums = $this->database->query($query);
  361. while ($album = $albums->fetch_object()) {
  362. # Invert public
  363. $public = ($album->public=='0' ? 1 : 0);
  364. # Convert visible
  365. $visible = ($visible==='true' ? 1 : 0);
  366. # Convert downloadable
  367. $downloadable = ($downloadable==='true' ? 1 : 0);
  368. # Set public
  369. $query = Database::prepare($this->database, "UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id = '?'", [LYCHEE_TABLE_ALBUMS, $public, $visible, $downloadable, $album->id]);
  370. $result = $this->database->query($query);
  371. if (!$result) {
  372. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  373. return false;
  374. }
  375. # Reset permissions for photos
  376. if ($public===1) {
  377. $query = Database::prepare($this->database, "UPDATE ? SET public = 0 WHERE album = '?'", [LYCHEE_TABLE_PHOTOS, $album->id]);
  378. $result = $this->database->query($query);
  379. if (!$result) {
  380. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  381. return false;
  382. }
  383. }
  384. }
  385. # Call plugins
  386. $this->plugins(__METHOD__, 1, func_get_args());
  387. # Set password
  388. if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
  389. return true;
  390. }
  391. private function setPassword($password) {
  392. # Check dependencies
  393. self::dependencies(isset($this->database, $this->albumIDs));
  394. # Call plugins
  395. $this->plugins(__METHOD__, 0, func_get_args());
  396. if (strlen($password)>0) {
  397. # Get hashed password
  398. $password = get_hashed_password($password);
  399. # Set hashed password
  400. # Do not prepare $password because it is hashed and save
  401. # Preparing (escaping) the password would destroy the hash
  402. $query = Database::prepare($this->database, "UPDATE ? SET password = '$password' WHERE id IN (?)", [LYCHEE_TABLE_ALBUMS, $this->albumIDs]);
  403. $result = $this->database->query($query);
  404. } else {
  405. # Unset password
  406. $query = Database::prepare($this->database, "UPDATE ? SET password = NULL WHERE id IN (?)", [LYCHEE_TABLE_ALBUMS, $this->albumIDs]);
  407. $result = $this->database->query($query);
  408. }
  409. # Call plugins
  410. $this->plugins(__METHOD__, 1, func_get_args());
  411. if (!$result) {
  412. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  413. return false;
  414. }
  415. return true;
  416. }
  417. public function checkPassword($password) {
  418. # Check dependencies
  419. self::dependencies(isset($this->database, $this->albumIDs));
  420. # Call plugins
  421. $this->plugins(__METHOD__, 0, func_get_args());
  422. # Execute query
  423. $query = Database::prepare($this->database, "SELECT password FROM ? WHERE id = '?' LIMIT 1", [LYCHEE_TABLE_ALBUMS, $this->albumIDs]);
  424. $albums = $this->database->query($query);
  425. $album = $albums->fetch_object();
  426. # Call plugins
  427. $this->plugins(__METHOD__, 1, func_get_args());
  428. if ($album->password=='') return true;
  429. else if ($album->password===$password||$album->password===crypt($password, $album->password)) return true;
  430. return false;
  431. }
  432. public function delete() {
  433. # Check dependencies
  434. self::dependencies(isset($this->database, $this->albumIDs));
  435. # Call plugins
  436. $this->plugins(__METHOD__, 0, func_get_args());
  437. # Init vars
  438. $error = false;
  439. # Execute query
  440. $query = Database::prepare($this->database, "SELECT id FROM ? WHERE album IN (?)", [LYCHEE_TABLE_PHOTOS, $this->albumIDs]);
  441. $photos = $this->database->query($query);
  442. # For each album delete photo
  443. while ($row = $photos->fetch_object()) {
  444. $photo = new Photo($this->database, $this->plugins, null, $row->id);
  445. if (!$photo->delete($row->id)) $error = true;
  446. }
  447. # Delete albums
  448. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", [LYCHEE_TABLE_ALBUMS, $this->albumIDs]);
  449. $result = $this->database->query($query);
  450. # Call plugins
  451. $this->plugins(__METHOD__, 1, func_get_args());
  452. if ($error) return false;
  453. if (!$result) {
  454. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  455. return false;
  456. }
  457. return true;
  458. }
  459. }
  460. ?>