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