Photo.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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||hasPermissions(LYCHEE_UPLOADS_THUMB)===false) {
  36. Log::error($this->database, __METHOD__, __LINE__, 'Wrong permissions in uploads/');
  37. exit('Error: Wrong permissions in uploads-folder!');
  38. }
  39. # Call plugins
  40. $this->plugins(__METHOD__, 0, func_get_args());
  41. switch($albumID) {
  42. case 's':
  43. # s for public (share)
  44. $public = 1;
  45. $star = 0;
  46. $albumID = 0;
  47. break;
  48. case 'f':
  49. # f for starred (fav)
  50. $star = 1;
  51. $public = 0;
  52. $albumID = 0;
  53. break;
  54. case 'r':
  55. # r for recent
  56. $public = 0;
  57. $star = 0;
  58. $albumID = 0;
  59. break;
  60. default:
  61. $star = 0;
  62. $public = 0;
  63. break;
  64. }
  65. foreach ($files as $file) {
  66. # Verify extension
  67. $extension = getExtension($file['name']);
  68. if (!in_array(strtolower($extension), $this->validExtensions, true)) continue;
  69. # Verify image
  70. $type = @exif_imagetype($file['tmp_name']);
  71. if (!in_array($type, $this->allowedTypes, true)) continue;
  72. # Generate id
  73. $id = str_replace('.', '', microtime(true));
  74. while(strlen($id)<14) $id .= 0;
  75. $tmp_name = $file['tmp_name'];
  76. $photo_name = md5($id) . $extension;
  77. $path = LYCHEE_UPLOADS_BIG . $photo_name;
  78. # Import if not uploaded via web
  79. if (!is_uploaded_file($tmp_name)) {
  80. if (!@copy($tmp_name, $path)) {
  81. Log::error($this->database, __METHOD__, __LINE__, 'Could not copy photo to uploads');
  82. exit('Error: Could not copy photo to uploads!');
  83. } else @unlink($tmp_name);
  84. } else {
  85. if (!@move_uploaded_file($tmp_name, $path)) {
  86. Log::error($this->database, __METHOD__, __LINE__, 'Could not move photo to uploads');
  87. exit('Error: Could not move photo to uploads!');
  88. }
  89. }
  90. # Calculate checksum
  91. $checksum = sha1_file($path);
  92. if ($checksum===false) $checksum = '';
  93. # Read infos
  94. $info = $this->getInfo($path);
  95. # Use title of file if IPTC title missing
  96. if ($info['title']==='') $info['title'] = mysqli_real_escape_string($this->database, substr(basename($file['name'], $extension), 0, 30));
  97. # Use description parameter if set
  98. if ($description==='') $description = $info['description'];
  99. # Set orientation based on EXIF data
  100. if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!==''&&isset($info['width'])&&isset($info['height'])) {
  101. if (!$this->adjustFile($path, $info)) Log::notice($this->database, __METHOD__, __LINE__, 'Could not adjust photo (' . $info['title'] . ')');
  102. }
  103. # Set original date
  104. if ($info['takestamp']!=='') @touch($path, $info['takestamp']);
  105. # Create Thumb
  106. if (!$this->createThumb($path, $photo_name)) {
  107. Log::error($this->database, __METHOD__, __LINE__, 'Could not create thumbnail for photo');
  108. exit('Error: Could not create thumbnail for photo!');
  109. }
  110. # Save to DB
  111. $query = "INSERT INTO lychee_photos (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum)
  112. VALUES (
  113. '" . $id . "',
  114. '" . $info['title'] . "',
  115. '" . $photo_name . "',
  116. '" . $description . "',
  117. '" . $tags . "',
  118. '" . $info['type'] . "',
  119. '" . $info['width'] . "',
  120. '" . $info['height'] . "',
  121. '" . $info['size'] . "',
  122. '" . $info['iso'] . "',
  123. '" . $info['aperture'] . "',
  124. '" . $info['make'] . "',
  125. '" . $info['model'] . "',
  126. '" . $info['shutter'] . "',
  127. '" . $info['focal'] . "',
  128. '" . $info['takestamp'] . "',
  129. '" . md5($id) . ".jpeg',
  130. '" . $albumID . "',
  131. '" . $public . "',
  132. '" . $star . "',
  133. '" . $checksum . "');";
  134. $result = $this->database->query($query);
  135. if (!$result) {
  136. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  137. exit('Error: Could not save photo in database!');
  138. }
  139. }
  140. # Call plugins
  141. $this->plugins(__METHOD__, 1, func_get_args());
  142. return true;
  143. }
  144. private function createThumb($url, $filename, $width = 200, $height = 200) {
  145. # Check dependencies
  146. self::dependencies(isset($this->database, $this->settings, $url, $filename));
  147. # Call plugins
  148. $this->plugins(__METHOD__, 0, func_get_args());
  149. $info = getimagesize($url);
  150. $photoName = explode(".", $filename);
  151. $newUrl = LYCHEE_UPLOADS_THUMB . $photoName[0] . '.jpeg';
  152. $newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName[0] . '@2x.jpeg';
  153. # create thumbnails with Imagick
  154. if(extension_loaded('imagick')) {
  155. # Read image
  156. $thumb = new Imagick();
  157. $thumb->readImage($url);
  158. $thumb->setImageCompressionQuality($this->settings['thumbQuality']);
  159. $thumb->setImageFormat('jpeg');
  160. # Copy image for 2nd thumb version
  161. $thumb2x = clone $thumb;
  162. # Create 1st version
  163. $thumb->cropThumbnailImage($width, $height);
  164. $thumb->writeImage($newUrl);
  165. $thumb->clear();
  166. $thumb->destroy();
  167. # Create 2nd version
  168. $thumb2x->cropThumbnailImage($width*2, $height*2);
  169. $thumb2x->writeImage($newUrl2x);
  170. $thumb2x->clear();
  171. $thumb2x->destroy();
  172. } else {
  173. # Set position and size
  174. $thumb = imagecreatetruecolor($width, $height);
  175. $thumb2x = imagecreatetruecolor($width*2, $height*2);
  176. if ($info[0]<$info[1]) {
  177. $newSize = $info[0];
  178. $startWidth = 0;
  179. $startHeight = $info[1]/2 - $info[0]/2;
  180. } else {
  181. $newSize = $info[1];
  182. $startWidth = $info[0]/2 - $info[1]/2;
  183. $startHeight = 0;
  184. }
  185. # Create new image
  186. switch($info['mime']) {
  187. case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break;
  188. case 'image/png': $sourceImg = imagecreatefrompng($url); break;
  189. case 'image/gif': $sourceImg = imagecreatefromgif($url); break;
  190. default: Log::error($this->database, __METHOD__, __LINE__, 'Type of photo is not supported');
  191. return false;
  192. break;
  193. }
  194. # Create thumb
  195. fastimagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $width, $height, $newSize, $newSize);
  196. imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']);
  197. imagedestroy($thumb);
  198. # Create retina thumb
  199. fastimagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $width*2, $height*2, $newSize, $newSize);
  200. imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']);
  201. imagedestroy($thumb2x);
  202. # Free memory
  203. imagedestroy($sourceImg);
  204. }
  205. # Call plugins
  206. $this->plugins(__METHOD__, 1, func_get_args());
  207. return true;
  208. }
  209. private function adjustFile($path, $info) {
  210. # Check dependencies
  211. self::dependencies(isset($path, $info));
  212. # Call plugins
  213. $this->plugins(__METHOD__, 0, func_get_args());
  214. if (extension_loaded('imagick')) {
  215. $rotateImage = 0;
  216. switch ($info['orientation']) {
  217. case 3:
  218. $rotateImage = 180;
  219. $imageOrientation = 1;
  220. break;
  221. case 6:
  222. $rotateImage = 90;
  223. $imageOrientation = 1;
  224. break;
  225. case 8:
  226. $rotateImage = 270;
  227. $imageOrientation = 1;
  228. break;
  229. }
  230. if ($rotateImage!==0) {
  231. $image = new Imagick();
  232. $image->readImage($path);
  233. $image->rotateImage(new ImagickPixel(), $rotateImage);
  234. $image->setImageOrientation($imageOrientation);
  235. $image->writeImage($path);
  236. $image->clear();
  237. $image->destroy();
  238. }
  239. } else {
  240. $newWidth = $info['width'];
  241. $newHeight = $info['height'];
  242. $process = false;
  243. $sourceImg = imagecreatefromjpeg($path);
  244. switch ($info['orientation']) {
  245. case 2:
  246. # mirror
  247. # not yet implemented
  248. break;
  249. case 3:
  250. $process = true;
  251. $sourceImg = imagerotate($sourceImg, -180, 0);
  252. break;
  253. case 4:
  254. # rotate 180 and mirror
  255. # not yet implemented
  256. break;
  257. case 5:
  258. # rotate 90 and mirror
  259. # not yet implemented
  260. break;
  261. case 6:
  262. $process = true;
  263. $sourceImg = imagerotate($sourceImg, -90, 0);
  264. $newWidth = $info['height'];
  265. $newHeight = $info['width'];
  266. break;
  267. case 7:
  268. # rotate -90 and mirror
  269. # not yet implemented
  270. break;
  271. case 8:
  272. $process = true;
  273. $sourceImg = imagerotate($sourceImg, 90, 0);
  274. $newWidth = $info['height'];
  275. $newHeight = $info['width'];
  276. break;
  277. }
  278. # Need to adjust photo?
  279. if ($process===true) {
  280. # Recreate photo
  281. $newSourceImg = imagecreatetruecolor($newWidth, $newHeight);
  282. imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
  283. imagejpeg($newSourceImg, $path, 100);
  284. # Free memory
  285. imagedestroy($sourceImg);
  286. imagedestroy($newSourceImg);
  287. }
  288. }
  289. # Call plugins
  290. $this->plugins(__METHOD__, 1, func_get_args());
  291. return true;
  292. }
  293. public function get($albumID) {
  294. # Check dependencies
  295. self::dependencies(isset($this->database, $this->photoIDs));
  296. # Call plugins
  297. $this->plugins(__METHOD__, 0, func_get_args());
  298. # Get photo
  299. $photos = $this->database->query("SELECT * FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  300. $photo = $photos->fetch_assoc();
  301. # Parse photo
  302. $photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
  303. if (strlen($photo['takestamp'])>1) $photo['takedate'] = date('d M. Y', $photo['takestamp']);
  304. # Parse url
  305. $photo['url'] = LYCHEE_URL_UPLOADS_BIG . $photo['url'];
  306. if ($albumID!='false') {
  307. if ($photo['album']!=0) {
  308. # Get album
  309. $albums = $this->database->query("SELECT public FROM lychee_albums WHERE id = '" . $photo['album'] . " LIMIT 1';");
  310. $album = $albums->fetch_assoc();
  311. # Parse album
  312. $photo['public'] = ($album['public']=='1' ? '2' : $photo['public']);
  313. }
  314. $photo['original_album'] = $photo['album'];
  315. $photo['album'] = $albumID;
  316. }
  317. # Call plugins
  318. $this->plugins(__METHOD__, 1, func_get_args());
  319. return $photo;
  320. }
  321. private function getInfo($url) {
  322. # Check dependencies
  323. self::dependencies(isset($this->database, $url));
  324. # Call plugins
  325. $this->plugins(__METHOD__, 0, func_get_args());
  326. $iptcArray = array();
  327. $info = getimagesize($url, $iptcArray);
  328. # General information
  329. $return['type'] = $info['mime'];
  330. $return['width'] = $info[0];
  331. $return['height'] = $info[1];
  332. # Size
  333. $size = filesize($url)/1024;
  334. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  335. else $return['size'] = round($size, 1) . ' KB';
  336. # IPTC Metadata Fallback
  337. $return['title'] = '';
  338. $return['description'] = '';
  339. # IPTC Metadata
  340. if(isset($iptcArray['APP13'])) {
  341. $iptcInfo = iptcparse($iptcArray['APP13']);
  342. if (is_array($iptcInfo)) {
  343. $temp = @$iptcInfo['2#105'][0];
  344. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  345. $temp = @$iptcInfo['2#120'][0];
  346. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  347. }
  348. }
  349. # EXIF Metadata Fallback
  350. $return['orientation'] = '';
  351. $return['iso'] = '';
  352. $return['aperture'] = '';
  353. $return['make'] = '';
  354. $return['model'] = '';
  355. $return['shutter'] = '';
  356. $return['focal'] = '';
  357. $return['takestamp'] = 0;
  358. # Read EXIF
  359. if ($info['mime']=='image/jpeg') $exif = @exif_read_data($url, 'EXIF', 0);
  360. else $exif = false;
  361. # EXIF Metadata
  362. if ($exif!==false) {
  363. if (isset($exif['Orientation'])) $return['orientation'] = $exif['Orientation'];
  364. else if (isset($exif['IFD0']['Orientation'])) $return['orientation'] = $exif['IFD0']['Orientation'];
  365. $temp = @$exif['ISOSpeedRatings'];
  366. if (isset($temp)) $return['iso'] = $temp;
  367. $temp = @$exif['COMPUTED']['ApertureFNumber'];
  368. if (isset($temp)) $return['aperture'] = $temp;
  369. $temp = @$exif['Make'];
  370. if (isset($temp)) $return['make'] = trim($temp);
  371. $temp = @$exif['Model'];
  372. if (isset($temp)) $return['model'] = trim($temp);
  373. $temp = @$exif['ExposureTime'];
  374. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' Sec.';
  375. $temp = @$exif['FocalLength'];
  376. if (isset($temp)) $return['focal'] = ($temp/1) . ' mm';
  377. $temp = @$exif['DateTimeOriginal'];
  378. if (isset($temp)) $return['takestamp'] = strtotime($temp);
  379. }
  380. # Security
  381. foreach(array_keys($return) as $key) $return[$key] = mysqli_real_escape_string($this->database, $return[$key]);
  382. # Call plugins
  383. $this->plugins(__METHOD__, 1, func_get_args());
  384. return $return;
  385. }
  386. public function getArchive() {
  387. # Check dependencies
  388. self::dependencies(isset($this->database, $this->photoIDs));
  389. # Call plugins
  390. $this->plugins(__METHOD__, 0, func_get_args());
  391. # Get photo
  392. $photos = $this->database->query("SELECT title, url FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  393. $photo = $photos->fetch_object();
  394. # Get extension
  395. $extension = getExtension($photo->url);
  396. if ($extension===false) {
  397. Log::error($this->database, __METHOD__, __LINE__, 'Invalid photo extension');
  398. return false;
  399. }
  400. # Parse title
  401. if ($photo->title=='') $photo->title = 'Untitled';
  402. # Set headers
  403. header("Content-Type: application/octet-stream");
  404. header("Content-Disposition: attachment; filename=\"" . $photo->title . $extension . "\"");
  405. header("Content-Length: " . filesize(LYCHEE_UPLOADS_BIG . $photo->url));
  406. # Send file
  407. readfile(LYCHEE_UPLOADS_BIG . $photo->url);
  408. # Call plugins
  409. $this->plugins(__METHOD__, 1, func_get_args());
  410. return true;
  411. }
  412. public function setTitle($title) {
  413. # Check dependencies
  414. self::dependencies(isset($this->database, $this->photoIDs));
  415. # Call plugins
  416. $this->plugins(__METHOD__, 0, func_get_args());
  417. # Parse
  418. if (strlen($title)>50) $title = substr($title, 0, 50);
  419. # Set title
  420. $result = $this->database->query("UPDATE lychee_photos SET title = '$title' WHERE id IN ($this->photoIDs);");
  421. # Call plugins
  422. $this->plugins(__METHOD__, 1, func_get_args());
  423. if (!$result) {
  424. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  425. return false;
  426. }
  427. return true;
  428. }
  429. public function setDescription($description) {
  430. # Check dependencies
  431. self::dependencies(isset($this->database, $this->photoIDs));
  432. # Call plugins
  433. $this->plugins(__METHOD__, 0, func_get_args());
  434. # Parse
  435. $description = htmlentities($description);
  436. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  437. # Set description
  438. $result = $this->database->query("UPDATE lychee_photos SET description = '$description' WHERE id IN ('$this->photoIDs');");
  439. # Call plugins
  440. $this->plugins(__METHOD__, 1, func_get_args());
  441. if (!$result) {
  442. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  443. return false;
  444. }
  445. return true;
  446. }
  447. public function setStar() {
  448. # Check dependencies
  449. self::dependencies(isset($this->database, $this->photoIDs));
  450. # Call plugins
  451. $this->plugins(__METHOD__, 0, func_get_args());
  452. # Init vars
  453. $error = false;
  454. # Get photos
  455. $photos = $this->database->query("SELECT id, star FROM lychee_photos WHERE id IN ($this->photoIDs);");
  456. # For each photo
  457. while ($photo = $photos->fetch_object()) {
  458. # Invert star
  459. $star = ($photo->star==0 ? 1 : 0);
  460. # Set star
  461. $star = $this->database->query("UPDATE lychee_photos SET star = '$star' WHERE id = '$photo->id';");
  462. if (!$star) $error = true;
  463. }
  464. # Call plugins
  465. $this->plugins(__METHOD__, 1, func_get_args());
  466. if ($error===true) {
  467. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  468. return false;
  469. }
  470. return true;
  471. }
  472. public function getPublic($password) {
  473. # Check dependencies
  474. self::dependencies(isset($this->database, $this->photoIDs));
  475. # Call plugins
  476. $this->plugins(__METHOD__, 0, func_get_args());
  477. # Get photo
  478. $photos = $this->database->query("SELECT public, album FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  479. $photo = $photos->fetch_object();
  480. # Check if public
  481. if ($photo->public==1) return true;
  482. else {
  483. $album = new Album($this->database, null, null, $photo->album);
  484. $acP = $album->checkPassword($password);
  485. $agP = $album->getPublic();
  486. if ($acP===true&&$agP===true) return true;
  487. }
  488. # Call plugins
  489. $this->plugins(__METHOD__, 1, func_get_args());
  490. return false;
  491. }
  492. public function setPublic() {
  493. # Check dependencies
  494. self::dependencies(isset($this->database, $this->photoIDs));
  495. # Call plugins
  496. $this->plugins(__METHOD__, 0, func_get_args());
  497. # Get public
  498. $photos = $this->database->query("SELECT public FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  499. $photo = $photos->fetch_object();
  500. # Invert public
  501. $public = ($photo->public==0 ? 1 : 0);
  502. # Set public
  503. $result = $this->database->query("UPDATE lychee_photos SET public = '$public' WHERE id = '$this->photoIDs';");
  504. # Call plugins
  505. $this->plugins(__METHOD__, 1, func_get_args());
  506. if (!$result) {
  507. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  508. return false;
  509. }
  510. return true;
  511. }
  512. function setAlbum($albumID) {
  513. # Check dependencies
  514. self::dependencies(isset($this->database, $this->photoIDs));
  515. # Call plugins
  516. $this->plugins(__METHOD__, 0, func_get_args());
  517. # Set album
  518. $result = $this->database->query("UPDATE lychee_photos SET album = '$albumID' WHERE id IN ($this->photoIDs);");
  519. # Call plugins
  520. $this->plugins(__METHOD__, 1, func_get_args());
  521. if (!$result) {
  522. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  523. return false;
  524. }
  525. return true;
  526. }
  527. public function setTags($tags) {
  528. # Check dependencies
  529. self::dependencies(isset($this->database, $this->photoIDs));
  530. # Call plugins
  531. $this->plugins(__METHOD__, 0, func_get_args());
  532. # Parse tags
  533. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  534. $tags = preg_replace('/,$|^,|(\ ){0,}$/', '', $tags);
  535. if (strlen($tags)>1000) {
  536. Log::notice($this->database, __METHOD__, __LINE__, 'Length of tags higher than 1000');
  537. return false;
  538. }
  539. # Set tags
  540. $result = $this->database->query("UPDATE lychee_photos SET tags = '$tags' WHERE id IN ($this->photoIDs);");
  541. # Call plugins
  542. $this->plugins(__METHOD__, 1, func_get_args());
  543. if (!$result) {
  544. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  545. return false;
  546. }
  547. return true;
  548. }
  549. public function delete() {
  550. # Check dependencies
  551. self::dependencies(isset($this->database, $this->photoIDs));
  552. # Call plugins
  553. $this->plugins(__METHOD__, 0, func_get_args());
  554. # Get photos
  555. $photos = $this->database->query("SELECT id, url, thumbUrl FROM lychee_photos WHERE id IN ($this->photoIDs);");
  556. if (!$photos) {
  557. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  558. return false;
  559. }
  560. # For each photo
  561. while ($photo = $photos->fetch_object()) {
  562. # Get retina thumb url
  563. $thumbUrl2x = explode(".", $photo->thumbUrl);
  564. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  565. # Delete big
  566. if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) {
  567. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
  568. return false;
  569. }
  570. # Delete thumb
  571. if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) {
  572. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
  573. return false;
  574. }
  575. # Delete thumb@2x
  576. if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
  577. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
  578. return false;
  579. }
  580. # Delete db entry
  581. $delete = $this->database->query("DELETE FROM lychee_photos WHERE id = '$photo->id';");
  582. if (!$delete) {
  583. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  584. return false;
  585. }
  586. }
  587. # Call plugins
  588. $this->plugins(__METHOD__, 1, func_get_args());
  589. return true;
  590. }
  591. }
  592. ?>