6  * @subpackage PluginsFunction
 
  10  * Smarty {fetch} plugin
 
  14  * Purpose:  fetch file, web or ftp data and display results
 
  15  * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
 
  16  *       (Smarty online manual)
 
  17  * @author Monte Ohrt <monte at ohrt dot com>
 
  18  * @param array $params parameters
 
  19  * @param object $template template object
 
  20  * @return string|null if the assign parameter is passed, Smarty assigns the
 
  21  *                     result to a template variable
 
  23 function smarty_function_fetch($params, $template)
 
  25     if (empty($params['file'])) {
 
  26         trigger_error("[plugin] fetch parameter 'file' cannot be empty",E_USER_NOTICE);
 
  31     if (isset($template->security_policy) && !preg_match('!^(http|ftp)://!i', $params['file'])) {
 
  32         if(!$template->security_policy->isTrustedResourceDir($params['file'])) {
 
  37         if($fp = @fopen($params['file'],'r')) {
 
  39                 $content .= fgets ($fp,4096);
 
  43             trigger_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'',E_USER_NOTICE);
 
  48         if(preg_match('!^http://!i',$params['file'])) {
 
  50             if($uri_parts = parse_url($params['file'])) {
 
  52                 $host = $server_name = $uri_parts['host'];
 
  54                 $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
 
  55                 $agent = "Smarty Template Engine ".$template->_version;
 
  57                 $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
 
  58                 $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
 
  60                 if(empty($uri_parts['port'])) {
 
  63                     $port = $uri_parts['port'];
 
  65                 if(!empty($uri_parts['user'])) {
 
  66                     $user = $uri_parts['user'];
 
  68                 if(!empty($uri_parts['pass'])) {
 
  69                     $pass = $uri_parts['pass'];
 
  71                 // loop through parameters, setup headers
 
  72                 foreach($params as $param_key => $param_value) {
 
  76                         case "assign_headers":
 
  79                             if(!empty($param_value)) {
 
  84                             if(!empty($param_value)) {
 
  89                             if(!empty($param_value)) {
 
  90                                 $accept = $param_value;
 
  94                             if(!empty($param_value)) {
 
  95                                 if(!preg_match('![\w\d-]+: .+!',$param_value)) {
 
  96                                     trigger_error("[plugin] invalid header format '".$param_value."'",E_USER_NOTICE);
 
  99                                     $extra_headers[] = $param_value;
 
 104                             if(!empty($param_value)) {
 
 105                                 $proxy_host = $param_value;
 
 109                             if(!preg_match('!\D!', $param_value)) {
 
 110                                 $proxy_port = (int) $param_value;
 
 112                                 trigger_error("[plugin] invalid value for attribute '".$param_key."'",E_USER_NOTICE);
 
 117                             if(!empty($param_value)) {
 
 118                                 $agent = $param_value;
 
 122                             if(!empty($param_value)) {
 
 123                                 $referer = $param_value;
 
 127                             if(!preg_match('!\D!', $param_value)) {
 
 128                                 $timeout = (int) $param_value;
 
 130                                 trigger_error("[plugin] invalid value for attribute '".$param_key."'",E_USER_NOTICE);
 
 135                             trigger_error("[plugin] unrecognized attribute '".$param_key."'",E_USER_NOTICE);
 
 139                 if(!empty($proxy_host) && !empty($proxy_port)) {
 
 141                     $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
 
 143                     $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
 
 147                     trigger_error("[plugin] unable to fetch: $errstr ($errno)",E_USER_NOTICE);
 
 151                         fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
 
 153                         fputs($fp, "GET $uri HTTP/1.0\r\n");
 
 156                         fputs($fp, "Host: $host\r\n");
 
 158                     if(!empty($accept)) {
 
 159                         fputs($fp, "Accept: $accept\r\n");
 
 162                         fputs($fp, "User-Agent: $agent\r\n");
 
 164                     if(!empty($referer)) {
 
 165                         fputs($fp, "Referer: $referer\r\n");
 
 167                     if(isset($extra_headers) && is_array($extra_headers)) {
 
 168                         foreach($extra_headers as $curr_header) {
 
 169                             fputs($fp, $curr_header."\r\n");
 
 172                     if(!empty($user) && !empty($pass)) {
 
 173                         fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
 
 178                         $content .= fgets($fp,4096);
 
 181                     $csplit = preg_split("!\r\n\r\n!",$content,2);
 
 183                     $content = $csplit[1];
 
 185                     if(!empty($params['assign_headers'])) {
 
 186                         $template->assign($params['assign_headers'],preg_split("!\r\n!",$csplit[0]));
 
 190                 trigger_error("[plugin fetch] unable to parse URL, check syntax",E_USER_NOTICE);
 
 195             if($fp = @fopen($params['file'],'r')) {
 
 197                     $content .= fgets ($fp,4096);
 
 201                 trigger_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'',E_USER_NOTICE);
 
 209     if (!empty($params['assign'])) {
 
 210         $template->assign($params['assign'],$content);