Photo.php 27 KB

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