Photo.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. <?php
  2. ###
  3. # @name Photo Module
  4. # @copyright 2014 by Tobias Reich
  5. ###
  6. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  7. class Photo extends Module {
  8. private $database = null;
  9. private $settings = null;
  10. private $photoIDs = null;
  11. private $allowedTypes = array(
  12. IMAGETYPE_JPEG,
  13. IMAGETYPE_GIF,
  14. IMAGETYPE_PNG
  15. );
  16. private $validExtensions = array(
  17. '.jpg',
  18. '.jpeg',
  19. '.png',
  20. '.gif'
  21. );
  22. public function __construct($database, $plugins, $settings, $photoIDs) {
  23. # Init vars
  24. $this->database = $database;
  25. $this->plugins = $plugins;
  26. $this->settings = $settings;
  27. $this->photoIDs = $photoIDs;
  28. return true;
  29. }
  30. public function add($files, $albumID, $description = '', $tags = '') {
  31. # Check dependencies
  32. self::dependencies(isset($this->database));
  33. # Check permissions
  34. if (hasPermissions(LYCHEE_UPLOADS_BIG)===false||
  35. hasPermissions(LYCHEE_UPLOADS_THUMB)===false||
  36. hasPermissions(LYCHEE_UPLOADS_MEDIUM)===false) {
  37. Log::error($this->database, __METHOD__, __LINE__, 'Wrong permissions in uploads/');
  38. exit('Error: Wrong permissions in uploads-folder!');
  39. }
  40. # Call plugins
  41. $this->plugins(__METHOD__, 0, func_get_args());
  42. switch($albumID) {
  43. case 's':
  44. # s for public (share)
  45. $public = 1;
  46. $star = 0;
  47. $albumID = 0;
  48. break;
  49. case 'f':
  50. # f for starred (fav)
  51. $star = 1;
  52. $public = 0;
  53. $albumID = 0;
  54. break;
  55. case 'r':
  56. # r for recent
  57. $public = 0;
  58. $star = 0;
  59. $albumID = 0;
  60. break;
  61. default:
  62. $star = 0;
  63. $public = 0;
  64. break;
  65. }
  66. foreach ($files as $file) {
  67. # Verify extension
  68. $extension = getExtension($file['name']);
  69. if (!in_array(strtolower($extension), $this->validExtensions, true)) continue;
  70. # Verify image
  71. $type = @exif_imagetype($file['tmp_name']);
  72. if (!in_array($type, $this->allowedTypes, true)) continue;
  73. # Generate id
  74. $id = str_replace('.', '', microtime(true));
  75. while(strlen($id)<14) $id .= 0;
  76. # Set paths
  77. $tmp_name = $file['tmp_name'];
  78. $photo_name = md5($id) . $extension;
  79. $path = LYCHEE_UPLOADS_BIG . $photo_name;
  80. # Calculate checksum
  81. $checksum = sha1_file($tmp_name);
  82. if ($checksum===false) {
  83. Log::error($this->database, __METHOD__, __LINE__, 'Could not calculate checksum for photo');
  84. exit('Error: Could not calculate checksum for photo!');
  85. }
  86. # Check if image exists based on checksum
  87. if ($checksum===false) {
  88. $checksum = '';
  89. $exists = false;
  90. } else {
  91. $exists = $this->exists($checksum);
  92. if ($exists!==false) {
  93. $photo_name = $exists['photo_name'];
  94. $path = $exists['path'];
  95. $path_thumb = $exists['path_thumb'];
  96. $medium = ($exists['medium']==='1' ? true : false);
  97. $exists = true;
  98. }
  99. }
  100. if ($exists===false) {
  101. # Import if not uploaded via web
  102. if (!is_uploaded_file($tmp_name)) {
  103. if (!@copy($tmp_name, $path)) {
  104. Log::error($this->database, __METHOD__, __LINE__, 'Could not copy photo to uploads');
  105. exit('Error: Could not copy photo to uploads!');
  106. } else @unlink($tmp_name);
  107. } else {
  108. if (!@move_uploaded_file($tmp_name, $path)) {
  109. Log::error($this->database, __METHOD__, __LINE__, 'Could not move photo to uploads');
  110. exit('Error: Could not move photo to uploads!');
  111. }
  112. }
  113. }
  114. # Read infos
  115. $info = $this->getInfo($path);
  116. # Use title of file if IPTC title missing
  117. if ($info['title']==='') $info['title'] = substr(basename($file['name'], $extension), 0, 30);
  118. # Use description parameter if set
  119. if ($description==='') $description = $info['description'];
  120. if ($exists===false) {
  121. # Set orientation based on EXIF data
  122. if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!=='') {
  123. $adjustFile = $this->adjustFile($path, $info);
  124. if ($adjustFile!==false) $info = $adjustFile;
  125. else Log::notice($this->database, __METHOD__, __LINE__, 'Skipped adjustment of photo (' . $info['title'] . ')');
  126. }
  127. # Set original date
  128. if ($info['takestamp']!==''&&$info['takestamp']!==0) @touch($path, $info['takestamp']);
  129. # Create Thumb
  130. if (!$this->createThumb($path, $photo_name, $info['type'], $info['width'], $info['height'])) {
  131. Log::error($this->database, __METHOD__, __LINE__, 'Could not create thumbnail for photo');
  132. exit('Error: Could not create thumbnail for photo!');
  133. }
  134. # Create Medium
  135. if ($this->createMedium($path, $photo_name, $info['width'], $info['height'])) $medium = true;
  136. else $medium = false;
  137. # Set thumb url
  138. $path_thumb = md5($id) . '.jpeg';
  139. }
  140. # Save to DB
  141. $values = array(LYCHEE_TABLE_PHOTOS, $id, $info['title'], $photo_name, $description, $tags, $info['type'], $info['width'], $info['height'], $info['size'], $info['iso'], $info['aperture'], $info['make'], $info['model'], $info['shutter'], $info['focal'], $info['takestamp'], $path_thumb, $albumID, $public, $star, $checksum, $medium);
  142. $query = Database::prepare($this->database, "INSERT INTO ? (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum, medium) VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')", $values);
  143. $result = $this->database->query($query);
  144. if (!$result) {
  145. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  146. exit('Error: Could not save photo in database!');
  147. }
  148. }
  149. # Call plugins
  150. $this->plugins(__METHOD__, 1, func_get_args());
  151. return true;
  152. }
  153. private function exists($checksum, $photoID = null) {
  154. # Check dependencies
  155. self::dependencies(isset($this->database, $checksum));
  156. # Exclude $photoID from select when $photoID is set
  157. if (isset($photoID)) $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, medium FROM ? WHERE checksum = '?' AND id <> '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $checksum, $photoID));
  158. else $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, medium FROM ? WHERE checksum = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $checksum));
  159. $result = $this->database->query($query);
  160. if (!$result) {
  161. Log::error($this->database, __METHOD__, __LINE__, 'Could not check for existing photos with the same checksum');
  162. return false;
  163. }
  164. if ($result->num_rows===1) {
  165. $result = $result->fetch_object();
  166. $return = array(
  167. 'photo_name' => $result->url,
  168. 'path' => LYCHEE_UPLOADS_BIG . $result->url,
  169. 'path_thumb' => $result->thumbUrl,
  170. 'medium' => $result->medium
  171. );
  172. return $return;
  173. }
  174. return false;
  175. }
  176. private function createThumb($url, $filename, $type, $width, $height) {
  177. # Check dependencies
  178. self::dependencies(isset($this->database, $this->settings, $url, $filename, $type, $width, $height));
  179. # Call plugins
  180. $this->plugins(__METHOD__, 0, func_get_args());
  181. # Size of the thumbnail
  182. $newWidth = 200;
  183. $newHeight = 200;
  184. $photoName = explode('.', $filename);
  185. $newUrl = LYCHEE_UPLOADS_THUMB . $photoName[0] . '.jpeg';
  186. $newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName[0] . '@2x.jpeg';
  187. # Create thumbnails with Imagick
  188. if(extension_loaded('imagick')&&$this->settings['imagick']==='1') {
  189. # Read image
  190. $thumb = new Imagick();
  191. $thumb->readImage($url);
  192. $thumb->setImageCompressionQuality($this->settings['thumbQuality']);
  193. $thumb->setImageFormat('jpeg');
  194. # Copy image for 2nd thumb version
  195. $thumb2x = clone $thumb;
  196. # Create 1st version
  197. $thumb->cropThumbnailImage($newWidth, $newHeight);
  198. $thumb->writeImage($newUrl);
  199. $thumb->clear();
  200. $thumb->destroy();
  201. # Create 2nd version
  202. $thumb2x->cropThumbnailImage($newWidth*2, $newHeight*2);
  203. $thumb2x->writeImage($newUrl2x);
  204. $thumb2x->clear();
  205. $thumb2x->destroy();
  206. } else {
  207. # Create image
  208. $thumb = imagecreatetruecolor($newWidth, $newHeight);
  209. $thumb2x = imagecreatetruecolor($newWidth*2, $newHeight*2);
  210. # Set position
  211. if ($width<$height) {
  212. $newSize = $width;
  213. $startWidth = 0;
  214. $startHeight = $height/2 - $width/2;
  215. } else {
  216. $newSize = $height;
  217. $startWidth = $width/2 - $height/2;
  218. $startHeight = 0;
  219. }
  220. # Create new image
  221. switch($type) {
  222. case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break;
  223. case 'image/png': $sourceImg = imagecreatefrompng($url); break;
  224. case 'image/gif': $sourceImg = imagecreatefromgif($url); break;
  225. default: Log::error($this->database, __METHOD__, __LINE__, 'Type of photo is not supported');
  226. return false;
  227. break;
  228. }
  229. # Create thumb
  230. fastimagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth, $newHeight, $newSize, $newSize);
  231. imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']);
  232. imagedestroy($thumb);
  233. # Create retina thumb
  234. fastimagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth*2, $newHeight*2, $newSize, $newSize);
  235. imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']);
  236. imagedestroy($thumb2x);
  237. # Free memory
  238. imagedestroy($sourceImg);
  239. }
  240. # Call plugins
  241. $this->plugins(__METHOD__, 1, func_get_args());
  242. return true;
  243. }
  244. private function createMedium($url, $filename, $width, $height) {
  245. # Check dependencies
  246. self::dependencies(isset($this->database, $this->settings, $url, $filename, $width, $height));
  247. # Call plugins
  248. $this->plugins(__METHOD__, 0, func_get_args());
  249. # Size of the medium-photo
  250. # When changing these values,
  251. # also change the size detection in the front-end
  252. $newWidth = 1920;
  253. $newHeight = 1080;
  254. # Is photo big enough?
  255. # Is medium activated?
  256. # Is Imagick installed and activated?
  257. if (($width>$newWidth||$height>$newHeight)&&
  258. ($this->settings['medium']==='1')&&
  259. (extension_loaded('imagick')&&$this->settings['imagick']==='1')) {
  260. # $info = getimagesize($url);
  261. $newUrl = LYCHEE_UPLOADS_MEDIUM . $filename;
  262. # Read image
  263. $medium = new Imagick();
  264. $medium->readImage($url);
  265. $medium->scaleImage($newWidth, $newHeight, true);
  266. $medium->writeImage($newUrl);
  267. $medium->clear();
  268. $medium->destroy();
  269. $error = false;
  270. } else {
  271. # Photo too small or
  272. # Medium is deactivated or
  273. # Imagick not installed
  274. $error = true;
  275. }
  276. # Call plugins
  277. $this->plugins(__METHOD__, 1, func_get_args());
  278. if ($error===true) return false;
  279. return true;
  280. }
  281. public function adjustFile($path, $info) {
  282. # Check dependencies
  283. self::dependencies(isset($path, $info));
  284. # Call plugins
  285. $this->plugins(__METHOD__, 0, func_get_args());
  286. $swapSize = false;
  287. if (extension_loaded('imagick')&&$this->settings['imagick']==='1') {
  288. $rotateImage = 0;
  289. switch ($info['orientation']) {
  290. case 3:
  291. $rotateImage = 180;
  292. break;
  293. case 6:
  294. $rotateImage = 90;
  295. $swapSize = true;
  296. break;
  297. case 8:
  298. $rotateImage = 270;
  299. $swapSize = true;
  300. break;
  301. default:
  302. return false;
  303. break;
  304. }
  305. if ($rotateImage!==0) {
  306. $image = new Imagick();
  307. $image->readImage($path);
  308. $image->rotateImage(new ImagickPixel(), $rotateImage);
  309. $image->setImageOrientation(1);
  310. $image->writeImage($path);
  311. $image->clear();
  312. $image->destroy();
  313. }
  314. } else {
  315. $newWidth = $info['width'];
  316. $newHeight = $info['height'];
  317. $sourceImg = imagecreatefromjpeg($path);
  318. switch ($info['orientation']) {
  319. case 2:
  320. # mirror
  321. # not yet implemented
  322. return false;
  323. break;
  324. case 3:
  325. $process = true;
  326. $sourceImg = imagerotate($sourceImg, -180, 0);
  327. break;
  328. case 4:
  329. # rotate 180 and mirror
  330. # not yet implemented
  331. return false;
  332. break;
  333. case 5:
  334. # rotate 90 and mirror
  335. # not yet implemented
  336. return false;
  337. break;
  338. case 6:
  339. $process = true;
  340. $sourceImg = imagerotate($sourceImg, -90, 0);
  341. $newWidth = $info['height'];
  342. $newHeight = $info['width'];
  343. $swapSize = true;
  344. break;
  345. case 7:
  346. # rotate -90 and mirror
  347. # not yet implemented
  348. return false;
  349. break;
  350. case 8:
  351. $process = true;
  352. $sourceImg = imagerotate($sourceImg, 90, 0);
  353. $newWidth = $info['height'];
  354. $newHeight = $info['width'];
  355. $swapSize = true;
  356. break;
  357. default:
  358. return false;
  359. break;
  360. }
  361. # Recreate photo
  362. $newSourceImg = imagecreatetruecolor($newWidth, $newHeight);
  363. imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
  364. imagejpeg($newSourceImg, $path, 100);
  365. # Free memory
  366. imagedestroy($sourceImg);
  367. imagedestroy($newSourceImg);
  368. }
  369. # Call plugins
  370. $this->plugins(__METHOD__, 1, func_get_args());
  371. # SwapSize should be true when the image has been rotated
  372. # Return new dimensions in this case
  373. if ($swapSize===true) {
  374. $swapSize = $info['width'];
  375. $info['width'] = $info['height'];
  376. $info['height'] = $swapSize;
  377. }
  378. return $info;
  379. }
  380. public function get($albumID) {
  381. # Check dependencies
  382. self::dependencies(isset($this->database, $this->photoIDs));
  383. # Call plugins
  384. $this->plugins(__METHOD__, 0, func_get_args());
  385. # Get photo
  386. $query = Database::prepare($this->database, "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  387. $photos = $this->database->query($query);
  388. $photo = $photos->fetch_assoc();
  389. # Parse photo
  390. $photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
  391. if (strlen($photo['takestamp'])>1) $photo['takedate'] = date('d M. Y', $photo['takestamp']);
  392. # Parse medium
  393. if ($photo['medium']==='1') $photo['medium'] = LYCHEE_URL_UPLOADS_MEDIUM . $photo['url'];
  394. else $photo['medium'] = '';
  395. # Parse paths
  396. $photo['url'] = LYCHEE_URL_UPLOADS_BIG . $photo['url'];
  397. $photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $photo['thumbUrl'];
  398. if ($albumID!='false') {
  399. # Show photo as public when parent album is public
  400. # Check if parent album is available and not photo not unsorted
  401. if ($photo['album']!=0) {
  402. # Get album
  403. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $photo['album']));
  404. $albums = $this->database->query($query);
  405. $album = $albums->fetch_assoc();
  406. # Parse album
  407. $photo['public'] = ($album['public']=='1' ? '2' : $photo['public']);
  408. }
  409. $photo['original_album'] = $photo['album'];
  410. $photo['album'] = $albumID;
  411. }
  412. # Call plugins
  413. $this->plugins(__METHOD__, 1, func_get_args());
  414. return $photo;
  415. }
  416. public function getInfo($url) {
  417. # Check dependencies
  418. self::dependencies(isset($this->database, $url));
  419. # Call plugins
  420. $this->plugins(__METHOD__, 0, func_get_args());
  421. $iptcArray = array();
  422. $info = getimagesize($url, $iptcArray);
  423. # General information
  424. $return['type'] = $info['mime'];
  425. $return['width'] = $info[0];
  426. $return['height'] = $info[1];
  427. # Size
  428. $size = filesize($url)/1024;
  429. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  430. else $return['size'] = round($size, 1) . ' KB';
  431. # IPTC Metadata Fallback
  432. $return['title'] = '';
  433. $return['description'] = '';
  434. # IPTC Metadata
  435. if(isset($iptcArray['APP13'])) {
  436. $iptcInfo = iptcparse($iptcArray['APP13']);
  437. if (is_array($iptcInfo)) {
  438. $temp = @$iptcInfo['2#105'][0];
  439. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  440. $temp = @$iptcInfo['2#120'][0];
  441. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  442. $temp = @$iptcInfo['2#005'][0];
  443. if (isset($temp)&&strlen($temp)>0&&$return['title']==='') $return['title'] = $temp;
  444. }
  445. }
  446. # EXIF Metadata Fallback
  447. $return['orientation'] = '';
  448. $return['iso'] = '';
  449. $return['aperture'] = '';
  450. $return['make'] = '';
  451. $return['model'] = '';
  452. $return['shutter'] = '';
  453. $return['focal'] = '';
  454. $return['takestamp'] = 0;
  455. # Read EXIF
  456. if ($info['mime']=='image/jpeg') $exif = @exif_read_data($url, 'EXIF', 0);
  457. else $exif = false;
  458. # EXIF Metadata
  459. if ($exif!==false) {
  460. if (isset($exif['Orientation'])) $return['orientation'] = $exif['Orientation'];
  461. else if (isset($exif['IFD0']['Orientation'])) $return['orientation'] = $exif['IFD0']['Orientation'];
  462. $temp = @$exif['ISOSpeedRatings'];
  463. if (isset($temp)) $return['iso'] = $temp;
  464. $temp = @$exif['COMPUTED']['ApertureFNumber'];
  465. if (isset($temp)) $return['aperture'] = $temp;
  466. $temp = @$exif['Make'];
  467. if (isset($temp)) $return['make'] = trim($temp);
  468. $temp = @$exif['Model'];
  469. if (isset($temp)) $return['model'] = trim($temp);
  470. $temp = @$exif['ExposureTime'];
  471. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' Sec.';
  472. $temp = @$exif['FocalLength'];
  473. if (isset($temp)) $return['focal'] = ($temp/1) . ' mm';
  474. $temp = @$exif['DateTimeOriginal'];
  475. if (isset($temp)) $return['takestamp'] = strtotime($temp);
  476. }
  477. # Call plugins
  478. $this->plugins(__METHOD__, 1, func_get_args());
  479. return $return;
  480. }
  481. public function getArchive() {
  482. # Check dependencies
  483. self::dependencies(isset($this->database, $this->photoIDs));
  484. # Call plugins
  485. $this->plugins(__METHOD__, 0, func_get_args());
  486. # Get photo
  487. $query = Database::prepare($this->database, "SELECT title, url FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  488. $photos = $this->database->query($query);
  489. $photo = $photos->fetch_object();
  490. # Get extension
  491. $extension = getExtension($photo->url);
  492. if ($extension===false) {
  493. Log::error($this->database, __METHOD__, __LINE__, 'Invalid photo extension');
  494. return false;
  495. }
  496. # Illicit chars
  497. $badChars = array_merge(
  498. array_map('chr', range(0,31)),
  499. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  500. );
  501. # Parse title
  502. if ($photo->title=='') $photo->title = 'Untitled';
  503. # Escape title
  504. $photo->title = str_replace($badChars, '', $photo->title);
  505. # Set headers
  506. header("Content-Type: application/octet-stream");
  507. header("Content-Disposition: attachment; filename=\"" . $photo->title . $extension . "\"");
  508. header("Content-Length: " . filesize(LYCHEE_UPLOADS_BIG . $photo->url));
  509. # Send file
  510. readfile(LYCHEE_UPLOADS_BIG . $photo->url);
  511. # Call plugins
  512. $this->plugins(__METHOD__, 1, func_get_args());
  513. return true;
  514. }
  515. public function setTitle($title) {
  516. # Check dependencies
  517. self::dependencies(isset($this->database, $this->photoIDs));
  518. # Call plugins
  519. $this->plugins(__METHOD__, 0, func_get_args());
  520. # Parse
  521. if (strlen($title)>50) $title = substr($title, 0, 50);
  522. # Set title
  523. $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $title, $this->photoIDs));
  524. $result = $this->database->query($query);
  525. # Call plugins
  526. $this->plugins(__METHOD__, 1, func_get_args());
  527. if (!$result) {
  528. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  529. return false;
  530. }
  531. return true;
  532. }
  533. public function setDescription($description) {
  534. # Check dependencies
  535. self::dependencies(isset($this->database, $this->photoIDs));
  536. # Call plugins
  537. $this->plugins(__METHOD__, 0, func_get_args());
  538. # Parse
  539. $description = htmlentities($description, ENT_COMPAT | ENT_HTML401, 'UTF-8');
  540. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  541. # Set description
  542. $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN ('?')", array(LYCHEE_TABLE_PHOTOS, $description, $this->photoIDs));
  543. $result = $this->database->query($query);
  544. # Call plugins
  545. $this->plugins(__METHOD__, 1, func_get_args());
  546. if (!$result) {
  547. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  548. return false;
  549. }
  550. return true;
  551. }
  552. public function setStar() {
  553. # Check dependencies
  554. self::dependencies(isset($this->database, $this->photoIDs));
  555. # Call plugins
  556. $this->plugins(__METHOD__, 0, func_get_args());
  557. # Init vars
  558. $error = false;
  559. # Get photos
  560. $query = Database::prepare($this->database, "SELECT id, star FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  561. $photos = $this->database->query($query);
  562. # For each photo
  563. while ($photo = $photos->fetch_object()) {
  564. # Invert star
  565. $star = ($photo->star==0 ? 1 : 0);
  566. # Set star
  567. $query = Database::prepare($this->database, "UPDATE ? SET star = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $star, $photo->id));
  568. $star = $this->database->query($query);
  569. if (!$star) $error = true;
  570. }
  571. # Call plugins
  572. $this->plugins(__METHOD__, 1, func_get_args());
  573. if ($error===true) {
  574. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  575. return false;
  576. }
  577. return true;
  578. }
  579. public function getPublic($password) {
  580. # Check dependencies
  581. self::dependencies(isset($this->database, $this->photoIDs));
  582. # Call plugins
  583. $this->plugins(__METHOD__, 0, func_get_args());
  584. # Get photo
  585. $query = Database::prepare($this->database, "SELECT public, album FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  586. $photos = $this->database->query($query);
  587. $photo = $photos->fetch_object();
  588. # Check if public
  589. if ($photo->public==1) return true;
  590. else {
  591. $album = new Album($this->database, null, null, $photo->album);
  592. $acP = $album->checkPassword($password);
  593. $agP = $album->getPublic();
  594. if ($acP===true&&$agP===true) return true;
  595. }
  596. # Call plugins
  597. $this->plugins(__METHOD__, 1, func_get_args());
  598. return false;
  599. }
  600. public function setPublic() {
  601. # Check dependencies
  602. self::dependencies(isset($this->database, $this->photoIDs));
  603. # Call plugins
  604. $this->plugins(__METHOD__, 0, func_get_args());
  605. # Get public
  606. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  607. $photos = $this->database->query($query);
  608. $photo = $photos->fetch_object();
  609. # Invert public
  610. $public = ($photo->public==0 ? 1 : 0);
  611. # Set public
  612. $query = Database::prepare($this->database, "UPDATE ? SET public = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $public, $this->photoIDs));
  613. $result = $this->database->query($query);
  614. # Call plugins
  615. $this->plugins(__METHOD__, 1, func_get_args());
  616. if (!$result) {
  617. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  618. return false;
  619. }
  620. return true;
  621. }
  622. function setAlbum($albumID) {
  623. # Check dependencies
  624. self::dependencies(isset($this->database, $this->photoIDs));
  625. # Call plugins
  626. $this->plugins(__METHOD__, 0, func_get_args());
  627. # Set album
  628. $query = Database::prepare($this->database, "UPDATE ? SET album = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->photoIDs));
  629. $result = $this->database->query($query);
  630. # Call plugins
  631. $this->plugins(__METHOD__, 1, func_get_args());
  632. if (!$result) {
  633. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  634. return false;
  635. }
  636. return true;
  637. }
  638. public function setTags($tags) {
  639. # Check dependencies
  640. self::dependencies(isset($this->database, $this->photoIDs));
  641. # Call plugins
  642. $this->plugins(__METHOD__, 0, func_get_args());
  643. # Parse tags
  644. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  645. $tags = preg_replace('/,$|^,|(\ ){0,}$/', '', $tags);
  646. if (strlen($tags)>1000) {
  647. Log::notice($this->database, __METHOD__, __LINE__, 'Length of tags higher than 1000');
  648. return false;
  649. }
  650. # Set tags
  651. $query = Database::prepare($this->database, "UPDATE ? SET tags = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $tags, $this->photoIDs));
  652. $result = $this->database->query($query);
  653. # Call plugins
  654. $this->plugins(__METHOD__, 1, func_get_args());
  655. if (!$result) {
  656. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  657. return false;
  658. }
  659. return true;
  660. }
  661. public function duplicate() {
  662. # Check dependencies
  663. self::dependencies(isset($this->database, $this->photoIDs));
  664. # Call plugins
  665. $this->plugins(__METHOD__, 0, func_get_args());
  666. # Get photos
  667. $query = Database::prepare($this->database, "SELECT id, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  668. $photos = $this->database->query($query);
  669. if (!$photos) {
  670. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  671. return false;
  672. }
  673. # For each photo
  674. while ($photo = $photos->fetch_object()) {
  675. # Generate id
  676. $id = str_replace('.', '', microtime(true));
  677. while(strlen($id)<14) $id .= 0;
  678. # Duplicate entry
  679. $values = array(LYCHEE_TABLE_PHOTOS, $id, LYCHEE_TABLE_PHOTOS, $photo->id);
  680. $query = Database::prepare($this->database, "INSERT INTO ? (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum) SELECT '?' AS id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum FROM ? WHERE id = '?'", $values);
  681. $duplicate = $this->database->query($query);
  682. if (!$duplicate) {
  683. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  684. return false;
  685. }
  686. }
  687. return true;
  688. }
  689. public function delete() {
  690. # Check dependencies
  691. self::dependencies(isset($this->database, $this->photoIDs));
  692. # Call plugins
  693. $this->plugins(__METHOD__, 0, func_get_args());
  694. # Get photos
  695. $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  696. $photos = $this->database->query($query);
  697. if (!$photos) {
  698. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  699. return false;
  700. }
  701. # For each photo
  702. while ($photo = $photos->fetch_object()) {
  703. # Check if other photos are referring to this images
  704. # If so, only delete the db entry
  705. if ($this->exists($photo->checksum, $photo->id)===false) {
  706. # Get retina thumb url
  707. $thumbUrl2x = explode(".", $photo->thumbUrl);
  708. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  709. # Delete big
  710. if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) {
  711. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
  712. return false;
  713. }
  714. # Delete medium
  715. if (file_exists(LYCHEE_UPLOADS_MEDIUM . $photo->url)&&!unlink(LYCHEE_UPLOADS_MEDIUM . $photo->url)) {
  716. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/medium/');
  717. return false;
  718. }
  719. # Delete thumb
  720. if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) {
  721. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
  722. return false;
  723. }
  724. # Delete thumb@2x
  725. if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
  726. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
  727. return false;
  728. }
  729. }
  730. # Delete db entry
  731. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $photo->id));
  732. $delete = $this->database->query($query);
  733. if (!$delete) {
  734. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  735. return false;
  736. }
  737. }
  738. # Call plugins
  739. $this->plugins(__METHOD__, 1, func_get_args());
  740. return true;
  741. }
  742. }
  743. ?>