Photo.php 22 KB

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