PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/sync/actions/findfile.act.php

http://awarenet.googlecode.com/
PHP | 110 lines | 60 code | 26 blank | 24 comment | 23 complexity | 3cd5a94eecb8b930f14ffb6e3bfd39d7 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?
  2. require_once($kapenta->installPath . 'modules/sync/models/download.mod.php');
  3. //-------------------------------------------------------------------------------------------------
  4. // looks for a file among peers and downloads it if found
  5. //-------------------------------------------------------------------------------------------------
  6. //---------------------------------------------------------------------------------------------
  7. // load the download record and set status to 'searching'
  8. //---------------------------------------------------------------------------------------------
  9. if ('' == $req->ref) { $page->doXmlError('Download not specified.'); }
  10. if (false == $db->objectExists('sync_download', $req->ref)) { $page->doXmlError('no such download'); }
  11. $model = new Sync_Download($req->ref);
  12. if ($model->status != 'wait') { $page->doXmlError('busy'); }
  13. if (true == $model->maxDownloads()) { $page->doXmlError('Already downloading max files.'); }
  14. $model->status = 'searching';
  15. $model->save();
  16. $kapenta->logSync("searching for file " . $model->filename . "\n");
  17. $downloadComplete = false;
  18. //---------------------------------------------------------------------------------------------
  19. // check all our peers, maybe they have the file
  20. //---------------------------------------------------------------------------------------------
  21. $peers = $sync->listPeers();
  22. foreach($peers as $peer) {
  23. $kapenta->logSync("peer " . $peer['UID'] . " serverurl " . $peer['serverurl'] . "\n");
  24. $testUrl = $peer['serverurl'] . 'sync/hasfile/file_'
  25. . base64_encode($model->filename) . '/';
  26. $result = $sync->curlGet($testUrl, $peer['password']);
  27. $kapenta->logSync("findfile peer result ($testUrl) \n$result\n");
  28. $found = false;
  29. if (strpos($result, '</result>') > 0) { $found = true; } // not an error
  30. if (strpos($result, 'not found') > 0) { $found = false; }
  31. if (true == $found) {
  32. //-------------------------------------------------------------------------------------
  33. // peer has the file we're looking for, download it
  34. //-------------------------------------------------------------------------------------
  35. $success = false;
  36. $hash = trim(strip_tags($result));
  37. $model->hash = $hash;
  38. $model->save();
  39. $kapenta->logSync("found file " . $model->filename . " (hash: " . $hash . ")\n");
  40. $getUrl = $peer['serverurl'] . 'sync/getfile/file_'
  41. . base64_encode($model->filename) . '/';
  42. $result = $sync->curlGet($getUrl, $peer['password']);
  43. $rHash = trim(sha1($result));
  44. $kapenta->logSync("downloaded file $getUrl \n(" . strlen($result) . " bytes)(hash: $hash)\n");
  45. if (trim($rHash) == trim($hash)) {
  46. //---------------------------------------------------------------------------------
  47. // file has come through correctly
  48. //---------------------------------------------------------------------------------
  49. $kapenta->logSync("hashes match, saving file " . $model->filename . "\n");
  50. $kapenta->filePutContents($model->filename, $result, true);
  51. $model->delete(); // done with this
  52. $downloadComplete = true;
  53. break;
  54. } else {
  55. //---------------------------------------------------------------------------------
  56. // file has NOT come through correctly
  57. //---------------------------------------------------------------------------------
  58. $kapenta->logSync("hashes do not match, discarding download\n$hash != " . $rHash . "\n");
  59. }
  60. }
  61. }
  62. //---------------------------------------------------------------------------------------------
  63. // file not found, set status back to wait
  64. //---------------------------------------------------------------------------------------------
  65. if (false == $downloadComplete) {
  66. $kapenta->logSync("file not sound, adding back to queue\n");
  67. $model->status = 'wait';
  68. $model->save();
  69. } else {
  70. //-----------------------------------------------------------------------------------------
  71. // try for next file only if the last one worked (sync will try it again before long)
  72. //-----------------------------------------------------------------------------------------
  73. $nextDownload = $model->getNextDownload();
  74. if (false == $nextDownload) {
  75. $kapenta->logSync("all downloads completed\n");
  76. } else {
  77. $ofn = $kapenta->installPath . 'data/temp/' . $kapenta->createUID() . '.sync'
  78. $od = '--output-document=' . $ofn;
  79. $cmd = 'wget ' . $od . ' ' . $kapenta->serverPath . 'sync/findfile/' . $nextDownload;
  80. $kapenta->logSync("processing next download: $nextDownload\n$cmd\n");
  81. $kapenta->procExecBackground($cmd);
  82. }
  83. }
  84. ?>