Import.php 5.8 KB

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