Photo.php 28 KB

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