Album.php 14 KB

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