function rsscloud_send_post_notifications($rss2_url = false)
{
    if ($rss2_url === false) {
        $rss2_url = get_bloginfo('rss2_url');
        if (defined('RSSCLOUD_FEED_URL')) {
            $rss2_url = RSSCLOUD_FEED_URL;
        }
    }
    do_action('rsscloud_feed_notifications', $rss2_url);
    $notify = rsscloud_get_hub_notifications();
    if (!is_array($notify)) {
        $notify = array();
    }
    $need_update = false;
    foreach ($notify[$rss2_url] as $notify_url => $n) {
        if ($n['status'] != 'active') {
            continue;
        }
        if ($n['protocol'] == 'http-post') {
            $url = parse_url($notify_url);
            $port = 80;
            if (!empty($url['port'])) {
                $port = $url['port'];
            }
            $result = wp_remote_post($notify_url, array('method' => 'POST', 'timeout' => RSSCLOUD_HTTP_TIMEOUT, 'user-agent' => RSSCLOUD_USER_AGENT, 'port' => $port, 'body' => array('url' => $rss2_url)));
            do_action('rsscloud_send_notification');
            if (!is_wp_error($result)) {
                $status_code = (int) $result['response']['code'];
            }
            if (is_wp_error($result) || ($status_code < 200 || $status_code > 299)) {
                do_action('rsscloud_notify_failure');
                $notify[$rss2_url][$notify_url]['failure_count']++;
                if ($notify[$rss2_url][$notify_url]['failure_count'] > RSSCLOUD_MAX_FAILURES) {
                    do_action('rsscloud_suspend_notification_url');
                    $notify[$rss2_url][$notify_url]['status'] = 'suspended';
                }
                $need_update = true;
            } elseif ($notify[$rss2_url][$notify_url]['failure_count'] > 0) {
                do_action('rsscloud_reset_failure_count');
                $notify[$rss2_url][$notify_url]['failure_count'] = 0;
                $need_update = true;
            }
        }
    }
    // foreach
    if ($need_update) {
        rsscloud_update_hub_notifications($notify);
    }
}
Example #2
0
function rsscloud_hub_process_notification_request()
{
    // Get the current set of notifications
    $notify = rsscloud_get_hub_notifications();
    if (empty($notify)) {
        $notify = array();
    }
    // Must provide at least one URL to get notifications about
    if (empty($_POST['url1'])) {
        rsscloud_notify_result('false', 'No feed for url1.');
    }
    // Only support http-post
    $protocol = 'http-post';
    if (!empty($_POST['protocol']) && strtolower($_POST['protocol']) !== 'http-post') {
        do_action('rsscloud_protocol_not_post');
        rsscloud_notify_result('false', 'Only http-post notifications are supported at this time.');
    }
    // Assume port 80
    $port = 80;
    if (!empty($_POST['port'])) {
        $port = (int) $_POST['port'];
    }
    // Path is required
    if (empty($_POST['path'])) {
        rsscloud_notify_result('false', 'No path provided.');
    }
    $path = str_replace('@', '', $_POST['path']);
    if ($path[0] != '/') {
        $path = '/' . $path;
    }
    // Figure out what the blog and notification URLs are
    $rss2_url = get_bloginfo('rss2_url');
    if (defined('RSSCLOUD_FEED_URL')) {
        $rss2_url = RSSCLOUD_FEED_URL;
    }
    $notify_url = $_SERVER['REMOTE_ADDR'] . ':' . $port . $path;
    if (!empty($_POST['domain'])) {
        $domain = str_replace('@', '', $_POST['domain']);
        $notify_url = $domain . ':' . $port . $path;
        $challenge = rsscloud_generate_challenge();
        $result = wp_remote_get($notify_url . '?url=' . esc_url($_POST['url1']) . '&challenge=' . $challenge, array('method' => 'GET', 'timeout' => RSSCLOUD_HTTP_TIMEOUT, 'user-agent' => RSSCLOUD_USER_AGENT, 'port' => $port));
    } else {
        $result = wp_remote_post($notify_url, array('method' => 'POST', 'timeout' => RSSCLOUD_HTTP_TIMEOUT, 'user-agent' => RSSCLOUD_USER_AGENT, 'port' => $port, 'body' => array('url' => $_POST['url1'])));
    }
    if (isset($result->errors['http_request_failed'][0])) {
        rsscloud_notify_result('false', 'Error testing notification URL : ' . $result->errors['http_request_failed'][0]);
    }
    $status_code = (int) $result['response']['code'];
    if ($status_code < 200 || $status_code > 299) {
        rsscloud_notify_result('false', 'Error testing notification URL.  The URL returned HTTP status code: ' . $result['response']['code'] . ' - ' . $result['response']['message'] . '.');
    }
    // challenge must match for domain requests
    if (!empty($_POST['domain'])) {
        if (empty($result['body']) || $result['body'] != $challenge) {
            rsscloud_notify_result('false', 'The response body did not match the challenge string');
        }
    }
    // Passed all the tests, add this to the list of notifications for
    foreach ($_POST as $key => $feed_url) {
        if (!preg_match('|url\\d+|', $key)) {
            continue;
        }
        // Only allow requests for the RSS2 posts feed
        if ($feed_url != $rss2_url) {
            rsscloud_notify_result('false', "You can only request updates for {$rss2_url}");
        }
        $notify[$feed_url][$notify_url]['protocol'] = $protocol;
        $notify[$feed_url][$notify_url]['status'] = 'active';
        $notify[$feed_url][$notify_url]['failure_count'] = 0;
    }
    do_action('rsscloud_add_notify_subscription');
    rsscloud_update_hub_notifications($notify);
    rsscloud_notify_result('true', 'Registration successful.');
}