You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_debug.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Debug
  4. * Class to collect data for the Smarty Debugging Console
  5. *
  6. * @package Smarty
  7. * @subpackage Debug
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Debug Class
  12. *
  13. * @package Smarty
  14. * @subpackage Debug
  15. */
  16. class Smarty_Internal_Debug extends Smarty_Internal_Data
  17. {
  18. /**
  19. * template data
  20. *
  21. * @var array
  22. */
  23. public $template_data = array();
  24. /**
  25. * List of uid's which shall be ignored
  26. *
  27. * @var array
  28. */
  29. public $ignore_uid = array();
  30. /**
  31. * Index of display() and fetch() calls
  32. *
  33. * @var int
  34. */
  35. public $index = 0;
  36. /**
  37. * Counter for window offset
  38. *
  39. * @var int
  40. */
  41. public $offset = 0;
  42. /**
  43. * Start logging template
  44. *
  45. * @param \Smarty_Internal_Template $template template
  46. * @param null $mode true: display false: fetch null: subtemplate
  47. */
  48. public function start_template(Smarty_Internal_Template $template, $mode = null)
  49. {
  50. if (isset($mode)) {
  51. $this->index ++;
  52. $this->offset ++;
  53. $this->template_data[$this->index] = null;
  54. }
  55. $key = $this->get_key($template);
  56. $this->template_data[$this->index][$key]['start_template_time'] = microtime(true);
  57. }
  58. /**
  59. * End logging of cache time
  60. *
  61. * @param \Smarty_Internal_Template $template cached template
  62. */
  63. public function end_template(Smarty_Internal_Template $template)
  64. {
  65. $key = $this->get_key($template);
  66. $this->template_data[$this->index][$key]['total_time'] +=
  67. microtime(true) - $this->template_data[$this->index][$key]['start_template_time'];
  68. //$this->template_data[$this->index][$key]['properties'] = $template->properties;
  69. }
  70. /**
  71. * Start logging of compile time
  72. *
  73. * @param \Smarty_Internal_Template $template
  74. */
  75. public function start_compile(Smarty_Internal_Template $template)
  76. {
  77. static $_is_stringy = array('string' => true, 'eval' => true);
  78. if (!empty($template->compiler->trace_uid)) {
  79. $key = $template->compiler->trace_uid;
  80. if (!isset($this->template_data[$this->index][$key])) {
  81. if (isset($_is_stringy[$template->source->type])) {
  82. $this->template_data[$this->index][$key]['name'] =
  83. '\'' . substr($template->source->name, 0, 25) . '...\'';
  84. } else {
  85. $this->template_data[$this->index][$key]['name'] = $template->source->filepath;
  86. }
  87. $this->template_data[$this->index][$key]['compile_time'] = 0;
  88. $this->template_data[$this->index][$key]['render_time'] = 0;
  89. $this->template_data[$this->index][$key]['cache_time'] = 0;
  90. }
  91. } else {
  92. if (isset($this->ignore_uid[$template->source->uid])) {
  93. return;
  94. }
  95. $key = $this->get_key($template);
  96. }
  97. $this->template_data[$this->index][$key]['start_time'] = microtime(true);
  98. }
  99. /**
  100. * End logging of compile time
  101. *
  102. * @param \Smarty_Internal_Template $template
  103. */
  104. public function end_compile(Smarty_Internal_Template $template)
  105. {
  106. if (!empty($template->compiler->trace_uid)) {
  107. $key = $template->compiler->trace_uid;
  108. } else {
  109. if (isset($this->ignore_uid[$template->source->uid])) {
  110. return;
  111. }
  112. $key = $this->get_key($template);
  113. }
  114. $this->template_data[$this->index][$key]['compile_time'] +=
  115. microtime(true) - $this->template_data[$this->index][$key]['start_time'];
  116. }
  117. /**
  118. * Start logging of render time
  119. *
  120. * @param \Smarty_Internal_Template $template
  121. */
  122. public function start_render(Smarty_Internal_Template $template)
  123. {
  124. $key = $this->get_key($template);
  125. $this->template_data[$this->index][$key]['start_time'] = microtime(true);
  126. }
  127. /**
  128. * End logging of compile time
  129. *
  130. * @param \Smarty_Internal_Template $template
  131. */
  132. public function end_render(Smarty_Internal_Template $template)
  133. {
  134. $key = $this->get_key($template);
  135. $this->template_data[$this->index][$key]['render_time'] +=
  136. microtime(true) - $this->template_data[$this->index][$key]['start_time'];
  137. }
  138. /**
  139. * Start logging of cache time
  140. *
  141. * @param \Smarty_Internal_Template $template cached template
  142. */
  143. public function start_cache(Smarty_Internal_Template $template)
  144. {
  145. $key = $this->get_key($template);
  146. $this->template_data[$this->index][$key]['start_time'] = microtime(true);
  147. }
  148. /**
  149. * End logging of cache time
  150. *
  151. * @param \Smarty_Internal_Template $template cached template
  152. */
  153. public function end_cache(Smarty_Internal_Template $template)
  154. {
  155. $key = $this->get_key($template);
  156. $this->template_data[$this->index][$key]['cache_time'] +=
  157. microtime(true) - $this->template_data[$this->index][$key]['start_time'];
  158. }
  159. /**
  160. * Register template object
  161. *
  162. * @param \Smarty_Internal_Template $template cached template
  163. */
  164. public function register_template(Smarty_Internal_Template $template)
  165. {
  166. }
  167. /**
  168. * Register data object
  169. *
  170. * @param \Smarty_Data $data data object
  171. */
  172. public static function register_data(Smarty_Data $data)
  173. {
  174. }
  175. /**
  176. * Opens a window for the Smarty Debugging Console and display the data
  177. *
  178. * @param Smarty_Internal_Template|Smarty $obj object to debug
  179. * @param bool $full
  180. */
  181. public function display_debug($obj, $full = false)
  182. {
  183. if (!$full) {
  184. $this->offset ++;
  185. $savedIndex = $this->index;
  186. $this->index = 9999;
  187. }
  188. if ($obj->_objType == 1) {
  189. $smarty = $obj;
  190. } else {
  191. $smarty = $obj->smarty;
  192. }
  193. // create fresh instance of smarty for displaying the debug console
  194. // to avoid problems if the application did overload the Smarty class
  195. $debObj = new Smarty();
  196. // copy the working dirs from application
  197. $debObj->setCompileDir($smarty->getCompileDir());
  198. // init properties by hand as user may have edited the original Smarty class
  199. $debObj->setPluginsDir(is_dir(__DIR__ . '/../plugins') ? __DIR__ . '/../plugins' : $smarty->getPluginsDir());
  200. $debObj->force_compile = false;
  201. $debObj->compile_check = true;
  202. $debObj->left_delimiter = '{';
  203. $debObj->right_delimiter = '}';
  204. $debObj->security_policy = null;
  205. $debObj->debugging = false;
  206. $debObj->debugging_ctrl = 'NONE';
  207. $debObj->error_reporting = E_ALL & ~E_NOTICE;
  208. $debObj->debug_tpl = isset($smarty->debug_tpl) ? $smarty->debug_tpl : 'file:' . __DIR__ . '/../debug.tpl';
  209. $debObj->registered_plugins = array();
  210. $debObj->registered_resources = array();
  211. $debObj->registered_filters = array();
  212. $debObj->autoload_filters = array();
  213. $debObj->default_modifiers = array();
  214. $debObj->escape_html = true;
  215. $debObj->caching = false;
  216. $debObj->compile_id = null;
  217. $debObj->cache_id = null;
  218. // prepare information of assigned variables
  219. $ptr = $this->get_debug_vars($obj);
  220. $_assigned_vars = $ptr->tpl_vars;
  221. ksort($_assigned_vars);
  222. $_config_vars = $ptr->config_vars;
  223. ksort($_config_vars);
  224. $debugging = $smarty->debugging;
  225. $_template = new Smarty_Internal_Template($debObj->debug_tpl, $debObj);
  226. if ($obj->_objType == 2) {
  227. $_template->assign('template_name', $obj->source->type . ':' . $obj->source->name);
  228. }
  229. if ($obj->_objType == 1 || $full) {
  230. $_template->assign('template_data', $this->template_data[$this->index]);
  231. } else {
  232. $_template->assign('template_data', null);
  233. }
  234. $_template->assign('assigned_vars', $_assigned_vars);
  235. $_template->assign('config_vars', $_config_vars);
  236. $_template->assign('execution_time', microtime(true) - $smarty->start_time);
  237. $_template->assign('display_mode', $debugging == 2 || !$full);
  238. $_template->assign('offset', $this->offset * 50);
  239. echo $_template->fetch();
  240. if (isset($full)) {
  241. $this->index --;
  242. }
  243. if (!$full) {
  244. $this->index = $savedIndex;
  245. }
  246. }
  247. /**
  248. * Recursively gets variables from all template/data scopes
  249. *
  250. * @param Smarty_Internal_Template|Smarty_Data $obj object to debug
  251. *
  252. * @return StdClass
  253. */
  254. public function get_debug_vars($obj)
  255. {
  256. $config_vars = array();
  257. foreach ($obj->config_vars as $key => $var) {
  258. $config_vars[$key]['value'] = $var;
  259. if ($obj->_objType == 2) {
  260. $config_vars[$key]['scope'] = $obj->source->type . ':' . $obj->source->name;
  261. } elseif ($obj->_objType == 4) {
  262. $tpl_vars[$key]['scope'] = $obj->dataObjectName;
  263. } else {
  264. $config_vars[$key]['scope'] = 'Smarty object';
  265. }
  266. }
  267. $tpl_vars = array();
  268. foreach ($obj->tpl_vars as $key => $var) {
  269. foreach ($var as $varkey => $varvalue) {
  270. if ($varkey == 'value') {
  271. $tpl_vars[$key][$varkey] = $varvalue;
  272. } else {
  273. if ($varkey == 'nocache') {
  274. if ($varvalue == true) {
  275. $tpl_vars[$key][$varkey] = $varvalue;
  276. }
  277. } else {
  278. if ($varkey != 'scope' || $varvalue !== 0) {
  279. $tpl_vars[$key]['attributes'][$varkey] = $varvalue;
  280. }
  281. }
  282. }
  283. }
  284. if ($obj->_objType == 2) {
  285. $tpl_vars[$key]['scope'] = $obj->source->type . ':' . $obj->source->name;
  286. } elseif ($obj->_objType == 4) {
  287. $tpl_vars[$key]['scope'] = $obj->dataObjectName;
  288. } else {
  289. $tpl_vars[$key]['scope'] = 'Smarty object';
  290. }
  291. }
  292. if (isset($obj->parent)) {
  293. $parent = $this->get_debug_vars($obj->parent);
  294. foreach ($parent->tpl_vars as $name => $pvar) {
  295. if (isset($tpl_vars[$name]) && $tpl_vars[$name]['value'] === $pvar['value']) {
  296. $tpl_vars[$name]['scope'] = $pvar['scope'];
  297. }
  298. }
  299. $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
  300. foreach ($parent->config_vars as $name => $pvar) {
  301. if (isset($config_vars[$name]) && $config_vars[$name]['value'] === $pvar['value']) {
  302. $config_vars[$name]['scope'] = $pvar['scope'];
  303. }
  304. }
  305. $config_vars = array_merge($parent->config_vars, $config_vars);
  306. } else {
  307. foreach (Smarty::$global_tpl_vars as $key => $var) {
  308. if (!array_key_exists($key, $tpl_vars)) {
  309. foreach ($var as $varkey => $varvalue) {
  310. if ($varkey == 'value') {
  311. $tpl_vars[$key][$varkey] = $varvalue;
  312. } else {
  313. if ($varkey == 'nocache') {
  314. if ($varvalue == true) {
  315. $tpl_vars[$key][$varkey] = $varvalue;
  316. }
  317. } else {
  318. if ($varkey != 'scope' || $varvalue !== 0) {
  319. $tpl_vars[$key]['attributes'][$varkey] = $varvalue;
  320. }
  321. }
  322. }
  323. }
  324. $tpl_vars[$key]['scope'] = 'Global';
  325. }
  326. }
  327. }
  328. return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
  329. }
  330. /**
  331. * Return key into $template_data for template
  332. *
  333. * @param \Smarty_Internal_Template $template template object
  334. *
  335. * @return string key into $template_data
  336. */
  337. private function get_key(Smarty_Internal_Template $template)
  338. {
  339. static $_is_stringy = array('string' => true, 'eval' => true);
  340. // calculate Uid if not already done
  341. if ($template->source->uid == '') {
  342. $template->source->filepath;
  343. }
  344. $key = $template->source->uid;
  345. if (isset($this->template_data[$this->index][$key])) {
  346. return $key;
  347. } else {
  348. if (isset($_is_stringy[$template->source->type])) {
  349. $this->template_data[$this->index][$key]['name'] =
  350. '\'' . substr($template->source->name, 0, 25) . '...\'';
  351. } else {
  352. $this->template_data[$this->index][$key]['name'] = $template->source->filepath;
  353. }
  354. $this->template_data[$this->index][$key]['compile_time'] = 0;
  355. $this->template_data[$this->index][$key]['render_time'] = 0;
  356. $this->template_data[$this->index][$key]['cache_time'] = 0;
  357. $this->template_data[$this->index][$key]['total_time'] = 0;
  358. return $key;
  359. }
  360. }
  361. /**
  362. * Ignore template
  363. *
  364. * @param \Smarty_Internal_Template $template
  365. */
  366. public function ignore(Smarty_Internal_Template $template)
  367. {
  368. // calculate Uid if not already done
  369. if ($template->source->uid == '') {
  370. $template->source->filepath;
  371. }
  372. $this->ignore_uid[$template->source->uid] = true;
  373. }
  374. /**
  375. * handle 'URL' debugging mode
  376. *
  377. * @param Smarty $smarty
  378. */
  379. public function debugUrl(Smarty $smarty)
  380. {
  381. if (isset($_SERVER['QUERY_STRING'])) {
  382. $_query_string = $_SERVER['QUERY_STRING'];
  383. } else {
  384. $_query_string = '';
  385. }
  386. if (false !== strpos($_query_string, $smarty->smarty_debug_id)) {
  387. if (false !== strpos($_query_string, $smarty->smarty_debug_id . '=on')) {
  388. // enable debugging for this browser session
  389. setcookie('SMARTY_DEBUG', true);
  390. $smarty->debugging = true;
  391. } elseif (false !== strpos($_query_string, $smarty->smarty_debug_id . '=off')) {
  392. // disable debugging for this browser session
  393. setcookie('SMARTY_DEBUG', false);
  394. $smarty->debugging = false;
  395. } else {
  396. // enable debugging for this page
  397. $smarty->debugging = true;
  398. }
  399. } else {
  400. if (isset($_COOKIE['SMARTY_DEBUG'])) {
  401. $smarty->debugging = true;
  402. }
  403. }
  404. }
  405. }