Photo.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. <?php
  2. ###
  3. # @name Photo Module
  4. # @copyright 2015 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. public static $validTypes = array(
  12. IMAGETYPE_JPEG,
  13. IMAGETYPE_GIF,
  14. IMAGETYPE_PNG
  15. );
  16. public static $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 = 0, $description = '', $tags = '', $returnOnError = false) {
  31. # Use $returnOnError if you want to handle errors by your own
  32. # e.g. when calling this functions inside an if-condition
  33. # Check dependencies
  34. self::dependencies(isset($this->database, $this->settings, $files));
  35. # Check permissions
  36. if (hasPermissions(LYCHEE_UPLOADS)===false||
  37. hasPermissions(LYCHEE_UPLOADS_BIG)===false||
  38. hasPermissions(LYCHEE_UPLOADS_THUMB)===false) {
  39. Log::error($this->database, __METHOD__, __LINE__, 'An upload-folder is missing or not readable and writable');
  40. exit('Error: An upload-folder is missing or not readable and writable!');
  41. }
  42. # Call plugins
  43. $this->plugins(__METHOD__, 0, func_get_args());
  44. switch($albumID) {
  45. case 's':
  46. # s for public (share)
  47. $public = 1;
  48. $star = 0;
  49. $albumID = 0;
  50. break;
  51. case 'f':
  52. # f for starred (fav)
  53. $star = 1;
  54. $public = 0;
  55. $albumID = 0;
  56. break;
  57. case 'r':
  58. # r for recent
  59. $public = 0;
  60. $star = 0;
  61. $albumID = 0;
  62. break;
  63. default:
  64. $star = 0;
  65. $public = 0;
  66. break;
  67. }
  68. foreach ($files as $file) {
  69. # Check if file exceeds the upload_max_filesize directive
  70. if ($file['error']===UPLOAD_ERR_INI_SIZE) {
  71. Log::error($this->database, __METHOD__, __LINE__, 'The uploaded file exceeds the upload_max_filesize directive in php.ini');
  72. if ($returnOnError===true) return false;
  73. exit('Error: The uploaded file exceeds the upload_max_filesize directive in php.ini!');
  74. }
  75. # Check if file was only partially uploaded
  76. if ($file['error']===UPLOAD_ERR_PARTIAL) {
  77. Log::error($this->database, __METHOD__, __LINE__, 'The uploaded file was only partially uploaded');
  78. if ($returnOnError===true) return false;
  79. exit('Error: The uploaded file was only partially uploaded!');
  80. }
  81. # Check if writing file to disk failed
  82. if ($file['error']===UPLOAD_ERR_CANT_WRITE) {
  83. Log::error($this->database, __METHOD__, __LINE__, 'Failed to write photo to disk');
  84. if ($returnOnError===true) return false;
  85. exit('Error: Failed to write photo to disk!');
  86. }
  87. # Check if a extension stopped the file upload
  88. if ($file['error']===UPLOAD_ERR_EXTENSION) {
  89. Log::error($this->database, __METHOD__, __LINE__, 'A PHP extension stopped the file upload');
  90. if ($returnOnError===true) return false;
  91. exit('Error: A PHP extension stopped the file upload!');
  92. }
  93. # Check if the upload was successful
  94. if ($file['error']!==UPLOAD_ERR_OK) {
  95. Log::error($this->database, __METHOD__, __LINE__, 'Upload contains an error (' . $file['error'] . ')');
  96. if ($returnOnError===true) return false;
  97. exit('Error: Upload failed!');
  98. }
  99. # Verify extension
  100. $extension = getExtension($file['name']);
  101. if (!in_array(strtolower($extension), Photo::$validExtensions, true)) {
  102. Log::error($this->database, __METHOD__, __LINE__, 'Photo format not supported');
  103. if ($returnOnError===true) return false;
  104. exit('Error: Photo format not supported!');
  105. }
  106. # Verify image
  107. $type = @exif_imagetype($file['tmp_name']);
  108. if (!in_array($type, Photo::$validTypes, true)) {
  109. Log::error($this->database, __METHOD__, __LINE__, 'Photo type not supported');
  110. if ($returnOnError===true) return false;
  111. exit('Error: Photo type not supported!');
  112. }
  113. # Generate id
  114. $id = str_replace('.', '', microtime(true));
  115. while(strlen($id)<14) $id .= 0;
  116. # Set paths
  117. $tmp_name = $file['tmp_name'];
  118. $photo_name = md5($id) . $extension;
  119. $path = LYCHEE_UPLOADS_BIG . $photo_name;
  120. # Calculate checksum
  121. $checksum = sha1_file($tmp_name);
  122. if ($checksum===false) {
  123. Log::error($this->database, __METHOD__, __LINE__, 'Could not calculate checksum for photo');
  124. if ($returnOnError===true) return false;
  125. exit('Error: Could not calculate checksum for photo!');
  126. }
  127. # Check if image exists based on checksum
  128. if ($checksum===false) {
  129. $checksum = '';
  130. $exists = false;
  131. } else {
  132. $exists = $this->exists($checksum);
  133. if ($exists!==false) {
  134. $photo_name = $exists['photo_name'];
  135. $path = $exists['path'];
  136. $path_thumb = $exists['path_thumb'];
  137. $medium = ($exists['medium']==='1' ? 1 : 0);
  138. $exists = true;
  139. }
  140. }
  141. if ($exists===false) {
  142. # Import if not uploaded via web
  143. if (!is_uploaded_file($tmp_name)) {
  144. if (!@copy($tmp_name, $path)) {
  145. Log::error($this->database, __METHOD__, __LINE__, 'Could not copy photo to uploads');
  146. if ($returnOnError===true) return false;
  147. exit('Error: Could not copy photo to uploads!');
  148. } else @unlink($tmp_name);
  149. } else {
  150. if (!@move_uploaded_file($tmp_name, $path)) {
  151. Log::error($this->database, __METHOD__, __LINE__, 'Could not move photo to uploads');
  152. if ($returnOnError===true) return false;
  153. exit('Error: Could not move photo to uploads!');
  154. }
  155. }
  156. } else {
  157. # Photo already exists
  158. # Check if the user wants to skip duplicates
  159. if ($this->settings['skipDuplicates']==='1') {
  160. Log::notice($this->database, __METHOD__, __LINE__, 'Skipped upload of existing photo because skipDuplicates is activated');
  161. if ($returnOnError===true) return false;
  162. exit('Warning: This photo has been skipped because it\'s already in your library.');
  163. }
  164. }
  165. # Read infos
  166. $info = $this->getInfo($path);
  167. # Use title of file if IPTC title missing
  168. if ($info['title']==='') $info['title'] = substr(basename($file['name'], $extension), 0, 30);
  169. # Use description parameter if set
  170. if ($description==='') $description = $info['description'];
  171. if ($exists===false) {
  172. # Set orientation based on EXIF data
  173. if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!=='') {
  174. $adjustFile = $this->adjustFile($path, $info);
  175. if ($adjustFile!==false) $info = $adjustFile;
  176. else Log::notice($this->database, __METHOD__, __LINE__, 'Skipped adjustment of photo (' . $info['title'] . ')');
  177. }
  178. # Set original date
  179. if ($info['takestamp']!==''&&$info['takestamp']!==0) @touch($path, $info['takestamp']);
  180. # Create Thumb
  181. if (!$this->createThumb($path, $photo_name, $info['type'], $info['width'], $info['height'])) {
  182. Log::error($this->database, __METHOD__, __LINE__, 'Could not create thumbnail for photo');
  183. if ($returnOnError===true) return false;
  184. exit('Error: Could not create thumbnail for photo!');
  185. }
  186. # Create Medium
  187. if ($this->createMedium($path, $photo_name, $info['width'], $info['height'])) $medium = 1;
  188. else $medium = 0;
  189. # Set thumb url
  190. $path_thumb = md5($id) . '.jpeg';
  191. }
  192. # Save to DB
  193. $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);
  194. $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);
  195. $result = $this->database->query($query);
  196. if (!$result) {
  197. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  198. if ($returnOnError===true) return false;
  199. exit('Error: Could not save photo in database!');
  200. }
  201. }
  202. # Call plugins
  203. $this->plugins(__METHOD__, 1, func_get_args());
  204. return true;
  205. }
  206. private function exists($checksum, $photoID = null) {
  207. # Check dependencies
  208. self::dependencies(isset($this->database, $checksum));
  209. # Exclude $photoID from select when $photoID is set
  210. 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));
  211. else $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, medium FROM ? WHERE checksum = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $checksum));
  212. $result = $this->database->query($query);
  213. if (!$result) {
  214. Log::error($this->database, __METHOD__, __LINE__, 'Could not check for existing photos with the same checksum');
  215. return false;
  216. }
  217. if ($result->num_rows===1) {
  218. $result = $result->fetch_object();
  219. $return = array(
  220. 'photo_name' => $result->url,
  221. 'path' => LYCHEE_UPLOADS_BIG . $result->url,
  222. 'path_thumb' => $result->thumbUrl,
  223. 'medium' => $result->medium
  224. );
  225. return $return;
  226. }
  227. return false;
  228. }
  229. private function createThumb($url, $filename, $type, $width, $height) {
  230. # Check dependencies
  231. self::dependencies(isset($this->database, $this->settings, $url, $filename, $type, $width, $height));
  232. # Call plugins
  233. $this->plugins(__METHOD__, 0, func_get_args());
  234. # Size of the thumbnail
  235. $newWidth = 200;
  236. $newHeight = 200;
  237. $photoName = explode('.', $filename);
  238. $newUrl = LYCHEE_UPLOADS_THUMB . $photoName[0] . '.jpeg';
  239. $newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName[0] . '@2x.jpeg';
  240. # Create thumbnails with Imagick
  241. if(extension_loaded('imagick')&&$this->settings['imagick']==='1') {
  242. # Read image
  243. $thumb = new Imagick();
  244. $thumb->readImage($url);
  245. $thumb->setImageCompressionQuality($this->settings['thumbQuality']);
  246. $thumb->setImageFormat('jpeg');
  247. # Copy image for 2nd thumb version
  248. $thumb2x = clone $thumb;
  249. # Create 1st version
  250. $thumb->cropThumbnailImage($newWidth, $newHeight);
  251. $thumb->writeImage($newUrl);
  252. $thumb->clear();
  253. $thumb->destroy();
  254. # Create 2nd version
  255. $thumb2x->cropThumbnailImage($newWidth*2, $newHeight*2);
  256. $thumb2x->writeImage($newUrl2x);
  257. $thumb2x->clear();
  258. $thumb2x->destroy();
  259. } else {
  260. # Create image
  261. $thumb = imagecreatetruecolor($newWidth, $newHeight);
  262. $thumb2x = imagecreatetruecolor($newWidth*2, $newHeight*2);
  263. # Set position
  264. if ($width<$height) {
  265. $newSize = $width;
  266. $startWidth = 0;
  267. $startHeight = $height/2 - $width/2;
  268. } else {
  269. $newSize = $height;
  270. $startWidth = $width/2 - $height/2;
  271. $startHeight = 0;
  272. }
  273. # Create new image
  274. switch($type) {
  275. case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break;
  276. case 'image/png': $sourceImg = imagecreatefrompng($url); break;
  277. case 'image/gif': $sourceImg = imagecreatefromgif($url); break;
  278. default: Log::error($this->database, __METHOD__, __LINE__, 'Type of photo is not supported');
  279. return false;
  280. break;
  281. }
  282. # Create thumb
  283. fastimagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth, $newHeight, $newSize, $newSize);
  284. imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']);
  285. imagedestroy($thumb);
  286. # Create retina thumb
  287. fastimagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth*2, $newHeight*2, $newSize, $newSize);
  288. imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']);
  289. imagedestroy($thumb2x);
  290. # Free memory
  291. imagedestroy($sourceImg);
  292. }
  293. # Call plugins
  294. $this->plugins(__METHOD__, 1, func_get_args());
  295. return true;
  296. }
  297. private function createMedium($url, $filename, $width, $height) {
  298. # Function creates a smaller version of a photo when its size is bigger than a preset size
  299. # Excepts the following:
  300. # (string) $url = Path to the photo-file
  301. # (string) $filename = Name of the photo-file
  302. # (int) $width = Width of the photo
  303. # (int) $height = Height of the photo
  304. # Returns the following
  305. # (boolean) true = Success
  306. # (boolean) false = Failure
  307. # Check dependencies
  308. self::dependencies(isset($this->database, $this->settings, $url, $filename, $width, $height));
  309. # Call plugins
  310. $this->plugins(__METHOD__, 0, func_get_args());
  311. # Set to true when creation of medium-photo failed
  312. $error = false;
  313. # Size of the medium-photo
  314. # When changing these values,
  315. # also change the size detection in the front-end
  316. $newWidth = 1920;
  317. $newHeight = 1080;
  318. # Check permissions
  319. if (hasPermissions(LYCHEE_UPLOADS_MEDIUM)===false) {
  320. # Permissions are missing
  321. Log::notice($this->database, __METHOD__, __LINE__, 'Skipped creation of medium-photo, because uploads/medium/ is missing or not readable and writable.');
  322. $error = true;
  323. }
  324. # Is photo big enough?
  325. # Is medium activated?
  326. # Is Imagick installed and activated?
  327. if (($error===false)&&
  328. ($width>$newWidth||$height>$newHeight)&&
  329. ($this->settings['medium']==='1')&&
  330. (extension_loaded('imagick')&&$this->settings['imagick']==='1')) {
  331. $newUrl = LYCHEE_UPLOADS_MEDIUM . $filename;
  332. # Read image
  333. $medium = new Imagick();
  334. $medium->readImage($url);
  335. # Adjust image
  336. $medium->scaleImage($newWidth, $newHeight, true);
  337. # Save image
  338. try { $medium->writeImage($newUrl); }
  339. catch (ImagickException $err) {
  340. Log::notice($this->database, __METHOD__, __LINE__, 'Could not save medium-photo: ' . $err->getMessage());
  341. $error = true;
  342. }
  343. $medium->clear();
  344. $medium->destroy();
  345. } else {
  346. # Photo too small or
  347. # Medium is deactivated or
  348. # Imagick not installed
  349. $error = true;
  350. }
  351. # Call plugins
  352. $this->plugins(__METHOD__, 1, func_get_args());
  353. if ($error===true) return false;
  354. return true;
  355. }
  356. public function adjustFile($path, $info) {
  357. # Function rotates and flips a photo based on its EXIF orientation
  358. # Excepts the following:
  359. # (string) $path = Path to the photo-file
  360. # (array) $info = ['orientation', 'width', 'height']
  361. # Returns the following
  362. # (array) $info = ['orientation', 'width', 'height'] = Success
  363. # (boolean) false = Failure
  364. # Check dependencies
  365. self::dependencies(isset($path, $info));
  366. # Call plugins
  367. $this->plugins(__METHOD__, 0, func_get_args());
  368. $swapSize = false;
  369. if (extension_loaded('imagick')&&$this->settings['imagick']==='1') {
  370. switch ($info['orientation']) {
  371. case 3:
  372. $rotateImage = 180;
  373. break;
  374. case 6:
  375. $rotateImage = 90;
  376. $swapSize = true;
  377. break;
  378. case 8:
  379. $rotateImage = 270;
  380. $swapSize = true;
  381. break;
  382. default:
  383. return false;
  384. break;
  385. }
  386. if ($rotateImage!==0) {
  387. $image = new Imagick();
  388. $image->readImage($path);
  389. $image->rotateImage(new ImagickPixel(), $rotateImage);
  390. $image->setImageOrientation(1);
  391. $image->writeImage($path);
  392. $image->clear();
  393. $image->destroy();
  394. }
  395. } else {
  396. $newWidth = $info['width'];
  397. $newHeight = $info['height'];
  398. $sourceImg = imagecreatefromjpeg($path);
  399. switch ($info['orientation']) {
  400. case 2:
  401. # mirror
  402. # not yet implemented
  403. return false;
  404. break;
  405. case 3:
  406. $sourceImg = imagerotate($sourceImg, -180, 0);
  407. break;
  408. case 4:
  409. # rotate 180 and mirror
  410. # not yet implemented
  411. return false;
  412. break;
  413. case 5:
  414. # rotate 90 and mirror
  415. # not yet implemented
  416. return false;
  417. break;
  418. case 6:
  419. $sourceImg = imagerotate($sourceImg, -90, 0);
  420. $newWidth = $info['height'];
  421. $newHeight = $info['width'];
  422. $swapSize = true;
  423. break;
  424. case 7:
  425. # rotate -90 and mirror
  426. # not yet implemented
  427. return false;
  428. break;
  429. case 8:
  430. $sourceImg = imagerotate($sourceImg, 90, 0);
  431. $newWidth = $info['height'];
  432. $newHeight = $info['width'];
  433. $swapSize = true;
  434. break;
  435. default:
  436. return false;
  437. break;
  438. }
  439. # Recreate photo
  440. $newSourceImg = imagecreatetruecolor($newWidth, $newHeight);
  441. imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
  442. imagejpeg($newSourceImg, $path, 100);
  443. # Free memory
  444. imagedestroy($sourceImg);
  445. imagedestroy($newSourceImg);
  446. }
  447. # Call plugins
  448. $this->plugins(__METHOD__, 1, func_get_args());
  449. # SwapSize should be true when the image has been rotated
  450. # Return new dimensions in this case
  451. if ($swapSize===true) {
  452. $swapSize = $info['width'];
  453. $info['width'] = $info['height'];
  454. $info['height'] = $swapSize;
  455. }
  456. return $info;
  457. }
  458. public static function prepareData($data) {
  459. # Function turns photo-attributes into a front-end friendly format. Note that some attributes remain unchanged.
  460. # Excepts the following:
  461. # (array) $data = ['id', 'title', 'tags', 'public', 'star', 'album', 'thumbUrl', 'takestamp', 'url']
  462. # Returns the following:
  463. # (array) $photo
  464. # Check dependencies
  465. self::dependencies(isset($data));
  466. # Init
  467. $photo = null;
  468. # Set unchanged attributes
  469. $photo['id'] = $data['id'];
  470. $photo['title'] = $data['title'];
  471. $photo['tags'] = $data['tags'];
  472. $photo['public'] = $data['public'];
  473. $photo['star'] = $data['star'];
  474. $photo['album'] = $data['album'];
  475. # Parse urls
  476. $photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $data['thumbUrl'];
  477. $photo['url'] = LYCHEE_URL_UPLOADS_BIG . $data['url'];
  478. # Use takestamp as sysdate when possible
  479. if (isset($data['takestamp'])&&$data['takestamp']!=='0') {
  480. # Use takestamp
  481. $photo['cameraDate'] = '1';
  482. $photo['sysdate'] = date('d F Y', $data['takestamp']);
  483. } else {
  484. # Use sysstamp from the id
  485. $photo['cameraDate'] = '0';
  486. $photo['sysdate'] = date('d F Y', substr($data['id'], 0, -4));
  487. }
  488. return $photo;
  489. }
  490. public function get($albumID) {
  491. # Functions returns data of a photo
  492. # Excepts the following:
  493. # (string) $albumID = Album which is currently visible to the user
  494. # Returns the following:
  495. # (array) $photo
  496. # Check dependencies
  497. self::dependencies(isset($this->database, $this->photoIDs));
  498. # Call plugins
  499. $this->plugins(__METHOD__, 0, func_get_args());
  500. # Get photo
  501. $query = Database::prepare($this->database, "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  502. $photos = $this->database->query($query);
  503. $photo = $photos->fetch_assoc();
  504. # Parse photo
  505. $photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
  506. if (strlen($photo['takestamp'])>1) $photo['takedate'] = date('d M. Y', $photo['takestamp']);
  507. # Parse medium
  508. if ($photo['medium']==='1') $photo['medium'] = LYCHEE_URL_UPLOADS_MEDIUM . $photo['url'];
  509. else $photo['medium'] = '';
  510. # Parse paths
  511. $photo['url'] = LYCHEE_URL_UPLOADS_BIG . $photo['url'];
  512. $photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $photo['thumbUrl'];
  513. if ($albumID!='false') {
  514. # Only show photo as public when parent album is public
  515. # Check if parent album is not 'Unsorted'
  516. if ($photo['album']!=='0') {
  517. # Get album
  518. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $photo['album']));
  519. $albums = $this->database->query($query);
  520. $album = $albums->fetch_assoc();
  521. # Parse album
  522. $photo['public'] = ($album['public']==='1' ? '2' : $photo['public']);
  523. }
  524. $photo['original_album'] = $photo['album'];
  525. $photo['album'] = $albumID;
  526. }
  527. # Call plugins
  528. $this->plugins(__METHOD__, 1, func_get_args());
  529. return $photo;
  530. }
  531. public function getInfo($url) {
  532. # Functions returns information and metadata of a photo
  533. # Excepts the following:
  534. # (string) $url = Path to photo-file
  535. # Returns the following:
  536. # (array) $return
  537. # Check dependencies
  538. self::dependencies(isset($this->database, $url));
  539. # Call plugins
  540. $this->plugins(__METHOD__, 0, func_get_args());
  541. $iptcArray = array();
  542. $info = getimagesize($url, $iptcArray);
  543. # General information
  544. $return['type'] = $info['mime'];
  545. $return['width'] = $info[0];
  546. $return['height'] = $info[1];
  547. # Size
  548. $size = filesize($url)/1024;
  549. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  550. else $return['size'] = round($size, 1) . ' KB';
  551. # IPTC Metadata Fallback
  552. $return['title'] = '';
  553. $return['description'] = '';
  554. # IPTC Metadata
  555. if(isset($iptcArray['APP13'])) {
  556. $iptcInfo = iptcparse($iptcArray['APP13']);
  557. if (is_array($iptcInfo)) {
  558. $temp = @$iptcInfo['2#105'][0];
  559. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  560. $temp = @$iptcInfo['2#120'][0];
  561. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  562. $temp = @$iptcInfo['2#005'][0];
  563. if (isset($temp)&&strlen($temp)>0&&$return['title']==='') $return['title'] = $temp;
  564. }
  565. }
  566. # EXIF Metadata Fallback
  567. $return['orientation'] = '';
  568. $return['iso'] = '';
  569. $return['aperture'] = '';
  570. $return['make'] = '';
  571. $return['model'] = '';
  572. $return['shutter'] = '';
  573. $return['focal'] = '';
  574. $return['takestamp'] = 0;
  575. # Read EXIF
  576. if ($info['mime']=='image/jpeg') $exif = @exif_read_data($url, 'EXIF', 0);
  577. else $exif = false;
  578. # EXIF Metadata
  579. if ($exif!==false) {
  580. if (isset($exif['Orientation'])) $return['orientation'] = $exif['Orientation'];
  581. else if (isset($exif['IFD0']['Orientation'])) $return['orientation'] = $exif['IFD0']['Orientation'];
  582. $temp = @$exif['ISOSpeedRatings'];
  583. if (isset($temp)) $return['iso'] = $temp;
  584. $temp = @$exif['COMPUTED']['ApertureFNumber'];
  585. if (isset($temp)) $return['aperture'] = $temp;
  586. $temp = @$exif['Make'];
  587. if (isset($temp)) $return['make'] = trim($temp);
  588. $temp = @$exif['Model'];
  589. if (isset($temp)) $return['model'] = trim($temp);
  590. $temp = @$exif['ExposureTime'];
  591. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' s';
  592. $temp = @$exif['FocalLength'];
  593. if (isset($temp)) {
  594. if (strpos($temp, '/')!==FALSE) {
  595. $temp = explode('/', $temp, 2);
  596. $temp = $temp[0] / $temp[1];
  597. $temp = round($temp, 1);
  598. $return['focal'] = $temp . ' mm';
  599. }
  600. $return['focal'] = $temp . ' mm';
  601. }
  602. $temp = @$exif['DateTimeOriginal'];
  603. if (isset($temp)) $return['takestamp'] = strtotime($temp);
  604. }
  605. # Call plugins
  606. $this->plugins(__METHOD__, 1, func_get_args());
  607. return $return;
  608. }
  609. public function getArchive() {
  610. # Functions starts a download of a photo
  611. # Returns the following:
  612. # (boolean + output) true = Success
  613. # (boolean) false = Failure
  614. # Check dependencies
  615. self::dependencies(isset($this->database, $this->photoIDs));
  616. # Call plugins
  617. $this->plugins(__METHOD__, 0, func_get_args());
  618. # Get photo
  619. $query = Database::prepare($this->database, "SELECT title, url FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  620. $photos = $this->database->query($query);
  621. $photo = $photos->fetch_object();
  622. # Error in database query
  623. if (!$photos) {
  624. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  625. return false;
  626. }
  627. # Photo not found
  628. if ($photo===null) {
  629. Log::error($this->database, __METHOD__, __LINE__, 'Album not found. Cannot start download.');
  630. return false;
  631. }
  632. # Get extension
  633. $extension = getExtension($photo->url);
  634. if ($extension===false) {
  635. Log::error($this->database, __METHOD__, __LINE__, 'Invalid photo extension');
  636. return false;
  637. }
  638. # Illicit chars
  639. $badChars = array_merge(
  640. array_map('chr', range(0,31)),
  641. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  642. );
  643. # Parse title
  644. if ($photo->title=='') $photo->title = 'Untitled';
  645. # Escape title
  646. $photo->title = str_replace($badChars, '', $photo->title);
  647. # Set headers
  648. header("Content-Type: application/octet-stream");
  649. header("Content-Disposition: attachment; filename=\"" . $photo->title . $extension . "\"");
  650. header("Content-Length: " . filesize(LYCHEE_UPLOADS_BIG . $photo->url));
  651. # Send file
  652. readfile(LYCHEE_UPLOADS_BIG . $photo->url);
  653. # Call plugins
  654. $this->plugins(__METHOD__, 1, func_get_args());
  655. return true;
  656. }
  657. public function setTitle($title) {
  658. # Functions sets the title of a photo
  659. # Excepts the following:
  660. # (string) $title = Title with a maximum length of 50 chars
  661. # Returns the following:
  662. # (boolean) true = Success
  663. # (boolean) false = Failure
  664. # Check dependencies
  665. self::dependencies(isset($this->database, $this->photoIDs));
  666. # Call plugins
  667. $this->plugins(__METHOD__, 0, func_get_args());
  668. # Set title
  669. $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $title, $this->photoIDs));
  670. $result = $this->database->query($query);
  671. # Call plugins
  672. $this->plugins(__METHOD__, 1, func_get_args());
  673. if (!$result) {
  674. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  675. return false;
  676. }
  677. return true;
  678. }
  679. public function setDescription($description) {
  680. # Functions sets the description of a photo
  681. # Excepts the following:
  682. # (string) $description = Description with a maximum length of 1000 chars
  683. # Returns the following:
  684. # (boolean) true = Success
  685. # (boolean) false = Failure
  686. # Check dependencies
  687. self::dependencies(isset($this->database, $this->photoIDs));
  688. # Call plugins
  689. $this->plugins(__METHOD__, 0, func_get_args());
  690. # Set description
  691. $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN ('?')", array(LYCHEE_TABLE_PHOTOS, $description, $this->photoIDs));
  692. $result = $this->database->query($query);
  693. # Call plugins
  694. $this->plugins(__METHOD__, 1, func_get_args());
  695. if (!$result) {
  696. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  697. return false;
  698. }
  699. return true;
  700. }
  701. public function setStar() {
  702. # Functions stars a photo
  703. # Returns the following:
  704. # (boolean) true = Success
  705. # (boolean) false = Failure
  706. # Check dependencies
  707. self::dependencies(isset($this->database, $this->photoIDs));
  708. # Call plugins
  709. $this->plugins(__METHOD__, 0, func_get_args());
  710. # Init vars
  711. $error = false;
  712. # Get photos
  713. $query = Database::prepare($this->database, "SELECT id, star FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  714. $photos = $this->database->query($query);
  715. # For each photo
  716. while ($photo = $photos->fetch_object()) {
  717. # Invert star
  718. $star = ($photo->star==0 ? 1 : 0);
  719. # Set star
  720. $query = Database::prepare($this->database, "UPDATE ? SET star = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $star, $photo->id));
  721. $star = $this->database->query($query);
  722. if (!$star) $error = true;
  723. }
  724. # Call plugins
  725. $this->plugins(__METHOD__, 1, func_get_args());
  726. if ($error===true) {
  727. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  728. return false;
  729. }
  730. return true;
  731. }
  732. public function getPublic($password) {
  733. # Functions checks if photo or parent album is public
  734. # Returns the following:
  735. # (int) 0 = Photo private and parent album private
  736. # (int) 1 = Album public, but password incorrect
  737. # (int) 2 = Photo public or album public and password correct
  738. # Check dependencies
  739. self::dependencies(isset($this->database, $this->photoIDs));
  740. # Call plugins
  741. $this->plugins(__METHOD__, 0, func_get_args());
  742. # Get photo
  743. $query = Database::prepare($this->database, "SELECT public, album FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  744. $photos = $this->database->query($query);
  745. $photo = $photos->fetch_object();
  746. # Check if public
  747. if ($photo->public==='1') {
  748. # Photo public
  749. return 2;
  750. } else {
  751. # Check if album public
  752. $album = new Album($this->database, null, null, $photo->album);
  753. $agP = $album->getPublic();
  754. $acP = $album->checkPassword($password);
  755. # Album public and password correct
  756. if ($agP===true&&$acP===true) return 2;
  757. # Album public, but password incorrect
  758. if ($agP===true&&$acP===false) return 1;
  759. }
  760. # Call plugins
  761. $this->plugins(__METHOD__, 1, func_get_args());
  762. # Photo private
  763. return 0;
  764. }
  765. public function setPublic() {
  766. # Functions toggles the public property of a photo
  767. # Returns the following:
  768. # (boolean) true = Success
  769. # (boolean) false = Failure
  770. # Check dependencies
  771. self::dependencies(isset($this->database, $this->photoIDs));
  772. # Call plugins
  773. $this->plugins(__METHOD__, 0, func_get_args());
  774. # Get public
  775. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  776. $photos = $this->database->query($query);
  777. $photo = $photos->fetch_object();
  778. # Invert public
  779. $public = ($photo->public==0 ? 1 : 0);
  780. # Set public
  781. $query = Database::prepare($this->database, "UPDATE ? SET public = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $public, $this->photoIDs));
  782. $result = $this->database->query($query);
  783. # Call plugins
  784. $this->plugins(__METHOD__, 1, func_get_args());
  785. if (!$result) {
  786. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  787. return false;
  788. }
  789. return true;
  790. }
  791. function setAlbum($albumID) {
  792. # Functions sets the parent album of a photo
  793. # Returns the following:
  794. # (boolean) true = Success
  795. # (boolean) false = Failure
  796. # Check dependencies
  797. self::dependencies(isset($this->database, $this->photoIDs));
  798. # Call plugins
  799. $this->plugins(__METHOD__, 0, func_get_args());
  800. # Set album
  801. $query = Database::prepare($this->database, "UPDATE ? SET album = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->photoIDs));
  802. $result = $this->database->query($query);
  803. # Call plugins
  804. $this->plugins(__METHOD__, 1, func_get_args());
  805. if (!$result) {
  806. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  807. return false;
  808. }
  809. return true;
  810. }
  811. public function setTags($tags) {
  812. # Functions sets the tags of a photo
  813. # Excepts the following:
  814. # (string) $tags = Comma separated list of tags with a maximum length of 1000 chars
  815. # Returns the following:
  816. # (boolean) true = Success
  817. # (boolean) false = Failure
  818. # Check dependencies
  819. self::dependencies(isset($this->database, $this->photoIDs));
  820. # Call plugins
  821. $this->plugins(__METHOD__, 0, func_get_args());
  822. # Parse tags
  823. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  824. $tags = preg_replace('/,$|^,|(\ ){0,}$/', '', $tags);
  825. # Set tags
  826. $query = Database::prepare($this->database, "UPDATE ? SET tags = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $tags, $this->photoIDs));
  827. $result = $this->database->query($query);
  828. # Call plugins
  829. $this->plugins(__METHOD__, 1, func_get_args());
  830. if (!$result) {
  831. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  832. return false;
  833. }
  834. return true;
  835. }
  836. public function duplicate() {
  837. # Functions duplicates a photo
  838. # Returns the following:
  839. # (boolean) true = Success
  840. # (boolean) false = Failure
  841. # Check dependencies
  842. self::dependencies(isset($this->database, $this->photoIDs));
  843. # Call plugins
  844. $this->plugins(__METHOD__, 0, func_get_args());
  845. # Get photos
  846. $query = Database::prepare($this->database, "SELECT id, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  847. $photos = $this->database->query($query);
  848. if (!$photos) {
  849. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  850. return false;
  851. }
  852. # For each photo
  853. while ($photo = $photos->fetch_object()) {
  854. # Generate id
  855. $id = str_replace('.', '', microtime(true));
  856. while(strlen($id)<14) $id .= 0;
  857. # Duplicate entry
  858. $values = array(LYCHEE_TABLE_PHOTOS, $id, LYCHEE_TABLE_PHOTOS, $photo->id);
  859. $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);
  860. $duplicate = $this->database->query($query);
  861. if (!$duplicate) {
  862. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  863. return false;
  864. }
  865. }
  866. return true;
  867. }
  868. public function delete() {
  869. # Functions deletes a photo with all its data and files
  870. # Returns the following:
  871. # (boolean) true = Success
  872. # (boolean) false = Failure
  873. # Check dependencies
  874. self::dependencies(isset($this->database, $this->photoIDs));
  875. # Call plugins
  876. $this->plugins(__METHOD__, 0, func_get_args());
  877. # Get photos
  878. $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  879. $photos = $this->database->query($query);
  880. if (!$photos) {
  881. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  882. return false;
  883. }
  884. # For each photo
  885. while ($photo = $photos->fetch_object()) {
  886. # Check if other photos are referring to this images
  887. # If so, only delete the db entry
  888. if ($this->exists($photo->checksum, $photo->id)===false) {
  889. # Get retina thumb url
  890. $thumbUrl2x = explode(".", $photo->thumbUrl);
  891. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  892. # Delete big
  893. if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) {
  894. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
  895. return false;
  896. }
  897. # Delete medium
  898. if (file_exists(LYCHEE_UPLOADS_MEDIUM . $photo->url)&&!unlink(LYCHEE_UPLOADS_MEDIUM . $photo->url)) {
  899. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/medium/');
  900. return false;
  901. }
  902. # Delete thumb
  903. if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) {
  904. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
  905. return false;
  906. }
  907. # Delete thumb@2x
  908. if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
  909. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
  910. return false;
  911. }
  912. }
  913. # Delete db entry
  914. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $photo->id));
  915. $delete = $this->database->query($query);
  916. if (!$delete) {
  917. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  918. return false;
  919. }
  920. }
  921. # Call plugins
  922. $this->plugins(__METHOD__, 1, func_get_args());
  923. return true;
  924. }
  925. }
  926. ?>