1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Psr\Log\LogLevel;
13: use Xoops\Core\PreloadItem;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class DebugbarPreload extends PreloadItem
29: {
30: private static $registry = array();
31:
32: 33: 34: 35: 36:
37: private static function getConfigs()
38: {
39: static $configs = null;
40:
41: if (is_null($configs)) {
42: $xoops = Xoops::getInstance();
43: $user_groups = $xoops->getUserGroups();
44: $moduleperm_handler = $xoops->getHandlerGroupPermission();
45: $helper = $xoops->getModuleHelper('debugbar');
46: $mid = $helper->getModule()->getVar('mid');
47: if ($moduleperm_handler->checkRight('use_debugbar', 0, $user_groups, $mid)) {
48:
49: $configs['debugbar_enable'] = $helper->getConfig('debugbar_enable');
50: $configs['debug_smarty_enable'] = $helper->getConfig('debug_smarty_enable');
51:
52: $uchelper = $xoops->getModuleHelper('userconfigs');
53: if ($xoops->isUser() && $uchelper) {
54: $config_handler = $uchelper->getHandlerConfig();
55: $user_configs =
56: $config_handler->getConfigsByUser($xoops->user->getVar('uid'), $mid);
57: if (array_key_exists('debugbar_enable', $user_configs)) {
58: $configs['debugbar_enable'] = $user_configs['debugbar_enable'];
59: }
60: if (array_key_exists('debug_smarty_enable', $user_configs)) {
61: $configs['debug_smarty_enable'] = $user_configs['debug_smarty_enable'];
62: }
63: }
64: } else {
65:
66: $configs['debugbar_enable'] = 0;
67: $configs['debug_smarty_enable'] = 0;
68: }
69: }
70:
71: return $configs;
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public static function eventCoreException($e)
82: {
83: DebugbarLogger::getInstance()->addException($e);
84: }
85:
86: 87: 88: 89: 90: 91: 92:
93: public static function eventCoreIncludeCommonClassmaps($args)
94: {
95: $path = dirname(__DIR__);
96: XoopsLoad::addMap(array(
97: 'debugbarlogger' => $path . '/class/debugbarlogger.php',
98: ));
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: public static function eventCoreIncludeCommonStart($args)
109: {
110: $logger = DebugbarLogger::getInstance();
111:
112: $logger->enable();
113:
114:
115:
116: $logger->startTime();
117: $logger->startTime('XOOPS Boot');
118: }
119:
120: 121: 122: 123: 124: 125: 126:
127: public static function eventCoreDatabaseNoconn($args)
128: {
129: if (class_exists('DebugbarLogger')) {
130:
131: $db = $args[0];
132: DebugbarLogger::getInstance()->log(LogLevel::ALERT, $db->error(), array('errno' => $db->errno()));
133: }
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: public static function eventCoreDatabaseNodb($args)
144: {
145: if (class_exists('DebugbarLogger')) {
146:
147: $db = $args[0];
148: DebugbarLogger::getInstance()->log(LogLevel::ALERT, $db->error(), array('errno' => $db->errno()));
149: }
150: }
151:
152: 153: 154: 155: 156:
157: public static function eventCoreIncludeCommonAuthSuccess()
158: {
159: $xoops = Xoops::getInstance();
160: $logger = DebugbarLogger::getInstance();
161: $configs = self::getConfigs();
162: if ($configs['debugbar_enable']) {
163: $xoops->loadLocale();
164: $xoops->loadLanguage('main', 'debugbar');
165: $logger->enable();
166: } else {
167: $logger->disable();
168: }
169: }
170:
171: 172: 173: 174: 175: 176: 177:
178: public static function eventCoreIncludeCommonEnd($args)
179: {
180: $logger = DebugbarLogger::getInstance();
181: $logger->stopTime('XOOPS Boot');
182: $logger->startTime('Module init');
183: }
184:
185: 186: 187: 188: 189: 190: 191:
192: public static function eventCoreTemplateConstructStart($args)
193: {
194: $tpl = $args[0];
195: $configs = self::getConfigs();
196: if ($configs['debugbar_enable']) {
197: $tpl->debugging_ctrl = 'URL';
198: }
199: if ($configs['debug_smarty_enable']) {
200: $tpl->debugging = Smarty::DEBUG_ON;
201: }
202: }
203:
204: 205: 206: 207: 208: 209: 210:
211: public static function eventCoreThemeRenderStart($args)
212: {
213: DebugbarLogger::getInstance()->startTime('Page rendering');
214: }
215:
216: 217: 218: 219: 220: 221: 222:
223: public static function eventCoreThemeRenderEnd($args)
224: {
225: DebugbarLogger::getInstance()->stopTime('Page rendering');
226: DebugbarLogger::getInstance()->addSmarty();
227: }
228:
229: 230: 231: 232: 233: 234: 235:
236: public static function eventCoreThemeCheckcacheSuccess($args)
237: {
238: $template = $args[0];
239: $theme = $args[1];
240: DebugbarLogger::getInstance()->addExtra(
241: $template,
242: sprintf('Cached (regenerates every %d seconds)', $theme->contentCacheLifetime)
243: );
244: }
245:
246: 247: 248: 249: 250: 251: 252:
253: public static function eventCoreThemeblocksBuildblockStart($args)
254: {
255:
256: $block = $args[0];
257: $isCached= $args[1];
258:
259: $context = array('channel'=>'Blocks', 'cached'=>$isCached, 'cachetime'=>$block->getVar('bcachetime'));
260: DebugbarLogger::getInstance()->log(LogLevel::INFO, $block->getVar('name'), $context);
261:
262: }
263:
264: 265: 266: 267: 268: 269: 270:
271: public static function eventCoreDeprecated($args)
272: {
273: $message = $args[0];
274: DebugbarLogger::getInstance()->log(LogLevel::WARNING, $message, array('channel'=>'Deprecated'));
275: }
276:
277: 278: 279: 280: 281: 282: 283:
284: public static function eventCoreDisableerrorreporting($args)
285: {
286: DebugbarLogger::getInstance()->disable();
287: }
288:
289: 290: 291: 292: 293: 294: 295:
296: public static function eventCoreHeaderStart($args)
297: {
298: $logger = DebugbarLogger::getInstance();
299: $logger->stopTime('Module init');
300: $logger->startTime('XOOPS output init');
301: }
302:
303: 304: 305: 306: 307: 308: 309:
310: public static function eventCoreHeaderEnd($args)
311: {
312: $logger = DebugbarLogger::getInstance();
313: $logger->stopTime('XOOPS output init');
314: $logger->startTime('Module display');
315: }
316:
317: 318: 319: 320: 321: 322: 323:
324: public static function eventCoreFooterStart($args)
325: {
326: $logger = DebugbarLogger::getInstance();
327: $logger->stopTime('Module display');
328: }
329:
330: 331: 332: 333: 334: 335: 336:
337: public static function eventCoreFooterEnd($args)
338: {
339: $logger = DebugbarLogger::getInstance();
340: $logger->stopTime();
341: }
342:
343: 344: 345: 346: 347: 348: 349:
350: public static function eventCoreRedirectStart($args)
351: {
352: DebugbarLogger::getInstance()->stackData();
353: }
354:
355: 356: 357: 358: 359: 360: 361:
362: public static function eventCoreSecurityValidatetokenEnd($args)
363: {
364: $logger = DebugbarLogger::getInstance();
365: $logs = $args[0];
366: foreach ($logs as $log) {
367: $context = array('channel'=>'Extra', 'name'=>$log[0]);
368: $logger->log(LogLevel::INFO, $log[1], $context);
369: }
370: }
371:
372: 373: 374: 375: 376: 377: 378:
379: public static function eventCoreModuleAddlog($args)
380: {
381: $context = array('channel'=>'Extra', 'name'=>$args[0]);
382: DebugbarLogger::getInstance()->log(LogLevel::DEBUG, $args[1], $context);
383:
384: }
385:
386: 387: 388: 389: 390: 391: 392:
393: public static function eventDebugLog($args)
394: {
395: DebugbarLogger::getInstance()->dump($args);
396: }
397:
398: 399: 400: 401: 402: 403: 404:
405: public static function eventDebugTimerStart($args)
406: {
407: $args = (array) $args;
408: DebugbarLogger::getInstance()->startTime($args[0], isset($args[1]) ? $args[1] : null);
409: }
410:
411: 412: 413: 414: 415: 416: 417:
418: public static function eventDebugTimerStop($args)
419: {
420: DebugbarLogger::getInstance()->stopTime($args);
421: }
422:
423: 424: 425: 426: 427: 428: 429:
430: public static function eventCoreSessionShutdown($args)
431: {
432: DebugbarLogger::getInstance()->renderDebugBar();
433: }
434: }
435: