You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1075 lines
31 KiB

  1. <?php
  2. /**
  3. * These functions are needed to load WordPress.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Return the HTTP protocol sent by the server.
  9. *
  10. * @since 4.4.0
  11. *
  12. * @return string The HTTP protocol. Default: HTTP/1.0.
  13. */
  14. function wp_get_server_protocol() {
  15. $protocol = $_SERVER['SERVER_PROTOCOL'];
  16. if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0' ) ) ) {
  17. $protocol = 'HTTP/1.0';
  18. }
  19. return $protocol;
  20. }
  21. /**
  22. * Turn register globals off.
  23. *
  24. * @since 2.1.0
  25. * @access private
  26. */
  27. function wp_unregister_GLOBALS() {
  28. if ( !ini_get( 'register_globals' ) )
  29. return;
  30. if ( isset( $_REQUEST['GLOBALS'] ) )
  31. die( 'GLOBALS overwrite attempt detected' );
  32. // Variables that shouldn't be unset
  33. $no_unset = array( 'GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix' );
  34. $input = array_merge( $_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset( $_SESSION ) && is_array( $_SESSION ) ? $_SESSION : array() );
  35. foreach ( $input as $k => $v )
  36. if ( !in_array( $k, $no_unset ) && isset( $GLOBALS[$k] ) ) {
  37. unset( $GLOBALS[$k] );
  38. }
  39. }
  40. /**
  41. * Fix `$_SERVER` variables for various setups.
  42. *
  43. * @since 3.0.0
  44. * @access private
  45. *
  46. * @global string $PHP_SELF The filename of the currently executing script,
  47. * relative to the document root.
  48. */
  49. function wp_fix_server_vars() {
  50. global $PHP_SELF;
  51. $default_server_values = array(
  52. 'SERVER_SOFTWARE' => '',
  53. 'REQUEST_URI' => '',
  54. );
  55. $_SERVER = array_merge( $default_server_values, $_SERVER );
  56. // Fix for IIS when running with PHP ISAPI
  57. if ( empty( $_SERVER['REQUEST_URI'] ) || ( PHP_SAPI != 'cgi-fcgi' && preg_match( '/^Microsoft-IIS\//', $_SERVER['SERVER_SOFTWARE'] ) ) ) {
  58. // IIS Mod-Rewrite
  59. if ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
  60. $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
  61. }
  62. // IIS Isapi_Rewrite
  63. elseif ( isset( $_SERVER['HTTP_X_REWRITE_URL'] ) ) {
  64. $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
  65. } else {
  66. // Use ORIG_PATH_INFO if there is no PATH_INFO
  67. if ( !isset( $_SERVER['PATH_INFO'] ) && isset( $_SERVER['ORIG_PATH_INFO'] ) )
  68. $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
  69. // Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
  70. if ( isset( $_SERVER['PATH_INFO'] ) ) {
  71. if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] )
  72. $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
  73. else
  74. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
  75. }
  76. // Append the query string if it exists and isn't null
  77. if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
  78. $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
  79. }
  80. }
  81. }
  82. // Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests
  83. if ( isset( $_SERVER['SCRIPT_FILENAME'] ) && ( strpos( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) == strlen( $_SERVER['SCRIPT_FILENAME'] ) - 7 ) )
  84. $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'];
  85. // Fix for Dreamhost and other PHP as CGI hosts
  86. if ( strpos( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) !== false )
  87. unset( $_SERVER['PATH_INFO'] );
  88. // Fix empty PHP_SELF
  89. $PHP_SELF = $_SERVER['PHP_SELF'];
  90. if ( empty( $PHP_SELF ) )
  91. $_SERVER['PHP_SELF'] = $PHP_SELF = preg_replace( '/(\?.*)?$/', '', $_SERVER["REQUEST_URI"] );
  92. }
  93. /**
  94. * Check for the required PHP version, and the MySQL extension or
  95. * a database drop-in.
  96. *
  97. * Dies if requirements are not met.
  98. *
  99. * @since 3.0.0
  100. * @access private
  101. *
  102. * @global string $required_php_version The required PHP version string.
  103. * @global string $wp_version The WordPress version string.
  104. */
  105. function wp_check_php_mysql_versions() {
  106. global $required_php_version, $wp_version;
  107. $php_version = phpversion();
  108. if ( version_compare( $required_php_version, $php_version, '>' ) ) {
  109. wp_load_translations_early();
  110. $protocol = wp_get_server_protocol();
  111. header( sprintf( '%s 500 Internal Server Error', $protocol ), true, 500 );
  112. header( 'Content-Type: text/html; charset=utf-8' );
  113. /* translators: 1: Current PHP version number, 2: WordPress version number, 3: Minimum required PHP version number */
  114. die( sprintf( __( 'Your server is running PHP version %1$s but WordPress %2$s requires at least %3$s.' ), $php_version, $wp_version, $required_php_version ) );
  115. }
  116. if ( ! extension_loaded( 'mysql' ) && ! extension_loaded( 'mysqli' ) && ! extension_loaded( 'mysqlnd' ) && ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) {
  117. wp_load_translations_early();
  118. $protocol = wp_get_server_protocol();
  119. header( sprintf( '%s 500 Internal Server Error', $protocol ), true, 500 );
  120. header( 'Content-Type: text/html; charset=utf-8' );
  121. die( __( 'Your PHP installation appears to be missing the MySQL extension which is required by WordPress.' ) );
  122. }
  123. }
  124. /**
  125. * Don't load all of WordPress when handling a favicon.ico request.
  126. *
  127. * Instead, send the headers for a zero-length favicon and bail.
  128. *
  129. * @since 3.0.0
  130. */
  131. function wp_favicon_request() {
  132. if ( '/favicon.ico' == $_SERVER['REQUEST_URI'] ) {
  133. header('Content-Type: image/vnd.microsoft.icon');
  134. exit;
  135. }
  136. }
  137. /**
  138. * Die with a maintenance message when conditions are met.
  139. *
  140. * Checks for a file in the WordPress root directory named ".maintenance".
  141. * This file will contain the variable $upgrading, set to the time the file
  142. * was created. If the file was created less than 10 minutes ago, WordPress
  143. * enters maintenance mode and displays a message.
  144. *
  145. * The default message can be replaced by using a drop-in (maintenance.php in
  146. * the wp-content directory).
  147. *
  148. * @since 3.0.0
  149. * @access private
  150. *
  151. * @global int $upgrading the unix timestamp marking when upgrading WordPress began.
  152. */
  153. function wp_maintenance() {
  154. if ( ! file_exists( ABSPATH . '.maintenance' ) || wp_installing() )
  155. return;
  156. global $upgrading;
  157. include( ABSPATH . '.maintenance' );
  158. // If the $upgrading timestamp is older than 10 minutes, don't die.
  159. if ( ( time() - $upgrading ) >= 600 )
  160. return;
  161. /**
  162. * Filters whether to enable maintenance mode.
  163. *
  164. * This filter runs before it can be used by plugins. It is designed for
  165. * non-web runtimes. If this filter returns true, maintenance mode will be
  166. * active and the request will end. If false, the request will be allowed to
  167. * continue processing even if maintenance mode should be active.
  168. *
  169. * @since 4.6.0
  170. *
  171. * @param bool $enable_checks Whether to enable maintenance mode. Default true.
  172. * @param int $upgrading The timestamp set in the .maintenance file.
  173. */
  174. if ( ! apply_filters( 'enable_maintenance_mode', true, $upgrading ) ) {
  175. return;
  176. }
  177. if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) {
  178. require_once( WP_CONTENT_DIR . '/maintenance.php' );
  179. die();
  180. }
  181. wp_load_translations_early();
  182. $protocol = wp_get_server_protocol();
  183. header( "$protocol 503 Service Unavailable", true, 503 );
  184. header( 'Content-Type: text/html; charset=utf-8' );
  185. header( 'Retry-After: 600' );
  186. ?>
  187. <!DOCTYPE html>
  188. <html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
  189. <head>
  190. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  191. <title><?php _e( 'Maintenance' ); ?></title>
  192. </head>
  193. <body>
  194. <h1><?php _e( 'Briefly unavailable for scheduled maintenance. Check back in a minute.' ); ?></h1>
  195. </body>
  196. </html>
  197. <?php
  198. die();
  199. }
  200. /**
  201. * Start the WordPress micro-timer.
  202. *
  203. * @since 0.71
  204. * @access private
  205. *
  206. * @global float $timestart Unix timestamp set at the beginning of the page load.
  207. * @see timer_stop()
  208. *
  209. * @return bool Always returns true.
  210. */
  211. function timer_start() {
  212. global $timestart;
  213. $timestart = microtime( true );
  214. return true;
  215. }
  216. /**
  217. * Retrieve or display the time from the page start to when function is called.
  218. *
  219. * @since 0.71
  220. *
  221. * @global float $timestart Seconds from when timer_start() is called.
  222. * @global float $timeend Seconds from when function is called.
  223. *
  224. * @param int|bool $display Whether to echo or return the results. Accepts 0|false for return,
  225. * 1|true for echo. Default 0|false.
  226. * @param int $precision The number of digits from the right of the decimal to display.
  227. * Default 3.
  228. * @return string The "second.microsecond" finished time calculation. The number is formatted
  229. * for human consumption, both localized and rounded.
  230. */
  231. function timer_stop( $display = 0, $precision = 3 ) {
  232. global $timestart, $timeend;
  233. $timeend = microtime( true );
  234. $timetotal = $timeend - $timestart;
  235. $r = ( function_exists( 'number_format_i18n' ) ) ? number_format_i18n( $timetotal, $precision ) : number_format( $timetotal, $precision );
  236. if ( $display )
  237. echo $r;
  238. return $r;
  239. }
  240. /**
  241. * Set PHP error reporting based on WordPress debug settings.
  242. *
  243. * Uses three constants: `WP_DEBUG`, `WP_DEBUG_DISPLAY`, and `WP_DEBUG_LOG`.
  244. * All three can be defined in wp-config.php. By default, `WP_DEBUG` and
  245. * `WP_DEBUG_LOG` are set to false, and `WP_DEBUG_DISPLAY` is set to true.
  246. *
  247. * When `WP_DEBUG` is true, all PHP notices are reported. WordPress will also
  248. * display internal notices: when a deprecated WordPress function, function
  249. * argument, or file is used. Deprecated code may be removed from a later
  250. * version.
  251. *
  252. * It is strongly recommended that plugin and theme developers use `WP_DEBUG`
  253. * in their development environments.
  254. *
  255. * `WP_DEBUG_DISPLAY` and `WP_DEBUG_LOG` perform no function unless `WP_DEBUG`
  256. * is true.
  257. *
  258. * When `WP_DEBUG_DISPLAY` is true, WordPress will force errors to be displayed.
  259. * `WP_DEBUG_DISPLAY` defaults to true. Defining it as null prevents WordPress
  260. * from changing the global configuration setting. Defining `WP_DEBUG_DISPLAY`
  261. * as false will force errors to be hidden.
  262. *
  263. * When `WP_DEBUG_LOG` is true, errors will be logged to debug.log in the content
  264. * directory.
  265. *
  266. * Errors are never displayed for XML-RPC, REST, and Ajax requests.
  267. *
  268. * @since 3.0.0
  269. * @access private
  270. */
  271. function wp_debug_mode() {
  272. /**
  273. * Filters whether to allow the debug mode check to occur.
  274. *
  275. * This filter runs before it can be used by plugins. It is designed for
  276. * non-web run-times. Returning false causes the `WP_DEBUG` and related
  277. * constants to not be checked and the default php values for errors
  278. * will be used unless you take care to update them yourself.
  279. *
  280. * @since 4.6.0
  281. *
  282. * @param bool $enable_debug_mode Whether to enable debug mode checks to occur. Default true.
  283. */
  284. if ( ! apply_filters( 'enable_wp_debug_mode_checks', true ) ){
  285. return;
  286. }
  287. if ( WP_DEBUG ) {
  288. error_reporting( E_ALL );
  289. if ( WP_DEBUG_DISPLAY )
  290. ini_set( 'display_errors', 1 );
  291. elseif ( null !== WP_DEBUG_DISPLAY )
  292. ini_set( 'display_errors', 0 );
  293. if ( WP_DEBUG_LOG ) {
  294. ini_set( 'log_errors', 1 );
  295. ini_set( 'error_log', WP_CONTENT_DIR . '/debug.log' );
  296. }
  297. } else {
  298. error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
  299. }
  300. if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() ) {
  301. @ini_set( 'display_errors', 0 );
  302. }
  303. }
  304. /**
  305. * Set the location of the language directory.
  306. *
  307. * To set directory manually, define the `WP_LANG_DIR` constant
  308. * in wp-config.php.
  309. *
  310. * If the language directory exists within `WP_CONTENT_DIR`, it
  311. * is used. Otherwise the language directory is assumed to live
  312. * in `WPINC`.
  313. *
  314. * @since 3.0.0
  315. * @access private
  316. */
  317. function wp_set_lang_dir() {
  318. if ( !defined( 'WP_LANG_DIR' ) ) {
  319. if ( file_exists( WP_CONTENT_DIR . '/languages' ) && @is_dir( WP_CONTENT_DIR . '/languages' ) || !@is_dir(ABSPATH . WPINC . '/languages') ) {
  320. /**
  321. * Server path of the language directory.
  322. *
  323. * No leading slash, no trailing slash, full path, not relative to ABSPATH
  324. *
  325. * @since 2.1.0
  326. */
  327. define( 'WP_LANG_DIR', WP_CONTENT_DIR . '/languages' );
  328. if ( !defined( 'LANGDIR' ) ) {
  329. // Old static relative path maintained for limited backward compatibility - won't work in some cases.
  330. define( 'LANGDIR', 'wp-content/languages' );
  331. }
  332. } else {
  333. /**
  334. * Server path of the language directory.
  335. *
  336. * No leading slash, no trailing slash, full path, not relative to `ABSPATH`.
  337. *
  338. * @since 2.1.0
  339. */
  340. define( 'WP_LANG_DIR', ABSPATH . WPINC . '/languages' );
  341. if ( !defined( 'LANGDIR' ) ) {
  342. // Old relative path maintained for backward compatibility.
  343. define( 'LANGDIR', WPINC . '/languages' );
  344. }
  345. }
  346. }
  347. }
  348. /**
  349. * Load the database class file and instantiate the `$wpdb` global.
  350. *
  351. * @since 2.5.0
  352. *
  353. * @global wpdb $wpdb The WordPress database class.
  354. */
  355. function require_wp_db() {
  356. global $wpdb;
  357. require_once( ABSPATH . WPINC . '/wp-db.php' );
  358. if ( file_exists( WP_CONTENT_DIR . '/db.php' ) )
  359. require_once( WP_CONTENT_DIR . '/db.php' );
  360. if ( isset( $wpdb ) ) {
  361. return;
  362. }
  363. $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
  364. }
  365. /**
  366. * Set the database table prefix and the format specifiers for database
  367. * table columns.
  368. *
  369. * Columns not listed here default to `%s`.
  370. *
  371. * @since 3.0.0
  372. * @access private
  373. *
  374. * @global wpdb $wpdb The WordPress database class.
  375. * @global string $table_prefix The database table prefix.
  376. */
  377. function wp_set_wpdb_vars() {
  378. global $wpdb, $table_prefix;
  379. if ( !empty( $wpdb->error ) )
  380. dead_db();
  381. $wpdb->field_types = array( 'post_author' => '%d', 'post_parent' => '%d', 'menu_order' => '%d', 'term_id' => '%d', 'term_group' => '%d', 'term_taxonomy_id' => '%d',
  382. 'parent' => '%d', 'count' => '%d','object_id' => '%d', 'term_order' => '%d', 'ID' => '%d', 'comment_ID' => '%d', 'comment_post_ID' => '%d', 'comment_parent' => '%d',
  383. 'user_id' => '%d', 'link_id' => '%d', 'link_owner' => '%d', 'link_rating' => '%d', 'option_id' => '%d', 'blog_id' => '%d', 'meta_id' => '%d', 'post_id' => '%d',
  384. 'user_status' => '%d', 'umeta_id' => '%d', 'comment_karma' => '%d', 'comment_count' => '%d',
  385. // multisite:
  386. 'active' => '%d', 'cat_id' => '%d', 'deleted' => '%d', 'lang_id' => '%d', 'mature' => '%d', 'public' => '%d', 'site_id' => '%d', 'spam' => '%d',
  387. );
  388. $prefix = $wpdb->set_prefix( $table_prefix );
  389. if ( is_wp_error( $prefix ) ) {
  390. wp_load_translations_early();
  391. wp_die(
  392. /* translators: 1: $table_prefix 2: wp-config.php */
  393. sprintf( __( '<strong>ERROR</strong>: %1$s in %2$s can only contain numbers, letters, and underscores.' ),
  394. '<code>$table_prefix</code>',
  395. '<code>wp-config.php</code>'
  396. )
  397. );
  398. }
  399. }
  400. /**
  401. * Toggle `$_wp_using_ext_object_cache` on and off without directly
  402. * touching global.
  403. *
  404. * @since 3.7.0
  405. *
  406. * @global bool $_wp_using_ext_object_cache
  407. *
  408. * @param bool $using Whether external object cache is being used.
  409. * @return bool The current 'using' setting.
  410. */
  411. function wp_using_ext_object_cache( $using = null ) {
  412. global $_wp_using_ext_object_cache;
  413. $current_using = $_wp_using_ext_object_cache;
  414. if ( null !== $using )
  415. $_wp_using_ext_object_cache = $using;
  416. return $current_using;
  417. }
  418. /**
  419. * Start the WordPress object cache.
  420. *
  421. * If an object-cache.php file exists in the wp-content directory,
  422. * it uses that drop-in as an external object cache.
  423. *
  424. * @since 3.0.0
  425. * @access private
  426. */
  427. function wp_start_object_cache() {
  428. global $wp_filter;
  429. $first_init = false;
  430. if ( ! function_exists( 'wp_cache_init' ) ) {
  431. if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
  432. require_once ( WP_CONTENT_DIR . '/object-cache.php' );
  433. if ( function_exists( 'wp_cache_init' ) ) {
  434. wp_using_ext_object_cache( true );
  435. }
  436. // Re-initialize any hooks added manually by object-cache.php
  437. if ( $wp_filter ) {
  438. $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
  439. }
  440. }
  441. $first_init = true;
  442. } elseif ( ! wp_using_ext_object_cache() && file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
  443. /*
  444. * Sometimes advanced-cache.php can load object-cache.php before
  445. * it is loaded here. This breaks the function_exists check above
  446. * and can result in `$_wp_using_ext_object_cache` being set
  447. * incorrectly. Double check if an external cache exists.
  448. */
  449. wp_using_ext_object_cache( true );
  450. }
  451. if ( ! wp_using_ext_object_cache() ) {
  452. require_once ( ABSPATH . WPINC . '/cache.php' );
  453. }
  454. /*
  455. * If cache supports reset, reset instead of init if already
  456. * initialized. Reset signals to the cache that global IDs
  457. * have changed and it may need to update keys and cleanup caches.
  458. */
  459. if ( ! $first_init && function_exists( 'wp_cache_switch_to_blog' ) ) {
  460. wp_cache_switch_to_blog( get_current_blog_id() );
  461. } elseif ( function_exists( 'wp_cache_init' ) ) {
  462. wp_cache_init();
  463. }
  464. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  465. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) );
  466. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
  467. }
  468. }
  469. /**
  470. * Redirect to the installer if WordPress is not installed.
  471. *
  472. * Dies with an error message when Multisite is enabled.
  473. *
  474. * @since 3.0.0
  475. * @access private
  476. */
  477. function wp_not_installed() {
  478. if ( is_multisite() ) {
  479. if ( ! is_blog_installed() && ! wp_installing() ) {
  480. nocache_headers();
  481. wp_die( __( 'The site you have requested is not installed properly. Please contact the system administrator.' ) );
  482. }
  483. } elseif ( ! is_blog_installed() && ! wp_installing() ) {
  484. nocache_headers();
  485. require( ABSPATH . WPINC . '/kses.php' );
  486. require( ABSPATH . WPINC . '/pluggable.php' );
  487. require( ABSPATH . WPINC . '/formatting.php' );
  488. $link = wp_guess_url() . '/wp-admin/install.php';
  489. wp_redirect( $link );
  490. die();
  491. }
  492. }
  493. /**
  494. * Retrieve an array of must-use plugin files.
  495. *
  496. * The default directory is wp-content/mu-plugins. To change the default
  497. * directory manually, define `WPMU_PLUGIN_DIR` and `WPMU_PLUGIN_URL`
  498. * in wp-config.php.
  499. *
  500. * @since 3.0.0
  501. * @access private
  502. *
  503. * @return array Files to include.
  504. */
  505. function wp_get_mu_plugins() {
  506. $mu_plugins = array();
  507. if ( !is_dir( WPMU_PLUGIN_DIR ) )
  508. return $mu_plugins;
  509. if ( ! $dh = opendir( WPMU_PLUGIN_DIR ) )
  510. return $mu_plugins;
  511. while ( ( $plugin = readdir( $dh ) ) !== false ) {
  512. if ( substr( $plugin, -4 ) == '.php' )
  513. $mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin;
  514. }
  515. closedir( $dh );
  516. sort( $mu_plugins );
  517. return $mu_plugins;
  518. }
  519. /**
  520. * Retrieve an array of active and valid plugin files.
  521. *
  522. * While upgrading or installing WordPress, no plugins are returned.
  523. *
  524. * The default directory is wp-content/plugins. To change the default
  525. * directory manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL`
  526. * in wp-config.php.
  527. *
  528. * @since 3.0.0
  529. * @access private
  530. *
  531. * @return array Files.
  532. */
  533. function wp_get_active_and_valid_plugins() {
  534. $plugins = array();
  535. $active_plugins = (array) get_option( 'active_plugins', array() );
  536. // Check for hacks file if the option is enabled
  537. if ( get_option( 'hack_file' ) && file_exists( ABSPATH . 'my-hacks.php' ) ) {
  538. _deprecated_file( 'my-hacks.php', '1.5.0' );
  539. array_unshift( $plugins, ABSPATH . 'my-hacks.php' );
  540. }
  541. if ( empty( $active_plugins ) || wp_installing() )
  542. return $plugins;
  543. $network_plugins = is_multisite() ? wp_get_active_network_plugins() : false;
  544. foreach ( $active_plugins as $plugin ) {
  545. if ( ! validate_file( $plugin ) // $plugin must validate as file
  546. && '.php' == substr( $plugin, -4 ) // $plugin must end with '.php'
  547. && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
  548. // not already included as a network plugin
  549. && ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins ) )
  550. )
  551. $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
  552. }
  553. return $plugins;
  554. }
  555. /**
  556. * Set internal encoding.
  557. *
  558. * In most cases the default internal encoding is latin1, which is
  559. * of no use, since we want to use the `mb_` functions for `utf-8` strings.
  560. *
  561. * @since 3.0.0
  562. * @access private
  563. */
  564. function wp_set_internal_encoding() {
  565. if ( function_exists( 'mb_internal_encoding' ) ) {
  566. $charset = get_option( 'blog_charset' );
  567. if ( ! $charset || ! @mb_internal_encoding( $charset ) )
  568. mb_internal_encoding( 'UTF-8' );
  569. }
  570. }
  571. /**
  572. * Add magic quotes to `$_GET`, `$_POST`, `$_COOKIE`, and `$_SERVER`.
  573. *
  574. * Also forces `$_REQUEST` to be `$_GET + $_POST`. If `$_SERVER`,
  575. * `$_COOKIE`, or `$_ENV` are needed, use those superglobals directly.
  576. *
  577. * @since 3.0.0
  578. * @access private
  579. */
  580. function wp_magic_quotes() {
  581. // If already slashed, strip.
  582. if ( get_magic_quotes_gpc() ) {
  583. $_GET = stripslashes_deep( $_GET );
  584. $_POST = stripslashes_deep( $_POST );
  585. $_COOKIE = stripslashes_deep( $_COOKIE );
  586. }
  587. // Escape with wpdb.
  588. $_GET = add_magic_quotes( $_GET );
  589. $_POST = add_magic_quotes( $_POST );
  590. $_COOKIE = add_magic_quotes( $_COOKIE );
  591. $_SERVER = add_magic_quotes( $_SERVER );
  592. // Force REQUEST to be GET + POST.
  593. $_REQUEST = array_merge( $_GET, $_POST );
  594. }
  595. /**
  596. * Runs just before PHP shuts down execution.
  597. *
  598. * @since 1.2.0
  599. * @access private
  600. */
  601. function shutdown_action_hook() {
  602. /**
  603. * Fires just before PHP shuts down execution.
  604. *
  605. * @since 1.2.0
  606. */
  607. do_action( 'shutdown' );
  608. wp_cache_close();
  609. }
  610. /**
  611. * Copy an object.
  612. *
  613. * @since 2.7.0
  614. * @deprecated 3.2.0
  615. *
  616. * @param object $object The object to clone.
  617. * @return object The cloned object.
  618. */
  619. function wp_clone( $object ) {
  620. // Use parens for clone to accommodate PHP 4. See #17880
  621. return clone( $object );
  622. }
  623. /**
  624. * Whether the current request is for an administrative interface page.
  625. *
  626. * Does not check if the user is an administrator; current_user_can()
  627. * for checking roles and capabilities.
  628. *
  629. * @since 1.5.1
  630. *
  631. * @global WP_Screen $current_screen
  632. *
  633. * @return bool True if inside WordPress administration interface, false otherwise.
  634. */
  635. function is_admin() {
  636. if ( isset( $GLOBALS['current_screen'] ) )
  637. return $GLOBALS['current_screen']->in_admin();
  638. elseif ( defined( 'WP_ADMIN' ) )
  639. return WP_ADMIN;
  640. return false;
  641. }
  642. /**
  643. * Whether the current request is for a site's admininstrative interface.
  644. *
  645. * e.g. `/wp-admin/`
  646. *
  647. * Does not check if the user is an administrator; current_user_can()
  648. * for checking roles and capabilities.
  649. *
  650. * @since 3.1.0
  651. *
  652. * @global WP_Screen $current_screen
  653. *
  654. * @return bool True if inside WordPress blog administration pages.
  655. */
  656. function is_blog_admin() {
  657. if ( isset( $GLOBALS['current_screen'] ) )
  658. return $GLOBALS['current_screen']->in_admin( 'site' );
  659. elseif ( defined( 'WP_BLOG_ADMIN' ) )
  660. return WP_BLOG_ADMIN;
  661. return false;
  662. }
  663. /**
  664. * Whether the current request is for the network administrative interface.
  665. *
  666. * e.g. `/wp-admin/network/`
  667. *
  668. * Does not check if the user is an administrator; current_user_can()
  669. * for checking roles and capabilities.
  670. *
  671. * @since 3.1.0
  672. *
  673. * @global WP_Screen $current_screen
  674. *
  675. * @return bool True if inside WordPress network administration pages.
  676. */
  677. function is_network_admin() {
  678. if ( isset( $GLOBALS['current_screen'] ) )
  679. return $GLOBALS['current_screen']->in_admin( 'network' );
  680. elseif ( defined( 'WP_NETWORK_ADMIN' ) )
  681. return WP_NETWORK_ADMIN;
  682. return false;
  683. }
  684. /**
  685. * Whether the current request is for a user admin screen.
  686. *
  687. * e.g. `/wp-admin/user/`
  688. *
  689. * Does not inform on whether the user is an admin! Use capability
  690. * checks to tell if the user should be accessing a section or not
  691. * current_user_can().
  692. *
  693. * @since 3.1.0
  694. *
  695. * @global WP_Screen $current_screen
  696. *
  697. * @return bool True if inside WordPress user administration pages.
  698. */
  699. function is_user_admin() {
  700. if ( isset( $GLOBALS['current_screen'] ) )
  701. return $GLOBALS['current_screen']->in_admin( 'user' );
  702. elseif ( defined( 'WP_USER_ADMIN' ) )
  703. return WP_USER_ADMIN;
  704. return false;
  705. }
  706. /**
  707. * If Multisite is enabled.
  708. *
  709. * @since 3.0.0
  710. *
  711. * @return bool True if Multisite is enabled, false otherwise.
  712. */
  713. function is_multisite() {
  714. if ( defined( 'MULTISITE' ) )
  715. return MULTISITE;
  716. if ( defined( 'SUBDOMAIN_INSTALL' ) || defined( 'VHOST' ) || defined( 'SUNRISE' ) )
  717. return true;
  718. return false;
  719. }
  720. /**
  721. * Retrieve the current site ID.
  722. *
  723. * @since 3.1.0
  724. *
  725. * @global int $blog_id
  726. *
  727. * @return int Site ID.
  728. */
  729. function get_current_blog_id() {
  730. global $blog_id;
  731. return absint($blog_id);
  732. }
  733. /**
  734. * Retrieves the current network ID.
  735. *
  736. * @since 4.6.0
  737. *
  738. * @return int The ID of the current network.
  739. */
  740. function get_current_network_id() {
  741. if ( ! is_multisite() ) {
  742. return 1;
  743. }
  744. $current_network = get_network();
  745. if ( ! isset( $current_network->id ) ) {
  746. return get_main_network_id();
  747. }
  748. return absint( $current_network->id );
  749. }
  750. /**
  751. * Attempt an early load of translations.
  752. *
  753. * Used for errors encountered during the initial loading process, before
  754. * the locale has been properly detected and loaded.
  755. *
  756. * Designed for unusual load sequences (like setup-config.php) or for when
  757. * the script will then terminate with an error, otherwise there is a risk
  758. * that a file can be double-included.
  759. *
  760. * @since 3.4.0
  761. * @access private
  762. *
  763. * @global WP_Locale $wp_locale The WordPress date and time locale object.
  764. *
  765. * @staticvar bool $loaded
  766. */
  767. function wp_load_translations_early() {
  768. global $wp_locale;
  769. static $loaded = false;
  770. if ( $loaded )
  771. return;
  772. $loaded = true;
  773. if ( function_exists( 'did_action' ) && did_action( 'init' ) )
  774. return;
  775. // We need $wp_local_package
  776. require ABSPATH . WPINC . '/version.php';
  777. // Translation and localization
  778. require_once ABSPATH . WPINC . '/pomo/mo.php';
  779. require_once ABSPATH . WPINC . '/l10n.php';
  780. require_once ABSPATH . WPINC . '/class-wp-locale.php';
  781. require_once ABSPATH . WPINC . '/class-wp-locale-switcher.php';
  782. // General libraries
  783. require_once ABSPATH . WPINC . '/plugin.php';
  784. $locales = $locations = array();
  785. while ( true ) {
  786. if ( defined( 'WPLANG' ) ) {
  787. if ( '' == WPLANG )
  788. break;
  789. $locales[] = WPLANG;
  790. }
  791. if ( isset( $wp_local_package ) )
  792. $locales[] = $wp_local_package;
  793. if ( ! $locales )
  794. break;
  795. if ( defined( 'WP_LANG_DIR' ) && @is_dir( WP_LANG_DIR ) )
  796. $locations[] = WP_LANG_DIR;
  797. if ( defined( 'WP_CONTENT_DIR' ) && @is_dir( WP_CONTENT_DIR . '/languages' ) )
  798. $locations[] = WP_CONTENT_DIR . '/languages';
  799. if ( @is_dir( ABSPATH . 'wp-content/languages' ) )
  800. $locations[] = ABSPATH . 'wp-content/languages';
  801. if ( @is_dir( ABSPATH . WPINC . '/languages' ) )
  802. $locations[] = ABSPATH . WPINC . '/languages';
  803. if ( ! $locations )
  804. break;
  805. $locations = array_unique( $locations );
  806. foreach ( $locales as $locale ) {
  807. foreach ( $locations as $location ) {
  808. if ( file_exists( $location . '/' . $locale . '.mo' ) ) {
  809. load_textdomain( 'default', $location . '/' . $locale . '.mo' );
  810. if ( defined( 'WP_SETUP_CONFIG' ) && file_exists( $location . '/admin-' . $locale . '.mo' ) )
  811. load_textdomain( 'default', $location . '/admin-' . $locale . '.mo' );
  812. break 2;
  813. }
  814. }
  815. }
  816. break;
  817. }
  818. $wp_locale = new WP_Locale();
  819. }
  820. /**
  821. * Check or set whether WordPress is in "installation" mode.
  822. *
  823. * If the `WP_INSTALLING` constant is defined during the bootstrap, `wp_installing()` will default to `true`.
  824. *
  825. * @since 4.4.0
  826. *
  827. * @staticvar bool $installing
  828. *
  829. * @param bool $is_installing Optional. True to set WP into Installing mode, false to turn Installing mode off.
  830. * Omit this parameter if you only want to fetch the current status.
  831. * @return bool True if WP is installing, otherwise false. When a `$is_installing` is passed, the function will
  832. * report whether WP was in installing mode prior to the change to `$is_installing`.
  833. */
  834. function wp_installing( $is_installing = null ) {
  835. static $installing = null;
  836. // Support for the `WP_INSTALLING` constant, defined before WP is loaded.
  837. if ( is_null( $installing ) ) {
  838. $installing = defined( 'WP_INSTALLING' ) && WP_INSTALLING;
  839. }
  840. if ( ! is_null( $is_installing ) ) {
  841. $old_installing = $installing;
  842. $installing = $is_installing;
  843. return (bool) $old_installing;
  844. }
  845. return (bool) $installing;
  846. }
  847. /**
  848. * Determines if SSL is used.
  849. *
  850. * @since 2.6.0
  851. * @since 4.6.0 Moved from functions.php to load.php.
  852. *
  853. * @return bool True if SSL, otherwise false.
  854. */
  855. function is_ssl() {
  856. if ( isset( $_SERVER['HTTPS'] ) ) {
  857. if ( 'on' == strtolower( $_SERVER['HTTPS'] ) ) {
  858. return true;
  859. }
  860. if ( '1' == $_SERVER['HTTPS'] ) {
  861. return true;
  862. }
  863. } elseif ( isset($_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
  864. return true;
  865. }
  866. return false;
  867. }
  868. /**
  869. * Converts a shorthand byte value to an integer byte value.
  870. *
  871. * @since 2.3.0
  872. * @since 4.6.0 Moved from media.php to load.php.
  873. *
  874. * @link https://secure.php.net/manual/en/function.ini-get.php
  875. * @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
  876. *
  877. * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
  878. * @return int An integer byte value.
  879. */
  880. function wp_convert_hr_to_bytes( $value ) {
  881. $value = strtolower( trim( $value ) );
  882. $bytes = (int) $value;
  883. if ( false !== strpos( $value, 'g' ) ) {
  884. $bytes *= GB_IN_BYTES;
  885. } elseif ( false !== strpos( $value, 'm' ) ) {
  886. $bytes *= MB_IN_BYTES;
  887. } elseif ( false !== strpos( $value, 'k' ) ) {
  888. $bytes *= KB_IN_BYTES;
  889. }
  890. // Deal with large (float) values which run into the maximum integer size.
  891. return min( $bytes, PHP_INT_MAX );
  892. }
  893. /**
  894. * Determines whether a PHP ini value is changeable at runtime.
  895. *
  896. * @since 4.6.0
  897. *
  898. * @link https://secure.php.net/manual/en/function.ini-get-all.php
  899. *
  900. * @param string $setting The name of the ini setting to check.
  901. * @return bool True if the value is changeable at runtime. False otherwise.
  902. */
  903. function wp_is_ini_value_changeable( $setting ) {
  904. static $ini_all;
  905. if ( ! isset( $ini_all ) ) {
  906. $ini_all = false;
  907. // Sometimes `ini_get_all()` is disabled via the `disable_functions` option for "security purposes".
  908. if ( function_exists( 'ini_get_all' ) ) {
  909. $ini_all = ini_get_all();
  910. }
  911. }
  912. // Bit operator to workaround https://bugs.php.net/bug.php?id=44936 which changes access level to 63 in PHP 5.2.6 - 5.2.17.
  913. if ( isset( $ini_all[ $setting ]['access'] ) && ( INI_ALL === ( $ini_all[ $setting ]['access'] & 7 ) || INI_USER === ( $ini_all[ $setting ]['access'] & 7 ) ) ) {
  914. return true;
  915. }
  916. // If we were unable to retrieve the details, fail gracefully to assume it's changeable.
  917. if ( ! is_array( $ini_all ) ) {
  918. return true;
  919. }
  920. return false;
  921. }
  922. /**
  923. * Determines whether the current request is a WordPress Ajax request.
  924. *
  925. * @since 4.7.0
  926. *
  927. * @return bool True if it's a WordPress Ajax request, false otherwise.
  928. */
  929. function wp_doing_ajax() {
  930. /**
  931. * Filters whether the current request is a WordPress Ajax request.
  932. *
  933. * @since 4.7.0
  934. *
  935. * @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
  936. */
  937. return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
  938. }
  939. /**
  940. * Check whether variable is a WordPress Error.
  941. *
  942. * Returns true if $thing is an object of the WP_Error class.
  943. *
  944. * @since 2.1.0
  945. *
  946. * @param mixed $thing Check if unknown variable is a WP_Error object.
  947. * @return bool True, if WP_Error. False, if not WP_Error.
  948. */
  949. function is_wp_error( $thing ) {
  950. return ( $thing instanceof WP_Error );
  951. }