~katiekitty/+junk/wordpress-byet

« back to all changes in this revision

Viewing changes to wp-admin/includes/file.php

  • Committer: kserver
  • Date: 2010-05-15 01:16:36 UTC
  • Revision ID: kserver@kserver-desktop-20100515011636-mnr1j7t637suptdq
Wordpress 2.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
}
100
100
 
101
101
/**
102
 
 * {@internal Missing Short Description}}
103
 
 *
104
 
 * @since unknown
105
 
 *
106
 
 * @param string $folder Optional. Full path to folder
107
 
 * @param int $levels Optional. Levels of folders to follow, Default: 100 (PHP Loop limit).
108
 
 * @return bool|array
 
102
 * Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep.
 
103
 * The depth of the recursiveness can be controlled by the $levels param.
 
104
 *
 
105
 * @since 2.6.0
 
106
 *
 
107
 * @param string $folder Full path to folder
 
108
 * @param int $levels (optional) Levels of folders to follow, Default: 100 (PHP Loop limit).
 
109
 * @return bool|array False on failure, Else array of files
109
110
 */
110
111
function list_files( $folder = '', $levels = 100 ) {
111
112
        if( empty($folder) )
135
136
}
136
137
 
137
138
/**
138
 
 * {@internal Missing Short Description}}
139
 
 *
140
 
 * @since unknown
141
 
 *
142
 
 * @return unknown
 
139
 * Determines a writable directory for temporary files.
 
140
 * Function's preference is to WP_CONTENT_DIR followed by the return value of <code>sys_get_temp_dir()</code>, before finally defaulting to /tmp/
 
141
 *
 
142
 * In the event that this function does not find a writable location, It may be overridden by the <code>WP_TEMP_DIR</code> constant in your <code>wp-config.php</code> file.
 
143
 *
 
144
 * @since 2.5.0
 
145
 *
 
146
 * @return string Writable temporary directory
143
147
 */
144
148
function get_temp_dir() {
145
149
        if ( defined('WP_TEMP_DIR') )
156
160
}
157
161
 
158
162
/**
159
 
 * {@internal Missing Short Description}}
160
 
 *
161
 
 * @since unknown
162
 
 *
163
 
 * @param unknown_type $filename
164
 
 * @param unknown_type $dir
165
 
 * @return unknown
 
163
 * Returns a filename of a Temporary unique file.
 
164
 * Please note that the calling function must unlink() this itself.
 
165
 *
 
166
 * The filename is based off the passed parameter or defaults to the current unix timestamp,
 
167
 * while the directory can either be passed as well, or by leaving  it blank, default to a writable temporary directory.
 
168
 *
 
169
 * @since 2.6.0
 
170
 *
 
171
 * @param string $filename (optional) Filename to base the Unique file off
 
172
 * @param string $dir (optional) Directory to store the file in
 
173
 * @return string a writable filename
166
174
 */
167
175
function wp_tempnam($filename = '', $dir = ''){
168
176
        if ( empty($dir) )
171
179
        if ( empty($filename) )
172
180
                $filename = time();
173
181
 
 
182
        $filename = preg_replace('|\..*$|', '.tmp', $filename);
174
183
        $filename = $dir . wp_unique_filename($dir, $filename);
175
184
        touch($filename);
176
185
        return $filename;
186
195
 * @return unknown
187
196
 */
188
197
function validate_file_to_edit( $file, $allowed_files = '' ) {
189
 
        $file = stripslashes( $file );
190
 
 
191
198
        $code = validate_file( $file, $allowed_files );
192
199
 
193
200
        if (!$code )
197
204
                case 1 :
198
205
                        wp_die( __('Sorry, can&#8217;t edit files with &#8220;..&#8221; 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.' ));
199
206
 
200
 
                case 2 :
201
 
                        wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
 
207
                //case 2 :
 
208
                //      wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
202
209
 
203
210
                case 3 :
204
211
                        wp_die( __('Sorry, that file cannot be edited.' ));
222
229
                }
223
230
        }
