Import.php 5.1 KB

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