25 if (empty($params[
'file'])) {
26 trigger_error(
"[plugin] fetch parameter 'file' cannot be empty",E_USER_NOTICE);
31 if (stripos($params[
'file'],
'file://') === 0) {
32 $params[
'file'] = substr($params[
'file'], 7);
35 $protocol = strpos($params[
'file'],
'://');
36 if ($protocol !==
false) {
37 $protocol = strtolower(substr($params[
'file'], 0, $protocol));
40 if (isset($template->smarty->security_policy)) {
43 if(!$template->smarty->security_policy->isTrustedUri($params[
'file'])) {
48 if(!$template->smarty->security_policy->isTrustedResourceDir($params[
'file'])) {
55 if ($protocol ==
'http') {
57 if($uri_parts = parse_url($params[
'file'])) {
59 $host = $server_name = $uri_parts[
'host'];
61 $accept =
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
64 $uri = !empty($uri_parts[
'path']) ? $uri_parts[
'path'] :
'/';
65 $uri .= !empty($uri_parts[
'query']) ?
'?' . $uri_parts[
'query'] :
'';
67 if(empty($uri_parts[
'port'])) {
70 $port = $uri_parts[
'port'];
72 if(!empty($uri_parts[
'user'])) {
73 $user = $uri_parts[
'user'];
75 if(!empty($uri_parts[
'pass'])) {
76 $pass = $uri_parts[
'pass'];
79 foreach($params as $param_key => $param_value) {
83 case "assign_headers":
86 if(!empty($param_value)) {
91 if(!empty($param_value)) {
96 if(!empty($param_value)) {
97 $accept = $param_value;
101 if(!empty($param_value)) {
102 if(!preg_match(
'![\w\d-]+: .+!',$param_value)) {
103 trigger_error(
"[plugin] invalid header format '".$param_value.
"'",E_USER_NOTICE);
106 $extra_headers[] = $param_value;
111 if(!empty($param_value)) {
112 $proxy_host = $param_value;
116 if(!preg_match(
'!\D!', $param_value)) {
117 $proxy_port = (int) $param_value;
119 trigger_error(
"[plugin] invalid value for attribute '".$param_key.
"'",E_USER_NOTICE);
124 if(!empty($param_value)) {
125 $agent = $param_value;
129 if(!empty($param_value)) {
130 $referer = $param_value;
134 if(!preg_match(
'!\D!', $param_value)) {
135 $timeout = (int) $param_value;
137 trigger_error(
"[plugin] invalid value for attribute '".$param_key.
"'",E_USER_NOTICE);
142 trigger_error(
"[plugin] unrecognized attribute '".$param_key.
"'",E_USER_NOTICE);
146 if(!empty($proxy_host) && !empty($proxy_port)) {
148 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
150 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
154 trigger_error(
"[plugin] unable to fetch: $errstr ($errno)",E_USER_NOTICE);
158 fputs(
$fp,
'GET ' . $params[
'file'] .
" HTTP/1.0\r\n");
160 fputs(
$fp,
"GET $uri HTTP/1.0\r\n");
163 fputs(
$fp,
"Host: $host\r\n");
165 if(!empty($accept)) {
166 fputs(
$fp,
"Accept: $accept\r\n");
169 fputs(
$fp,
"User-Agent: $agent\r\n");
171 if(!empty($referer)) {
172 fputs(
$fp,
"Referer: $referer\r\n");
174 if(isset($extra_headers) && is_array($extra_headers)) {
175 foreach($extra_headers as $curr_header) {
176 fputs(
$fp, $curr_header.
"\r\n");
179 if(!empty($user) && !empty($pass)) {
180 fputs(
$fp,
"Authorization: BASIC ".base64_encode(
"$user:$pass").
"\r\n");
185 $content .= fgets(
$fp,4096);
188 $csplit = preg_split(
"!\r\n\r\n!",$content,2);
190 $content = $csplit[1];
192 if(!empty($params[
'assign_headers'])) {
193 $template->assign($params[
'assign_headers'],preg_split(
"!\r\n!",$csplit[0]));
197 trigger_error(
"[plugin fetch] unable to parse URL, check syntax",E_USER_NOTICE);
201 $content = @file_get_contents($params[
'file']);
202 if ($content ===
false) {
203 throw new SmartyException(
"{fetch} cannot read resource '" . $params[
'file'] .
"'");
207 if (!empty($params[
'assign'])) {
208 $template->assign($params[
'assign'], $content);
smarty_function_fetch($params, $template)
Smarty {fetch} plugin.
const SMARTY_VERSION
#@+ constant definitions