6 * @subpackage PluginsFunction
10 * Smarty {fetch} plugin
14 * Purpose: fetch file, web or ftp data and display results
16 * @link http://www.smarty.net/manual/en/language.function.fetch.php {fetch}
17 * (Smarty online manual)
18 * @author Monte Ohrt <monte at ohrt dot com>
19 * @param array $params parameters
20 * @param Smarty_Internal_Template $template template object
21 * @return string|null if the assign parameter is passed, Smarty assigns the result to a template variable
23 function smarty_function_fetch($params, $template)
25 if (empty($params['file'])) {
26 trigger_error("[plugin] fetch parameter 'file' cannot be empty",E_USER_NOTICE);
30 // strip file protocol
31 if (stripos($params['file'], 'file://') === 0) {
32 $params['file'] = substr($params['file'], 7);
35 $protocol = strpos($params['file'], '://');
36 if ($protocol !== false) {
37 $protocol = strtolower(substr($params['file'], 0, $protocol));
40 if (isset($template->smarty->security_policy)) {
42 // remote resource (or php stream, …)
43 if(!$template->smarty->security_policy->isTrustedUri($params['file'])) {
48 if(!$template->smarty->security_policy->isTrustedResourceDir($params['file'])) {
55 if ($protocol == 'http') {
57 if($uri_parts = parse_url($params['file'])) {
59 $host = $server_name = $uri_parts['host'];
61 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
62 $agent = "Smarty Template Engine ". Smarty::SMARTY_VERSION;
64 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
65 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
67 if(empty($uri_parts['port'])) {
70 $port = $uri_parts['port'];
72 if(!empty($uri_parts['user'])) {
73 $user = $uri_parts['user'];
75 if(!empty($uri_parts['pass'])) {
76 $pass = $uri_parts['pass'];
78 // loop through parameters, setup headers
79 foreach($params as $param_key => $param_value) {
83 case "assign_headers":
86 if(!empty($param_value)) {
91 if(!empty($param_value)) {
96 if(!empty($param_value)) {
97 $accept = $param_value;
101 if(!empty($param_value)) {
102 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
103 trigger_error("[plugin] invalid header format '".$param_value."'",E_USER_NOTICE);
106 $extra_headers[] = $param_value;
111 if(!empty($param_value)) {
112 $proxy_host = $param_value;
116 if(!preg_match('!\D!', $param_value)) {
117 $proxy_port = (int) $param_value;
119 trigger_error("[plugin] invalid value for attribute '".$param_key."'",E_USER_NOTICE);
124 if(!empty($param_value)) {
125 $agent = $param_value;
129 if(!empty($param_value)) {
130 $referer = $param_value;
134 if(!preg_match('!\D!', $param_value)) {
135 $timeout = (int) $param_value;
137 trigger_error("[plugin] invalid value for attribute '".$param_key."'",E_USER_NOTICE);
142 trigger_error("[plugin] unrecognized attribute '".$param_key."'",E_USER_NOTICE);
146 if(!empty($proxy_host) && !empty($proxy_port)) {
148 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
150 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
154 trigger_error("[plugin] unable to fetch: $errstr ($errno)",E_USER_NOTICE);
158 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
160 fputs($fp, "GET $uri HTTP/1.0\r\n");
163 fputs($fp, "Host: $host\r\n");
165 if(!empty($accept)) {
166 fputs($fp, "Accept: $accept\r\n");
169 fputs($fp, "User-Agent: $agent\r\n");
171 if(!empty($referer)) {
172 fputs($fp, "Referer: $referer\r\n");
174 if(isset($extra_headers) && is_array($extra_headers)) {
175 foreach($extra_headers as $curr_header) {
176 fputs($fp, $curr_header."\r\n");
179 if(!empty($user) && !empty($pass)) {
180 fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
185 $content .= fgets($fp,4096);
188 $csplit = preg_split("!\r\n\r\n!",$content,2);
190 $content = $csplit[1];
192 if(!empty($params['assign_headers'])) {
193 $template->assign($params['assign_headers'],preg_split("!\r\n!",$csplit[0]));
197 trigger_error("[plugin fetch] unable to parse URL, check syntax",E_USER_NOTICE);
201 $content = @file_get_contents($params['file']);
202 if ($content === false) {
203 throw new SmartyException("{fetch} cannot read resource '" . $params['file'] ."'");
207 if (!empty($params['assign'])) {
208 $template->assign($params['assign'], $content);