<?php
if (!defined('ABSPATH'))
{
    exit;
}

abstract class PDAPP_BasePhpFastCache
{
    var $tmp = array();
    var $config = array();
    var $fallback = false;
    var $instant;

    /*
     * Basic Functions
	 * time default 5 years
     */
    public function pdapp_set($keyword, $value = "", $time = 157680000, $option = array())
    {
        /*
         * Infinity Time
         * Khoa. B
         */
        if((Int)$time <= 0)
        {
            $time = 3600*24*365*5;
        }

        /*
         * Temporary disabled PDAPP_PhpFastCache::$disabled = true
         * Khoa. B
         */
        if(PDAPP_PhpFastCache::$disabled === true)
        {
            return false;
        }
        $object = array(
            "value" => $value,
            "write_time"  => gmdate("U"),
            "expired_in"  => $time,
            "expired_time"  => gmdate("U") + (Int)$time,
        );

        return $this->pdapp_driver_set($keyword,$object,$time,$option);
    }

    public function pdapp_get($keyword, $option = array())
    {
        /*
       * Temporary disabled PDAPP_PhpFastCache::$disabled = true
       * Khoa. B
       */
        if(PDAPP_PhpFastCache::$disabled === true)
        {
            return null;
        }

        $object = $this->pdapp_driver_get($keyword,$option);
        if($object == null)
        {
            return null;
        }
        if (!isset($object) || !isset($object['value']))
        {
            return null;
        }

        return isset($option['all_keys']) && $option['all_keys'] ? $object : $object['value'];
    }
    
    function pdapp_get_info($keyword, $option = array())
    {
        $object = $this->pdapp_driver_get($keyword,$option);

        if($object == null)
        {
            return null;
        }

        return $object;
    }

    function pdapp_delete($keyword, $option = array())
    {
        return $this->pdapp_driver_delete($keyword,$option);
    }

    function pdapp_stats($option = array())
    {
        return $this->pdapp_driver_stats($option);
    }

    function pdapp_clean($option = array())
    {
        return $this->pdapp_driver_clean($option);
    }

    function pdapp_is_existing($keyword)
    {
        if(method_exists($this,"pdapp_driver_is_existing"))
        {
            return $this->pdapp_driver_is_existing($keyword);
        }

        $data = $this->pdapp_get($keyword);
        if($data == null)
        {
            return false;
        } else {
            return true;
        }
    }

    function pdapp_search($query)
    {
        if(method_exists($this,"pdapp_driver_search"))
        {
            return $this->pdapp_driver_search($query);
        }

        throw new Exception('Search method is not supported by this driver.');
    }

    function pdapp_increment($keyword, $step = 1 , $option = array())
    {
        $object = $this->pdapp_get($keyword, array('all_keys' => true));
        if($object == null)
        {
            return false;
        } else
        {
            $value = (Int)$object['value'] + (Int)$step;
            $time = $object['expired_time'] - gmdate("U");
            $this->pdapp_set($keyword, $value, $time, $option);
            return true;
        }
    }

    function pdapp_decrement($keyword, $step = 1 , $option = array())
    {
        $object = $this->pdapp_get($keyword, array('all_keys' => true));
        if($object == null)
        {
            return false;
        } else
        {
            $value = (Int)$object['value'] - (Int)$step;
            $time = $object['expired_time'] - gmdate("U");
            $this->pdapp_set($keyword, $value, $time, $option);
            return true;
        }
    }

    /*
     * Extend more time
     */
    function pdapp_touch($keyword, $time = 300, $option = array())
    {
        $object = $this->pdapp_get($keyword, array('all_keys' => true));
        if($object == null)
        {
            return false;
        } else
        {
            $value = $object['value'];
            $time = $object['expired_time'] - gmdate("U") + $time;
            $this->pdapp_set($keyword, $value, $time, $option);
            return true;
        }
    }

    /*
    * Other Functions Built-int for PDAPP_PhpFastCache since 1.3
    */
    public function pdapp_set_multi($list = array())
    {
        foreach($list as $array)
        {
            $this->pdapp_set($array[0], isset($array[1]) ? $array[1] : 0, isset($array[2]) ? $array[2] : array());
        }
    }

    public function pdapp_get_multi($list = array())
    {
        $res = array();
        foreach($list as $array)
        {
            $name = $array[0];
            $res[$name] = $this->pdapp_get($name, isset($array[1]) ? $array[1] : array());
        }

        return $res;
    }

    public function pdapp_get_info_multi($list = array()) {
        $res = array();
        foreach($list as $array) {
            $name = $array[0];
            $res[$name] = $this->pdapp_get_info($name, isset($array[1]) ? $array[1] : array());
        }
        return $res;
    }

    public function pdapp_delete_multi($list = array())
    {
        foreach($list as $array)
        {
            $this->pdapp_delete($array[0], isset($array[1]) ? $array[1] : array());
        }
    }

    public function pdapp_is_existing_multi($list = array())
    {
        $res = array();
        foreach($list as $array)
        {
            $name = $array[0];
            $res[$name] = $this->pdapp_is_existing($name);
        }

        return $res;
    }

    public function pdapp_increment_multi($list = array())
    {
        $res = array();
        foreach($list as $array)
        {
            $name = $array[0];
            $res[$name] = $this->pdapp_increment($name, $array[1], isset($array[2]) ? $array[2] : array());
        }

        return $res;
    }

    public function pdapp_decrement_multi($list = array())
    {
        $res = array();
        foreach($list as $array)
        {
            $name = $array[0];
            $res[$name] = $this->pdapp_decrement($name, $array[1], isset($array[2]) ? $array[2] : array());
        }

        return $res;
    }

    public function pdapp_touch_multi($list = array())
    {
        $res = array();
        foreach($list as $array)
        {
            $name = $array[0];
            $res[$name] = $this->pdapp_touch($name, $array[1], isset($array[2]) ? $array[2] : array());
        }

        return $res;
    }

    public function pdapp_setup($config_name,$value = "")
    {
        /*
         * Config for class
         */
        if(is_array($config_name))
        {
            $this->config = $config_name;
        } else
        {
            $this->config[$config_name] = $value;
        }

    }
    
    /*
     * return PATH for Files & PDO only
     */
    public function pdapp_get_path($create_path = false)
    {
        return PDAPP_PhpFastCache::pdapp_get_path($create_path,$this->config);
    }

    /*
     * Object for Files & SQLite
     */
    protected function pdapp_encode($data)
    {
        return serialize($data);
    }

    protected function pdapp_decode($value)
    {
        $x = @unserialize($value);
        if($x == false)
        {
            return $value;
        } else
        {
            return $x;
        }
    }

    /*
    * Check phpModules or CGI
    */
    protected function pdapp_is_php_module()
    {
       return PDAPP_PhpFastCache::pdapp_is_php_module();
    }

    protected function __pdapp_set_chmod_auto()
    {
        return PDAPP_PhpFastCache::__pdapp_set_chmod_auto($this->config);
    }
}
