PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/phpmyvisites/libs/smarty/plugins/function.fetch.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 220 lines | 31 code | 5 blank | 184 comment | 9 complexity | 2cd7a8379bd822e3488453e5856a0270 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_CORE_DIR . '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 = $uri_parts['user'];
  65. }
  66. if(!empty($uri_parts['pass'])) {
  67. $pass = $uri_parts['pass'];
  68. }
  69. // loop through parameters, setup headers
  70. foreach($params as $param_key => $param_value) {
  71. switch($param_key) {
  72. case "file":
  73. case "assign":
  74. case "assign_headers":
  75. break;
  76. case "user":
  77. if(!empty($param_value)) {
  78. $user = $param_value;
  79. }
  80. break;
  81. case "pass":
  82. if(!empty($param_value)) {
  83. $pass = $param_value;
  84. }
  85. break;
  86. case "accept":
  87. if(!empty($param_value)) {
  88. $accept = $param_value;
  89. }
  90. break;
  91. case "header":
  92. if(!empty($param_value)) {
  93. if(!preg_match('![\w\d-]+: .+!',$param_value)) {
  94. $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
  95. return;
  96. } else {
  97. $extra_headers[] = $param_value;
  98. }
  99. }
  100. break;
  101. case "proxy_host":
  102. if(!empty($param_value)) {
  103. $proxy_host = $param_value;
  104. }
  105. break;
  106. case "proxy_port":
  107. if(!preg_match('!\D!', $param_value)) {
  108. $proxy_port = (int) $param_value;
  109. } else {
  110. $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
  111. return;
  112. }
  113. break;
  114. case "agent":
  115. if(!empty($param_value)) {
  116. $agent = $param_value;
  117. }
  118. break;
  119. case "referer":
  120. if(!empty($param_value)) {
  121. $referer = $param_value;
  122. }
  123. break;
  124. case "timeout":
  125. if(!preg_match('!\D!', $param_value)) {
  126. $timeout = (int) $param_value;
  127. } else {
  128. $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
  129. return;
  130. }
  131. break;
  132. default:
  133. $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
  134. return;
  135. }
  136. }
  137. if(!empty($proxy_host) && !empty($proxy_port)) {
  138. $_is_proxy = true;
  139. $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
  140. } else {
  141. $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
  142. }
  143. if(!$fp) {
  144. $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
  145. return;
  146. } else {
  147. if($_is_proxy) {
  148. fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
  149. } else {
  150. fputs($fp, "GET $uri HTTP/1.0\r\n");
  151. }
  152. if(!empty($host)) {
  153. fputs($fp, "Host: $host\r\n");
  154. }
  155. if(!empty($accept)) {
  156. fputs($fp, "Accept: $accept\r\n");
  157. }
  158. if(!empty($agent)) {
  159. fputs($fp, "User-Agent: $agent\r\n");
  160. }
  161. if(!empty($referer)) {
  162. fputs($fp, "Referer: $referer\r\n");
  163. }
  164. if(isset($extra_headers) && is_array($extra_headers)) {
  165. foreach($extra_headers as $curr_header) {
  166. fputs($fp, $curr_header."\r\n");
  167. }
  168. }
  169. if(!empty($user) && !empty($pass)) {
  170. fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
  171. }
  172. fputs($fp, "\r\n");
  173. while(!feof($fp)) {
  174. $content .= fgets($fp,4096);
  175. }
  176. fclose($fp);
  177. $csplit = split("\r\n\r\n",$content,2);
  178. $content = $csplit[1];
  179. if(!empty($params['assign_headers'])) {
  180. $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
  181. }
  182. }
  183. } else {
  184. $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
  185. return;
  186. }
  187. } else {
  188. // ftp fetch
  189. if($fp = @fopen($params['file'],'r')) {
  190. while(!feof($fp)) {
  191. $content .= fgets ($fp,4096);
  192. }
  193. fclose($fp);
  194. } else {
  195. $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
  196. return;
  197. }
  198. }
  199. }
  200. if (!empty($params['assign'])) {
  201. $smarty->assign($params['assign'],$content);
  202. } else {
  203. return $content;
  204. }
  205. }
  206. /* vim: set expandtab: */
  207. ?>