Changeset 6779 for trunk/wp-admin/includes/file.php
- Timestamp:
- 02/11/2008 05:45:54 AM (18 years ago)
- File:
-
- 1 edited
-
trunk/wp-admin/includes/file.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/includes/file.php
r6551 r6779 172 172 } 173 173 174 /** 175 * Downloads a url to a local file using the Snoopy HTTP Class 176 * 177 * @param string $url the URL of the file to download 178 * @return mixed false on failure, string Filename on success. 179 */ 180 function download_url( $url ) { 181 //WARNING: The file is not automatically deleted, The script must unlink() the file. 182 if( ! $url ) 183 return false; 184 185 $tmpfname = tempnam('/tmp', 'wpupdate'); 186 if( ! $tmpfname ) 187 return false; 188 189 $handle = fopen($tmpfname, 'w'); 190 if( ! $handle ) 191 return false; 192 193 require_once( ABSPATH . 'wp-includes/class-snoopy.php' ); 194 $snoopy = new Snoopy(); 195 $snoopy->fetch($url); 196 197 fwrite($handle, $snoopy->results); 198 fclose($handle); 199 200 return $tmpfname; 201 } 202 203 function unzip_file($file, $to) { 204 global $wp_filesystem; 205 206 if ( ! $wp_filesystem || !is_object($wp_filesystem) ) 207 return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 208 209 $fs =& $wp_filesystem; 210 211 require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php'); 212 213 $archive = new PclZip($file); 214 215 // Is the archive valid? 216 if ( false == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING)) ) 217 return new WP_Error('incompatible_archive', __('Incompatible archive'), $archive->error_string); 218 219 if ( 0 == count($archive_files) ) 220 return new WP_Error('empty_archive', __('Empty archive')); 221 222 $to = trailingslashit($to); 223 $path = explode('/', $to); 224 $tmppath = ''; 225 for ( $j = 0; $j < count($path) - 1; $j++ ) { 226 $tmppath .= $path[$j] . '/'; 227 if ( ! $fs->is_dir($tmppath) ) { 228 $fs->mkdir($tmppath); 229 } else { 230 $fs->setDefaultPermissions( $fs->getchmod($tmppath) ); 231 } 232 } 233 234 foreach ($archive_files as $file) { 235 $path = explode('/', $file['filename']); 236 $tmppath = ''; 237 238 // Loop through each of the items and check that the folder exists. 239 for ( $j = 0; $j < count($path) - 1; $j++ ) { 240 $tmppath .= $path[$j] . '/'; 241 if ( ! $fs->is_dir($to . $tmppath) ) 242 $fs->mkdir($to . $tmppath); 243 } 244 245 // We've made sure the folders are there, so let's extract the file now: 246 if ( ! $file['folder'] ) 247 $fs->put_contents( $to . $file['filename'], $file['content']); 248 } 249 } 250 251 function copy_dir($from, $to) { 252 global $wp_filesystem; 253 254 $dirlist = $wp_filesystem->dirlist($from); 255 256 $from = trailingslashit($from); 257 $to = trailingslashit($to); 258 259 foreach ( (array) $dirlist as $filename => $fileinfo ) { 260 if ( 'file' == $fileinfo['type'] ) { 261 $wp_filesystem->copy($from . $filename, $to . $filename, true); 262 } elseif ( 'folder' == $fileinfo['type'] ) { 263 $wp_filesystem->mkdir($to . $filename); 264 copy_dir($from . $filename, $to . $filename); 265 } 266 } 267 } 268 269 function WP_Filesystem( $args = false, $preference = false ) { 270 global $wp_filesystem; 271 272 $method = get_filesystem_method($preference); 273 if ( ! $method ) 274 return false; 275 276 require_once('class-wp-filesystem-'.$method.'.php'); 277 $method = "WP_Filesystem_$method"; 278 279 $wp_filesystem = new $method($args); 280 281 if ( $wp_filesystem->errors->get_error_code() ) 282 return false; 283 284 if ( !$wp_filesystem->connect() ) 285 return false; //There was an erorr connecting to the server. 286 287 return true; 288 } 289 290 function get_filesystem_method() { 291 $tempFile = tempnam('/tmp', 'WPU'); 292 293 if ( getmyuid() == fileowner($tempFile) ) { 294 unlink($tempFile); 295 //return 'direct'; 296 } else { 297 unlink($tempFile); 298 } 299 300 if ( extension_loaded('ftp') ) return 'ftpext'; 301 if ( extension_loaded('sockets') ) return 'ftpsockets'; 302 return false; 303 } 304 174 305 ?>
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)