Photo.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. ###
  3. # @name Photo 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 Photo extends Module {
  9. private $database = null;
  10. private $photoIDs = null;
  11. public function __construct($database, $plugins, $photoIDs) {
  12. # Init vars
  13. $this->database = $database;
  14. $this->plugins = $plugins;
  15. $this->photoIDs = $photoIDs;
  16. return true;
  17. }
  18. public function get($albumID) {
  19. if (!isset($this->database, $this->photoIDs)) return false;
  20. # Call plugins
  21. $this->plugins(__METHOD__, 0, func_get_args());
  22. # Get photo
  23. $photos = $this->database->query("SELECT * FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  24. $photo = $photos->fetch_assoc();
  25. # Parse photo
  26. $photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
  27. if (strlen($photo['takedate'])>0) $photo['takedate'] = date('d M. Y', strtotime($photo['takedate']));
  28. if ($albumID!='false') {
  29. if ($photo['album']!=0) {
  30. # Get album
  31. $albums = $this->database->query("SELECT public FROM lychee_albums WHERE id = '" . $photo['album'] . " LIMIT 1';");
  32. $album = $albums->fetch_assoc();
  33. # Parse album
  34. $photo['public'] = ($album['public']=='1' ? '2' : $photo['public']);
  35. }
  36. $photo['original_album'] = $photo['album'];
  37. $photo['album'] = $albumID;
  38. }
  39. # Call plugins
  40. $this->plugins(__METHOD__, 1, func_get_args());
  41. return $photo;
  42. }
  43. public function getArchive() {
  44. if (!isset($this->database, $this->photoIDs)) return false;
  45. # Call plugins
  46. $this->plugins(__METHOD__, 0, func_get_args());
  47. # Get photo
  48. $photos = $this->database->query("SELECT title, url FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  49. $photo = $photos->fetch_object();
  50. # Get extension
  51. $extension = array_reverse(explode('.', $photo->url));
  52. # Parse title
  53. if ($photo->title=='') $photo->title = 'Untitled';
  54. # Set headers
  55. header("Content-Type: application/octet-stream");
  56. header("Content-Disposition: attachment; filename=\"$photo->title.$extension[0]\"");
  57. header("Content-Length: " . filesize("../uploads/big/$photo->url"));
  58. # Send file
  59. readfile("../uploads/big/$photo->url");
  60. # Call plugins
  61. $this->plugins(__METHOD__, 1, func_get_args());
  62. return true;
  63. }
  64. function setTitle($title) {
  65. if (!isset($this->database, $this->photoIDs)) return false;
  66. # Call plugins
  67. $this->plugins(__METHOD__, 0, func_get_args());
  68. # Parse
  69. if (strlen($title)>50) $title = substr($title, 0, 50);
  70. # Set title
  71. $result = $this->database->query("UPDATE lychee_photos SET title = '$title' WHERE id IN ($this->photoIDs);");
  72. # Call plugins
  73. $this->plugins(__METHOD__, 1, func_get_args());
  74. if (!$result) return false;
  75. return true;
  76. }
  77. function setDescription($description) {
  78. if (!isset($this->database, $this->photoIDs)) return false;
  79. # Call plugins
  80. $this->plugins(__METHOD__, 0, func_get_args());
  81. # Parse
  82. $description = htmlentities($description);
  83. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  84. # Set description
  85. $result = $this->database->query("UPDATE lychee_photos SET description = '$description' WHERE id IN ('$this->photoIDs');");
  86. # Call plugins
  87. $this->plugins(__METHOD__, 1, func_get_args());
  88. if (!$result) return false;
  89. return true;
  90. }
  91. public function setStar() {
  92. if (!isset($this->database, $this->photoIDs)) return false;
  93. # Call plugins
  94. $this->plugins(__METHOD__, 0, func_get_args());
  95. # Init vars
  96. $error = false;
  97. # Get photos
  98. $photos = $this->database->query("SELECT id, star FROM lychee_photos WHERE id IN ($this->photoIDs);");
  99. # For each photo
  100. while ($photo = $photos->fetch_object()) {
  101. # Invert star
  102. $star = ($photo->star==0 ? 1 : 0);
  103. # Set star
  104. $star = $this->database->query("UPDATE lychee_photos SET star = '$star' WHERE id = '$photo->id';");
  105. if (!$star) $error = true;
  106. }
  107. # Call plugins
  108. $this->plugins(__METHOD__, 1, func_get_args());
  109. if ($error) return false;
  110. return true;
  111. }
  112. function getPublic($password) {
  113. if (!isset($this->database, $this->photoIDs)) return false;
  114. # Call plugins
  115. $this->plugins(__METHOD__, 0, func_get_args());
  116. # Get photo
  117. $photos = $this->database->query("SELECT public, album FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  118. $photo = $photos->fetch_object();
  119. # Check if public
  120. if ($photo->public==1) return true;
  121. else {
  122. $album = new Album($this->database, null, null, $photo->album);
  123. $acP = $album->checkPassword($password);
  124. $agP = $album->getPublic();
  125. if ($acP===true&&$agP===true) return true;
  126. }
  127. # Call plugins
  128. $this->plugins(__METHOD__, 1, func_get_args());
  129. return false;
  130. }
  131. public function setPublic() {
  132. if (!isset($this->database, $this->photoIDs)) return false;
  133. # Call plugins
  134. $this->plugins(__METHOD__, 0, func_get_args());
  135. # Get public
  136. $photos = $this->database->query("SELECT public FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  137. $photo = $photos->fetch_object();
  138. # Invert public
  139. $public = ($photo->public==0 ? 1 : 0);
  140. # Set public
  141. $result = $this->database->query("UPDATE lychee_photos SET public = '$public' WHERE id = '$this->photoIDs';");
  142. # Call plugins
  143. $this->plugins(__METHOD__, 1, func_get_args());
  144. if (!$result) return false;
  145. return true;
  146. }
  147. function setAlbum($albumID) {
  148. if (!isset($this->database, $this->photoIDs)) return false;
  149. # Call plugins
  150. $this->plugins(__METHOD__, 0, func_get_args());
  151. # Set album
  152. $result = $this->database->query("UPDATE lychee_photos SET album = '$albumID' WHERE id IN ($this->photoIDs);");
  153. # Call plugins
  154. $this->plugins(__METHOD__, 1, func_get_args());
  155. if (!$result) return false;
  156. return true;
  157. }
  158. public function setTags($tags) {
  159. if (!isset($this->database, $this->photoIDs)) return false;
  160. # Call plugins
  161. $this->plugins(__METHOD__, 0, func_get_args());
  162. # Parse tags
  163. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  164. $tags = preg_replace('/,$|^,/', ',', $tags);
  165. if (strlen($tags)>1000) return false;
  166. # Set tags
  167. $result = $this->database->query("UPDATE lychee_photos SET tags = '$tags' WHERE id IN ($this->photoIDs);");
  168. # Call plugins
  169. $this->plugins(__METHOD__, 1, func_get_args());
  170. if (!$result) return false;
  171. return true;
  172. }
  173. public function delete() {
  174. if (!isset($this->database, $this->photoIDs)) return false;
  175. # Call plugins
  176. $this->plugins(__METHOD__, 0, func_get_args());
  177. # Get photos
  178. $photos = $this->database->query("SELECT id, url, thumbUrl FROM lychee_photos WHERE id IN ($this->photoIDs);");
  179. # For each photo
  180. while ($photo = $photos->fetch_object()) {
  181. # Get retina thumb url
  182. $thumbUrl2x = explode(".", $photo->thumbUrl);
  183. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  184. # Delete files
  185. if (!unlink('../uploads/big/' . $photo->url)) return false;
  186. if (!unlink('../uploads/thumb/' . $photo->thumbUrl)) return false;
  187. if (!unlink('../uploads/thumb/' . $thumbUrl2x)) return false;
  188. # Delete db entry
  189. $delete = $this->database->query("DELETE FROM lychee_photos WHERE id = '$photo->id';");
  190. if (!$delete) return false;
  191. }
  192. # Call plugins
  193. $this->plugins(__METHOD__, 1, func_get_args());
  194. if (!$photos) return false;
  195. return true;
  196. }
  197. }
  198. ?>