Photo.php 23 KB

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