Ir para conteúdo
Entre para seguir isso  
Fixthow

[Znoteacc ] ERRO CACHE FIX

Recommended Posts

Fixthow    33
Fixthow

Estava estudando a estrutura do OOP do website znoteacc e verifiquei que é possível visualizar os arquivos de cache pelo browser, até ai tudo bem, mas caso o usuário deseje usar o sistema de cache para algum sistema que contém configurações do banco de dados e deseja indexar as configurações via cache o arquivo poderá ser visto pelo browser, e assim, as configuração dele estariam exposta.

 

É simples, eu corrigi o erro apenas retornando o cache dentro de uma variável e colocando o arquivo com tags PHP e quando é chamado o cache ele lê a variável. Caso o arquivo seja acessado pelo navegador não mostrará resultado algum.

O Code:

<?php
   class Cache    {        protected $_file = false;        protected $_lifespan = 0;        protected $_content;
       const EXT = '.cache.php';

       /**         * @param  string $file         * @access public         * @return void        **/        public function __construct($file) {            $this->_file = $file . self::EXT;            $this->setExpiration(config('cache_lifespan'));        }

       /**         * Sets the cache expiration limit (IMPORTANT NOTE: seconds, NOT ms!).         *         * @param  integer $span         * @access public         * @return void        **/        public function setExpiration($span) {            $this->_lifespan = $span;        }

       /**         * Set the content you'd like to cache.         *         * @param  mixed $content         * @access public         * @return void        **/        public function setContent($content) {            switch (strtolower(gettype($content))) {                case 'array':                    $this->_content = json_encode($content);                    break;
               default:                    $this->_content = $content;                    break;            }        }

       /**         * Validates whether it is time to refresh the cache data or not.         *         * @access public         * @return boolean        **/        public function hasExpired() {            if (is_file($this->_file) && time() < filemtime($this->_file) + $this->_lifespan) {                return false;            }
           return true;        }
       /**         * Returns remaining time before scoreboard will update itself.         *         * @access public         * @return integer        **/        public function remainingTime() {            $remaining = 0;            if (!$this->hasExpired()) {                $remaining = (filemtime($this->_file) + $this->_lifespan) - time();            }            return $remaining;        }

       /**         * Saves the content into its appropriate cache file.         *         * @access public         * @return void        **/        public function save() {            $handle = fopen($this->_file, 'w');            $content = '<?php $cache =\''.$this->_content.'\' ?>';            fwrite($handle, $content);            fclose($handle);        }

       /**         * Loads the content from a specified cache file.         *          * @access public         * @return mixed        **/        public function load() {            if (!is_file($this->_file)) {                return false;            }
           //$content = trim(@file_get_contents($this->_file));            include $this->_file;            

           if (!isset($cache) && strlen($cache) == 0) {                return false;            }
           if ($content = json_decode($cache, true)) {                return (array) $content;            } else {                return $content;            }        }    }

 

Só alterar o code atual do arquivo engine/function/cache.phh por este code.

Recomendo após alterar o arquivo deletar todo conteúdo da pasta cache antes de atualizar o site.

 

Para os mais curiosos:

 

Alterei:

 

public function save() {            $handle = fopen($this->_file, 'w');            fwrite($handle, $this->_content);            fclose($handle);        }

 

Por:

 

public function save() {            $handle = fopen($this->_file, 'w');            $content = '<?php $cache =\''.$this->_content.'\' ?>';            fwrite($handle, $content);            fclose($handle);        }

 

Aqui o arquivo quando for salvo o conteúdo é saldo dentro de uma variável e o arquivo é aberto e fechado com tags php.

 

Antes:

 

public function load() {            if (!is_file($this->_file)) {                return false;            }
           ob_start();            include_once($this->_file);            $content = ob_get_clean();
           if (!isset($content) && strlen($content) == 0) {                return false;            }
           if ($content = json_decode($content, true)) {                return (array) $content;            } else {                return $content;            }        }

 

Por:

 

public function load() {            if (!is_file($this->_file)) {                return false;            }
                       include $this->_file;            

           if (!isset($cache) && strlen($cache) == 0) {                return false;            }
           if ($content = json_decode($cache, true)) {                return (array) $content;            } else {                return $content;            }        }

 

Aqui ele inclui o arquivo e pega o conteúdo da variável.

Compartilhar este post


Link para o post
vHp    6
vHp

A identação ficou um pouco estranha, foi a tag [ code ] que fez isso?

 

Mas, está muito boa a lógica, gratz :)

Compartilhar este post


Link para o post
Fixthow    33
Fixthow
A identação ficou um pouco estranha, foi a tag [ code ] que fez isso?

 

Mas, está muito boa a lógica, gratz :)

 

Sim, foi o

 que quebro...

Compartilhar este post


Link para o post
Visitante
Este tópico está impedido de receber novos posts.
Entre para seguir isso  
  • Quem Está Navegando   0 membros estão online

    Nenhum usuário registrado visualizando esta página.

×