PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/site/wp-includes/update.php

https://github.com/jptoto/barcampphilly
PHP | 173 lines | 108 code | 32 blank | 33 comment | 27 complexity | b028be36d4259a6f6737587031af3f6e MD5 | raw file
  1. <?php
  2. /**
  3. * A simple set of functions to check our version 1.0 update service
  4. *
  5. * @package WordPress
  6. * @since 2.3
  7. */
  8. /**
  9. * wp_version_check() - Check WordPress version against the newest version.
  10. *
  11. * The WordPress version, PHP version, and Locale is sent. Checks against the WordPress server at
  12. * api.wordpress.org. Will only check if PHP has fsockopen enabled and WordPress isn't installing.
  13. *
  14. * @package WordPress
  15. * @since 2.3
  16. * @uses $wp_version Used to check against the newest WordPress version.
  17. *
  18. * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
  19. */
  20. function wp_version_check() {
  21. if ( !function_exists('fsockopen') || defined('WP_INSTALLING') )
  22. return;
  23. global $wp_version;
  24. $php_version = phpversion();
  25. $current = get_option( 'update_core' );
  26. $locale = get_locale();
  27. if (
  28. isset( $current->last_checked ) &&
  29. 43200 > ( time() - $current->last_checked ) &&
  30. $current->version_checked == $wp_version
  31. )
  32. return false;
  33. $new_option = '';
  34. $new_option->last_checked = time(); // this gets set whether we get a response or not, so if something is down or misconfigured it won't delay the page load for more than 3 seconds, twice a day
  35. $new_option->version_checked = $wp_version;
  36. $http_request = "GET /core/version-check/1.1/?version=$wp_version&php=$php_version&locale=$locale HTTP/1.0\r\n";
  37. $http_request .= "Host: api.wordpress.org\r\n";
  38. $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_option('blog_charset') . "\r\n";
  39. $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
  40. $http_request .= "\r\n";
  41. $response = '';
  42. if ( false !== ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3 ) ) && is_resource($fs) ) {
  43. fwrite( $fs, $http_request );
  44. while ( !feof( $fs ) )
  45. $response .= fgets( $fs, 1160 ); // One TCP-IP packet
  46. fclose( $fs );
  47. $response = explode("\r\n\r\n", $response, 2);
  48. if ( !preg_match( '|HTTP/.*? 200|', $response[0] ) )
  49. return false;
  50. $body = trim( $response[1] );
  51. $body = str_replace(array("\r\n", "\r"), "\n", $body);
  52. $returns = explode("\n", $body);
  53. $new_option->response = attribute_escape( $returns[0] );
  54. if ( isset( $returns[1] ) )
  55. $new_option->url = clean_url( $returns[1] );
  56. if ( isset( $returns[2] ) )
  57. $new_option->current = attribute_escape( $returns[2] );
  58. }
  59. update_option( 'update_core', $new_option );
  60. }
  61. add_action( 'init', 'wp_version_check' );
  62. /**
  63. * wp_update_plugins() - Check plugin versions against the latest versions hosted on WordPress.org.
  64. *
  65. * The WordPress version, PHP version, and Locale is sent along with a list of all plugins installed.
  66. * Checks against the WordPress server at api.wordpress.org.
  67. * Will only check if PHP has fsockopen enabled and WordPress isn't installing.
  68. *
  69. * @package WordPress
  70. * @since 2.3
  71. * @uses $wp_version Used to notidy the WordPress version.
  72. *
  73. * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
  74. */
  75. function wp_update_plugins() {
  76. global $wp_version;
  77. if ( !function_exists('fsockopen') || defined('WP_INSTALLING') )
  78. return false;
  79. // If running blog-side, bail unless we've not checked in the last 12 hours
  80. if ( !function_exists( 'get_plugins' ) )
  81. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  82. $plugins = get_plugins();
  83. $active = get_option( 'active_plugins' );
  84. $current = get_option( 'update_plugins' );
  85. $new_option = '';
  86. $new_option->last_checked = time();
  87. $time_not_changed = isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked );
  88. $plugin_changed = false;
  89. foreach ( $plugins as $file => $p ) {
  90. $new_option->checked[ $file ] = $p['Version'];
  91. if ( !isset( $current->checked[ $file ] ) ) {
  92. $plugin_changed = true;
  93. continue;
  94. }
  95. if ( strval($current->checked[ $file ]) !== strval($p['Version']) )
  96. $plugin_changed = true;
  97. }
  98. foreach ( (array) $current->response as $plugin_file => $update_details ) {
  99. if ( ! isset($plugins[ $plugin_file ]) ) {
  100. $plugin_changed = true;
  101. }
  102. }
  103. // Bail if we've checked in the last 12 hours and if nothing has changed
  104. if ( $time_not_changed && !$plugin_changed )
  105. return false;
  106. $to_send->plugins = $plugins;
  107. $to_send->active = $active;
  108. $send = serialize( $to_send );
  109. $request = 'plugins=' . urlencode( $send );
  110. $http_request = "POST /plugins/update-check/1.0/ HTTP/1.0\r\n";
  111. $http_request .= "Host: api.wordpress.org\r\n";
  112. $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_option('blog_charset') . "\r\n";
  113. $http_request .= "Content-Length: " . strlen($request) . "\r\n";
  114. $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
  115. $http_request .= "\r\n";
  116. $http_request .= $request;
  117. $response = '';
  118. if( false != ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3) ) && is_resource($fs) ) {
  119. fwrite($fs, $http_request);
  120. while ( !feof($fs) )
  121. $response .= fgets($fs, 1160); // One TCP-IP packet
  122. fclose($fs);
  123. $response = explode("\r\n\r\n", $response, 2);
  124. }
  125. $response = unserialize( $response[1] );
  126. if ( $response )
  127. $new_option->response = $response;
  128. update_option( 'update_plugins', $new_option );
  129. }
  130. function _maybe_update_plugins() {
  131. $current = get_option( 'update_plugins' );
  132. if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
  133. return;
  134. wp_update_plugins();
  135. }
  136. add_action( 'load-plugins.php', 'wp_update_plugins' );
  137. add_action( 'admin_init', '_maybe_update_plugins' );
  138. add_action( 'wp_update_plugins', 'wp_update_plugins' );
  139. if ( !wp_next_scheduled('wp_update_plugins') )
  140. wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
  141. ?>