10 * Smarty {fetch} plugin
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)
19 * @return string|null if the assign parameter is passed, Smarty assigns the
20 * result to a template variable
22 function smarty_function_fetch($params, &$smarty)
24 if (empty($params['file'])) {
25 $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
30 if ($smarty->security
&& !preg_match('!^(http|ftp)://!i', $params['file'])) {
31 $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
32 require_once(SMARTY_CORE_DIR
. 'core.is_secure.php');
33 if(!smarty_core_is_secure($_params, $smarty)) {
34 $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
39 if($fp = @fopen
($params['file'],'r')) {
41 $content .= fgets ($fp,4096);
45 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
50 if(preg_match('!^http://!i',$params['file'])) {
52 if($uri_parts = parse_url($params['file'])) {
54 $host = $server_name = $uri_parts['host'];
56 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
57 $agent = "Smarty Template Engine ".$smarty->_version
;
59 $uri = !empty($uri_parts['path']) ?
$uri_parts['path'] : '/';
60 $uri .= !empty($uri_parts['query']) ?
'?' . $uri_parts['query'] : '';
62 if(empty($uri_parts['port'])) {
65 $port = $uri_parts['port'];
67 if(!empty($uri_parts['user'])) {
68 $user = $uri_parts['user'];
70 if(!empty($uri_parts['pass'])) {
71 $pass = $uri_parts['pass'];
73 // loop through parameters, setup headers
74 foreach($params as $param_key => $param_value) {
78 case "assign_headers":
81 if(!empty($param_value)) {
86 if(!empty($param_value)) {
91 if(!empty($param_value)) {
92 $accept = $param_value;
96 if(!empty($param_value)) {
97 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
98 $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
101 $extra_headers[] = $param_value;
106 if(!empty($param_value)) {
107 $proxy_host = $param_value;
111 if(!preg_match('!\D!', $param_value)) {
112 $proxy_port = (int) $param_value;
114 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
119 if(!empty($param_value)) {
120 $agent = $param_value;
124 if(!empty($param_value)) {
125 $referer = $param_value;
129 if(!preg_match('!\D!', $param_value)) {
130 $timeout = (int) $param_value;
132 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
137 $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
141 if(!empty($proxy_host) && !empty($proxy_port)) {
143 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
145 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
149 $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
153 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
155 fputs($fp, "GET $uri HTTP/1.0\r\n");
158 fputs($fp, "Host: $host\r\n");
160 if(!empty($accept)) {
161 fputs($fp, "Accept: $accept\r\n");
164 fputs($fp, "User-Agent: $agent\r\n");
166 if(!empty($referer)) {
167 fputs($fp, "Referer: $referer\r\n");
169 if(isset($extra_headers) && is_array($extra_headers)) {
170 foreach($extra_headers as $curr_header) {
171 fputs($fp, $curr_header."\r\n");
174 if(!empty($user) && !empty($pass)) {
175 fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
180 $content .= fgets($fp,4096);
183 $csplit = split("\r\n\r\n",$content,2);
185 $content = $csplit[1];
187 if(!empty($params['assign_headers'])) {
188 $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
192 $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
197 if($fp = @fopen
($params['file'],'r')) {
199 $content .= fgets ($fp,4096);
203 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
211 if (!empty($params['assign'])) {
212 $smarty->assign($params['assign'],$content);
218 /* vim: set expandtab: */