Import.php 5.2 KB

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