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-ftpsockets.php < prev    next >
Encoding:
PHP Script  |  2008-05-30  |  7.6 KB  |  319 lines

  1. <?php
  2. class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
  3.     var $ftp = false;
  4.     var $timeout = 5;
  5.     var $errors;
  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_ftpsockets($opt='') {
  26.         $this->method = 'ftpsockets';
  27.         $this->errors = new WP_Error();
  28.  
  29.         //Check if possible to use ftp functions.
  30.         if( ! @include_once ABSPATH . 'wp-admin/includes/class-ftp.php' )
  31.                 return false;
  32.         $this->ftp = new ftp();
  33.  
  34.         //Set defaults:
  35.         if ( empty($opt['port']) )
  36.             $this->options['port'] = 21;
  37.         else
  38.             $this->options['port'] = $opt['port'];
  39.  
  40.         if ( empty($opt['hostname']) )
  41.             $this->errors->add('empty_hostname', __('FTP hostname is required'));
  42.         else
  43.             $this->options['hostname'] = $opt['hostname'];
  44.  
  45.         if ( isset($opt['base']) && ! empty($opt['base']) )
  46.             $this->wp_base = $opt['base'];
  47.  
  48.         // Check if the options provided are OK.
  49.         if ( empty ($opt['username']) )
  50.             $this->errors->add('empty_username', __('FTP username is required'));
  51.         else
  52.             $this->options['username'] = $opt['username'];
  53.  
  54.         if ( empty ($opt['password']) )
  55.             $this->errors->add('empty_password', __('FTP password is required'));
  56.         else
  57.             $this->options['password'] = $opt['password'];
  58.     }
  59.  
  60.     function connect() {
  61.         if ( ! $this->ftp )
  62.             return false;
  63.  
  64.         //$this->ftp->Verbose = true;
  65.  
  66.         if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
  67.             $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  68.             return false;
  69.         }
  70.         if ( ! $this->ftp->connect() ) {
  71.             $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  72.             return false;
  73.         }
  74.  
  75.         if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
  76.             $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
  77.             return false;
  78.         }
  79.  
  80.         $this->ftp->SetType(FTP_AUTOASCII);
  81.         $this->ftp->Passive(true);
  82.         return true;
  83.     }
  84.  
  85.     function setDefaultPermissions($perm) {
  86.         $this->permission = $perm;
  87.     }
  88.  
  89.     function get_contents($file, $type = '', $resumepos = 0){
  90.         if( ! $this->exists($file) )
  91.             return false;
  92.  
  93.         if( empty($type) ){
  94.             $extension = substr(strrchr($file, '.'), 1);
  95.             $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
  96.         }
  97.         $this->ftp->SetType($type);
  98.         $temp = wp_tempnam( $file );
  99.         if ( ! $temphandle = fopen($temp, 'w+') )
  100.             return false;
  101.         if ( ! $this->ftp->fget($temphandle, $file) ) {
  102.             fclose($temphandle);
  103.             unlink($temp);
  104.             return ''; //Blank document, File does exist, Its just blank.
  105.         }
  106.         fseek($temphandle, 0); //Skip back to the start of the file being written to
  107.         $contents = '';
  108.         while ( ! feof($temphandle) )
  109.             $contents .= fread($temphandle, 8192);
  110.         fclose($temphandle);
  111.         unlink($temp);
  112.         return $contents;
  113.     }
  114.  
  115.     function get_contents_array($file){
  116.         return explode("\n", $this->get_contents($file) );
  117.     }
  118.  
  119.     function put_contents($file, $contents, $type = '' ) {
  120.         if( empty($type) ){
  121.             $extension = substr(strrchr($file, '.'), 1);
  122.             $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
  123.         }
  124.         $this->ftp->SetType($type);
  125.  
  126.         $temp = wp_tempnam( $file );
  127.         if ( ! $temphandle = fopen($temp, 'w+') ){
  128.             unlink($temp);        
  129.             return false;
  130.         }
  131.         fwrite($temphandle, $contents);
  132.         fseek($temphandle, 0); //Skip back to the start of the file being written to
  133.         $ret = $this->ftp->fput($file, $temphandle);
  134.         fclose($temphandle);
  135.         unlink($temp);
  136.         return $ret;
  137.     }
  138.  
  139.     function cwd() {
  140.         $cwd = $this->ftp->pwd();
  141.         if( $cwd )
  142.             $cwd = trailingslashit($cwd);
  143.         return $cwd;
  144.     }
  145.  
  146.     function chdir($file) {
  147.         return $this->ftp->chdir($file);
  148.     }
  149.     
  150.     function chgrp($file, $group, $recursive = false ) {
  151.         return false;
  152.     }
  153.  
  154.     function chmod($file, $mode = false, $recursive = false ){
  155.         if( ! $mode )
  156.             $mode = $this->permission;
  157.         if( ! $mode )
  158.             return false;
  159.         //if( ! $this->exists($file) )
  160.         //    return false;
  161.         if( ! $recursive || ! $this->is_dir($file) ) {
  162.             return $this->ftp->chmod($file,$mode);
  163.         }
  164.         //Is a directory, and we want recursive
  165.         $filelist = $this->dirlist($file);
  166.         foreach($filelist as $filename){
  167.             $this->chmod($file . '/' . $filename, $mode, $recursive);
  168.         }
  169.         return true;
  170.     }
  171.  
  172.     function chown($file, $owner, $recursive = false ) {
  173.         return false;
  174.     }
  175.  
  176.     function owner($file) {
  177.         $dir = $this->dirlist($file);
  178.         return $dir[$file]['owner'];
  179.     }
  180.  
  181.     function getchmod($file) {
  182.         $dir = $this->dirlist($file);
  183.         return $dir[$file]['permsn'];
  184.     }
  185.  
  186.     function group($file) {
  187.         $dir = $this->dirlist($file);
  188.         return $dir[$file]['group'];
  189.     }
  190.  
  191.     function copy($source, $destination, $overwrite = false ) {
  192.         if( ! $overwrite && $this->exists($destination) )
  193.             return false;
  194.  
  195.         $content = $this->get_contents($source);
  196.         if ( false === $content )
  197.             return false;
  198.  
  199.         return $this->put_contents($destination, $content);
  200.     }
  201.  
  202.     function move($source, $destination, $overwrite = false ) {
  203.         return $this->ftp->rename($source, $destination);
  204.     }
  205.  
  206.     function delete($file, $recursive = false ) {
  207.         if ( $this->is_file($file) )
  208.             return $this->ftp->delete($file);
  209.         if ( !$recursive )
  210.             return $this->ftp->rmdir($file);
  211.  
  212.         return $this->ftp->mdel($file);
  213.     }
  214.  
  215.     function exists($file) {
  216.         return $this->ftp->is_exists($file);
  217.     }
  218.  
  219.     function is_file($file) {
  220.         return $this->is_dir($file) ? false : true;
  221.     }
  222.  
  223.     function is_dir($path) {
  224.         $cwd = $this->cwd();
  225.         if ( $this->chdir($path) ) {
  226.             $this->chdir($cwd);
  227.             return true;
  228.         }
  229.         return false;
  230.     }
  231.  
  232.     function is_readable($file) {
  233.         //Get dir list, Check if the file is writable by the current user??
  234.         return true;
  235.     }
  236.  
  237.     function is_writable($file) {
  238.         //Get dir list, Check if the file is writable by the current user??
  239.         return true;
  240.     }
  241.  
  242.     function atime($file) {
  243.         return false;
  244.     }
  245.  
  246.     function mtime($file) {
  247.         return $this->ftp->mdtm($file);
  248.     }
  249.  
  250.     function size($file) {
  251.         return $this->ftp->filesize($file);
  252.     }
  253.  
  254.     function touch($file, $time = 0, $atime = 0 ){
  255.         return false;
  256.     }
  257.  
  258.     function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
  259.         if( ! $this->ftp->mkdir($path) )
  260.             return false;
  261.         if( $chmod )
  262.             $this->chmod($path, $chmod);
  263.         if( $chown )
  264.             $this->chown($path, $chown);
  265.         if( $chgrp )
  266.             $this->chgrp($path, $chgrp);
  267.         return true;
  268.     }
  269.  
  270.     function rmdir($path, $recursive = false ) {
  271.         if( ! $recursive )
  272.             return $this->ftp->rmdir($path);
  273.  
  274.         return $this->ftp->mdel($path);
  275.     }
  276.  
  277.     function dirlist($path = '.', $incdot = false, $recursive = false ) {
  278.         if( $this->is_file($path) ) {
  279.             $limitFile = basename($path);
  280.             $path = dirname($path) . '/';
  281.         } else {
  282.             $limitFile = false;
  283.         }
  284.  
  285.         $list = $this->ftp->dirlist($path);
  286.         if( ! $list )
  287.             return false;
  288.         if( empty($list) )
  289.             return array();
  290.  
  291.         $ret = array();
  292.         foreach ( $list as $struc ) {
  293.  
  294.             if ( 'd' == $struc['type'] ) {
  295.                 $struc['files'] = array();
  296.  
  297.                 if ( $incdot ){
  298.                     //We're including the doted starts
  299.                     if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
  300.                         if ($recursive)
  301.                             $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
  302.                     }
  303.                 } else { //No dots
  304.                     if ($recursive)
  305.                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
  306.                 }
  307.             }
  308.             //File
  309.             $ret[$struc['name']] = $struc;
  310.         }
  311.         return $ret;
  312.     }
  313.  
  314.     function __destruct(){
  315.         $this->ftp->quit();
  316.     }
  317. }
  318. ?>
  319.