Import.php 5.9 KB

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