upload.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. # Open the DB connection
  18. $dbh = dbOpen();
  19. # Open the OpenDrive session
  20. $session = login();
  21. if (!$session) {
  22. die("Unable to connect to OpenDrive\n");
  23. }
  24. # Prepare statements
  25. $folders = $dbh->prepare('SELECT base, path FROM files WHERE ' .
  26. "type = 'folder' AND " .
  27. '(remote_mtime IS NULL OR remote_mtime < mtime) ' .
  28. 'ORDER BY base, path');
  29. $files = $dbh->prepare('SELECT base, path FROM files WHERE ' .
  30. "(type != 'ignored' AND type != 'folder') AND " .
  31. '(remote_mtime IS NULL OR remote_mtime < mtime) ' .
  32. 'ORDER BY base, path'
  33. );
  34. $update = $dbh->prepare('UPDATE files SET remote_mtime = now() WHERE base = :base AND path = :path');
  35. # Create all the folders
  36. if ($DEBUG) {
  37. echo "Creating folders\n";
  38. }
  39. $folders->execute();
  40. while ($row = $folders->fetch()) {
  41. # Ensure the local reference is valid
  42. $file = $row['base'] . '/' . $row['path'];
  43. if (!is_dir($file)) {
  44. echo 'Invalid directory: ' . $file . "\n";
  45. continue;
  46. }
  47. # Create the folder
  48. if ($DEBUG) {
  49. echo 'Creating folder: ' . $row['path'] . "\n";
  50. }
  51. if (folderID($session, 'Test/' . $row['path']) || mkFolder($session, 'Test/' . $row['path'])) {
  52. $update->execute(array(':base' => $row['base'], ':path' => $row['path']));
  53. }
  54. }
  55. # Loop through all the files
  56. if ($DEBUG) {
  57. echo 'Uploading files with byte limit: ' . $BYTES . "\n";
  58. }
  59. $uploaded = 0;
  60. $files->execute();
  61. while ($row = $files->fetch()) {
  62. # Ensure the local reference is valid
  63. $file = $row['base'] . '/' . $row['path'];
  64. if (!is_readable($file)) {
  65. echo 'Invalid file: ' . $file . "\n";
  66. continue;
  67. }
  68. $stat = stat($file);
  69. # Upload the file
  70. if ($DEBUG) {
  71. echo 'Uploading file (' . $stat['size'] . ' bytes): ' . $row['path'] . "\n";
  72. }
  73. $result = fileUpload($session, 'Test/' . $row['path'], $file);
  74. if ($result !== false && $result == $stat['size']) {
  75. $uploaded += $result;
  76. $update->execute(array(':base' => $row['base'], ':path' => $row['path']));
  77. }
  78. # Bail if we exceed the byte limit
  79. if ($DEBUG) {
  80. echo 'Uploaded ' . $uploaded . ' of ' . $BYTES . " bytes\n";
  81. }
  82. if ($BYTES && $uploaded >= $BYTES) {
  83. if ($DEBUG) {
  84. echo "Exiting due to byte limit\n";
  85. }
  86. break;
  87. }
  88. }
  89. # Logout from OpenDrive
  90. logout($session);
  91. ?>