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