/
OcSmarty.class.php
443 lines (366 loc) · 12.4 KB
/
OcSmarty.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/***************************************************************************
* For license information see doc/license.txt
*
* Unicode Reminder メモ
*
* Inherit Smarty-Class and extend it
***************************************************************************/
require_once($opt['rootpath'] . 'lib2/smarty/Smarty.class.php');
require_once($opt['rootpath'] . 'lib2/db.inc.php');
require_once($opt['rootpath'] . 'lib2/logic/labels.inc.php');
class OcSmarty extends Smarty
{
var $name = 'sys_nothing';
var $main_template = 'sys_main';
var $bench = null;
var $compile_id = null;
var $cache_id = null;
var $title = '';
var $menuitem = null;
var $nowpsearch = false;
// no header, menu or footer
var $popup = false;
// show a thin border when using popup
// disable popupmargin to appear fullscreen
var $popupmargin = true;
// url to call if login is required
var $target = '';
var $header_javascript = array();
var $body_load = array();
var $body_unload = array();
function OcSmarty()
{
global $opt, $sqldebugger;
require_once($opt['rootpath'] . 'lib2/bench.inc.php');
$this->bench = new CBench();
$this->bench->start();
// configuration
$this->template_dir = $opt['stylepath'];
$this->compile_dir = $opt['rootpath'] . 'cache2/smarty/compiled/';
$this->cache_dir = $opt['rootpath'] . 'cache2/smarty/cache/';
$this->plugins_dir = array('plugins', 'ocplugins');
// disable caching ... if caching is enabled, 1 hour is default
$this->caching = false;
$this->cache_lifetime = 3600; // default
// register additional functions
require_once($opt['rootpath'] . 'lib2/smarty/ocplugins/block.nocache.php');
$this->register_block('nocache', 'smarty_block_nocache', false);
$this->load_filter('pre', 't');
if ($opt['session']['mode'] == SAVE_SESSION)
$this->load_filter('output', 'session');
// cache control
if (($opt['debug'] & DEBUG_TEMPLATES) == DEBUG_TEMPLATES)
$this->force_compile = true;
// process debug level
if (($opt['debug'] & DEBUG_SQLDEBUGGER) == DEBUG_SQLDEBUGGER)
{
require_once($opt['rootpath'] . 'lib2/sqldebugger.class.php');
}
else if (($opt['debug'] & DEBUG_OUTOFSERVICE) == DEBUG_OUTOFSERVICE)
{
$this->name = 'sys_outofservice';
$this->display();
}
/* set login target
*/
if (isset($_REQUEST['target']))
{
$this->target = trim($_REQUEST['target']);
if (strtolower(substr($this->target, 0, 7)) == 'http://')
$this->target = '';
}
else
{
$target = basename($_SERVER['PHP_SELF']) . '?';
// REQUEST-Variablen durchlaufen und an target anhaengen
reset($_REQUEST);
while (list($varname, $varvalue) = each($_REQUEST))
if (in_array($varname, $opt['logic']['targetvars']))
$target .= urlencode($varname) . '=' . urlencode($varvalue) . '&';
reset($_REQUEST);
if (mb_substr($target, -1) == '?' || mb_substr($target, -1) == '&')
$target = mb_substr($target, 0, -1);
$this->target = $target;
}
}
/* ATTENTION: copied from internal implementation!
*/
function compile($resource_name, $compile_id = null)
{
if (!isset($compile_id)) {
$compile_id = $this->compile_id;
}
$this->_compile_id = $compile_id;
// load filters that are marked as autoload
if (count($this->autoload_filters)) {
foreach ($this->autoload_filters as $_filter_type => $_filters) {
foreach ($_filters as $_filter) {
$this->load_filter($_filter_type, $_filter);
}
}
}
$_smarty_compile_path = $this->_get_compile_path($resource_name);
// if we just need to display the results, don't perform output
// buffering - for speed
$_cache_including = $this->_cache_including;
$this->_cache_including = false;
// compile the resource
if (!$this->_is_compiled($resource_name, $_smarty_compile_path))
$this->_compile_resource($resource_name, $_smarty_compile_path);
$this->_cache_including = $_cache_including;
}
function display($dummy1=null, $dummy2=null, $dummy3=null)
{
global $opt, $db, $cookie, $login, $menu, $sqldebugger, $translate;
$cookie->close();
// // if the user is an admin, dont cache the content
// if (isset($login))
// if ($login->admin)
// $this->caching = false;
//Give Smarty access to the whole options array.
$this->assign('siteSettings', $opt);
//Should we remove this whole block since we now have
//access using the siteSettings above?
// assign main template vars
// ... and some of the $opt
$optn['debug'] = $opt['debug'];
$optn['template']['locales'] = $opt['template']['locales'];
$optn['template']['locale'] = $opt['template']['locale'];
$optn['template']['style'] = $opt['template']['style'];
$optn['template']['country'] = $login->getUserCountry();
$optn['page']['subtitle1'] = isset($opt['locale'][$opt['template']['locale']]['page']['subtitle1']) ? $opt['locale'][$opt['template']['locale']]['page']['subtitle1'] : $opt['page']['subtitle1'];
$optn['page']['subtitle2'] = isset($opt['locale'][$opt['template']['locale']]['page']['subtitle2']) ? $opt['locale'][$opt['template']['locale']]['page']['subtitle2'] : $opt['page']['subtitle2'];
$optn['page']['max_logins_per_hour'] = $opt['page']['max_logins_per_hour'];
$optn['page']['absolute_url'] = $opt['page']['absolute_url'];
$optn['page']['target'] = $this->target;
$optn['page']['showdonations'] = $opt['page']['showdonations'];
$optn['page']['title'] = $opt['page']['title'];
$optn['page']['nowpsearch'] = $this->nowpsearch;
$optn['page']['header_javascript'] = $this->header_javascript;
$optn['page']['body_load'] = $this->body_load;
$optn['page']['body_unload'] = $this->body_unload;
$optn['page']['sponsor'] = $opt['page']['sponsor'];
$optn['template']['title'] = $this->title;
$optn['template']['caching'] = $this->caching;
$optn['template']['popup'] = $this->popup;
$optn['template']['popupmargin'] = $this->popupmargin;
$optn['format'] = $opt['locale'][$opt['template']['locale']]['format'];
$optn['mail'] = $opt['mail'];
$optn['lib'] = $opt['lib'];
$optn['cms'] = $opt['cms'];
$optn['geokrety'] = $opt['geokrety'];
$optn['template']['usercountrieslist'] = labels::getLabels('usercountrieslist');
// url-sessions? (for session timout display)
$optn['session']['url'] = false;
if ($opt['session']['mode']==SAVE_SESSION && $login->userid!=0)
{
if (isset($_GET['SESSION']) || isset($_POST['SESSION']))
{
$optn['session']['url'] = true;
}
$optn['session']['id'] = session_id();
}
if (isset($login))
{
$loginn['username'] = $login->username;
$loginn['userid'] = $login->userid;
$loginn['admin'] = $login->admin;
}
else
{
$loginn['username'] = '';
$loginn['userid'] = '';
$loginn['admin'] = '';
}
// build menu
if ($this->menuitem == null)
$menu->SetSelectItem(MNU_ROOT);
else
$menu->SetSelectItem($this->menuitem);
$this->assign('topmenu', $menu->getTopMenu());
$this->assign('submenu', $menu->getSubMenu());
$this->assign('breadcrumb', $menu->getBreadcrumb());
$this->assign('menucolor', $menu->getMenuColor());
if ($this->title == '')
$optn['template']['title'] = $menu->GetMenuTitle();
$this->assign('opt', $optn);
$this->assign('login', $loginn);
if ($db['connected'] == true)
$this->assign('sys_dbconnected', true);
else
$this->assign('sys_dbconnected', false);
$this->assign('sys_dbslave', ($db['slave_id'] != -1));
if ($this->template_exists($this->name . '.tpl'))
$this->assign('template', $this->name);
else if ($this->name != 'sys_error')
$this->error(ERROR_TEMPLATE_NOT_FOUND);
$this->bench->stop();
$this->assign('sys_runtime', $this->bench->diff());
// check if the template is compiled
// if not, check if translation works correct
$_smarty_compile_path = $this->_get_compile_path($this->name);
if (!$this->_is_compiled($this->name, $_smarty_compile_path) && $this->name != 'error')
{
$internal_lang = $translate->t('INTERNAL_LANG', 'all', 'OcSmarty.class.php', '');
if (($internal_lang != $opt['template']['locale']) && ($internal_lang != 'INTERNAL_LANG'))
$this->error(ERROR_COMPILATION_FAILED);
}
if ($this->is_cached() == true)
$this->assign('sys_cached', true);
else
$this->assign('sys_cached', false);
if (($opt['debug'] & DEBUG_SQLDEBUGGER) == DEBUG_SQLDEBUGGER)
{
parent::fetch($this->main_template . '.tpl', $this->get_cache_id(), $this->get_compile_id());
$this->clear_all_assign();
$this->main_template = 'sys_sqldebugger';
$this->assign('commands', $sqldebugger->getCommands());
$this->assign('cancel', $sqldebugger->getCancel());
unset($sqldebugger);
$this->assign('opt', $optn);
$this->assign('login', $loginn);
$this->caching = false;
// unset sqldebugger to allow proper translation of sqldebugger template
$opt['debug'] = $opt['debug'] & ~DEBUG_SQLDEBUGGER;
$this->header();
parent::display($this->main_template . '.tpl');
}
else
{
$this->header();
parent::display($this->main_template . '.tpl', $this->get_cache_id(), $this->get_compile_id());
}
exit;
}
// show an error dialog
function error($id)
{
$this->clear_all_assign();
$this->caching = false;
$this->assign('page', $this->name);
$this->assign('id', $id);
if ($this->menuitem == null)
$this->menuitem = MNU_ERROR;
$args = func_get_args();
unset($args[0]);
for ($i = 1; isset($args[$i]); $i++)
$this->assign('p' . $i, $args[$i]);
$this->name = 'error';
$this->display();
}
// check if this template is valid
function is_cached($dummy1=null, $dummy2=null, $dummy3=null)
{
global $login;
// if the user is an admin, dont cache the content
if (isset($login))
if ($login->admin)
return false;
return parent::is_cached($this->main_template . '.tpl', $this->get_cache_id(), $this->get_compile_id());
}
function get_cache_id()
{
global $opt;
return $this->name . '|' . $this->cache_id;
}
function get_compile_id()
{
global $opt;
return $opt['template']['style'] . '|' . $opt['template']['locale'] . '|' . $this->compile_id;
}
function redirect($page)
{
global $cookie, $opt;
$cookie->close();
// close db-connection
sql_disconnect();
$this->header();
if (strpos($page, "\n") !== false)
$page = substr($page, 0, strpos($page, "\n"));
// redirect
if (substr($page, 0, 7) != 'http://')
{
if (substr($page, 0, 1) == '/') $page = substr($page, 1);
$page = $opt['page']['absolute_url'] . $page;
}
if ($opt['session']['mode'] == SAVE_SESSION)
{
if (defined('SID') && SID != '' && session_id() != '')
{
if (strpos($page, '?') === false)
header("Location: " . $page . '?' . urlencode(session_name()) . '=' . urlencode(session_id()));
else
header("Location: " . $page . '&' . urlencode(session_name()) . '=' . urlencode(session_id()));
}
else
header("Location: " . $page);
}
else
header("Location: " . $page);
exit;
}
function redirect_login()
{
// we cannot redirect the POST-data
if (count($_POST) > 0)
$this->error(ERROR_LOGIN_REQUIRED);
// ok ... redirect the get-data
$target = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$this->redirect('login.php?target=' . urlencode($target));
}
function assign_rs($name, $rs)
{
$items = array();
while ($r = sql_fetch_assoc($rs))
$items[] = $r;
$this->assign($name, $items);
}
function add_header_javascript($src)
{
$this->header_javascript[] = $src;
}
function add_body_load($script)
{
$this->body_load[] = $script;
}
function add_body_unload($script)
{
$this->body_unload[] = $script;
}
function header()
{
global $opt;
global $cookie;
if ($opt['gui'] == GUI_HTML)
{
// charset setzen
header('Content-type: text/html; charset=utf-8');
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// set the cookie
$cookie->header();
}
}
/* - trim target and strip newlines
* - use sDefault if sTarget is absolute and sDefault!=null
*/
function checkTarget($sTarget, $sDefault=null)
{
if (mb_strpos($sTarget, "\n") !== false)
$sTarget = mb_substr($sTarget, 0, mb_strpos($sTarget, "\n"));
$sTarget = mb_trim($sTarget);
if (mb_strtolower(mb_substr($sTarget, 0, 7)) == 'http://' || $sTarget=='')
if ($sDefault != null)
return $sDefault;
return $sTarget;
}
}
?>