1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: function smarty_resource_db_systemTpl($tpl_name)
15: {
16:
17: static $patterns = null;
18: static $replacements = null;
19: if (!$patterns) {
20: $root=&XCube_Root::getSingleton();
21: $systemTemplates = explode(',',$root->getSiteConfig('Legacy_RenderSystem','SystemTemplate',''));
22: $prefix = $root->getSiteConfig('Legacy_RenderSystem','SystemTemplatePrefix','legacy');
23: $patterns = preg_replace('/^\s*([^\s]*)\s*$/e', '"/".preg_quote("\1","/")."/"', $systemTemplates);
24: $replacements = preg_replace('/^\s*system_([^\s]*)\s*/', $prefix.'_\1', $systemTemplates);
25: }
26: if ($patterns) {
27: $tpl_name = preg_replace($patterns, $replacements,$tpl_name);
28: }
29: return $tpl_name;
30: }
31:
32: function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty)
33: {
34: $tpl_name = smarty_resource_db_systemTpl($tpl_name);
35: if (!$tpl = smarty_resource_db_tplinfo($tpl_name, $smarty)) {
36: return false;
37: }
38: if (is_object($tpl)) {
39: $tpl_source = $tpl->getVar('tpl_source', 'n');
40: } else {
41: $tpl_source = file_get_contents($tpl) ;
42: }
43: return true;
44: }
45:
46: function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
47: {
48: $tpl_name = smarty_resource_db_systemTpl($tpl_name);
49: if (!$tpl = smarty_resource_db_tplinfo($tpl_name, $smarty)) {
50: return false;
51: }
52: if (is_object($tpl)) {
53: $tpl_timestamp = $tpl->getVar('tpl_lastmodified', 'n');
54: } else {
55: $tpl_timestamp = filemtime($tpl);
56: }
57: return true;
58: }
59:
60: function smarty_resource_db_secure($tpl_name, &$smarty)
61: {
62:
63: return true;
64: }
65:
66: function smarty_resource_db_trusted($tpl_name, &$smarty)
67: {
68:
69: }
70:
71:
72: function smarty_resource_db_tplinfo($tpl_name, $smarty)
73: {
74: static $cache = array();
75: global $xoopsConfig;
76:
77:
78: if (isset($cache[$tpl_name])) {
79: return $cache[$tpl_name];
80: }
81:
82: $tplset = isset($xoopsConfig['template_set']) ? $xoopsConfig['template_set']: 'default' ;
83: $theme = isset($xoopsConfig['theme_set']) ? $xoopsConfig['theme_set'] : 'default';
84:
85:
86: $filepath = XOOPS_THEME_PATH . '/' . $theme . '/templates/' . $tpl_name ;
87: if (file_exists($filepath)) {
88: return $cache[$tpl_name] = $filepath ;
89: }
90:
91:
92: @list($dirname , $base_tpl_name) = explode('_' , $tpl_name , 2) ;
93: $mytrustdirname = Legacy_ResourcedbUtils::getTrustPath($dirname);
94: if($mytrustdirname && $base_tpl_name) {
95: $filepath = XOOPS_THEME_PATH . '/' . $theme . '/templates/' . $mytrustdirname . '/' . $base_tpl_name ;
96: if (file_exists($filepath)) {
97: return $cache[$tpl_name] = $filepath ;
98: }
99: }
100:
101:
102:
103: $tplfileHandler =& xoops_gethandler('tplfile');
104: $tplObj = $tplfileHandler->find($tplset, null, null, null, $tpl_name, true);
105: if(empty($tplObj)) {
106:
107: $tplObj = $tplfileHandler->find('default', null, null, null, $tpl_name, true);
108: if(empty($tplObj)){
109: return false;
110: }
111:
112: if(XCube_Root::getSingleton()->mContext->mUser->isInRole('Site.Administrator') && $smarty->xoops_canUpdateFromFile()){
113: Legacy_ResourcedbUtils::updateTemplate($tplObj[0]);
114: }
115: }
116: return $cache[$tpl_name] = $tplObj[0];
117: }
118:
119: class Legacy_ResourcedbUtils
120: {
121: public static function getModuleTemplatePath(XoopsTplfile $tplObj)
122: {
123: $block = ($tplObj->getVar('tpl_type')==='block') ? '/blocks' : null;
124: $dirname = $tplObj->getVar('tpl_module');
125: $modulePath = $dirname.'/templates'.$block;
126:
127:
128: $publicPath = XOOPS_MODULE_PATH.'/'.$modulePath.'/'.$tplObj->getVar('tpl_file');
129: if(file_exists($publicPath)){
130: return $publicPath;
131: }
132:
133:
134: if(! $trustDirname = self::getTrustPath($dirname)){
135: return false;
136: }
137: $filename = preg_replace('/'.$dirname.'/', $trustDirname, $tplObj->getVar('tpl_file'), 1);
138:
139:
140: $publicPath = XOOPS_MODULE_PATH.'/'.$modulePath.'/'.$filename;
141: if(file_exists($publicPath)){
142: return $publicPath;
143: }
144:
145: $trustPath = XOOPS_TRUST_PATH.'/modules/'.$trustDirname.'/templates'.$block.'/'.$filename;
146: if(file_exists($trustPath)){
147: return $trustPath;
148: }
149: return false;
150: }
151:
152: public static function getTrustPath( $dirname)
153: {
154: if(file_exists(XOOPS_ROOT_PATH.'/modules/'.$dirname.'/mytrustdirname.php')){
155: @include XOOPS_ROOT_PATH.'/modules/'.$dirname.'/mytrustdirname.php' ;
156: return $mytrustdirname;
157: }
158: else{
159: $root = XCube_Root::getSingleton();
160: $handler = xoops_gethandler('module');
161: $module = $handler->getByDirname($dirname);
162: return ($module && ($trustDirname = $module->get('trust_dirname'))) ? $trustDirname : null;
163: }
164: }
165:
166: public static function updateTemplate($tplObj)
167: {
168: if($filepath = self::getModuleTemplatePath($tplObj)){
169: $file_modified = filemtime($filepath);
170: if ($file_modified > $tplObj->getVar('tpl_lastmodified')) {
171: if (false != $fp = fopen($filepath, 'r')) {
172: $handler = xoops_gethandler('tplfile');
173: $filesource = fread($fp, filesize($filepath));
174: fclose($fp);
175: $tplObj->setVar('tpl_source', $filesource, true);
176: $tplObj->setVar('tpl_lastmodified', time());
177: $tplObj->setVar('tpl_lastimported', time());
178: $handler->forceUpdate($tplObj);
179: }
180: }
181: }
182: }
183: }
184:
185: ?>
186: