082c3483cc3d614aafa8319d8f1ad3fb9a33bb48
[atutor.git] / mods / phpdoc2 / PhpDocumentor / phpDocumentor / Smarty-2.6.0 / libs / plugins / function.fetch.php
1 <?php\r
2 /**\r
3  * Smarty plugin\r
4  * @package Smarty\r
5  * @subpackage plugins\r
6  */\r
7 \r
8 \r
9 /**\r
10  * Smarty {fetch} plugin\r
11  *\r
12  * Type:     function<br>\r
13  * Name:     fetch<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
17  * @param array\r
18  * @param Smarty\r
19  * @return string|null if the assign parameter is passed, Smarty assigns the\r
20  *                     result to a template variable\r
21  */\r
22 function smarty_function_fetch($params, &$smarty)\r
23 {\r
24     if (empty($params['file'])) {\r
25         $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");\r
26         return;\r
27     }\r
28 \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
34             return;                     \r
35                 }\r
36                 \r
37                 // fetch the file\r
38                 if($fp = @fopen($params['file'],'r')) {\r
39                         while(!feof($fp)) {\r
40                                 $content .= fgets ($fp,4096);\r
41                         }\r
42                         fclose($fp);\r
43                 } else {\r
44             $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');\r
45             return;                     \r
46                 }\r
47     } else {\r
48                 // not a local file\r
49                 if(preg_match('!^http://!i',$params['file'])) {\r
50                         // http fetch\r
51                         if($uri_parts = parse_url($params['file'])) {\r
52                                 // set defaults\r
53                                 $host = $server_name = $uri_parts['host'];\r
54                                 $timeout = 30;\r
55                                 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";\r
56                                 $agent = "Smarty Template Engine ".$smarty->_version;\r
57                                 $referer = "";\r
58                                 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';\r
59                                 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';\r
60                                 $_is_proxy = false;\r
61                                 if(empty($uri_parts['port'])) {\r
62                                         $port = 80;\r
63                                 } else {\r
64                                         $port = $uri_parts['port'];\r
65                                 }\r
66                                 if(empty($uri_parts['user'])) {\r
67                                         $user = '';\r
68                                 }                               \r
69                                 // loop through parameters, setup headers\r
70                                 foreach($params as $param_key => $param_value) {                        \r
71                                         switch($param_key) {\r
72                                                 case "file":\r
73                                                 case "assign":\r
74                                                 case "assign_headers":\r
75                                                         break;\r
76                                                 case "user":\r
77                                                         if(!empty($param_value)) {\r
78                                                                 $user = $param_value;\r
79                                                         }\r
80                                                         break;\r
81                                                 case "pass":\r
82                                                         if(!empty($param_value)) {\r
83                                                                 $pass = $param_value;\r
84                                                         }\r
85                                                         break;\r
86                                                 case "accept":\r
87                                                         if(!empty($param_value)) {\r
88                                                                 $accept = $param_value;\r
89                                                         }\r
90                                                         break;\r
91                                                 case "header":\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
95                                                         return;                                                                 \r
96                                                                 } else {\r
97                                                                         $extra_headers[] = $param_value;\r
98                                                                 }\r
99                                                         }\r
100                                                         break;\r
101                                                 case "proxy_host":\r
102                                                         if(!empty($param_value)) {\r
103                                                                 $proxy_host = $param_value;\r
104                                                         }\r
105                                                         break;\r
106                                                 case "proxy_port":\r
107                                                         if(!preg_match('!\D!', $param_value)) {\r
108                                                                 $proxy_port = (int) $param_value;\r
109                                                         } else {\r
110                                                 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");\r
111                                                 return;                                                                 \r
112                                                         }\r
113                                                         break;\r
114                                                 case "agent":\r
115                                                         if(!empty($param_value)) {\r
116                                                                 $agent = $param_value;\r
117                                                         }\r
118                                                         break;\r
119                                                 case "referer":\r
120                                                         if(!empty($param_value)) {\r
121                                                                 $referer = $param_value;\r
122                                                         }\r
123                                                         break;\r
124                                                 case "timeout":\r
125                                                         if(!preg_match('!\D!', $param_value)) {\r
126                                                                 $timeout = (int) $param_value;\r
127                                                         } else {\r
128                                                 $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");\r
129                                                 return;                                                                 \r
130                                                         }\r
131                                                         break;\r
132                                                 default:\r
133                                         $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");\r
134                                         return;\r
135                                         }                       \r
136                                 }\r
137                                 if(!empty($proxy_host) && !empty($proxy_port)) {\r
138                                         $_is_proxy = true;\r
139                                         $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);\r
140                                 } else {\r
141                                         $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);\r
142                                 }\r
143 \r
144                                 if(!$fp) {\r
145                         $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");\r
146                         return;                         \r
147                                 } else {\r
148                                         if($_is_proxy) {\r
149                                                 fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");                                         \r
150                                         } else {\r
151                                                 fputs($fp, "GET $uri HTTP/1.0\r\n");\r
152                                         }\r
153                                         if(!empty($host)) {\r
154                                                 fputs($fp, "Host: $host\r\n");\r
155                                         }\r
156                                         if(!empty($accept)) {\r
157                                                 fputs($fp, "Accept: $accept\r\n");\r
158                                         }\r
159                                         if(!empty($agent)) {\r
160                                                 fputs($fp, "User-Agent: $agent\r\n");\r
161                                         }\r
162                                         if(!empty($referer)) {\r
163                                                 fputs($fp, "Referer: $referer\r\n");\r
164                                         }\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
168                                                 }\r
169                                         }\r
170                                         if(!empty($user) && !empty($pass)) {\r
171                                                 fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");                                                \r
172                                         }\r
173 \r
174                                         $content = '';                                  \r
175                                         fputs($fp, "\r\n");\r
176                                         while(!feof($fp)) {\r
177                                                 $content .= fgets($fp,4096);\r
178                                         }\r
179                                         fclose($fp);                                    \r
180                                         $csplit = split("\r\n\r\n",$content,2);\r
181 \r
182                                         $content = $csplit[1];\r
183                                         \r
184                                         if(!empty($params['assign_headers'])) {\r
185                                                 $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));\r
186                                         }\r
187                                 }\r
188                         } else {\r
189                         $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");\r
190                         return;\r
191                         }\r
192                 } else {\r
193                         // ftp fetch\r
194                         if($fp = @fopen($params['file'],'r')) {\r
195                                 while(!feof($fp)) {\r
196                                         $content .= fgets ($fp,4096);\r
197                                 }\r
198                                 fclose($fp);\r
199                         } else {\r
200                 $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');\r
201                 return;                 \r
202                         }\r
203                 }\r
204                 \r
205         }\r
206 \r
207 \r
208     if (!empty($params['assign'])) {\r
209         $smarty->assign($params['assign'],$content);\r
210     } else {\r
211         return $content;\r
212     }\r
213 }\r
214 \r
215 /* vim: set expandtab: */\r
216 \r
217 ?>\r