Import.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 move($database, $path) {
  42. # Determine OS type and set move cmd (Windows untested!)
  43. $myos = substr(PHP_OS,0,3);
  44. $myos = strtoupper($myos);
  45. if ($myos==='WIN') $osmv = 'MOVE';
  46. else $osmv = 'mv';
  47. # Generate tmp dir name by hashing epoch time & random number
  48. $tmpdirname = md5(time() . rand());
  49. # Make temporary directory
  50. if (@mkdir(LYCHEE_DATA . $tmpdirname)===false) {
  51. Log::error($database, __METHOD__, __LINE__, 'Failed to create temporary directory');
  52. return false;
  53. }
  54. # Get list of files and move them to tmpdir
  55. $files = glob($path . '*');
  56. if (isset($files)) {
  57. foreach ($files as $file) {
  58. # Prevent unsupported files from being moved
  59. if (is_dir($file)===false&&@exif_imagetype($file)===false) continue;
  60. $out = '';
  61. $ret = '';
  62. $file = escapeshellarg($file);
  63. $cmd = $osmv . " $file " . LYCHEE_DATA . $tmpdirname;
  64. @exec($cmd, $out, $ret);
  65. if (isset($ret)&&($ret>0)) Log::error($database, __METHOD__, __LINE__, "Failed to move directory or file ($ret):" . $file);
  66. }
  67. }
  68. # If no files could be copied to the temp dir, remove
  69. $files = glob(LYCHEE_DATA . $tmpdirname . '/*');
  70. if (count($files)===0) {
  71. rmdir(LYCHEE_DATA . $tmpdirname);
  72. Log::error($database, __METHOD__, __LINE__, 'Import failed, because files could not be temporary moved to ' . LYCHEE_DATA);
  73. return false;
  74. }
  75. # Set new path
  76. $path = LYCHEE_DATA . $tmpdirname;
  77. return $path;
  78. }
  79. static function server($albumID = 0, $path, $useTemp = false) {
  80. global $database, $plugins, $settings;
  81. if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
  82. if ($useTemp===true) {
  83. $path = Import::move($database, $path);
  84. if ($path===false) {
  85. Log::error($database, __METHOD__, __LINE__, 'Failed to move import to temporary directory');
  86. return false;
  87. }
  88. }
  89. $error = false;
  90. $contains['photos'] = false;
  91. $contains['albums'] = false;
  92. # Get all files
  93. $files = glob($path . '/*');
  94. foreach ($files as $file) {
  95. # It is possible to move a file because of directory permissions but
  96. # the file may still be unreadable by the user
  97. if (!is_readable($file)) {
  98. $error = true;
  99. Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
  100. continue;
  101. }
  102. if (@exif_imagetype($file)!==false) {
  103. # Photo
  104. if (!Import::photo($database, $plugins, $settings, $file, $albumID)) {
  105. $error = true;
  106. Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file);
  107. continue;
  108. }
  109. $contains['photos'] = true;
  110. } else if (is_dir($file)) {
  111. # Folder
  112. $name = mysqli_real_escape_string($database, basename($file));
  113. $album = new Album($database, null, null, null);
  114. $newAlbumID = $album->add('[Import] ' . $name);
  115. if ($newAlbumID===false) {
  116. $error = true;
  117. Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
  118. continue;
  119. }
  120. if (Import::server($newAlbumID, $file . '/', false)==='Warning: Folder empty or no readable files to process!') {
  121. $error = true;
  122. Log::error($database, __METHOD__, __LINE__, 'Could not import folder. Function returned error');
  123. continue;
  124. }
  125. $contains['albums'] = true;
  126. }
  127. }
  128. # Delete tmpdir if import was successful
  129. if ($error===false&&$useTemp===true&&file_exists(LYCHEE_DATA . $tmpdirname)) {
  130. if (@rmdir(LYCHEE_DATA . $tmpdirname)===false) Log::error($database, __METHOD__, __LINE__, 'Could not delete temp-folder (' . LYCHEE_DATA . $tmpdirname . ') after successful import');
  131. }
  132. if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!';
  133. if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contains albums!';
  134. return true;
  135. }
  136. }
  137. ?>