/[cvs]/nfo/php/libs/net.php.smarty/plugins/function.fetch.php
ViewVC logotype

Diff of /nfo/php/libs/net.php.smarty/plugins/function.fetch.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by cvsjoko, Wed Oct 9 00:53:36 2002 UTC revision 1.2 by joko, Wed Jun 16 21:58:16 2004 UTC
# Line 1  Line 1 
1  <?php  <?php
2    /**
 /*  
3   * Smarty plugin   * Smarty plugin
4   * -------------------------------------------------------------   * @package Smarty
5   * Type:     function   * @subpackage plugins
6   * Name:     fetch   */
7    
8    
9    /**
10     * Smarty {fetch} plugin
11     *
12     * Type:     function<br>
13     * Name:     fetch<br>
14   * Purpose:  fetch file, web or ftp data and display results   * 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     * @param array
18     * @param Smarty
19     * @return string|null if the assign parameter is passed, Smarty assigns the
20     *                     result to a template variable
21   */   */
22  function smarty_function_fetch($params, &$smarty)  function smarty_function_fetch($params, &$smarty)
23  {  {
24          $file = $params['file'];      if (empty($params['file'])) {
25            $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
     if (empty($file)) {  
         $smarty->_trigger_plugin_error("parameter 'file' cannot be empty");  
26          return;          return;
27      }      }
28    
29      if ($smarty->security && !preg_match('!^(http|ftp)://!i', $file)) {      $content = '';
30          // fetching file, make sure it comes from secure directory      if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
31          foreach ($smarty->secure_dir as $curr_dir) {          $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
32              if (substr(realpath($file), 0, strlen(realpath($curr_dir))) == realpath($curr_dir)) {          require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.is_secure.php');
33                  $resource_is_secure = true;          if(!smarty_core_is_secure($_params, $smarty)) {
34                  break;              $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
35              }              return;
36          }          }
37          if (!$resource_is_secure) {          
38              $smarty->_trigger_plugin_error("(secure mode) fetch '$file' is not allowed");          // fetch the file
39            if($fp = @fopen($params['file'],'r')) {
40                while(!feof($fp)) {
41                    $content .= fgets ($fp,4096);
42                }
43                fclose($fp);
44            } else {
45                $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
46              return;              return;
47          }          }
                 // fetch the file  
                 if($fp = @fopen($file,'r')) {  
                         while(!feof($fp)) {  
                                 $content .= fgets ($fp,4096);  
                         }  
                         fclose($fp);  
                 } else {  
             $smarty->_trigger_plugin_error("fetch cannot read file '$file'");  
             return;                      
                 }  
48      } else {      } else {
49                  // not a local file          // not a local file
50                  if(preg_match('!^http://!i',$file)) {          if(preg_match('!^http://!i',$params['file'])) {
51                          // http fetch              // http fetch
52                          if($uri_parts = parse_url($file)) {              if($uri_parts = parse_url($params['file'])) {
53                                  // set defaults                  // set defaults
54                                  $host = $server_name = $uri_parts['host'];                  $host = $server_name = $uri_parts['host'];
55                                  $timeout = 30;                  $timeout = 30;
56                                  $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";                  $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
57                                  $agent = "Smarty Template Engine ".$smarty->_version;                  $agent = "Smarty Template Engine ".$smarty->_version;
58                                  $referer = "";                  $referer = "";
59                                  if(!empty($uri_parts['path'])) {                  $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
60                                          $uri = $uri_parts['path'];                  $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
61                                  } else {                  $_is_proxy = false;
62                                          $uri = '/';                  if(empty($uri_parts['port'])) {
63                                  }                      $port = 80;
64                                  $_is_proxy = false;                  } else {
65                                  if(empty($uri_parts['port'])) {                      $port = $uri_parts['port'];
66                                          $port = 80;                  }
67                                  } else {                  if(empty($uri_parts['user'])) {
68                                          $port = $uri_parts['port'];                      $user = '';
69                                  }                  }
70                                  if(empty($uri_parts['user'])) {                  // loop through parameters, setup headers
71                                          $user = $uri_parts['user'];                  foreach($params as $param_key => $param_value) {
72                                  }                                                    switch($param_key) {
73                                  // loop through parameters, setup headers                          case "file":
74                                  foreach($params as $param_key => $param_value) {                                                  case "assign":
75                                          switch($param_key) {                          case "assign_headers":
76                                                  case "file":                              break;
77                                                  case "assign":                          case "user":
78                                                  case "assign_headers":                              if(!empty($param_value)) {
79                                                          break;                                  $user = $param_value;
80                                                  case "user":                              }
81                                                          if(!empty($param_value)) {                              break;
82                                                                  $user = $param_value;                          case "pass":
83                                                          }                              if(!empty($param_value)) {
84                                                          break;                                  $pass = $param_value;
85                                                  case "pass":                              }
86                                                          if(!empty($param_value)) {                              break;
87                                                                  $pass = $param_value;                          case "accept":
88                                                          }                              if(!empty($param_value)) {
89                                                          break;                                  $accept = $param_value;
90                                                  case "accept":                              }
91                                                          if(!empty($param_value)) {                              break;
92                                                                  $accept = $param_value;                          case "header":
93                                                          }                              if(!empty($param_value)) {
94                                                          break;                                  if(!preg_match('![\w\d-]+: .+!',$param_value)) {
95                                                  case "header":                                      $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
96                                                          if(!empty($param_value)) {                                      return;
97                                                                  if(!preg_match('![\w\d-]+: .+!',$param_value)) {                                  } else {
98                                                          $smarty->_trigger_plugin_error("invalid header format '".$param_value."'");                                      $extra_headers[] = $param_value;
99                                                          return;                                                                                                  }
100                                                                  } else {                              }
101                                                                          $extra_headers[] = $param_value;                              break;
102                                                                  }                          case "proxy_host":
103                                                          }                              if(!empty($param_value)) {
104                                                          break;                                  $proxy_host = $param_value;
105                                                  case "proxy_host":                              }
106                                                          if(!empty($param_value)) {                              break;
107                                                                  $proxy_host = $param_value;                          case "proxy_port":
108                                                          }                              if(!preg_match('!\D!', $param_value)) {
109                                                          break;                                  $proxy_port = (int) $param_value;
110                                                  case "proxy_port":                              } else {
111                                                          if(!preg_match('!\D!', $param_value)) {                                  $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
112                                                                  $proxy_port = (int) $param_value;                                  return;
113                                                          } else {                              }
114                                                  $smarty->_trigger_plugin_error("invalid value for attribute '".$param_key."'");                              break;
115                                                  return;                                                                                          case "agent":
116                                                          }                              if(!empty($param_value)) {
117                                                          break;                                  $agent = $param_value;
118                                                  case "agent":                              }
119                                                          if(!empty($param_value)) {                              break;
120                                                                  $agent = $param_value;                          case "referer":
121                                                          }                              if(!empty($param_value)) {
122                                                          break;                                  $referer = $param_value;
123                                                  case "referer":                              }
124                                                          if(!empty($param_value)) {                              break;
125                                                                  $referer = $param_value;                          case "timeout":
126                                                          }                              if(!preg_match('!\D!', $param_value)) {
127                                                          break;                                  $timeout = (int) $param_value;
128                                                  case "timeout":                              } else {
129                                                          if(!preg_match('!\D!', $param_value)) {                                  $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
130                                                                  $timeout = (int) $param_value;                                  return;
131                                                          } else {                              }
132                                                  $smarty->_trigger_plugin_error("invalid value for attribute '".$param_key."'");                              break;
133                                                  return;                                                                                          default:
134                                                          }                              $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
135                                                          break;                              return;
136                                                  default:                      }
137                                          $smarty->_trigger_plugin_error("unrecognized attribute '".$param_key."'");                  }
138                                          return;                  if(!empty($proxy_host) && !empty($proxy_port)) {
139                                          }                                            $_is_proxy = true;
140                                  }                      $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
141                                  if(!empty($proxy_host) && !empty($proxy_port)) {                  } else {
142                                          $_is_proxy = true;                      $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
143                                          $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);                  }
144                                  } else {  
145                                          $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);                  if(!$fp) {
146                                  }                      $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
147                        return;
148                                  if(!$fp) {                  } else {
149                          $smarty->_trigger_plugin_error("unable to fetch: $errstr ($errno)");                      if($_is_proxy) {
150                          return;                                                  fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
151                                  } else {                      } else {
152                                          if($_is_proxy) {                          fputs($fp, "GET $uri HTTP/1.0\r\n");
153                                                  fputs($fp, "GET $file HTTP/1.0\r\n");                                                                }
154                                          } else {                      if(!empty($host)) {
155                                                  fputs($fp, "GET $uri HTTP/1.0\r\n");                          fputs($fp, "Host: $host\r\n");
156                                          }                      }
157                                          if(!empty($host)) {                      if(!empty($accept)) {
158                                                  fputs($fp, "Host: $host\r\n");                          fputs($fp, "Accept: $accept\r\n");
159                                          }                      }
160                                          if(!empty($accept)) {                      if(!empty($agent)) {
161                                                  fputs($fp, "Accept: $accept\r\n");                          fputs($fp, "User-Agent: $agent\r\n");
162                                          }                      }
163                                          if(!empty($agent)) {                      if(!empty($referer)) {
164                                                  fputs($fp, "User-Agent: $agent\r\n");                          fputs($fp, "Referer: $referer\r\n");
165                                          }                      }
166                                          if(!empty($referer)) {                      if(isset($extra_headers) && is_array($extra_headers)) {
167                                                  fputs($fp, "Referer: $referer\r\n");                          foreach($extra_headers as $curr_header) {
168                                          }                              fputs($fp, $curr_header."\r\n");
169                                          if(is_array($extra_headers)) {                          }
170                                                  foreach($extra_headers as $curr_header) {                      }
171                                                          fputs($fp, $curr_header."\r\n");                      if(!empty($user) && !empty($pass)) {
172                                                  }                          fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
173                                          }                      }
174                                          if(!empty($user) && !empty($pass)) {  
175                                                  fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");                                                                      fputs($fp, "\r\n");
176                                          }                      while(!feof($fp)) {
177                                                                    $content .= fgets($fp,4096);
178                                          fputs($fp, "\r\n");                      }
179                                          while(!feof($fp)) {                      fclose($fp);
180                                                  $content .= fgets($fp,4096);                      $csplit = split("\r\n\r\n",$content,2);
181                                          }  
182                                          fclose($fp);                                                          $content = $csplit[1];
183                                          $csplit = split("\r\n\r\n",$content,2);  
184                        if(!empty($params['assign_headers'])) {
185                                          $content = $csplit[1];                          $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
186                                                                }
187                                          if(!empty($params['assign_headers'])) {                  }
188                                                  $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));              } else {
189                                          }                  $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
190                                  }                  return;
191                          } else {              }
192                          $smarty->_trigger_plugin_error("unable to parse URL, check syntax");          } else {
193                          return;              // ftp fetch
194                          }              if($fp = @fopen($params['file'],'r')) {
195                  } else {                  while(!feof($fp)) {
196                          // ftp fetch                      $content .= fgets ($fp,4096);
197                          if($fp = @fopen($file,'r')) {                  }
198                                  while(!feof($fp)) {                  fclose($fp);
199                                          $content .= fgets ($fp,4096);              } else {
200                                  }                  $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
201                                  fclose($fp);                  return;
202                          } else {              }
203                  $smarty->_trigger_plugin_error("fetch cannot read file '$file'");          }
204                  return;                  
205                          }      }
                 }  
                   
         }  
206    
207    
208      if (!empty($params['assign'])) {      if (!empty($params['assign'])) {
209          $smarty->assign($params['assign'],$content);          $smarty->assign($params['assign'],$content);
210      } else {      } else {
211          echo $content;          return $content;
212      }      }
213  }  }
214    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed