home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-includes / load.php < prev    next >
Encoding:
PHP Script  |  2017-10-18  |  33.1 KB  |  1,160 lines

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