Photo.php 20 KB

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