Import.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Lychee\Modules;
  3. final class Import {
  4. private function photo($path, $albumID = 0, $description = '', $tags = '') {
  5. // No need to validate photo type and extension in this function.
  6. // $photo->add will take care of it.
  7. $info = getimagesize($path);
  8. $size = filesize($path);
  9. $photo = new Photo(null);
  10. $nameFile = array(array());
  11. $nameFile[0]['name'] = $path;
  12. $nameFile[0]['type'] = $info['mime'];
  13. $nameFile[0]['tmp_name'] = $path;
  14. $nameFile[0]['error'] = 0;
  15. $nameFile[0]['size'] = $size;
  16. $nameFile[0]['error'] = UPLOAD_ERR_OK;
  17. if (!$photo->add($nameFile, $albumID, $description, $tags, true)) return false;
  18. return true;
  19. }
  20. public function url($urls, $albumID = 0) {
  21. // Call plugins
  22. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  23. $error = false;
  24. // Parse URLs
  25. $urls = str_replace(' ', '%20', $urls);
  26. $urls = explode(',', $urls);
  27. foreach ($urls as &$url) {
  28. // Validate photo type and extension even when $this->photo (=> $photo->add) will do the same.
  29. // This prevents us from downloading invalid photos.
  30. // Verify extension
  31. $extension = getExtension($url);
  32. if (!in_array(strtolower($extension), Photo::$validExtensions, true)) {
  33. $error = true;
  34. Log::error(__METHOD__, __LINE__, 'Photo format not supported (' . $url . ')');
  35. continue;
  36. }
  37. // Verify image
  38. $type = @exif_imagetype($url);
  39. if (!in_array($type, Photo::$validTypes, true)) {
  40. $error = true;
  41. Log::error(__METHOD__, __LINE__, 'Photo type not supported (' . $url . ')');
  42. continue;
  43. }
  44. $pathinfo = pathinfo($url);
  45. $filename = $pathinfo['filename'] . '.' . $pathinfo['extension'];
  46. $tmp_name = LYCHEE_DATA . $filename;
  47. if (@copy($url, $tmp_name)===false) {
  48. $error = true;
  49. Log::error(__METHOD__, __LINE__, 'Could not copy file (' . $tmp_name . ') to temp-folder (' . $tmp_name . ')');
  50. continue;
  51. }
  52. // Import photo
  53. if (!$this->photo($tmp_name, $albumID)) {
  54. $error = true;
  55. Log::error(__METHOD__, __LINE__, 'Could not import file: ' . $tmp_name);
  56. continue;
  57. }
  58. }
  59. // Call plugins
  60. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  61. if ($error===false) return true;
  62. return false;
  63. }
  64. public function server($path, $albumID = 0) {
  65. // Parse path
  66. if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
  67. if (substr($path, -1)==='/') $path = substr($path, 0, -1);
  68. if (is_dir($path)===false) {
  69. Log::error(__METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
  70. return 'Error: Given path is not a directory!';
  71. }
  72. // Skip folders of Lychee
  73. if ($path===LYCHEE_UPLOADS_BIG||($path . '/')===LYCHEE_UPLOADS_BIG||
  74. $path===LYCHEE_UPLOADS_MEDIUM||($path . '/')===LYCHEE_UPLOADS_MEDIUM||
  75. $path===LYCHEE_UPLOADS_THUMB||($path . '/')===LYCHEE_UPLOADS_THUMB) {
  76. Log::error(__METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
  77. return 'Error: Given path is a reserved path of Lychee!';
  78. }
  79. $error = false;
  80. $contains['photos'] = false;
  81. $contains['albums'] = false;
  82. // Call plugins
  83. // Note that updated albumId and path explicitly passed, rather
  84. // than using func_get_args() which will only return original ones
  85. Plugins::get()->activate(__METHOD__, 0, array($albumID, $path));
  86. // Get all files
  87. $files = glob($path . '/*');
  88. foreach ($files as $file) {
  89. // It is possible to move a file because of directory permissions but
  90. // the file may still be unreadable by the user
  91. if (!is_readable($file)) {
  92. $error = true;
  93. Log::error(__METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
  94. continue;
  95. }
  96. if (@exif_imagetype($file)!==false) {
  97. // Photo
  98. $contains['photos'] = true;
  99. if (!$this->photo($file, $albumID)) {
  100. $error = true;
  101. Log::error(__METHOD__, __LINE__, 'Could not import file: ' . $file);
  102. continue;
  103. }
  104. } else if (is_dir($file)) {
  105. // Folder
  106. $album = new Album(null);
  107. $newAlbumID = $album->add('[Import] ' . basename($file));
  108. $contains['albums'] = true;
  109. if ($newAlbumID===false) {
  110. $error = true;
  111. Log::error(__METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
  112. continue;
  113. }
  114. $import = $this->server($file . '/', $newAlbumID);
  115. if ($import!==true&&$import!=='Notice: Import only contains albums!') {
  116. $error = true;
  117. Log::error(__METHOD__, __LINE__, 'Could not import folder. Function returned warning.');
  118. continue;
  119. }
  120. }
  121. }
  122. // Call plugins
  123. // Note that updated albumId and path explicitly passed, rather
  124. // than using func_get_args() which will only return original ones
  125. Plugins::get()->activate(__METHOD__, 1, array($albumID, $path));
  126. // The following returns will be caught in the front-end
  127. if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!';
  128. if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contained albums!';
  129. if ($error===true) return false;
  130. return true;
  131. }
  132. }
  133. ?>