From 0384c2cfdb8988144e73be904dba800f60b34b15 Mon Sep 17 00:00:00 2001 From: mohrt Date: Wed, 13 Mar 2002 23:14:26 +0000 Subject: [PATCH] update fetch function with new parameters --- libs/plugins/function.fetch.php | 189 ++++++++++++++++++++++++++++++-- plugins/function.fetch.php | 189 ++++++++++++++++++++++++++++++-- 2 files changed, 354 insertions(+), 24 deletions(-) diff --git a/libs/plugins/function.fetch.php b/libs/plugins/function.fetch.php index e86408c6..ee46c160 100644 --- a/libs/plugins/function.fetch.php +++ b/libs/plugins/function.fetch.php @@ -10,15 +10,15 @@ */ function smarty_function_fetch($params, &$smarty) { - extract($params); + $file = $params['file']; if (empty($file)) { $smarty->trigger_error("parameter 'file' cannot be empty"); return; } - if ($smarty->security && !preg_match('!^(http|ftp)://!', $file)) { - // make sure fetched file comes from secure directory + if ($smarty->security && !preg_match('!^(http|ftp)://!i', $file)) { + // fetching file, make sure it comes from secure directory foreach ($smarty->secure_dir as $curr_dir) { if (substr(realpath($file), 0, strlen(realpath($curr_dir))) == realpath($curr_dir)) { $resource_is_secure = true; @@ -33,16 +33,181 @@ function smarty_function_fetch($params, &$smarty) $smarty->trigger_error("fetch cannot read file '$file'"); return; } - } - - - if (!empty($assign)) { - ob_start(); - readfile($file); - $smarty->assign($assign,ob_get_contents()); - ob_end_clean(); + // fetch the file + $fp = fopen($file,'r'); + while(!feof($fp)) { + $content .= fgets ($fp,4096); + } + fclose($fp); } else { - readfile($file); + // not a local file + if(preg_match('!^http://!i',$file)) { + // http fetch + if($uri_parts = parse_url($file)) { + // set defaults + $host = $server_name = $uri_parts['host']; + $timeout = 30; + $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; + $agent = "Smarty Template Engine ".$smarty->_version; + $referer = $_SERVER['REFERER']; + if(!empty($uri_parts['path'])) { + $uri = $uri_parts['path']; + } else { + $uri = '/'; + } + $_is_proxy = false; + if(empty($uri_parts['port'])) { + $port = 80; + } else { + $port = $uri_parts['port']; + } + if(empty($uri_parts['user'])) { + $user = $uri_parts['user']; + } + // loop through parameters, setup headers + foreach($params as $param_key => $param_value) { + switch($param_key) { + 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)) { + $smarty->trigger_error("invalid header format '".$param_value."'"); + 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 { + $smarty->trigger_error("invalid value for attribute '".$param_key."'"); + return; + } + 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; + } else { + $smarty->trigger_error("invalid value for attribute '".$param_key."'"); + return; + } + break; + case "cluster_host": + if(!empty($param_value) + && (gethostbyname($param_value) != $param_value)) { + $server_name = $param_value; + } + break; + default: + $smarty->trigger_error("unrecognized attribute '".$param_key."'"); + return; + } + } + if(!empty($proxy_host) && !empty($proxy_port)) { + $_is_proxy = true; + $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout); + } else { + $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout); + } + + if(!$fp) { + $smarty->trigger_error("unable to fetch: $errstr ($errno)"); + return; + } else { + if($_is_proxy) { + fputs($fp, "GET $file HTTP/1.0\r\n"); + } else { + fputs($fp, "GET $uri HTTP/1.0\r\n"); + } + if(!empty($host)) { + fputs($fp, "Host: $host\r\n"); + } + if(!empty($accept)) { + fputs($fp, "Accept: $accept\r\n"); + } + if(!empty($agent)) { + fputs($fp, "User-Agent: $agent\r\n"); + } + if(!empty($referer)) { + fputs($fp, "Referer: $referer\r\n"); + } + if(is_array($extra_headers)) { + foreach($extra_headers as $curr_header) { + fputs($fp, $curr_header."\r\n"); + } + } + if(!empty($user) && !empty($pass)) { + fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n"); + } + + fputs($fp, "\r\n"); + while(!feof($fp)) { + $content .= fgets($fp,4096); + } + fclose($fp); + $csplit = split("\r\n\r\n",$content,2); + + $content = $csplit[1]; + + if(!empty($params['assign_headers'])) { + $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0])); + } + } + } else { + $smarty->trigger_error("unable to parse URL, check syntax"); + return; + } + } else { + // ftp fetch + $fp = fopen($file,'r'); + while(!feof($fp)) { + $content .= fgets ($fp,4096); + } + fclose($fp); + } + + } + + + if (!empty($params['assign'])) { + $smarty->assign($params['assign'],$content); + } else { + echo $content; } } diff --git a/plugins/function.fetch.php b/plugins/function.fetch.php index e86408c6..ee46c160 100644 --- a/plugins/function.fetch.php +++ b/plugins/function.fetch.php @@ -10,15 +10,15 @@ */ function smarty_function_fetch($params, &$smarty) { - extract($params); + $file = $params['file']; if (empty($file)) { $smarty->trigger_error("parameter 'file' cannot be empty"); return; } - if ($smarty->security && !preg_match('!^(http|ftp)://!', $file)) { - // make sure fetched file comes from secure directory + if ($smarty->security && !preg_match('!^(http|ftp)://!i', $file)) { + // fetching file, make sure it comes from secure directory foreach ($smarty->secure_dir as $curr_dir) { if (substr(realpath($file), 0, strlen(realpath($curr_dir))) == realpath($curr_dir)) { $resource_is_secure = true; @@ -33,16 +33,181 @@ function smarty_function_fetch($params, &$smarty) $smarty->trigger_error("fetch cannot read file '$file'"); return; } - } - - - if (!empty($assign)) { - ob_start(); - readfile($file); - $smarty->assign($assign,ob_get_contents()); - ob_end_clean(); + // fetch the file + $fp = fopen($file,'r'); + while(!feof($fp)) { + $content .= fgets ($fp,4096); + } + fclose($fp); } else { - readfile($file); + // not a local file + if(preg_match('!^http://!i',$file)) { + // http fetch + if($uri_parts = parse_url($file)) { + // set defaults + $host = $server_name = $uri_parts['host']; + $timeout = 30; + $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; + $agent = "Smarty Template Engine ".$smarty->_version; + $referer = $_SERVER['REFERER']; + if(!empty($uri_parts['path'])) { + $uri = $uri_parts['path']; + } else { + $uri = '/'; + } + $_is_proxy = false; + if(empty($uri_parts['port'])) { + $port = 80; + } else { + $port = $uri_parts['port']; + } + if(empty($uri_parts['user'])) { + $user = $uri_parts['user']; + } + // loop through parameters, setup headers + foreach($params as $param_key => $param_value) { + switch($param_key) { + 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)) { + $smarty->trigger_error("invalid header format '".$param_value."'"); + 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 { + $smarty->trigger_error("invalid value for attribute '".$param_key."'"); + return; + } + 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; + } else { + $smarty->trigger_error("invalid value for attribute '".$param_key."'"); + return; + } + break; + case "cluster_host": + if(!empty($param_value) + && (gethostbyname($param_value) != $param_value)) { + $server_name = $param_value; + } + break; + default: + $smarty->trigger_error("unrecognized attribute '".$param_key."'"); + return; + } + } + if(!empty($proxy_host) && !empty($proxy_port)) { + $_is_proxy = true; + $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout); + } else { + $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout); + } + + if(!$fp) { + $smarty->trigger_error("unable to fetch: $errstr ($errno)"); + return; + } else { + if($_is_proxy) { + fputs($fp, "GET $file HTTP/1.0\r\n"); + } else { + fputs($fp, "GET $uri HTTP/1.0\r\n"); + } + if(!empty($host)) { + fputs($fp, "Host: $host\r\n"); + } + if(!empty($accept)) { + fputs($fp, "Accept: $accept\r\n"); + } + if(!empty($agent)) { + fputs($fp, "User-Agent: $agent\r\n"); + } + if(!empty($referer)) { + fputs($fp, "Referer: $referer\r\n"); + } + if(is_array($extra_headers)) { + foreach($extra_headers as $curr_header) { + fputs($fp, $curr_header."\r\n"); + } + } + if(!empty($user) && !empty($pass)) { + fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n"); + } + + fputs($fp, "\r\n"); + while(!feof($fp)) { + $content .= fgets($fp,4096); + } + fclose($fp); + $csplit = split("\r\n\r\n",$content,2); + + $content = $csplit[1]; + + if(!empty($params['assign_headers'])) { + $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0])); + } + } + } else { + $smarty->trigger_error("unable to parse URL, check syntax"); + return; + } + } else { + // ftp fetch + $fp = fopen($file,'r'); + while(!feof($fp)) { + $content .= fgets ($fp,4096); + } + fclose($fp); + } + + } + + + if (!empty($params['assign'])) { + $smarty->assign($params['assign'],$content); + } else { + echo $content; } }