示例#1
0
function RedirectToCachedFile()
{
    global $phpThumb, $PHPTHUMB_CONFIG;
    $nice_cachefile = str_replace(DIRECTORY_SEPARATOR, '/', $phpThumb->cache_filename);
    $nice_docroot = str_replace(DIRECTORY_SEPARATOR, '/', rtrim($PHPTHUMB_CONFIG['document_root'], '/\\'));
    $parsed_url = phpThumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']);
    $nModified = filemtime($phpThumb->cache_filename);
    if ($phpThumb->config_nooffsitelink_enabled && @$_SERVER['HTTP_REFERER'] && !in_array(@$parsed_url['host'], $phpThumb->config_nooffsitelink_valid_domains)) {
        $phpThumb->DebugMessage('Would have used cached (image/' . $phpThumb->thumbnailFormat . ') file "' . $phpThumb->cache_filename . '" (Last-Modified: ' . gmdate('D, d M Y H:i:s', $nModified) . ' GMT), but skipping because $_SERVER[HTTP_REFERER] (' . @$_SERVER['HTTP_REFERER'] . ') is not in $phpThumb->config_nooffsitelink_valid_domains (' . implode(';', $phpThumb->config_nooffsitelink_valid_domains) . ')', __FILE__, __LINE__);
    } elseif ($phpThumb->phpThumbDebug) {
        $phpThumb->DebugTimingMessage('skipped using cached image', __FILE__, __LINE__);
        $phpThumb->DebugMessage('Would have used cached file, but skipping due to phpThumbDebug', __FILE__, __LINE__);
        $phpThumb->DebugMessage('* Would have sent headers (1): Last-Modified: ' . gmdate('D, d M Y H:i:s', $nModified) . ' GMT', __FILE__, __LINE__);
        if ($getimagesize = @GetImageSize($phpThumb->cache_filename)) {
            $phpThumb->DebugMessage('* Would have sent headers (2): Content-Type: ' . phpThumb_functions::ImageTypeToMIMEtype($getimagesize[2]), __FILE__, __LINE__);
        }
        if (preg_match('#^' . preg_quote($nice_docroot) . '(.*)$#', $nice_cachefile, $matches)) {
            $phpThumb->DebugMessage('* Would have sent headers (3): Location: ' . dirname($matches[1]) . '/' . urlencode(basename($matches[1])), __FILE__, __LINE__);
        } else {
            $phpThumb->DebugMessage('* Would have sent data: readfile(' . $phpThumb->cache_filename . ')', __FILE__, __LINE__);
        }
    } else {
        if (headers_sent()) {
            $phpThumb->ErrorImage('Headers already sent (' . basename(__FILE__) . ' line ' . __LINE__ . ')');
            exit;
        }
        SendSaveAsFileHeaderIfNeeded();
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $nModified) . ' GMT');
        if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] && $nModified == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) && @$_SERVER['SERVER_PROTOCOL']) {
            header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
            exit;
        }
        if ($getimagesize = @GetImageSize($phpThumb->cache_filename)) {
            header('Content-Type: ' . phpThumb_functions::ImageTypeToMIMEtype($getimagesize[2]));
        } elseif (preg_match('#\\.ico$#i', $phpThumb->cache_filename)) {
            header('Content-Type: image/x-icon');
        }
        if (!@$PHPTHUMB_CONFIG['cache_force_passthru'] && preg_match('#^' . preg_quote($nice_docroot) . '(.*)$#', $nice_cachefile, $matches)) {
            header('Location: ' . dirname($matches[1]) . '/' . urlencode(basename($matches[1])));
        } else {
            @readfile($phpThumb->cache_filename);
        }
        exit;
    }
    return true;
}
 static function SafeURLread($url, &$error, $timeout = 10, $followredirects = true)
 {
     $error = '';
     $parsed_url = phpThumb_functions::ParseURLbetter($url);
     $alreadyLookedAtURLs[trim($url)] = true;
     while (true) {
         $tryagain = false;
         $rawData = phpThumb_functions::URLreadFsock(@$parsed_url['host'], @$parsed_url['path'] . '?' . @$parsed_url['query'], $errstr, true, @$parsed_url['port'] ? @$parsed_url['port'] : 80, $timeout);
         if (preg_match('#302 [a-z ]+; Location\\: (http.*)#i', $errstr, $matches)) {
             $matches[1] = trim(@$matches[1]);
             if (!@$alreadyLookedAtURLs[$matches[1]]) {
                 // loop through and examine new URL
                 $error .= 'URL "' . $url . '" redirected to "' . $matches[1] . '"';
                 $tryagain = true;
                 $alreadyLookedAtURLs[$matches[1]] = true;
                 $parsed_url = phpThumb_functions::ParseURLbetter($matches[1]);
             }
         }
         if (!$tryagain) {
             break;
         }
     }
     if ($rawData === false) {
         $error .= 'Error opening "' . $url . '":' . "\n\n" . $errstr;
         return false;
     } elseif ($rawData === null) {
         // fall through
         $error .= 'Error opening "' . $url . '":' . "\n\n" . $errstr;
     } else {
         return $rawData;
     }
     if (function_exists('curl_version') && !phpThumb_functions::FunctionIsDisabled('curl_exec')) {
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_HEADER, false);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
         curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
         $rawData = curl_exec($ch);
         curl_close($ch);
         if (strlen($rawData) > 0) {
             $error .= 'CURL succeeded (' . strlen($rawData) . ' bytes); ';
             return $rawData;
         }
         $error .= 'CURL available but returned no data; ';
     } else {
         $error .= 'CURL unavailable; ';
     }
     $BrokenURLfopenPHPversions = array('4.4.2');
     if (in_array(phpversion(), $BrokenURLfopenPHPversions)) {
         $error .= 'fopen(URL) broken in PHP v' . phpversion() . '; ';
     } elseif (@ini_get('allow_url_fopen')) {
         $rawData = '';
         $error_fopen = '';
         ob_start();
         if ($fp = fopen($url, 'rb')) {
             do {
                 $buffer = fread($fp, 8192);
                 $rawData .= $buffer;
             } while (strlen($buffer) > 0);
             fclose($fp);
         } else {
             $error_fopen .= trim(strip_tags(ob_get_contents()));
         }
         ob_end_clean();
         $error .= $error_fopen;
         if (!$error_fopen) {
             $error .= '; "allow_url_fopen" succeeded (' . strlen($rawData) . ' bytes); ';
             return $rawData;
         }
         $error .= '; "allow_url_fopen" enabled but returned no data (' . $error_fopen . '); ';
     } else {
         $error .= '"allow_url_fopen" disabled; ';
     }
     return false;
 }