PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/external_lib/Smarty/plugins/function.fetch.php

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