Sigue acá el código:
Código PHP:
if (count($files) > CACHE_SIZE) {
foreach ($files as $file) {
$i ++;
if ($i >= CACHE_CLEAR) {
return;
}
if (@filemtime($file) > $yesterday) {
return;
}
if (file_exists($file)) {
unlink($file);
}
}
}
}
}
/**
* compare the file time of two files
*/
function filemtime_compare($a, $b) {
return filemtime($a) - filemtime($b);
}
/**
* determine the file mime type
*/
function mime_type($file) {
if (stristr(PHP_OS, 'WIN')) {
$os = 'WIN';
} else {
$os = PHP_OS;
}
$mime_type = '';
if (function_exists('mime_content_type') && $os != 'WIN') {
$mime_type = mime_content_type($file);
}
// use PECL fileinfo to determine mime type
if (!valid_src_mime_type($mime_type)) {
if (function_exists('finfo_open')) {
$finfo = @finfo_open(FILEINFO_MIME);
if ($finfo != '') {
$mime_type = finfo_file($finfo, $file);
finfo_close($finfo);
}
}
}
// try to determine mime type by using unix file command
// this should not be executed on windows
if (!valid_src_mime_type($mime_type) && $os != "WIN") {
if (preg_match("/FreeBSD|FREEBSD|LINUX/", $os)) {
$mime_type = trim(@shell_exec('file -bi ' . escapeshellarg($file)));
}
}
// use file's extension to determine mime type
if (!valid_src_mime_type($mime_type)) {
// set defaults
$mime_type = 'image/png';
// file details
$fileDetails = pathinfo($file);
$ext = strtolower($fileDetails["extension"]);
// mime types
$types = array(
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif'
);
if (strlen($ext) && strlen($types[$ext])) {
$mime_type = $types[$ext];
}
}
return $mime_type;
}
/**
*
*/
function valid_src_mime_type($mime_type) {
if (preg_match("/jpg|jpeg|gif|png/i", $mime_type)) {
return true;
}
return false;
}
/**
*
*/
function check_cache ($mime_type) {
// make sure cache dir exists
if (!file_exists(DIRECTORY_CACHE)) {
// give 777 permissions so that developer can overwrite
// files created by web server user
mkdir(DIRECTORY_CACHE);
chmod(DIRECTORY_CACHE, 0777);
}
show_cache_file ($mime_type);
}
/**
*
*/
function show_cache_file ($mime_type) {
$cache_file = DIRECTORY_CACHE . '/' . get_cache_file();
if (file_exists($cache_file)) {
$gmdate_mod = gmdate("D, d M Y H:i:s", filemtime($cache_file));
if(! strstr($gmdate_mod, "GMT")) {
$gmdate_mod .= " GMT";
}
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
// check for updates
$if_modified_since = preg_replace ("/;.*$/", "", $_SERVER["HTTP_IF_MODIFIED_SINCE"]);
if ($if_modified_since == $gmdate_mod) {
header("HTTP/1.1 304 Not Modified");
die();
}
}
clearstatcache();
$fileSize = filesize ($cache_file);
// send headers then display image
header ('Content-Type: ' . $mime_type);
header ('Accept-Ranges: bytes');
header ('Last-Modified: ' . $gmdate_mod);
header ('Content-Length: ' . $fileSize);
header ('Cache-Control: max-age=9999, must-revalidate');
header ('Expires: ' . $gmdate_mod);
readfile ($cache_file);
die();
}
}
/**
*
*/
function get_cache_file() {
global $lastModified;
static $cache_file;
if (!$cache_file) {
$cachename = $_SERVER['QUERY_STRING'] . VERSION . $lastModified;
$cache_file = md5($cachename) . '.png';
}
return $cache_file;
}
/**
* check to if the url is valid or not
*/
function valid_extension ($ext) {
if (preg_match("/jpg|jpeg|png|gif/i", $ext)) {
return TRUE;
} else {
return FALSE;
}
}
/**
*
*/
function checkExternal ($src) {
$allowedSites = array(
'flickr.com',
'picasa.com',
'blogger.com',
'wordpress.com',
'img.youtube.com',
);
if (preg_match('/http:\/\//', $src) == true) {
$url_info = parse_url ($src);
$isAllowedSite = false;
foreach ($allowedSites as $site) {
$site = '/' . addslashes($site) . '/';
if (preg_match($site, $url_info['host']) == true) {
$isAllowedSite = true;
}
}
if ($isAllowedSite) {
$fileDetails = pathinfo($src);
$ext = strtolower($fileDetails['extension']);
$filename = md5($src);
$local_filepath = DIRECTORY_TEMP . '/' . $filename . '.' . $ext;
if (!file_exists($local_filepath)) {
if (function_exists('curl_init')) {
$fh = fopen($local_filepath, 'w');
$ch = curl_init($src);
curl_setopt($ch, CURLOPT_URL, $src);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
curl_setopt($ch, CURLOPT_FILE, $fh);
if (curl_exec($ch) === FALSE) {
if (file_exists($local_filepath)) {
unlink($local_filepath);
}
displayError('error reading file ' . $src . ' from remote host: ' . curl_error($ch));
}
curl_close($ch);
fclose($fh);
} else {
if (!$img = file_get_contents($src)) {
displayError('remote file for ' . $src . ' can not be accessed. It is likely that the file permissions are restricted');
}
if (file_put_contents($local_filepath, $img) == FALSE) {
displayError('error writing temporary file');
}
}
if (!file_exists($local_filepath)) {
displayError('local file for ' . $src . ' can not be created');
}
}
$src = $local_filepath;
} else {
displayError('remote host "' . $url_info['host'] . '" not allowed');
}
}
return $src;
}
/**
* tidy up the image source url
*/
function cleanSource($src) {
$host = str_replace('www.', '', $_SERVER['HTTP_HOST']);
$regex = "/^((ht|f)tp(s|):\/\/)(www\.|)" . $host . "/i";
$src = preg_replace ($regex, '', $src);
$src = strip_tags ($src);
$src = checkExternal ($src);
// remove slash from start of string
if (strpos($src, '/') === 0) {
$src = substr ($src, -(strlen($src) - 1));
}
// don't allow users the ability to use '../'
// in order to gain access to files below document root
$src = preg_replace("/\.\.+\//", "", $src);
// get path to image on file system
$src = get_document_root($src) . '/' . $src;
return $src;
}
/**
*
*/
function get_document_root ($src) {
// check for unix servers
if(file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $src)) {
return $_SERVER['DOCUMENT_ROOT'];
}
// check from script filename (to get all directories to timthumb location)
$parts = array_diff(explode('/', $_SERVER['SCRIPT_FILENAME']), explode('/', $_SERVER['DOCUMENT_ROOT']));
$path = $_SERVER['DOCUMENT_ROOT'];
foreach ($parts as $part) {
$path .= '/' . $part;
if (file_exists($path . '/' . $src)) {
return $path;
}
}
// the relative paths below are useful if timthumb is moved outside of document root
// specifically if installed in wordpress themes like mimbo pro:
// /wp-content/themes/mimbopro/scripts/timthumb.php
$paths = array(
".",
"..",
"../..",
"../../..",
"../../../..",
"../../../../.."
);
foreach ($paths as $path) {
if(file_exists($path . '/' . $src)) {
return $path;
}
}
// special check for microsoft servers
if (!isset($_SERVER['DOCUMENT_ROOT'])) {
$path = str_replace("/", "\\", $_SERVER['ORIG_PATH_INFO']);
$path = str_replace($path, "", $_SERVER['SCRIPT_FILENAME']);
if (file_exists($path . '/' . $src)) {
return $path;
}
}
displayError('file not found ' . $src);
}
/**
* generic error message
*/
function displayError ($errorString = '') {
header('HTTP/1.1 400 Bad Request');
echo '<pre>' . $errorString . '<br />TimThumb version : ' . VERSION . '</pre>';
die();
}
?>