Location: PHPKode > projects > Entier Studio > entierstudio/framework/file.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 
 * @version	   $Id: file.manager.php 82 2008-01-18 18:04:17Z yannromefort $
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefFILEManager")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefFILEManager", "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 . "file.datasource.php");
    //-------------------------------------------------------------------------
    //	Class
    class FILEManager extends DataSourceManager {
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         * Data source constructor
         * @param	string	host
         * @param	string	port
         * @param	string	user
         * @param	string	pass
         * @param	string	base
         */
        function FILEManager($hostName = "", $hostPort = 0, $userName = "", $passWord = "", $directory = "") {
            //
            $this->m_Source = new FILEDataSource($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":
                    //
                    $path = @dirname($directory . "/" . $name);
                    $name = @basename($name);
                    if (empty($path))
					    $path = $directory;
                    //
                    break;
                case "directory":
                    //
                    $type = "dir";
                    $path = @dirname($directory . "/" . $name);
                    $name = @basename($name);
                    if (empty($path))
					    $path = $directory;
                    //
                    break;
                case "host":
                    //
                    return (true);
                    //
                    break;
            }
			//
			if ($this->m_Source->openDirectory($path) == false)
				return (false);
			//
			$dirlist = array();
			while (false !== ($file = @readdir($this->m_Source->Connection()))) {
				$dirlist[] = $file;
			}
			//
			$this->m_Source->closeDirectory();
			//
			while (list($code, $file) = @each($dirlist)) {
				//
				$case = @filetype($path . "/" . $file);
				//
				if (($name == $file) && ($case == $type)) {
					//
					@clearstatcache();
					//
					return (true);
				}
			}
			//
			@clearstatcache();
			//
			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) {
            //
            $done = 0;
            //
            if (empty($name) || empty($type))
			    return (false);
            //
            $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);
                    if (false == @file_exists($name)) {
                        if (empty($path) || (false == @file_exists($path)) || (false == @is_writable($path))) {
                            //
                            if (empty($path) || (false == @file_exists($path)))
							    $this->m_Error = FILEERR_PATH;
                            else
							    $this->m_Error = FILEERR_MASK;
                            //
                            @clearstatcache();
                            //
                            return (false);
                        }
                    } elseif (true == $save) {
                        if (@copy($name, $name . "." . date("U") . ".save") == false) {
                            //
                            $this->m_Error = FILEERR_COPY;
                            //
                            @clearstatcache();
                            //
                            return (false);
                        }
                    }
                    //
                    if (empty($data) || empty($mime)) {
                        //
                        if (empty($data))
						    $this->m_Error = FILEERR_DATA;
                        else
						    $this->m_Error = FILEERR_MIME;
                        //
                        @clearstatcache();
                        //
                        return (false);
                    }
                    //
                    $part = explode("/", $mime);
                    if (count($part) < 2) {
                        //
                        $this->m_Error = FILEERR_MIME;
                        //
                        @clearstatcache();
                        //
                        return (false);
                    }
                    //
                    $done = 0;
                    switch ($part[0]) {
                        case "text":
                            //
                            $file = @fopen($name, "w");
                            if ($file) {
                                $done = @fwrite($file, $data);
                                @fclose($file);
                            }
                        //                                
                        break;
                        default:
                            //
                            $file = @fopen($name, "wb "); ///???
                            if ($file) {
                                $done = @fwrite($file, $data, strlen($data));
                                @fclose($file);
                            }
                        //                                
                        break;
                    }
                    //
                    if ($done == 0) {
                        //
                        $this->m_Error = FILEERR_SAVE;
                    }
                //
                break;
                case "directory":
                    //
                    if (@file_exists($name)) {
                        //
                        $this->m_Error = FILEERR_LIFE;
                        //
                        @clearstatcache();
                        //
                        return (false);
                    }
                    //
                    $path = @dirname($name);
                    if (empty($path) || (@file_exists($path) == false)) {
                        //
                        $this->m_Error = FILEERR_PATH;
                        //
                        @clearstatcache();
                        //
                        return (false);
                    }
                    //
                    $todo = $make[FILEMAN_MAKE];
                    $mode = $make[FILEMAN_MODE];
                    //
                    switch ($todo) {
                        case FILECMD_CREATE:
                            //
                            $mask = @umask(0);
                            if (@mkdir($name, 0777) == true) // or even 01777 so you get the sticky bit set
						       $done = 1;
                            else
							    $this->m_Error = FILEERR_MAKE;
                            @umask($mask);
                        //
                        break;
                    }
                //
                break;
            } // type
            //
            @clearstatcache();
            //
            return ($done > 0);
        }
    };
    //	Class
    //-------------------------------------------------------------------------
            
}
//	namespace
//-----------------------------------------------------------------------------
        
?>
Return current item: Entier Studio