function.fetch.php
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:9k
源码类别:

多媒体编程

开发平台:

Visual C++

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