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