/interface/main/calendar/modules/PostCalendar/pnincludes/Smarty/plugins/function.fetch.php

https://github.com/md-tech/openemr · PHP · 214 lines · 35 code · 4 blank · 175 comment · 11 complexity · e87d53fe7473c747381eb789d22e0923 MD5 · raw file

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