ping.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  20. function ping_broadcast_notice($notice) {
  21. if ($notice->is_local != Notice::LOCAL_PUBLIC && $notice->is_local != Notice::LOCAL_NONPUBLIC) {
  22. return true;
  23. }
  24. // Array of servers, URL => type
  25. $notify = common_config('ping', 'notify');
  26. try {
  27. $profile = $notice->getProfile();
  28. } catch (Exception $e) {
  29. // @todo: distinguish the 'broken notice/profile' case from more general
  30. // transitory errors.
  31. common_log(LOG_ERR, "Exception getting notice profile: " . $e->getMessage());
  32. return true;
  33. }
  34. $tags = ping_notice_tags($notice);
  35. foreach ($notify as $notify_url => $type) {
  36. switch ($type) {
  37. case 'xmlrpc':
  38. case 'extended':
  39. $req = xmlrpc_encode_request('weblogUpdates.ping',
  40. array($profile->nickname, # site name
  41. common_local_url('showstream',
  42. array('nickname' => $profile->nickname)),
  43. common_local_url('shownotice',
  44. array('notice' => $notice->id)),
  45. common_local_url('userrss',
  46. array('nickname' => $profile->nickname)),
  47. $tags));
  48. $request = HTTPClient::start();
  49. $request->setConfig('connect_timeout', common_config('ping', 'timeout'));
  50. $request->setConfig('timeout', common_config('ping', 'timeout'));
  51. try {
  52. $httpResponse = $request->post($notify_url, array('Content-Type: text/xml'), $req);
  53. } catch (Exception $e) {
  54. common_log(LOG_ERR,
  55. "Exception pinging $notify_url: " . $e->getMessage());
  56. continue;
  57. }
  58. if (!$httpResponse || mb_strlen($httpResponse->getBody()) == 0) {
  59. common_log(LOG_WARNING,
  60. "XML-RPC empty results for ping ($notify_url, $notice->id) ");
  61. continue;
  62. }
  63. $response = xmlrpc_decode($httpResponse->getBody());
  64. if (is_array($response) && xmlrpc_is_fault($response)) {
  65. common_log(LOG_WARNING,
  66. "XML-RPC error for ping ($notify_url, $notice->id) ".
  67. "$response[faultString] ($response[faultCode])");
  68. } else {
  69. common_log(LOG_INFO,
  70. "Ping success for $notify_url $notice->id");
  71. }
  72. break;
  73. case 'get':
  74. case 'post':
  75. $args = array('name' => $profile->nickname,
  76. 'url' => common_local_url('showstream',
  77. array('nickname' => $profile->nickname)),
  78. 'changesURL' => common_local_url('userrss',
  79. array('nickname' => $profile->nickname)));
  80. $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
  81. if ($type === 'get') {
  82. $result = $fetcher->get($notify_url . '?' . http_build_query($args),
  83. array('User-Agent: ' . HTTPClient::userAgent()));
  84. } else {
  85. $result = $fetcher->post($notify_url,
  86. http_build_query($args),
  87. array('User-Agent: ' . HTTPClient::userAgent()));
  88. }
  89. if ($result->status != '200') {
  90. common_log(LOG_WARNING,
  91. "Ping error for '$notify_url' ($notice->id): ".
  92. "$result->body");
  93. } else {
  94. common_log(LOG_INFO,
  95. "Ping success for '$notify_url' ($notice->id): ".
  96. "'$result->body'");
  97. }
  98. break;
  99. default:
  100. common_log(LOG_WARNING, 'Unknown notify type for ' . $notify_url . ': ' . $type);
  101. }
  102. }
  103. return true;
  104. }
  105. function ping_notice_tags($notice) {
  106. $tag = new Notice_tag();
  107. $tag->notice_id = $notice->id;
  108. $tags = array();
  109. if ($tag->find()) {
  110. while ($tag->fetch()) {
  111. $tags[] = $tag->tag;
  112. }
  113. $tag->free();
  114. unset($tag);
  115. return implode('|', $tags);
  116. }
  117. return NULL;
  118. }