Import.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. # Parse path
  82. if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
  83. if (substr($path, -1)==='/') $path = substr($path, 0, -1);
  84. if (is_dir($path)===false) {
  85. Log::error($database, __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
  86. return 'Error: Given path is not a directory!';
  87. }
  88. # Skip folders of Lychee
  89. if ($path===LYCHEE_UPLOADS_BIG||($path . '/')===LYCHEE_UPLOADS_BIG||$path===LYCHEE_UPLOADS_THUMB||($path . '/')===LYCHEE_UPLOADS_THUMB) {
  90. Log::error($database, __METHOD__, __LINE__, 'Given path is a reserved path of Lychee (' . $path . ')');
  91. return 'Error: Given path is a reserved path of Lychee!';
  92. }
  93. /*if ($useTemp===true) {
  94. $path = Import::move($database, $path);
  95. if ($path===false) {
  96. Log::error($database, __METHOD__, __LINE__, 'Failed to move import to temporary directory');
  97. return false;
  98. }
  99. }*/
  100. $error = false;
  101. $contains['photos'] = false;
  102. $contains['albums'] = false;
  103. # Get all files
  104. $files = glob($path . '/*');
  105. foreach ($files as $file) {
  106. # It is possible to move a file because of directory permissions but
  107. # the file may still be unreadable by the user
  108. if (!is_readable($file)) {
  109. $error = true;
  110. Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
  111. continue;
  112. }
  113. if (@exif_imagetype($file)!==false) {
  114. # Photo
  115. if (!Import::photo($database, $plugins, $settings, $file, $albumID)) {
  116. $error = true;
  117. Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file);
  118. continue;
  119. }
  120. $contains['photos'] = true;
  121. } else if (is_dir($file)) {
  122. # Folder
  123. $name = mysqli_real_escape_string($database, basename($file));
  124. $album = new Album($database, null, null, null);
  125. $newAlbumID = $album->add('[Import] ' . $name);
  126. $contains['albums'] = true;
  127. if ($newAlbumID===false) {
  128. $error = true;
  129. Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
  130. continue;
  131. }
  132. $import = Import::server($newAlbumID, $file . '/', false);
  133. if ($import!==true&&$import!=='Notice: Import only contains albums!') {
  134. $error = true;
  135. Log::error($database, __METHOD__, __LINE__, 'Could not import folder. Function returned warning');
  136. continue;
  137. }
  138. }
  139. }
  140. # Delete tmpdir if import was successful
  141. /*if ($error===false&&$useTemp===true&&file_exists(LYCHEE_DATA . $tmpdirname)) {
  142. if (@rmdir(LYCHEE_DATA . $tmpdirname)===false) Log::error($database, __METHOD__, __LINE__, 'Could not delete temp-folder (' . LYCHEE_DATA . $tmpdirname . ') after successful import');
  143. }*/
  144. if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!';
  145. if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contains albums!';
  146. return true;
  147. }
  148. }
  149. ?>