00001 <?php
00022 function smarty_function_fetch($params, &$smarty)
00023 {
00024 if (empty($params['file'])) {
00025 $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
00026 return;
00027 }
00028
00029 $content = '';
00030 if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
00031 $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
00032 require_once(SMARTY_CORE_DIR . 'core.is_secure.php');
00033 if(!smarty_core_is_secure($_params, $smarty)) {
00034 $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
00035 return;
00036 }
00037
00038 // fetch the file
00039 if($fp = @fopen($params['file'],'r')) {
00040 while(!feof($fp)) {
00041 $content .= fgets ($fp,4096);
00042 }
00043 fclose($fp);
00044 } else {
00045 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
00046 return;
00047 }
00048 } else {
00049
00050 if(preg_match('!^http://!i',$params['file'])) {
00051
00052 if($uri_parts = parse_url($params['file'])) {
00053
00054 $host = $server_name = $uri_parts['host'];
00055 $timeout = 30;
00056 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
00057 $agent = "Smarty Template Engine ".$smarty->_version;
00058 $referer = "";
00059 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
00060 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
00061 $_is_proxy = false;
00062 if(empty($uri_parts['port'])) {
00063 $port = 80;
00064 } else {
00065 $port = $uri_parts['port'];
00066 }
00067 if(!empty($uri_parts['user'])) {
00068 $user = $uri_parts['user'];
00069 }
00070 if(!empty($uri_parts['pass'])) {
00071 $pass = $uri_parts['pass'];
00072 }
00073
00074 foreach($params as $param_key => $param_value) {
00075 switch($param_key) {
00076 case "file":
00077 case "assign":
00078 case "assign_headers":
00079 break;
00080 case "user":
00081 if(!empty($param_value)) {
00082 $user = $param_value;
00083 }
00084 break;
00085 case "pass":
00086 if(!empty($param_value)) {
00087 $pass = $param_value;
00088 }
00089 break;
00090 case "accept":
00091 if(!empty($param_value)) {
00092 $accept = $param_value;
00093 }
00094 break;
00095 case "header":
00096 if(!empty($param_value)) {
00097 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
00098 $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
00099 return;
00100 } else {
00101 $extra_headers[] = $param_value;
00102 }
00103 }
00104 break;
00105 case "proxy_host":
00106 if(!empty($param_value)) {
00107 $proxy_host = $param_value;
00108 }
00109 break;
00110 case "proxy_port":
00111 if(!preg_match('!\D!', $param_value)) {
00112 $proxy_port = (int) $param_value;
00113 } else {
00114 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
00115 return;
00116 }
00117 break;
00118 case "agent":
00119 if(!empty($param_value)) {
00120 $agent = $param_value;
00121 }
00122 break;
00123 case "referer":
00124 if(!empty($param_value)) {
00125 $referer = $param_value;
00126 }
00127 break;
00128 case "timeout":
00129 if(!preg_match('!\D!', $param_value)) {
00130 $timeout = (int) $param_value;
00131 } else {
00132 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
00133 return;
00134 }
00135 break;
00136 default:
00137 $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
00138 return;
00139 }
00140 }
00141 if(!empty($proxy_host) && !empty($proxy_port)) {
00142 $_is_proxy = true;
00143 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
00144 } else {
00145 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
00146 }
00147
00148 if(!$fp) {
00149 $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
00150 return;
00151 } else {
00152 if($_is_proxy) {
00153 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
00154 } else {
00155 fputs($fp, "GET $uri HTTP/1.0\r\n");
00156 }
00157 if(!empty($host)) {
00158 fputs($fp, "Host: $host\r\n");
00159 }
00160 if(!empty($accept)) {
00161 fputs($fp, "Accept: $accept\r\n");
00162 }
00163 if(!empty($agent)) {
00164 fputs($fp, "User-Agent: $agent\r\n");
00165 }
00166 if(!empty($referer)) {
00167 fputs($fp, "Referer: $referer\r\n");
00168 }
00169 if(isset($extra_headers) && is_array($extra_headers)) {
00170 foreach($extra_headers as $curr_header) {
00171 fputs($fp, $curr_header."\r\n");
00172 }
00173 }
00174 if(!empty($user) && !empty($pass)) {
00175 fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
00176 }
00177
00178 fputs($fp, "\r\n");
00179 while(!feof($fp)) {
00180 $content .= fgets($fp,4096);
00181 }
00182 fclose($fp);
00183 $csplit = split("\r\n\r\n",$content,2);
00184
00185 $content = $csplit[1];
00186
00187 if(!empty($params['assign_headers'])) {
00188 $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
00189 }
00190 }
00191 } else {
00192 $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
00193 return;
00194 }
00195 } else {
00196
00197 if($fp = @fopen($params['file'],'r')) {
00198 while(!feof($fp)) {
00199 $content .= fgets ($fp,4096);
00200 }
00201 fclose($fp);
00202 } else {
00203 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
00204 return;
00205 }
00206 }
00207
00208 }
00209
00210
00211 if (!empty($params['assign'])) {
00212 $smarty->assign($params['assign'],$content);
00213 } else {
00214 return $content;
00215 }
00216 }
00217
00218
00219
00220 ?>