vars.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Creates common globals for the rest of WordPress
  4. *
  5. * Sets $pagenow global which is the filename of the current screen.
  6. * Checks for the browser to set which one is currently being used.
  7. *
  8. * Detects which user environment WordPress is being used on.
  9. * Only attempts to check for Apache, Nginx and IIS -- three web
  10. * servers with known pretty permalink capability.
  11. *
  12. * Note: Though Nginx is detected, WordPress does not currently
  13. * generate rewrite rules for it. See https://wordpress.org/support/article/nginx/
  14. *
  15. * @package WordPress
  16. */
  17. global $pagenow,
  18. $is_lynx, $is_gecko, $is_winIE, $is_macIE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone, $is_IE, $is_edge,
  19. $is_apache, $is_IIS, $is_iis7, $is_nginx;
  20. // On which page are we?
  21. if ( is_admin() ) {
  22. // wp-admin pages are checked more carefully.
  23. if ( is_network_admin() ) {
  24. preg_match( '#/wp-admin/network/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
  25. } elseif ( is_user_admin() ) {
  26. preg_match( '#/wp-admin/user/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
  27. } else {
  28. preg_match( '#/wp-admin/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
  29. }
  30. $pagenow = ! empty( $self_matches[1] ) ? $self_matches[1] : '';
  31. $pagenow = trim( $pagenow, '/' );
  32. $pagenow = preg_replace( '#\?.*?$#', '', $pagenow );
  33. if ( '' === $pagenow || 'index' === $pagenow || 'index.php' === $pagenow ) {
  34. $pagenow = 'index.php';
  35. } else {
  36. preg_match( '#(.*?)(/|$)#', $pagenow, $self_matches );
  37. $pagenow = strtolower( $self_matches[1] );
  38. if ( '.php' !== substr( $pagenow, -4, 4 ) ) {
  39. $pagenow .= '.php'; // For `Options +Multiviews`: /wp-admin/themes/index.php (themes.php is queried).
  40. }
  41. }
  42. } else {
  43. if ( preg_match( '#([^/]+\.php)([?/].*?)?$#i', $_SERVER['PHP_SELF'], $self_matches ) ) {
  44. $pagenow = strtolower( $self_matches[1] );
  45. } else {
  46. $pagenow = 'index.php';
  47. }
  48. }
  49. unset( $self_matches );
  50. // Simple browser detection.
  51. $is_lynx = false;
  52. $is_gecko = false;
  53. $is_winIE = false;
  54. $is_macIE = false;
  55. $is_opera = false;
  56. $is_NS4 = false;
  57. $is_safari = false;
  58. $is_chrome = false;
  59. $is_iphone = false;
  60. $is_edge = false;
  61. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
  62. if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Lynx' ) !== false ) {
  63. $is_lynx = true;
  64. } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Edg' ) !== false ) {
  65. $is_edge = true;
  66. } elseif ( stripos( $_SERVER['HTTP_USER_AGENT'], 'chrome' ) !== false ) {
  67. if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'chromeframe' ) !== false ) {
  68. $is_admin = is_admin();
  69. /**
  70. * Filters whether Google Chrome Frame should be used, if available.
  71. *
  72. * @since 3.2.0
  73. *
  74. * @param bool $is_admin Whether to use the Google Chrome Frame. Default is the value of is_admin().
  75. */
  76. $is_chrome = apply_filters( 'use_google_chrome_frame', $is_admin );
  77. if ( $is_chrome ) {
  78. header( 'X-UA-Compatible: chrome=1' );
  79. }
  80. $is_winIE = ! $is_chrome;
  81. } else {
  82. $is_chrome = true;
  83. }
  84. } elseif ( stripos( $_SERVER['HTTP_USER_AGENT'], 'safari' ) !== false ) {
  85. $is_safari = true;
  86. } elseif ( ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false || strpos( $_SERVER['HTTP_USER_AGENT'], 'Trident' ) !== false ) && strpos( $_SERVER['HTTP_USER_AGENT'], 'Win' ) !== false ) {
  87. $is_winIE = true;
  88. } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false && strpos( $_SERVER['HTTP_USER_AGENT'], 'Mac' ) !== false ) {
  89. $is_macIE = true;
  90. } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Gecko' ) !== false ) {
  91. $is_gecko = true;
  92. } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera' ) !== false ) {
  93. $is_opera = true;
  94. } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Nav' ) !== false && strpos( $_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.' ) !== false ) {
  95. $is_NS4 = true;
  96. }
  97. }
  98. if ( $is_safari && stripos( $_SERVER['HTTP_USER_AGENT'], 'mobile' ) !== false ) {
  99. $is_iphone = true;
  100. }
  101. $is_IE = ( $is_macIE || $is_winIE );
  102. // Server detection.
  103. /**
  104. * Whether the server software is Apache or something else
  105. *
  106. * @global bool $is_apache
  107. */
  108. $is_apache = ( strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false );
  109. /**
  110. * Whether the server software is Nginx or something else
  111. *
  112. * @global bool $is_nginx
  113. */
  114. $is_nginx = ( strpos( $_SERVER['SERVER_SOFTWARE'], 'nginx' ) !== false );
  115. /**
  116. * Whether the server software is IIS or something else
  117. *
  118. * @global bool $is_IIS
  119. */
  120. $is_IIS = ! $is_apache && ( strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) !== false || strpos( $_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer' ) !== false );
  121. /**
  122. * Whether the server software is IIS 7.X or greater
  123. *
  124. * @global bool $is_iis7
  125. */
  126. $is_iis7 = $is_IIS && (int) substr( $_SERVER['SERVER_SOFTWARE'], strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/' ) + 14 ) >= 7;
  127. /**
  128. * Test if the current browser runs on a mobile device (smart phone, tablet, etc.)
  129. *
  130. * @since 3.4.0
  131. *
  132. * @return bool
  133. */
  134. function wp_is_mobile() {
  135. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
  136. $is_mobile = false;
  137. } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) !== false // Many mobile devices (all iPhone, iPad, etc.)
  138. || strpos( $_SERVER['HTTP_USER_AGENT'], 'Android' ) !== false
  139. || strpos( $_SERVER['HTTP_USER_AGENT'], 'Silk/' ) !== false
  140. || strpos( $_SERVER['HTTP_USER_AGENT'], 'Kindle' ) !== false
  141. || strpos( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' ) !== false
  142. || strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' ) !== false
  143. || strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) !== false ) {
  144. $is_mobile = true;
  145. } else {
  146. $is_mobile = false;
  147. }
  148. /**
  149. * Filters whether the request should be treated as coming from a mobile device or not.
  150. *
  151. * @since 4.9.0
  152. *
  153. * @param bool $is_mobile Whether the request is from a mobile device or not.
  154. */
  155. return apply_filters( 'wp_is_mobile', $is_mobile );
  156. }