Photo.php 26 KB

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