Your IP : 216.73.216.216


Current Path : /proc/self/root/proc/self/root/proc/self/root/tmp/
Upload File :
Current File : //proc/self/root/proc/self/root/proc/self/root/tmp/phpJ2T6AE

<?php

namespace Nextend\Framework\Cache;


use Nextend\Framework\Cache\Storage\AbstractStorage;

abstract class AbstractCache {

    protected $group = '';
    protected $isAccessible = false;

    /** @var AbstractStorage */
    public $storage;

    protected $_storageEngine = 'filesystem';

    /**
     * @param string $engine
     *
     * @return AbstractStorage
     */
    public static function getStorage($engine = "filesystem") {
        static $storage = null;
        if ($storage === null) {
            $storage = array(
                'filesystem' => new  Storage\Filesystem(),
                'database'   => new Storage\Database()
            );
        }

        return $storage[$engine];
    }

    public static function clearAll() {
        self::getStorage('filesystem')
            ->clearAll();
        self::getStorage('filesystem')
            ->clearAll('web');
    }

    public static function clearGroup($group) {
        self::getStorage('filesystem')
            ->clear($group);
        self::getStorage('filesystem')
            ->clear($group, 'web');
        self::getStorage('database')
            ->clear($group);
        self::getStorage('database')
            ->clear($group, 'web');
    }

    public function __construct($group, $isAccessible = false) {
        $this->group        = $group;
        $this->isAccessible = $isAccessible;
        $this->storage      = self::getStorage($this->_storageEngine);
    }

    protected function clearCurrentGroup() {
        $this->storage->clear($this->group, $this->getScope());
    }

    protected function getScope() {
        if ($this->isAccessible) {
            return 'web';
        }

        return 'notweb';
    }

    protected function exists($key) {
        return $this->storage->exists($this->group, $key, $this->getScope());
    }

    protected function get($key) {
        return $this->storage->get($this->group, $key, $this->getScope());
    }

    protected function set($key, $value) {
        $this->storage->set($this->group, $key, $value, $this->getScope());
    }

    protected function getPath($key) {
        return $this->storage->getPath($this->group, $key, $this->getScope());
    }

    protected function remove($key) {
        return $this->storage->remove($this->group, $key, $this->getScope());
    }
}<?php

namespace Nextend\Framework\Cache;

use Nextend\Framework\Filesystem\Filesystem;
use Nextend\Framework\Misc\HttpClient;

class CacheGoogleFont extends AbstractCache {

    protected $_storageEngine = 'filesystem';
    private $fontExtension;

    public function __construct() {
        parent::__construct('googlefonts', true);
    }

    /**
     * @param string $url
     *
     * @return boolean|string The path of the cached file
     */
    public function makeCache($url, $extension) {

        $hash = $this->generateHash($url);

        $fileName              = $hash;
        $fileNameWithExtension = $fileName . '.' . $extension;

        $isCached = $this->exists($fileNameWithExtension);

        if ($isCached) {
            if (!$this->testManifestFile($fileName)) {
                $isCached = false;
            }
        }

        if (!$isCached) {

            $cssContent = HttpClient::get($url);

            if (!$cssContent) {
                return false;
            }

            if ($extension === 'css') {
                $fontExtensions = array(
                    'woff2',
                    'ttf'
                );

                foreach ($fontExtensions as $this->fontExtension) {
                    $cssContent = preg_replace_callback('/url\(["\']?(.*?\.' . $this->fontExtension . ')["\']?\)/i', function ($matches) {

                        $url = $matches[1];

                        $cache = new CacheGoogleFont();

                        $path = $cache->makeCache($url, $this->fontExtension);

                        if ($path) {
                            $url = Filesystem::pathToAbsoluteURL($path);
                        }

                        return 'url(' . $url . ')';
                    }, $cssContent);
                }
            }

            $this->set($fileNameWithExtension, $cssContent);

            $this->createManifestFile($fileName);
        }

        return $this->getPath($fileNameWithExtension);
    }

    private function generateHash($url) {
        return md5($url);
    }

