fb5daec20557346b3cfc3448241cb0f20efad7ae
[mirrors/Kyberia-bloodline.git] / wwwroot / smarty / libs / plugins / function.fetch.php
1 <?php
2 /**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage plugins
6 */
7
8
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 * @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 */
22 function smarty_function_fetch($params, &$smarty)
23 {
24 if (empty($params['file'])) {
25 $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
26 return;
27 }
28
29 $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_CORE_DIR . '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 }
43 fclose($fp);
44 } else {
45 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
46 return;
47 }
48 } else {
49 // 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 = $uri_parts['user'];
69 }
70 if(!empty($uri_parts['pass'])) {
71 $pass = $uri_parts['pass'];
72 }
73 // loop through parameters, setup headers
74 foreach($params as $param_key => $param_value) {
75 switch($param_key) {
76 case "file":
77 case "assign":
78 case "assign_headers":
79 break;
80 case "user":
81 if(!empty($param_value)) {
82 $user = $param_value;
83 }
84 break;
85 case "pass":
86 if(!empty($param_value)) {
87 $pass = $param_value;
88 }
89 break;
90 case "accept":
91 if(!empty($param_value)) {
92 $accept = $param_value;
93 }
94 break;
95 case "header":
96 if(!empty($param_value)) {
97 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
98 $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
99 return;
100 } else {
101 $extra_headers[] = $param_value;
102 }
103 }
104 break;
105 case "proxy_host":
106 if(!empty($param_value)) {
107 $proxy_host = $param_value;
108 }
109 break;
110 case "proxy_port":
111 if(!preg_match('!\D!', $param_value)) {
112 $proxy_port = (int) $param_value;
113 } else {
114 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
115 return;
116 }
117 break;
118 case "agent":
119 if(!empty($param_value)) {
120 $agent = $param_value;
121 }
122 break;
123 case "referer":
124 if(!empty($param_value)) {
125 $referer = $param_value;
126 }
127 break;
128 case "timeout":
129 if(!preg_match('!\D!', $param_value)) {
130 $timeout = (int) $param_value;
131 } else {
132 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
133 return;
134 }
135 break;
136 default:
137 $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
138 return;
139 }
140 }
141 if(!empty($proxy_host) && !empty($proxy_port)) {
142 $_is_proxy = true;
143 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
144 } else {
145 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
146 }
147
148 if(!$fp) {
149 $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
150 return;
151 } else {
152 if($_is_proxy) {
153 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
154 } else {
155 fputs($fp, "GET $uri HTTP/1.0\r\n");
156 }
157 if(!empty($host)) {
158 fputs($fp, "Host: $host\r\n");
159 }
160 if(!empty($accept)) {
161 fputs($fp, "Accept: $accept\r\n");
162 }
163 if(!empty($agent)) {
164 fputs($fp, "User-Agent: $agent\r\n");
165 }
166 if(!empty($referer)) {
167 fputs($fp, "Referer: $referer\r\n");
168 }
169 if(isset($extra_headers) && is_array($extra_headers)) {
170 foreach($extra_headers as $curr_header) {
171 fputs($fp, $curr_header."\r\n");
172 }
173 }
174 if(!empty($user) && !empty($pass)) {
175 fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
176 }
177
178 fputs($fp, "\r\n");
179 while(!feof($fp)) {
180 $content .= fgets($fp,4096);
181 }
182 fclose($fp);
183 $csplit = split("\r\n\r\n",$content,2);
184
185 $content = $csplit[1];
186
187 if(!empty($params['assign_headers'])) {
188 $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
189 }
190 }
191 } else {
192 $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
193 return;
194 }
195 } else {
196 // ftp fetch
197 if($fp = @fopen($params['file'],'r')) {
198 while(!feof($fp)) {
199 $content .= fgets ($fp,4096);
200 }
201 fclose($fp);
202 } else {
203 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
204 return;
205 }
206 }
207
208 }
209
210
211 if (!empty($params['assign'])) {
212 $smarty->assign($params['assign'],$content);
213 } else {
214 return $content;
215 }
216 }
217
218 /* vim: set expandtab: */
219
220 ?>
This page took 0.403432 seconds and 3 git commands to generate.