PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Smarty/plugins/function.fetch.php

https://github.com/jacknicole/sugarcrm_dev
PHP | 253 lines | 31 code | 8 blank | 214 comment | 9 complexity | 23aea2dfaba739298fe7ed0fa667efdb MD5 | raw file
  1. <?php
  2. /*
  3. Modification information for LGPL compliance
  4. r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
  5. r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
  6. r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
  7. r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
  8. r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
  9. r51443 - 2009-10-12 13:34:36 -0700 (Mon, 12 Oct 2009) - jmertic - Bug 33332 - Made application PHP 5.3 compliant with E_DEPRECATED warnings on by:
  10. - Changing all ereg function to either preg or simple string based ones
  11. - No more references to magic quotes.
  12. - Change all the session_unregister() functions to just unset() the correct session variable instead.
  13. r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
  14. r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
  15. r10971 - 2006-01-12 14:58:30 -0800 (Thu, 12 Jan 2006) - chris - Bug 4128: updating Smarty templates to 2.6.11, a version supposedly that plays better with PHP 5.1
  16. r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
  17. */
  18. /**
  19. * Smarty plugin
  20. * @package Smarty
  21. * @subpackage plugins
  22. */
  23. /**
  24. * Smarty {fetch} plugin
  25. *
  26. * Type: function<br>
  27. * Name: fetch<br>
  28. * Purpose: fetch file, web or ftp data and display results
  29. * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
  30. * (Smarty online manual)
  31. * @author Monte Ohrt <monte at ohrt dot com>
  32. * @param array
  33. * @param Smarty
  34. * @return string|null if the assign parameter is passed, Smarty assigns the
  35. * result to a template variable
  36. */
  37. function smarty_function_fetch($params, &$smarty)
  38. {
  39. if (empty($params['file'])) {
  40. $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
  41. return;
  42. }
  43. $content = '';
  44. if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
  45. $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
  46. require_once(SMARTY_CORE_DIR . 'core.is_secure.php');
  47. if(!smarty_core_is_secure($_params, $smarty)) {
  48. $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
  49. return;
  50. }
  51. // fetch the file
  52. if($fp = @fopen($params['file'],'r')) {
  53. while(!feof($fp)) {
  54. $content .= fgets ($fp,4096);
  55. }
  56. fclose($fp);
  57. } else {
  58. $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
  59. return;
  60. }
  61. } else {
  62. // not a local file
  63. if(preg_match('!^http://!i',$params['file'])) {
  64. // http fetch
  65. if($uri_parts = parse_url($params['file'])) {
  66. // set defaults
  67. $host = $server_name = $uri_parts['host'];
  68. $timeout = 30;
  69. $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
  70. $agent = "Smarty Template Engine ".$smarty->_version;
  71. $referer = "";
  72. $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
  73. $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
  74. $_is_proxy = false;
  75. if(empty($uri_parts['port'])) {
  76. $port = 80;
  77. } else {
  78. $port = $uri_parts['port'];
  79. }
  80. if(!empty($uri_parts['user'])) {
  81. $user = $uri_parts['user'];
  82. }
  83. if(!empty($uri_parts['pass'])) {
  84. $pass = $uri_parts['pass'];
  85. }
  86. // loop through parameters, setup headers
  87. foreach($params as $param_key => $param_value) {
  88. switch($param_key) {
  89. case "file":
  90. case "assign":
  91. case "assign_headers":
  92. break;
  93. case "user":
  94. if(!empty($param_value)) {
  95. $user = $param_value;
  96. }
  97. break;
  98. case "pass":
  99. if(!empty($param_value)) {
  100. $pass = $param_value;
  101. }
  102. break;
  103. case "accept":
  104. if(!empty($param_value)) {
  105. $accept = $param_value;
  106. }
  107. break;
  108. case "header":
  109. if(!empty($param_value)) {
  110. if(!preg_match('![\w\d-]+: .+!',$param_value)) {
  111. $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
  112. return;
  113. } else {
  114. $extra_headers[] = $param_value;
  115. }
  116. }
  117. break;
  118. case "proxy_host":
  119. if(!empty($param_value)) {
  120. $proxy_host = $param_value;
  121. }
  122. break;
  123. case "proxy_port":
  124. if(!preg_match('!\D!', $param_value)) {
  125. $proxy_port = (int) $param_value;
  126. } else {
  127. $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
  128. return;
  129. }
  130. break;
  131. case "agent":
  132. if(!empty($param_value)) {
  133. $agent = $param_value;
  134. }
  135. break;
  136. case "referer":
  137. if(!empty($param_value)) {
  138. $referer = $param_value;
  139. }
  140. break;
  141. case "timeout":
  142. if(!preg_match('!\D!', $param_value)) {
  143. $timeout = (int) $param_value;
  144. } else {
  145. $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
  146. return;
  147. }
  148. break;
  149. default:
  150. $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
  151. return;
  152. }
  153. }
  154. if(!empty($proxy_host) && !empty($proxy_port)) {
  155. $_is_proxy = true;
  156. $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
  157. } else {
  158. $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
  159. }
  160. if(!$fp) {
  161. $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
  162. return;
  163. } else {
  164. if($_is_proxy) {
  165. fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
  166. } else {
  167. fputs($fp, "GET $uri HTTP/1.0\r\n");
  168. }
  169. if(!empty($host)) {
  170. fputs($fp, "Host: $host\r\n");
  171. }
  172. if(!empty($accept)) {
  173. fputs($fp, "Accept: $accept\r\n");
  174. }
  175. if(!empty($agent)) {
  176. fputs($fp, "User-Agent: $agent\r\n");
  177. }
  178. if(!empty($referer)) {
  179. fputs($fp, "Referer: $referer\r\n");
  180. }
  181. if(isset($extra_headers) && is_array($extra_headers)) {
  182. foreach($extra_headers as $curr_header) {
  183. fputs($fp, $curr_header."\r\n");
  184. }
  185. }
  186. if(!empty($user) && !empty($pass)) {
  187. fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
  188. }
  189. fputs($fp, "\r\n");
  190. while(!feof($fp)) {
  191. $content .= fgets($fp,4096);
  192. }
  193. fclose($fp);
  194. $csplit = explode("\r\n\r\n",$content,2);
  195. $content = $csplit[1];
  196. if(!empty($params['assign_headers'])) {
  197. $smarty->assign($params['assign_headers'],explode("\r\n",$csplit[0]));
  198. }
  199. }
  200. } else {
  201. $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
  202. return;
  203. }
  204. } else {
  205. // ftp fetch
  206. if($fp = @fopen($params['file'],'r')) {
  207. while(!feof($fp)) {
  208. $content .= fgets ($fp,4096);
  209. }
  210. fclose($fp);
  211. } else {
  212. $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
  213. return;
  214. }
  215. }
  216. }
  217. if (!empty($params['assign'])) {
  218. $smarty->assign($params['assign'],$content);
  219. } else {
  220. return $content;
  221. }
  222. }
  223. /* vim: set expandtab: */
  224. ?>