Import.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. ###
  3. # @name Upload Module
  4. # @author Tobias Reich
  5. # @copyright 2014 by Tobias Reich
  6. ###
  7. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  8. class Import extends Module {
  9. static function photo($database, $plugins, $settings, $path, $albumID = 0, $description = '', $tags = '') {
  10. $info = getimagesize($path);
  11. $size = filesize($path);
  12. $photo = new Photo($database, $plugins, $settings, null);
  13. $nameFile = array(array());
  14. $nameFile[0]['name'] = $path;
  15. $nameFile[0]['type'] = $info['mime'];
  16. $nameFile[0]['tmp_name'] = $path;
  17. $nameFile[0]['error'] = 0;
  18. $nameFile[0]['size'] = $size;
  19. if (!$photo->add($nameFile, $albumID, $description, $tags)) return false;
  20. return true;
  21. }
  22. static function url($urls, $albumID = 0) {
  23. $error = false;
  24. # Parse
  25. $urls = str_replace(' ', '%20', $urls);
  26. $urls = explode(',', $urls);
  27. foreach ($urls as &$url) {
  28. if (@exif_imagetype($url)===false) {
  29. $error = true;
  30. continue;
  31. }
  32. $pathinfo = pathinfo($url);
  33. $filename = $pathinfo['filename'] . '.' . $pathinfo['extension'];
  34. $tmp_name = LYCHEE_DATA . $filename;
  35. if (@copy($url, $tmp_name)===false) $error = true;
  36. }
  37. $import = Import::server($albumID, LYCHEE_DATA);
  38. if ($error===false&&$import===true) return true;
  39. else return false;
  40. }
  41. static function server($albumID = 0, $path) {
  42. if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
  43. //determine OS type and set move cmd (Windows untested!)
  44. $myos = strtoupper( substr(PHP_OS,0,3) );
  45. if ( $myos === "WIN" ) { $osmv = 'MOVE'; } else { $osmv = 'mv'; }
  46. //generate tmp dir name by hashing epoch time & random number
  47. $tmpdirname = md5(time() . rand());
  48. global $database;
  49. if ( isset($tmpdirname) ){
  50. //make temporary directory
  51. if (@mkdir(LYCHEE_DATA . "$tmpdirname")!==false) {
  52. //get list of folders and move them to tmpdir
  53. $folders = glob($path . '*', GLOB_ONLYDIR);
  54. if ( isset($folders) ) {
  55. foreach ($folders as $folder) {
  56. $out = '';
  57. $ret = '';
  58. @exec("$osmv " . $folder . ' ' . LYCHEE_DATA . $tmpdirname, $out, $ret);
  59. if ( isset($ret) && ($ret > 0) ) Log::error($database, __METHOD__, __LINE__, "Failed to move directory or file ($ret):" . $folder);
  60. }
  61. }
  62. //get list of files and move them to tmpdir
  63. $files = glob($path . '*');
  64. if ( isset($files) ) {
  65. foreach ($files as $file) {
  66. //prevent logging second error for directories that could not be moved.
  67. if ( is_dir($file) ) continue;
  68. $out = '';
  69. $ret = '';
  70. @exec("$osmv " . $file . ' ' . LYCHEE_DATA . $tmpdirname, $out, $ret);
  71. if ( isset($ret) && ($ret > 0) ) Log::error($database, __METHOD__, __LINE__, "Failed to move directory or file ($ret):" . $file);
  72. }
  73. }
  74. //If no files could be copied to the temp dir, remove.
  75. if ( count( glob(LYCHEE_DATA . "$tmpdirname/*") ) == 0 ) { rmdir(LYCHEE_DATA . "$tmpdirname"); return false; }
  76. } else {
  77. Log::error($database, __METHOD__, __LINE__, 'Failed to create temporary directory');
  78. return false;
  79. }
  80. } else {
  81. Log::error($database, __METHOD__, __LINE__, 'Failed to generate temporary directory name');
  82. return false;
  83. }
  84. global $plugins, $settings;
  85. $contains['photos'] = false;
  86. $contains['albums'] = false;
  87. $path = LYCHEE_DATA . "$tmpdirname";
  88. $files = glob($path . '/*');
  89. foreach ($files as $file) {
  90. //It's possible to move a file because of directory permissions but
  91. //the file may still be unreadable by the user
  92. if (!is_readable($file) ) { Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file); continue; }
  93. if (@exif_imagetype($file)!==false) {
  94. # Photo
  95. if (!Import::photo($database, $plugins, $settings, $file, $albumID)) return false;
  96. $contains['photos'] = true;
  97. } else if (is_dir($file)) {
  98. # Folder
  99. $name = mysqli_real_escape_string($database, basename($file));
  100. $album = new Album($database, null, null, null);
  101. $newAlbumID = $album->add('[Import] ' . $name);
  102. if ($newAlbumID!==false) Import::server($newAlbumID, $file . '/');
  103. $contains['albums'] = true;
  104. }
  105. }
  106. if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!';
  107. if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contains albums!';
  108. return true;
  109. }
  110. }
  111. ?>