224
231
 
 
232
        $file = apply_filters( 'wp_handle_upload_prefilter', $file );
 
233
 
225
234
        // You may define your own function and pass the name in $overrides['upload_error_handler']
226
235
        $upload_error_handler = 'wp_handle_upload_error';
227
236
 
 
237
        // You may have had one or more 'wp_handle_upload_prefilter' functions error out the file.  Handle that gracefully.
 
238
        if ( isset( $file['error'] ) && !is_numeric( $file['error'] ) && $file['error'] )
 
239
                return $upload_error_handler( $file, $file['error'] );
 
240
 
228
241
        // You may define your own function and pass the name in $overrides['unique_filename_callback']
229
242
        $unique_filename_callback = null;
230
243
 
264
277
 
265
278
        // A non-empty file will pass this test.
266
279
        if ( $test_size && !($file['size'] > 0 ) )
267
 
                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.' ));
 
280
                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 or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' ));
268
281
 
269
282
        // A properly uploaded file will pass this test. There should be no reason to override this one.
270
283
        if (! @ is_uploaded_file( $file['tmp_name'] ) )
424
437
}
425
438
 
426
439
/**
427
 
 * Downloads a url to a local file using the Snoopy HTTP Class.
 
440
 * Downloads a url to a local temporary file using the WordPress HTTP Class.
 
441
 * Please note, That the calling function must unlink() the  file.
428
442
 *
429
 
 * @since unknown
430
 
 * @todo Transition over to using the new HTTP Request API (jacob).
 
443
 * @since 2.5.0
431
444
 *
432
445
 * @param string $url the URL of the file to download
433
446
 * @return mixed WP_Error on failure, string Filename on success.
445
458
        if ( ! $handle )
446
459
                return new WP_Error('http_no_file', __('Could not create Temporary file'));
447
460
 
448
 
        $response = wp_remote_get($url, array('timeout' => 60));
 
461
        $response = wp_remote_get($url, array('timeout' => 300));
449
462
 
450
463
        if ( is_wp_error($response) ) {
451
464
                fclose($handle);
466
479
}
467
480
 
468
481
/**
469
 
 * {@internal Missing Short Description}}
470
 
 *
471
 
 * @since unknown
472
 
 *
473
 
 * @param unknown_type $file
474
 
 * @param unknown_type $to
475
 
 * @return unknown
 
482
 * Unzip's a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
 
483
 * Assumes that WP_Filesystem() has already been called and set up.
 
484
 *
 
485
 * Attempts to increase the PHP Memory limit to 256M before uncompressing,
 
486
 * However, The most memory required shouldn't be much larger than the Archive itself.
 
487
 *
 
488
 * @since 2.5.0
 
489
 *
 
490
 * @param string $file Full path and filename of zip archive
 
491
 * @param string $to Full path on the filesystem to extract archive to
 
492
 * @return mixed WP_Error on failure, True on success
476
493
 */
477
494
function unzip_file($file, $to) {
478
495
        global $wp_filesystem;
480
497
        if ( ! $wp_filesystem || !is_object($wp_filesystem) )
481
498
                return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
482
499
 
483
 
        // Unzip uses a lot of memory
 
500
        // Unzip uses a lot of memory, but not this much hopefully
484
501
        @ini_set('memory_limit', '256M');
485
502
 
486
503
        $fs =& $wp_filesystem;
538
555
}
539
556
 
540
557
/**
541
 
 * {@internal Missing Short Description}}
542
 
 *
543
 
 * @since unknown
544
 
 *
545
 
 * @param unknown_type $from
546
 
 * @param unknown_type $to
547
 
 * @return unknown
 
558
 * Copies a directory from one location to another via the WordPress Filesystem Abstraction.
 
559
 * Assumes that WP_Filesystem() has already been called and setup.
 
560
 *
 
561
 * @since 2.5.0
 
562
 *
 
563
 * @param string $from source directory
 
564
 * @param string $to destination directory
 
565
 * @return mixed WP_Error on failure, True on success.
548
566
 */
549
567
function copy_dir($from, $to) {
550
568
        global $wp_filesystem;
573
591
                                return $result;
574
592
                }
575
593
        }
 
594
        return true;
576
595
}
577
596
 
578
597
/**
579
 
 * {@internal Missing Short Description}}
580
 
 *
581
 
 * @since unknown
582
 
 *
583
 
 * @param unknown_type $args
584
 
 * @return unknown
 
598
 * Initialises and connects the WordPress Filesystem Abstraction classes.
 
599
 * This function will include the chosen transport and attempt connecting.
 
600
 *
 
601
 * Plugins may add extra transports, And force WordPress to use them by returning the filename via the 'filesystem_method_file' filter.
 
602
 *
 
603
 * @since 2.5.0
 
604
 *
 
605
 * @param array $args (optional) Connection args, These are passed directly to the WP_Filesystem_*() classes.
 
606
 * @param string $context (optional) Context for get_filesystem_method(), See function declaration for more information.
 
607
 * @return boolean false on failure, true on success
585
608
 */
586
609
function WP_Filesystem( $args = false, $context = false ) {
587
610
        global $wp_filesystem;
597
620
                $abstraction_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method);
598
621
                if( ! file_exists($abstraction_file) )
599
622
                        return;
600
 
        
 
623
 
601
624
                require_once($abstraction_file);
602
625
        }
603
626
        $method = "WP_Filesystem_$method";
604
627
 
605
628
        $wp_filesystem = new $method($args);
606
629
 
 
630
        //Define the timeouts for the connections. Only available after the construct is called to allow for per-transport overriding of the default.
 
631
        if ( ! defined('FS_CONNECT_TIMEOUT') )
 
632
                define('FS_CONNECT_TIMEOUT', 30);
 
633
        if ( ! defined('FS_TIMEOUT') )
 
634
                define('FS_TIMEOUT', 30);
 
635
 
607
636
        if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
608
637
                return false;
609
638
 
620
649
}
621
650
 
622
651
/**
623
 
 * {@internal Missing Short Description}}
624
 
 *
625
 
 * @since unknown
626
 
 *
627
 
 * @param unknown_type $args
 
652
 * Determines which Filesystem Method to use.
 
653
 * The priority of the Transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets (Via Sockets class, or fsoxkopen())
 
654
 *
 
655
 * Note that the return value of this function can be overridden in 2 ways
 
656
 *  - By defining FS_METHOD in your <code>wp-config.php</code> file
 
657
 *  - By using the filesystem_method filter
 
658
 * Valid values for these are: 'direct', 'ssh', 'ftpext' or 'ftpsockets'
 
659
 * Plugins may also define a custom transport handler, See the WP_Filesystem function for more information.
 
660
 *
 
661
 * @since 2.5.0
 
662
 *
 
663
 * @param array $args Connection details.
628
664
 * @param string $context Full path to the directory that is tested for being writable.
629
 
 * @return unknown
 
665
 * @return string The transport to use, see description for valid return values.
630
666
 */
631
667
function get_filesystem_method($args = array(), $context = false) {
632
668
        $method = defined('FS_METHOD') ? FS_METHOD : false; //Please ensure that this is either 'direct', 'ssh', 'ftpext' or 'ftpsockets'
635
671
                if ( !$context )
636
672
                        $context = WP_CONTENT_DIR;
637
673
                $context = trailingslashit($context);
638
 
                $temp_file_name = $context . '.write-test-' . time();
 
674
                $temp_file_name = $context . 'temp-write-test-' . time();
639
675
                $temp_handle = @fopen($temp_file_name, 'w');
640
676
                if ( $temp_handle ) {
641
 
                        if ( getmyuid() == fileowner($temp_file_name) )
 
677
                        if ( getmyuid() == @fileowner($temp_file_name) )
642
678
                                $method = 'direct';
643
679
                        @fclose($temp_handle);
644
 
                        unlink($temp_file_name);
 
680
                        @unlink($temp_file_name);
645
681
                }
646
682
        }
647
683
 
652
688
}
653
689
 
654
690
/**
655
 
 * {@internal Missing Short Description}}
656
 
 *
657
 
 * @since unknown
658
 
 *
659
 
 * @param unknown_type $form_post
660
 
 * @param unknown_type $type
661
 
 * @param unknown_type $error
662
 
 * @return unknown
 
691
 * Displays a form to the user to request for their FTP/SSH details in order to  connect to the filesystem.
 
692
 * All chosen/entered details are saved, Excluding the Password.
 
693
 *
 
694
 * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) to specify an alternate FTP/SSH port.
 
695
 *
 
696
 * Plugins may override this form by returning true|false via the <code>request_filesystem_credentials</code> filter.
 
697
 *
 
698
 * @since 2.5.0
 
699
 *
 
700
 * @param string $form_post the URL to post the form to
 
701
 * @param string $type the chosen Filesystem method in use
 
702
 * @param boolean $error if the current request has failed to connect
 
703
 * @param string $context The directory which is needed access to, The write-test will be performed on  this directory by get_filesystem_method()
 
704
 * @return boolean False on failure. True on success.
663
705
 */
664
706
function request_filesystem_credentials($form_post, $type = '', $error = false, $context = false) {
665
707
        $req_cred = apply_filters('request_filesystem_credentials', '', $form_post, $type, $error, $context);
675
717
        $credentials = get_option('ftp_credentials', array( 'hostname' => '', 'username' => ''));
676
718
 
677
719
        // 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)
678
 
        $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? $_POST['hostname'] : $credentials['hostname']);
679
 
        $credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? $_POST['username'] : $credentials['username']);
680
 
        $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($_POST['password']) ? $_POST['password'] : '');
 
720
        $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? stripslashes($_POST['hostname']) : $credentials['hostname']);
 
721
        $credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? stripslashes($_POST['username']) : $credentials['username']);
 
722
        $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($_POST['password']) ? stripslashes($_POST['password']) : '');
681
723
 
682
724
        // Check to see if we are setting the public/private keys for ssh
683
 
        $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($_POST['public_key']) ? $_POST['public_key'] : '');
684
 
        $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($_POST['private_key']) ? $_POST['private_key'] : '');
 
725
        $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($_POST['public_key']) ? stripslashes($_POST['public_key']) : '');
 
726
        $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($_POST['private_key']) ? stripslashes($_POST['private_key']) : '');
685
727
 
686
728
        //sanitize the hostname, Some people might pass in odd-data:
687
729
        $credentials['hostname'] = preg_replace('|\w+://|', '', $credentials['hostname']); //Strip any schemes off
688
730
 
689
 
        if ( strpos($credentials['hostname'], ':') )
 
731
        if ( strpos($credentials['hostname'], ':') ) {
690
732
                list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2);
691
 
        else
 
733
                if ( ! is_numeric($credentials['port']) )
 
734
                        unset($credentials['port']);
 
735
        } else {
692
736
                unset($credentials['port']);
 
737
        }
693
738
 
694
 
        if ( defined('FTP_SSH') || (defined('FS_METHOD') && 'ssh' == FS_METHOD) )
 
739
        if ( (defined('FTP_SSH') && FTP_SSH) || (defined('FS_METHOD') && 'ssh' == FS_METHOD) )
695
740
                $credentials['connection_type'] = 'ssh';
696
 
        else if ( defined('FTP_SSL') && 'ftpext' == $type ) //Only the FTP Extension understands SSL
 
741
        else if ( (defined('FTP_SSL') && FTP_SSL) && 'ftpext' == $type ) //Only the FTP Extension understands SSL
697
742
                $credentials['connection_type'] = 'ftps';
698
743
        else if ( !empty($_POST['connection_type']) )
699
 
                $credentials['connection_type'] = $_POST['connection_type'];
 
744
                $credentials['connection_type'] = stripslashes($_POST['connection_type']);
700
745
        else if ( !isset($credentials['connection_type']) ) //All else fails (And its not defaulted to something else saved), Default to FTP
701
746
                $credentials['connection_type'] = 'ftp';
