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