Photo.php 25 KB

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