Source for file function.fetch.php
Documentation is available at function.fetch.php
* Purpose: fetch file, web or ftp data and display results
* @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
* @return string|nullif the assign parameter is passed, Smarty assigns the
* result to a template variable
if (empty($params['file'])) {
$smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
$_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.is_secure.php');
$smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
if($fp = @fopen($params['file'],'r')) {
$content .= fgets ($fp,4096);
$smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
if($uri_parts = parse_url($params['file'])) {
$host = $server_name = $uri_parts['host'];
$accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
$agent = "Smarty Template Engine ". $smarty->_version;
$uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
$uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
if(empty($uri_parts['port'])) {
$port = $uri_parts['port'];
if(empty($uri_parts['user'])) {
// loop through parameters, setup headers
foreach($params as $param_key => $param_value) {
if(!empty($param_value)) {
if(!empty($param_value)) {
if(!empty($param_value)) {
if(!empty($param_value)) {
$smarty->_trigger_fatal_error("[plugin] invalid header format '". $param_value. "'");
$extra_headers[] = $param_value;
if(!empty($param_value)) {
$proxy_host = $param_value;
$proxy_port = (int) $param_value;
$smarty->_trigger_fatal_error("[plugin] invalid value for attribute '". $param_key. "'");
if(!empty($param_value)) {
if(!empty($param_value)) {
$timeout = (int) $param_value;
$smarty->_trigger_fatal_error("[plugin] invalid value for attribute '". $param_key. "'");
$smarty->_trigger_fatal_error("[plugin] unrecognized attribute '". $param_key. "'");
if(!empty($proxy_host) && !empty($proxy_port)) {
$fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
$fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
$smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
fputs($fp, "GET $uri HTTP/1.0\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Accept: $accept\r\n");
fputs($fp, "User-Agent: $agent\r\n");
fputs($fp, "Referer: $referer\r\n");
if(isset ($extra_headers) && is_array($extra_headers)) {
foreach($extra_headers as $curr_header) {
fputs($fp, $curr_header. "\r\n");
if(!empty($user) && !empty($pass)) {
$content .= fgets($fp,4096);
$csplit = split("\r\n\r\n",$content,2);
if(!empty($params['assign_headers'])) {
$smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
$smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
if($fp = @fopen($params['file'],'r')) {
$content .= fgets ($fp,4096);
$smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
if (!empty($params['assign'])) {
$smarty->assign($params['assign'],$content);
/* vim: set expandtab: */
|