openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Scp.class.php (2931B)


      1 <?php
      2 // OpenRat Content Management System
      3 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      4 //
      5 // This program is free software; you can redistribute it and/or
      6 // modify it under the terms of the GNU General Public License
      7 // as published by the Free Software Foundation; either version 2
      8 // of the License, or (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU General Public License
     16 // along with this program; if not, write to the Free Software
     17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     18 namespace cms\generator\target;
     19 
     20 use logger\Logger;
     21 use util\exception\PublisherException;
     22 use util\exception\UIException;
     23 
     24 
     25 /**
     26  * Darstellen einer FTP-Verbindung, das beinhaltet
     27  * das Login, das Kopieren von Dateien sowie praktische
     28  * FTP-Funktionen
     29  *
     30  * @author $Author$
     31  * @version $Revision$
     32  * @package openrat.services
     33  */
     34 class Scp extends BaseTarget
     35 {
     36 	/**
     37 	 * @var resource
     38 	 */
     39 	protected $sshConnection;
     40 
     41 	// Aufbauen der Verbindung
     42 	public function open()
     43 	{
     44 		$this->sshConnection = $this->createConnection();
     45 	}
     46 
     47 
     48 
     49 	protected function createConnection() {
     50 
     51 		if   ( empty($this->url->port) )
     52 			$this->url->port = 22; // Default-SSH-port
     53 
     54 		$sshConnection = @ssh2_connect($this->url->host,$this->url->port );
     55 
     56 		if (! $sshConnection)
     57 			throw new PublisherException("Could not connect to ".$this->url->host.':'.$this->url->port );
     58 
     59 
     60 		if (! @ssh2_auth_password($sshConnection, $this->url->user,$this->url->pass) )
     61 			throw new PublisherException("Could not authenticate with user ".$this->url->user);
     62 
     63 		$this->sshConnection = $sshConnection;
     64 
     65 		return $sshConnection;
     66 	}
     67 
     68 
     69 
     70 	/**
     71 	 * Kopieren einer Datei vom lokalen System auf den FTP-Server.
     72 	 *
     73 	 * @param String Quelle
     74 	 * @param String Ziel
     75 	 * @param int FTP-Mode (BINARY oder ASCII)
     76 	 */
     77 	public function put($source, $dest, $lastChangeDate)
     78 	{
     79 		$dest = $this->url->path . '/' . $dest;
     80 
     81 		// ok, lets create the necessary directories on the remote side.
     82 		$stream = ssh2_exec($this->sshConnection,'mkdir -p '.dirname($dest) );
     83 		if   ( $stream )
     84 			fclose($stream);
     85 		else
     86 			throw new PublisherException("Failed to mkdir ".dirname($dest).' on remote');
     87 
     88 		$success = ssh2_scp_send($this->sshConnection, $source, $dest, 0644);
     89 
     90 		if   ( !$success) {
     91 			throw new PublisherException( 'Failed to publish '.$source.' to '.$dest );
     92 		}
     93 	}
     94 
     95 
     96 	/**
     97 	 * Schliessen der FTP-Verbindung.<br>
     98 	 * Sollte unbedingt aufgerufen werden, damit keine unn�tigen Sockets aufbleiben.
     99 	 */
    100 	public function close()
    101 	{
    102 		ssh2_disconnect($this->sshConnection);
    103 	}
    104 
    105 
    106 	public static function isAvailable()
    107 	{
    108 		return function_exists('ssh2_connect');
    109 	}
    110 }
    111 
    112 
    113 ?>