| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2017-11-11 07:11:33 +01:00
										 |  |  |  * Type:     function
 | 
					
						
							|  |  |  |  * Name:     fetch | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |  * Purpose:  fetch file, web or ftp data and display results | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-10-13 12:15:17 +02:00
										 |  |  |  * @link   https://www.smarty.net/manual/en/language.function.fetch.php {fetch} | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  *         (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' ])) { | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |         trigger_error('[plugin] fetch parameter \'file\' cannot be empty', E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  |     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 = ''; | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |     if ($protocol === 'http') { | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |         // 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; | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |             $accept = 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*'; | 
					
						
							| 
									
										
										
										
											2022-11-30 00:25:27 +01:00
										 |  |  |             $agent = 'Smarty Template Engine ' . \Smarty\Smarty::SMARTY_VERSION; | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01: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) { | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                     case 'file': | 
					
						
							|  |  |  |                     case 'assign': | 
					
						
							|  |  |  |                     case 'assign_headers': | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 'user': | 
					
						
							|  |  |  |                         if (!empty($param_value)) { | 
					
						
							|  |  |  |                             $user = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 'pass': | 
					
						
							|  |  |  |                         if (!empty($param_value)) { | 
					
						
							|  |  |  |                             $pass = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 'accept': | 
					
						
							|  |  |  |                         if (!empty($param_value)) { | 
					
						
							|  |  |  |                             $accept = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 'header': | 
					
						
							|  |  |  |                         if (!empty($param_value)) { | 
					
						
							|  |  |  |                             if (!preg_match('![\w\d-]+: .+!', $param_value)) { | 
					
						
							|  |  |  |                                 trigger_error("[plugin] invalid header format '{$param_value}'", E_USER_NOTICE); | 
					
						
							|  |  |  |                                 return; | 
					
						
							|  |  |  |                             } else { | 
					
						
							|  |  |  |                                 $extra_headers[] = $param_value; | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 'proxy_host': | 
					
						
							|  |  |  |                         if (!empty($param_value)) { | 
					
						
							|  |  |  |                             $proxy_host = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 'proxy_port': | 
					
						
							|  |  |  |                         if (!preg_match('!\D!', $param_value)) { | 
					
						
							|  |  |  |                             $proxy_port = (int)$param_value; | 
					
						
							|  |  |  |                         } else { | 
					
						
							|  |  |  |                             trigger_error("[plugin] invalid value for attribute '{$param_key }'", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                             return; | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 'agent': | 
					
						
							|  |  |  |                         if (!empty($param_value)) { | 
					
						
							|  |  |  |                             $agent = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 'referer': | 
					
						
							|  |  |  |                         if (!empty($param_value)) { | 
					
						
							|  |  |  |                             $referer = $param_value; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 'timeout': | 
					
						
							|  |  |  |                         if (!preg_match('!\D!', $param_value)) { | 
					
						
							|  |  |  |                             $timeout = (int)$param_value; | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                         } else { | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                             trigger_error("[plugin] invalid value for attribute '{$param_key}'", E_USER_NOTICE); | 
					
						
							|  |  |  |                             return; | 
					
						
							| 
									
										
										
										
											2011-12-18 22:21:49 +00:00
										 |  |  |                         } | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                         break; | 
					
						
							|  |  |  |                     default: | 
					
						
							|  |  |  |                         trigger_error("[plugin] unrecognized attribute '{$param_key}'", E_USER_NOTICE); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											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)) { | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                     fputs($fp, 'Authorization: BASIC ' . base64_encode("$user:$pass") . "\r\n"); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                 $content = $csplit[ 1 ]; | 
					
						
							|  |  |  |                 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); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |