Photo.php 20 KB

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