load.php 33 KB

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