<?php
/**
 * Page Cache powered by Jpresta (jpresta . com)
 *
 *    @author    Jpresta
 *    @copyright Jpresta
 *    @license   You are just allowed to modify this copy for your own use. You must not redistribute it. License
 *               is permitted for one Prestashop instance only but you can install it on your test instances.
 */

if (!defined('_PS_VERSION_'))
    exit;

include_once(dirname(__FILE__) . '/../../pagecache.php');

class pagecachePcspeedanalysisModuleFrontController extends ModuleFrontController
{
    public $php_self = null;

    public function init()
    {
        parent::init();
    }

    public function initContent()
    {
        parent::initContent();

        if (Tools::version_compare(_PS_VERSION_,'1.7','<')) {
            $this->setTemplate('clearcache.tpl');
        }
        else {
            $this->setTemplate('module:pagecache/views/templates/front/clearcache.tpl');
        }

        $result = 'Not OK';
        if (Module::isEnabled("pagecache")) {
            $token = Tools::getValue('token', '');
            $url = Tools::getValue('url', '');
            $goodToken = Configuration::get('pagecache_cron_token');
            if ($goodToken && strcmp($goodToken, $token) === 0 && Tools::strlen($url) > 0) {
                $page = Tools::file_get_contents($url);
                if ($page !== false) {
                    $result = 'OK';
                }
                else {
                    header("HTTP/1.0 404 Not Found");
                    $result = 'Not OK: cannot read URL (SSL certificate problem?)';
                }
            } else {
                header("HTTP/1.0 404 Not Found");
                $result = 'Not OK: bad token ' . $token;
            }
        } else {
            $result = 'Not OK: module not active';
        }
        $this->context->smarty->assign(array(
            'result' => $result
        ));
    }

    public function getLayout()
    {
        return _PS_MODULE_DIR_ . 'pagecache/views/templates/front/layout.tpl';
    }

}
