]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/load.php
WordPress 4.7.1
[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         global $wp_filter;
476
477         $first_init = false;
478         if ( ! function_exists( 'wp_cache_init' ) ) {
479                 if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
480                         require_once ( WP_CONTENT_DIR . '/object-cache.php' );
481                         if ( function_exists( 'wp_cache_init' ) ) {
482                                 wp_using_ext_object_cache( true );
483                         }
484
485                         // Re-initialize any hooks added manually by object-cache.php
486                         if ( $wp_filter ) {
487                                 $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
488                         }
489                 }
490
491                 $first_init = true;
492         } elseif ( ! wp_using_ext_object_cache() && file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
493                 /*
494                  * Sometimes advanced-cache.php can load object-cache.php before
495                  * it is loaded here. This breaks the function_exists check above
496                  * and can result in `$_wp_using_ext_object_cache` being set
497                  * incorrectly. Double check if an external cache exists.
498                  */
499                 wp_using_ext_object_cache( true );
500         }
501
502         if ( ! wp_using_ext_object_cache() ) {
503                 require_once ( ABSPATH . WPINC . '/cache.php' );
504         }
505
506         /*
507          * If cache supports reset, reset instead of init if already
508          * initialized. Reset signals to the cache that global IDs
509          * have changed and it may need to update keys and cleanup caches.
510          */
511         if ( ! $first_init && function_exists( 'wp_cache_switch_to_blog' ) ) {
512                 wp_cache_switch_to_blog( get_current_blog_id() );
513         } elseif ( function_exists( 'wp_cache_init' ) ) {
514                 wp_cache_init();
515         }
516
517         if ( function_exists( 'wp_cache_add_global_groups' ) ) {
518                 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' ) );
519                 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
520         }
521 }
522
523 /**
524  * Redirect to the installer if WordPress is not installed.
525  *
526  * Dies with an error message when Multisite is enabled.
527  *
528  * @since 3.0.0
529  * @access private
530  */
531 function wp_not_installed() {
532         if ( is_multisite() ) {
533                 if ( ! is_blog_installed() && ! wp_installing() ) {
534                         nocache_headers();
535
536                         wp_die( __( 'The site you have requested is not installed properly. Please contact the system administrator.' ) );
537                 }
538         } elseif ( ! is_blog_installed() && ! wp_installing() ) {
539                 nocache_headers();
540
541                 require( ABSPATH . WPINC . '/kses.php' );
542                 require( ABSPATH . WPINC . '/pluggable.php' );
543                 require( ABSPATH . WPINC . '/formatting.php' );
544
545                 $link = wp_guess_url() . '/wp-admin/install.php';
546
547                 wp_redirect( $link );
548                 die();
549         }
550 }
551
552 /**
553  * Retrieve an array of must-use plugin files.
554  *
555  * The default directory is wp-content/mu-plugins. To change the default
556  * directory manually, define `WPMU_PLUGIN_DIR` and `WPMU_PLUGIN_URL`
557  * in wp-config.php.
558  *
559  * @since 3.0.0
560  * @access private
561  *
562  * @return array Files to include.
563  */
564 function wp_get_mu_plugins() {
565         $mu_plugins = array();
566         if ( !is_dir( WPMU_PLUGIN_DIR ) )
567                 return $mu_plugins;
568         if ( ! $dh = opendir( WPMU_PLUGIN_DIR ) )
569                 return $mu_plugins;
570         while ( ( $plugin = readdir( $dh ) ) !== false ) {
571                 if ( substr( $plugin, -4 ) == '.php' )
572                         $mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin;
573         }
574         closedir( $dh );
575         sort( $mu_plugins );
576
577         return $mu_plugins;
578 }
579
580 /**
581  * Retrieve an array of active and valid plugin files.
582  *
583  * While upgrading or installing WordPress, no plugins are returned.
584  *
585  * The default directory is wp-content/plugins. To change the default
586  * directory manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL`
587  * in wp-config.php.
588  *
589  * @since 3.0.0
590  * @access private
591  *
592  * @return array Files.
593  */
594 function wp_get_active_and_valid_plugins() {
595         $plugins = array();
596         $active_plugins = (array) get_option( 'active_plugins', array() );
597
598         // Check for hacks file if the option is enabled
599         if ( get_option( 'hack_file' ) && file_exists( ABSPATH . 'my-hacks.php' ) ) {
600                 _deprecated_file( 'my-hacks.php', '1.5.0' );
601                 array_unshift( $plugins, ABSPATH . 'my-hacks.php' );
602         }
603
604         if ( empty( $active_plugins ) || wp_installing() )
605                 return $plugins;
606
607         $network_plugins = is_multisite() ? wp_get_active_network_plugins() : false;
608
609         foreach ( $active_plugins as $plugin ) {
610                 if ( ! validate_file( $plugin ) // $plugin must validate as file
611                         && '.php' == substr( $plugin, -4 ) // $plugin must end with '.php'
612                         && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
613                         // not already included as a network plugin
614                         && ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins ) )
615                         )
616                 $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
617         }
618         return $plugins;
619 }
620
621 /**
622  * Set internal encoding.
623  *
624  * In most cases the default internal encoding is latin1, which is
625  * of no use, since we want to use the `mb_` functions for `utf-8` strings.
626  *
627  * @since 3.0.0
628  * @access private
629  */
630 function wp_set_internal_encoding() {
631         if ( function_exists( 'mb_internal_encoding' ) ) {
632                 $charset = get_option( 'blog_charset' );
633                 if ( ! $charset || ! @mb_internal_encoding( $charset ) )
634                         mb_internal_encoding( 'UTF-8' );
635         }
636 }
637
638 /**
639  * Add magic quotes to `$_GET`, `$_POST`, `$_COOKIE`, and `$_SERVER`.
640  *
641  * Also forces `$_REQUEST` to be `$_GET + $_POST`. If `$_SERVER`,
642  * `$_COOKIE`, or `$_ENV` are needed, use those superglobals directly.
643  *
644  * @since 3.0.0
645  * @access private
646  */
647 function wp_magic_quotes() {
648         // If already slashed, strip.
649         if ( get_magic_quotes_gpc() ) {
650                 $_GET    = stripslashes_deep( $_GET    );
651                 $_POST   = stripslashes_deep( $_POST   );
652                 $_COOKIE = stripslashes_deep( $_COOKIE );
653         }
654
655         // Escape with wpdb.
656         $_GET    = add_magic_quotes( $_GET    );
657         $_POST   = add_magic_quotes( $_POST   );
658         $_COOKIE = add_magic_quotes( $_COOKIE );
659         $_SERVER = add_magic_quotes( $_SERVER );
660
661         // Force REQUEST to be GET + POST.
662         $_REQUEST = array_merge( $_GET, $_POST );
663 }
664
665 /**
666  * Runs just before PHP shuts down execution.
667  *
668  * @since 1.2.0
669  * @access private
670  */
671 function shutdown_action_hook() {
672         /**
673          * Fires just before PHP shuts down execution.
674          *
675          * @since 1.2.0
676          */
677         do_action( 'shutdown' );
678
679         wp_cache_close();
680 }
681
682 /**
683  * Copy an object.
684  *
685  * @since 2.7.0
686  * @deprecated 3.2.0
687  *
688  * @param object $object The object to clone.
689  * @return object The cloned object.
690  */
691 function wp_clone( $object ) {
692         // Use parens for clone to accommodate PHP 4. See #17880
693         return clone( $object );
694 }
695
696 /**
697  * Whether the current request is for an administrative interface page.
698  *
699  * Does not check if the user is an administrator; current_user_can()
700  * for checking roles and capabilities.
701  *
702  * @since 1.5.1
703  *
704  * @global WP_Screen $current_screen
705  *
706  * @return bool True if inside WordPress administration interface, false otherwise.
707  */
708 function is_admin() {
709         if ( isset( $GLOBALS['current_screen'] ) )
710                 return $GLOBALS['current_screen']->in_admin();
711         elseif ( defined( 'WP_ADMIN' ) )
712                 return WP_ADMIN;
713
714         return false;
715 }
716
717 /**
718  * Whether the current request is for a site's admininstrative interface.
719  *
720  * e.g. `/wp-admin/`
721  *
722  * Does not check if the user is an administrator; current_user_can()
723  * for checking roles and capabilities.
724  *
725  * @since 3.1.0
726  *
727  * @global WP_Screen $current_screen
728  *
729  * @return bool True if inside WordPress blog administration pages.
730  */
731 function is_blog_admin() {
732         if ( isset( $GLOBALS['current_screen'] ) )
733                 return $GLOBALS['current_screen']->in_admin( 'site' );
734         elseif ( defined( 'WP_BLOG_ADMIN' ) )
735                 return WP_BLOG_ADMIN;
736
737         return false;
738 }
739
740 /**
741  * Whether the current request is for the network administrative interface.
742  *
743  * e.g. `/wp-admin/network/`
744  *
745  * Does not check if the user is an administrator; current_user_can()
746  * for checking roles and capabilities.
747  *
748  * @since 3.1.0
749  *
750  * @global WP_Screen $current_screen
751  *
752  * @return bool True if inside WordPress network administration pages.
753  */
754 function is_network_admin() {
755         if ( isset( $GLOBALS['current_screen'] ) )
756                 return $GLOBALS['current_screen']->in_admin( 'network' );
757         elseif ( defined( 'WP_NETWORK_ADMIN' ) )
758                 return WP_NETWORK_ADMIN;
759
760         return false;
761 }
762
763 /**
764  * Whether the current request is for a user admin screen.
765  *
766  * e.g. `/wp-admin/user/`
767  *
768  * Does not inform on whether the user is an admin! Use capability
769  * checks to tell if the user should be accessing a section or not
770  * current_user_can().
771  *
772  * @since 3.1.0
773  *
774  * @global WP_Screen $current_screen
775  *
776  * @return bool True if inside WordPress user administration pages.
777  */
778 function is_user_admin() {
779         if ( isset( $GLOBALS['current_screen'] ) )
780                 return $GLOBALS['current_screen']->in_admin( 'user' );
781         elseif ( defined( 'WP_USER_ADMIN' ) )
782                 return WP_USER_ADMIN;
783
784         return false;
785 }
786
787 /**
788  * If Multisite is enabled.
789  *
790  * @since 3.0.0
791  *
792  * @return bool True if Multisite is enabled, false otherwise.
793  */
794 function is_multisite() {
795         if ( defined( 'MULTISITE' ) )
796                 return MULTISITE;
797
798         if ( defined( 'SUBDOMAIN_INSTALL' ) || defined( 'VHOST' ) || defined( 'SUNRISE' ) )
799                 return true;
800
801         return false;
802 }
803
804 /**
805  * Retrieve the current site ID.
806  *
807  * @since 3.1.0
808  *
809  * @global int $blog_id
810  *
811  * @return int Site ID.
812  */
813 function get_current_blog_id() {
814         global $blog_id;
815         return absint($blog_id);
816 }
817
818 /**
819  * Retrieves the current network ID.
820  *
821  * @since 4.6.0
822  *
823  * @return int The ID of the current network.
824  */
825 function get_current_network_id() {
826         if ( ! is_multisite() ) {
827                 return 1;
828         }
829
830         $current_network = get_network();
831
832         if ( ! isset( $current_network->id ) ) {
833                 return get_main_network_id();
834         }
835
836         return absint( $current_network->id );
837 }
838
839 /**
840  * Attempt an early load of translations.
841  *
842  * Used for errors encountered during the initial loading process, before
843  * the locale has been properly detected and loaded.
844  *
845  * Designed for unusual load sequences (like setup-config.php) or for when
846  * the script will then terminate with an error, otherwise there is a risk
847  * that a file can be double-included.
848  *
849  * @since 3.4.0
850  * @access private
851  *
852  * @global WP_Locale $wp_locale The WordPress date and time locale object.
853  *
854  * @staticvar bool $loaded
855  */
856 function wp_load_translations_early() {
857         global $wp_locale;
858
859         static $loaded = false;
860         if ( $loaded )
861                 return;
862         $loaded = true;
863
864         if ( function_exists( 'did_action' ) && did_action( 'init' ) )
865                 return;
866
867         // We need $wp_local_package
868         require ABSPATH . WPINC . '/version.php';
869
870         // Translation and localization
871         require_once ABSPATH . WPINC . '/pomo/mo.php';
872         require_once ABSPATH . WPINC . '/l10n.php';
873         require_once ABSPATH . WPINC . '/class-wp-locale.php';
874         require_once ABSPATH . WPINC . '/class-wp-locale-switcher.php';
875
876         // General libraries
877         require_once ABSPATH . WPINC . '/plugin.php';
878
879         $locales = $locations = array();
880
881         while ( true ) {
882                 if ( defined( 'WPLANG' ) ) {
883                         if ( '' == WPLANG )
884                                 break;
885                         $locales[] = WPLANG;
886                 }
887
888                 if ( isset( $wp_local_package ) )
889                         $locales[] = $wp_local_package;
890
891                 if ( ! $locales )
892                         break;
893
894                 if ( defined( 'WP_LANG_DIR' ) && @is_dir( WP_LANG_DIR ) )
895                         $locations[] = WP_LANG_DIR;
896
897                 if ( defined( 'WP_CONTENT_DIR' ) && @is_dir( WP_CONTENT_DIR . '/languages' ) )
898                         $locations[] = WP_CONTENT_DIR . '/languages';
899
900                 if ( @is_dir( ABSPATH . 'wp-content/languages' ) )
901                         $locations[] = ABSPATH . 'wp-content/languages';
902
903                 if ( @is_dir( ABSPATH . WPINC . '/languages' ) )
904                         $locations[] = ABSPATH . WPINC . '/languages';
905
906                 if ( ! $locations )
907                         break;
908
909                 $locations = array_unique( $locations );
910
911                 foreach ( $locales as $locale ) {
912                         foreach ( $locations as $location ) {
913                                 if ( file_exists( $location . '/' . $locale . '.mo' ) ) {
914                                         load_textdomain( 'default', $location . '/' . $locale . '.mo' );
915                                         if ( defined( 'WP_SETUP_CONFIG' ) && file_exists( $location . '/admin-' . $locale . '.mo' ) )
916                                                 load_textdomain( 'default', $location . '/admin-' . $locale . '.mo' );
917                                         break 2;
918                                 }
919                         }
920                 }
921
922                 break;
923         }
924
925         $wp_locale = new WP_Locale();
926 }
927
928 /**
929  * Check or set whether WordPress is in "installation" mode.
930  *
931  * If the `WP_INSTALLING` constant is defined during the bootstrap, `wp_installing()` will default to `true`.
932  *
933  * @since 4.4.0
934  *
935  * @staticvar bool $installing
936  *
937  * @param bool $is_installing Optional. True to set WP into Installing mode, false to turn Installing mode off.
938  *                            Omit this parameter if you only want to fetch the current status.
939  * @return bool True if WP is installing, otherwise false. When a `$is_installing` is passed, the function will
940  *              report whether WP was in installing mode prior to the change to `$is_installing`.
941  */
942 function wp_installing( $is_installing = null ) {
943         static $installing = null;
944
945         // Support for the `WP_INSTALLING` constant, defined before WP is loaded.
946         if ( is_null( $installing ) ) {
947                 $installing = defined( 'WP_INSTALLING' ) && WP_INSTALLING;
948         }
949
950         if ( ! is_null( $is_installing ) ) {
951                 $old_installing = $installing;
952                 $installing = $is_installing;
953                 return (bool) $old_installing;
954         }
955
956         return (bool) $installing;
957 }
958
959 /**
960  * Determines if SSL is used.
961  *
962  * @since 2.6.0
963  * @since 4.6.0 Moved from functions.php to load.php.
964  *
965  * @return bool True if SSL, otherwise false.
966  */
967 function is_ssl() {
968         if ( isset( $_SERVER['HTTPS'] ) ) {
969                 if ( 'on' == strtolower( $_SERVER['HTTPS'] ) ) {
970                         return true;
971                 }
972
973                 if ( '1' == $_SERVER['HTTPS'] ) {
974                         return true;
975                 }
976         } elseif ( isset($_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
977                 return true;
978         }
979         return false;
980 }
981
982 /**
983  * Converts a shorthand byte value to an integer byte value.
984  *
985  * @since 2.3.0
986  * @since 4.6.0 Moved from media.php to load.php.
987  *
988  * @link https://secure.php.net/manual/en/function.ini-get.php
989  * @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
990  *
991  * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
992  * @return int An integer byte value.
993  */
994 function wp_convert_hr_to_bytes( $value ) {
995         $value = strtolower( trim( $value ) );
996         $bytes = (int) $value;
997
998         if ( false !== strpos( $value, 'g' ) ) {
999                 $bytes *= GB_IN_BYTES;
1000         } elseif ( false !== strpos( $value, 'm' ) ) {
1001                 $bytes *= MB_IN_BYTES;
1002         } elseif ( false !== strpos( $value, 'k' ) ) {
1003                 $bytes *= KB_IN_BYTES;
1004         }
1005
1006         // Deal with large (float) values which run into the maximum integer size.
1007         return min( $bytes, PHP_INT_MAX );
1008 }
1009
1010 /**
1011  * Determines whether a PHP ini value is changeable at runtime.
1012  *
1013  * @since 4.6.0
1014  *
1015  * @link https://secure.php.net/manual/en/function.ini-get-all.php
1016  *
1017  * @param string $setting The name of the ini setting to check.
1018  * @return bool True if the value is changeable at runtime. False otherwise.
1019  */
1020 function wp_is_ini_value_changeable( $setting ) {
1021         static $ini_all;
1022
1023         if ( ! isset( $ini_all ) ) {
1024                 $ini_all = false;
1025                 // Sometimes `ini_get_all()` is disabled via the `disable_functions` option for "security purposes".
1026                 if ( function_exists( 'ini_get_all' ) ) {
1027                         $ini_all = ini_get_all();
1028                 }
1029         }
1030
1031         // 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.
1032         if ( isset( $ini_all[ $setting ]['access'] ) && ( INI_ALL === ( $ini_all[ $setting ]['access'] & 7 ) || INI_USER === ( $ini_all[ $setting ]['access'] & 7 ) ) ) {
1033                 return true;
1034         }
1035
1036         // If we were unable to retrieve the details, fail gracefully to assume it's changeable.
1037         if ( ! is_array( $ini_all ) ) {
1038                 return true;
1039         }
1040
1041         return false;
1042 }
1043
1044 /**
1045  * Determines whether the current request is a WordPress Ajax request.
1046  *
1047  * @since 4.7.0
1048  *
1049  * @return bool True if it's a WordPress Ajax request, false otherwise.
1050  */
1051 function wp_doing_ajax() {
1052         /**
1053          * Filters whether the current request is a WordPress Ajax request.
1054          *
1055          * @since 4.7.0
1056          *
1057          * @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
1058          */
1059         return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
1060 }
1061
1062 /**
1063  * Check whether variable is a WordPress Error.
1064  *
1065  * Returns true if $thing is an object of the WP_Error class.
1066  *
1067  * @since 2.1.0
1068  *
1069  * @param mixed $thing Check if unknown variable is a WP_Error object.
1070  * @return bool True, if WP_Error. False, if not WP_Error.
1071  */
1072 function is_wp_error( $thing ) {
1073         return ( $thing instanceof WP_Error );
1074 }