Photo.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. 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' ? 1 : 0);
  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 = 1;
  136. else $medium = 0;
  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 static function prepareData($data) {
  394. # This function requires the following photo-attributes and turns them
  395. # into a front-end friendly format: id, title, tags, public, star, album, thumbUrl, takestamp, url
  396. # Note that some attributes remain unchanged
  397. # Check dependencies
  398. self::dependencies(isset($data));
  399. # Init
  400. $photo = null;
  401. # Set unchanged attributes
  402. $photo['id'] = $data['id'];
  403. $photo['title'] = $data['title'];
  404. $photo['tags'] = $data['tags'];
  405. $photo['public'] = $data['public'];
  406. $photo['star'] = $data['star'];
  407. $photo['album'] = $data['album'];
  408. # Parse urls
  409. $photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $data['thumbUrl'];
  410. $photo['url'] = LYCHEE_URL_UPLOADS_BIG . $data['url'];
  411. # Use takestamp as sysdate when possible
  412. if (isset($data['takestamp'])&&$data['takestamp']!=='0') {
  413. # Use takestamp
  414. $photo['cameraDate'] = '1';
  415. $photo['sysdate'] = date('d F Y', $data['takestamp']);
  416. } else {
  417. # Use sysstamp from the id
  418. $photo['cameraDate'] = '0';
  419. $photo['sysdate'] = date('d F Y', substr($data['id'], 0, -4));
  420. }
  421. return $photo;
  422. }
  423. public function get($albumID) {
  424. # Functions returns data of a photo
  425. # Excepts the following:
  426. # (string) $albumID = Album which is currently visible to the user
  427. # Returns the following:
  428. # (array) $photo
  429. # Check dependencies
  430. self::dependencies(isset($this->database, $this->photoIDs));
  431. # Call plugins
  432. $this->plugins(__METHOD__, 0, func_get_args());
  433. # Get photo
  434. $query = Database::prepare($this->database, "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  435. $photos = $this->database->query($query);
  436. $photo = $photos->fetch_assoc();
  437. # Parse photo
  438. $photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
  439. if (strlen($photo['takestamp'])>1) $photo['takedate'] = date('d M. Y', $photo['takestamp']);
  440. # Parse medium
  441. if ($photo['medium']==='1') $photo['medium'] = LYCHEE_URL_UPLOADS_MEDIUM . $photo['url'];
  442. else $photo['medium'] = '';
  443. # Parse paths
  444. $photo['url'] = LYCHEE_URL_UPLOADS_BIG . $photo['url'];
  445. $photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $photo['thumbUrl'];
  446. if ($albumID!='false') {
  447. # Only show photo as public when parent album is public
  448. # Check if parent album is not 'Unsorted'
  449. if ($photo['album']!=='0') {
  450. # Get album
  451. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $photo['album']));
  452. $albums = $this->database->query($query);
  453. $album = $albums->fetch_assoc();
  454. # Parse album
  455. $photo['public'] = ($album['public']==='1' ? '2' : $photo['public']);
  456. }
  457. $photo['original_album'] = $photo['album'];
  458. $photo['album'] = $albumID;
  459. }
  460. # Call plugins
  461. $this->plugins(__METHOD__, 1, func_get_args());
  462. return $photo;
  463. }
  464. public function getInfo($url) {
  465. # Functions returns information and metadata of a photo
  466. # Excepts the following:
  467. # (string) $url = Path to photo-file
  468. # Returns the following:
  469. # (array) $return
  470. # Check dependencies
  471. self::dependencies(isset($this->database, $url));
  472. # Call plugins
  473. $this->plugins(__METHOD__, 0, func_get_args());
  474. $iptcArray = array();
  475. $info = getimagesize($url, $iptcArray);
  476. # General information
  477. $return['type'] = $info['mime'];
  478. $return['width'] = $info[0];
  479. $return['height'] = $info[1];
  480. # Size
  481. $size = filesize($url)/1024;
  482. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  483. else $return['size'] = round($size, 1) . ' KB';
  484. # IPTC Metadata Fallback
  485. $return['title'] = '';
  486. $return['description'] = '';
  487. # IPTC Metadata
  488. if(isset($iptcArray['APP13'])) {
  489. $iptcInfo = iptcparse($iptcArray['APP13']);
  490. if (is_array($iptcInfo)) {
  491. $temp = @$iptcInfo['2#105'][0];
  492. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  493. $temp = @$iptcInfo['2#120'][0];
  494. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  495. $temp = @$iptcInfo['2#005'][0];
  496. if (isset($temp)&&strlen($temp)>0&&$return['title']==='') $return['title'] = $temp;
  497. }
  498. }
  499. # EXIF Metadata Fallback
  500. $return['orientation'] = '';
  501. $return['iso'] = '';
  502. $return['aperture'] = '';
  503. $return['make'] = '';
  504. $return['model'] = '';
  505. $return['shutter'] = '';
  506. $return['focal'] = '';
  507. $return['takestamp'] = 0;
  508. # Read EXIF
  509. if ($info['mime']=='image/jpeg') $exif = @exif_read_data($url, 'EXIF', 0);
  510. else $exif = false;
  511. # EXIF Metadata
  512. if ($exif!==false) {
  513. if (isset($exif['Orientation'])) $return['orientation'] = $exif['Orientation'];
  514. else if (isset($exif['IFD0']['Orientation'])) $return['orientation'] = $exif['IFD0']['Orientation'];
  515. $temp = @$exif['ISOSpeedRatings'];
  516. if (isset($temp)) $return['iso'] = $temp;
  517. $temp = @$exif['COMPUTED']['ApertureFNumber'];
  518. if (isset($temp)) $return['aperture'] = $temp;
  519. $temp = @$exif['Make'];
  520. if (isset($temp)) $return['make'] = trim($temp);
  521. $temp = @$exif['Model'];
  522. if (isset($temp)) $return['model'] = trim($temp);
  523. $temp = @$exif['ExposureTime'];
  524. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' s';
  525. $temp = @$exif['FocalLength'];
  526. if (isset($temp)) {
  527. if (strpos($temp, '/')!==FALSE) {
  528. $temp = explode('/', $temp, 2);
  529. $temp = $temp[0] / $temp[1];
  530. $temp = round($temp, 1);
  531. $return['focal'] = $temp . ' mm';
  532. }
  533. $return['focal'] = $temp . ' mm';
  534. }
  535. $temp = @$exif['DateTimeOriginal'];
  536. if (isset($temp)) $return['takestamp'] = strtotime($temp);
  537. }
  538. # Call plugins
  539. $this->plugins(__METHOD__, 1, func_get_args());
  540. return $return;
  541. }
  542. public function getArchive() {
  543. # Functions starts a download of a photo
  544. # Returns the following:
  545. # (boolean + output) true = Success
  546. # (boolean) false = Failure
  547. # Check dependencies
  548. self::dependencies(isset($this->database, $this->photoIDs));
  549. # Call plugins
  550. $this->plugins(__METHOD__, 0, func_get_args());
  551. # Get photo
  552. $query = Database::prepare($this->database, "SELECT title, url FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  553. $photos = $this->database->query($query);
  554. $photo = $photos->fetch_object();
  555. # Error in database query
  556. if (!$photos) {
  557. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  558. return false;
  559. }
  560. # Photo not found
  561. if ($photo===null) {
  562. Log::error($this->database, __METHOD__, __LINE__, 'Album not found. Cannot start download.');
  563. return false;
  564. }
  565. # Get extension
  566. $extension = getExtension($photo->url);
  567. if ($extension===false) {
  568. Log::error($this->database, __METHOD__, __LINE__, 'Invalid photo extension');
  569. return false;
  570. }
  571. # Illicit chars
  572. $badChars = array_merge(
  573. array_map('chr', range(0,31)),
  574. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  575. );
  576. # Parse title
  577. if ($photo->title=='') $photo->title = 'Untitled';
  578. # Escape title
  579. $photo->title = str_replace($badChars, '', $photo->title);
  580. # Set headers
  581. header("Content-Type: application/octet-stream");
  582. header("Content-Disposition: attachment; filename=\"" . $photo->title . $extension . "\"");
  583. header("Content-Length: " . filesize(LYCHEE_UPLOADS_BIG . $photo->url));
  584. # Send file
  585. readfile(LYCHEE_UPLOADS_BIG . $photo->url);
  586. # Call plugins
  587. $this->plugins(__METHOD__, 1, func_get_args());
  588. return true;
  589. }
  590. public function setTitle($title) {
  591. # Functions sets the title of a photo
  592. # Excepts the following:
  593. # (string) $title = Title with a maximum length of 50 chars
  594. # Returns the following:
  595. # (boolean) true = Success
  596. # (boolean) false = Failure
  597. # Check dependencies
  598. self::dependencies(isset($this->database, $this->photoIDs));
  599. # Call plugins
  600. $this->plugins(__METHOD__, 0, func_get_args());
  601. # Parse
  602. if (strlen($title)>50) $title = substr($title, 0, 50);
  603. # Set title
  604. $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $title, $this->photoIDs));
  605. $result = $this->database->query($query);
  606. # Call plugins
  607. $this->plugins(__METHOD__, 1, func_get_args());
  608. if (!$result) {
  609. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  610. return false;
  611. }
  612. return true;
  613. }
  614. public function setDescription($description) {
  615. # Functions sets the description of a photo
  616. # Excepts the following:
  617. # (string) $description = Description with a maximum length of 1000 chars
  618. # Returns the following:
  619. # (boolean) true = Success
  620. # (boolean) false = Failure
  621. # Check dependencies
  622. self::dependencies(isset($this->database, $this->photoIDs));
  623. # Call plugins
  624. $this->plugins(__METHOD__, 0, func_get_args());
  625. # Parse
  626. $description = htmlentities($description, ENT_COMPAT | ENT_HTML401, 'UTF-8');
  627. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  628. # Set description
  629. $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN ('?')", array(LYCHEE_TABLE_PHOTOS, $description, $this->photoIDs));
  630. $result = $this->database->query($query);
  631. # Call plugins
  632. $this->plugins(__METHOD__, 1, func_get_args());
  633. if (!$result) {
  634. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  635. return false;
  636. }
  637. return true;
  638. }
  639. public function setStar() {
  640. # Functions stars a photo
  641. # Returns the following:
  642. # (boolean) true = Success
  643. # (boolean) false = Failure
  644. # Check dependencies
  645. self::dependencies(isset($this->database, $this->photoIDs));
  646. # Call plugins
  647. $this->plugins(__METHOD__, 0, func_get_args());
  648. # Init vars
  649. $error = false;
  650. # Get photos
  651. $query = Database::prepare($this->database, "SELECT id, star FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  652. $photos = $this->database->query($query);
  653. # For each photo
  654. while ($photo = $photos->fetch_object()) {
  655. # Invert star
  656. $star = ($photo->star==0 ? 1 : 0);
  657. # Set star
  658. $query = Database::prepare($this->database, "UPDATE ? SET star = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $star, $photo->id));
  659. $star = $this->database->query($query);
  660. if (!$star) $error = true;
  661. }
  662. # Call plugins
  663. $this->plugins(__METHOD__, 1, func_get_args());
  664. if ($error===true) {
  665. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  666. return false;
  667. }
  668. return true;
  669. }
  670. public function getPublic($password) {
  671. # Functions checks if photo or parent album is public
  672. # Returns the following:
  673. # (int) 0 = Photo private and parent album private
  674. # (int) 1 = Album public, but password incorrect
  675. # (int) 2 = Photo public or album public and password correct
  676. # Check dependencies
  677. self::dependencies(isset($this->database, $this->photoIDs));
  678. # Call plugins
  679. $this->plugins(__METHOD__, 0, func_get_args());
  680. # Get photo
  681. $query = Database::prepare($this->database, "SELECT public, album FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  682. $photos = $this->database->query($query);
  683. $photo = $photos->fetch_object();
  684. # Check if public
  685. if ($photo->public==='1') {
  686. # Photo public
  687. return 2;
  688. } else {
  689. # Check if album public
  690. $album = new Album($this->database, null, null, $photo->album);
  691. $agP = $album->getPublic();
  692. $acP = $album->checkPassword($password);
  693. # Album public and password correct
  694. if ($agP===true&&$acP===true) return 2;
  695. # Album public, but password incorrect
  696. if ($agP===true&&$acP===false) return 1;
  697. }
  698. # Call plugins
  699. $this->plugins(__METHOD__, 1, func_get_args());
  700. # Photo private
  701. return 0;
  702. }
  703. public function setPublic() {
  704. # Functions toggles the public property of a photo
  705. # Returns the following:
  706. # (boolean) true = Success
  707. # (boolean) false = Failure
  708. # Check dependencies
  709. self::dependencies(isset($this->database, $this->photoIDs));
  710. # Call plugins
  711. $this->plugins(__METHOD__, 0, func_get_args());
  712. # Get public
  713. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  714. $photos = $this->database->query($query);
  715. $photo = $photos->fetch_object();
  716. # Invert public
  717. $public = ($photo->public==0 ? 1 : 0);
  718. # Set public
  719. $query = Database::prepare($this->database, "UPDATE ? SET public = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $public, $this->photoIDs));
  720. $result = $this->database->query($query);
  721. # Call plugins
  722. $this->plugins(__METHOD__, 1, func_get_args());
  723. if (!$result) {
  724. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  725. return false;
  726. }
  727. return true;
  728. }
  729. function setAlbum($albumID) {
  730. # Functions sets the parent album of a photo
  731. # Returns the following:
  732. # (boolean) true = Success
  733. # (boolean) false = Failure
  734. # Check dependencies
  735. self::dependencies(isset($this->database, $this->photoIDs));
  736. # Call plugins
  737. $this->plugins(__METHOD__, 0, func_get_args());
  738. # Set album
  739. $query = Database::prepare($this->database, "UPDATE ? SET album = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->photoIDs));
  740. $result = $this->database->query($query);
  741. # Call plugins
  742. $this->plugins(__METHOD__, 1, func_get_args());
  743. if (!$result) {
  744. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  745. return false;
  746. }
  747. return true;
  748. }
  749. public function setTags($tags) {
  750. # Functions sets the tags of a photo
  751. # Excepts the following:
  752. # (string) $tags = Comma separated list of tags with a maximum length of 1000 chars
  753. # Returns the following:
  754. # (boolean) true = Success
  755. # (boolean) false = Failure
  756. # Check dependencies
  757. self::dependencies(isset($this->database, $this->photoIDs));
  758. # Call plugins
  759. $this->plugins(__METHOD__, 0, func_get_args());
  760. # Parse tags
  761. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  762. $tags = preg_replace('/,$|^,|(\ ){0,}$/', '', $tags);
  763. if (strlen($tags)>1000) {
  764. Log::notice($this->database, __METHOD__, __LINE__, 'Length of tags higher than 1000');
  765. return false;
  766. }
  767. # Set tags
  768. $query = Database::prepare($this->database, "UPDATE ? SET tags = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $tags, $this->photoIDs));
  769. $result = $this->database->query($query);
  770. # Call plugins
  771. $this->plugins(__METHOD__, 1, func_get_args());
  772. if (!$result) {
  773. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  774. return false;
  775. }
  776. return true;
  777. }
  778. public function duplicate() {
  779. # Functions duplicates a photo
  780. # Returns the following:
  781. # (boolean) true = Success
  782. # (boolean) false = Failure
  783. # Check dependencies
  784. self::dependencies(isset($this->database, $this->photoIDs));
  785. # Call plugins
  786. $this->plugins(__METHOD__, 0, func_get_args());
  787. # Get photos
  788. $query = Database::prepare($this->database, "SELECT id, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  789. $photos = $this->database->query($query);
  790. if (!$photos) {
  791. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  792. return false;
  793. }
  794. # For each photo
  795. while ($photo = $photos->fetch_object()) {
  796. # Generate id
  797. $id = str_replace('.', '', microtime(true));
  798. while(strlen($id)<14) $id .= 0;
  799. # Duplicate entry
  800. $values = array(LYCHEE_TABLE_PHOTOS, $id, LYCHEE_TABLE_PHOTOS, $photo->id);
  801. $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);
  802. $duplicate = $this->database->query($query);
  803. if (!$duplicate) {
  804. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  805. return false;
  806. }
  807. }
  808. return true;
  809. }
  810. public function delete() {
  811. # Functions deletes a photo with all its data and files
  812. # Returns the following:
  813. # (boolean) true = Success
  814. # (boolean) false = Failure
  815. # Check dependencies
  816. self::dependencies(isset($this->database, $this->photoIDs));
  817. # Call plugins
  818. $this->plugins(__METHOD__, 0, func_get_args());
  819. # Get photos
  820. $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  821. $photos = $this->database->query($query);
  822. if (!$photos) {
  823. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  824. return false;
  825. }
  826. # For each photo
  827. while ($photo = $photos->fetch_object()) {
  828. # Check if other photos are referring to this images
  829. # If so, only delete the db entry
  830. if ($this->exists($photo->checksum, $photo->id)===false) {
  831. # Get retina thumb url
  832. $thumbUrl2x = explode(".", $photo->thumbUrl);
  833. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  834. # Delete big
  835. if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) {
  836. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
  837. return false;
  838. }
  839. # Delete medium
  840. if (file_exists(LYCHEE_UPLOADS_MEDIUM . $photo->url)&&!unlink(LYCHEE_UPLOADS_MEDIUM . $photo->url)) {
  841. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/medium/');
  842. return false;
  843. }
  844. # Delete thumb
  845. if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) {
  846. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
  847. return false;
  848. }
  849. # Delete thumb@2x
  850. if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
  851. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
  852. return false;
  853. }
  854. }
  855. # Delete db entry
  856. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $photo->id));
  857. $delete = $this->database->query($query);
  858. if (!$delete) {
  859. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  860. return false;
  861. }
  862. }
  863. # Call plugins
  864. $this->plugins(__METHOD__, 1, func_get_args());
  865. return true;
  866. }
  867. }
  868. ?>