salmon.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * A sample module to show best practices for StatusNet plugins
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @package StatusNet
  24. * @author James Walker <james@status.net>
  25. * @copyright 2010 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  27. * @link http://status.net/
  28. */
  29. class Salmon
  30. {
  31. const REL_SALMON = 'salmon';
  32. const REL_MENTIONED = 'mentioned';
  33. // XXX: these are deprecated
  34. const NS_REPLIES = "http://salmon-protocol.org/ns/salmon-replies";
  35. const NS_MENTIONS = "http://salmon-protocol.org/ns/salmon-mention";
  36. /**
  37. * Sign and post the given Atom entry as a Salmon message.
  38. *
  39. * Side effects: may generate a keypair on-demand for the given user,
  40. * which can be very slow on some systems (like those without php5-gmp).
  41. *
  42. * @param string $endpoint_uri
  43. * @param string $xml string representation of payload
  44. * @param User $user local user profile whose keys we sign with
  45. * @return boolean success
  46. */
  47. public static function post($endpoint_uri, $xml, User $user)
  48. {
  49. if (empty($endpoint_uri)) {
  50. common_debug('No endpoint URI for Salmon post to '.$user->getUri());
  51. return false;
  52. }
  53. try {
  54. $magic_env = MagicEnvelope::signAsUser($xml, $user);
  55. $envxml = $magic_env->toXML();
  56. } catch (Exception $e) {
  57. common_log(LOG_ERR, "Salmon unable to sign: " . $e->getMessage());
  58. return false;
  59. }
  60. $headers = array('Content-Type: application/magic-envelope+xml');
  61. try {
  62. $client = new HTTPClient();
  63. $client->setBody($envxml);
  64. $response = $client->post($endpoint_uri, $headers);
  65. } catch (HTTP_Request2_Exception $e) {
  66. common_log(LOG_ERR, "Salmon post to $endpoint_uri failed: " . $e->getMessage());
  67. return false;
  68. }
  69. if ($response->getStatus() != 200) {
  70. common_log(LOG_ERR, sprintf('Salmon (from profile %d) endpoint %s returned status %s: %s',
  71. $user->id, $endpoint_uri, $response->getStatus(), $response->getBody()));
  72. return false;
  73. }
  74. // Success!
  75. return true;
  76. }
  77. }