Photo.php 27 KB

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