Response, if including the Template fails

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!
Post Reply
cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Response, if including the Template fails

Post by cmb » Sat Sep 01, 2012 3:34 pm

Hello Community,

the template is included in cmsimple/cms.php in line 446. If that fails for whatever reason, an appropriate message is echo'd by:

Code: Select all

echo '<html><head><title>' . $tx['heading']['error'] . '</title></head><body>Template nicht gefunden.(' . $pth['file']['template'] . ')</body></html>';
Please note the German message! I consider this a bug, as the default language of CMSimple_XH is English, so this should be at least hardcoded as English message for XH 1.5.4.

But I suggest the following instead:

Code: Select all

    header('HTTP/1.0 500 Internal Server Error');
    header('Content-Type: text/plain; charset=utf-8');
    echo $tx['error']['missing'], ' ', $tx['filetype']['template'], "\n", $pth['file']['template'];
    exit; 
This sends the appropriate HTTP response headers, makes use of already available internationalization, and exits afterwards, as it's not meaningful to proceed, if the template can't be included.

Christoph
Christoph M. Becker – Plugins for CMSimple_XH

svasti
Posts: 1660
Joined: Wed Dec 17, 2008 5:08 pm

Re: Response, if including the Template fails

Post by svasti » Sat Sep 01, 2012 4:12 pm

Interesting, of course it should be done like this... still some work for the coming version.
svasti

Gert
Posts: 3078
Joined: Fri May 30, 2008 4:53 pm
Location: Berlin
Contact:

Re: Response, if including the Template fails

Post by Gert » Sun Sep 02, 2012 4:32 pm

Hello,

I have added a fallback template to solve that problem in the next version of CMSimpleCoAuthors. So you can login and activate an existing template.

cms.php:

Code: Select all

// for subsite solution - GE 20011-02

if($txc['subsite']['template']=="")
{
    $pth['folder']['template'] = $pth['folder']['templates'].$cf['site']['template'].'/';
}
else
{
    $pth['folder']['template'] = $pth['folder']['templates'].$txc['subsite']['template'].'/';
}

$pth['file']['template'] = $pth['folder']['template'].'template.htm';
$pth['file']['stylesheet'] = $pth['folder']['template'].'stylesheet.css';
$pth['folder']['menubuttons'] = $pth['folder']['template'].'menu/';
$pth['folder']['templateimages'] = $pth['folder']['template'].'images/';

// END for subsite solution - GE 20011-02

// template fallback (the new code)

if (!file_exists($pth['file']['template'])) 
{
    $pth['folder']['template'] = $pth['folder']['templates'].'__fallback__/';
    $pth['file']['template'] = $pth['folder']['template'].'template.htm';
    $pth['file']['stylesheet'] = $pth['folder']['template'].'stylesheet.css';
    $pth['folder']['menubuttons'] = $pth['folder']['template'].'menu/';
    $pth['folder']['templateimages'] = $pth['folder']['template'].'images/';
}

// END template fallback           
For that nobody can activate the fallback template (__fallback__ ), I have added to adm.php:

Code: Select all

    foreach ($options as $option => $selected) // search for that
    {
        if($option != '__fallback__' && $option != '__maintenance__')
        {
            $o .= '<option value="' . $option . '"';
            if ($selected)
            {
                $o .= ' selected="selected"';
            }
        }
        $o .= '>' . $option . '</option>';
    } 
The __mainenance__ template you can neglect, it is a special thing of CMSimpleCoAuthors. The __fallback__ template is a copy of the default template.

Because I do not have a "open development", and I will also adopt some of the modifications of CMSimple_XH 1.5.4 to the next version of CMSimpleCoAuthors, I have posted the Code here ;)

Tests are not finished, but it seems to work without problems,

Gert
Gert Ebersbach | CMSimple | Templates - Plugins - Services

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Response, if including the Template fails

Post by cmb » Sun Sep 02, 2012 5:05 pm

Hi Gert,
Gert wrote:Because I do not have a "open development", and I will also adopt some of the modifications of CMSimple_XH 1.5.4 to the next version of CMSimpleCoAuthors, I have posted the Code here ;)
Thank you! :)

Indeed an interesting idea! This is a useful protection against a user switching to an non existing template. But there remains a problem, if the template exists, but can't be read (is_readable() instead of file_exists() might be better, but still wouldn't cater for some extraordinary circumstances, such as an corrupted template).

As indeed in this rare case only a fallback template is necessary, what about hardcoding an absolute minimum to the core? Say:

Code: Select all

<!DOCTYPE html>
<html>
<head><?php echo head();?></head>
<body <?php echo onload();?>><?php echo toc(), content();?></body>
</html>
This could be stored in a constant, and be evaluated, if the selected template can't be included, and it should be enough to operate the system.

Christoph
Christoph M. Becker – Plugins for CMSimple_XH

Gert
Posts: 3078
Joined: Fri May 30, 2008 4:53 pm
Location: Berlin
Contact:

Re: Response, if including the Template fails

Post by Gert » Sun Sep 02, 2012 6:07 pm

Hi Christoph,

you can use the code or only the idea of a fallback template, however you want ;)

In reality nobody can switch per backend to a not existing template. In the most cases it happens, if somebody deletes or renames an activated template.
cmb wrote:As indeed in this rare case only a fallback template is necessary, what about hardcoding an absolute minimum to the core?
Yes, it's possible, for example:

Code: Select all

if (!include($pth['file']['template'])) {
echo '<!DOCTYPE html>
<html>
<head>';
echo head();
echo '</head>';
echo '<body>
<div style="width: 480px; margin: 0 auto;">';
echo '<div style="background: #eee; color: #c00; font-size: 20px; letter-spacing: 2px; text-align: center; font-weight: 700; border: 5px solid #c00; padding: 24px; margin: 20px 0;">
<p><b>!!! Template is missing !!!</b></p>' 
. loginlink() . '</div>';
echo content();
echo '</div>
</body>
</html>';
} 
But the effect, if it happens online, is better with a complete fallback template, like a copy of the default template. And I do not believe, that anybody (normal user) deletes or modifies a template __fallback__ in top of the list,

Gert

PS: The code above maybe the absolutely fallback, if anybody deletes the __fallback__ template.
Gert Ebersbach | CMSimple | Templates - Plugins - Services

Post Reply