예제 #1
0
function get_avatar_url_size($url)
{
    global $sourcedir;
    require_once $sourcedir . '/Class-CurlFetchWeb.php';
    $fetch_data = new curl_fetch_web_data(array(CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5, CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246', CURLOPT_RANGE => '0-16383'));
    $fetch_data->get_url_data($url);
    if (in_array($fetch_data->result('code'), array(200, 206)) && !$fetch_data->result('error')) {
        $data = $fetch_data->result('body');
        return get_image_size_from_string($data);
    } else {
        return false;
    }
}
예제 #2
0
/**
 * Get the contents of a URL, irrespective of allow_url_fopen.
 *
 * - reads the contents of an http or ftp address and retruns the page in a string
 * - will accept up to 3 page redirections (redirectio_level in the function call is private)
 * - if post_data is supplied, the value and lenght is posted to the given url as form data
 * - URL must be supplied in lowercase
 *
 * @param string $url
 * @param string $post_data = ''
 * @param bool $keep_alive = false
 * @param $redirection_level = 0
 * @return string
 */
function fetch_web_data($url, $post_data = '', $keep_alive = false, $redirection_level = 0)
{
    global $webmaster_email, $sourcedir;
    static $keep_alive_dom = null, $keep_alive_fp = null;
    preg_match('~^(http|ftp)(s)?://([^/:]+)(:(\\d+))?(.+)$~', $url, $match);
    // An FTP url. We should try connecting and RETRieving it...
    if (empty($match[1])) {
        return false;
    } elseif ($match[1] == 'ftp') {
        // Include the file containing the ftp_connection class.
        require_once $sourcedir . '/Class-Package.php';
        // Establish a connection and attempt to enable passive mode.
        $ftp = new ftp_connection(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? 21 : $match[5], 'anonymous', $webmaster_email);
        if ($ftp->error !== false || !$ftp->passive()) {
            return false;
        }
        // I want that one *points*!
        fwrite($ftp->connection, 'RETR ' . $match[6] . "\r\n");
        // Since passive mode worked (or we would have returned already!) open the connection.
        $fp = @fsockopen($ftp->pasv['ip'], $ftp->pasv['port'], $err, $err, 5);
        if (!$fp) {
            return false;
        }
        // The server should now say something in acknowledgement.
        $ftp->check_response(150);
        $data = '';
        while (!feof($fp)) {
            $data .= fread($fp, 4096);
        }
        fclose($fp);
        // All done, right?  Good.
        $ftp->check_response(226);
        $ftp->close();
    } elseif (isset($match[1]) && $match[1] === 'http' && function_exists('curl_init')) {
        // Include the file containing the curl_fetch_web_data class.
        require_once $sourcedir . '/Class-CurlFetchWeb.php';
        $fetch_data = new curl_fetch_web_data();
        $fetch_data->get_url_data($url, $post_data);
        // no errors and a 200 result, then we have a good dataset, well we at least have data ;)
        if ($fetch_data->result('code') == 200 && !$fetch_data->result('error')) {
            $data = $fetch_data->result('body');
        } else {
            return false;
        }
    } elseif (isset($match[1]) && $match[1] == 'http') {
        if ($keep_alive && $match[3] == $keep_alive_dom) {
            $fp = $keep_alive_fp;
        }
        if (empty($fp)) {
            // Open the socket on the port we want...
            $fp = @fsockopen(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? $match[2] ? 443 : 80 : $match[5], $err, $err, 5);
            if (!$fp) {
                return false;
            }
        }
        if ($keep_alive) {
            $keep_alive_dom = $match[3];
            $keep_alive_fp = $fp;
        }
        // I want this, from there, and I'm not going to be bothering you for more (probably.)
        if (empty($post_data)) {
            fwrite($fp, 'GET ' . ($match[6] !== '/' ? str_replace(' ', '%20', $match[6]) : '') . ' HTTP/1.0' . "\r\n");
            fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? $match[2] ? ':443' : '' : ':' . $match[5]) . "\r\n");
            fwrite($fp, 'User-Agent: PHP/SMF' . "\r\n");
            if ($keep_alive) {
                fwrite($fp, 'Connection: Keep-Alive' . "\r\n\r\n");
            } else {
                fwrite($fp, 'Connection: close' . "\r\n\r\n");
            }
        } else {
            fwrite($fp, 'POST ' . ($match[6] !== '/' ? $match[6] : '') . ' HTTP/1.0' . "\r\n");
            fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? $match[2] ? ':443' : '' : ':' . $match[5]) . "\r\n");
            fwrite($fp, 'User-Agent: PHP/SMF' . "\r\n");
            if ($keep_alive) {
                fwrite($fp, 'Connection: Keep-Alive' . "\r\n");
            } else {
                fwrite($fp, 'Connection: close' . "\r\n");
            }
            fwrite($fp, 'Content-Type: application/x-www-form-urlencoded' . "\r\n");
            fwrite($fp, 'Content-Length: ' . strlen($post_data) . "\r\n\r\n");
            fwrite($fp, $post_data);
        }
        $response = fgets($fp, 768);
        // Redirect in case this location is permanently or temporarily moved.
        if ($redirection_level < 3 && preg_match('~^HTTP/\\S+\\s+30[127]~i', $response) === 1) {
            $header = '';
            $location = '';
            while (!feof($fp) && trim($header = fgets($fp, 4096)) != '') {
                if (strpos($header, 'Location:') !== false) {
                    $location = trim(substr($header, strpos($header, ':') + 1));
                }
            }
            if (empty($location)) {
                return false;
            } else {
                if (!$keep_alive) {
                    fclose($fp);
                }
                return fetch_web_data($location, $post_data, $keep_alive, $redirection_level + 1);
            }
        } elseif (preg_match('~^HTTP/\\S+\\s+20[01]~i', $response) === 0) {
            return false;
        }
        // Skip the headers...
        while (!feof($fp) && trim($header = fgets($fp, 4096)) != '') {
            if (preg_match('~content-length:\\s*(\\d+)~i', $header, $match) != 0) {
                $content_length = $match[1];
            } elseif (preg_match('~connection:\\s*close~i', $header) != 0) {
                $keep_alive_dom = null;
                $keep_alive = false;
            }
            continue;
        }
        $data = '';
        if (isset($content_length)) {
            while (!feof($fp) && strlen($data) < $content_length) {
                $data .= fread($fp, $content_length - strlen($data));
            }
        } else {
            while (!feof($fp)) {
                $data .= fread($fp, 4096);
            }
        }
        if (!$keep_alive) {
            fclose($fp);
        }
    } else {
        // Umm, this shouldn't happen?
        trigger_error('fetch_web_data(): Bad URL', E_USER_NOTICE);
        $data = false;
    }
    return $data;
}