5 Modification information for LGPL compliance
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
17 r51443 - 2009-10-12 13:34:36 -0700 (Mon, 12 Oct 2009) - jmertic - Bug 33332 - Made application PHP 5.3 compliant with E_DEPRECATED warnings on by:
18 - Changing all ereg function to either preg or simple string based ones
19 - No more references to magic quotes.
20 - Change all the session_unregister() functions to just unset() the correct session variable instead.
22 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
24 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
26 r10971 - 2006-01-12 14:58:30 -0800 (Thu, 12 Jan 2006) - chris - Bug 4128: updating Smarty templates to 2.6.11, a version supposedly that plays better with PHP 5.1
28 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
42 * Smarty {fetch} plugin
46 * Purpose: fetch file, web or ftp data and display results
47 * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
48 * (Smarty online manual)
49 * @author Monte Ohrt <monte at ohrt dot com>
52 * @return string|null if the assign parameter is passed, Smarty assigns the
53 * result to a template variable
55 function smarty_function_fetch($params, &$smarty)
57 if (empty($params['file'])) {
58 $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
63 if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
64 $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
65 require_once(SMARTY_CORE_DIR . 'core.is_secure.php');
66 if(!smarty_core_is_secure($_params, $smarty)) {
67 $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
72 if($fp = @fopen($params['file'],'r')) {
74 $content .= fgets ($fp,4096);
78 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
83 if(preg_match('!^http://!i',$params['file'])) {
85 if($uri_parts = parse_url($params['file'])) {
87 $host = $server_name = $uri_parts['host'];
89 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
90 $agent = "Smarty Template Engine ".$smarty->_version;
92 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
93 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
95 if(empty($uri_parts['port'])) {
98 $port = $uri_parts['port'];
100 if(!empty($uri_parts['user'])) {
101 $user = $uri_parts['user'];
103 if(!empty($uri_parts['pass'])) {
104 $pass = $uri_parts['pass'];
106 // loop through parameters, setup headers
107 foreach($params as $param_key => $param_value) {
111 case "assign_headers":
114 if(!empty($param_value)) {
115 $user = $param_value;
119 if(!empty($param_value)) {
120 $pass = $param_value;
124 if(!empty($param_value)) {
125 $accept = $param_value;
129 if(!empty($param_value)) {
130 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
131 $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
134 $extra_headers[] = $param_value;
139 if(!empty($param_value)) {
140 $proxy_host = $param_value;
144 if(!preg_match('!\D!', $param_value)) {
145 $proxy_port = (int) $param_value;
147 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
152 if(!empty($param_value)) {
153 $agent = $param_value;
157 if(!empty($param_value)) {
158 $referer = $param_value;
162 if(!preg_match('!\D!', $param_value)) {
163 $timeout = (int) $param_value;
165 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
170 $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
174 if(!empty($proxy_host) && !empty($proxy_port)) {
176 $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
178 $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
182 $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
186 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
188 fputs($fp, "GET $uri HTTP/1.0\r\n");
191 fputs($fp, "Host: $host\r\n");
193 if(!empty($accept)) {
194 fputs($fp, "Accept: $accept\r\n");
197 fputs($fp, "User-Agent: $agent\r\n");
199 if(!empty($referer)) {
200 fputs($fp, "Referer: $referer\r\n");
202 if(isset($extra_headers) && is_array($extra_headers)) {
203 foreach($extra_headers as $curr_header) {
204 fputs($fp, $curr_header."\r\n");
207 if(!empty($user) && !empty($pass)) {
208 fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
213 $content .= fgets($fp,4096);
216 $csplit = explode("\r\n\r\n",$content,2);
218 $content = $csplit[1];
220 if(!empty($params['assign_headers'])) {
221 $smarty->assign($params['assign_headers'],explode("\r\n",$csplit[0]));
225 $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
230 if($fp = @fopen($params['file'],'r')) {
232 $content .= fgets ($fp,4096);
236 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
244 if (!empty($params['assign'])) {
245 $smarty->assign($params['assign'],$content);
251 /* vim: set expandtab: */