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

/system/class/cls_http.inc.php

https://gitlab.com/Ltaimao/wecenter
PHP | 420 lines | 282 code | 54 blank | 84 comment | 46 complexity | 0cea64ba435412c30b12880c337608cf MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | WeCenter [#RELEASE_VERSION#]
  5. | ========================================
  6. | by WeCenter Software
  7. | © 2011 - 2014 WeCenter. All Rights Reserved
  8. | http://www.wecenter.com
  9. | ========================================
  10. | Support: WeCenter@qq.com
  11. |
  12. +---------------------------------------------------------------------------
  13. */
  14. class HTTP
  15. {
  16. /**
  17. * NO CACHE 文件头
  18. *
  19. * @param $type
  20. * @param $charset
  21. */
  22. public static function no_cache_header($type = 'text/html', $charset = 'utf-8')
  23. {
  24. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  25. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  26. header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
  27. header('Pragma: no-cache');
  28. header('Content-Type: ' . $type . '; charset=' . $charset . '');
  29. }
  30. /**
  31. * 获取 COOKIE
  32. *
  33. * @param $name
  34. */
  35. public static function get_cookie($name)
  36. {
  37. if (isset($_COOKIE[G_COOKIE_PREFIX . $name]))
  38. {
  39. return $_COOKIE[G_COOKIE_PREFIX . $name];
  40. }
  41. return false;
  42. }
  43. /**
  44. * 设置 COOKIE
  45. *
  46. * @param $name
  47. * @param $value
  48. * @param $expire
  49. * @param $path
  50. * @param $domain
  51. * @param $secure
  52. * @param $httponly
  53. */
  54. public static function set_cookie($name, $value = '', $expire = null, $path = '/', $domain = null, $secure = false, $httponly = false)
  55. {
  56. if (! $domain and G_COOKIE_DOMAIN)
  57. {
  58. $domain = G_COOKIE_DOMAIN;
  59. }
  60. return setcookie(G_COOKIE_PREFIX . $name, $value, $expire, $path, $domain, $secure, $httponly);
  61. }
  62. public static function error_404()
  63. {
  64. if ($_POST['_post_type'] == 'ajax')
  65. {
  66. H::ajax_json_output(AWS_APP::RSM(null, -1, 'HTTP/1.1 404 Not Found'));
  67. }
  68. else
  69. {
  70. header('HTTP/1.1 404 Not Found');
  71. TPL::output('global/error_404');
  72. exit;
  73. }
  74. }
  75. public static function parse_redirect_url($url)
  76. {
  77. if (substr($url, 0, 1) == '?')
  78. {
  79. $url = base_url() . $url;
  80. }
  81. else if (substr($url, 0, 1) == '/')
  82. {
  83. $url = get_js_url($url);
  84. }
  85. return $url;
  86. }
  87. public static function redirect($url)
  88. {
  89. if ($url = HTTP::parse_redirect_url($url))
  90. {
  91. header('Location: ' . $url);
  92. die;
  93. }
  94. }
  95. static function download_filename_header($filename)
  96. {
  97. if (preg_match('~&#([0-9]+);~', $filename))
  98. {
  99. $filename_conv = @iconv('utf-8', 'UTF-8//IGNORE', $filename);
  100. if ($filename_conv !== false)
  101. {
  102. $filename = $filename_conv;
  103. }
  104. $filename = preg_replace(
  105. '~&#([0-9]+);~e',
  106. "convert_int_to_utf8('\\1')",
  107. $filename
  108. );
  109. }
  110. $filename_charset = 'utf-8';
  111. $filename = preg_replace('#[\r\n]#', '', $filename);
  112. // Opera and IE have not a clue about this, mozilla puts on incorrect extensions.
  113. if (self::is_browser('mozilla'))
  114. {
  115. $filename = "filename*=" . $filename_charset . "''" . rawurlencode($filename);
  116. //$filename = "filename==?'utf-8'?B?" . base64_encode($filename) . "?=";
  117. }
  118. else
  119. {
  120. // other browsers seem to want names in UTF-8
  121. if ($filename_charset != 'utf-8' AND function_exists('iconv'))
  122. {
  123. $filename_conv = iconv($filename_charset, 'UTF-8//IGNORE', $filename);
  124. if ($filename_conv !== false)
  125. {
  126. $filename = $filename_conv;
  127. }
  128. }
  129. if (self::is_browser('opera') OR self::is_browser('konqueror') OR self::is_browser('safari'))
  130. {
  131. // Opera / Konqueror does not support encoded file names
  132. $filename = 'filename="' . str_replace('"', '', $filename) . '"';
  133. }
  134. else if (self::is_browser('ie'))
  135. {
  136. $filename = 'filename="' . str_replace('+', ' ', urlencode($filename)) . '"';
  137. }
  138. else
  139. {
  140. // encode the filename to stay within spec
  141. $filename = 'filename="' . rawurlencode($filename) . '"';
  142. }
  143. }
  144. return $filename;
  145. }
  146. static function force_download_header($filename, $filesize = 0, $modifytime = 0)
  147. {
  148. $range = 0;
  149. if ($_SERVER['HTTP_RANGE'])
  150. {
  151. list($range) = explode('-',(str_replace('bytes=', '', $_SERVER['HTTP_RANGE'])));
  152. }
  153. ob_end_clean();
  154. header("Cache-Control: no-cache, must-revalidate");
  155. header("Pragma: no-cache");
  156. header('Date: ' . gmdate('D, d M Y H:i:s', $modifytime) . ' GMT');
  157. header('Content-Disposition: attachment; ' . self::download_filename_header($filename));
  158. header("Content-Type: application/octet-stream"); // has bug with IE
  159. //header('HTTP/1.1 206 Partial Content');
  160. header('Accept-Ranges: bytes');
  161. if ($filesize)
  162. {
  163. if ($_SERVER['HTTP_RANGE'])
  164. {
  165. $rangesize = ($filesize - $range) > 0 ? ($filesize - $range) : 0;
  166. header('Content-Length: ' . $rangesize);
  167. header('Content-Range: bytes ' . $range . '-' . ($filesize - 1) . '/' . ($filesize));
  168. }
  169. else
  170. {
  171. header('Content-Length: ' . $filesize);
  172. }
  173. }
  174. }
  175. /**
  176. * Browser detection system - returns whether or not the visiting browser is the one specified
  177. *
  178. * @param string Browser name (opera, ie, mozilla, firebord, firefox... etc. - see $is array)
  179. * @param float Minimum acceptable version for true result (optional)
  180. *
  181. * @return boolean
  182. */
  183. public static function is_browser($browser, $version = 0)
  184. {
  185. static $is;
  186. if (! is_array($is))
  187. {
  188. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  189. $is = array(
  190. 'opera' => 0,
  191. 'ie' => 0,
  192. 'mozilla' => 0,
  193. 'firebird' => 0,
  194. 'firefox' => 0,
  195. 'camino' => 0,
  196. 'konqueror' => 0,
  197. 'safari' => 0,
  198. 'webkit' => 0,
  199. 'webtv' => 0,
  200. 'netscape' => 0,
  201. 'mac' => 0
  202. );
  203. // detect opera
  204. # Opera/7.11 (Windows NT 5.1; U) [en]
  205. # Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 7.02 Bork-edition [en]
  206. # Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 4.0) Opera 7.0 [en]
  207. # Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.0 [en]
  208. # Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC) Opera 5.0 [en]
  209. if (strpos($useragent, 'opera') !== false)
  210. {
  211. preg_match('#opera(/| )([0-9\.]+)#', $useragent, $regs);
  212. $is['opera'] = $regs[2];
  213. }
  214. // detect internet explorer
  215. # Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)
  216. # Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)
  217. # Mozilla/4.0 (compatible; MSIE 5.22; Mac_PowerPC)
  218. # Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC; e504460WanadooNL)
  219. if (strpos($useragent, 'msie ') !== false and ! $is['opera'])
  220. {
  221. preg_match('#msie ([0-9\.]+)#', $useragent, $regs);
  222. $is['ie'] = $regs[1];
  223. }
  224. // detect macintosh
  225. if (strpos($useragent, 'mac') !== false)
  226. {
  227. $is['mac'] = 1;
  228. }
  229. // detect safari
  230. # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/74 (KHTML, like Gecko) Safari/74
  231. # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/51 (like Gecko) Safari/51
  232. if (strpos($useragent, 'applewebkit') !== false and $is['mac'])
  233. {
  234. preg_match('#applewebkit/(\d+)#', $useragent, $regs);
  235. $is['webkit'] = $regs[1];
  236. if (strpos($useragent, 'safari') !== false)
  237. {
  238. preg_match('#safari/([0-9\.]+)#', $useragent, $regs);
  239. $is['safari'] = $regs[1];
  240. }
  241. }
  242. // detect konqueror
  243. # Mozilla/5.0 (compatible; Konqueror/3.1; Linux; X11; i686)
  244. # Mozilla/5.0 (compatible; Konqueror/3.1; Linux 2.4.19-32mdkenterprise; X11; i686; ar, en_US)
  245. # Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)
  246. if (strpos($useragent, 'konqueror') !== false)
  247. {
  248. preg_match('#konqueror/([0-9\.-]+)#', $useragent, $regs);
  249. $is['konqueror'] = $regs[1];
  250. }
  251. // detect mozilla
  252. # Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4b) Gecko/20030504 Mozilla
  253. # Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2a) Gecko/20020910
  254. # Mozilla/5.0 (X11; U; Linux 2.4.3-20mdk i586; en-US; rv:0.9.1) Gecko/20010611
  255. if (strpos($useragent, 'gecko') !== false and ! $is['safari'] and ! $is['konqueror'])
  256. {
  257. preg_match('#gecko/(\d+)#', $useragent, $regs);
  258. $is['mozilla'] = $regs[1];
  259. // detect firebird / firefox
  260. # Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.3a) Gecko/20021207 Phoenix/0.5
  261. # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6
  262. # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
  263. # Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040206 Firefox/0.8
  264. if (strpos($useragent, 'firefox') !== false or strpos($useragent, 'firebird') !== false or strpos($useragent, 'phoenix') !== false)
  265. {
  266. preg_match('#(phoenix|firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $regs);
  267. $is['firebird'] = $regs[3];
  268. if ($regs[1] == 'firefox')
  269. {
  270. $is['firefox'] = $regs[3];
  271. }
  272. }
  273. // detect camino
  274. # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20021104 Chimera/0.6
  275. if (strpos($useragent, 'chimera') !== false or strpos($useragent, 'camino') !== false)
  276. {
  277. preg_match('#(chimera|camino)/([0-9\.]+)#', $useragent, $regs);
  278. $is['camino'] = $regs[2];
  279. }
  280. }
  281. // detect web tv
  282. if (strpos($useragent, 'webtv') !== false)
  283. {
  284. preg_match('#webtv/([0-9\.]+)#', $useragent, $regs);
  285. $is['webtv'] = $regs[1];
  286. }
  287. // detect pre-gecko netscape
  288. if (preg_match('#mozilla/([1-4]{1})\.([0-9]{2}|[1-8]{1})#', $useragent, $regs))
  289. {
  290. $is['netscape'] = "$regs[1].$regs[2]";
  291. }
  292. }
  293. // sanitize the incoming browser name
  294. $browser = strtolower($browser);
  295. if (substr($browser, 0, 3) == 'is_')
  296. {
  297. $browser = substr($browser, 3);
  298. }
  299. // return the version number of the detected browser if it is the same as $browser
  300. if ($is["$browser"])
  301. {
  302. // $version was specified - only return version number if detected version is >= to specified $version
  303. if ($version)
  304. {
  305. if ($is["$browser"] >= $version)
  306. {
  307. return $is["$browser"];
  308. }
  309. }
  310. else
  311. {
  312. return $is["$browser"];
  313. }
  314. }
  315. // if we got this far, we are not the specified browser, or the version number is too low
  316. return 0;
  317. }
  318. public static function request($url, $method, $data = null, $timeout = 15, $header = null, $cookie = null)
  319. {
  320. if (defined('WECENTER_CURL_USERAGENT'))
  321. {
  322. $user_agent = WECENTER_CURL_USERAGENT;
  323. }
  324. else
  325. {
  326. $user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12';
  327. }
  328. $headers = array(
  329. 'API-RemoteIP' => fetch_ip()
  330. );
  331. if ($header)
  332. {
  333. $headers = array_merge($header, $headers);
  334. }
  335. $options = array(
  336. 'useragent' => $user_agent,
  337. 'timeout' => $timeout,
  338. 'cookies' => $cookie,
  339. 'verify' => false,
  340. 'verifyname' => false,
  341. );
  342. switch (strtoupper($method))
  343. {
  344. default:
  345. case 'GET':
  346. $request = Services_Requests::get($url, $headers, $options);
  347. break;
  348. case 'POST':
  349. $request = Services_Requests::post($url, $headers, $data, $options);
  350. break;
  351. case 'DELETE':
  352. $request = Services_Requests::delete($url, $headers, $options);
  353. break;
  354. case 'PUT':
  355. $request = Services_Requests::put($url, $headers, $data, $options);
  356. break;
  357. case 'PATCH':
  358. $request = Services_Requests::put($url, $headers, $data, $options);
  359. break;
  360. }
  361. if ($request->status_code == 200)
  362. {
  363. return $request->body;
  364. }
  365. }
  366. }