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