Пример #1
0
/**
 * Maybe enable pretty permalinks on install.
 *
 * If after enabling pretty permalinks don't work, fallback to query-string permalinks.
 *
 * @since 0.0.1
 *
 * @global HQ_Rewrite hq_rewrite HiveQueen rewrite component.
 *
 * @return bool Whether pretty permalinks are enabled. False otherwise.
 */
function hq_install_maybe_enable_pretty_permalinks()
{
    global $hq_rewrite;
    // Bail if a permalink structure is already enabled.
    if (get_option('permalink_structure')) {
        return true;
    }
    /*
     * The Permalink structures to attempt.
     *
     * The first is designed for mod_rewrite or nginx rewriting.
     *
     * The second is PATHINFO-based permalinks for web server configurations
     * without a true rewrite module enabled.
     */
    $permalink_structures = array('/%year%/%monthnum%/%day%/%postname%/', '/index.php/%year%/%monthnum%/%day%/%postname%/');
    foreach ((array) $permalink_structures as $permalink_structure) {
        $hq_rewrite->set_permalink_structure($permalink_structure);
        /*
         * Flush rules with the hard option to force refresh of the web-server's
         * rewrite config file (e.g. .htaccess or web.config).
         */
        $hq_rewrite->flush_rules(true);
        // Test against a real HiveQueen Post, or if none were created, a random 404 page.
        $test_url = get_permalink(1);
        if (!$test_url) {
            $test_url = home_url('/hivequeen-check-for-rewrites/');
        }
        /*
         * Send a request to the site, and check whether
         * the 'x-pingback' header is returned as expected.
         *
         * Uses hq_remote_get() instead of hq_remote_head() because web servers
         * can block head requests.
         */
        $response = hq_remote_get($test_url, array('timeout' => 5));
        $x_pingback_header = hq_remote_retrieve_header($response, 'x-pingback');
        $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo('pingback_url');
        if ($pretty_permalinks) {
            return true;
        }
    }
    /*
     * If it makes it this far, pretty permalinks failed.
     * Fallback to query-string permalinks.
     */
    $hq_rewrite->set_permalink_structure('');
    $hq_rewrite->flush_rules(true);
    return false;
}
Пример #2
0
/**
 * Finds a pingback server URI based on the given URL.
 *
 * Checks the HTML for the rel="pingback" link and x-pingback headers. It does
 * a check for the x-pingback headers first and returns that, if available. The
 * check for the rel="pingback" has more overhead than just the header.
 *
 * @since 0.0.1
 *
 * @param string $url URL to ping.
 * @param int $deprecated Not Used.
 * @return false|string False on failure, string containing URI on success.
 */
function discover_pingback_server_uri($url, $deprecated = '')
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.7');
    }
    $pingback_str_dquote = 'rel="pingback"';
    $pingback_str_squote = 'rel=\'pingback\'';
    /** @todo Should use Filter Extension or custom preg_match instead. */
    $parsed_url = parse_url($url);
    if (!isset($parsed_url['host'])) {
        // Not an URL. This should never happen.
        return false;
    }
    //Do not search for a pingback server on our own uploads
    $uploads_dir = hq_upload_dir();
    if (0 === strpos($url, $uploads_dir['baseurl'])) {
        return false;
    }
    $response = hq_safe_remote_head($url, array('timeout' => 2, 'httpversion' => '1.0'));
    if (is_hq_error($response)) {
        return false;
    }
    if (hq_remote_retrieve_header($response, 'x-pingback')) {
        return hq_remote_retrieve_header($response, 'x-pingback');
    }
    // Not an (x)html, sgml, or xml page, no use going further.
    if (preg_match('#(image|audio|video|model)/#is', hq_remote_retrieve_header($response, 'content-type'))) {
        return false;
    }
    // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file)
    $response = hq_safe_remote_get($url, array('timeout' => 2, 'httpversion' => '1.0'));
    if (is_hq_error($response)) {
        return false;
    }
    $contents = hq_remote_retrieve_body($response);
    $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
    $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
    if ($pingback_link_offset_dquote || $pingback_link_offset_squote) {
        $quote = $pingback_link_offset_dquote ? '"' : '\'';
        $pingback_link_offset = $quote == '"' ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
        $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
        $pingback_href_start = $pingback_href_pos + 6;
        $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
        $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
        $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
        // We may find rel="pingback" but an incomplete pingback URL
        if ($pingback_server_url_len > 0) {
            // We got it!
            return $pingback_server_url;
        }
    }
    return false;
}
Пример #3
0
/**
 * Downloads a url to a local temporary file using the HiveQueen HTTP Class.
 * Please note, That the calling function must unlink() the file.
 *
 * @since 0.0.1
 *
 * @param string $url the URL of the file to download
 * @param int $timeout The timeout for the request to download the file default 300 seconds
 * @return mixed HQ_Error on failure, string Filename on success.
 */
function download_url($url, $timeout = 300)
{
    //WARNING: The file is not automatically deleted, The script must unlink() the file.
    if (!$url) {
        return new HQ_Error('http_no_url', __('Invalid URL Provided.'));
    }
    $tmpfname = hq_tempnam($url);
    if (!$tmpfname) {
        return new HQ_Error('http_no_file', __('Could not create Temporary file.'));
    }
    $response = hq_safe_remote_get($url, array('timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname));
    if (is_hq_error($response)) {
        unlink($tmpfname);
        return $response;
    }
    if (200 != hq_remote_retrieve_response_code($response)) {
        unlink($tmpfname);
        return new HQ_Error('http_404', trim(hq_remote_retrieve_response_message($response)));
    }
    $content_md5 = hq_remote_retrieve_header($response, 'content-md5');
    if ($content_md5) {
        $md5_check = verify_file_md5($tmpfname, $content_md5);
        if (is_hq_error($md5_check)) {
            unlink($tmpfname);
            return $md5_check;
        }
    }
    return $tmpfname;
}