Pego el resto del código, que no me cabía en un sólo post:
Código PHP:
Ver original<?php
/**
* clean out old files from the cache
* you can change the number of files to store and to delete per loop in the defines at the top of the code
*/
function cleanCache() {
$files = glob("cache/*", GLOB_BRACE
);
$yesterday = time() - (24 * 60 * 60);
usort($files, 'filemtime_compare'); $i = 0;
if (count($files) > CACHE_SIZE
) {
foreach ($files as $file) {
$i ++;
if ($i >= CACHE_CLEAR) {
return;
}
return;
}
}
}
}
}
}
/**
* compare the file time of two files
*/
function filemtime_compare($a, $b) {
}
/**
* determine the file mime type
*/
function mime_type($file) {
$os = 'WIN';
} else {
$os = PHP_OS;
}
$mime_type = '';
}
// use PECL fileinfo to determine mime type
if (!valid_src_mime_type($mime_type)) {
if ($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") {
}
}
// use file's extension to determine mime type
if (!valid_src_mime_type($mime_type)) {
// set defaults
$mime_type = 'image/png';
// file details
// mime types
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif'
);
$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 ($cache_dir, $mime_type) {
// make sure cache dir exists
// give 777 permissions so that developer can overwrite
// files created by web server user
}
show_cache_file ($cache_dir, $mime_type);
}
/**
*
*/
function show_cache_file ($cache_dir, $mime_type) {
$cache_file = $cache_dir . '/' . get_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"); }
}
// 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);
}
}
/**
*
*/
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) {
return TRUE;
} else {
return FALSE;
}
}
/**
*
*/
function checkExternal ($src) {
'flickr.com',
'picasa.com',
'blogger.com',
'wordpress.com',
);
if (ereg('http://', $src) == true) {
$isAllowedSite = false;
foreach ($allowedSites as $site) {
if (ereg($site, $url_info['host']) == true) { $isAllowedSite = true;
}
}
if ($isAllowedSite) {
$local_filepath = 'temp/' . $filename[count($filename) - 1];
$fh = fopen($local_filepath, 'w');
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');
}
displayError
('error reading file ' . $src . ' from remote host: ' . curl_error($ch)); }
} else {
displayError('remote file for ' . $src . ' can not be accessed. It is likely that the file permissions are restricted');
}
displayError('error writing temporary file');
}
}
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) {
$src = str_replace('http://' . $_SERVER['HTTP_HOST'], '', $src); $src = str_replace('https://' . $_SERVER['HTTP_HOST'], '', $src);
$src = checkExternal ($src);
// remove slash from start of string
}
// remove http/ https/ ftp
// remove domain name from the source url
$host = $_SERVER['HTTP_HOST'];
// don't allow users the ability to use '../'
// in order to gain access to files below document root
// src should be specified relative to document root like:
// src=images/img.jpg or src=/images/img.jpg
// not like:
// src=../images/img.jpg
// get path to image on file system
$src = get_document_root($src) . '/' . $src;
return $src;
}
?>