10 * Smarty {fetch} plugin
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>
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");
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');
40 if($fp = @fopen
($params['file'],'r')) {
42 $content .= fgets ($fp,4096);
46 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
51 if(preg_match('!^http://!i',$params['file'])) {
53 if($uri_parts = parse_url($params['file'])) {
55 $host = $server_name = $uri_parts['host'];
57 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
58 $agent = "Smarty Template Engine ".$smarty->_version
;
60 $uri = !empty($uri_parts['path']) ?
$uri_parts['path'] : '/';
61 $uri .= !empty($uri_parts['query']) ?
'?' . $uri_parts['query'] : '';
63 if(empty($uri_parts['port'])) {
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) {
79 case "assign_headers":
82 if(!empty($param_value)) {
87 if(!empty($param_value)) {
92 if(!empty($param_value)) {
93 $accept = $param_value;
97 if(!empty($param_value)) {
98 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
99 $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
102 $extra_headers[] = $param_value;
107 if(!empty($param_value)) {
108 $proxy_host = $param_value;
112 if(!preg_match('!\D!', $param_value)) {
113 $proxy_port = (int) $param_value;
115 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
120 if(!empty($param_value)) {
121 $agent = $param_value;
125 if(!empty($param_value)) {
126 $referer = $param_value;
130 if(!preg_match('!\D!', $param_value)) {
131 $timeout = (int) $param_value;
133 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
138 $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
142 if(!empty($proxy_host) && !empty($proxy_port)) {
144 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
146 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
150 $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
154 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
156 fputs($fp, "GET $uri HTTP/1.0\r\n");
159 fputs($fp, "Host: $host\r\n");
161 if(!empty($accept)) {
162 fputs($fp, "Accept: $accept\r\n");
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");
181 $content .= fgets($fp,4096);
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]));
193 $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
198 if($fp = @fopen
($params['file'],'r')) {
200 $content .= fgets ($fp,4096);
204 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
212 if (!empty($params['assign'])) {
213 $smarty->assign($params['assign'],$content);
219 /* vim: set expandtab: */
This page took 3.19738 seconds and 4 git commands to generate.