6 * @subpackage PluginsFunction
10 * Smarty {fetch} plugin
13 * Purpose: fetch file, web or ftp data and display results
15 * @link http://www.smarty.net/manual/en/language.function.fetch.php {fetch}
16 * (Smarty online manual)
17 * @author Monte Ohrt <monte at ohrt dot com>
19 * @param array $params parameters
20 * @param Smarty_Internal_Template $template template object
22 * @throws SmartyException
23 * @return string|null if the assign parameter is passed, Smarty assigns the result to a template variable
25 function smarty_function_fetch($params, $template)
27 if (empty($params['file'])) {
28 trigger_error("[plugin] fetch parameter 'file' cannot be empty", E_USER_NOTICE);
33 // strip file protocol
34 if (stripos($params['file'], 'file://') === 0) {
35 $params['file'] = substr($params['file'], 7);
38 $protocol = strpos($params['file'], '://');
39 if ($protocol !== false) {
40 $protocol = strtolower(substr($params['file'], 0, $protocol));
43 if (isset($template->smarty->security_policy)) {
45 // remote resource (or php stream, …)
46 if (!$template->smarty->security_policy->isTrustedUri($params['file'])) {
51 if (!$template->smarty->security_policy->isTrustedResourceDir($params['file'])) {
58 if ($protocol == 'http') {
60 if ($uri_parts = parse_url($params['file'])) {
62 $host = $server_name = $uri_parts['host'];
64 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
65 $agent = "Smarty Template Engine " . Smarty::SMARTY_VERSION;
67 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
68 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
70 if (empty($uri_parts['port'])) {
73 $port = $uri_parts['port'];
75 if (!empty($uri_parts['user'])) {
76 $user = $uri_parts['user'];
78 if (!empty($uri_parts['pass'])) {
79 $pass = $uri_parts['pass'];
81 // loop through parameters, setup headers
82 foreach ($params as $param_key => $param_value) {
86 case "assign_headers":
89 if (!empty($param_value)) {
94 if (!empty($param_value)) {
99 if (!empty($param_value)) {
100 $accept = $param_value;
104 if (!empty($param_value)) {
105 if (!preg_match('![\w\d-]+: .+!', $param_value)) {
106 trigger_error("[plugin] invalid header format '" . $param_value . "'", E_USER_NOTICE);
110 $extra_headers[] = $param_value;
115 if (!empty($param_value)) {
116 $proxy_host = $param_value;
120 if (!preg_match('!\D!', $param_value)) {
121 $proxy_port = (int) $param_value;
123 trigger_error("[plugin] invalid value for attribute '" . $param_key . "'", E_USER_NOTICE);
129 if (!empty($param_value)) {
130 $agent = $param_value;
134 if (!empty($param_value)) {
135 $referer = $param_value;
139 if (!preg_match('!\D!', $param_value)) {
140 $timeout = (int) $param_value;
142 trigger_error("[plugin] invalid value for attribute '" . $param_key . "'", E_USER_NOTICE);
148 trigger_error("[plugin] unrecognized attribute '" . $param_key . "'", E_USER_NOTICE);
153 if (!empty($proxy_host) && !empty($proxy_port)) {
155 $fp = fsockopen($proxy_host, $proxy_port, $errno, $errstr, $timeout);
157 $fp = fsockopen($server_name, $port, $errno, $errstr, $timeout);
161 trigger_error("[plugin] unable to fetch: $errstr ($errno)", E_USER_NOTICE);
166 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
168 fputs($fp, "GET $uri HTTP/1.0\r\n");
171 fputs($fp, "Host: $host\r\n");
173 if (!empty($accept)) {
174 fputs($fp, "Accept: $accept\r\n");
176 if (!empty($agent)) {
177 fputs($fp, "User-Agent: $agent\r\n");
179 if (!empty($referer)) {
180 fputs($fp, "Referer: $referer\r\n");
182 if (isset($extra_headers) && is_array($extra_headers)) {
183 foreach ($extra_headers as $curr_header) {
184 fputs($fp, $curr_header . "\r\n");
187 if (!empty($user) && !empty($pass)) {
188 fputs($fp, "Authorization: BASIC " . base64_encode("$user:$pass") . "\r\n");
193 $content .= fgets($fp, 4096);
196 $csplit = preg_split("!\r\n\r\n!", $content, 2);
198 $content = $csplit[1];
200 if (!empty($params['assign_headers'])) {
201 $template->assign($params['assign_headers'], preg_split("!\r\n!", $csplit[0]));
205 trigger_error("[plugin fetch] unable to parse URL, check syntax", E_USER_NOTICE);
210 $content = @file_get_contents($params['file']);
211 if ($content === false) {
212 throw new SmartyException("{fetch} cannot read resource '" . $params['file'] . "'");
216 if (!empty($params['assign'])) {
217 $template->assign($params['assign'], $content);