Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Do not store email templates in database. [Bug #54505]
To explain what this is about, first a short phpBB code history lesson: ;-)

r9823 originally introduced the usage of our template class for emails. The
messenger class uses set_custom_template() to initialise the template object
which neither disables storedb nor inheritance. These two values are set in
$user->theme rather than inside a particular template instance (quite a design
failure if I may add). Thus the html page that is displayed to the user also
determines these settings for the email templates. This obviously causes
problems because both emails and other custom templates can quite simply not
be stored in the database because the db table only stores the filename, not
the path and requires a template id.

r9839 then generally disabled storedb and template inheritance for custom
templates to fix Bug #40515. This works for custom templates, but not for
emails where lots of template objects are created. In such a situation the
last call to set(_custom)_template() would now determine the values of storedb
and inheritance in _tpl_load. So any page sending emails would neither load
its template from the database nor use template inheritance. The same revision
also introduced orig_tpl_* variables in set_template() which on their own are
very much pointless, but could allow resetting the storedb and inheritance
values if they were used to reset $user->theme just before template execution
in _tpl_load.

In r10150 these orig_tpl_* variables are correctly used to access information
about the template of the page being displayed - contrary to the last template
used - from within the bbcode, fixing Bug #51285. However r10150 also introduces
a pointless $template_mode parameter for set_custom_template(). $template_mode
is really just a boolean flag (value you can be 'template' or an arbitrary other
value) that if it set circumvents the unsetting of storedb and template
inheritance. The very code that had been added to prevent issues with emails and
custom templates. Fixing the problem introduced by r8839 but at the same time
reintroducing the much greater problem from the original implementation of email
templates.

And now an explanation of what I did:

Based on this I have now changed the set_custom_template method to always
disable storedb. It can now properly use inheritance, you simply tell it the
path where the parent template can be found, by default the path is false which
will turn inheritance off. To make this work the template class now always
overwrites $user->theme storedb and inheritance variabbles with orig_tpl_* just
before rendering a template in _tpl_load. This way they are guaranteed to always
contain the value they had at the time set_template/set_custom_template were
called. This fixes [Bug #54505].

In summary, using global state is simply a horrible idea in object oriented
programming. Always Pass values, that an object depends on, as parameters - never
through magic global variables. Following this principle will safe you from a lot
of headaches.

Please test this patch as much as possible to make sure templates still work
properly for you, focus on multiple languages, missing language files, and
custom templates in systems that make use of the template class outside of
phpBB itself.


git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@10460 89ea8834-ac86-4346-8a33-228a782c2dd0
  • Loading branch information
naderman committed Jan 26, 2010
1 parent 81e62b4 commit 6e31ce8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions phpBB/docs/CHANGELOG.html
Expand Up @@ -151,6 +151,7 @@ <h1>Changelog</h1>
<li>[Fix] Database updater now separates ADD COLUMN from SET NOT NULL and SET DEFAULT, when using PostgreSQL &lt;= 7.4 (Bug #54435)</li>
<li>[Fix] Styles adjustment to correctly display an order of rtl/ltr mixed content. (Bugs #55485, #55545)</li>
<li>[Fix] Fix language string for PM-Reports refering to post-data. (Bug #54745)</li>
<li>[Fix] Do not store email templates in database. (Bug #54505)</li>
<li>[Change] Move redirect into a hidden field to avoid issues with mod_security. (Bug #54145)</li>
<li>[Change] Log activation through inactive users ACP. (Bug #30145)</li>
<li>[Change] Send time of last item instead of current time in ATOM Feeds. (Bug #53305)</li>
Expand Down
23 changes: 12 additions & 11 deletions phpBB/includes/functions_messenger.php
Expand Up @@ -182,16 +182,7 @@ function template($template_file, $template_lang = '', $template_path = '')
trigger_error('No template file for emailing set.', E_USER_ERROR);
}

if (!$template_path)
{
$path_check = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
}
else
{
$path_check = $template_path;
}

if (!trim($template_lang) || !file_exists("$path_check$template_lang/email/$template_file.txt"))
if (!trim($template_lang))
{
// fall back to board default language if the user's language is
// missing $template_file. If this does not exist either,
Expand All @@ -205,13 +196,23 @@ function template($template_file, $template_lang = '', $template_path = '')
$this->tpl_msg[$template_lang . $template_file] = new template();
$tpl = &$this->tpl_msg[$template_lang . $template_file];

$fallback_template_path = false;

if (!$template_path)
{
$template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
$template_path .= $template_lang . '/email';

// we can only specify default language fallback when the path is not a custom one for which we
// do not know the default language alternative
if ($template_lang !== basename($config['default_lang']))
{
$fallback_template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
$fallback_template_path .= basename($config['default_lang']) . '/email';
}
}

$tpl->set_custom_template($template_path, $template_lang . '_email', 'email');
$tpl->set_custom_template($template_path, $template_lang . '_email', $fallback_template_path);

$tpl->set_filenames(array(
'body' => $template_file . '.txt',
Expand Down
28 changes: 23 additions & 5 deletions phpBB/includes/template.php
Expand Up @@ -90,7 +90,7 @@ function set_template()
* Set custom template location (able to use directory outside of phpBB)
* @access public
*/
function set_custom_template($template_path, $template_name, $template_mode = 'template')
function set_custom_template($template_path, $template_name, $fallback_template_path = false)
{
global $phpbb_root_path, $user;

Expand All @@ -103,13 +103,25 @@ function set_custom_template($template_path, $template_name, $template_mode = 't
$this->root = $template_path;
$this->cachepath = $phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_';

// As the template-engine is used for more than the template (emails, etc.), we should not set $user->theme in all cases, but only on the real template.
if ($template_mode == 'template')
if ($fallback_template_path !== false)
{
$user->theme['template_storedb'] = false;
$user->theme['template_inherits_id'] = false;
if (substr($fallback_template_path, -1) == '/')
{
$fallback_template_path = substr($fallback_template_path, 0, -1);
}

$this->inherit_root = $fallback_template_path;
$this->orig_tpl_inherits_id = true;
}
else
{
$this->orig_tpl_inherits_id = false;
}

// the database does not store the path or name of a custom template
// so there is no way we can properly store custom templates there
$this->orig_tpl_storedb = false;

$this->_rootref = &$this->_tpldata['.'][0];

return true;
Expand Down Expand Up @@ -254,6 +266,12 @@ function _tpl_load(&$handle)
trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
}

// reload these settings to have the values they had when this object was initialised
// using set_template or set_custom_template, they might otherwise have been overwritten
// by other template class instances in between.
$user->theme['template_storedb'] = $this->orig_tpl_storedb;
$user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id;

$filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx;
$this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0;

Expand Down
6 changes: 6 additions & 0 deletions phpBB/install/database_update.php
Expand Up @@ -1585,6 +1585,12 @@ function change_database_data(&$no_updates, $version)
set_config('feed_topics_new', (!empty($config['feed_overall_topics']) ? '1' : '0'));
set_config('feed_topics_active', (!empty($config['feed_overall_topics']) ? '1' : '0'));

// Delete all text-templates from the template_data
$sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . '
WHERE template_filename ' . $db->sql_like_expression($db->any_char . '.txt');
_sql($sql, $errored, $error_ary);

$no_updates = false;
break;
}
}
Expand Down

0 comments on commit 6e31ce8

Please sign in to comment.