function.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * 获得当前的域名
  4. *
  5. * @return string
  6. */
  7. function get_domain()
  8. {
  9. /* 协议 */
  10. $protocol = (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://';
  11. /* 域名或IP地址 */
  12. if (isset($_SERVER['HTTP_X_FORWARDED_HOST']))
  13. {
  14. $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
  15. }
  16. elseif (isset($_SERVER['HTTP_HOST']))
  17. {
  18. $host = $_SERVER['HTTP_HOST'];
  19. }
  20. else
  21. {
  22. /* 端口 */
  23. if (isset($_SERVER['SERVER_PORT']))
  24. {
  25. $port = ':' . $_SERVER['SERVER_PORT'];
  26. if ((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol))
  27. {
  28. $port = '';
  29. }
  30. }
  31. else
  32. {
  33. $port = '';
  34. }
  35. if (isset($_SERVER['SERVER_NAME']))
  36. {
  37. $host = $_SERVER['SERVER_NAME'] . $port;
  38. }
  39. elseif (isset($_SERVER['SERVER_ADDR']))
  40. {
  41. $host = $_SERVER['SERVER_ADDR'] . $port;
  42. }
  43. }
  44. return $protocol . $host;
  45. }
  46. /**
  47. * 获得网站的URL地址
  48. *
  49. * @return string
  50. */
  51. function site_url()
  52. {
  53. return get_domain() . substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));
  54. }
  55. //导出称word
  56. function output_word($data,$fileName=''){
  57. if(empty($data)) return '';
  58. $data = '<html xmlns:v="urn:schemas-microsoft-com:vml"
  59. xmlns:o="urn:schemas-microsoft-com:office:office"
  60. xmlns:w="urn:schemas-microsoft-com:office:word"
  61. xmlns="http://www.w3.org/TR/REC-html40">
  62. <head><meta http-equiv=Content-Type content="text/html; charset=utf-8">
  63. <style type="text/css">
  64. table
  65. {
  66. border-collapse: collapse;
  67. border: none;
  68. width: 100%;
  69. }
  70. td,tr
  71. {
  72. border: solid #CCC 1px;
  73. padding:3px;
  74. font-size:9pt;
  75. }
  76. .codestyle{
  77. word-break: break-all;
  78. mso-highlight:rgb(252, 252, 252);
  79. padding-left: 5px; background-color: rgb(252, 252, 252); border: 1px solid rgb(225, 225, 232);
  80. }
  81. img {
  82. width:100;
  83. }
  84. </style>
  85. <meta name=ProgId content=Word.Document>
  86. <meta name=Generator content="Microsoft Word 11">
  87. <meta name=Originator content="Microsoft Word 11">
  88. <xml><w:WordDocument><w:View>Print</w:View></xml></head>
  89. <body>'.$data.'</body></html>';
  90. $filepath = tmpfile();
  91. $data = str_replace("<thead>\n<tr>","<thead><tr style='background-color: rgb(0, 136, 204); color: rgb(255, 255, 255);'>",$data);
  92. $data = str_replace("<pre><code","<table width='100%' class='codestyle'><pre><code",$data);
  93. $data = str_replace("</code></pre>","</code></pre></table>",$data);
  94. $data = str_replace("<img ","<img width=500 ",$data);
  95. $len = strlen($data);
  96. fwrite($filepath, $data);
  97. header("Content-type: application/octet-stream");
  98. header("Content-Disposition: attachment; filename={$fileName}.doc");
  99. header('Content-Description: File Transfer');
  100. header('Content-Type: application/octet-stream');
  101. header('Content-Disposition: attachment; filename='.$fileName.'.doc');
  102. header('Content-Transfer-Encoding: binary');
  103. header('Expires: 0');
  104. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  105. header('Pragma: public');
  106. header('Content-Length: ' . $len);
  107. rewind($filepath);
  108. echo fread($filepath,$len);
  109. }
  110. function clear_runtime($path = RUNTIME_PATH){
  111. //给定的目录不是一个文件夹
  112. if(!is_dir($path)){
  113. return null;
  114. }
  115. $fh = opendir($path);
  116. while(($row = readdir($fh)) !== false){
  117. //过滤掉虚拟目录
  118. if($row == '.' || $row == '..'|| $row == 'index.html'){
  119. continue;
  120. }
  121. if(!is_dir($path.'/'.$row)){
  122. unlink($path.'/'.$row);
  123. }
  124. clear_runtime($path.'/'.$row);
  125. }
  126. //关闭目录句柄,否则出Permission denied
  127. closedir($fh);
  128. return true;
  129. }
  130. //获取ip
  131. function getIPaddress(){
  132. $IPaddress='';
  133. if (isset($_SERVER)){
  134. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
  135. $IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
  136. } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
  137. $IPaddress = $_SERVER["HTTP_CLIENT_IP"];
  138. } else {
  139. $IPaddress = $_SERVER["REMOTE_ADDR"];
  140. }
  141. } else {
  142. if (getenv("HTTP_X_FORWARDED_FOR")){
  143. $IPaddress = getenv("HTTP_X_FORWARDED_FOR");
  144. } else if (getenv("HTTP_CLIENT_IP")) {
  145. $IPaddress = getenv("HTTP_CLIENT_IP");
  146. } else {
  147. $IPaddress = getenv("REMOTE_ADDR");
  148. }
  149. }
  150. return $IPaddress;
  151. }
  152. /**
  153. * POST 请求
  154. *
  155. * @param string $url
  156. * @param array $param
  157. * @return string content
  158. */
  159. function http_post($url, $param) {
  160. $oCurl = curl_init ();
  161. if (stripos ( $url, "https://" ) !== FALSE) {
  162. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
  163. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
  164. }
  165. if (is_string ( $param )) {
  166. $strPOST = $param;
  167. } else {
  168. $aPOST = array ();
  169. foreach ( $param as $key => $val ) {
  170. $aPOST [] = $key . "=" . urlencode ( $val );
  171. }
  172. $strPOST = join ( "&", $aPOST );
  173. }
  174. curl_setopt ( $oCurl, CURLOPT_URL, $url );
  175. curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
  176. curl_setopt ( $oCurl, CURLOPT_POST, true );
  177. curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );
  178. $sContent = curl_exec ( $oCurl );
  179. curl_close ( $oCurl );
  180. return $sContent;
  181. }
  182. function compress_string($string){
  183. return base64_encode( gzcompress($string, 9)) ;
  184. }
  185. function uncompress_string($string){
  186. return gzuncompress(base64_decode($string));
  187. }