<?php

/*  Alternate WebKit
(C) 2011 by Alternate Tools
-----------------------------------------------------

File:         class_storagefile.php
Description:  Class cStorageFile definition,
              abstraction of file storage

*/

if (!defined("CLASS_STORAGEFILE"))
{
    define ("CLASS_STORAGEFILE", "DEFINED");

    class cStorageFile
    {
        var $m_strgFileName;
        var $m_strgType;
        var $m_strgVersion;
        var $m_strgData;
        var $m_strgLineDivider;

        function cStorageFile()  // PHP 4
        {
            $this->InitStorageFile();
        }
        
        function __construct()
        {
            $this->InitStorageFile();
        }

        function InitStorageFile()
        {
            $this->m_strgFileName = "";
            $this->m_strgType = "unknown";
            $this->m_strgVersion = "V0.000";
            $this->m_strgData = array();
            $this->m_strgLineDivider = "-$|$-";
        }

        // Getter and setter
        
        function SetFileName($fileName)
        {
            $this->m_strgFileName = $fileName;
        }

        function GetType()
        {
            return $this->m_strgType;
        }
        function SetType($type)
        {
            $this->m_strgType = $type;
        }

        function GetVersion()
        {
            return $this->m_strgVersion;
        }
        function SetVersion($version)
        {
            $this->m_strgVersion = $version;
        }

        function SetDivider($divider)
        {
            $this->m_strgLineDivider = $divider;
        }

        // Main methods

        function GetData($key)
        {
            if (isset($this->m_strgData[$key]) == true)
            {
                $tmpDataArray = explode($this->m_strgLineDivider, $this->m_strgData[$key]);
                return $this->NoMagic(implode("\r\n", $tmpDataArray));
            }
            // Not found
            return "";
        }

        function AddData($key, $data, $isString = true, $replaceQuotes = true)
        {
            if ($isString == true)  // Avoid linebreaks
            {
                $tmpDataArray = explode("\r\n", $data);
                $data = implode($this->m_strgLineDivider, $tmpDataArray);
            }
            if ($replaceQuotes == true)
            {
                $data = $this->NoDoubleQuotes($data);
            }
            $this->m_strgData[$key] = array($data, $isString);
        }
        
        function LoadStorageFromFile($fileName)
        {
            $this->SetFileName($fileName);
            return $this->LoadStorage();
        }

        function LoadStorage()
        {
            if (file_exists($this->m_strgFileName) == false)
                return false;
                
            $this->m_strgData = parse_ini_file($this->m_strgFileName);
            if (isset($this->m_strgData["Version"]) == true)
            {
                $this->SetVersion($this->m_strgData["Version"]);
                // Unset it from array, cause it will be added on save
                unset($this->m_strgData["Version"]);
            }

            return true;

        }

        function SaveStorageToFile($fileName)
        {
            $this->SetFileName($fileName);
            return $this->SaveStorage();
        }

        function SaveStorage()
        {
            $newRow = "\r\n";

            $fp = fopen($this->m_strgFileName, "w");
            if ($fp == false)
                return false;

            fputs($fp, '[' . $this->m_strgType . ']' . $newRow);
            fputs($fp, $this->NoMagic('Version="' . $this->m_strgVersion . '"' . $newRow));
            foreach ($this->m_strgData as $key => $value)
            {
                if ($value[1] == true)
                    fputs($fp, $this->NoMagic($key . '="' . $value[0] . '"' . $newRow));
                else
                    fputs($fp, $this->NoMagic($key . '=' . $value[0] . $newRow));
            }
            fclose($fp);

            return true;
        }

        function NoMagic($string)
        {
            if (get_magic_quotes_gpc())  // Magic quotes??
                return stripslashes($string);
            return $string;
        }
        
        function NoDoubleQuotes($string)
        {
            // No doublequotes
            return str_replace('"', "'", $string);
        }
    }
}
?>