    protected function testManifestFile($fileName) {
        $manifestKey = $this->getManifestKey($fileName);
        if ($this->exists($manifestKey)) {

            $manifestData = json_decode($this->get($manifestKey), true);

            if ($manifestData['mtime'] > strtotime('-30 days')) {
                return true;
            }
        }

        return false;
    }

    protected function createManifestFile($fileName) {

        $this->set($this->getManifestKey($fileName), json_encode($this->getManifestData()));
    }

    private function getManifestData() {

        return array(
            'mtime' => time()
        );
    }

    protected function getManifestKey($fileName) {
        return $fileName . '.manifest';
    }
}<?php

namespace Nextend\Framework\Cache;

use DateTime;

class CacheImage extends AbstractCache {

    protected $_storageEngine = 'filesystem';

    protected $lazy = false;

    public function __construct($group) {
        parent::__construct($group, true);
    }

    protected function getScope() {
        return 'image';
    }

    public function setLazy($lazy) {
        $this->lazy = $lazy;
    }

    /**
     * @param          $fileExtension
     * @param callable $callable
     * @param array    $parameters
     * @param bool     $hash
     *
     * @return mixed
     */
    public function makeCache($fileExtension, $callable, $parameters = array(), $hash = false) {

        if (!$hash) {
            $hash = $this->generateHash($fileExtension, $callable, $parameters);
        }

        if (strpos($parameters[1], '?') !== false) {
            $fileNameParts = explode('?', $parameters[1]);
            $keepFileName  = pathinfo($fileNameParts[0], PATHINFO_FILENAME);
        } else {
            $keepFileName = pathinfo($parameters[1], PATHINFO_FILENAME);
        }

        $fileName              = $hash . (!empty($keepFileName) ? '/' . $keepFileName : '');
        $fileNameWithExtension = $fileName . '.' . $fileExtension;

        $isCached = $this->exists($fileNameWithExtension);
        if ($isCached) {
            if (!$this->testManifestFile($fileName, $parameters[1])) {
                $isCached = false;
            }
        }

        if (!$isCached) {
            if ($this->lazy) {
                return $parameters[1];
            }

            array_unshift($parameters, $this->getPath($fileNameWithExtension));
            call_user_func_array($callable, $parameters);

            $this->createManifestFile($fileName, $parameters[2]);
        }

        return $this->getPath($fileNameWithExtension);
    }

    private function generateHash($fileExtension, $callable, $parameters) {
        return md5(json_encode(array(
            $fileExtension,
            $callable,
            $parameters
        )));
    }

    protected function testManifestFile($fileName, $originalFile) {
        $manifestKey = $this->getManifestKey($fileName);
        if ($this->exists($manifestKey)) {

            $manifestData = json_decode($this->get($manifestKey), true);

            $newManifestData = $this->getManifestData($originalFile);
            if ($manifestData['mtime'] == $newManifestData['mtime']) {
                return true;
            }
        } else {
            // Backward compatibility
            $this->createManifestFile($fileName, $originalFile);

            return true;
        }

        return false;
    }

    protected function createManifestFile($fileName, $originalFile) {

        $this->set($this->getManifestKey($fileName), json_encode($this->getManifestData($originalFile)));
    }

    private function getManifestData($originalFile) {
        $manifestData = array();
        if (strpos($originalFile, '//') !== false) {
            $manifestData['mtime'] = $this->getRemoteMTime($originalFile);
        } else {
            $manifestData['mtime'] = filemtime($originalFile);
        }

        return $manifestData;
    }

    private function getRemoteMTime($url) {
        if (ini_get('allow_url_fopen')) {
            $h = get_headers($url, 1);
            if (!$h || strpos($h[0], '200') !== false) {
                foreach ($h as $k => $v) {
                    if (strtolower(trim($k)) == "last-modified") {
                        return (new DateTime($v))->getTimestamp();
                    }
                }
            }
        }

        return 0;
    }

    protected function getManifestKey($fileName) {
        return $fileName . '.manifest';
    }
}