Album.php 15 KB

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