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");
29 if ($smarty->security
&& !preg_match('!^(http|ftp)://!i', $params['file'])) {
30 $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
31 require_once(SMARTY_DIR
. 'core' . DIRECTORY_SEPARATOR
. 'core.is_secure.php');
32 if(!smarty_core_is_secure($_params, $smarty)) {
33 $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
38 if($fp = @fopen
($params['file'],'r')) {
40 $content .= fgets ($fp,4096);
44 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
49 if(preg_match('!^http://!i',$params['file'])) {
51 if($uri_parts = parse_url($params['file'])) {
53 $host = $server_name = $uri_parts['host'];
55 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
56 $agent = "Smarty Template Engine ".$smarty->_version
;
58 $uri = !empty($uri_parts['path']) ?
$uri_parts['path'] : '/';
59 $uri .= !empty($uri_parts['query']) ?
'?' . $uri_parts['query'] : '';
61 if(empty($uri_parts['port'])) {
64 $port = $uri_parts['port'];
66 if(empty($uri_parts['user'])) {
69 // loop through parameters, setup headers
70 foreach($params as $param_key => $param_value) {
74 case "assign_headers":
77 if(!empty($param_value)) {
82 if(!empty($param_value)) {
87 if(!empty($param_value)) {
88 $accept = $param_value;
92 if(!empty($param_value)) {
93 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
94 $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
97 $extra_headers[] = $param_value;
102 if(!empty($param_value)) {
103 $proxy_host = $param_value;
107 if(!preg_match('!\D!', $param_value)) {
108 $proxy_port = (int) $param_value;
110 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
115 if(!empty($param_value)) {
116 $agent = $param_value;
120 if(!empty($param_value)) {
121 $referer = $param_value;
125 if(!preg_match('!\D!', $param_value)) {
126 $timeout = (int) $param_value;
128 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
133 $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
137 if(!empty($proxy_host) && !empty($proxy_port)) {
139 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
141 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
145 $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
149 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
151 fputs($fp, "GET $uri HTTP/1.0\r\n");
154 fputs($fp, "Host: $host\r\n");
156 if(!empty($accept)) {
157 fputs($fp, "Accept: $accept\r\n");
160 fputs($fp, "User-Agent: $agent\r\n");
162 if(!empty($referer)) {
163 fputs($fp, "Referer: $referer\r\n");
165 if(isset($extra_headers) && is_array($extra_headers)) {
166 foreach($extra_headers as $curr_header) {
167 fputs($fp, $curr_header."\r\n");
170 if(!empty($user) && !empty($pass)) {
171 fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
177 $content .= fgets($fp,4096);
180 $csplit = split("\r\n\r\n",$content,2);
182 $content = $csplit[1];
184 if(!empty($params['assign_headers'])) {
185 $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
189 $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
194 if($fp = @fopen
($params['file'],'r')) {
196 $content .= fgets ($fp,4096);
200 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
208 if (!empty($params['assign'])) {
209 $smarty->assign($params['assign'],$content);
215 /* vim: set expandtab: */