]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/update.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-admin / includes / update.php
1 <?php
2
3 // The admin side of our 1.1 update system
4
5 function core_update_footer( $msg = '' ) {
6         if ( !current_user_can('manage_options') )
7                 return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'] );
8
9         $cur = get_option( 'update_core' );
10
11         switch ( $cur->response ) {
12         case 'development' :
13                 return sprintf( '| '.__( 'You are using a development version (%s). Cool! Please <a href="%s">stay updated</a>.' ), $GLOBALS['wp_version'], $cur->url, $cur->current );
14         break;
15
16         case 'upgrade' :
17                 if ( current_user_can('manage_options') ) {
18                         return sprintf( '| <strong>'.__( '<a href="%2$s">Get Version %3$s</a>' ).'</strong>', $GLOBALS['wp_version'], $cur->url, $cur->current );
19                         break;
20                 }
21
22         case 'latest' :
23         default :
24                 return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'], $cur->url, $cur->current );
25         break;
26         }
27 }
28 add_filter( 'update_footer', 'core_update_footer' );
29
30 function update_nag() {
31         $cur = get_option( 'update_core' );
32
33         if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
34                 return false;
35
36         if ( current_user_can('manage_options') )
37                 $msg = sprintf( __('WordPress %2$s is available! <a href="%1$s">Please update now</a>.'), $cur->url, $cur->current );
38         else
39                 $msg = sprintf( __('WordPress %2$s is available! Please notify the site administrator.'), $cur->url, $cur->current );
40
41         echo "<div id='update-nag'>$msg</div>";
42 }
43 add_action( 'admin_notices', 'update_nag', 3 );
44
45 // Called directly from dashboard
46 function update_right_now_message() {
47         $cur = get_option( 'update_core' );
48
49         $msg = sprintf( __('This is WordPress version %s.'), $GLOBALS['wp_version'] );
50         if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('manage_options') )
51                 $msg .= " <a href='$cur->url' class='rbutton'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
52
53         echo "<span id='wp-version-message'>$msg</span>";
54 }
55
56 function wp_update_plugins() {
57         global $wp_version;
58
59         if ( !function_exists('fsockopen') )
60                 return false;
61
62         $plugins = get_plugins();
63         $active  = get_option( 'active_plugins' );
64         $current = get_option( 'update_plugins' );
65
66         $new_option = '';
67         $new_option->last_checked = time();
68
69         $plugin_changed = false;
70         foreach ( $plugins as $file => $p ) {
71                 $new_option->checked[ $file ] = $p['Version'];
72
73                 if ( !isset( $current->checked[ $file ] ) ) {
74                         $plugin_changed = true;
75                         continue;
76                 }
77
78                 if ( strval($current->checked[ $file ]) !== strval($p['Version']) )
79                         $plugin_changed = true;
80         }
81
82         if (
83                 isset( $current->last_checked ) &&
84                 43200 > ( time() - $current->last_checked ) &&
85                 !$plugin_changed
86         )
87                 return false;
88
89         $to_send->plugins = $plugins;
90         $to_send->active = $active;
91         $send = serialize( $to_send );
92
93         $request = 'plugins=' . urlencode( $send );
94         $http_request  = "POST /plugins/update-check/1.0/ HTTP/1.0\r\n";
95         $http_request .= "Host: api.wordpress.org\r\n";
96         $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_option('blog_charset') . "\r\n";
97         $http_request .= "Content-Length: " . strlen($request) . "\r\n";
98         $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
99         $http_request .= "\r\n";
100         $http_request .= $request;
101
102         $response = '';
103         if( false != ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3) ) && is_resource($fs) ) {
104                 fwrite($fs, $http_request);
105
106                 while ( !feof($fs) )
107                         $response .= fgets($fs, 1160); // One TCP-IP packet
108                 fclose($fs);
109                 $response = explode("\r\n\r\n", $response, 2);
110         }
111
112         $response = unserialize( $response[1] );
113
114         if ( $response )
115                 $new_option->response = $response;
116
117         update_option( 'update_plugins', $new_option );
118 }
119 add_action( 'load-plugins.php', 'wp_update_plugins' );
120
121 function wp_plugin_update_row( $file ) {
122         global $plugin_data;
123         $current = get_option( 'update_plugins' );
124         if ( !isset( $current->response[ $file ] ) )
125                 return false;
126
127         $r = $current->response[ $file ];
128
129         echo "<tr><td colspan='5' class='plugin-update'>";
130         if ( !current_user_can('edit_plugins') )
131                 printf( __('There is a new version of %1$s available. <a href="%2$s">Download version %3$s here</a>.'), $plugin_data['Name'], $r->url, $r->new_version);
132         else if ( empty($r->package) )
133                 printf( __('There is a new version of %1$s available. <a href="%2$s">Download version %3$s here</a> <em>automatic upgrade unavailable for this plugin</em>.'), $plugin_data['Name'], $r->url, $r->new_version);
134         else
135                 printf( __('There is a new version of %1$s available. <a href="%2$s">Download version %3$s here</a> or <a href="%4$s">upgrade automatically</a>.'), $plugin_data['Name'], $r->url, $r->new_version, wp_nonce_url("update.php?action=upgrade-plugin&amp;plugin=$file", 'upgrade-plugin_' . $file) );
136         
137         echo "</td></tr>";
138 }
139 add_action( 'after_plugin_row', 'wp_plugin_update_row' );
140
141 function wp_update_plugin($plugin, $feedback = '') {
142         global $wp_filesystem;
143
144         if ( !empty($feedback) )
145                 add_filter('update_feedback', $feedback);
146
147         // Is an update available?
148         $current = get_option( 'update_plugins' );
149         if ( !isset( $current->response[ $plugin ] ) )
150                 return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
151
152         // Is a filesystem accessor setup?
153         if ( ! $wp_filesystem || !is_object($wp_filesystem) )
154                 WP_Filesystem();
155
156         if ( ! is_object($wp_filesystem) )
157                 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
158
159         if ( $wp_filesystem->errors->get_error_code() )
160                 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
161
162         //Get the Base folder
163         $base = $wp_filesystem->get_base_dir();
164         
165         if ( empty($base) )
166                 return new WP_Error('fs_nowordpress', __('Unable to locate WordPress directory.'));
167
168         // Get the URL to the zip file
169         $r = $current->response[ $plugin ];
170
171         if ( empty($r->package) )
172                 return new WP_Error('no_package', __('Upgrade package not available.'));
173
174         // Download the package
175         $package = $r->package;
176         apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
177         $file = download_url($package);
178
179         if ( is_wp_error($file) )
180                 return new WP_Error('download_failed', __('Download failed.'), $file->get_error_message());
181
182         $working_dir = $base . 'wp-content/upgrade/' . basename($plugin, '.php');
183
184         // Clean up working directory
185         if ( $wp_filesystem->is_dir($working_dir) )
186                 $wp_filesystem->delete($working_dir, true);
187
188         apply_filters('update_feedback', __('Unpacking the update'));
189         // Unzip package to working directory
190         $result = unzip_file($file, $working_dir);
191         if ( is_wp_error($result) ) {
192                 unlink($file);
193                 $wp_filesystem->delete($working_dir, true);
194                 return $result;
195         }
196
197         // Once extracted, delete the package
198         unlink($file);
199
200         if ( is_plugin_active($plugin) ) {
201                 //Deactivate the plugin silently, Prevent deactivation hooks from running.
202                 apply_filters('update_feedback', __('Deactivating the plugin'));
203                 deactivate_plugins($plugin, true);
204         }
205
206         // Remove the existing plugin.
207         apply_filters('update_feedback', __('Removing the old version of the plugin'));
208         $plugin_dir = dirname($base . PLUGINDIR . "/$plugin");
209         $plugin_dir = trailingslashit($plugin_dir);
210         
211         // If plugin is in its own directory, recursively delete the directory.
212         if ( strpos($plugin, '/') && $plugin_dir != $base . PLUGINDIR . '/' ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
213                 $deleted = $wp_filesystem->delete($plugin_dir, true);
214         else
215                 $deleted = $wp_filesystem->delete($base . PLUGINDIR . "/$plugin");
216
217         if ( !$deleted ) {
218                 $wp_filesystem->delete($working_dir, true);
219                 return new WP_Error('delete_failed', __('Could not remove the old plugin'));
220         }
221
222         apply_filters('update_feedback', __('Installing the latest version'));
223         // Copy new version of plugin into place.
224         if ( !copy_dir($working_dir, $base . PLUGINDIR) ) {
225                 //$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
226                 return new WP_Error('install_failed', __('Installation failed'));
227         }
228
229         //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
230         $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
231
232         // Remove working directory
233         $wp_filesystem->delete($working_dir, true);
234
235         // Force refresh of plugin update information
236         delete_option('update_plugins');
237         
238         if( empty($filelist) )
239                 return false; //We couldnt find any files in the working dir
240         
241         $folder = $filelist[0];
242         $plugin = get_plugins('/' . $folder); //Pass it with a leading slash, search out the plugins in the folder, 
243         $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
244
245         return  $folder . '/' . $pluginfiles[0]; //Pass it without a leading slash as WP requires
246 }
247
248 ?>