home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / function.fetch.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  6.3 KB  |  224 lines

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * @package Smarty
  5.  * @subpackage plugins
  6.  */
  7.  
  8.  
  9. /**
  10.  * Smarty {fetch} plugin
  11.  *
  12.  * Type:     function<br>
  13.  * Name:     fetch<br>
  14.  * Purpose:  fetch file, web or ftp data and display results
  15.  * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
  16.  *       (Smarty online manual)
  17.  * @param array
  18.  * @param Smarty
  19.  * @return string|null if the assign parameter is passed, Smarty assigns the
  20.  *                     result to a template variable
  21.  */
  22. function smarty_function_fetch($params, &$smarty)
  23. {
  24.     $file = $params['file'];
  25.  
  26.     if (empty($file)) {
  27.         $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
  28.         return;
  29.     }
  30.  
  31.     if ($smarty->security && !preg_match('!^(http|ftp)://!i', $file)) {
  32.         // fetching file, make sure it comes from secure directory
  33.         foreach ($smarty->secure_dir as $curr_dir) {
  34.             if (substr(realpath($file), 0, strlen(realpath($curr_dir))) == realpath($curr_dir)) {
  35.                 $resource_is_secure = true;
  36.                 break;
  37.             }
  38.         }
  39.         if (!$resource_is_secure) {
  40.             $smarty->_trigger_fatal_error("[plugin] (secure mode) fetch '$file' is not allowed");
  41.             return;
  42.         }
  43.         // fetch the file
  44.         if($fp = @fopen($file,'r')) {
  45.             while(!feof($fp)) {
  46.                 $content .= fgets ($fp,4096);
  47.             }
  48.             fclose($fp);
  49.         } else {
  50.             $smarty->_trigger_fatal_error("[plugin] fetch cannot read file '$file'");
  51.             return;            
  52.         }
  53.     } else {
  54.         // not a local file
  55.         if(preg_match('!^http://!i',$file)) {
  56.             // http fetch
  57.             if($uri_parts = parse_url($file)) {
  58.                 // set defaults
  59.                 $host = $server_name = $uri_parts['host'];
  60.                 $timeout = 30;
  61.                 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
  62.                 $agent = "Smarty Template Engine ".$smarty->_version;
  63.                 $referer = "";
  64.                 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
  65.                 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
  66.                 $_is_proxy = false;
  67.                 if(empty($uri_parts['port'])) {
  68.                     $port = 80;
  69.                 } else {
  70.                     $port = $uri_parts['port'];
  71.                 }
  72.                 if(empty($uri_parts['user'])) {
  73.                     $user = '';
  74.                 }                
  75.                 // loop through parameters, setup headers
  76.                 foreach($params as $param_key => $param_value) {            
  77.                     switch($param_key) {
  78.                         case "file":
  79.                         case "assign":
  80.                         case "assign_headers":
  81.                             break;
  82.                         case "user":
  83.                             if(!empty($param_value)) {
  84.                                 $user = $param_value;
  85.                             }
  86.                             break;
  87.                         case "pass":
  88.                             if(!empty($param_value)) {
  89.                                 $pass = $param_value;
  90.                             }
  91.                             break;
  92.                         case "accept":
  93.                             if(!empty($param_value)) {
  94.                                 $accept = $param_value;
  95.                             }
  96.                             break;
  97.                         case "header":
  98.                             if(!empty($param_value)) {
  99.                                 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
  100.                                     $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
  101.                                     return;                                    
  102.                                 } else {
  103.                                     $extra_headers[] = $param_value;
  104.                                 }
  105.                             }
  106.                             break;
  107.                         case "proxy_host":
  108.                             if(!empty($param_value)) {
  109.                                 $proxy_host = $param_value;
  110.                             }
  111.                             break;
  112.                         case "proxy_port":
  113.                             if(!preg_match('!\D!', $param_value)) {
  114.                                 $proxy_port = (int) $param_value;
  115.                             } else {
  116.                                 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
  117.                                 return;                                    
  118.                             }
  119.                             break;
  120.                         case "agent":
  121.                             if(!empty($param_value)) {
  122.                                 $agent = $param_value;
  123.                             }
  124.                             break;
  125.                         case "referer":
  126.                             if(!empty($param_value)) {
  127.                                 $referer = $param_value;
  128.                             }
  129.                             break;
  130.                         case "timeout":
  131.                             if(!preg_match('!\D!', $param_value)) {
  132.                                 $timeout = (int) $param_value;
  133.                             } else {
  134.                                 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
  135.                                 return;                                    
  136.                             }
  137.                             break;
  138.                         default:
  139.                             $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
  140.                             return;
  141.                     }            
  142.                 }
  143.                 if(!empty($proxy_host) && !empty($proxy_port)) {
  144.                     $_is_proxy = true;
  145.                     $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
  146.                 } else {
  147.                     $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
  148.                 }
  149.  
  150.                 if(!$fp) {
  151.                     $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
  152.                     return;                
  153.                 } else {
  154.                     if($_is_proxy) {
  155.                         fputs($fp, "GET $file HTTP/1.0\r\n");                        
  156.                     } else {
  157.                         fputs($fp, "GET $uri HTTP/1.0\r\n");
  158.                     }
  159.                     if(!empty($host)) {
  160.                         fputs($fp, "Host: $host\r\n");
  161.                     }
  162.                     if(!empty($accept)) {
  163.                         fputs($fp, "Accept: $accept\r\n");
  164.                     }
  165.                     if(!empty($agent)) {
  166.                         fputs($fp, "User-Agent: $agent\r\n");
  167.                     }
  168.                     if(!empty($referer)) {
  169.                         fputs($fp, "Referer: $referer\r\n");
  170.                     }
  171.                     if(isset($extra_headers) && is_array($extra_headers)) {
  172.                         foreach($extra_headers as $curr_header) {
  173.                             fputs($fp, $curr_header."\r\n");
  174.                         }
  175.                     }
  176.                     if(!empty($user) && !empty($pass)) {
  177.                         fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");                        
  178.                     }
  179.  
  180.                     $content = '';                    
  181.                     fputs($fp, "\r\n");
  182.                     while(!feof($fp)) {
  183.                         $content .= fgets($fp,4096);
  184.                     }
  185.                     fclose($fp);                    
  186.                     $csplit = split("\r\n\r\n",$content,2);
  187.  
  188.                     $content = $csplit[1];
  189.                     
  190.                     if(!empty($params['assign_headers'])) {
  191.                         $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
  192.                     }
  193.                 }
  194.             } else {
  195.                     $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
  196.                     return;
  197.             }
  198.         } else {
  199.             // ftp fetch
  200.             if($fp = @fopen($file,'r')) {
  201.                 while(!feof($fp)) {
  202.                     $content .= fgets ($fp,4096);
  203.                 }
  204.                 fclose($fp);
  205.             } else {
  206.                 $smarty->_trigger_fatal_error("[plugin] fetch cannot read file '$file'");
  207.                 return;            
  208.             }
  209.         }
  210.         
  211.     }
  212.  
  213.  
  214.     if (!empty($params['assign'])) {
  215.         $smarty->assign($params['assign'],$content);
  216.     } else {
  217.         return $content;
  218.     }
  219. }
  220.  
  221. /* vim: set expandtab: */
  222.  
  223. ?>
  224.