1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: /**
13: * Smarty plugin
14: *
15: * Fetches templates from a database
16: *
17: * @copyright 2008-2022 XOOPS Project (http://xoops.org)
18: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
19: */
20: class Smarty_Resource_Db extends Smarty_Resource_Custom
21: {
22:
23: /**
24: * Fetch a template and its modification time from database
25: *
26: * @param string $name template name
27: * @param string $source template source
28: * @param int $mtime template modification timestamp (epoch)
29: *
30: * @return void
31: */
32: protected function fetch($name, &$source, &$mtime)
33: {
34: $tpl = $this->dbTplInfo($name);
35: if (is_object($tpl)) {
36: /** @var XoopsTplFile $tpl */
37: $source = $tpl->getVar('tpl_source', 'n');
38: $mtime = $tpl->getVar('tpl_lastmodified', 'n');
39: } else {
40: $stat = stat($tpl);
41:
42: // Did we fail to get stat information?
43: if ($stat) {
44: $mtime = $stat['mtime'];
45: $filesize = $stat['size'];
46: $fp = fopen($tpl, 'r');
47: $source = ($filesize > 0) ? fread($fp, $filesize) : '';
48: fclose($fp);
49: } else {
50: $source = null;
51: $mtime = null;
52: }
53: }
54: }
55:
56: /**
57: * Get template info from db, or file name if available
58: *
59: * @param string $tpl_name template name
60: *
61: * @return XoopsTplFile|string tpl object from database or absolute file name path
62: */
63: private function dbTplInfo($tpl_name)
64: {
65: static $cache = array();
66: global $xoopsConfig;
67: // $xoops = Xoops::getInstance();
68:
69: if (isset($cache[$tpl_name])) {
70: return $cache[$tpl_name];
71: }
72: $tplset = $xoopsConfig['template_set'];
73: $theme = isset($xoopsConfig['theme_set']) ? $xoopsConfig['theme_set'] : 'default';
74: $tplfile_handler = xoops_getHandler('tplfile'); // $xoops->getHandlerTplFile();
75: // If we're not using the "default" template set, then get the templates from the DB
76: if ($tplset !== "default") {
77: $tplobj = $tplfile_handler->find($tplset, null, null, null, $tpl_name, true);
78: if (count($tplobj)) {
79: return $cache[$tpl_name] = $tplobj[0];
80: }
81: }
82: // If we'using the default tplset, get the template from the filesystem
83: $tplobj = $tplfile_handler->find("default", null, null, null, $tpl_name, true);
84:
85: if (!count($tplobj)) {
86: return $cache[$tpl_name] = $tpl_name;
87: }
88: /** @var XoopsTplFile $tplobj */
89: $tplobj = $tplobj[0];
90: $module = $tplobj->getVar('tpl_module', 'n');
91: $type = $tplobj->getVar('tpl_type', 'n');
92: // Construct template path
93: switch ($type) {
94: case 'block':
95: $directory = XOOPS_ROOT_PATH . '/themes'; //\XoopsBaseConfig::get('themes-path');
96: $path = 'blocks/';
97: break;
98: case 'admin':
99: $theme = isset($xoopsConfig['cpanel']) ? $xoopsConfig['cpanel'] : 'default';
100: $directory = XOOPS_ROOT_PATH . '/modules/system/themes'; //\XoopsBaseConfig::get('adminthemes-path');
101: $path = 'admin/';
102: break;
103: default:
104: $directory = XOOPS_ROOT_PATH . '/themes'; //\XoopsBaseConfig::get('themes-path');
105: $path = '';
106: if (class_exists('XoopsSystemCpanel', false)) {
107: $directory = XOOPS_ADMINTHEME_PATH;
108: $theme = isset($xoopsConfig['cpanel']) ? $xoopsConfig['cpanel'] : 'default';
109: }
110: break;
111: }
112: // First, check for an overloaded version within the theme folder
113: $filepath = $directory . "/{$theme}/modules/{$module}/{$path}{$tpl_name}";
114: if (!file_exists($filepath)) {
115: // If no custom version exists, get the tpl from its default location
116: $filepath = XOOPS_ROOT_PATH . "/modules/{$module}/templates/{$path}{$tpl_name}";
117: if (!file_exists($filepath)) {
118: return $cache[$tpl_name] = $tplobj ;
119: }
120: }
121: return $cache[$tpl_name] = $filepath;
122: }
123: }
124: