function.fetch.php 7.8 KB

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