Import.php 5.7 KB

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