Album.php 19 KB

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