Ver Mensaje Individual
  #8 (permalink)  
Antiguo 11/02/2010, 09:11
Masane
 
Fecha de Ingreso: marzo-2008
Mensajes: 207
Antigüedad: 16 años, 2 meses
Puntos: 0
Respuesta: Guardar thumbnails

Pego el resto del código, que no me cabía en un sólo post:

Código PHP:
Ver original
  1. <?php
  2. /**
  3.  * clean out old files from the cache
  4.  * you can change the number of files to store and to delete per loop in the defines at the top of the code
  5.  */
  6. function cleanCache() {
  7.  
  8.     $files = glob("cache/*", GLOB_BRACE);
  9.    
  10.     if (count($files) > 0) {
  11.    
  12.         $yesterday = time() - (24 * 60 * 60);
  13.        
  14.         usort($files, 'filemtime_compare');
  15.         $i = 0;
  16.        
  17.         if (count($files) > CACHE_SIZE) {
  18.            
  19.             foreach ($files as $file) {
  20.                
  21.                 $i ++;
  22.                
  23.                 if ($i >= CACHE_CLEAR) {
  24.                     return;
  25.                 }
  26.                
  27.                 if (@filemtime($file) > $yesterday) {
  28.                     return;
  29.                 }
  30.                
  31.                 if (file_exists($file)) {
  32.                     unlink($file);
  33.                 }
  34.                
  35.             }
  36.            
  37.         }
  38.        
  39.     }
  40.  
  41. }
  42.  
  43.  
  44. /**
  45.  * compare the file time of two files
  46.  */
  47. function filemtime_compare($a, $b) {
  48.  
  49.     return filemtime($a) - filemtime($b);
  50.    
  51. }
  52.  
  53.  
  54. /**
  55.  * determine the file mime type
  56.  */
  57. function mime_type($file) {
  58.  
  59.     if (stristr(PHP_OS, 'WIN')) {
  60.         $os = 'WIN';
  61.     } else {
  62.         $os = PHP_OS;
  63.     }
  64.  
  65.     $mime_type = '';
  66.  
  67.     if (function_exists('mime_content_type')) {
  68.         $mime_type = mime_content_type($file);
  69.     }
  70.    
  71.     // use PECL fileinfo to determine mime type
  72.     if (!valid_src_mime_type($mime_type)) {
  73.         if (function_exists('finfo_open')) {
  74.             $finfo = @finfo_open(FILEINFO_MIME);
  75.             if ($finfo != '') {
  76.                 $mime_type = finfo_file($finfo, $file);
  77.                 finfo_close($finfo);
  78.             }
  79.         }
  80.     }
  81.  
  82.     // try to determine mime type by using unix file command
  83.     // this should not be executed on windows
  84.     if (!valid_src_mime_type($mime_type) && $os != "WIN") {
  85.         if (preg_match("/FREEBSD|LINUX/", $os)) {
  86.             $mime_type = trim(@shell_exec('file -bi ' . escapeshellarg($file)));
  87.         }
  88.     }
  89.  
  90.     // use file's extension to determine mime type
  91.     if (!valid_src_mime_type($mime_type)) {
  92.  
  93.         // set defaults
  94.         $mime_type = 'image/png';
  95.         // file details
  96.         $fileDetails = pathinfo($file);
  97.         $ext = strtolower($fileDetails["extension"]);
  98.         // mime types
  99.         $types = array(
  100.              'jpg'  => 'image/jpeg',
  101.              'jpeg' => 'image/jpeg',
  102.              'png'  => 'image/png',
  103.              'gif'  => 'image/gif'
  104.          );
  105.        
  106.         if (strlen($ext) && strlen($types[$ext])) {
  107.             $mime_type = $types[$ext];
  108.         }
  109.        
  110.     }
  111.    
  112.     return $mime_type;
  113.  
  114. }
  115.  
  116.  
  117. /**
  118.  *
  119.  */
  120. function valid_src_mime_type($mime_type) {
  121.  
  122.     if (preg_match("/jpg|jpeg|gif|png/i", $mime_type)) {
  123.         return true;
  124.     }
  125.    
  126.     return false;
  127.  
  128. }
  129.  
  130.  
  131. /**
  132.  *
  133.  */
  134. function check_cache ($cache_dir, $mime_type) {
  135.  
  136.     // make sure cache dir exists
  137.     if (!file_exists($cache_dir)) {
  138.         // give 777 permissions so that developer can overwrite
  139.         // files created by web server user
  140.         mkdir($cache_dir);
  141.         chmod($cache_dir, 0777);
  142.     }
  143.  
  144.     show_cache_file ($cache_dir, $mime_type);
  145.  
  146. }
  147.  
  148.  
  149. /**
  150.  *
  151.  */
  152. function show_cache_file ($cache_dir, $mime_type) {
  153.  
  154.     $cache_file = $cache_dir . '/' . get_cache_file();
  155.  
  156.     if (file_exists($cache_file)) {
  157.        
  158.         $gmdate_mod = gmdate("D, d M Y H:i:s", filemtime($cache_file));
  159.        
  160.         if(! strstr($gmdate_mod, "GMT")) {
  161.             $gmdate_mod .= " GMT";
  162.         }
  163.        
  164.         if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
  165.        
  166.             // check for updates
  167.             $if_modified_since = preg_replace ("/;.*$/", "", $_SERVER["HTTP_IF_MODIFIED_SINCE"]);
  168.            
  169.             if ($if_modified_since == $gmdate_mod) {
  170.                 header("HTTP/1.1 304 Not Modified");
  171.                 die();
  172.             }
  173.  
  174.         }
  175.        
  176.         $fileSize = filesize ($cache_file);
  177.        
  178.         // send headers then display image
  179.         header ('Content-Type: ' . $mime_type);
  180.         header ('Accept-Ranges: bytes');
  181.         header ('Last-Modified: ' . $gmdate_mod);
  182.         header ('Content-Length: ' . $fileSize);
  183.         header ('Cache-Control: max-age=9999, must-revalidate');
  184.         header ('Expires: ' . $gmdate_mod);
  185.        
  186.         readfile ($cache_file);
  187.        
  188.         die();
  189.  
  190.     }
  191.    
  192. }
  193.  
  194.  
  195. /**
  196.  *
  197.  */
  198. function get_cache_file() {
  199.  
  200.     global $lastModified;
  201.     static $cache_file;
  202.    
  203.     if (!$cache_file) {
  204.         $cachename = $_SERVER['QUERY_STRING'] . VERSION . $lastModified;
  205.         $cache_file = md5($cachename) . '.png';
  206.     }
  207.    
  208.     return $cache_file;
  209.  
  210. }
  211.  
  212.  
  213. /**
  214.  * check to if the url is valid or not
  215.  */
  216. function valid_extension ($ext) {
  217.  
  218.     if (preg_match("/jpg|jpeg|png|gif/i", $ext)) {
  219.         return TRUE;
  220.     } else {
  221.         return FALSE;
  222.     }
  223.    
  224. }
  225.  
  226.  
  227. /**
  228.  *
  229.  */
  230. function checkExternal ($src) {
  231.  
  232.     $allowedSites = array(
  233.         'flickr.com',
  234.         'picasa.com',
  235.         'blogger.com',
  236.         'wordpress.com',
  237.     );
  238.  
  239.     if (ereg('http://', $src) == true) {
  240.    
  241.         $url_info = parse_url ($src);
  242.        
  243.         $isAllowedSite = false;
  244.         foreach ($allowedSites as $site) {
  245.             if (ereg($site, $url_info['host']) == true) {
  246.                 $isAllowedSite = true;
  247.             }
  248.         }
  249.        
  250.         if ($isAllowedSite) {
  251.        
  252.             $filename = explode('/', $src);
  253.             $local_filepath = 'temp/' . $filename[count($filename) - 1];
  254.            
  255.             if (!file_exists($local_filepath)) {
  256.                
  257.                 if (function_exists('curl_init')) {
  258.                
  259.                     $fh = fopen($local_filepath, 'w');
  260.                     $ch = curl_init($src);
  261.                    
  262.                     curl_setopt($ch, CURLOPT_URL, $src);
  263.                     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  264.                     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  265.                     curl_setopt($ch, CURLOPT_HEADER, 0);
  266.                     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');
  267.                     curl_setopt($ch, CURLOPT_FILE, $fh);
  268.                    
  269.                     if (curl_exec($ch) === FALSE) {
  270.                         if (file_exists($local_filepath)) {
  271.                             unlink($local_filepath);
  272.                         }
  273.                         displayError('error reading file ' . $src . ' from remote host: ' . curl_error($ch));
  274.                     }
  275.                    
  276.                     curl_close($ch);
  277.                     fclose($fh);
  278.  
  279.                 } else {
  280.            
  281.                     if (!$img = file_get_contents($src)) {
  282.                         displayError('remote file for ' . $src . ' can not be accessed. It is likely that the file permissions are restricted');
  283.                     }
  284.                    
  285.                     if (file_put_contents($local_filepath, $img) == FALSE) {
  286.                         displayError('error writing temporary file');
  287.                     }
  288.                    
  289.                 }
  290.                
  291.                 if (!file_exists($local_filepath)) {
  292.                     displayError('local file for ' . $src . ' can not be created');
  293.                 }
  294.                
  295.             }
  296.            
  297.             $src = $local_filepath;
  298.            
  299.         } else {
  300.        
  301.             displayError('remote host "' . $url_info['host'] . '" not allowed');
  302.            
  303.         }
  304.        
  305.     }
  306.    
  307.     return $src;
  308.    
  309. }
  310.  
  311.  
  312.  
  313. /**
  314.  * tidy up the image source url
  315.  */
  316. function cleanSource($src) {
  317.  
  318.     $src = str_replace('http://' . $_SERVER['HTTP_HOST'], '', $src);
  319.     $src = str_replace('https://' . $_SERVER['HTTP_HOST'], '', $src);
  320.     $src = htmlentities($src);
  321.  
  322.     $src = checkExternal ($src);
  323.    
  324.     // remove slash from start of string
  325.     if(strpos($src, '/') === 0) {
  326.         $src = substr($src, -(strlen($src) - 1));
  327.     }
  328.  
  329.     // remove http/ https/ ftp
  330.     $src = preg_replace("/^((ht|f)tp(s|):\/\/)/i", '', $src);
  331.     // remove domain name from the source url
  332.     $host = $_SERVER['HTTP_HOST'];
  333.     $src = str_replace($host, '', $src);
  334.     $host = str_replace('www.', '', $host);
  335.     $src = str_replace($host, '', $src);
  336.  
  337.     // don't allow users the ability to use '../'
  338.     // in order to gain access to files below document root
  339.  
  340.     // src should be specified relative to document root like:
  341.     // src=images/img.jpg or src=/images/img.jpg
  342.     // not like:
  343.     // src=../images/img.jpg
  344.     $src = preg_replace("/\.\.+\//", "", $src);
  345.    
  346.     // get path to image on file system
  347.     $src = get_document_root($src) . '/' . $src;
  348.  
  349.     return $src;
  350.  
  351. }
  352. ?>