Import.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. $info = getimagesize($path);
  10. $size = filesize($path);
  11. $photo = new Photo($database, $plugins, $settings, 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. if (!$photo->add($nameFile, $albumID, $description, $tags)) return false;
  19. return true;
  20. }
  21. static function url($urls, $albumID = 0) {
  22. $error = false;
  23. # Parse
  24. $urls = str_replace(' ', '%20', $urls);
  25. $urls = explode(',', $urls);
  26. foreach ($urls as &$url) {
  27. if (@exif_imagetype($url)===false) {
  28. $error = true;
  29. continue;
  30. }
  31. $pathinfo = pathinfo($url);
  32. $filename = $pathinfo['filename'] . '.' . $pathinfo['extension'];
  33. $tmp_name = LYCHEE_DATA . $filename;
  34. if (@copy($url, $tmp_name)===false) $error = true;
  35. }
  36. $import = Import::server($albumID, LYCHEE_DATA);
  37. if ($error===false&&$import===true) return true;
  38. else return false;
  39. }
  40. static function server($albumID = 0, $path) {
  41. global $database, $plugins, $settings;
  42. # Parse path
  43. if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
  44. if (substr($path, -1)==='/') $path = substr($path, 0, -1);
  45. if (is_dir($path)===false) {
  46. Log::error($database, __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
  47. return 'Error: Given path is not a directory!';
  48. }
  49. # Skip folders of Lychee
  50. if ($path===LYCHEE_UPLOADS_BIG||($path . '/')===LYCHEE_UPLOADS_BIG||
  51. $path===LYCHEE_UPLOADS_MEDIUM||($path . '/')===LYCHEE_UPLOADS_MEDIUM||
  52. $path===LYCHEE_UPLOADS_THUMB||($path . '/')===LYCHEE_UPLOADS_THUMB) {
  53. Log::error($database, __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
  54. return 'Error: Given path is a reserved path of Lychee!';
  55. }
  56. $error = false;
  57. $contains['photos'] = false;
  58. $contains['albums'] = false;
  59. # Get all files
  60. $files = glob($path . '/*');
  61. foreach ($files as $file) {
  62. # It is possible to move a file because of directory permissions but
  63. # the file may still be unreadable by the user
  64. if (!is_readable($file)) {
  65. $error = true;
  66. Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
  67. continue;
  68. }
  69. if (@exif_imagetype($file)!==false) {
  70. # Photo
  71. if (!Import::photo($database, $plugins, $settings, $file, $albumID)) {
  72. $error = true;
  73. Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file);
  74. continue;
  75. }
  76. $contains['photos'] = true;
  77. } else if (is_dir($file)) {
  78. # Folder
  79. $name = mysqli_real_escape_string($database, basename($file));
  80. $album = new Album($database, null, null, null);
  81. $newAlbumID = $album->add('[Import] ' . $name);
  82. $contains['albums'] = true;
  83. if ($newAlbumID===false) {
  84. $error = true;
  85. Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
  86. continue;
  87. }
  88. $import = Import::server($newAlbumID, $file . '/');
  89. if ($import!==true&&$import!=='Notice: Import only contains albums!') {
  90. $error = true;
  91. Log::error($database, __METHOD__, __LINE__, 'Could not import folder. Function returned warning');
  92. continue;
  93. }
  94. }
  95. }
  96. if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!';
  97. if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contains albums!';
  98. return true;
  99. }
  100. }
  101. ?>