function.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 = '
  59. <html xmlns:v="urn:schemas-microsoft-com:vml"
  60. xmlns:o="urn:schemas-microsoft-com:office:office"
  61. xmlns:w="urn:schemas-microsoft-com:office:word"
  62. xmlns="http://www.w3.org/TR/REC-html40">
  63. <head><meta http-equiv=Content-Type content="text/html;
  64. charset=utf-8">
  65. <style type="text/css">
  66. table
  67. {
  68. border-collapse: collapse;
  69. border: none;
  70. width: 100%;
  71. }
  72. td
  73. {
  74. border: solid #CCC 1px;
  75. }
  76. .codestyle{
  77. word-break: break-all;
  78. background:silver;mso-highlight:silver;
  79. }
  80. </style>
  81. <meta name=ProgId content=Word.Document>
  82. <meta name=Generator content="Microsoft Word 11">
  83. <meta name=Originator content="Microsoft Word 11">
  84. <xml><w:WordDocument><w:View>Print</w:View></xml></head>
  85. <body>'.$data.'</body></html>';
  86. $filepath = tmpfile();
  87. $data = str_replace("<thead>\n<tr>","<thead><tr style='background-color: rgb(0, 136, 204); color: rgb(255, 255, 255);'>",$data);
  88. $data = str_replace("<pre><code>","<table width='100%' class='codestyle'><pre><code>",$data);
  89. $data = str_replace("</code></pre>","</code></pre></table>",$data);
  90. $len = strlen($data);
  91. fwrite($filepath, $data);
  92. header("Content-type: application/octet-stream");
  93. header("Content-Disposition: attachment; filename={$fileName}.doc");
  94. header('Content-Description: File Transfer');
  95. header('Content-Type: application/octet-stream');
  96. header('Content-Disposition: attachment; filename='.$fileName.'.doc');
  97. header('Content-Transfer-Encoding: binary');
  98. header('Expires: 0');
  99. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  100. header('Pragma: public');
  101. header('Content-Length: ' . $len);
  102. rewind($filepath);
  103. echo fread($filepath,$len);
  104. }
  105. function clear_runtime($path = RUNTIME_PATH){
  106. //给定的目录不是一个文件夹
  107. if(!is_dir($path)){
  108. return null;
  109. }
  110. $fh = opendir($path);
  111. while(($row = readdir($fh)) !== false){
  112. //过滤掉虚拟目录
  113. if($row == '.' || $row == '..'|| $row == 'index.html'){
  114. continue;
  115. }
  116. if(!is_dir($path.'/'.$row)){
  117. unlink($path.'/'.$row);
  118. }
  119. clear_runtime($path.'/'.$row);
  120. }
  121. //关闭目录句柄,否则出Permission denied
  122. closedir($fh);
  123. return true;
  124. }
  125. //获取ip
  126. function getIPaddress(){
  127. $IPaddress='';
  128. if (isset($_SERVER)){
  129. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
  130. $IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
  131. } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
  132. $IPaddress = $_SERVER["HTTP_CLIENT_IP"];
  133. } else {
  134. $IPaddress = $_SERVER["REMOTE_ADDR"];
  135. }
  136. } else {
  137. if (getenv("HTTP_X_FORWARDED_FOR")){
  138. $IPaddress = getenv("HTTP_X_FORWARDED_FOR");
  139. } else if (getenv("HTTP_CLIENT_IP")) {
  140. $IPaddress = getenv("HTTP_CLIENT_IP");
  141. } else {
  142. $IPaddress = getenv("REMOTE_ADDR");
  143. }
  144. }
  145. return $IPaddress;
  146. }
  147. function http_post($url, $param) {
  148. $oCurl = curl_init ();
  149. if (stripos ( $url, "https://" ) !== FALSE) {
  150. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
  151. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
  152. }
  153. if (is_string ( $param )) {
  154. $strPOST = $param;
  155. } else {
  156. $aPOST = array ();
  157. foreach ( $param as $key => $val ) {
  158. $aPOST [] = $key . "=" . urlencode ( $val );
  159. }
  160. $strPOST = join ( "&", $aPOST );
  161. }
  162. curl_setopt ( $oCurl, CURLOPT_URL, $url );
  163. curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
  164. curl_setopt ( $oCurl, CURLOPT_POST, true );
  165. curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );
  166. $sContent = curl_exec ( $oCurl );
  167. curl_close ( $oCurl );
  168. return $sContent;
  169. }