Added Canvas 1.1.0, originally not under SCM so no historical development records...
[canvas.git] / library / smarty / plugins / function.fetch.php
blob0d5ce7f7591134a5eb46f843b2595f7d2d5ebfa0
1 <?php
2 /**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage plugins
6 */
9 /**
10 * Smarty {fetch} plugin
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
19 * @param Smarty
20 * @return string|null if the assign parameter is passed, Smarty assigns the
21 * result to a template variable
23 function smarty_function_fetch($params, &$smarty)
25 if (empty($params['file'])) {
26 $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
27 return;
30 $content = '';
31 if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
32 $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
33 require_once(SMARTY_CORE_DIR . 'core.is_secure.php');
34 if(!smarty_core_is_secure($_params, $smarty)) {
35 $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
36 return;
39 // fetch the file
40 if($fp = @fopen($params['file'],'r')) {
41 while(!feof($fp)) {
42 $content .= fgets ($fp,4096);
44 fclose($fp);
45 } else {
46 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
47 return;
49 } else {
50 // not a local file
51 if(preg_match('!^http://!i',$params['file'])) {
52 // http fetch
53 if($uri_parts = parse_url($params['file'])) {
54 // set defaults
55 $host = $server_name = $uri_parts['host'];
56 $timeout = 30;
57 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
58 $agent = "Smarty Template Engine ".$smarty->_version;
59 $referer = "";
60 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
61 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
62 $_is_proxy = false;
63 if(empty($uri_parts['port'])) {
64 $port = 80;
65 } else {
66 $port = $uri_parts['port'];
68 if(!empty($uri_parts['user'])) {
69 $user = $uri_parts['user'];
71 if(!empty($uri_parts['pass'])) {
72 $pass = $uri_parts['pass'];
74 // loop through parameters, setup headers
75 foreach($params as $param_key => $param_value) {
76 switch($param_key) {
77 case "file":
78 case "assign":
79 case "assign_headers":
80 break;
81 case "user":
82 if(!empty($param_value)) {
83 $user = $param_value;
85 break;
86 case "pass":
87 if(!empty($param_value)) {
88 $pass = $param_value;
90 break;
91 case "accept":
92 if(!empty($param_value)) {
93 $accept = $param_value;
95 break;
96 case "header":
97 if(!empty($param_value)) {
98 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
99 $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
100 return;
101 } else {
102 $extra_headers[] = $param_value;
105 break;
106 case "proxy_host":
107 if(!empty($param_value)) {
108 $proxy_host = $param_value;
110 break;
111 case "proxy_port":
112 if(!preg_match('!\D!', $param_value)) {
113 $proxy_port = (int) $param_value;
114 } else {
115 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
116 return;
118 break;
119 case "agent":
120 if(!empty($param_value)) {
121 $agent = $param_value;
123 break;
124 case "referer":
125 if(!empty($param_value)) {
126 $referer = $param_value;
128 break;
129 case "timeout":
130 if(!preg_match('!\D!', $param_value)) {
131 $timeout = (int) $param_value;
132 } else {
133 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
134 return;
136 break;
137 default:
138 $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
139 return;
142 if(!empty($proxy_host) && !empty($proxy_port)) {
143 $_is_proxy = true;
144 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
145 } else {
146 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
149 if(!$fp) {
150 $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
151 return;
152 } else {
153 if($_is_proxy) {
154 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
155 } else {
156 fputs($fp, "GET $uri HTTP/1.0\r\n");
158 if(!empty($host)) {
159 fputs($fp, "Host: $host\r\n");
161 if(!empty($accept)) {
162 fputs($fp, "Accept: $accept\r\n");
164 if(!empty($agent)) {
165 fputs($fp, "User-Agent: $agent\r\n");
167 if(!empty($referer)) {
168 fputs($fp, "Referer: $referer\r\n");
170 if(isset($extra_headers) && is_array($extra_headers)) {
171 foreach($extra_headers as $curr_header) {
172 fputs($fp, $curr_header."\r\n");
175 if(!empty($user) && !empty($pass)) {
176 fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
179 fputs($fp, "\r\n");
180 while(!feof($fp)) {
181 $content .= fgets($fp,4096);
183 fclose($fp);
184 $csplit = split("\r\n\r\n",$content,2);
186 $content = $csplit[1];
188 if(!empty($params['assign_headers'])) {
189 $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
192 } else {
193 $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
194 return;
196 } else {
197 // ftp fetch
198 if($fp = @fopen($params['file'],'r')) {
199 while(!feof($fp)) {
200 $content .= fgets ($fp,4096);
202 fclose($fp);
203 } else {
204 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
205 return;
212 if (!empty($params['assign'])) {
213 $smarty->assign($params['assign'],$content);
214 } else {
215 return $content;
219 /* vim: set expandtab: */