Import.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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, true);
  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. $filename = pathinfo($url, PATHINFO_FILENAME) . $extension;
  52. $tmp_name = LYCHEE_DATA . $filename;
  53. if (@copy($url, $tmp_name)===false) {
  54. $error = true;
  55. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not copy file (' . $url . ') to temp-folder (' . $tmp_name . ')');
  56. continue;
  57. }
  58. // Import photo
  59. if (!$this->photo($tmp_name, $albumID)) {
  60. $error = true;
  61. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not import file (' . $tmp_name . ')');
  62. continue;
  63. }
  64. }
  65. // Call plugins
  66. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  67. if ($error===false) return true;
  68. return false;
  69. }
  70. /**
  71. * @return boolean|string Returns true when successful.
  72. * Warning: Folder empty or no readable files to process!
  73. * Notice: Import only contained albums!
  74. */
  75. public function server($path, $albumID = 0) {
  76. // Parse path
  77. if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
  78. if (substr($path, -1)==='/') $path = substr($path, 0, -1);
  79. if (is_dir($path)===false) {
  80. Log::error(Database::get(), __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
  81. return false;
  82. }
  83. // Skip folders of Lychee
  84. if ($path===LYCHEE_UPLOADS_BIG||($path . '/')===LYCHEE_UPLOADS_BIG||
  85. $path===LYCHEE_UPLOADS_MEDIUM||($path . '/')===LYCHEE_UPLOADS_MEDIUM||
  86. $path===LYCHEE_UPLOADS_THUMB||($path . '/')===LYCHEE_UPLOADS_THUMB) {
  87. Log::error(Database::get(), __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
  88. return false;
  89. }
  90. $error = false;
  91. $contains['photos'] = false;
  92. $contains['albums'] = false;
  93. // Call plugins
  94. // Note that updated albumId and path explicitly passed, rather
  95. // than using func_get_args() which will only return original ones
  96. Plugins::get()->activate(__METHOD__, 0, array($albumID, $path));
  97. // Get all files
  98. $files = glob($path . '/*');
  99. foreach ($files as $file) {
  100. // It is possible to move a file because of directory permissions but
  101. // the file may still be unreadable by the user
  102. if (!is_readable($file)) {
  103. $error = true;
  104. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not read file or directory (' . $file . ')');
  105. continue;
  106. }
  107. if (@exif_imagetype($file)!==false) {
  108. // Photo
  109. $contains['photos'] = true;
  110. if ($this->photo($file, $albumID)===false) {
  111. $error = true;
  112. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not import file (' . $file . ')');
  113. continue;
  114. }
  115. } else if (is_dir($file)) {
  116. // Folder
  117. $album = new Album(null);
  118. $newAlbumID = $album->add('[Import] ' . basename($file));
  119. $contains['albums'] = true;
  120. if ($newAlbumID===false) {
  121. $error = true;
  122. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
  123. continue;
  124. }
  125. $import = $this->server($file . '/', $newAlbumID);
  126. if ($import!==true&&$import!=='Notice: Import only contains albums!') {
  127. $error = true;
  128. Log::error(Database::get(), __METHOD__, __LINE__, 'Could not import folder. Function returned warning.');
  129. continue;
  130. }
  131. }
  132. }
  133. // Call plugins
  134. // Note that updated albumId and path explicitly passed, rather
  135. // than using func_get_args() which will only return original ones
  136. Plugins::get()->activate(__METHOD__, 1, array($albumID, $path));
  137. // The following returns will be caught in the front-end
  138. if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!';
  139. if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contained albums!';
  140. if ($error===true) return false;
  141. return true;
  142. }
  143. }
  144. ?>