-
+ D15748396BC55BEFC5382493A14277183743BDFBDAC539ADF53761FF46F57F90E2EDD86ED3231AFB0D0F6981E51A1BA1F04756C59CCD4CD100EBEBB737D412B8
mp-wp/wp-admin/includes/file.php
(0 . 0)(1 . 769)
33342 <?php
33343 /**
33344 * File contains all the administration image manipulation functions.
33345 *
33346 * @package WordPress
33347 * @subpackage Administration
33348 */
33349
33350 /** The descriptions for theme files. */
33351 $wp_file_descriptions = array (
33352 'index.php' => __( 'Main Index Template' ),
33353 'style.css' => __( 'Stylesheet' ),
33354 'rtl.css' => __( 'RTL Stylesheet' ),
33355 'comments.php' => __( 'Comments' ),
33356 'comments-popup.php' => __( 'Popup Comments' ),
33357 'footer.php' => __( 'Footer' ),
33358 'header.php' => __( 'Header' ),
33359 'sidebar.php' => __( 'Sidebar' ),
33360 'archive.php' => __( 'Archives' ),
33361 'category.php' => __( 'Category Template' ),
33362 'page.php' => __( 'Page Template' ),
33363 'search.php' => __( 'Search Results' ),
33364 'searchform.php' => __( 'Search Form' ),
33365 'single.php' => __( 'Single Post' ),
33366 '404.php' => __( '404 Template' ),
33367 'link.php' => __( 'Links Template' ),
33368 'functions.php' => __( 'Theme Functions' ),
33369 'attachment.php' => __( 'Attachment Template' ),
33370 'image.php' => __('Image Attachment Template'),
33371 'video.php' => __('Video Attachment Template'),
33372 'audio.php' => __('Audio Attachment Template'),
33373 'application.php' => __('Application Attachment Template'),
33374 'my-hacks.php' => __( 'my-hacks.php (legacy hacks support)' ),
33375 '.htaccess' => __( '.htaccess (for rewrite rules )' ),
33376 // Deprecated files
33377 'wp-layout.css' => __( 'Stylesheet' ), 'wp-comments.php' => __( 'Comments Template' ), 'wp-comments-popup.php' => __( 'Popup Comments Template' ));
33378
33379 /**
33380 * {@internal Missing Short Description}}
33381 *
33382 * @since unknown
33383 *
33384 * @param unknown_type $file
33385 * @return unknown
33386 */
33387 function get_file_description( $file ) {
33388 global $wp_file_descriptions;
33389
33390 if ( isset( $wp_file_descriptions[basename( $file )] ) ) {
33391 return $wp_file_descriptions[basename( $file )];
33392 }
33393 elseif ( file_exists( WP_CONTENT_DIR . $file ) && is_file( WP_CONTENT_DIR . $file ) ) {
33394 $template_data = implode( '', file( WP_CONTENT_DIR . $file ) );
33395 if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ))
33396 return $name[1] . ' Page Template';
33397 }
33398
33399 return basename( $file );
33400 }
33401
33402 /**
33403 * {@internal Missing Short Description}}
33404 *
33405 * @since unknown
33406 *
33407 * @return unknown
33408 */
33409 function get_home_path() {
33410 $home = get_option( 'home' );
33411 if ( $home != '' && $home != get_option( 'siteurl' ) ) {
33412 $home_path = parse_url( $home );
33413 $home_path = $home_path['path'];
33414 $root = str_replace( $_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"] );
33415 $home_path = trailingslashit( $root.$home_path );
33416 } else {
33417 $home_path = ABSPATH;
33418 }
33419
33420 return $home_path;
33421 }
33422
33423 /**
33424 * {@internal Missing Short Description}}
33425 *
33426 * @since unknown
33427 *
33428 * @param unknown_type $file
33429 * @return unknown
33430 */
33431 function get_real_file_to_edit( $file ) {
33432 if ('index.php' == $file || '.htaccess' == $file ) {
33433 $real_file = get_home_path() . $file;
33434 } else {
33435 $real_file = WP_CONTENT_DIR . $file;
33436 }
33437
33438 return $real_file;
33439 }
33440
33441 /**
33442 * {@internal Missing Short Description}}
33443 *
33444 * @since unknown
33445 *
33446 * @param string $folder Optional. Full path to folder
33447 * @param int $levels Optional. Levels of folders to follow, Default: 100 (PHP Loop limit).
33448 * @return bool|array
33449 */
33450 function list_files( $folder = '', $levels = 100 ) {
33451 if( empty($folder) )
33452 return false;
33453
33454 if( ! $levels )
33455 return false;
33456
33457 $files = array();
33458 if ( $dir = @opendir( $folder ) ) {
33459 while (($file = readdir( $dir ) ) !== false ) {
33460 if ( in_array($file, array('.', '..') ) )
33461 continue;
33462 if ( is_dir( $folder . '/' . $file ) ) {
33463 $files2 = list_files( $folder . '/' . $file, $levels - 1);
33464 if( $files2 )
33465 $files = array_merge($files, $files2 );
33466 else
33467 $files[] = $folder . '/' . $file . '/';
33468 } else {
33469 $files[] = $folder . '/' . $file;
33470 }
33471 }
33472 }
33473 @closedir( $dir );
33474 return $files;
33475 }
33476
33477 /**
33478 * {@internal Missing Short Description}}
33479 *
33480 * @since unknown
33481 *
33482 * @return unknown
33483 */
33484 function get_temp_dir() {
33485 if ( defined('WP_TEMP_DIR') )
33486 return trailingslashit(WP_TEMP_DIR);
33487
33488 $temp = WP_CONTENT_DIR . '/';
33489 if ( is_dir($temp) && is_writable($temp) )
33490 return $temp;
33491
33492 if ( function_exists('sys_get_temp_dir') )
33493 return trailingslashit(sys_get_temp_dir());
33494
33495 return '/tmp/';
33496 }
33497
33498 /**
33499 * {@internal Missing Short Description}}
33500 *
33501 * @since unknown
33502 *
33503 * @param unknown_type $filename
33504 * @param unknown_type $dir
33505 * @return unknown
33506 */
33507 function wp_tempnam($filename = '', $dir = ''){
33508 if ( empty($dir) )
33509 $dir = get_temp_dir();
33510 $filename = basename($filename);
33511 if ( empty($filename) )
33512 $filename = time();
33513
33514 $filename = $dir . wp_unique_filename($dir, $filename);
33515 touch($filename);
33516 return $filename;
33517 }
33518
33519 /**
33520 * {@internal Missing Short Description}}
33521 *
33522 * @since unknown
33523 *
33524 * @param unknown_type $file
33525 * @param unknown_type $allowed_files
33526 * @return unknown
33527 */
33528 function validate_file_to_edit( $file, $allowed_files = '' ) {
33529 $file = stripslashes( $file );
33530
33531 $code = validate_file( $file, $allowed_files );
33532
33533 if (!$code )
33534 return $file;
33535
33536 switch ( $code ) {
33537 case 1 :
33538 wp_die( __('Sorry, can’t edit files with ".." in the name. If you are trying to edit a file in your WordPress home directory, you can just type the name of the file in.' ));
33539
33540 case 2 :
33541 wp_die( __('Sorry, can’t call files with their real path.' ));
33542
33543 case 3 :
33544 wp_die( __('Sorry, that file cannot be edited.' ));
33545 }
33546 }
33547
33548 /**
33549 * {@internal Missing Short Description}}
33550 *
33551 * @since unknown
33552 *
33553 * @param array $file Reference to a single element of $_FILES. Call the function once for each uploaded file.
33554 * @param array $overrides Optional. An associative array of names=>values to override default variables with extract( $overrides, EXTR_OVERWRITE ).
33555 * @return array On success, returns an associative array of file attributes. On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
33556 */
33557 function wp_handle_upload( &$file, $overrides = false, $time = null ) {
33558 // The default error handler.
33559 if (! function_exists( 'wp_handle_upload_error' ) ) {
33560 function wp_handle_upload_error( &$file, $message ) {
33561 return array( 'error'=>$message );
33562 }
33563 }
33564
33565 // You may define your own function and pass the name in $overrides['upload_error_handler']
33566 $upload_error_handler = 'wp_handle_upload_error';
33567
33568 // You may define your own function and pass the name in $overrides['unique_filename_callback']
33569 $unique_filename_callback = null;
33570
33571 // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
33572 $action = 'wp_handle_upload';
33573
33574 // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
33575 $upload_error_strings = array( false,
33576 __( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
33577 __( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
33578 __( "The uploaded file was only partially uploaded." ),
33579 __( "No file was uploaded." ),
33580 '',
33581 __( "Missing a temporary folder." ),
33582 __( "Failed to write file to disk." ));
33583
33584 // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
33585 $test_form = true;
33586 $test_size = true;
33587
33588 // If you override this, you must provide $ext and $type!!!!
33589 $test_type = true;
33590 $mimes = false;
33591
33592 // Install user overrides. Did we mention that this voids your warranty?
33593 if ( is_array( $overrides ) )
33594 extract( $overrides, EXTR_OVERWRITE );
33595
33596 // A correct form post will pass this test.
33597 if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
33598 return $upload_error_handler( $file, __( 'Invalid form submission.' ));
33599
33600 // A successful upload will pass this test. It makes no sense to override this one.
33601 if ( $file['error'] > 0 )
33602 return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
33603
33604 // A non-empty file will pass this test.
33605 if ( $test_size && !($file['size'] > 0 ) )
33606 return $upload_error_handler( $file, __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini.' ));
33607
33608 // A properly uploaded file will pass this test. There should be no reason to override this one.
33609 if (! @ is_uploaded_file( $file['tmp_name'] ) )
33610 return $upload_error_handler( $file, __( 'Specified file failed upload test.' ));
33611
33612 // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
33613 if ( $test_type ) {
33614 $wp_filetype = wp_check_filetype( $file['name'], $mimes );
33615
33616 extract( $wp_filetype );
33617
33618 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
33619 return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
33620
33621 if ( !$ext )
33622 $ext = ltrim(strrchr($file['name'], '.'), '.');
33623
33624 if ( !$type )
33625 $type = $file['type'];
33626 }
33627
33628 // A writable uploads dir will pass this test. Again, there's no point overriding this one.
33629 if ( ! ( ( $uploads = wp_upload_dir($time) ) && false === $uploads['error'] ) )
33630 return $upload_error_handler( $file, $uploads['error'] );
33631
33632 $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
33633
33634 // Move the file to the uploads dir
33635 $new_file = $uploads['path'] . "/$filename";
33636 if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) {
33637 return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
33638 }
33639
33640 // Set correct file permissions
33641 $stat = stat( dirname( $new_file ));
33642 $perms = $stat['mode'] & 0000666;
33643 @ chmod( $new_file, $perms );
33644
33645 // Compute the URL
33646 $url = $uploads['url'] . "/$filename";
33647
33648 $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
33649
33650 return $return;
33651 }
33652
33653 /**
33654 * {@internal Missing Short Description}}
33655 *
33656 * Pass this function an array similar to that of a $_FILES POST array.
33657 *
33658 * @since unknown
33659 *
33660 * @param unknown_type $file
33661 * @param unknown_type $overrides
33662 * @return unknown
33663 */
33664 function wp_handle_sideload( &$file, $overrides = false ) {
33665 // The default error handler.
33666 if (! function_exists( 'wp_handle_upload_error' ) ) {
33667 function wp_handle_upload_error( &$file, $message ) {
33668 return array( 'error'=>$message );
33669 }
33670 }
33671
33672 // You may define your own function and pass the name in $overrides['upload_error_handler']
33673 $upload_error_handler = 'wp_handle_upload_error';
33674
33675 // You may define your own function and pass the name in $overrides['unique_filename_callback']
33676 $unique_filename_callback = null;
33677
33678 // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
33679 $action = 'wp_handle_sideload';
33680
33681 // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
33682 $upload_error_strings = array( false,
33683 __( "The file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
33684 __( "The file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
33685 __( "The file was only partially uploaded." ),
33686 __( "No file was sent." ),
33687 __( "Missing a temporary folder." ),
33688 __( "Failed to write file to disk." ));
33689
33690 // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
33691 $test_form = true;
33692 $test_size = true;
33693
33694 // If you override this, you must provide $ext and $type!!!!
33695 $test_type = true;
33696 $mimes = false;
33697
33698 // Install user overrides. Did we mention that this voids your warranty?
33699 if ( is_array( $overrides ) )
33700 extract( $overrides, EXTR_OVERWRITE );
33701
33702 // A correct form post will pass this test.
33703 if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
33704 return $upload_error_handler( $file, __( 'Invalid form submission.' ));
33705
33706 // A successful upload will pass this test. It makes no sense to override this one.
33707 if ( $file['error'] > 0 )
33708 return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
33709
33710 // A non-empty file will pass this test.
33711 if ( $test_size && !(filesize($file['tmp_name']) > 0 ) )
33712 return $upload_error_handler( $file, __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini.' ));
33713
33714 // A properly uploaded file will pass this test. There should be no reason to override this one.
33715 if (! @ is_file( $file['tmp_name'] ) )
33716 return $upload_error_handler( $file, __( 'Specified file does not exist.' ));
33717
33718 // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
33719 if ( $test_type ) {
33720 $wp_filetype = wp_check_filetype( $file['name'], $mimes );
33721
33722 extract( $wp_filetype );
33723
33724 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
33725 return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
33726
33727 if ( !$ext )
33728 $ext = ltrim(strrchr($file['name'], '.'), '.');
33729
33730 if ( !$type )
33731 $type = $file['type'];
33732 }
33733
33734 // A writable uploads dir will pass this test. Again, there's no point overriding this one.
33735 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
33736 return $upload_error_handler( $file, $uploads['error'] );
33737
33738 $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
33739
33740 // Strip the query strings.
33741 $filename = str_replace('?','-', $filename);
33742 $filename = str_replace('&','-', $filename);
33743
33744 // Move the file to the uploads dir
33745 $new_file = $uploads['path'] . "/$filename";
33746 if ( false === @ rename( $file['tmp_name'], $new_file ) ) {
33747 return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
33748 }
33749
33750 // Set correct file permissions
33751 $stat = stat( dirname( $new_file ));
33752 $perms = $stat['mode'] & 0000666;
33753 @ chmod( $new_file, $perms );
33754
33755 // Compute the URL
33756 $url = $uploads['url'] . "/$filename";
33757
33758 $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
33759
33760 return $return;
33761 }
33762
33763 /**
33764 * Downloads a url to a local file using the Snoopy HTTP Class.
33765 *
33766 * @since unknown
33767 * @todo Transition over to using the new HTTP Request API (jacob).
33768 *
33769 * @param string $url the URL of the file to download
33770 * @return mixed WP_Error on failure, string Filename on success.
33771 */
33772 function download_url( $url ) {
33773 //WARNING: The file is not automatically deleted, The script must unlink() the file.
33774 if ( ! $url )
33775 return new WP_Error('http_no_url', __('Invalid URL Provided'));
33776
33777 $tmpfname = wp_tempnam($url);
33778 if ( ! $tmpfname )
33779 return new WP_Error('http_no_file', __('Could not create Temporary file'));
33780
33781 $handle = @fopen($tmpfname, 'wb');
33782 if ( ! $handle )
33783 return new WP_Error('http_no_file', __('Could not create Temporary file'));
33784
33785 $response = wp_remote_get($url, array('timeout' => 30));
33786
33787 if ( is_wp_error($response) ) {
33788 fclose($handle);
33789 unlink($tmpfname);
33790 return $response;
33791 }
33792
33793 if ( $response['response']['code'] != '200' ){
33794 fclose($handle);
33795 unlink($tmpfname);
33796 return new WP_Error('http_404', trim($response['response']['message']));
33797 }
33798
33799 fwrite($handle, $response['body']);
33800 fclose($handle);
33801
33802 return $tmpfname;
33803 }
33804
33805 /**
33806 * {@internal Missing Short Description}}
33807 *
33808 * @since unknown
33809 *
33810 * @param unknown_type $file
33811 * @param unknown_type $to
33812 * @return unknown
33813 */
33814 function unzip_file($file, $to) {
33815 global $wp_filesystem;
33816
33817 if ( ! $wp_filesystem || !is_object($wp_filesystem) )
33818 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
33819
33820 // Unzip uses a lot of memory
33821 @ini_set('memory_limit', '256M');
33822
33823 $fs =& $wp_filesystem;
33824
33825 require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
33826
33827 $archive = new PclZip($file);
33828
33829 // Is the archive valid?
33830 if ( false == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING)) )
33831 return new WP_Error('incompatible_archive', __('Incompatible archive'), $archive->errorInfo(true));
33832
33833 if ( 0 == count($archive_files) )
33834 return new WP_Error('empty_archive', __('Empty archive'));
33835
33836 $path = explode('/', untrailingslashit($to));
33837 for ( $i = count($path); $i > 0; $i-- ) { //>0 = first element is empty allways for paths starting with '/'
33838 $tmppath = implode('/', array_slice($path, 0, $i) );
33839 if ( $fs->is_dir($tmppath) ) { //Found the highest folder that exists, Create from here(ie +1)
33840 for ( $i = $i + 1; $i <= count($path); $i++ ) {
33841 $tmppath = implode('/', array_slice($path, 0, $i) );
33842 if ( ! $fs->mkdir($tmppath, FS_CHMOD_DIR) )
33843 return new WP_Error('mkdir_failed', __('Could not create directory'), $tmppath);
33844 }
33845 break; //Exit main for loop
33846 }
33847 }
33848
33849 $to = trailingslashit($to);
33850 foreach ($archive_files as $file) {
33851 $path = $file['folder'] ? $file['filename'] : dirname($file['filename']);
33852 $path = explode('/', $path);
33853 for ( $i = count($path); $i >= 0; $i-- ) { //>=0 as the first element contains data
33854 if ( empty($path[$i]) )
33855 continue;
33856 $tmppath = $to . implode('/', array_slice($path, 0, $i) );
33857 if ( $fs->is_dir($tmppath) ) {//Found the highest folder that exists, Create from here
33858 for ( $i = $i + 1; $i <= count($path); $i++ ) { //< count() no file component please.
33859 $tmppath = $to . implode('/', array_slice($path, 0, $i) );
33860 if ( ! $fs->is_dir($tmppath) && ! $fs->mkdir($tmppath, FS_CHMOD_DIR) )
33861 return new WP_Error('mkdir_failed', __('Could not create directory'), $tmppath);
33862 }
33863 break; //Exit main for loop
33864 }
33865 }
33866
33867 // We've made sure the folders are there, so let's extract the file now:
33868 if ( ! $file['folder'] ) {
33869 if ( !$fs->put_contents( $to . $file['filename'], $file['content']) )
33870 return new WP_Error('copy_failed', __('Could not copy file'), $to . $file['filename']);
33871 $fs->chmod($to . $file['filename'], FS_CHMOD_FILE);
33872 }
33873 }
33874 return true;
33875 }
33876
33877 /**
33878 * {@internal Missing Short Description}}
33879 *
33880 * @since unknown
33881 *
33882 * @param unknown_type $from
33883 * @param unknown_type $to
33884 * @return unknown
33885 */
33886 function copy_dir($from, $to) {
33887 global $wp_filesystem;
33888
33889 $dirlist = $wp_filesystem->dirlist($from);
33890
33891 $from = trailingslashit($from);
33892 $to = trailingslashit($to);
33893
33894 foreach ( (array) $dirlist as $filename => $fileinfo ) {
33895 if ( 'f' == $fileinfo['type'] ) {
33896 if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) ) {
33897 // If copy failed, chmod file to 0644 and try again.
33898 $wp_filesystem->chmod($to . $filename, 0644);
33899 if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) )
33900 return new WP_Error('copy_failed', __('Could not copy file'), $to . $filename);
33901 }
33902 $wp_filesystem->chmod($to . $filename, FS_CHMOD_FILE);
33903 } elseif ( 'd' == $fileinfo['type'] ) {
33904 if ( !$wp_filesystem->is_dir($to . $filename) ) {
33905 if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
33906 return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $filename);
33907 }
33908 $result = copy_dir($from . $filename, $to . $filename);
33909 if ( is_wp_error($result) )
33910 return $result;
33911 }
33912 }
33913 }
33914
33915 /**
33916 * {@internal Missing Short Description}}
33917 *
33918 * @since unknown
33919 *
33920 * @param unknown_type $args
33921 * @return unknown
33922 */
33923 function WP_Filesystem( $args = false ) {
33924 global $wp_filesystem;
33925
33926 require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
33927
33928 $method = get_filesystem_method($args);
33929
33930 if ( ! $method )
33931 return false;
33932
33933 $abstraction_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method);
33934 if( ! file_exists($abstraction_file) )
33935 return;
33936
33937 require_once($abstraction_file);
33938 $method = "WP_Filesystem_$method";
33939
33940 $wp_filesystem = new $method($args);
33941
33942 if ( $wp_filesystem->errors->get_error_code() )
33943 return false;
33944
33945 if ( !$wp_filesystem->connect() )
33946 return false; //There was an erorr connecting to the server.
33947
33948 // Set the permission constants if not already set.
33949 if ( ! defined('FS_CHMOD_DIR') )
33950 define('FS_CHMOD_DIR', 0755 );
33951 if ( ! defined('FS_CHMOD_FILE') )
33952 define('FS_CHMOD_FILE', 0644 );
33953
33954 return true;
33955 }
33956
33957 /**
33958 * {@internal Missing Short Description}}
33959 *
33960 * @since unknown
33961 *
33962 * @param unknown_type $args
33963 * @return unknown
33964 */
33965 function get_filesystem_method($args = array()) {
33966 $method = false;
33967 if( function_exists('getmyuid') && function_exists('fileowner') ){
33968 $temp_file = wp_tempnam();
33969 if ( getmyuid() == fileowner($temp_file) )
33970 $method = 'direct';
33971 unlink($temp_file);
33972 }
33973
33974 if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') ) $method = 'ssh2';
33975 if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext';
33976 if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread
33977 return apply_filters('filesystem_method', $method);
33978 }
33979
33980 /**
33981 * {@internal Missing Short Description}}
33982 *
33983 * @since unknown
33984 *
33985 * @param unknown_type $form_post
33986 * @param unknown_type $type
33987 * @param unknown_type $error
33988 * @return unknown
33989 */
33990 function request_filesystem_credentials($form_post, $type = '', $error = false) {
33991 $req_cred = apply_filters('request_filesystem_credentials', '', $form_post, $type, $error);
33992 if ( '' !== $req_cred )
33993 return $req_cred;
33994
33995 if ( empty($type) )
33996 $type = get_filesystem_method();
33997
33998 if ( 'direct' == $type )
33999 return true;
34000
34001 $credentials = get_option('ftp_credentials', array());
34002 // If defined, set it to that, Else, If POST'd, set it to that, If not, Set it to whatever it previously was(saved details in option)
34003 $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? $_POST['hostname'] : $credentials['hostname']);
34004 $credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? $_POST['username'] : $credentials['username']);
34005 $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($_POST['password']) ? $_POST['password'] : $credentials['password']);
34006
34007 // Check to see if we are setting the public/private keys for ssh
34008 $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($_POST['public_key']) ? $_POST['public_key'] : $credentials['public_key']);
34009 $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($_POST['private_key']) ? $_POST['private_key'] : $credentials['private_key']);
34010
34011 if ( strpos($credentials['hostname'], ':') )
34012 list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2);
34013
34014 if ( defined('FTP_SSH') || (isset($_POST['connection_type']) && 'ssh' == $_POST['connection_type']) )
34015 $credentials['connection_type'] = 'ssh';
34016 else if ( defined('FTP_SSL') || (isset($_POST['connection_type']) && 'ftps' == $_POST['connection_type']) )
34017 $credentials['connection_type'] = 'ftps';
34018 else if ( !isset($credentials['connection_type']) || (isset($_POST['connection_type']) && 'ftp' == $_POST['connection_type']) )
34019 $credentials['connection_type'] = 'ftp';
34020
34021 if ( ! $error && !empty($credentials['password']) && !empty($credentials['username']) && !empty($credentials['hostname']) ) {
34022 $stored_credentials = $credentials;
34023 unset($stored_credentials['password'], $stored_credentials['private_key'], $stored_credentials['public_key']);
34024 update_option('ftp_credentials', $stored_credentials);
34025 return $credentials;
34026 }
34027 $hostname = '';
34028 $username = '';
34029 $password = '';
34030 $connection_type = '';
34031 if ( !empty($credentials) )
34032 extract($credentials, EXTR_OVERWRITE);
34033 if ( $error ) {
34034 $error_string = __('<strong>Error:</strong> There was an error connecting to the server, Please verify the settings are correct.');
34035 if ( is_wp_error($error) )
34036 $error_string = $error->get_error_message();
34037 echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
34038 }
34039 ?>
34040 <script type="text/javascript">
34041 <!--
34042 jQuery(function($){
34043 jQuery("#ssh").click(function () {
34044 jQuery("#ssh_keys").show();
34045 });
34046 jQuery("#ftp, #ftps").click(function () {
34047 jQuery("#ssh_keys").hide();
34048 });
34049 });
34050 -->
34051 </script>
34052 <form action="<?php echo $form_post ?>" method="post">
34053 <div class="wrap">
34054 <h2><?php _e('Connection Information') ?></h2>
34055 <p><?php _e('To perform the requested action, connection information is required.') ?></p>
34056
34057 <table class="form-table">
34058 <tr valign="top">
34059 <th scope="row"><label for="hostname"><?php _e('Hostname') ?></label></th>
34060 <td><input name="hostname" type="text" id="hostname" value="<?php echo attribute_escape($hostname); if ( !empty($port) ) echo ":$port"; ?>"<?php if( defined('FTP_HOST') ) echo ' disabled="disabled"' ?> size="40" /></td>
34061 </tr>
34062
34063 <tr valign="top">
34064 <th scope="row"><label for="username"><?php _e('Username') ?></label></th>
34065 <td><input name="username" type="text" id="username" value="<?php echo attribute_escape($username) ?>"<?php if( defined('FTP_USER') ) echo ' disabled="disabled"' ?> size="40" /></td>
34066 </tr>
34067
34068 <tr valign="top">
34069 <th scope="row"><label for="password"><?php _e('Password') ?></label></th>
34070 <td><input name="password" type="password" id="password" value=""<?php if( defined('FTP_PASS') ) echo ' disabled="disabled"' ?> size="40" /><?php if( defined('FTP_PASS') && !empty($password) ) echo '<em>'.__('(Password not shown)').'</em>'; ?></td>
34071 </tr>
34072
34073 <tr id="ssh_keys" valign="top" style="<?php if ( 'ssh' != $connection_type ) echo 'display:none' ?>">
34074 <th scope="row"><?php _e('Authentication Keys') ?>
34075 <div class="key-labels textright">
34076 <label for="public_key"><?php _e('Public Key:') ?></label ><br />
34077 <label for="private_key"><?php _e('Private Key:') ?></label>
34078 </div></th>
34079 <td><br /><input name="public_key" type="text" id="public_key" value=""<?php if( defined('FTP_PUBKEY') ) echo ' disabled="disabled"' ?> size="40" /><br /><input name="private_key" type="text" id="private_key" value=""<?php if( defined('FTP_PRIKEY') ) echo ' disabled="disabled"' ?> size="40" />
34080 <div><?php _e('Enter the location on the server where the keys are located. If a passphrase is needed, enter that in the password field above.') ?></div></td>
34081 </tr>
34082
34083 <tr valign="top">
34084 <th scope="row"><?php _e('Connection Type') ?></th>
34085 <td>
34086 <fieldset><legend class="hidden"><?php _e('Connection Type') ?></legend>
34087 <label><input id="ftp" name="connection_type" type="radio" value="ftp" <?php checked('ftp', $connection_type); if ( defined('FTP_SSL') || defined('FTP_SSH') ) echo ' disabled="disabled"'; ?>/> <?php _e('FTP') ?></label><br />
34088 <label><input id="ftps" name="connection_type" type="radio" value="ftps" <?php checked('ftps', $connection_type); if ( defined('FTP_SSH') || defined('FTP_SSH') ) echo ' disabled="disabled"'; ?>/> <?php _e('FTPS (SSL)') ?></label><br />
34089 <?php if ( extension_loaded('ssh2') ) { ?><label><input id="ssh" name="connection_type" type="radio" value="ssh" <?php checked('ssh', $connection_type); if ( defined('FTP_SSL') || defined('FTP_SSH') ) echo ' disabled="disabled"'; ?>/> <?php _e('SSH') ?></label><?php } ?>
34090 </fieldset>
34091 </td>
34092 </tr>
34093 </table>
34094
34095 <?php if ( isset( $_POST['version'] ) ) : ?>
34096 <input type="hidden" name="version" value="<?php echo attribute_escape($_POST['version']) ?>" />
34097 <?php endif; ?>
34098 <?php if ( isset( $_POST['locale'] ) ) : ?>
34099 <input type="hidden" name="locale" value="<?php echo attribute_escape($_POST['locale']) ?>" />
34100 <?php endif; ?>
34101 <p class="submit">
34102 <input id="upgrade" name="upgrade" type="submit" class="button" value="<?php _e('Proceed'); ?>" />
34103 </p>
34104 </div>
34105 </form>
34106 <?php
34107 return false;
34108 }
34109
34110 ?>