Import.php 5.6 KB

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