Location: PHPKode > projects > Entier Studio > entierstudio/framework/ssh2.datasource.php
<?php
/**
 * Entier Studio
 *
 * LICENSE
 *
 *	Copyright 2006 Entier Studio team.
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *
 *		http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 *
 * @package    entier.framework
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 * @version	   $Id: ssh2.datasource.php 84 2008-01-19 23:49:20Z yannromefort $
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefSSH2DataSource")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefSSH2DataSource", "1");
    //-------------------------------------------------------------------------
    //	Include
    @require_once (FRAMEWORK_DIR . "datasource.php");
    //-------------------------------------------------------------------------
    //	Class
    class SSH2DataSource extends DataSource {
        //---------------------------------------------------------------------
        //	Attributes
        
        /**
         * ssh connection resource
         * @var	resource
         * @see
         */
        var $m_sshResource = false;
        /**
         * ftp connection resource
         * @var	resource
         * @see
         */
        var $m_ftpResource = false;
        /**
         * error string
         * @var	string
         * @see
         */
        var $m_errorString = "";
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         * Data source constructor
         * @param	string	host
         * @param	string	port
         * @param	string	user
         * @param	string	pass
         * @param	string	base
         */
        function SSH2DataSource($hostName = "", $hostPort = 22, $userName = "", $passWord = "", $fileRoot = "") {
            $this->m_protocol = "ssh2.sftp";
            $this->m_hostName = $hostName;
            $this->m_hostPort = $hostPort;
            $this->m_userName = $userName;
            $this->m_passWord = $passWord;
            $this->m_dataBase = $fileRoot;
        }
        //---------------------------------------------------------------------
        //	Properties
        
        /**
         *
         * overrided DataSource property
         *
         * @return	object
         *
         * @see
         */
        function getDataHandler() {
            //
            return ($this);
        }
		/**
		 * ~= Database property	
         *
         * @return	string
         */
        function Directory() {
            //
            return ($this->m_dataBase);
        }
        /**
         *
         * overrided DataSource property
         *
         * @return	string
         */
        function errorString() {
            //
            if ($this->isConnectionOpen() == false) return ("SSH2 server not available");
            //
            return ("");
        }
        /**
         *
         * overrided DataSource property
         *
         * @return	integer
         */
        function errorNumber() {
            //
            return (0);
        }
        //---------------------------------------------------------------------
        //	Methods
        //
        // DataSource interface
        //
        
        /**
         * overrided DataSource method
         *
         * @param	object	DataSource
         * @return	boolean
         */
        function openConnection() {
            //
            if (@version_compare(PHP_VERSION, '5', '>=') && @extension_loaded('ssh2')) {
                //
                $this->m_sshResource = @ssh2_connect($this->m_hostName, $this->m_hostPort);
                if ($this->m_sshResource != false) {
                    //
                    if (@ssh2_auth_password($this->m_sshResource, $this->m_userName, $this->m_passWord) == true) {
                        $this->m_ftpResource = @ssh2_sftp($this->m_sshResource);
                        return ($this->m_ftpResource != false);
                    }
                    //
                    $this->closeConnection();
                }
            }
            //
            return (false);
        }
        /**
         * overrided DataSource method
         *
         * @return	boolean
         */
        function closeConnection() {
            //
            if (($this->m_sshResource == false) || ($this->m_ftpResource == false)) return (false);
            //
            unset($this->m_sshResource);
            unset($this->m_ftpResource);
            //
            return (true);
        }
        /**
         * resource locator builder
         * @param	string	$item	database item name
         * @return	string
         */
        function URLocator($node = "") {
            //
            $wrapper = "ssh2.sftp://" . $this->m_ftpResource;
            if ($node != "") $locator = $wrapper . $node;
            else $locator = $wrapper . $this->m_dataBase;
            //
            return ($locator);
        }
        /**
         *
         * @param	object	DataSource
         * @return	boolean
         */
        function openDirectory($fileRoot = "") {
            //
            if (($this->m_sshResource == false) || ($this->m_ftpResource == false))
                return (false);
            //
            // "ssh2.sftp://$sftp/path/to/dir/";
            $wrapper = "ssh2.sftp://" . $this->m_ftpResource;
            if ($fileroot != "")
                $directory = $wrapper . $fileroot;
            else
                $directory = $wrapper . $this->m_dataBase;
            //
            $this->m_resource = @opendir($directory);
            //
            return (false !== $this->m_resource);
        }
        /**
         *
         * @return	boolean
         */
        function closeDirectory() {
            //
            if ($this->isConnectionOpen() == false)
                return (false);
            //
            @closedir($this->m_resource);
            $this->m_resource = false;
            //
            return (true);
        }
    };
    //	Class
    //-------------------------------------------------------------------------
    
}
//	namespace
//-----------------------------------------------------------------------------
?>
Return current item: Entier Studio