smarty-php / smarty

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
Other
2.24k stars 705 forks source link

Debug function is broken #922

Closed JordyThijsRB-Media closed 8 months ago

JordyThijsRB-Media commented 8 months ago

Hi, Currently the debug function ({debug}) is broken, because the COMPILECHECK_ON and CACHING_OFF constants from the Smarty class are called using the wrong namespace (The backslash should be dropped). The path to debug.tpl is also wrong (the "../" needs to be removed). This is the fixed version of display_debug():

    public function display_debug($obj, $full = false)
    {
        if (!$full) {
            $this->offset++;
            $savedIndex = $this->index;
            $this->index = 9999;
        }
        $smarty = $obj->getSmarty();
        // create fresh instance of smarty for displaying the debug console
        // to avoid problems if the application did overload the Smarty class
        $debObj = new Smarty();
        // copy the working dirs from application
        $debObj->setCompileDir($smarty->getCompileDir());
        $debObj->compile_check = Smarty::COMPILECHECK_ON;
        $debObj->security_policy = null;
        $debObj->debugging = false;
        $debObj->debugging_ctrl = 'NONE';
        $debObj->error_reporting = E_ALL & ~E_NOTICE;
        $debObj->debug_tpl = $smarty->debug_tpl ?? 'file:' . __DIR__ . '/debug.tpl';
        $debObj->registered_resources = array();
        $debObj->escape_html = true;
        $debObj->caching = Smarty::CACHING_OFF;
        // prepare information of assigned variables
        $ptr = $this->get_debug_vars($obj);
        $_assigned_vars = $ptr->tpl_vars;
        ksort($_assigned_vars);
        $_config_vars = $ptr->config_vars;
        ksort($_config_vars);
        $debugging = $smarty->debugging;
        $templateName = $obj->getSource()->type . ':' . $obj->getSource()->name;
        $displayMode = $debugging === 2 || !$full;
        $offset = $this->offset * 50;
        $_template = $debObj->doCreateTemplate($debObj->debug_tpl);
        if ($obj instanceof Template) {
            $_template->assign('template_name', $templateName);
        } elseif ($obj instanceof Smarty || $full) {
            $_template->assign('template_data', $this->template_data[$this->index]);
        } else {
            $_template->assign('template_data', null);
        }
        $_template->assign('assigned_vars', $_assigned_vars);
        $_template->assign('config_vars', $_config_vars);
        $_template->assign('execution_time', microtime(true) - $smarty->start_time);
        $_template->assign('targetWindow', $displayMode ? md5("$offset$templateName") : '__Smarty__');
        $_template->assign('offset', $offset);
        echo $_template->fetch();
        if (isset($full)) {
            $this->index--;
        }
        if (!$full) {
            $this->index = $savedIndex;
        }
    }

The FQN is also wrong in the debug.tpl, but there it's the other way around. {Smarty::SMARTY_VERSION} should be {Smarty\Smarty::SMARTY_VERSION}.

wisskid commented 8 months ago

@JordyThijsRB-Media thanks! Care to make it into Smarty's wall of fame by making a PR to fix this?