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