upload.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @name Upload Module
  4. * @author Philipp Maurer
  5. * @author Tobias Reich
  6. * @copyright 2014 by Philipp Maurer, Tobias Reich
  7. */
  8. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  9. function importPhoto($path, $albumID = 0, $description = '', $tags = '') {
  10. $info = getimagesize($path);
  11. $size = filesize($path);
  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. return upload($nameFile, $albumID, $description, $tags);
  19. }
  20. function importUrl($url, $albumID = 0) {
  21. if (strpos($url, ',')!==false) {
  22. // Multiple photos
  23. $url = explode(',', $url);
  24. foreach ($url as &$key) {
  25. $key = str_replace(' ', '%20', $key);
  26. if (@getimagesize($key)) {
  27. $pathinfo = pathinfo($key);
  28. $filename = $pathinfo['filename'].".".$pathinfo['extension'];
  29. $tmp_name = __DIR__ . '/../../uploads/import/' . $filename;
  30. copy($key, $tmp_name);
  31. }
  32. }
  33. return importServer($albumID);
  34. } else {
  35. // One photo
  36. $url = str_replace(' ', '%20', $url);
  37. if (@getimagesize($url)) {
  38. $pathinfo = pathinfo($url);
  39. $filename = $pathinfo['filename'].".".$pathinfo['extension'];
  40. $tmp_name = __DIR__ . "/../../uploads/import/$filename";
  41. copy($url, $tmp_name);
  42. return importPhoto($tmp_name, $albumID);
  43. }
  44. }
  45. return false;
  46. }
  47. function importServer($albumID = 0, $path) {
  48. if (!isset($path)) $path = __DIR__ . '/../../uploads/import/';
  49. global $database;
  50. $files = glob($path . '*');
  51. $contains['photos'] = false;
  52. $contains['albums'] = false;
  53. foreach ($files as $file) {
  54. if (@getimagesize($file)) {
  55. // Photo
  56. if (!importPhoto($file, $albumID)) return false;
  57. $contains['photos'] = true;
  58. } else if (is_dir($file)) {
  59. $name = mysqli_real_escape_string($database, basename($file));
  60. $newAlbumID = addAlbum('[Import] ' . $name);
  61. if ($newAlbumID!==false) importServer($newAlbumID, $file . '/');
  62. $contains['albums'] = true;
  63. }
  64. }
  65. if ($contains['photos']===false&&$contains['albums']===false) return "Warning: Folder empty!";
  66. if ($contains['photos']===false&&$contains['albums']===true) return "Notice: Import only contains albums!";
  67. return true;
  68. }
  69. ?>