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

/modules/p2p/actions/giveobject.act.php

http://awarenet.googlecode.com/
PHP | 116 lines | 50 code | 19 blank | 47 comment | 23 complexity | 98d95e8152d2767eb661114a0a4afaf2 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?
  2. require_once($kapenta->installPath . 'modules/p2p/models/peer.mod.php');
  3. //--------------------------------------------------------------------------------------------------
  4. //* peer is giving a set of objects that we requested from them
  5. //--------------------------------------------------------------------------------------------------
  6. //+ Message from peer should be a signed list of objects, eg:
  7. //+
  8. //+ <kobjects>
  9. //+ <kobject type='aliases_alias'>
  10. //+ <UID>123456788</path>
  11. //+ ... rest of fields here ...
  12. //+ </kobject>
  13. //+ </kobjects>
  14. //+
  15. //+ Response to peer details the new state of these gifts, eg:
  16. //+
  17. //+ <thankyou>
  18. //+ <object>
  19. //+ <model>aliases_alias</model>
  20. //+ <UID>123456788</UID>
  21. //+ <status>has</status>
  22. //+ </object>
  23. //+ </thankyou>
  24. //+
  25. //+ Where status may be has, dnw or want (in the case of database failure)
  26. //postarg: message - object serialized as XML [string]
  27. //postarg: signature - RSA 4096 signature of message [string]
  28. //postarg: peer - UID of a P2P_Peer object [string]
  29. //----------------------------------------------------------------------------------------------
  30. // check arguments and message signature
  31. //----------------------------------------------------------------------------------------------
  32. if ('yes' != $registry->get('p2p.enabled')) { $page->doXmlError('P2P disabled on this peer.'); }
  33. if (false == array_key_exists('message', $_POST)) { $page->doXmlError('No message sent.'); }
  34. if (false == array_key_exists('signature', $_POST)) { $page->doXmlError('No signature sent.'); }
  35. if (false == array_key_exists('peer', $_POST)) { $page->doXmlError('Peer UID not sent.'); }
  36. $model = new P2P_Peer($_POST['peer']);
  37. if (false == $model->loaded) { $page->doXmlError('Peer not recognized.'); }
  38. $message = base64_decode($_POST['message']);
  39. $signature = base64_decode($_POST['signature']);
  40. if (false == $model->checkMessage($message, $signature)) { $page->doXmlError('Bad signature.'); }
  41. //----------------------------------------------------------------------------------------------
  42. // parse into array and store in database
  43. //----------------------------------------------------------------------------------------------
  44. $xd = new KXmlDocument($message);
  45. $children = $xd->getChildren();
  46. $objects = array();
  47. $reponse = '';
  48. foreach($children as $childId) {
  49. $add = true;
  50. $objXml = $xd->getInnerXml($childId, true);
  51. $objAry = $db->objectXmlToArray($objXml);
  52. $state = '';
  53. if (0 == count($objAry)) { $add = false; echo "COULD NOT PARSE XML<br/>\n"; }
  54. if (true == $db->objectExists($objAry['model'], $objAry['fields']['UID'])) {
  55. //--------------------------------------------------------------------------------------
  56. // check that this object is newer than our own, if we have it
  57. //--------------------------------------------------------------------------------------
  58. $local = $db->getObject($objAry['model'], $objAry['fields']['UID']);
  59. $localTime = $kapenta->strtotime($local['editedOn']);
  60. $newTime = $kapenta->strtotime($objAry['fields']['editedOn']);
  61. if ($localTime > $newTime) { $add = false; /* echo "OURS IS MORE RECENT."; */ }
  62. }
  63. //--------------------------------------------------------------------------------------
  64. // try save it if we want it
  65. //--------------------------------------------------------------------------------------
  66. if (true == $add) {
  67. $check = $db->storeObjectXml($objXml, false, false, false);
  68. if (true == $check) { $state = 'has'; } // we now have it
  69. else { $state = 'want'; } // we still want it
  70. } else { $state = 'dnw'; } // we didn't want it
  71. //--------------------------------------------------------------------------------------
  72. // let other peers know about it
  73. //--------------------------------------------------------------------------------------
  74. $args = array(
  75. 'type' => 'object',
  76. 'model' => $objAry['model'],
  77. 'UID' => $objAry['fields']['UID'],
  78. 'properties' => $objAry['fields'],
  79. 'fileName' => '',
  80. 'peer' => $model->UID
  81. );
  82. $kapenta->raiseEvent('*', 'object_received', $args);
  83. //--------------------------------------------------------------------------------------
  84. // note this in the reply
  85. //--------------------------------------------------------------------------------------
  86. $response .= ''
  87. . "\t<object>\n"
  88. . "\t\t<model>" . $objAry['model'] . "</model>\n"
  89. . "\t\t<UID>" . $objAry['fields']['UID'] . "</UID>\n"
  90. . "\t\t<status>$state</status>\n"
  91. . "\t</object>\n";
  92. }
  93. //----------------------------------------------------------------------------------------------
  94. // done
  95. //----------------------------------------------------------------------------------------------
  96. echo "<thankyou>\n" . $response . "</thankyou>\n";
  97. ?>