<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 */

/**
 * SecureFileStream
 *
 * @access      public
 * @package     yana
 * @subpackage  io
 */
class SecureFileStream extends SecureInputStream
{

    /**
     * insert
     *
     * @access public
     * @param scalar $text
     */
    function insert($text)
    {
        if (is_scalar($text)) {
            if (!isset($this->content)) {
                $this->content = array();
            }
            assert('is_array($this->content);');
            array_push($this->content, $text);
        } else {
            trigger_error("Scalar value expected for parameter 1, found '".gettype($text)."' instead.", E_USER_ERROR);
        }
    }

    /**
     * write
     *
     * @access public 
     * @return boolean
     */ 
    function write()
    {
        settype($this->content, "array");
        settype($this->source, "array");

        if ($this->is_writeable() && is_array($this->source) &&
           @$this->attributes['MTIME'] == date("d.m.Y",filemtime($this->filename)) &&
           ($this->attributes['FILESIZE'] == 0 || count($this->source) > 0)) {

            $handle = fopen($this->getFilename(), "w+"); flock($handle, 2);
            fwrite($handle, implode("", $this->content));
            flock($handle, 3); fclose($handle);
            $this->source = $this->content;
            return true;

        } else {
            return false;
        }
    }

    /**
     * delete
     *
     * @access public 
     * @return boolean
     */ 
    function delete()
    {
        if (@unlink($this->getFilename())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * failSafeWrite
     *
     * @access public 
     * @return boolean
     */ 
    function failSafeWrite()
    {
        for ($i=0; $i<3; $i++)
        {
            if ($this->write()) {
              return true;
              break;
            } else {
              sleep(0.7);
            }
        }
        return false;
    }

    /**
     * create
     *
     * @access public 
     * @return boolean
     */ 
    function create()
    {
        if (!$this->exists() && !empty($this->filename)) {
            $fhandle = fopen($this->filename, "w+");
            if ($fhandle !== false) {
                fclose($fhandle);
                $this->resetStats();
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * is_writeable
     *
     * @access public 
     * @return boolean
     */ 
    function is_writeable()
    {
        return ( $this->exists() && is_writeable($this->filename) );
    }

    /**
     * length
     *
     * @access public 
     * @return integer
     */ 
    function length()
    {
        if (!isset($this->content)) {
            return 0;
        } else {
            assert('is_array($this->content);');
            return count($this->content);
        }
    }

    /**
     * is_writeable
     *
     * @access public
     * @param string $key (optional)
     * @return boolean
     */ 
    function remove($key = "")
    {
        settype($key, "string");

        if ($key == "" || $key == "*") {
            $this->content = array();
            return true;
        } else {
            if (is_numeric($key)) {
                if (!isset($this->content[$key])) {
                    return false;
                } else {
                    array_splice($this->content, $key, 1);
                    return true;
                }
            } else {
                return Hashtable::remove($this->content, $key);
            }
        }

    }

}
?>