-
+ 793E8A50C47EBCA2824DD8B2CA9E681F8670286B499EE169B2DDD10CED61322BD86279A8376725483BAD2D4D42D499FBFE860EAFE505FCD25C32F930F44D9D01
mp-wp/wp-admin/includes/class-wp-filesystem-ssh2.php
(0 . 0)(1 . 507)
31481 <?php
31482 /**
31483 * WordPress SSH2 Filesystem.
31484 *
31485 * @package WordPress
31486 * @subpackage Filesystem
31487 */
31488
31489 /**
31490 * WordPress Filesystem Class for implementing SSH2.
31491 *
31492 * To use this class you must follow these steps for PHP 5.2.6+
31493 *
31494 * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
31495 *
31496 * Complie libssh2 (Note: Only 0.14 is officaly working with PHP 5.2.6+ right now.)
31497 *
31498 * cd /usr/src
31499 * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
31500 * tar -zxvf libssh2-0.14.tar.gz
31501 * cd libssh2-0.14/
31502 * ./configure
31503 * make all install
31504 *
31505 * Note: No not leave the directory yet!
31506 *
31507 * Enter: pecl install -f ssh2
31508 *
31509 * Copy the ssh.so file it creates to your PHP Module Directory.
31510 * Open up your PHP.INI file and look for where extensions are placed.
31511 * Add in your PHP.ini file: extension=ssh2.so
31512 *
31513 * Restart Apache!
31514 * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp exist.
31515 *
31516 *
31517 * @since 2.7
31518 * @package WordPress
31519 * @subpackage Filesystem
31520 * @uses WP_Filesystem_Base Extends class
31521 */
31522 class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
31523
31524 var $debugtest = false; // set this to true only if your a debuging your connection
31525
31526 var $link = null;
31527 var $sftp_link = null;
31528 var $keys = false;
31529 /*
31530 * This is the timeout value for ssh results to comeback.
31531 * Slower servers might need this incressed, but this number otherwise should not change.
31532 *
31533 * @parm $timeout int
31534 *
31535 */
31536 var $timeout = 15;
31537 var $errors = array();
31538 var $options = array();
31539
31540 var $permission = 0644;
31541
31542 function WP_Filesystem_SSH2($opt='') {
31543 $this->method = 'ssh2';
31544 $this->errors = new WP_Error();
31545
31546 //Check if possible to use ssh2 functions.
31547 if ( ! extension_loaded('ssh2') ) {
31548 $this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
31549 return false;
31550 }
31551
31552 // Set defaults:
31553 if ( empty($opt['port']) )
31554 $this->options['port'] = 22;
31555 else
31556 $this->options['port'] = $opt['port'];
31557
31558 if ( empty($opt['hostname']) )
31559 $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
31560 else
31561 $this->options['hostname'] = $opt['hostname'];
31562
31563 if ( isset($opt['base']) && ! empty($opt['base']) )
31564 $this->wp_base = $opt['base'];
31565
31566 // Check if the options provided are OK.
31567 if ( empty ($opt['username']) )
31568 $this->errors->add('empty_username', __('SSH2 username is required'));
31569 else
31570 $this->options['username'] = $opt['username'];
31571
31572 if ( ( !empty ($opt['public_key']) ) && ( !empty ($opt['private_key']) ) ) {
31573 $this->options['public_key'] = $opt['public_key'];
31574 $this->options['private_key'] = $opt['private_key'];
31575
31576 $this->options['hostkey'] = array("hostkey" => "ssh-rsa");
31577
31578 $this->keys = true;
31579 }
31580
31581
31582 if ( empty ($opt['password']) ) {
31583 if ( !$this->keys ) // password can be blank if we are using keys
31584 $this->errors->add('empty_password', __('SSH2 password is required'));
31585 } else {
31586 $this->options['password'] = $opt['password'];
31587 }
31588
31589 }
31590
31591 function connect() {
31592 $this->debug("connect();");
31593
31594 if ( ! $this->keys ) {
31595 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
31596 } else {
31597 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
31598 }
31599
31600 if ( ! $this->link ) {
31601 $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
31602 return false;
31603 }
31604
31605 if ( !$this->keys ) {
31606 if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
31607 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
31608 return false;
31609 }
31610 } else {
31611 if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
31612 $this->errors->add('auth', sprintf(__('Public and Private keys incorrent for %s'), $this->options['username']));
31613 return false;
31614 }
31615 }
31616
31617 $this->sftp_link = ssh2_sftp($this->link);
31618
31619 return true;
31620 }
31621
31622 function run_command($link, $command, $returnbool = false) {
31623 $this->debug("run_command();");
31624 if(!($stream = @ssh2_exec( $link, $command . "; echo \"__COMMAND_FINISHED__\";"))) {
31625 $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
31626 } else {
31627 stream_set_blocking( $stream, true );
31628 $time_start = time();
31629 $data = null;
31630 while( true ) {
31631 if (strpos($data,"__COMMAND_FINISHED__") !== false){
31632 break; // the command has finshed!
31633 }
31634 if( (time()-$time_start) > $this->timeout ){
31635 $this->errors->add('command', sprintf(__('Connection to the server has timeout after %s seconds.'), $this->timeout));
31636 unset($this->link);
31637 unset($this->sftp_link); // close connections
31638 return false;
31639 }
31640 while( $buf = fread( $stream, strlen($stream) ) )
31641 $data .= $buf;
31642 }
31643 fclose($stream);
31644 $data = trim(str_replace("__COMMAND_FINISHED__", "", $data));
31645 if (($returnbool) && ( (int) $data )) {
31646 return true;
31647 } elseif (($returnbool) && (! (int) $data )) {
31648 return false;
31649 } else {
31650 return $data;
31651 }
31652 }
31653 return false;
31654 }
31655
31656 function debug($text)
31657 {
31658 if ($this->debugtest)
31659 {
31660 echo "<br/>" . $text . "<br/>";
31661 }
31662 }
31663
31664 function setDefaultPermissions($perm) {
31665 $this->debug("setDefaultPermissions();");
31666 if ( $perm )
31667 $this->permission = $perm;
31668 }
31669
31670 function get_contents($file, $type = '', $resumepos = 0 ) {
31671 $this->debug("get_contents();");
31672 $tempfile = wp_tempnam( $file );
31673 if ( ! $tempfile )
31674 return false;
31675 if( ! ssh2_scp_recv($this->link, $file, $tempfile) )
31676 return false;
31677 $contents = file_get_contents($tempfile);
31678 unlink($tempfile);
31679 return $contents;
31680 }
31681
31682 function get_contents_array($file) {
31683 $this->debug("get_contents_array();");
31684 return explode("\n", $this->get_contents($file));
31685 }
31686
31687 function put_contents($file, $contents, $type = '' ) {
31688 $this->debug("put_contents($file);");
31689 $tempfile = wp_tempnam( $file );
31690 $temp = fopen($tempfile, 'w');
31691 if ( ! $temp )
31692 return false;
31693 fwrite($temp, $contents);
31694 fclose($temp);
31695 $ret = ssh2_scp_send($this->link, $tempfile, $file, $this->permission);
31696 unlink($tempfile);
31697 return $ret;
31698 }
31699
31700 function cwd() {
31701 $this->debug("cwd();");
31702 $cwd = $this->run_command($this->link, 'pwd');
31703 if( $cwd )
31704 $cwd = trailingslashit($cwd);
31705 return $cwd;
31706 }
31707
31708 function chdir($dir) {
31709 $this->debug("chdir();");
31710 return $this->run_command($this->link, 'cd ' . $dir, true);
31711 }
31712
31713 function chgrp($file, $group, $recursive = false ) {
31714 $this->debug("chgrp();");
31715 if ( ! $this->exists($file) )
31716 return false;
31717 if ( ! $recursive || ! $this->is_dir($file) )
31718 return $this->run_command($this->link, sprintf('chgrp %o %s', $mode, $file), true);
31719 return $this->run_command($this->link, sprintf('chgrp -R %o %s', $mode, $file), true);
31720 }
31721
31722 function chmod($file, $mode = false, $recursive = false) {
31723 $this->debug("chmod();");
31724 if( ! $mode )
31725 $mode = $this->permission;
31726 if( ! $mode )
31727 return false;
31728 if ( ! $this->exists($file) )
31729 return false;
31730 if ( ! $recursive || ! $this->is_dir($file) )
31731 return $this->run_command($this->link, sprintf('chmod %o %s', $mode, $file), true);
31732 return $this->run_command($this->link, sprintf('chmod -R %o %s', $mode, $file), true);
31733 }
31734
31735 function chown($file, $owner, $recursive = false ) {
31736 $this->debug("chown();");
31737 if ( ! $this->exists($file) )
31738 return false;
31739 if ( ! $recursive || ! $this->is_dir($file) )
31740 return $this->run_command($this->link, sprintf('chown %o %s', $mode, $file), true);
31741 return $this->run_command($this->link, sprintf('chown -R %o %s', $mode, $file), true);
31742 }
31743
31744 function owner($file) {
31745 $this->debug("owner();");
31746 $dir = $this->dirlist($file);
31747 return $dir[$file]['owner'];
31748 }
31749
31750 function getchmod($file) {
31751 $this->debug("getchmod();");
31752 $dir = $this->dirlist($file);
31753 return $dir[$file]['permsn'];
31754 }
31755
31756 function group($file) {
31757 $this->debug("group();");
31758 $dir = $this->dirlist($file);
31759 return $dir[$file]['group'];
31760 }
31761
31762 function copy($source, $destination, $overwrite = false ) {
31763 $this->debug("copy();");
31764 if( ! $overwrite && $this->exists($destination) )
31765 return false;
31766 $content = $this->get_contents($source);
31767 if( false === $content)
31768 return false;
31769 return $this->put_contents($destination, $content);
31770 }
31771
31772 function move($source, $destination, $overwrite = false) {
31773 $this->debug("move();");
31774 return @ssh2_sftp_rename($this->link, $source, $destination);
31775 }
31776
31777 function delete($file, $recursive = false) {
31778 $this->debug("delete();");
31779 if ( $this->is_file($file) )
31780 return ssh2_sftp_unlink($this->sftp_link, $file);
31781 if ( ! $recursive )
31782 return ssh2_sftp_rmdir($this->sftp_link, $file);
31783 $filelist = $this->dirlist($file);
31784 if ( is_array($filelist) ) {
31785 foreach ( $filelist as $filename => $fileinfo) {
31786 $this->delete($file . '/' . $filename, $recursive);
31787 }
31788 }
31789 return ssh2_sftp_rmdir($this->sftp_link, $file);
31790 }
31791
31792 function exists($file) {
31793 $this->debug("exists();");
31794 return $this->run_command($this->link, sprintf('ls -lad %s', $file), true);
31795 }
31796
31797 function is_file($file) {
31798 $this->debug("is_file();");
31799 //DO NOT RELY ON dirlist()!
31800 $list = $this->run_command($this->link, sprintf('ls -lad %s', $file));
31801 $list = $this->parselisting($list);
31802 if ( ! $list )
31803 return false;
31804 else
31805 return ( !$list['isdir'] && !$list['islink'] ); //ie. not a file or link, yet exists, must be file.
31806 }
31807
31808 function is_dir($path) {
31809 $this->debug("is_dir();");
31810 //DO NOT RELY ON dirlist()!
31811 $list = $this->parselisting($this->run_command($this->link, sprintf('ls -lad %s', untrailingslashit($path))));
31812 if ( ! $list )
31813 return false;
31814 else
31815 return $list['isdir'];
31816 }
31817
31818 function is_readable($file) {
31819 //Not implmented.
31820 }
31821
31822 function is_writable($file) {
31823 //Not implmented.
31824 }
31825
31826 function atime($file) {
31827 //Not implmented.
31828 }
31829
31830 function mtime($file) {
31831 //Not implmented.
31832 }
31833
31834 function size($file) {
31835 //Not implmented.
31836 }
31837
31838 function touch($file, $time = 0, $atime = 0) {
31839 //Not implmented.
31840 }
31841
31842 function mkdir($path, $chmod = null, $chown = false, $chgrp = false) {
31843 $this->debug("mkdir();");
31844 $path = untrailingslashit($path);
31845 if( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
31846 return false;
31847 if( $chown )
31848 $this->chown($path, $chown);
31849 if( $chgrp )
31850 $this->chgrp($path, $chgrp);
31851 return true;
31852 }
31853
31854 function rmdir($path, $recursive = false) {
31855 $this->debug("rmdir();");
31856 return $this->delete($path, $recursive);
31857 }
31858
31859 function parselisting($line) {
31860 $this->debug("parselisting();");
31861 $is_windows = ($this->OS_remote == FTP_OS_Windows);
31862 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)) {
31863 $b = array();
31864 if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
31865 $b['isdir'] = ($lucifer[7]=="<DIR>");
31866 if ( $b['isdir'] )
31867 $b['type'] = 'd';
31868 else
31869 $b['type'] = 'f';
31870 $b['size'] = $lucifer[7];
31871 $b['month'] = $lucifer[1];
31872 $b['day'] = $lucifer[2];
31873 $b['year'] = $lucifer[3];
31874 $b['hour'] = $lucifer[4];
31875 $b['minute'] = $lucifer[5];
31876 $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
31877 $b['am/pm'] = $lucifer[6];
31878 $b['name'] = $lucifer[8];
31879 } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
31880 //echo $line."\n";
31881 $lcount=count($lucifer);
31882 if ($lcount<8) return '';
31883 $b = array();
31884 $b['isdir'] = $lucifer[0]{0} === "d";
31885 $b['islink'] = $lucifer[0]{0} === "l";
31886 if ( $b['isdir'] )
31887 $b['type'] = 'd';
31888 elseif ( $b['islink'] )
31889 $b['type'] = 'l';
31890 else
31891 $b['type'] = 'f';
31892 $b['perms'] = $lucifer[0];
31893 $b['number'] = $lucifer[1];
31894 $b['owner'] = $lucifer[2];
31895 $b['group'] = $lucifer[3];
31896 $b['size'] = $lucifer[4];
31897 if ($lcount==8) {
31898 sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
31899 sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
31900 $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
31901 $b['name'] = $lucifer[7];
31902 } else {
31903 $b['month'] = $lucifer[5];
31904 $b['day'] = $lucifer[6];
31905 if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
31906 $b['year'] = date("Y");
31907 $b['hour'] = $l2[1];
31908 $b['minute'] = $l2[2];
31909 } else {
31910 $b['year'] = $lucifer[7];
31911 $b['hour'] = 0;
31912 $b['minute'] = 0;
31913 }
31914 $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
31915 $b['name'] = $lucifer[8];
31916 }
31917 }
31918
31919 return $b;
31920 }
31921
31922 function dirlist($path = '.', $incdot = false, $recursive = false) {
31923 $this->debug("dirlist();");
31924 if( $this->is_file($path) ) {
31925 $limitFile = basename($path);
31926 $path = trailingslashit(dirname($path));
31927 } else {
31928 $limitFile = false;
31929 }
31930
31931 $list = $this->run_command($this->link, sprintf('ls -la %s', $path));
31932
31933 if ( $list === false )
31934 return false;
31935
31936 $list = explode("\n", $list);
31937
31938 $dirlist = array();
31939 foreach ( (array)$list as $k => $v ) {
31940 $entry = $this->parselisting($v);
31941 if ( empty($entry) )
31942 continue;
31943
31944 if ( '.' == $entry['name'] || '..' == $entry['name'] )
31945 continue;
31946
31947 $dirlist[ $entry['name'] ] = $entry;
31948 }
31949
31950 if ( ! $dirlist )
31951 return false;
31952
31953 if ( empty($dirlist) )
31954 return array();
31955
31956 $ret = array();
31957 foreach ( $dirlist as $struc ) {
31958
31959 if ( 'd' == $struc['type'] ) {
31960 $struc['files'] = array();
31961
31962 if ( $incdot ){
31963 //We're including the doted starts
31964 if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
31965 if ($recursive)
31966 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
31967 }
31968 } else { //No dots
31969 if ( $recursive )
31970 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
31971 }
31972 }
31973 //File
31974 $ret[$struc['name']] = $struc;
31975 }
31976 return $ret;
31977 }
31978 function __destruct() {
31979 $this->debug("__destruct();");
31980 if ( $this->link )
31981 unset($this->link);
31982 if ( $this->sftp_link )
31983 unset($this->sftp_link);
31984 }
31985 }
31986
31987 ?>