upload.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/local/bin/php
  2. <?php
  3. # Debug
  4. $DEBUG = false;
  5. if ($_ENV['DEBUG']) {
  6. $DEBUG = true;
  7. }
  8. # Includes
  9. require_once 'opendrive.php';
  10. # Command-line parameters
  11. global $argc;
  12. global $argv;
  13. $BYTES = 1 * 1024 * 1024 * 1024;
  14. if (($argc > 1) && (strlen($argv[1]) > 0)) {
  15. $BYTES = intval($argv[1]) * 1024 * 1024;
  16. }
  17. # Run serially
  18. $PID_FILE = sys_get_temp_dir() . '/upload.pid';
  19. if (file_exists($PID_FILE)) {
  20. $OLD_PID = trim(@file_get_contents($PID_FILE));
  21. if ($OLD_PID && posix_kill($OLD_PID, 0)) {
  22. # Already running
  23. if ($DEBUG) {
  24. echo 'Already running: ' . $OLD_PID . "\n";
  25. }
  26. exit(0);
  27. }
  28. }
  29. file_put_contents($PID_FILE, posix_getpid());
  30. unset($PID_FILE);
  31. # Open the DB connection
  32. $dbh = dbOpen();
  33. # Open the OpenDrive session
  34. $session = login();
  35. if (!$session) {
  36. die("Unable to connect to OpenDrive\n");
  37. }
  38. # Prepare statements
  39. $folders = $dbh->prepare('SELECT base, path FROM files WHERE ' .
  40. "type = 'folder' AND " .
  41. '(remote_mtime IS NULL OR remote_mtime < mtime) ' .
  42. 'ORDER BY base, path');
  43. $files = $dbh->prepare('SELECT base, path FROM files WHERE ' .
  44. "(type != 'ignored' AND type != 'folder') AND " .
  45. '(remote_mtime IS NULL OR remote_mtime < mtime) ' .
  46. 'ORDER BY priority DESC, path'
  47. );
  48. $update = $dbh->prepare('UPDATE files SET remote_mtime = now() WHERE base = :base AND path = :path');
  49. # Create all the folders
  50. if ($DEBUG) {
  51. echo "Creating folders\n";
  52. }
  53. $folders->execute();
  54. while ($row = $folders->fetch()) {
  55. # Ensure the local reference is valid
  56. $file = $row['base'] . '/' . $row['path'];
  57. if (!is_dir($file)) {
  58. echo 'Invalid directory: ' . $file . "\n";
  59. continue;
  60. }
  61. # Create the folder
  62. if ($DEBUG) {
  63. echo 'Creating folder: ' . $row['path'] . "\n";
  64. }
  65. if (folderID($session, $row['path']) || mkFolder($session, $row['path'])) {
  66. $update->execute(array(':base' => $row['base'], ':path' => $row['path']));
  67. }
  68. }
  69. # Loop through all the files
  70. if ($DEBUG) {
  71. echo 'Uploading files with byte limit: ' . $BYTES . "\n";
  72. }
  73. $uploaded = 0;
  74. $files->execute();
  75. while ($row = $files->fetch()) {
  76. # Ensure the local reference is valid
  77. $file = $row['base'] . '/' . $row['path'];
  78. if (!is_readable($file)) {
  79. echo 'Invalid file: ' . $file . "\n";
  80. continue;
  81. }
  82. $stat = stat($file);
  83. # Upload the file
  84. if ($DEBUG) {
  85. echo 'Uploading file (' . $stat['size'] . ' bytes): ' . $row['path'] . "\n";
  86. }
  87. $result = fileUpload($session, $row['path'], $file);
  88. if ($result !== false && $result == $stat['size']) {
  89. $uploaded += $result;
  90. $update->execute(array(':base' => $row['base'], ':path' => $row['path']));
  91. }
  92. # Bail if we exceed the byte limit
  93. if ($DEBUG) {
  94. echo 'Uploaded ' . $uploaded . ' of ' . $BYTES . " bytes\n";
  95. }
  96. if ($BYTES && $uploaded >= $BYTES) {
  97. if ($DEBUG) {
  98. echo "Exiting due to byte limit\n";
  99. }
  100. break;
  101. }
  102. }
  103. # Logout from OpenDrive
  104. logout($session);
  105. ?>