Automatically exported from code.google.com/p/planningalerts
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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