Location: PHPKode > projects > Entier Studio > entierstudio/framework/ftp.manager.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: ftp.manager.php 83 2008-01-18 22:24:14Z yannromefort $
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefFTPManager")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefFTPManager", "1");
    //
    define(FILEMAN_MAKE, 0);
    define(FILEMAN_SAVE, 1);
    define(FILEMAN_MODE, 2);
    define(FILEMAN_DATA, 3);
    define(FILEMAN_MIME, 4);
    //
    define(FILEERR_LIFE, 1); // existing resource
    define(FILEERR_PATH, 2); // invalid resource path
    define(FILEERR_MAKE, 3); // dir creation error
    define(FILEERR_MASK, 4); // invalid rights
    define(FILEERR_COPY, 5); // file copy error
    define(FILEERR_DATA, 6); // no data
    define(FILEERR_MIME, 7); // no mime
    define(FILEERR_SAVE, 8); // file write error
    //
    define(FILECMD_CREATE, 1);
    //-------------------------------------------------------------------------
    //	Include
    @require_once (FRAMEWORK_DIR . "datasource.manager.php");
    @require_once (FRAMEWORK_DIR . "ftp.datasource.php");
    //-------------------------------------------------------------------------
    //	Class
    class FTPManager extends DataSourceManager {
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         * Data source constructor
         * @param	string	host
         * @param	string	port
         * @param	string	user
         * @param	string	pass
         * @param	string	base
         */
        function FTPManager($hostName = "", $hostPort = 0, $userName = "", $passWord = "", $directory = "") {
            //
            $this->m_Source = new FTPDataSource($hostName, $hostPort, $userName, $passWord, $directory);
        }
        //---------------------------------------------------------------------
        //	Methods
        //	DataSourceManager interface
        
        /**
         * Find a data source element
         * @param	string	Target element name
         * @param	string	Target element type
         * @return	boolean	Return flag
         */
        function selectObject($name = "", $type = "") {
            //
            $directory = $this->m_Source->Directory();
            // normalize path name and type
			switch ($type) {                
                case "file":
                    $type = "-";
                break;
                case "directory":
                    $type = "d";
                break;
                case "host":
                    return (true);
				break;
            }
			$path = @dirname($directory . "/" . $name);
			if (DIRECTORY_SEPARATOR == $path)
				$path = '/';
			$name = @basename($name);
			//
			if ($this->openSource() == false)
			    return (false);
			//
			$systype = @ftp_systype($this->m_Source->Connection());
			$dirlist = @ftp_rawlist($this->m_Source->Connection() , $path);
			//
			$this->m_Source->closeConnection();
			//
			while ((is_array($dirlist)) && (list($code, $line) = @each($dirlist))) {
				//
				switch ($systype) {
					case "UNIX":
						//	http://inebria.com/phpftp/ftp.phps
						$regs = split("[ ]+", $line, 9);
						$case = substr($regs[0], 0, 1);
						$file = $regs[8];
						$size = $regs[4];
						//
						if (($name === $file) && ($type === $case))
						    return (true);
						//
						break;
					}
			}
			//
			return (false);
		}
		/**
		 * Perform a data source query
		 * @param	string	Target element name
		 * @param	string	Target element type
		 * @param	Array	Target element parameters
		 * @return	boolean	Return flag
		 */
		function createObject($name = "", $type = "", $make = NULL) {
			//
			if (empty($name))
			    return (false);
			//
			if ($this->openSource() == false)
			    return (false);
			//			
			$done = false;
			//
			$directory = $this->m_Source->Directory();
            if( "" !== $directory)
				$name = $this->m_Source->Directory() . "/" . $name;			
			//			
			switch ($type) {
				case "file":
					//					
					$todo = $make[FILEMAN_MAKE];
					$save = $make[FILEMAN_SAVE];
					$data = $make[FILEMAN_DATA];
					$mime = $make[FILEMAN_MIME];
					//
					$path = @dirname($name);
					//
					//	1*	check if directory exists
					if (false == @ftp_chdir($this->m_Source->Connection() , $path))
					    break;
					//
					//	2*	check if filename exists
					$list = array();
					$list = @ftp_nlist($this->m_Source->Connection() , $path);
					if (@in_array($name, $list) && ($save == true)) {
						//	2.1*	backup filename if necessary
						if (false == @ftp_rename($this->m_Source->Connection() , $name, $name . "." . date("U") . ".save"))
						    break;
					}
					//
					//	3*	Check Resource object
					//
					if (empty($data) || empty($mime)) {
						//
						if (empty($data))
						    $this->m_Error = FILEERR_DATA;
						else
						    $this->m_Error = FILEERR_MIME;
						//
						return (false);
					}
					// check filemime
					$part = explode("/", $mime);
					if (count($part) < 2) {
						//
						$this->m_Error = FILEERR_MIME;
						//
						return (false);
					}
					//
					//	4*	Copy Resource to a temporary file
					$file = @tmpfile();
					if (!$file)
					    break;
					//
					$done = false;
					switch ($part[0]) {
						case "text":
							// mime text
							$done = @fwrite($file, $data);
							if ($done > 0)
							    $done = @rewind($file);
							if (true == $done)
							    $done = @ftp_fput($this->m_Source->Connection() , $name, $file, FTP_ASCII);
							//
							break;
						default:
							// 
							$done = @fwrite($file, $data, strlen($data));
							if ($done > 0)
							   $done = @rewind($file);
							if (true == $done)
							   $done = @ftp_fput($this->m_Source->Connection() , $name, $file, FTP_BINARY);
							//
							break;
					}
					//
                    if (false == $done) {
                        //
                        $this->m_Error = FILEERR_SAVE;
                    }
					//
					@fclose($file);
					//
				break;
				case "directory":
					//@ftp_chdir
					$path = @dirname($name);
					//	check if directory exists
					if (true == @ftp_chdir($this->m_Source->Connection() , $name)) {
						//
						$this->m_Error = FILEERR_LIFE;
						//
						return (false);
					} else {
						// not existing
						if (!empty($path) && (true == @ftp_chdir($this->m_Source->Connection(), $path))) {
							//
							$todo = $make[FILEMAN_MAKE];
							$mode = $make[FILEMAN_MODE];
							//
							$name = @basename($name);
							//
							switch ($todo) {
								case FILECMD_CREATE:
									//
									if (false !== @ftp_mkdir($this->m_Source->Connection() , $name)) 
									    $done = true;
									else
									    $this->m_Error = FILEERR_MAKE;
									//
								break;
							}
						}
					}
					//
				break;
			}
			// FIXME close connection ?
			// $this->closeSource();
			//
			return ($done);
		}
	};
	//	Class
	//-------------------------------------------------------------------------
	
}
//	namespace
//-----------------------------------------------------------------------------
?>
Return current item: Entier Studio