]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.fetch.php
Release 6.5.0
[Github/sugarcrm.git] / include / Smarty / plugins / function.fetch.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
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
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
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.
21
22 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
23
24 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
25
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
27
28 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
29
30
31 */
32
33
34 /**
35  * Smarty plugin
36  * @package Smarty
37  * @subpackage plugins
38  */
39
40
41 /**
42  * Smarty {fetch} plugin
43  *
44  * Type:     function<br>
45  * Name:     fetch<br>
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>
50  * @param array
51  * @param Smarty
52  * @return string|null if the assign parameter is passed, Smarty assigns the
53  *                     result to a template variable
54  */
55 function smarty_function_fetch($params, &$smarty)
56 {
57     if (empty($params['file'])) {
58         $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
59         return;
60     }
61
62     $content = '';
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');
68             return;
69         }
70         
71         // fetch the file
72         if($fp = @fopen($params['file'],'r')) {
73             while(!feof($fp)) {
74                 $content .= fgets ($fp,4096);
75             }
76             fclose($fp);
77         } else {
78             $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
79             return;
80         }
81     } else {
82         // not a local file
83         if(preg_match('!^http://!i',$params['file'])) {
84             // http fetch
85             if($uri_parts = parse_url($params['file'])) {
86                 // set defaults
87                 $host = $server_name = $uri_parts['host'];
88                 $timeout = 30;
89                 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
90                 $agent = "Smarty Template Engine ".$smarty->_version;
91                 $referer = "";
92                 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
93                 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
94                 $_is_proxy = false;
95                 if(empty($uri_parts['port'])) {
96                     $port = 80;
97                 } else {
98                     $port = $uri_parts['port'];
99                 }
100                 if(!empty($uri_parts['user'])) {
101                     $user = $uri_parts['user'];
102                 }
103                 if(!empty($uri_parts['pass'])) {
104                     $pass = $uri_parts['pass'];
105                 }
106                 // loop through parameters, setup headers
107                 foreach($params as $param_key => $param_value) {
108                     switch($param_key) {
109                         case "file":
110                         case "assign":
111                         case "assign_headers":
112                             break;
113                         case "user":
114                             if(!empty($param_value)) {
115                                 $user = $param_value;
116                             }
117                             break;
118                         case "pass":
119                             if(!empty($param_value)) {
120                                 $pass = $param_value;
121                             }
122                             break;
123                         case "accept":
124                             if(!empty($param_value)) {
125                                 $accept = $param_value;
126                             }
127                             break;
128                         case "header":
129                             if(!empty($param_value)) {
130                                 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
131                                     $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
132                                     return;
133                                 } else {
134                                     $extra_headers[] = $param_value;
135                                 }
136                             }
137                             break;
138                         case "proxy_host":
139                             if(!empty($param_value)) {
140                                 $proxy_host = $param_value;
141                             }
142                             break;
143                         case "proxy_port":
144                             if(!preg_match('!\D!', $param_value)) {
145                                 $proxy_port = (int) $param_value;
146                             } else {
147                                 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
148                                 return;
149                             }
150                             break;
151                         case "agent":
152                             if(!empty($param_value)) {
153                                 $agent = $param_value;
154                             }
155                             break;
156                         case "referer":
157                             if(!empty($param_value)) {
158                                 $referer = $param_value;
159                             }
160                             break;
161                         case "timeout":
162                             if(!preg_match('!\D!', $param_value)) {
163                                 $timeout = (int) $param_value;
164                             } else {
165                                 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
166                                 return;
167                             }
168                             break;
169                         default:
170                             $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
171                             return;
172                     }
173                 }
174                 if(!empty($proxy_host) && !empty($proxy_port)) {
175                     $_is_proxy = true;
176                     $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
177                 } else {
178                     $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
179                 }
180
181                 if(!$fp) {
182                     $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
183                     return;
184                 } else {
185                     if($_is_proxy) {
186                         fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
187                     } else {
188                         fputs($fp, "GET $uri HTTP/1.0\r\n");
189                     }
190                     if(!empty($host)) {
191                         fputs($fp, "Host: $host\r\n");
192                     }
193                     if(!empty($accept)) {
194                         fputs($fp, "Accept: $accept\r\n");
195                     }
196                     if(!empty($agent)) {
197                         fputs($fp, "User-Agent: $agent\r\n");
198                     }
199                     if(!empty($referer)) {
200                         fputs($fp, "Referer: $referer\r\n");
201                     }
202                     if(isset($extra_headers) && is_array($extra_headers)) {
203                         foreach($extra_headers as $curr_header) {
204                             fputs($fp, $curr_header."\r\n");
205                         }
206                     }
207                     if(!empty($user) && !empty($pass)) {
208                         fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
209                     }
210
211                     fputs($fp, "\r\n");
212                     while(!feof($fp)) {
213                         $content .= fgets($fp,4096);
214                     }
215                     fclose($fp);
216                     $csplit = explode("\r\n\r\n",$content,2);
217
218                     $content = $csplit[1];
219
220                     if(!empty($params['assign_headers'])) {
221                         $smarty->assign($params['assign_headers'],explode("\r\n",$csplit[0]));
222                     }
223                 }
224             } else {
225                 $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
226                 return;
227             }
228         } else {
229             // ftp fetch
230             if($fp = @fopen($params['file'],'r')) {
231                 while(!feof($fp)) {
232                     $content .= fgets ($fp,4096);
233                 }
234                 fclose($fp);
235             } else {
236                 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
237                 return;
238             }
239         }
240
241     }
242
243
244     if (!empty($params['assign'])) {
245         $smarty->assign($params['assign'],$content);
246     } else {
247         return $content;
248     }
249 }
250
251 /* vim: set expandtab: */
252
253 ?>