slap.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2010, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
  21. $longoptions = array('verify', 'slap=', 'notice=');
  22. $helptext = <<<END_OF_HELP
  23. slap.php [options]
  24. Test generation and sending of magic envelopes for Salmon slaps.
  25. --notice=N generate entry for this notice number
  26. --verify send signed magic envelope to Tuomas Koski's test service
  27. --slap=<url> send signed Salmon slap to the destination endpoint
  28. END_OF_HELP;
  29. require_once INSTALLDIR.'/scripts/commandline.inc';
  30. if (!have_option('--notice')) {
  31. print "$helptext";
  32. exit(1);
  33. }
  34. $notice_id = get_option_value('--notice');
  35. $notice = Notice::getKV('id', $notice_id);
  36. $profile = $notice->getProfile();
  37. $entry = $notice->asAtomEntry(true);
  38. echo "== Original entry ==\n\n";
  39. print $entry;
  40. print "\n\n";
  41. $magic_env = MagicEnvelope::signAsUser($entry, $profile->getUser());
  42. $envxml = $magic_env->toXML();
  43. echo "== Signed envelope ==\n\n";
  44. print $envxml;
  45. print "\n\n";
  46. echo "== Testing local verification ==\n\n";
  47. $magic_env = new MagicEnvelope($envxml);
  48. $activity = new Activity($magic_env->getPayload()->documentElement);
  49. $actprofile = Profile::fromUri($activity->actor->id);
  50. $ok = $magic_env->verify($actprofile);
  51. if ($ok) {
  52. print "OK\n\n";
  53. } else {
  54. print "FAIL\n\n";
  55. }
  56. if (have_option('--verify')) {
  57. $url = 'http://www.madebymonsieur.com/ostatus_discovery/magic_env/validate/';
  58. echo "== Testing remote verification ==\n\n";
  59. print "Sending for verification to $url ...\n";
  60. $client = new HTTPClient();
  61. try {
  62. $response = $client->post($url, array(), array('magic_env' => $envxml));
  63. print $response->getStatus() . "\n\n";
  64. print $response->getBody() . "\n\n";
  65. } catch (HTTP_Request2_Exception $e) {
  66. print 'Failed POST to URL '.var_export($url, true).': '.$e->getMessage();
  67. }
  68. }
  69. if (have_option('--slap')) {
  70. $url = get_option_value('--slap');
  71. echo "== Remote salmon slap ==\n\n";
  72. print "Sending signed Salmon slap to $url ...\n";
  73. $ok = Salmon::post($url, $entry, $profile->getUser());
  74. if ($ok) {
  75. print "OK\n\n";
  76. } else {
  77. print "FAIL\n\n";
  78. }
  79. }