X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fping.php;h=e1c7c748e280339524aecac58f47e6cd6a26ccb7;hb=9c8b2b567f00b7a686a31190ad5caf70661cd31d;hp=d7472b920a1075bb57150007260e0e3027d1e680;hpb=2e3f228b4bdb7bfc59a1fd0ce52fbebde7071566;p=quix0rs-gnu-social.git diff --git a/lib/ping.php b/lib/ping.php index d7472b920a..e1c7c748e2 100644 --- a/lib/ping.php +++ b/lib/ping.php @@ -1,7 +1,7 @@ . */ -if (!defined('LACONICA')) { exit(1); } +if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } function ping_broadcast_notice($notice) { + if ($notice->is_local != Notice::LOCAL_PUBLIC && $notice->is_local != Notice::LOCAL_NONPUBLIC) { + return true; + } - if (!$notice->is_local) { - return true; - } + # Array of servers, URL => type + $notify = common_config('ping', 'notify'); + try { + $profile = $notice->getProfile(); + } catch (Exception $e) { + // @todo: distinguish the 'broken notice/profile' case from more general + // transitory errors. + common_log(LOG_ERR, "Exception getting notice profile: " . $e->getMessage()); + return true; + } + $tags = ping_notice_tags($notice); - # Array of servers, URL => type - $notify = common_config('ping', 'notify'); - $profile = $notice->getProfile(); - $tags = ping_notice_tags($notice); + foreach ($notify as $notify_url => $type) { + switch ($type) { + case 'xmlrpc': + case 'extended': + $req = xmlrpc_encode_request('weblogUpdates.ping', + array($profile->nickname, # site name + common_local_url('showstream', + array('nickname' => $profile->nickname)), + common_local_url('shownotice', + array('notice' => $notice->id)), + common_local_url('userrss', + array('nickname' => $profile->nickname)), + $tags)); - foreach ($notify as $notify_url => $type) { - switch ($type) { - case 'xmlrpc': - case 'extended': - $req = xmlrpc_encode_request('weblogUpdates.ping', - array($profile->nickname, # site name - common_local_url('showstream', - array('nickname' => $profile->nickname)), - common_local_url('shownotice', - array('notice' => $notice->id)), - common_local_url('userrss', - array('nickname' => $profile->nickname)), - $tags)); + $request = HTTPClient::start(); + $request->setConfig('connect_timeout', common_config('ping', 'timeout')); + $request->setConfig('timeout', common_config('ping', 'timeout')); + try { + $httpResponse = $request->post($notify_url, array('Content-Type: text/xml'), $req); + } catch (Exception $e) { + common_log(LOG_ERR, + "Exception pinging $notify_url: " . $e->getMessage()); + continue; + } + + if (!$httpResponse || mb_strlen($httpResponse->getBody()) == 0) { + common_log(LOG_WARNING, + "XML-RPC empty results for ping ($notify_url, $notice->id) "); + continue; + } + + $response = xmlrpc_decode($httpResponse->getBody()); - $context = stream_context_create(array('http' => array('method' => "POST", - 'header' => - "Content-Type: text/xml\r\n". - "User-Agent: Laconica/".LACONICA_VERSION."\r\n", - 'content' => $req))); - $file = file_get_contents($notify_url, false, $context); - $response = xmlrpc_decode($file); - if (xmlrpc_is_fault($response)) { + if (is_array($response) && xmlrpc_is_fault($response)) { common_log(LOG_WARNING, "XML-RPC error for ping ($notify_url, $notice->id) ". "$response[faultString] ($response[faultCode])"); @@ -60,9 +78,8 @@ function ping_broadcast_notice($notice) { "Ping success for $notify_url $notice->id"); } break; - - case 'get': - case 'post': + case 'get': + case 'post': $args = array('name' => $profile->nickname, 'url' => common_local_url('showstream', array('nickname' => $profile->nickname)), @@ -73,11 +90,11 @@ function ping_broadcast_notice($notice) { if ($type === 'get') { $result = $fetcher->get($notify_url . '?' . http_build_query($args), - array('User-Agent: Laconica/'.LACONICA_VERSION)); + array('User-Agent: StatusNet/'.STATUSNET_VERSION)); } else { $result = $fetcher->post($notify_url, http_build_query($args), - array('User-Agent: Laconica/'.LACONICA_VERSION)); + array('User-Agent: StatusNet/'.STATUSNET_VERSION)); } if ($result->status != '200') { common_log(LOG_WARNING, @@ -89,26 +106,25 @@ function ping_broadcast_notice($notice) { "'$result->body'"); } break; - - default: - common_log(LOG_WARNING, 'Unknown notify type for ' . $notify_url . ': ' . $type); + default: + common_log(LOG_WARNING, 'Unknown notify type for ' . $notify_url . ': ' . $type); } - } + } return true; } function ping_notice_tags($notice) { - $tag = new Notice_tag(); - $tag->notice_id = $notice->id; - $tags = array(); - if ($tag->find()) { - while ($tag->fetch()) { - $tags[] = $tag->tag; - } - $tag->free(); - unset($tag); - return implode('|', $tags); - } - return NULL; -} \ No newline at end of file + $tag = new Notice_tag(); + $tag->notice_id = $notice->id; + $tags = array(); + if ($tag->find()) { + while ($tag->fetch()) { + $tags[] = $tag->tag; + } + $tag->free(); + unset($tag); + return implode('|', $tags); + } + return NULL; +}