@@ -190,21 +190,8 @@ private function getLanguageList() {
190190 */
191191 private function includeAtTemplateBase ($ file ) {
192192 $ data = $ this ->data ;
193- $ filename = $ this ->configuration ->getPathValue ('templatedir ' ) . $ this ->configuration ->getValue ('theme.use ' ) . '/ ' . $ file ;
194193
195- if (!file_exists ($ filename )) {
196-
197- SimpleSAML_Logger::error ($ _SERVER ['PHP_SELF ' ].' - Template: Could not find template file [ ' . $ file .
198- '] at [ ' . $ filename . '] - Now trying at base ' );
199-
200- $ filename = $ this ->configuration ->getPathValue ('templatedir ' ) . $ this ->configuration ->getValue ('theme.base ' ) . '/ ' . $ file ;
201- if (!file_exists ($ filename )) {
202- SimpleSAML_Logger::error ($ _SERVER ['PHP_SELF ' ].' - Template: Could not find template file [ ' . $ file .
203- '] at [ ' . $ filename . '] ' );
204- throw new Exception ('Could not load template file [ ' . $ file . '] ' );
205- }
206-
207- }
194+ $ filename = $ this ->findTemplatePath ($ file );
208195
209196 include ($ filename );
210197 }
@@ -223,8 +210,16 @@ private function getDictionary($name) {
223210 assert ('is_string($name) ' );
224211
225212 if (!array_key_exists ($ name , $ this ->dictionaries )) {
226- $ dictDir = $ this ->configuration ->getPathValue ('dictionarydir ' );
227- $ this ->dictionaries [$ name ] = $ this ->readDictionaryFile ($ dictDir . $ name . '.php ' );
213+ $ sepPos = strpos ($ name , ': ' );
214+ if ($ sepPos !== FALSE ) {
215+ $ module = substr ($ name , 0 , $ sepPos );
216+ $ fileName = substr ($ name , $ sepPos + 1 );
217+ $ dictDir = SimpleSAML_Module::getModuleDir ($ module ) . '/dictionaries/ ' ;
218+ } else {
219+ $ dictDir = $ this ->configuration ->getPathValue ('dictionarydir ' );
220+ $ fileName = $ name ;
221+ }
222+ $ this ->dictionaries [$ name ] = $ this ->readDictionaryFile ($ dictDir . $ fileName . '.php ' );
228223 }
229224
230225 return $ this ->dictionaries [$ name ];
@@ -249,7 +244,7 @@ public function getTag($tag) {
249244 }
250245
251246 /* Check whether we should use the default dictionary or a dictionary specified in the tag. */
252- if (substr ($ tag , 0 , 1 ) === '{ ' && preg_match ('/^{(\w+?):(.*)}$/ ' , $ tag , $ matches )) {
247+ if (substr ($ tag , 0 , 1 ) === '{ ' && preg_match ('/^{((?:\w+:)? \w+?):(.*)}$/ ' , $ tag , $ matches )) {
253248 $ dictionary = $ matches [1 ];
254249 $ tag = $ matches [2 ];
255250 } else {
@@ -460,31 +455,99 @@ private function readDictionaryFile($filename) {
460455 * Show the template to the user.
461456 */
462457 public function show () {
463-
464- $ filename = $ this ->configuration ->getPathValue ('templatedir ' ) .
465- $ this ->configuration ->getValue ('theme.use ' ) . '/ ' . $ this ->template ;
466-
467458
468- if (!file_exists ($ filename )) {
469- SimpleSAML_Logger::warning ($ _SERVER ['PHP_SELF ' ].' - Template: Could not find template file [ ' . $ this ->template . '] at [ ' . $ filename . '] - now trying the base template ' );
470-
471-
472- $ filename = $ this ->configuration ->getPathValue ('templatedir ' ) .
473- $ this ->configuration ->getValue ('theme.base ' ) . '/ ' . $ this ->template ;
474-
475-
476- if (!file_exists ($ filename )) {
477- SimpleSAML_Logger::critical ($ _SERVER ['PHP_SELF ' ].' - Template: Could not find template file [ ' . $ this ->template . '] at [ ' . $ filename . '] ' );
478-
479- echo 'Fatal error: Could not find template file [ ' . $ this ->template . '] at [ ' . $ filename . '] ' ;
480- exit (0 );
481- }
482- }
483-
459+ $ filename = $ this ->findTemplatePath ($ this ->template );
484460 require_once ($ filename );
485461 }
486-
487-
462+
463+
464+ /**
465+ * Find template path.
466+ *
467+ * This function locates the given template based on the template name.
468+ * It will first search for the template in the current theme directory, and
469+ * then the default theme.
470+ *
471+ * The template name may be on the form <module name>:<template path>, in which case
472+ * it will search for the template file in the given module.
473+ *
474+ * An error will be thrown if the template file couldn't be found.
475+ *
476+ * @param string $template The relative path from the theme directory to the template file.
477+ * @return string The absolute path to the template file.
478+ */
479+ private function findTemplatePath ($ template ) {
480+ assert ('is_string($template) ' );
481+
482+ $ tmp = explode (': ' , $ template , 2 );
483+ if (count ($ tmp ) === 2 ) {
484+ $ templateModule = $ tmp [0 ];
485+ $ templateName = $ tmp [1 ];
486+ } else {
487+ $ templateModule = 'default ' ;
488+ $ templateName = $ tmp [0 ];
489+ }
490+
491+ $ tmp = explode (': ' , $ this ->configuration ->getValue ('theme.use ' ), 2 );
492+ if (count ($ tmp ) === 2 ) {
493+ $ themeModule = $ tmp [0 ];
494+ $ themeName = $ tmp [1 ];
495+ } else {
496+ $ themeModule = NULL ;
497+ $ themeName = $ tmp [0 ];
498+ }
499+
500+
501+ /* First check the current theme. */
502+ if ($ themeModule !== NULL ) {
503+ /* .../module/<themeModule>/themes/<themeName>/<templateModule>/<templateName> */
504+
505+ $ filename = SimpleSAML_Module::getModuleDir ($ themeModule ) . '/themes/ ' . $ themeName . '/ ' .
506+ $ templateModule . '/ ' . $ templateName ;
507+ } elseif ($ templateModule !== 'default ' ) {
508+ /* .../module/<templateModule>/templates/<themeName>/<templateName> */
509+ $ filename = SimpleSAML_Module::getModuleDir ($ templateModule ) . '/templates/ ' .
510+ $ themeName . '/ ' . $ templateName ;
511+ } else {
512+ /* .../templates/<theme>/<templateName> */
513+ $ filename = $ this ->configuration ->getPathValue ('templatedir ' ) . $ themeName . '/ ' .
514+ $ templateName ;
515+ }
516+
517+ if (file_exists ($ filename )) {
518+ return $ filename ;
519+ }
520+
521+
522+ /* Not found in current theme. */
523+ SimpleSAML_Logger::info ($ _SERVER ['PHP_SELF ' ].' - Template: Could not find template file [ ' .
524+ $ template . '] at [ ' . $ filename . '] - now trying the base template ' );
525+
526+
527+ /* Try default theme. */
528+ $ baseTheme = $ this ->configuration ->getValue ('theme.base ' );
529+ if ($ templateModule !== 'default ' ) {
530+ /* .../module/<templateModule>/templates/<baseTheme>/<templateName> */
531+ $ filename = SimpleSAML_Module::getModuleDir ($ templateModule ) . '/templates/ ' .
532+ $ baseTheme . '/ ' . $ templateName ;
533+ } else {
534+ /* .../templates/<baseTheme>/<templateName> */
535+ $ filename = $ this ->configuration ->getPathValue ('templatedir ' ) . $ baseTheme . '/ ' .
536+ $ templateName ;
537+ }
538+
539+ if (file_exists ($ filename )) {
540+ return $ filename ;
541+ }
542+
543+
544+ /* Not found in default template - log error and throw exception. */
545+ $ error = 'Template: Could not find template file [ ' . $ template . '] at [ ' . $ filename . '] ' ;
546+ SimpleSAML_Logger::critical ($ _SERVER ['PHP_SELF ' ] . ' - ' . $ error );
547+
548+ throw new Exception ($ error );
549+ }
550+
488551}
489552
490553?>
0 commit comments