PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/p2p/actions/filepart.act.php

http://awarenet.googlecode.com/
PHP | 65 lines | 25 code | 13 blank | 27 comment | 26 complexity | 1ed2a34e57e299f125057b750ab76d66 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?
  2. require_once($kapenta->installPath . 'modules/p2p/models/peer.mod.php');
  3. require_once($kapenta->installPath . 'modules/p2p/inc/klargefile.class.php');
  4. //--------------------------------------------------------------------------------------------------
  5. //* allow clients to retrieve a part of a file
  6. //--------------------------------------------------------------------------------------------------
  7. //+ Example message:
  8. //+
  9. //+ <part>
  10. //+ <path>data/videos/1/2/3/12345678.flv</path>
  11. //+ <hash>[sha1 hash of part]</hash>
  12. //+ <size>512</size>
  13. //+ <index>3</index>
  14. //+ </part>
  15. //+
  16. //+ Note that part size is measured in kilobytes, and part number starts from 0.
  17. //+
  18. //postarg: message - XML document describing file part to be retrieved [string]
  19. //postarg: signature - RSA 4096 signature of message [string]
  20. //postarg: peer - UID of a P2P_Peer object [string]
  21. //----------------------------------------------------------------------------------------------
  22. // check arguments and message signature
  23. //----------------------------------------------------------------------------------------------
  24. if ('yes' != $registry->get('p2p.enabled')) { $page->doXmlError('P2P disabled on this peer.'); }
  25. if (false == array_key_exists('message', $_POST)) { $page->doXmlError('No message sent.'); }
  26. if (false == array_key_exists('signature', $_POST)) { $page->doXmlError('No signature sent.'); }
  27. if (false == array_key_exists('peer', $_POST)) { $page->doXmlError('Peer UID not sent.'); }
  28. $model = new P2P_Peer($_POST['peer']);
  29. if (false == $model->loaded) { $page->doXmlError('Peer not recognized.'); }
  30. $message = base64_decode($_POST['message']);
  31. $signature = base64_decode($_POST['signature']);
  32. if (false == $model->checkMessage($message, $signature)) { $page->doXmlError('Bad signature.'); }
  33. //----------------------------------------------------------------------------------------------
  34. // parse the message
  35. //----------------------------------------------------------------------------------------------
  36. $xd = new KXmlDocument($message);
  37. $detail = $xd->getChildren2d(); //% root node's children [dict]
  38. if (false == array_key_exists('path', $detail)) { $page->doXmlError('Missing: path'); }
  39. if (false == array_key_exists('hash', $detail)) { $page->doXmlError('Missing: hash'); }
  40. if (false == array_key_exists('size', $detail)) { $page->doXmlError('Missing: size'); }
  41. if (false == array_key_exists('index', $detail)) { $page->doXmlError('Missing: index'); }
  42. //----------------------------------------------------------------------------------------------
  43. // get and return the file part
  44. //----------------------------------------------------------------------------------------------
  45. if (false == $kapenta->fileExists($detail['path'])) { $page->doXmlError('File not found.'); }
  46. $meta = new KLargeFile($detail['path']);
  47. $raw = $meta->getPart($detail['index']);
  48. if ('' == $raw) { $page->doXmlError('Part could not be loaded: ' . $detail['index'] . '.'); }
  49. if (sha1($raw) != $detail['hash']) { $page->doXmlError('Hash mismatch.'); }
  50. //TODO: discover if we really need to b64 encode, it'd be nice to remove this overhead
  51. echo base64_encode($raw);
  52. ?>