Album.php 19 KB

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