Photo.php 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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)===false||
  35. hasPermissions(LYCHEE_UPLOADS_BIG)===false||
  36. hasPermissions(LYCHEE_UPLOADS_THUMB)===false) {
  37. Log::error($this->database, __METHOD__, __LINE__, 'An upload-folder is missing or not readable and writable');
  38. exit('Error: An upload-folder is missing or not readable and writable!');
  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. # Set to true when creation of medium-photo failed
  250. $error = false;
  251. # Size of the medium-photo
  252. # When changing these values,
  253. # also change the size detection in the front-end
  254. $newWidth = 1920;
  255. $newHeight = 1080;
  256. # Check permissions
  257. if (hasPermissions(LYCHEE_UPLOADS_MEDIUM)===false) {
  258. # Permissions are missing
  259. Log::notice($this->database, __METHOD__, __LINE__, 'Skipped creation of medium-photo, because uploads/medium/ is missing or not readable and writable.');
  260. $error = true;
  261. }
  262. # Is photo big enough?
  263. # Is medium activated?
  264. # Is Imagick installed and activated?
  265. if (($error===false)&&
  266. ($width>$newWidth||$height>$newHeight)&&
  267. ($this->settings['medium']==='1')&&
  268. (extension_loaded('imagick')&&$this->settings['imagick']==='1')) {
  269. $newUrl = LYCHEE_UPLOADS_MEDIUM . $filename;
  270. # Read image
  271. $medium = new Imagick();
  272. $medium->readImage($url);
  273. # Adjust image
  274. $medium->scaleImage($newWidth, $newHeight, true);
  275. # Save image
  276. try { $medium->writeImage($newUrl); }
  277. catch (ImagickException $err) {
  278. Log::notice($this->database, __METHOD__, __LINE__, 'Could not save medium-photo: ' . $err->getMessage());
  279. $error = true;
  280. }
  281. $medium->clear();
  282. $medium->destroy();
  283. } else {
  284. # Photo too small or
  285. # Medium is deactivated or
  286. # Imagick not installed
  287. $error = true;
  288. }
  289. # Call plugins
  290. $this->plugins(__METHOD__, 1, func_get_args());
  291. if ($error===true) return false;
  292. return true;
  293. }
  294. public function adjustFile($path, $info) {
  295. # Check dependencies
  296. self::dependencies(isset($path, $info));
  297. # Call plugins
  298. $this->plugins(__METHOD__, 0, func_get_args());
  299. $swapSize = false;
  300. if (extension_loaded('imagick')&&$this->settings['imagick']==='1') {
  301. $rotateImage = 0;
  302. switch ($info['orientation']) {
  303. case 3:
  304. $rotateImage = 180;
  305. break;
  306. case 6:
  307. $rotateImage = 90;
  308. $swapSize = true;
  309. break;
  310. case 8:
  311. $rotateImage = 270;
  312. $swapSize = true;
  313. break;
  314. default:
  315. return false;
  316. break;
  317. }
  318. if ($rotateImage!==0) {
  319. $image = new Imagick();
  320. $image->readImage($path);
  321. $image->rotateImage(new ImagickPixel(), $rotateImage);
  322. $image->setImageOrientation(1);
  323. $image->writeImage($path);
  324. $image->clear();
  325. $image->destroy();
  326. }
  327. } else {
  328. $newWidth = $info['width'];
  329. $newHeight = $info['height'];
  330. $sourceImg = imagecreatefromjpeg($path);
  331. switch ($info['orientation']) {
  332. case 2:
  333. # mirror
  334. # not yet implemented
  335. return false;
  336. break;
  337. case 3:
  338. $process = true;
  339. $sourceImg = imagerotate($sourceImg, -180, 0);
  340. break;
  341. case 4:
  342. # rotate 180 and mirror
  343. # not yet implemented
  344. return false;
  345. break;
  346. case 5:
  347. # rotate 90 and mirror
  348. # not yet implemented
  349. return false;
  350. break;
  351. case 6:
  352. $process = true;
  353. $sourceImg = imagerotate($sourceImg, -90, 0);
  354. $newWidth = $info['height'];
  355. $newHeight = $info['width'];
  356. $swapSize = true;
  357. break;
  358. case 7:
  359. # rotate -90 and mirror
  360. # not yet implemented
  361. return false;
  362. break;
  363. case 8:
  364. $process = true;
  365. $sourceImg = imagerotate($sourceImg, 90, 0);
  366. $newWidth = $info['height'];
  367. $newHeight = $info['width'];
  368. $swapSize = true;
  369. break;
  370. default:
  371. return false;
  372. break;
  373. }
  374. # Recreate photo
  375. $newSourceImg = imagecreatetruecolor($newWidth, $newHeight);
  376. imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
  377. imagejpeg($newSourceImg, $path, 100);
  378. # Free memory
  379. imagedestroy($sourceImg);
  380. imagedestroy($newSourceImg);
  381. }
  382. # Call plugins
  383. $this->plugins(__METHOD__, 1, func_get_args());
  384. # SwapSize should be true when the image has been rotated
  385. # Return new dimensions in this case
  386. if ($swapSize===true) {
  387. $swapSize = $info['width'];
  388. $info['width'] = $info['height'];
  389. $info['height'] = $swapSize;
  390. }
  391. return $info;
  392. }
  393. public function get($albumID) {
  394. # Check dependencies
  395. self::dependencies(isset($this->database, $this->photoIDs));
  396. # Call plugins
  397. $this->plugins(__METHOD__, 0, func_get_args());
  398. # Get photo
  399. $query = Database::prepare($this->database, "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  400. $photos = $this->database->query($query);
  401. $photo = $photos->fetch_assoc();
  402. # Parse photo
  403. $photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
  404. if (strlen($photo['takestamp'])>1) $photo['takedate'] = date('d M. Y', $photo['takestamp']);
  405. # Parse medium
  406. if ($photo['medium']==='1') $photo['medium'] = LYCHEE_URL_UPLOADS_MEDIUM . $photo['url'];
  407. else $photo['medium'] = '';
  408. # Parse paths
  409. $photo['url'] = LYCHEE_URL_UPLOADS_BIG . $photo['url'];
  410. $photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $photo['thumbUrl'];
  411. if ($albumID!='false') {
  412. # Show photo as public when parent album is public
  413. # Check if parent album is available and not photo not unsorted
  414. if ($photo['album']!=0) {
  415. # Get album
  416. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $photo['album']));
  417. $albums = $this->database->query($query);
  418. $album = $albums->fetch_assoc();
  419. # Parse album
  420. $photo['public'] = ($album['public']=='1' ? '2' : $photo['public']);
  421. }
  422. $photo['original_album'] = $photo['album'];
  423. $photo['album'] = $albumID;
  424. }
  425. # Call plugins
  426. $this->plugins(__METHOD__, 1, func_get_args());
  427. return $photo;
  428. }
  429. public function getInfo($url) {
  430. # Check dependencies
  431. self::dependencies(isset($this->database, $url));
  432. # Call plugins
  433. $this->plugins(__METHOD__, 0, func_get_args());
  434. $iptcArray = array();
  435. $info = getimagesize($url, $iptcArray);
  436. # General information
  437. $return['type'] = $info['mime'];
  438. $return['width'] = $info[0];
  439. $return['height'] = $info[1];
  440. # Size
  441. $size = filesize($url)/1024;
  442. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  443. else $return['size'] = round($size, 1) . ' KB';
  444. # IPTC Metadata Fallback
  445. $return['title'] = '';
  446. $return['description'] = '';
  447. # IPTC Metadata
  448. if(isset($iptcArray['APP13'])) {
  449. $iptcInfo = iptcparse($iptcArray['APP13']);
  450. if (is_array($iptcInfo)) {
  451. $temp = @$iptcInfo['2#105'][0];
  452. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  453. $temp = @$iptcInfo['2#120'][0];
  454. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  455. $temp = @$iptcInfo['2#005'][0];
  456. if (isset($temp)&&strlen($temp)>0&&$return['title']==='') $return['title'] = $temp;
  457. }
  458. }
  459. # EXIF Metadata Fallback
  460. $return['orientation'] = '';
  461. $return['iso'] = '';
  462. $return['aperture'] = '';
  463. $return['make'] = '';
  464. $return['model'] = '';
  465. $return['shutter'] = '';
  466. $return['focal'] = '';
  467. $return['takestamp'] = 0;
  468. # Read EXIF
  469. if ($info['mime']=='image/jpeg') $exif = @exif_read_data($url, 'EXIF', 0);
  470. else $exif = false;
  471. # EXIF Metadata
  472. if ($exif!==false) {
  473. if (isset($exif['Orientation'])) $return['orientation'] = $exif['Orientation'];
  474. else if (isset($exif['IFD0']['Orientation'])) $return['orientation'] = $exif['IFD0']['Orientation'];
  475. $temp = @$exif['ISOSpeedRatings'];
  476. if (isset($temp)) $return['iso'] = $temp;
  477. $temp = @$exif['COMPUTED']['ApertureFNumber'];
  478. if (isset($temp)) $return['aperture'] = $temp;
  479. $temp = @$exif['Make'];
  480. if (isset($temp)) $return['make'] = trim($temp);
  481. $temp = @$exif['Model'];
  482. if (isset($temp)) $return['model'] = trim($temp);
  483. $temp = @$exif['ExposureTime'];
  484. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' Sec.';
  485. $temp = @$exif['FocalLength'];
  486. if (isset($temp)) $return['focal'] = ($temp/1) . ' mm';
  487. $temp = @$exif['DateTimeOriginal'];
  488. if (isset($temp)) $return['takestamp'] = strtotime($temp);
  489. }
  490. # Call plugins
  491. $this->plugins(__METHOD__, 1, func_get_args());
  492. return $return;
  493. }
  494. public function getArchive() {
  495. # Check dependencies
  496. self::dependencies(isset($this->database, $this->photoIDs));
  497. # Call plugins
  498. $this->plugins(__METHOD__, 0, func_get_args());
  499. # Get photo
  500. $query = Database::prepare($this->database, "SELECT title, url FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  501. $photos = $this->database->query($query);
  502. $photo = $photos->fetch_object();
  503. # Get extension
  504. $extension = getExtension($photo->url);
  505. if ($extension===false) {
  506. Log::error($this->database, __METHOD__, __LINE__, 'Invalid photo extension');
  507. return false;
  508. }
  509. # Illicit chars
  510. $badChars = array_merge(
  511. array_map('chr', range(0,31)),
  512. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  513. );
  514. # Parse title
  515. if ($photo->title=='') $photo->title = 'Untitled';
  516. # Escape title
  517. $photo->title = str_replace($badChars, '', $photo->title);
  518. # Set headers
  519. header("Content-Type: application/octet-stream");
  520. header("Content-Disposition: attachment; filename=\"" . $photo->title . $extension . "\"");
  521. header("Content-Length: " . filesize(LYCHEE_UPLOADS_BIG . $photo->url));
  522. # Send file
  523. readfile(LYCHEE_UPLOADS_BIG . $photo->url);
  524. # Call plugins
  525. $this->plugins(__METHOD__, 1, func_get_args());
  526. return true;
  527. }
  528. public function setTitle($title) {
  529. # Check dependencies
  530. self::dependencies(isset($this->database, $this->photoIDs));
  531. # Call plugins
  532. $this->plugins(__METHOD__, 0, func_get_args());
  533. # Parse
  534. if (strlen($title)>50) $title = substr($title, 0, 50);
  535. # Set title
  536. $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $title, $this->photoIDs));
  537. $result = $this->database->query($query);
  538. # Call plugins
  539. $this->plugins(__METHOD__, 1, func_get_args());
  540. if (!$result) {
  541. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  542. return false;
  543. }
  544. return true;
  545. }
  546. public function setDescription($description) {
  547. # Check dependencies
  548. self::dependencies(isset($this->database, $this->photoIDs));
  549. # Call plugins
  550. $this->plugins(__METHOD__, 0, func_get_args());
  551. # Parse
  552. $description = htmlentities($description, ENT_COMPAT | ENT_HTML401, 'UTF-8');
  553. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  554. # Set description
  555. $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN ('?')", array(LYCHEE_TABLE_PHOTOS, $description, $this->photoIDs));
  556. $result = $this->database->query($query);
  557. # Call plugins
  558. $this->plugins(__METHOD__, 1, func_get_args());
  559. if (!$result) {
  560. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  561. return false;
  562. }
  563. return true;
  564. }
  565. public function setStar() {
  566. # Check dependencies
  567. self::dependencies(isset($this->database, $this->photoIDs));
  568. # Call plugins
  569. $this->plugins(__METHOD__, 0, func_get_args());
  570. # Init vars
  571. $error = false;
  572. # Get photos
  573. $query = Database::prepare($this->database, "SELECT id, star FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  574. $photos = $this->database->query($query);
  575. # For each photo
  576. while ($photo = $photos->fetch_object()) {
  577. # Invert star
  578. $star = ($photo->star==0 ? 1 : 0);
  579. # Set star
  580. $query = Database::prepare($this->database, "UPDATE ? SET star = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $star, $photo->id));
  581. $star = $this->database->query($query);
  582. if (!$star) $error = true;
  583. }
  584. # Call plugins
  585. $this->plugins(__METHOD__, 1, func_get_args());
  586. if ($error===true) {
  587. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  588. return false;
  589. }
  590. return true;
  591. }
  592. public function getPublic($password) {
  593. # Check dependencies
  594. self::dependencies(isset($this->database, $this->photoIDs));
  595. # Call plugins
  596. $this->plugins(__METHOD__, 0, func_get_args());
  597. # Get photo
  598. $query = Database::prepare($this->database, "SELECT public, album FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  599. $photos = $this->database->query($query);
  600. $photo = $photos->fetch_object();
  601. # Check if public
  602. if ($photo->public==1) return true;
  603. else {
  604. $album = new Album($this->database, null, null, $photo->album);
  605. $acP = $album->checkPassword($password);
  606. $agP = $album->getPublic();
  607. if ($acP===true&&$agP===true) return true;
  608. }
  609. # Call plugins
  610. $this->plugins(__METHOD__, 1, func_get_args());
  611. return false;
  612. }
  613. public function setPublic() {
  614. # Check dependencies
  615. self::dependencies(isset($this->database, $this->photoIDs));
  616. # Call plugins
  617. $this->plugins(__METHOD__, 0, func_get_args());
  618. # Get public
  619. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  620. $photos = $this->database->query($query);
  621. $photo = $photos->fetch_object();
  622. # Invert public
  623. $public = ($photo->public==0 ? 1 : 0);
  624. # Set public
  625. $query = Database::prepare($this->database, "UPDATE ? SET public = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $public, $this->photoIDs));
  626. $result = $this->database->query($query);
  627. # Call plugins
  628. $this->plugins(__METHOD__, 1, func_get_args());
  629. if (!$result) {
  630. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  631. return false;
  632. }
  633. return true;
  634. }
  635. function setAlbum($albumID) {
  636. # Check dependencies
  637. self::dependencies(isset($this->database, $this->photoIDs));
  638. # Call plugins
  639. $this->plugins(__METHOD__, 0, func_get_args());
  640. # Set album
  641. $query = Database::prepare($this->database, "UPDATE ? SET album = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->photoIDs));
  642. $result = $this->database->query($query);
  643. # Call plugins
  644. $this->plugins(__METHOD__, 1, func_get_args());
  645. if (!$result) {
  646. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  647. return false;
  648. }
  649. return true;
  650. }
  651. public function setTags($tags) {
  652. # Check dependencies
  653. self::dependencies(isset($this->database, $this->photoIDs));
  654. # Call plugins
  655. $this->plugins(__METHOD__, 0, func_get_args());
  656. # Parse tags
  657. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  658. $tags = preg_replace('/,$|^,|(\ ){0,}$/', '', $tags);
  659. if (strlen($tags)>1000) {
  660. Log::notice($this->database, __METHOD__, __LINE__, 'Length of tags higher than 1000');
  661. return false;
  662. }
  663. # Set tags
  664. $query = Database::prepare($this->database, "UPDATE ? SET tags = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $tags, $this->photoIDs));
  665. $result = $this->database->query($query);
  666. # Call plugins
  667. $this->plugins(__METHOD__, 1, func_get_args());
  668. if (!$result) {
  669. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  670. return false;
  671. }
  672. return true;
  673. }
  674. public function duplicate() {
  675. # Check dependencies
  676. self::dependencies(isset($this->database, $this->photoIDs));
  677. # Call plugins
  678. $this->plugins(__METHOD__, 0, func_get_args());
  679. # Get photos
  680. $query = Database::prepare($this->database, "SELECT id, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  681. $photos = $this->database->query($query);
  682. if (!$photos) {
  683. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  684. return false;
  685. }
  686. # For each photo
  687. while ($photo = $photos->fetch_object()) {
  688. # Generate id
  689. $id = str_replace('.', '', microtime(true));
  690. while(strlen($id)<14) $id .= 0;
  691. # Duplicate entry
  692. $values = array(LYCHEE_TABLE_PHOTOS, $id, LYCHEE_TABLE_PHOTOS, $photo->id);
  693. $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);
  694. $duplicate = $this->database->query($query);
  695. if (!$duplicate) {
  696. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  697. return false;
  698. }
  699. }
  700. return true;
  701. }
  702. public function delete() {
  703. # Check dependencies
  704. self::dependencies(isset($this->database, $this->photoIDs));
  705. # Call plugins
  706. $this->plugins(__METHOD__, 0, func_get_args());
  707. # Get photos
  708. $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  709. $photos = $this->database->query($query);
  710. if (!$photos) {
  711. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  712. return false;
  713. }
  714. # For each photo
  715. while ($photo = $photos->fetch_object()) {
  716. # Check if other photos are referring to this images
  717. # If so, only delete the db entry
  718. if ($this->exists($photo->checksum, $photo->id)===false) {
  719. # Get retina thumb url
  720. $thumbUrl2x = explode(".", $photo->thumbUrl);
  721. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  722. # Delete big
  723. if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) {
  724. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
  725. return false;
  726. }
  727. # Delete medium
  728. if (file_exists(LYCHEE_UPLOADS_MEDIUM . $photo->url)&&!unlink(LYCHEE_UPLOADS_MEDIUM . $photo->url)) {
  729. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/medium/');
  730. return false;
  731. }
  732. # Delete thumb
  733. if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) {
  734. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
  735. return false;
  736. }
  737. # Delete thumb@2x
  738. if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
  739. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
  740. return false;
  741. }
  742. }
  743. # Delete db entry
  744. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $photo->id));
  745. $delete = $this->database->query($query);
  746. if (!$delete) {
  747. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  748. return false;
  749. }
  750. }
  751. # Call plugins
  752. $this->plugins(__METHOD__, 1, func_get_args());
  753. return true;
  754. }
  755. }
  756. ?>