Import.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. static function photo($database, $plugins, $settings, $path, $albumID = 0, $description = '', $tags = '') {
  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($database, $plugins, $settings, 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. if (!$photo->add($nameFile, $albumID, $description, $tags)) return false;
  21. return true;
  22. }
  23. static function url($urls, $albumID = 0) {
  24. $error = false;
  25. # Parse
  26. $urls = str_replace(' ', '%20', $urls);
  27. $urls = explode(',', $urls);
  28. foreach ($urls as &$url) {
  29. # Verify extension
  30. $extension = getExtension($url);
  31. if (!in_array(strtolower($extension), Photo::$validExtensions, true)) {
  32. $error = true;
  33. continue;
  34. }
  35. # Verify image
  36. $type = @exif_imagetype($url);
  37. if (!in_array($type, Photo::$validTypes, true)) {
  38. $error = true;
  39. continue;
  40. }
  41. $pathinfo = pathinfo($url);
  42. $filename = $pathinfo['filename'] . '.' . $pathinfo['extension'];
  43. $tmp_name = LYCHEE_DATA . $filename;
  44. if (@copy($url, $tmp_name)===false) $error = true;
  45. }
  46. $import = Import::server($albumID, LYCHEE_DATA);
  47. if ($error===false&&$import===true) return true;
  48. else return false;
  49. }
  50. static function server($albumID = 0, $path) {
  51. global $database, $plugins, $settings;
  52. # Parse path
  53. if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
  54. if (substr($path, -1)==='/') $path = substr($path, 0, -1);
  55. if (is_dir($path)===false) {
  56. Log::error($database, __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
  57. return 'Error: Given path is not a directory!';
  58. }
  59. # Skip folders of Lychee
  60. if ($path===LYCHEE_UPLOADS_BIG||($path . '/')===LYCHEE_UPLOADS_BIG||
  61. $path===LYCHEE_UPLOADS_MEDIUM||($path . '/')===LYCHEE_UPLOADS_MEDIUM||
  62. $path===LYCHEE_UPLOADS_THUMB||($path . '/')===LYCHEE_UPLOADS_THUMB) {
  63. Log::error($database, __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
  64. return 'Error: Given path is a reserved path of Lychee!';
  65. }
  66. $error = false;
  67. $contains['photos'] = false;
  68. $contains['albums'] = false;
  69. # Invoke plugins directly, as instance method not valid here
  70. # Note that updated albumId and path explicitly passed, rather
  71. # than using func_get_args() which will only return original ones
  72. $plugins->activate(__METHOD__ . ":before", array($albumID, $path));
  73. # Get all files
  74. $files = glob($path . '/*');
  75. foreach ($files as $file) {
  76. # It is possible to move a file because of directory permissions but
  77. # the file may still be unreadable by the user
  78. if (!is_readable($file)) {
  79. $error = true;
  80. Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
  81. continue;
  82. }
  83. if (@exif_imagetype($file)!==false) {
  84. # Photo
  85. if (!Import::photo($database, $plugins, $settings, $file, $albumID)) {
  86. $error = true;
  87. Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file);
  88. continue;
  89. }
  90. $contains['photos'] = true;
  91. } else if (is_dir($file)) {
  92. # Folder
  93. $name = mysqli_real_escape_string($database, basename($file));
  94. $album = new Album($database, null, null, null);
  95. $newAlbumID = $album->add('[Import] ' . $name);
  96. $contains['albums'] = true;
  97. if ($newAlbumID===false) {
  98. $error = true;
  99. Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
  100. continue;
  101. }
  102. $import = Import::server($newAlbumID, $file . '/');
  103. if ($import!==true&&$import!=='Notice: Import only contains albums!') {
  104. $error = true;
  105. Log::error($database, __METHOD__, __LINE__, 'Could not import folder. Function returned warning');
  106. continue;
  107. }
  108. }
  109. }
  110. # Invoke plugins directly, as instance method not valid here
  111. # Note that updated albumId and path explicitly passed, rather
  112. # than using func_get_args() which will only return original ones
  113. $plugins->activate(__METHOD__ . ":after", array($albumID, $path));
  114. if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!';
  115. if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contains albums!';
  116. return true;
  117. }
  118. }
  119. ?>