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; }
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 (eregi('302 [a-z ]+; Location\\: (http.*)', $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; }
function SetCacheFilename() { if (!is_null($this->cache_filename)) { $this->DebugMessage('$this->cache_filename already set, skipping SetCacheFilename()', __FILE__, __LINE__); return true; } $this->setOutputFormat(); $this->setCacheDirectory(); if (!$this->config_cache_directory) { $this->DebugMessage('SetCacheFilename() failed because $this->config_cache_directory is empty', __FILE__, __LINE__); return false; } if (!$this->sourceFilename && !$this->rawImageData && $this->src) { $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src); } if ($this->config_cache_default_only_suffix && $this->sourceFilename) { // simplified cache filenames: // only use default parameters in phpThumb.config.php // substitute source filename into * in $this->config_cache_default_only_suffix // (eg: '*_thumb' becomes 'picture_thumb.jpg') if (strpos($this->config_cache_default_only_suffix, '*') === false) { $this->DebugMessage('aborting simplified caching filename because no * in "' . $this->config_cache_default_only_suffix . '"', __FILE__, __LINE__); } else { preg_match('#(.+)(\\.[a-z0-9]+)?$#i', basename($this->sourceFilename), $matches); $this->cache_filename = $this->config_cache_directory . DIRECTORY_SEPARATOR . rawurlencode(str_replace('*', @$matches[1], $this->config_cache_default_only_suffix)) . '.' . strtolower($this->thumbnailFormat); return true; } } $this->cache_filename = ''; $broad_directory_name = ''; if ($this->new) { $broad_directory_name = strtolower(md5($this->new)); $this->cache_filename .= '_new' . $broad_directory_name; } elseif ($this->md5s) { // source image MD5 hash provided $this->DebugMessage('SetCacheFilename() _raw set from $this->md5s = "' . $this->md5s . '"', __FILE__, __LINE__); $broad_directory_name = $this->md5s; $this->cache_filename .= '_raw' . $this->md5s; } elseif (!$this->src && $this->rawImageData) { $this->DebugMessage('SetCacheFilename() _raw set from md5($this->rawImageData) = "' . md5($this->rawImageData) . '"', __FILE__, __LINE__); $broad_directory_name = strtolower(md5($this->rawImageData)); $this->cache_filename .= '_raw' . $broad_directory_name; } else { $this->DebugMessage('SetCacheFilename() _src set from md5($this->sourceFilename) "' . $this->sourceFilename . '" = "' . md5($this->sourceFilename) . '"', __FILE__, __LINE__); $broad_directory_name = strtolower(md5($this->sourceFilename)); $this->cache_filename .= '_src' . $broad_directory_name; } if (@$_SERVER['HTTP_REFERER'] && $this->config_nooffsitelink_enabled) { $parsed_url1 = @phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']); $parsed_url2 = @phpthumb_functions::ParseURLbetter('http://' . @$_SERVER['HTTP_HOST']); if (@$parsed_url1['host'] && @$parsed_url2['host'] && $parsed_url1['host'] != $parsed_url2['host']) { // include "_offsite" only if nooffsitelink_enabled and if referrer doesn't match the domain of the current server $this->cache_filename .= '_offsite'; } } $ParametersString = ''; if ($this->fltr && is_array($this->fltr)) { $ParametersString .= '_fltr' . implode('_fltr', $this->fltr); } $FilenameParameters1 = array('ar', 'bg', 'bc', 'far', 'sx', 'sy', 'sw', 'sh', 'zc'); foreach ($FilenameParameters1 as $key) { if ($this->{$key}) { $ParametersString .= '_' . $key . $this->{$key}; } } $FilenameParameters2 = array('h', 'w', 'wl', 'wp', 'ws', 'hp', 'hs', 'xto', 'ra', 'iar', 'aoe', 'maxb', 'sfn', 'dpi'); foreach ($FilenameParameters2 as $key) { if ($this->{$key}) { $ParametersString .= '_' . $key . intval($this->{$key}); } } if ($this->thumbnailFormat == 'jpeg') { // only JPEG output has variable quality option $ParametersString .= '_q' . intval($this->thumbnailQuality); } $this->DebugMessage('SetCacheFilename() _par set from md5(' . $ParametersString . ')', __FILE__, __LINE__); $this->cache_filename .= '_par' . strtolower(md5($ParametersString)); if ($this->md5s) { // source image MD5 hash provided // do not source image modification date -- // cached image will be used even if file was modified or removed } elseif (!$this->config_cache_source_filemtime_ignore_remote && preg_match('#^(f|ht)tps?\\://#i', $this->src)) { $this->cache_filename .= '_dat' . intval(phpthumb_functions::filedate_remote($this->src)); } elseif (!$this->config_cache_source_filemtime_ignore_local && $this->src && !$this->rawImageData) { $this->cache_filename .= '_dat' . intval(@filemtime($this->sourceFilename)); } $this->cache_filename .= '.' . strtolower($this->thumbnailFormat); $broad_directories = ''; for ($i = 0; $i < $this->config_cache_directory_depth; $i++) { $broad_directories .= DIRECTORY_SEPARATOR . substr($broad_directory_name, 0, $i + 1); } $this->cache_filename = $this->config_cache_directory . $broad_directories . DIRECTORY_SEPARATOR . $this->config_cache_prefix . rawurlencode($this->cache_filename); return true; }