Album.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. $this->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. $this->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. $this->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. $this->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. $this->dependencies(isset($this->database, $this->albumIDs));
  169. # Call plugins
  170. $this->plugins(__METHOD__, 0, func_get_args());
  171. # Photos query
  172. switch($this->albumIDs) {
  173. case 's':
  174. $photos = "SELECT title, url FROM lychee_photos WHERE public = '1';";
  175. $zipTitle = 'Public';
  176. break;
  177. case 'f':
  178. $photos = "SELECT title, url FROM lychee_photos WHERE star = '1';";
  179. $zipTitle = 'Starred';
  180. break;
  181. default:
  182. $photos = "SELECT title, url FROM lychee_photos WHERE album = '$this->albumIDs';";
  183. $zipTitle = 'Unsorted';
  184. }
  185. # Set title
  186. $album = $this->database->query("SELECT title FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  187. if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) $zipTitle = $album->fetch_object()->title;
  188. $filename = LYCHEE_DATA . $zipTitle . '.zip';
  189. # Create zip
  190. $zip = new ZipArchive();
  191. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) return false;
  192. # Execute query
  193. $photos = $this->database->query($photos);
  194. # Check if album empty
  195. if ($photos->num_rows==0) return false;
  196. # Parse each path
  197. $files = array();
  198. while ($photo = $photos->fetch_object()) {
  199. # Parse url
  200. $photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
  201. # Parse title
  202. $badChars = array_merge(
  203. array_map('chr', range(0,31)),
  204. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  205. );
  206. $photo->title = str_replace($badChars, '', $photo->title);
  207. if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
  208. # Check if readable
  209. if (!@is_readable($photo->url)) continue;
  210. # Get extension of image
  211. $extension = array_reverse(explode('.', $photo->url));
  212. $extension = $extension[0];
  213. # Set title for photo
  214. $zipFileName = $zipTitle . '/' . $photo->title . '.' . $extension;
  215. # Check for duplicates
  216. if (!empty($files)) {
  217. $i = 1;
  218. while (in_array($zipFileName, $files)) {
  219. # Set new title for photo
  220. $zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . '.' . $extension;
  221. $i++;
  222. }
  223. }
  224. # Add to array
  225. $files[] = $zipFileName;
  226. # Add photo to zip
  227. $zip->addFile($photo->url, $zipFileName);
  228. }
  229. # Finish zip
  230. $zip->close();
  231. # Send zip
  232. header("Content-Type: application/zip");
  233. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  234. header("Content-Length: " . filesize($filename));
  235. readfile($filename);
  236. # Delete zip
  237. unlink($filename);
  238. # Call plugins
  239. $this->plugins(__METHOD__, 1, func_get_args());
  240. return true;
  241. }
  242. public function setTitle($title = 'Untitled') {
  243. # Check dependencies
  244. $this->dependencies(isset($this->database, $this->albumIDs));
  245. # Call plugins
  246. $this->plugins(__METHOD__, 0, func_get_args());
  247. # Parse
  248. if (strlen($title)>50) $title = substr($title, 0, 50);
  249. # Execute query
  250. $result = $this->database->query("UPDATE lychee_albums SET title = '$title' WHERE id IN ($this->albumIDs);");
  251. # Call plugins
  252. $this->plugins(__METHOD__, 1, func_get_args());
  253. if (!$result) {
  254. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  255. return false;
  256. }
  257. return true;
  258. }
  259. public function setDescription($description = '') {
  260. # Check dependencies
  261. $this->dependencies(isset($this->database, $this->albumIDs));
  262. # Call plugins
  263. $this->plugins(__METHOD__, 0, func_get_args());
  264. # Parse
  265. $description = htmlentities($description);
  266. if (strlen($description)>1000) return false;
  267. # Execute query
  268. $result = $this->database->query("UPDATE lychee_albums SET description = '$description' WHERE id IN ($this->albumIDs);");
  269. # Call plugins
  270. $this->plugins(__METHOD__, 1, func_get_args());
  271. if (!$result) {
  272. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  273. return false;
  274. }
  275. return true;
  276. }
  277. public function getPublic() {
  278. # Check dependencies
  279. $this->dependencies(isset($this->database, $this->albumIDs));
  280. # Call plugins
  281. $this->plugins(__METHOD__, 0, func_get_args());
  282. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
  283. # Execute query
  284. $albums = $this->database->query("SELECT public FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  285. $album = $albums->fetch_object();
  286. # Call plugins
  287. $this->plugins(__METHOD__, 1, func_get_args());
  288. if ($album->public==1) return true;
  289. return false;
  290. }
  291. public function setPublic($password) {
  292. # Check dependencies
  293. $this->dependencies(isset($this->database, $this->albumIDs));
  294. # Call plugins
  295. $this->plugins(__METHOD__, 0, func_get_args());
  296. # Get public
  297. $albums = $this->database->query("SELECT id, public FROM lychee_albums WHERE id IN ('$this->albumIDs');");
  298. while ($album = $albums->fetch_object()) {
  299. # Invert public
  300. $public = ($album->public=='0' ? 1 : 0);
  301. # Set public
  302. $result = $this->database->query("UPDATE lychee_albums SET public = '$public', password = NULL WHERE id = '$album->id';");
  303. if (!$result) {
  304. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  305. return false;
  306. }
  307. # Reset permissions for photos
  308. if ($public===1) {
  309. $result = $this->database->query("UPDATE lychee_photos SET public = 0 WHERE album = '$album->id';");
  310. if (!$result) {
  311. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  312. return false;
  313. }
  314. }
  315. }
  316. # Call plugins
  317. $this->plugins(__METHOD__, 1, func_get_args());
  318. # Set password
  319. if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
  320. return true;
  321. }
  322. public function setPassword($password) {
  323. # Check dependencies
  324. $this->dependencies(isset($this->database, $this->albumIDs));
  325. # Call plugins
  326. $this->plugins(__METHOD__, 0, func_get_args());
  327. if (strlen($password)>0) {
  328. # Get hashed password
  329. $password = get_hashed_password($password);
  330. # Set hashed password
  331. $result = $this->database->query("UPDATE lychee_albums SET password = '$password' WHERE id IN ('$this->albumIDs');");
  332. } else {
  333. # Unset password
  334. $result = $this->database->query("UPDATE lychee_albums SET password = NULL WHERE id IN ('$this->albumIDs');");
  335. }
  336. # Call plugins
  337. $this->plugins(__METHOD__, 1, func_get_args());
  338. if (!$result) {
  339. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  340. return false;
  341. }
  342. return true;
  343. }
  344. public function checkPassword($password) {
  345. # Check dependencies
  346. $this->dependencies(isset($this->database, $this->albumIDs));
  347. # Call plugins
  348. $this->plugins(__METHOD__, 0, func_get_args());
  349. # Execute query
  350. $albums = $this->database->query("SELECT password FROM lychee_albums WHERE id = '$this->albumIDs' LIMIT 1;");
  351. $album = $albums->fetch_object();
  352. # Call plugins
  353. $this->plugins(__METHOD__, 1, func_get_args());
  354. if ($album->password=='') return true;
  355. else if ($album->password===$password||$album->password===crypt($password, $album->password)) return true;
  356. return false;
  357. }
  358. public function delete($albumIDs) {
  359. # Check dependencies
  360. $this->dependencies(isset($this->database, $this->albumIDs));
  361. # Call plugins
  362. $this->plugins(__METHOD__, 0, func_get_args());
  363. # Init vars
  364. $error = false;
  365. # Execute query
  366. $photos = $this->database->query("SELECT id FROM lychee_photos WHERE album IN ($albumIDs);");
  367. # For each album delete photo
  368. while ($row = $photos->fetch_object()) {
  369. $photo = new Photo($this->database, $this->plugins, null, $row->id);
  370. if (!$photo->delete($row->id)) $error = true;
  371. }
  372. # Delete albums
  373. $result = $this->database->query("DELETE FROM lychee_albums WHERE id IN ($albumIDs);");
  374. # Call plugins
  375. $this->plugins(__METHOD__, 1, func_get_args());
  376. if ($error||!$result) return false;
  377. return true;
  378. }
  379. }
  380. ?>