PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/smarty/plugins/function.fetch.php

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