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