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