filesync.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /**
  9. * filesync can be called with a PUT method.
  10. * PUT takes a stream starting with a 2 byte blocksize,
  11. * followed by binary md5 of the blocks. Everything in big-endian.
  12. * The return is a json encoded with:
  13. * - 'transferid'
  14. * - 'needed' chunks
  15. * - 'last' checked chunk
  16. * The URL is made of 3 parts, the service url (remote.php/filesync/), the sync
  17. * type and the path in ownCloud.
  18. * At the moment the only supported sync type is 'oc_chunked'.
  19. * The final URL will look like http://.../remote.php/filesync/oc_chunked/path/to/file
  20. */
  21. // load needed apps
  22. $RUNTIME_APPTYPES=array('filesystem', 'authentication', 'logging');
  23. OC_App::loadApps($RUNTIME_APPTYPES);
  24. if(!OC_User::isLoggedIn()) {
  25. if(!isset($_SERVER['PHP_AUTH_USER'])) {
  26. header('WWW-Authenticate: Basic realm="ownCloud Server"');
  27. header('HTTP/1.0 401 Unauthorized');
  28. echo 'Valid credentials must be supplied';
  29. exit();
  30. } else {
  31. if(!OC_User::login($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"])) {
  32. exit();
  33. }
  34. }
  35. }
  36. list($type, $file) = explode('/', substr($path_info, 1+strlen($service)+1), 2);
  37. if ($type != 'oc_chunked') {
  38. OC_Response::setStatus(OC_Response::STATUS_NOT_FOUND);
  39. die;
  40. }
  41. if (!\OC\Files\Filesystem::is_file($file)) {
  42. OC_Response::setStatus(OC_Response::STATUS_NOT_FOUND);
  43. die;
  44. }
  45. switch($_SERVER['REQUEST_METHOD']) {
  46. case 'PUT':
  47. $input = fopen("php://input", "r");
  48. $org_file = \OC\Files\Filesystem::fopen($file, 'rb');
  49. $info = array(
  50. 'name' => basename($file),
  51. );
  52. $sync = new OC_FileChunking($info);
  53. $result = $sync->signature_split($org_file, $input);
  54. echo json_encode($result);
  55. break;
  56. default:
  57. OC_Response::setStatus(OC_Response::STATUS_NOT_FOUND);
  58. }