|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
/**
|
|
Chris PeBenito |
696b41 |
* Smarty plugin
|
|
Chris PeBenito |
696b41 |
* @package Smarty
|
|
Chris PeBenito |
696b41 |
* @subpackage plugins
|
|
Chris PeBenito |
696b41 |
*/
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
/**
|
|
Chris PeBenito |
696b41 |
* Smarty {fetch} plugin
|
|
Chris PeBenito |
696b41 |
*
|
|
Chris PeBenito |
696b41 |
* Type: function
|
|
Chris PeBenito |
696b41 |
* Name: fetch
|
|
Chris PeBenito |
696b41 |
* Purpose: fetch file, web or ftp data and display results
|
|
Chris PeBenito |
696b41 |
* @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
|
|
Chris PeBenito |
696b41 |
* (Smarty online manual)
|
|
Chris PeBenito |
696b41 |
* @param array
|
|
Chris PeBenito |
696b41 |
* @param Smarty
|
|
Chris PeBenito |
696b41 |
* @return string|null if the assign parameter is passed, Smarty assigns the
|
|
Chris PeBenito |
696b41 |
* result to a template variable
|
|
Chris PeBenito |
696b41 |
*/
|
|
Chris PeBenito |
696b41 |
function smarty_function_fetch($params, &$smarty)
|
|
Chris PeBenito |
696b41 |
{
|
|
Chris PeBenito |
696b41 |
if (empty($params['file'])) {
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
$content = '';
|
|
Chris PeBenito |
696b41 |
if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
|
|
Chris PeBenito |
696b41 |
$_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
|
|
Chris PeBenito |
696b41 |
require_once(SMARTY_CORE_DIR . 'core.is_secure.php');
|
|
Chris PeBenito |
696b41 |
if(!smarty_core_is_secure($_params, $smarty)) {
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
// fetch the file
|
|
Chris PeBenito |
696b41 |
if($fp = @fopen($params['file'],'r')) {
|
|
Chris PeBenito |
696b41 |
while(!feof($fp)) {
|
|
Chris PeBenito |
696b41 |
$content .= fgets ($fp,4096);
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
fclose($fp);
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
// not a local file
|
|
Chris PeBenito |
696b41 |
if(preg_match('!^http://!i',$params['file'])) {
|
|
Chris PeBenito |
696b41 |
// http fetch
|
|
Chris PeBenito |
696b41 |
if($uri_parts = parse_url($params['file'])) {
|
|
Chris PeBenito |
696b41 |
// set defaults
|
|
Chris PeBenito |
696b41 |
$host = $server_name = $uri_parts['host'];
|
|
Chris PeBenito |
696b41 |
$timeout = 30;
|
|
Chris PeBenito |
696b41 |
$accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
|
|
Chris PeBenito |
696b41 |
$agent = "Smarty Template Engine ".$smarty->_version;
|
|
Chris PeBenito |
696b41 |
$referer = "";
|
|
Chris PeBenito |
696b41 |
$uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
|
|
Chris PeBenito |
696b41 |
$uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
|
|
Chris PeBenito |
696b41 |
$_is_proxy = false;
|
|
Chris PeBenito |
696b41 |
if(empty($uri_parts['port'])) {
|
|
Chris PeBenito |
696b41 |
$port = 80;
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
$port = $uri_parts['port'];
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
if(!empty($uri_parts['user'])) {
|
|
Chris PeBenito |
696b41 |
$user = $uri_parts['user'];
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
if(!empty($uri_parts['pass'])) {
|
|
Chris PeBenito |
696b41 |
$pass = $uri_parts['pass'];
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
// loop through parameters, setup headers
|
|
Chris PeBenito |
696b41 |
foreach($params as $param_key => $param_value) {
|
|
Chris PeBenito |
696b41 |
switch($param_key) {
|
|
Chris PeBenito |
696b41 |
case "file":
|
|
Chris PeBenito |
696b41 |
case "assign":
|
|
Chris PeBenito |
696b41 |
case "assign_headers":
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
case "user":
|
|
Chris PeBenito |
696b41 |
if(!empty($param_value)) {
|
|
Chris PeBenito |
696b41 |
$user = $param_value;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
case "pass":
|
|
Chris PeBenito |
696b41 |
if(!empty($param_value)) {
|
|
Chris PeBenito |
696b41 |
$pass = $param_value;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
case "accept":
|
|
Chris PeBenito |
696b41 |
if(!empty($param_value)) {
|
|
Chris PeBenito |
696b41 |
$accept = $param_value;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
case "header":
|
|
Chris PeBenito |
696b41 |
if(!empty($param_value)) {
|
|
Chris PeBenito |
696b41 |
if(!preg_match('![\w\d-]+: .+!',$param_value)) {
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
$extra_headers[] = $param_value;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
case "proxy_host":
|
|
Chris PeBenito |
696b41 |
if(!empty($param_value)) {
|
|
Chris PeBenito |
696b41 |
$proxy_host = $param_value;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
case "proxy_port":
|
|
Chris PeBenito |
696b41 |
if(!preg_match('!\D!', $param_value)) {
|
|
Chris PeBenito |
696b41 |
$proxy_port = (int) $param_value;
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
case "agent":
|
|
Chris PeBenito |
696b41 |
if(!empty($param_value)) {
|
|
Chris PeBenito |
696b41 |
$agent = $param_value;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
case "referer":
|
|
Chris PeBenito |
696b41 |
if(!empty($param_value)) {
|
|
Chris PeBenito |
696b41 |
$referer = $param_value;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
case "timeout":
|
|
Chris PeBenito |
696b41 |
if(!preg_match('!\D!', $param_value)) {
|
|
Chris PeBenito |
696b41 |
$timeout = (int) $param_value;
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
break;
|
|
Chris PeBenito |
696b41 |
default:
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
if(!empty($proxy_host) && !empty($proxy_port)) {
|
|
Chris PeBenito |
696b41 |
$_is_proxy = true;
|
|
Chris PeBenito |
696b41 |
$fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
$fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
if(!$fp) {
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
if($_is_proxy) {
|
|
Chris PeBenito |
696b41 |
fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
fputs($fp, "GET $uri HTTP/1.0\r\n");
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
if(!empty($host)) {
|
|
Chris PeBenito |
696b41 |
fputs($fp, "Host: $host\r\n");
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
if(!empty($accept)) {
|
|
Chris PeBenito |
696b41 |
fputs($fp, "Accept: $accept\r\n");
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
if(!empty($agent)) {
|
|
Chris PeBenito |
696b41 |
fputs($fp, "User-Agent: $agent\r\n");
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
if(!empty($referer)) {
|
|
Chris PeBenito |
696b41 |
fputs($fp, "Referer: $referer\r\n");
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
if(isset($extra_headers) && is_array($extra_headers)) {
|
|
Chris PeBenito |
696b41 |
foreach($extra_headers as $curr_header) {
|
|
Chris PeBenito |
696b41 |
fputs($fp, $curr_header."\r\n");
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
if(!empty($user) && !empty($pass)) {
|
|
Chris PeBenito |
696b41 |
fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
fputs($fp, "\r\n");
|
|
Chris PeBenito |
696b41 |
while(!feof($fp)) {
|
|
Chris PeBenito |
696b41 |
$content .= fgets($fp,4096);
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
fclose($fp);
|
|
Chris PeBenito |
696b41 |
$csplit = split("\r\n\r\n",$content,2);
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
$content = $csplit[1];
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
if(!empty($params['assign_headers'])) {
|
|
Chris PeBenito |
696b41 |
$smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
// ftp fetch
|
|
Chris PeBenito |
696b41 |
if($fp = @fopen($params['file'],'r')) {
|
|
Chris PeBenito |
696b41 |
while(!feof($fp)) {
|
|
Chris PeBenito |
696b41 |
$content .= fgets ($fp,4096);
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
fclose($fp);
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
$smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
|
|
Chris PeBenito |
696b41 |
return;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
if (!empty($params['assign'])) {
|
|
Chris PeBenito |
696b41 |
$smarty->assign($params['assign'],$content);
|
|
Chris PeBenito |
696b41 |
} else {
|
|
Chris PeBenito |
696b41 |
return $content;
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
}
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
/* vim: set expandtab: */
|
|
Chris PeBenito |
696b41 |
|
|
Chris PeBenito |
696b41 |
?>
|