From 30a45ee78ef97783bbf4aafdaad31d641d02b534 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Tue, 29 Oct 2013 15:13:07 -0400 Subject: [PATCH] prevent a potentially badly written theme.json from blowing up your forum --- public/src/templates.js | 49 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/public/src/templates.js b/public/src/templates.js index 3329d2782b..2adb4553de 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -61,26 +61,37 @@ function loadServer() { var loaded = templatesToLoad.length; - for (var t in templatesToLoad) { - (function (file) { - fs.readFile((customTemplateDir ? customTemplateDir : __dirname + '/../templates') + '/' + file + '.tpl', function (err, html) { - var template = function () { - this.toString = function () { - return this.html; - }; - } - - template.prototype.file = file; - template.prototype.parse = parse; - template.prototype.html = String(html); - - global.templates[file] = new template; - - loaded--; - if (loaded == 0) templates.ready(); - }); - }(templatesToLoad[t])); + function getTemplates(directory) { + for (var t in templatesToLoad) { + (function (file) { + fs.readFile(directory + '/' + file + '.tpl', function (err, html) { + var template = function () { + this.toString = function () { + return this.html; + }; + } + + template.prototype.file = file; + template.prototype.parse = parse; + template.prototype.html = String(html); + + global.templates[file] = new template; + + loaded--; + if (loaded == 0) templates.ready(); + }); + }(templatesToLoad[t])); + } + } + if (customTemplateDir) { + fs.exists(customTemplateDir, function (exists) { + var directory = (exists ? customTemplateDir : __dirname + '/../templates'); + getTemplates(customTemplateDir); + }); + } else { + getTemplates(__dirname + '/../templates'); } + } function loadClient() {