<?php
/*
 * khoaofgod@gmail.com
 * Website: http://www.phpfastcache.com
 * Example at our website, any bugs, problems, please visit http://faster.phpfastcache.com
 * Edited by Product Designer App
 */

if (!defined('ABSPATH'))
{
    exit;
}

require_once(dirname(__FILE__)."/abstract.php");
require_once(dirname(__FILE__)."/driver.php");

if(!function_exists("pdapp_php_fast_cache"))
{
	function pdapp_php_fast_cache($storage = "auto", $config = array())
    {
        $storage = strtolower($storage);
        if(empty($config))
        {
            $config = PDAPP_PhpFastCache::$config;
        }

        if($storage == "" || $storage == "auto")
        {
            $storage = PDAPP_PhpFastCache::pdapp_get_auto_class($config);
        }

        $instance = md5(pdapp_json_encode($config).$storage);
		if(!isset(PDAPP_PhpFastCache_instances::$instances[$instance]))
        {
            $class = "PDAPP_phpfastcache_".$storage;
            PDAPP_PhpFastCache::pdapp_required($storage);
			PDAPP_PhpFastCache_instances::$instances[$instance] = new $class($config);
		}

		return PDAPP_PhpFastCache_instances::$instances[$instance];
	}
}

class PDAPP_PhpFastCache_instances
{
	public static $instances = array();
}

class PDAPP_PhpFastCache
{
    public static $disabled = false;
	public static $config = array(
        "storage"       =>  "",
        "default_chmod" =>  0777,
		"fallback"  => "files",
		"securityKey"   =>  "auto",
		"htaccess"      => true,
		"path"      =>  ""
	);

    protected static $tmp = array();
    var $instance;

    function __construct($storage = "", $config = array())
    {
        if(empty($config))
        {
            $config = PDAPP_PhpFastCache::$config;
        }
        $config['storage'] = $storage;

        $storage = strtolower($storage);
        if($storage == "" || $storage == "auto")
        {
            $storage = self::pdapp_get_auto_class($config);
        }

        $this->instance = pdapp_php_fast_cache($storage,$config);
    }
    
    /*
     * Cores
     */
    public static function pdapp_get_auto_class($config)
    {
        return "files";
    }

    public static function pdapp_get_path($skip_create_path = false, $config = array())
    {
        if ($config['path'] == '')
        {
            if(self::pdapp_is_php_module())
            {
                $tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
                $path = $tmp_dir;
            }else
            {
                $path = isset($_SERVER['DOCUMENT_ROOT']) ? rtrim(sanitize_text_field(wp_unslash($_SERVER['DOCUMENT_ROOT'])),"/")."/../" : rtrim(dirname(__FILE__),"/")."/";
            }

            if(self::$config['path'] != "")
            {
                $path = $config['path'];
            }

        }else
        {
            $path = $config['path'];
        }

        $securityKey = $config['securityKey'];
        if($securityKey == "" || $securityKey == "auto")
        {
            $securityKey = self::$config['securityKey'];
            if($securityKey == "auto" || $securityKey == "")
            {
                $securityKey = isset($_SERVER['HTTP_HOST']) ? ltrim(strtolower(sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST']))),"www.") : "default";
                $securityKey = preg_replace("/[^a-zA-Z0-9]+/","",$securityKey);
            }
        }
        if($securityKey != "")
        {
            $securityKey.= "/";
        }

        $full_path = $path."/".$securityKey;
        $full_pathx = md5($full_path);

        global $wp_filesystem;
        if($skip_create_path  == false && !isset(self::$tmp[$full_pathx]))
        {
            if(!file_exists($full_path) || !$wp_filesystem->is_writable($full_path))
            {
                if(!file_exists($full_path))
                {
                    wp_mkdir_p($full_path);
                }
                if(!$wp_filesystem->is_writable($full_path))
                {
                    $wp_filesystem->chmod($full_path,self::pdapp_set_chmod_auto($config));
                }
                if(!file_exists($full_path) || !$wp_filesystem->is_writable($full_path))
                {
                    die(esc_html("Sorry, Please create ".$full_path." and SET Mode 0777 or any Writable Permission!"));
                }
            }
            
            self::$tmp[$full_pathx] = true;
        }

        return $full_path;
    }

    public static function pdapp_set_chmod_auto($config)
    {
        if($config['default_chmod'] == "" || is_null($config['default_chmod']))
        {
            return 0777;
        }else
        {
            return $config['default_chmod'];
        }
    }

    public static function pdapp_is_php_module()
    {
        if(PHP_SAPI == "apache2handler")
        {
            return true;
        }else
        {
            if(strpos(PHP_SAPI,"handler") !== false)
            {
                return true;
            }
        }
        return false;
    }

    public static function pdapp_setup($name,$value = "")
    {
        if(is_array($name))
        {
            self::$config = $name;
        }else
        {
            self::$config[$name] = $value;
        }
    }

    public static function pdapp_required($class)
    {
        require_once(dirname(__FILE__)."/drivers/".$class.".php");
    }
}