1 <?php |
|
2 /** |
|
3 * Smarty plugin |
|
4 * |
|
5 * @package Smarty |
|
6 * @subpackage PluginsFunction |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Smarty {fetch} plugin |
|
11 * Type: function<br> |
|
12 * Name: fetch<br> |
|
13 * Purpose: fetch file, web or ftp data and display results |
|
14 * |
|
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> |
|
18 * |
|
19 * @param array $params parameters |
|
20 * @param Smarty_Internal_Template $template template object |
|
21 * |
|
22 * @throws SmartyException |
|
23 * @return string|null if the assign parameter is passed, Smarty assigns the result to a template variable |
|
24 */ |
|
25 function smarty_function_fetch($params, $template) { |
|
26 if (empty($params['file'])) { |
|
27 trigger_error("[plugin] fetch parameter 'file' cannot be empty", E_USER_NOTICE); |
|
28 |
|
29 return; |
|
30 } |
|
31 |
|
32 // strip file protocol |
|
33 if (stripos($params['file'], 'file://') === 0) { |
|
34 $params['file'] = substr($params['file'], 7); |
|
35 } |
|
36 |
|
37 $protocol = strpos($params['file'], '://'); |
|
38 if ($protocol !== false) { |
|
39 $protocol = strtolower(substr($params['file'], 0, $protocol)); |
|
40 } |
|
41 |
|
42 if (isset($template->smarty->security_policy)) { |
|
43 if ($protocol) { |
|
44 // remote resource (or php stream, …) |
|
45 if (!$template->smarty->security_policy->isTrustedUri($params['file'])) { |
|
46 return; |
|
47 } |
|
48 } else { |
|
49 // local file |
|
50 if (!$template->smarty->security_policy->isTrustedResourceDir($params['file'])) { |
|
51 return; |
|
52 } |
|
53 } |
|
54 } |
|
55 |
|
56 $content = ''; |
|
57 if ($protocol == 'http') { |
|
58 // http fetch |
|
59 if ($uri_parts = parse_url($params['file'])) { |
|
60 // set defaults |
|
61 $host = $server_name = $uri_parts['host']; |
|
62 $timeout = 30; |
|
63 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; |
|
64 $agent = "Smarty Template Engine " . Smarty::SMARTY_VERSION; |
|
65 $referer = ""; |
|
66 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/'; |
|
67 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : ''; |
|
68 $_is_proxy = false; |
|
69 if (empty($uri_parts['port'])) { |
|
70 $port = 80; |
|
71 } else { |
|
72 $port = $uri_parts['port']; |
|
73 } |
|
74 if (!empty($uri_parts['user'])) { |
|
75 $user = $uri_parts['user']; |
|
76 } |
|
77 if (!empty($uri_parts['pass'])) { |
|
78 $pass = $uri_parts['pass']; |
|
79 } |
|
80 // loop through parameters, setup headers |
|
81 foreach ($params as $param_key => $param_value) { |
|
82 switch ($param_key) { |
|
83 case "file": |
|
84 case "assign": |
|
85 case "assign_headers": |
|
86 break; |
|
87 case "user": |
|
88 if (!empty($param_value)) { |
|
89 $user = $param_value; |
|
90 } |
|
91 break; |
|
92 case "pass": |
|
93 if (!empty($param_value)) { |
|
94 $pass = $param_value; |
|
95 } |
|
96 break; |
|
97 case "accept": |
|
98 if (!empty($param_value)) { |
|
99 $accept = $param_value; |
|
100 } |
|
101 break; |
|
102 case "header": |
|
103 if (!empty($param_value)) { |
|
104 if (!preg_match('![\w\d-]+: .+!', $param_value)) { |
|
105 trigger_error("[plugin] invalid header format '" . $param_value . "'", E_USER_NOTICE); |
|
106 |
|
107 return; |
|
108 } else { |
|
109 $extra_headers[] = $param_value; |
|
110 } |
|
111 } |
|
112 break; |
|
113 case "proxy_host": |
|
114 if (!empty($param_value)) { |
|
115 $proxy_host = $param_value; |
|
116 } |
|
117 break; |
|
118 case "proxy_port": |
|
119 if (!preg_match('!\D!', $param_value)) { |
|
120 $proxy_port = (int)$param_value; |
|
121 } else { |
|
122 trigger_error("[plugin] invalid value for attribute '" . $param_key . "'", E_USER_NOTICE); |
|
123 |
|
124 return; |
|
125 } |
|
126 break; |
|
127 case "agent": |
|
128 if (!empty($param_value)) { |
|
129 $agent = $param_value; |
|
130 } |
|
131 break; |
|
132 case "referer": |
|
133 if (!empty($param_value)) { |
|
134 $referer = $param_value; |
|
135 } |
|
136 break; |
|
137 case "timeout": |
|
138 if (!preg_match('!\D!', $param_value)) { |
|
139 $timeout = (int)$param_value; |
|
140 } else { |
|
141 trigger_error("[plugin] invalid value for attribute '" . $param_key . "'", E_USER_NOTICE); |
|
142 |
|
143 return; |
|
144 } |
|
145 break; |
|
146 default: |
|
147 trigger_error("[plugin] unrecognized attribute '" . $param_key . "'", E_USER_NOTICE); |
|
148 |
|
149 return; |
|
150 } |
|
151 } |
|
152 if (!empty($proxy_host) && !empty($proxy_port)) { |
|
153 $_is_proxy = true; |
|
154 $fp = fsockopen($proxy_host, $proxy_port, $errno, $errstr, $timeout); |
|
155 } else { |
|
156 $fp = fsockopen($server_name, $port, $errno, $errstr, $timeout); |
|
157 } |
|
158 |
|
159 if (!$fp) { |
|
160 trigger_error("[plugin] unable to fetch: $errstr ($errno)", E_USER_NOTICE); |
|
161 |
|
162 return; |
|
163 } else { |
|
164 if ($_is_proxy) { |
|
165 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n"); |
|
166 } else { |
|
167 fputs($fp, "GET $uri HTTP/1.0\r\n"); |
|
168 } |
|
169 if (!empty($host)) { |
|
170 fputs($fp, "Host: $host\r\n"); |
|
171 } |
|
172 if (!empty($accept)) { |
|
173 fputs($fp, "Accept: $accept\r\n"); |
|
174 } |
|
175 if (!empty($agent)) { |
|
176 fputs($fp, "User-Agent: $agent\r\n"); |
|
177 } |
|
178 if (!empty($referer)) { |
|
179 fputs($fp, "Referer: $referer\r\n"); |
|
180 } |
|
181 if (isset($extra_headers) && is_array($extra_headers)) { |
|
182 foreach ($extra_headers as $curr_header) { |
|
183 fputs($fp, $curr_header . "\r\n"); |
|
184 } |
|
185 } |
|
186 if (!empty($user) && !empty($pass)) { |
|
187 fputs($fp, "Authorization: BASIC " . base64_encode("$user:$pass") . "\r\n"); |
|
188 } |
|
189 |
|
190 fputs($fp, "\r\n"); |
|
191 while (!feof($fp)) { |
|
192 $content .= fgets($fp, 4096); |
|
193 } |
|
194 fclose($fp); |
|
195 $csplit = preg_split("!\r\n\r\n!", $content, 2); |
|
196 |
|
197 $content = $csplit[1]; |
|
198 |
|
199 if (!empty($params['assign_headers'])) { |
|
200 $template->assign($params['assign_headers'], preg_split("!\r\n!", $csplit[0])); |
|
201 } |
|
202 } |
|
203 } else { |
|
204 trigger_error("[plugin fetch] unable to parse URL, check syntax", E_USER_NOTICE); |
|
205 |
|
206 return; |
|
207 } |
|
208 } else { |
|
209 $content = @file_get_contents($params['file']); |
|
210 if ($content === false) { |
|
211 throw new SmartyException("{fetch} cannot read resource '" . $params['file'] . "'"); |
|
212 } |
|
213 } |
|
214 |
|
215 if (!empty($params['assign'])) { |
|
216 $template->assign($params['assign'], $content); |
|
217 } else { |
|
218 return $content; |
|
219 } |
|
220 } |
|