home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / includes / class-wp-filesystem-ftpext.php < prev    next >
Encoding:
PHP Script  |  2008-05-29  |  10.2 KB  |  372 lines

  1. <?php
  2. class WP_Filesystem_FTPext extends WP_Filesystem_Base{
  3.     var $link;
  4.     var $timeout = 5;
  5.     var $errors = array();
  6.     var $options = array();
  7.  
  8.     var $permission = null;
  9.  
  10.     var $filetypes = array(
  11.                             'php'=>FTP_ASCII,
  12.                             'css'=>FTP_ASCII,
  13.                             'txt'=>FTP_ASCII,
  14.                             'js'=>FTP_ASCII,
  15.                             'html'=>FTP_ASCII,
  16.                             'htm'=>FTP_ASCII,
  17.                             'xml'=>FTP_ASCII,
  18.  
  19.                             'jpg'=>FTP_BINARY,
  20.                             'png'=>FTP_BINARY,
  21.                             'gif'=>FTP_BINARY,
  22.                             'bmp'=>FTP_BINARY
  23.                             );
  24.  
  25.     function WP_Filesystem_FTPext($opt='') {
  26.         $this->method = 'ftpext';
  27.         $this->errors = new WP_Error();
  28.  
  29.         //Check if possible to use ftp functions.
  30.         if ( ! extension_loaded('ftp') ) {
  31.             $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
  32.             return false;
  33.         }
  34.  
  35.         // Set defaults:
  36.         if ( empty($opt['port']) )
  37.             $this->options['port'] = 21;
  38.         else
  39.             $this->options['port'] = $opt['port'];
  40.  
  41.         if ( empty($opt['hostname']) )
  42.             $this->errors->add('empty_hostname', __('FTP hostname is required'));
  43.         else
  44.             $this->options['hostname'] = $opt['hostname'];
  45.  
  46.         if ( isset($opt['base']) && ! empty($opt['base']) )
  47.             $this->wp_base = $opt['base'];
  48.  
  49.         // Check if the options provided are OK.
  50.         if ( empty ($opt['username']) )
  51.             $this->errors->add('empty_username', __('FTP username is required'));
  52.         else
  53.             $this->options['username'] = $opt['username'];
  54.  
  55.         if ( empty ($opt['password']) )
  56.             $this->errors->add('empty_password', __('FTP password is required'));
  57.         else
  58.             $this->options['password'] = $opt['password'];
  59.  
  60.         $this->options['ssl'] = ( !empty($opt['ssl']) );
  61.     }
  62.  
  63.     function connect() {
  64.         if ( $this->options['ssl'] && function_exists('ftp_ssl_connect') )
  65.             $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'],$this->timeout);
  66.         else
  67.             $this->link = @ftp_connect($this->options['hostname'], $this->options['port'],$this->timeout);
  68.  
  69.         if ( ! $this->link ) {
  70.             $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  71.             return false;
  72.         }
  73.  
  74.         if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) {
  75.             $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
  76.             return false;
  77.         }
  78.  
  79.         return true;
  80.     }
  81.  
  82.     function setDefaultPermissions($perm) {
  83.         $this->permission = $perm;
  84.     }
  85.     
  86.     function get_contents($file, $type = '', $resumepos = 0 ){
  87.         if( empty($type) ){
  88.             $extension = substr(strrchr($file, "."), 1);
  89.             $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
  90.         }
  91.         $temp = tmpfile();
  92.         if ( ! $temp )
  93.             return false;
  94.         if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
  95.             return false;
  96.         fseek($temp, 0); //Skip back to the start of the file being written to
  97.         $contents = '';
  98.         while (!feof($temp)) {
  99.             $contents .= fread($temp, 8192);
  100.         }
  101.         fclose($temp);
  102.         return $contents;
  103.     }
  104.     function get_contents_array($file) {
  105.         return explode("\n", $this->get_contents($file));
  106.     }
  107.     function put_contents($file, $contents, $type = '' ) {
  108.         if( empty($type) ) {
  109.             $extension = substr(strrchr($file, "."), 1);
  110.             $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
  111.         }
  112.         $temp = tmpfile();
  113.         if ( ! $temp )
  114.             return false;
  115.         fwrite($temp, $contents);
  116.         fseek($temp, 0); //Skip back to the start of the file being written to
  117.         $ret = @ftp_fput($this->link, $file, $temp, $type);
  118.         fclose($temp);
  119.         return $ret;
  120.     }
  121.     function cwd() {
  122.         $cwd = ftp_pwd($this->link);
  123.         if( $cwd )
  124.             $cwd = trailingslashit($cwd);
  125.         return $cwd;
  126.     }
  127.     function chdir($dir) {
  128.         return @ftp_chdir($dir);
  129.     }
  130.     function chgrp($file, $group, $recursive = false ) {
  131.         return false;
  132.     }
  133.     function chmod($file, $mode = false, $recursive = false) {
  134.         if( ! $mode )
  135.             $mode = $this->permission;
  136.         if( ! $mode )
  137.             return false;
  138.         if ( ! $this->exists($file) )
  139.             return false;
  140.         if ( ! $recursive || ! $this->is_dir($file) ) {
  141.             if ( ! function_exists('ftp_chmod') )
  142.                 return @ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
  143.             return @ftp_chmod($this->link, $mode, $file);
  144.         }
  145.         //Is a directory, and we want recursive
  146.         $filelist = $this->dirlist($file);
  147.         foreach($filelist as $filename){
  148.             $this->chmod($file . '/' . $filename, $mode, $recursive);
  149.         }
  150.         return true;
  151.     }
  152.     function chown($file, $owner, $recursive = false ) {
  153.         return false;
  154.     }
  155.     function owner($file) {
  156.         $dir = $this->dirlist($file);
  157.         return $dir[$file]['owner'];
  158.     }
  159.     function getchmod($file) {
  160.         $dir = $this->dirlist($file);
  161.         return $dir[$file]['permsn'];
  162.     }
  163.     function group($file) {
  164.         $dir = $this->dirlist($file);
  165.         return $dir[$file]['group'];
  166.     }
  167.     function copy($source, $destination, $overwrite = false ) {
  168.         if( ! $overwrite && $this->exists($destination) )
  169.             return false;
  170.         $content = $this->get_contents($source);
  171.         if( false === $content)
  172.             return false;
  173.         return $this->put_contents($destination, $content);
  174.     }
  175.     function move($source, $destination, $overwrite = false) {
  176.         return ftp_rename($this->link, $source, $destination);
  177.     }
  178.  
  179.     function delete($file,$recursive=false) {
  180.         if ( $this->is_file($file) )
  181.             return @ftp_delete($this->link, $file);
  182.         if ( !$recursive )
  183.             return @ftp_rmdir($this->link, $file);
  184.         $filelist = $this->dirlist($file);
  185.         foreach ((array) $filelist as $filename => $fileinfo) {
  186.             $this->delete($file . '/' . $filename, $recursive);
  187.         }
  188.         return @ftp_rmdir($this->link, $file);
  189.     }
  190.  
  191.     function exists($file) {
  192.         $list = ftp_rawlist($this->link, $file, false);
  193.         if( ! $list )
  194.             return false;
  195.         return count($list) == 1 ? true : false;
  196.     }
  197.     function is_file($file) {
  198.         return $this->is_dir($file) ? false : true;
  199.     }
  200.     function is_dir($path) {
  201.         $cwd = $this->cwd();
  202.         $result = @ftp_chdir($this->link, $path);
  203.         if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
  204.             @ftp_chdir($this->link, $cwd);
  205.             return true;
  206.         }
  207.         return false;
  208.     }
  209.     function is_readable($file) {
  210.         //Get dir list, Check if the file is writable by the current user??
  211.         return true;
  212.     }
  213.     function is_writable($file) {
  214.         //Get dir list, Check if the file is writable by the current user??
  215.         return true;
  216.     }
  217.     function atime($file) {
  218.         return false;
  219.     }
  220.     function mtime($file) {
  221.         return ftp_mdtm($this->link, $file);
  222.     }
  223.     function size($file) {
  224.         return ftp_size($this->link, $file);
  225.     }
  226.     function touch($file, $time = 0, $atime = 0) {
  227.         return false;
  228.     }
  229.     function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
  230.         if( !@ftp_mkdir($this->link, $path) )
  231.             return false;
  232.         if( $chmod )
  233.             $this->chmod($path, $chmod);
  234.         if( $chown )
  235.             $this->chown($path, $chown);
  236.         if( $chgrp )
  237.             $this->chgrp($path, $chgrp);
  238.         return true;
  239.     }
  240.     function rmdir($path, $recursive = false) {
  241.         if( ! $recursive )
  242.             return @ftp_rmdir($this->link, $path);
  243.  
  244.         //TODO: Recursive Directory delete, Have to delete files from the folder first.
  245.         //$dir = $this->dirlist($path);
  246.         //foreach($dir as $file)
  247.  
  248.     }
  249.  
  250.     function parselisting($line) {
  251.         $is_windows = ($this->OS_remote == FTP_OS_Windows);
  252.         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)) {
  253.             $b = array();
  254.             if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
  255.             $b['isdir'] = ($lucifer[7]=="<DIR>");
  256.             if ( $b['isdir'] )
  257.                 $b['type'] = 'd';
  258.             else
  259.                 $b['type'] = 'f';
  260.             $b['size'] = $lucifer[7];
  261.             $b['month'] = $lucifer[1];
  262.             $b['day'] = $lucifer[2];
  263.             $b['year'] = $lucifer[3];
  264.             $b['hour'] = $lucifer[4];
  265.             $b['minute'] = $lucifer[5];
  266.             $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
  267.             $b['am/pm'] = $lucifer[6];
  268.             $b['name'] = $lucifer[8];
  269.         } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
  270.             //echo $line."\n";
  271.             $lcount=count($lucifer);
  272.             if ($lcount<8) return '';
  273.             $b = array();
  274.             $b['isdir'] = $lucifer[0]{0} === "d";
  275.             $b['islink'] = $lucifer[0]{0} === "l";
  276.             if ( $b['isdir'] )
  277.                 $b['type'] = 'd';
  278.             elseif ( $b['islink'] )
  279.                 $b['type'] = 'l';
  280.             else
  281.                 $b['type'] = 'f';
  282.             $b['perms'] = $lucifer[0];
  283.             $b['number'] = $lucifer[1];
  284.             $b['owner'] = $lucifer[2];
  285.             $b['group'] = $lucifer[3];
  286.             $b['size'] = $lucifer[4];
  287.             if ($lcount==8) {
  288.                 sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
  289.                 sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
  290.                 $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
  291.                 $b['name'] = $lucifer[7];
  292.             } else {
  293.                 $b['month'] = $lucifer[5];
  294.                 $b['day'] = $lucifer[6];
  295.                 if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
  296.                     $b['year'] = date("Y");
  297.                     $b['hour'] = $l2[1];
  298.                     $b['minute'] = $l2[2];
  299.                 } else {
  300.                     $b['year'] = $lucifer[7];
  301.                     $b['hour'] = 0;
  302.                     $b['minute'] = 0;
  303.                 }
  304.                 $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
  305.                 $b['name'] = $lucifer[8];
  306.             }
  307.         }
  308.  
  309.         return $b;
  310.     }
  311.  
  312.     function dirlist($path = '.', $incdot = false, $recursive = false) {
  313.         if( $this->is_file($path) ) {
  314.             $limitFile = basename($path);
  315.             $path = dirname($path) . '/';
  316.         } else {
  317.             $limitFile = false;
  318.         }
  319.  
  320.         $list = @ftp_rawlist($this->link, '-a ' . $path, false);
  321.  
  322.         if ( $list === false )
  323.             return false;
  324.  
  325.         $dirlist = array();
  326.         foreach ( $list as $k => $v ) {
  327.             $entry = $this->parselisting($v);
  328.             if ( empty($entry) )
  329.                 continue;
  330.  
  331.             if ( '.' == $entry["name"] || '..' == $entry["name"] )
  332.                 continue;
  333.  
  334.             $dirlist[ $entry['name'] ] = $entry;
  335.         }
  336.  
  337.         if ( ! $dirlist )
  338.             return false;
  339.         if ( empty($dirlist) )
  340.             return array();
  341.  
  342.         $ret = array();
  343.         foreach ( $dirlist as $struc ) {
  344.  
  345.             if ( 'd' == $struc['type'] ) {
  346.                 $struc['files'] = array();
  347.  
  348.                 if ( $incdot ){
  349.                     //We're including the doted starts
  350.                     if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
  351.                         if ($recursive)
  352.                             $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
  353.                     }
  354.                 } else { //No dots
  355.                     if ($recursive)
  356.                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
  357.                 }
  358.             }
  359.             //File
  360.             $ret[$struc['name']] = $struc;
  361.         }
  362.         return $ret;
  363.     }
  364.  
  365.     function __destruct(){
  366.         if( $this->link )
  367.             ftp_close($this->link);
  368.     }
  369. }
  370.  
  371. ?>
  372.