khanat-opennel-code/code/web/private_php/ams/smarty/libs/plugins/function.fetch.php

222 lines
7.9 KiB
PHP
Raw Normal View History

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