| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Smarty plugin | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * @package    Smarty | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |  * @subpackage PluginsFunction | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Smarty {fetch} plugin | 
					
						
							|  |  |  |  * Type:     function<br> | 
					
						
							|  |  |  |  * Name:     fetch<br> | 
					
						
							|  |  |  |  * Purpose:  fetch file, web or ftp data and display results | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * @link   http://www.smarty.net/manual/en/language.function.fetch.php {fetch} | 
					
						
							|  |  |  |  *         (Smarty online manual) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |  * @author Monte Ohrt <monte at ohrt dot com> | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @param array                    $params   parameters | 
					
						
							|  |  |  |  * @param Smarty_Internal_Template $template template object | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @throws SmartyException | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @return string|null if the assign parameter is passed, Smarty assigns the result to a template variable | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-11-12 23:42:32 +00:00
										 |  |  | function smarty_function_fetch($params, $template) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |     if (empty($params[ 'file' ])) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |         trigger_error("[plugin] fetch parameter 'file' cannot be empty", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |     // strip file protocol
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |     if (stripos($params[ 'file' ], 'file://') === 0) { | 
					
						
							|  |  |  |         $params[ 'file' ] = substr($params[ 'file' ], 7); | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |     $protocol = strpos($params[ 'file' ], '://'); | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |     if ($protocol !== false) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |         $protocol = strtolower(substr($params[ 'file' ], 0, $protocol)); | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |     if (isset($template->smarty->security_policy)) { | 
					
						
							|  |  |  |         if ($protocol) { | 
					
						
							|  |  |  |             // remote resource (or php stream, …)
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             if (!$template->smarty->security_policy->isTrustedUri($params[ 'file' ])) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             // local file
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             if (!$template->smarty->security_policy->isTrustedResourceDir($params[ 'file' ])) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $content = ''; | 
					
						
							|  |  |  |     if ($protocol == 'http') { | 
					
						
							|  |  |  |         // http fetch
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |         if ($uri_parts = parse_url($params[ 'file' ])) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             // set defaults
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             $host = $server_name = $uri_parts[ 'host' ]; | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             $timeout = 30; | 
					
						
							|  |  |  |             $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |             $agent = "Smarty Template Engine " . Smarty::SMARTY_VERSION; | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             $referer = ""; | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             $uri = !empty($uri_parts[ 'path' ]) ? $uri_parts[ 'path' ] : '/'; | 
					
						
							|  |  |  |             $uri .= !empty($uri_parts[ 'query' ]) ? '?' . $uri_parts[ 'query' ] : ''; | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             $_is_proxy = false; | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             if (empty($uri_parts[ 'port' ])) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                 $port = 80; | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                 $port = $uri_parts[ 'port' ]; | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             if (!empty($uri_parts[ 'user' ])) { | 
					
						
							|  |  |  |                 $user = $uri_parts[ 'user' ]; | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             if (!empty($uri_parts[ 'pass' ])) { | 
					
						
							|  |  |  |                 $pass = $uri_parts[ 'pass' ]; | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |             // loop through parameters, setup headers
 | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |             foreach ($params as $param_key => $param_value) { | 
					
						
							|  |  |  |                 switch ($param_key) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                     case "file": | 
					
						
							|  |  |  |                     case "assign": | 
					
						
							|  |  |  |                     case "assign_headers": | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case "user": | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                         if (!empty($param_value)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                             $user = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case "pass": | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                         if (!empty($param_value)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                             $pass = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case "accept": | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                         if (!empty($param_value)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                             $accept = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case "header": | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                         if (!empty($param_value)) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                             if (!preg_match('![\w\d-]+: .+!', $param_value)) { | 
					
						
							|  |  |  |                                 trigger_error("[plugin] invalid header format '" . $param_value . "'", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                                 return; | 
					
						
							|  |  |  |                             } else { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                                 $extra_headers[] = $param_value; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                             } | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case "proxy_host": | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                         if (!empty($param_value)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                             $proxy_host = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case "proxy_port": | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                         if (!preg_match('!\D!', $param_value)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                             $proxy_port = (int) $param_value; | 
					
						
							|  |  |  |                         } else { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                             trigger_error("[plugin] invalid value for attribute '" . $param_key . "'", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                             return; | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case "agent": | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                         if (!empty($param_value)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                             $agent = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case "referer": | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                         if (!empty($param_value)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                             $referer = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case "timeout": | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                         if (!preg_match('!\D!', $param_value)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                             $timeout = (int) $param_value; | 
					
						
							|  |  |  |                         } else { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                             trigger_error("[plugin] invalid value for attribute '" . $param_key . "'", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                             return; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     default: | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                         trigger_error("[plugin] unrecognized attribute '" . $param_key . "'", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                         return; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |             if (!empty($proxy_host) && !empty($proxy_port)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                 $_is_proxy = true; | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                 $fp = fsockopen($proxy_host, $proxy_port, $errno, $errstr, $timeout); | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                 $fp = fsockopen($server_name, $port, $errno, $errstr, $timeout); | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |             if (!$fp) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                 trigger_error("[plugin] unable to fetch: $errstr ($errno)", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                 if ($_is_proxy) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                     fputs($fp, 'GET ' . $params[ 'file' ] . " HTTP/1.0\r\n"); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                     fputs($fp, "GET $uri HTTP/1.0\r\n"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                 if (!empty($host)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                     fputs($fp, "Host: $host\r\n"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                 if (!empty($accept)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                     fputs($fp, "Accept: $accept\r\n"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                 if (!empty($agent)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                     fputs($fp, "User-Agent: $agent\r\n"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                 if (!empty($referer)) { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                     fputs($fp, "Referer: $referer\r\n"); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                 if (isset($extra_headers) && is_array($extra_headers)) { | 
					
						
							|  |  |  |                     foreach ($extra_headers as $curr_header) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                         fputs($fp, $curr_header . "\r\n"); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                 if (!empty($user) && !empty($pass)) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                     fputs($fp, "Authorization: BASIC " . base64_encode("$user:$pass") . "\r\n"); | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                 fputs($fp, "\r\n"); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |                 while (!feof($fp)) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                     $content .= fgets($fp, 4096); | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 fclose($fp); | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                 $csplit = preg_split("!\r\n\r\n!", $content, 2); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                 $content = $csplit[ 1 ]; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                 if (!empty($params[ 'assign_headers' ])) { | 
					
						
							|  |  |  |                     $template->assign($params[ 'assign_headers' ], preg_split("!\r\n!", $csplit[ 0 ])); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |             trigger_error("[plugin fetch] unable to parse URL, check syntax", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |         $content = @file_get_contents($params[ 'file' ]); | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |         if ($content === false) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             throw new SmartyException("{fetch} cannot read resource '" . $params[ 'file' ] . "'"); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |     if (!empty($params[ 'assign' ])) { | 
					
						
							|  |  |  |         $template->assign($params[ 'assign' ], $content); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         return $content; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |