-
+ 3129ADE306CE07E1FF8F7CC0EA5755D709BC1FD927DD6C795F3DE0D2FAFDD5CECCD7462B075A74B289C93CE8FA43637007312D5B177B9755066F1979DA80C92Amp-wp/wp-admin/includes/class-wp-filesystem-ftpext.php(0 . 0)(1 . 382)
30766 <?php
30767 /**
30768 * WordPress FTP Filesystem.
30769 *
30770 * @package WordPress
30771 * @subpackage Filesystem
30772 */
30773
30774 /**
30775 * WordPress Filesystem Class for implementing FTP.
30776 *
30777 * @since 2.5
30778 * @package WordPress
30779 * @subpackage Filesystem
30780 * @uses WP_Filesystem_Base Extends class
30781 */
30782 class WP_Filesystem_FTPext extends WP_Filesystem_Base {
30783 var $link;
30784 var $timeout = 5;
30785 var $errors = array();
30786 var $options = array();
30787
30788 var $permission = null;
30789
30790 function WP_Filesystem_FTPext($opt='') {
30791 $this->method = 'ftpext';
30792 $this->errors = new WP_Error();
30793
30794 //Check if possible to use ftp functions.
30795 if ( ! extension_loaded('ftp') ) {
30796 $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
30797 return false;
30798 }
30799
30800 // Set defaults:
30801 if ( empty($opt['port']) )
30802 $this->options['port'] = 21;
30803 else
30804 $this->options['port'] = $opt['port'];
30805
30806 if ( empty($opt['hostname']) )
30807 $this->errors->add('empty_hostname', __('FTP hostname is required'));
30808 else
30809 $this->options['hostname'] = $opt['hostname'];
30810
30811 if ( isset($opt['base']) && ! empty($opt['base']) )
30812 $this->wp_base = $opt['base'];
30813
30814 // Check if the options provided are OK.
30815 if ( empty ($opt['username']) )
30816 $this->errors->add('empty_username', __('FTP username is required'));
30817 else
30818 $this->options['username'] = $opt['username'];
30819
30820 if ( empty ($opt['password']) )
30821 $this->errors->add('empty_password', __('FTP password is required'));
30822 else
30823 $this->options['password'] = $opt['password'];
30824
30825 $this->options['ssl'] = false;
30826 if ( isset($opt['ssl']) )
30827 $this->options['ssl'] = ( !empty($opt['ssl']) );
30828 elseif ( isset( $opt['connection_type']) )
30829 $this->options['ssl'] = ( 'ftps' == $opt['connection_type'] );
30830 }
30831
30832 function connect() {
30833 if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') )
30834 $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'],$this->timeout);
30835 else
30836 $this->link = @ftp_connect($this->options['hostname'], $this->options['port'],$this->timeout);
30837
30838 if ( ! $this->link ) {
30839 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
30840 return false;
30841 }
30842
30843 if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) {
30844 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
30845 return false;
30846 }
30847
30848 //Set the Connection to use Passive FTP
30849 @ftp_pasv( $this->link, true );
30850
30851 return true;
30852 }
30853
30854 function setDefaultPermissions($perm) {
30855 $this->permission = $perm;
30856 }
30857
30858 function get_contents($file, $type = '', $resumepos = 0 ){
30859 if( empty($type) )
30860 $type = FTP_BINARY;
30861
30862 $temp = tmpfile();
30863 if ( ! $temp )
30864 return false;
30865
30866 if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
30867 return false;
30868
30869 fseek($temp, 0); //Skip back to the start of the file being written to
30870 $contents = '';
30871
30872 while ( ! feof($temp) )
30873 $contents .= fread($temp, 8192);
30874
30875 fclose($temp);
30876 return $contents;
30877 }
30878 function get_contents_array($file) {
30879 return explode("\n", $this->get_contents($file));
30880 }
30881 function put_contents($file, $contents, $type = '' ) {
30882 if( empty($type) )
30883 $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
30884
30885 $temp = tmpfile();
30886 if ( ! $temp )
30887 return false;
30888
30889 fwrite($temp, $contents);
30890 fseek($temp, 0); //Skip back to the start of the file being written to
30891
30892 $ret = @ftp_fput($this->link, $file, $temp, $type);
30893
30894 fclose($temp);
30895 return $ret;
30896 }
30897 function cwd() {
30898 $cwd = @ftp_pwd($this->link);
30899 if( $cwd )
30900 $cwd = trailingslashit($cwd);
30901 return $cwd;
30902 }
30903 function chdir($dir) {
30904 return @ftp_chdir($dir);
30905 }
30906 function chgrp($file, $group, $recursive = false ) {
30907 return false;
30908 }
30909 function chmod($file, $mode = false, $recursive = false) {
30910 if( ! $mode )
30911 $mode = $this->permission;
30912 if( ! $mode )
30913 return false;
30914 if ( ! $this->exists($file) )
30915 return false;
30916 if ( ! $recursive || ! $this->is_dir($file) ) {
30917 if ( ! function_exists('ftp_chmod') )
30918 return @ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
30919 return @ftp_chmod($this->link, $mode, $file);
30920 }
30921 //Is a directory, and we want recursive
30922 $filelist = $this->dirlist($file);
30923 foreach($filelist as $filename){
30924 $this->chmod($file . '/' . $filename, $mode, $recursive);
30925 }
30926 return true;
30927 }
30928 function chown($file, $owner, $recursive = false ) {
30929 return false;
30930 }
30931 function owner($file) {
30932 $dir = $this->dirlist($file);
30933 return $dir[$file]['owner'];
30934 }
30935 function getchmod($file) {
30936 $dir = $this->dirlist($file);
30937 return $dir[$file]['permsn'];
30938 }
30939 function group($file) {
30940 $dir = $this->dirlist($file);
30941 return $dir[$file]['group'];
30942 }
30943 function copy($source, $destination, $overwrite = false ) {
30944 if( ! $overwrite && $this->exists($destination) )
30945 return false;
30946 $content = $this->get_contents($source);
30947 if( false === $content)
30948 return false;
30949 return $this->put_contents($destination, $content);
30950 }
30951 function move($source, $destination, $overwrite = false) {
30952 return ftp_rename($this->link, $source, $destination);
30953 }
30954
30955 function delete($file,$recursive=false) {
30956 if ( $this->is_file($file) )
30957 return @ftp_delete($this->link, $file);
30958 if ( !$recursive )
30959 return @ftp_rmdir($this->link, $file);
30960 $filelist = $this->dirlist($file);
30961 foreach ((array) $filelist as $filename => $fileinfo) {
30962 $this->delete($file . '/' . $filename, $recursive);
30963 }
30964 return @ftp_rmdir($this->link, $file);
30965 }
30966
30967 function exists($file) {
30968 $list = ftp_rawlist($this->link, $file, false);
30969 if( ! $list )
30970 return false;
30971 return count($list) == 1 ? true : false;
30972 }
30973 function is_file($file) {
30974 return $this->is_dir($file) ? false : true;
30975 }
30976 function is_dir($path) {
30977 $cwd = $this->cwd();
30978 $result = @ftp_chdir($this->link, $path);
30979 if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
30980 @ftp_chdir($this->link, $cwd);
30981 return true;
30982 }
30983 return false;
30984 }
30985 function is_readable($file) {
30986 //Get dir list, Check if the file is writable by the current user??
30987 return true;
30988 }
30989 function is_writable($file) {
30990 //Get dir list, Check if the file is writable by the current user??
30991 return true;
30992 }
30993 function atime($file) {
30994 return false;
30995 }
30996 function mtime($file) {
30997 return ftp_mdtm($this->link, $file);
30998 }
30999 function size($file) {
31000 return ftp_size($this->link, $file);
31001 }
31002 function touch($file, $time = 0, $atime = 0) {
31003 return false;
31004 }
31005 function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
31006 if( !@ftp_mkdir($this->link, $path) )
31007 return false;
31008 if( $chmod )
31009 $this->chmod($path, $chmod);
31010 if( $chown )
31011 $this->chown($path, $chown);
31012 if( $chgrp )
31013 $this->chgrp($path, $chgrp);
31014 return true;
31015 }
31016 function rmdir($path, $recursive = false) {
31017 if( ! $recursive )
31018 return @ftp_rmdir($this->link, $path);
31019
31020 //TODO: Recursive Directory delete, Have to delete files from the folder first.
31021 //$dir = $this->dirlist($path);
31022 //foreach($dir as $file)
31023
31024 }
31025
31026 function parselisting($line) {
31027 $is_windows = ($this->OS_remote == FTP_OS_Windows);
31028 if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/", $line, $lucifer)) {
31029 $b = array();
31030 if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
31031 $b['isdir'] = ($lucifer[7]=="<DIR>");
31032 if ( $b['isdir'] )
31033 $b['type'] = 'd';
31034 else
31035 $b['type'] = 'f';
31036 $b['size'] = $lucifer[7];
31037 $b['month'] = $lucifer[1];
31038 $b['day'] = $lucifer[2];
31039 $b['year'] = $lucifer[3];
31040 $b['hour'] = $lucifer[4];
31041 $b['minute'] = $lucifer[5];
31042 $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
31043 $b['am/pm'] = $lucifer[6];
31044 $b['name'] = $lucifer[8];
31045 } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
31046 //echo $line."\n";
31047 $lcount=count($lucifer);
31048 if ($lcount<8) return '';
31049 $b = array();
31050 $b['isdir'] = $lucifer[0]{0} === "d";
31051 $b['islink'] = $lucifer[0]{0} === "l";
31052 if ( $b['isdir'] )
31053 $b['type'] = 'd';
31054 elseif ( $b['islink'] )
31055 $b['type'] = 'l';
31056 else
31057 $b['type'] = 'f';
31058 $b['perms'] = $lucifer[0];
31059 $b['number'] = $lucifer[1];
31060 $b['owner'] = $lucifer[2];
31061 $b['group'] = $lucifer[3];
31062 $b['size'] = $lucifer[4];
31063 if ($lcount==8) {
31064 sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
31065 sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
31066 $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
31067 $b['name'] = $lucifer[7];
31068 } else {
31069 $b['month'] = $lucifer[5];
31070 $b['day'] = $lucifer[6];
31071 if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
31072 $b['year'] = date("Y");
31073 $b['hour'] = $l2[1];
31074 $b['minute'] = $l2[2];
31075 } else {
31076 $b['year'] = $lucifer[7];
31077 $b['hour'] = 0;
31078 $b['minute'] = 0;
31079 }
31080 $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
31081 $b['name'] = $lucifer[8];
31082 }
31083 }
31084
31085 return $b;
31086 }
31087
31088 function dirlist($path = '.', $incdot = false, $recursive = false) {
31089 if( $this->is_file($path) ) {
31090 $limitFile = basename($path);
31091 $path = dirname($path) . '/';
31092 } else {
31093 $limitFile = false;
31094 }
31095
31096 $list = @ftp_rawlist($this->link, '-a ' . $path, false);
31097
31098 if ( $list === false )
31099 return false;
31100
31101 $dirlist = array();
31102 foreach ( $list as $k => $v ) {
31103 $entry = $this->parselisting($v);
31104 if ( empty($entry) )
31105 continue;
31106
31107 if ( '.' == $entry["name"] || '..' == $entry["name"] )
31108 continue;
31109
31110 $dirlist[ $entry['name'] ] = $entry;
31111 }
31112
31113 if ( ! $dirlist )
31114 return false;
31115 if ( empty($dirlist) )
31116 return array();
31117
31118 $ret = array();
31119 foreach ( $dirlist as $struc ) {
31120
31121 if ( 'd' == $struc['type'] ) {
31122 $struc['files'] = array();
31123
31124 if ( $incdot ){
31125 //We're including the doted starts
31126 if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
31127 if ($recursive)
31128 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
31129 }
31130 } else { //No dots
31131 if ($recursive)
31132 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
31133 }
31134 }
31135 //File
31136 $ret[$struc['name']] = $struc;
31137 }
31138 return $ret;
31139 }
31140
31141 function __destruct(){
31142 if( $this->link )
31143 ftp_close($this->link);
31144 }
31145 }
31146
31147 ?>