5 * @subpackage plugins
\r
10 * Smarty {fetch} plugin
\r
12 * Type: function<br>
\r
14 * Purpose: fetch file, web or ftp data and display results
\r
15 * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
\r
16 * (Smarty online manual)
\r
19 * @return string|null if the assign parameter is passed, Smarty assigns the
\r
20 * result to a template variable
\r
22 function smarty_function_fetch($params, &$smarty)
\r
24 if (empty($params['file'])) {
\r
25 $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
\r
29 if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
\r
30 $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
\r
31 require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.is_secure.php');
\r
32 if(!smarty_core_is_secure($_params, $smarty)) {
\r
33 $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
\r
38 if($fp = @fopen($params['file'],'r')) {
\r
40 $content .= fgets ($fp,4096);
\r
44 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
\r
49 if(preg_match('!^http://!i',$params['file'])) {
\r
51 if($uri_parts = parse_url($params['file'])) {
\r
53 $host = $server_name = $uri_parts['host'];
\r
55 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
\r
56 $agent = "Smarty Template Engine ".$smarty->_version;
\r
58 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
\r
59 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
\r
61 if(empty($uri_parts['port'])) {
\r
64 $port = $uri_parts['port'];
\r
66 if(empty($uri_parts['user'])) {
\r
69 // loop through parameters, setup headers
\r
70 foreach($params as $param_key => $param_value) {
\r
71 switch($param_key) {
\r
74 case "assign_headers":
\r
77 if(!empty($param_value)) {
\r
78 $user = $param_value;
\r
82 if(!empty($param_value)) {
\r
83 $pass = $param_value;
\r
87 if(!empty($param_value)) {
\r
88 $accept = $param_value;
\r
92 if(!empty($param_value)) {
\r
93 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
\r
94 $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
\r
97 $extra_headers[] = $param_value;
\r
102 if(!empty($param_value)) {
\r
103 $proxy_host = $param_value;
\r
107 if(!preg_match('!\D!', $param_value)) {
\r
108 $proxy_port = (int) $param_value;
\r
110 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
\r
115 if(!empty($param_value)) {
\r
116 $agent = $param_value;
\r
120 if(!empty($param_value)) {
\r
121 $referer = $param_value;
\r
125 if(!preg_match('!\D!', $param_value)) {
\r
126 $timeout = (int) $param_value;
\r
128 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
\r
133 $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
\r
137 if(!empty($proxy_host) && !empty($proxy_port)) {
\r
139 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
\r
141 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
\r
145 $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
\r
149 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
\r
151 fputs($fp, "GET $uri HTTP/1.0\r\n");
\r
153 if(!empty($host)) {
\r
154 fputs($fp, "Host: $host\r\n");
\r
156 if(!empty($accept)) {
\r
157 fputs($fp, "Accept: $accept\r\n");
\r
159 if(!empty($agent)) {
\r
160 fputs($fp, "User-Agent: $agent\r\n");
\r
162 if(!empty($referer)) {
\r
163 fputs($fp, "Referer: $referer\r\n");
\r
165 if(isset($extra_headers) && is_array($extra_headers)) {
\r
166 foreach($extra_headers as $curr_header) {
\r
167 fputs($fp, $curr_header."\r\n");
\r
170 if(!empty($user) && !empty($pass)) {
\r
171 fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
\r
175 fputs($fp, "\r\n");
\r
176 while(!feof($fp)) {
\r
177 $content .= fgets($fp,4096);
\r
180 $csplit = split("\r\n\r\n",$content,2);
\r
182 $content = $csplit[1];
\r
184 if(!empty($params['assign_headers'])) {
\r
185 $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
\r
189 $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
\r
194 if($fp = @fopen($params['file'],'r')) {
\r
195 while(!feof($fp)) {
\r
196 $content .= fgets ($fp,4096);
\r
200 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
\r
208 if (!empty($params['assign'])) {
\r
209 $smarty->assign($params['assign'],$content);
\r
215 /* vim: set expandtab: */
\r