702
747
 
725
770
                        $error_string = $error->get_error_message();
726
771
                echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
727
772
        }
 
773
 
 
774
        $types = array();
 
775
        if ( extension_loaded('ftp') || extension_loaded('sockets') || function_exists('fsockopen') )
 
776
                $types[ 'ftp' ] = __('FTP');
 
777
        if ( extension_loaded('ftp') ) //Only this supports FTPS
 
778
                $types[ 'ftps' ] = __('FTPS (SSL)');
 
779
        if ( extension_loaded('ssh2') && function_exists('stream_get_contents') )
 
780
                $types[ 'ssh' ] = __('SSH2');
 
781
 
 
782
        $types = apply_filters('fs_ftp_connection_types', $types, $credentials, $type, $error, $context);
 
783
 
728
784
?>
729
785
<script type="text/javascript">
730
786
<!--
761
817
<td><input name="password" type="password" id="password" value="<?php if ( defined('FTP_PASS') ) echo '*****'; ?>"<?php if ( defined('FTP_PASS') ) echo ' disabled="disabled"' ?> size="40" /></td>
762
818
</tr>
763
819
 
764
 
<?php if ( extension_loaded('ssh2') && function_exists('stream_get_contents') ) : ?>
 
820
<?php if ( isset($types['ssh']) ) : ?>
765
821
<tr id="ssh_keys" valign="top" style="<?php if ( 'ssh' != $connection_type ) echo 'display:none' ?>">
766
822
<th scope="row"><?php _e('Authentication Keys') ?>
767
823
<div class="key-labels textright">
777
833
<th scope="row"><?php _e('Connection Type') ?></th>
778
834
<td>
779
835
<fieldset><legend class="screen-reader-text"><span><?php _e('Connection Type') ?></span></legend>
780
 
<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>
781
 
<?php if ( 'ftpext' == $type ) : ?>
782
 
<br /><label><input id="ftps" name="connection_type" type="radio" value="ftps" <?php checked('ftps', $connection_type); if ( defined('FTP_SSL') || defined('FTP_SSH') ) echo ' disabled="disabled"';  ?>/> <?php _e('FTPS (SSL)') ?></label>
783
 
<?php endif; ?>
784
 
<?php if ( extension_loaded('ssh2') && function_exists('stream_get_contents') ) : ?>
785
 
<br /><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>
786
 
<?php endif; ?>
 
836
<?php
 
837
 
 
838
        $disabled = (defined('FTP_SSL') && FTP_SSL) || (defined('FTP_SSH') && FTP_SSH) ? ' disabled="disabled"' : '';
 
839
 
 
840
        foreach ( $types as $name => $text ) : ?>
 
841
        <label for="<?php echo esc_attr($name) ?>">
 
842
                <input type="radio" name="connection_type" id="<?php echo esc_attr($name) ?>" value="<?php echo esc_attr($name) ?>" <?php checked($name, $connection_type); echo $disabled; ?>/>
 
843
                <?php echo $text ?>
 
844
        </label>
 
845
        <?php endforeach; ?>
787
846
</fieldset>
788
847
</td>
789
848
</tr>
790
849
</table>
791
850
 
792
851
<?php if ( isset( $_POST['version'] ) ) : ?>
793
 
<input type="hidden" name="version" value="<?php echo esc_attr($_POST['version']) ?>" />
 
852
<input type="hidden" name="version" value="<?php echo esc_attr(stripslashes($_POST['version'])) ?>" />
794
853
<?php endif; ?>
795
854
<?php if ( isset( $_POST['locale'] ) ) : ?>
796
 
<input type="hidden" name="locale" value="<?php echo esc_attr($_POST['locale']) ?>" />
 
855
<input type="hidden" name="locale" value="<?php echo esc_attr(stripslashes($_POST['locale'])) ?>" />
797
856
<?php endif; ?>
798
857
<p class="submit">
799
858
<input id="upgrade" name="upgrade" type="submit" class="button" value="<?php esc_attr_e('Proceed'); ?>" />