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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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) && !$template->_isSubTpl()) {
  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. * @throws \Exception
  182. * @throws \SmartyException
  183. */
  184. public function display_debug($obj, $full = false)
  185. {
  186. if (!$full) {
  187. $this->offset ++;
  188. $savedIndex = $this->index;
  189. $this->index = 9999;
  190. }
  191. $smarty = $obj->_getSmartyObj();
  192. // create fresh instance of smarty for displaying the debug console
  193. // to avoid problems if the application did overload the Smarty class
  194. $debObj = new Smarty();
  195. // copy the working dirs from application
  196. $debObj->setCompileDir($smarty->getCompileDir());
  197. // init properties by hand as user may have edited the original Smarty class
  198. $debObj->setPluginsDir(is_dir(__DIR__ . '/../plugins') ? __DIR__ . '/../plugins' : $smarty->getPluginsDir());
  199. $debObj->force_compile = false;
  200. $debObj->compile_check = Smarty::COMPILECHECK_ON;
  201. $debObj->left_delimiter = '{';
  202. $debObj->right_delimiter = '}';
  203. $debObj->security_policy = null;
  204. $debObj->debugging = false;
  205. $debObj->debugging_ctrl = 'NONE';
  206. $debObj->error_reporting = E_ALL & ~E_NOTICE;
  207. $debObj->debug_tpl = isset($smarty->debug_tpl) ? $smarty->debug_tpl : 'file:' . __DIR__ . '/../debug.tpl';
  208. $debObj->registered_plugins = array();
  209. $debObj->registered_resources = array();
  210. $debObj->registered_filters = array();
  211. $debObj->autoload_filters = array();
  212. $debObj->default_modifiers = array();
  213. $debObj->escape_html = true;
  214. $debObj->caching = Smarty::CACHING_OFF;
  215. $debObj->compile_id = null;
  216. $debObj->cache_id = null;
  217. // prepare information of assigned variables
  218. $ptr = $this->get_debug_vars($obj);
  219. $_assigned_vars = $ptr->tpl_vars;
  220. ksort($_assigned_vars);
  221. $_config_vars = $ptr->config_vars;
  222. ksort($_config_vars);
  223. $debugging = $smarty->debugging;
  224. $_template = new Smarty_Internal_Template($debObj->debug_tpl, $debObj);
  225. if ($obj->_isTplObj()) {
  226. $_template->assign('template_name', $obj->source->type . ':' . $obj->source->name);
  227. }
  228. if ($obj->_objType === 1 || $full) {
  229. $_template->assign('template_data', $this->template_data[ $this->index ]);
  230. } else {
  231. $_template->assign('template_data', null);
  232. }
  233. $_template->assign('assigned_vars', $_assigned_vars);
  234. $_template->assign('config_vars', $_config_vars);
  235. $_template->assign('execution_time', microtime(true) - $smarty->start_time);
  236. $_template->assign('display_mode', $debugging === 2 || !$full);
  237. $_template->assign('offset', $this->offset * 50);
  238. echo $_template->fetch();
  239. if (isset($full)) {
  240. $this->index --;
  241. }
  242. if (!$full) {
  243. $this->index = $savedIndex;
  244. }
  245. }
  246. /**
  247. * Recursively gets variables from all template/data scopes
  248. *
  249. * @param Smarty_Internal_Template|Smarty_Data $obj object to debug
  250. *
  251. * @return StdClass
  252. */
  253. public function get_debug_vars($obj)
  254. {
  255. $config_vars = array();
  256. foreach ($obj->config_vars as $key => $var) {
  257. $config_vars[ $key ][ 'value' ] = $var;
  258. if ($obj->_isTplObj()) {
  259. $config_vars[ $key ][ 'scope' ] = $obj->source->type . ':' . $obj->source->name;
  260. } elseif ($obj->_isDataObj()) {
  261. $tpl_vars[ $key ][ 'scope' ] = $obj->dataObjectName;
  262. } else {
  263. $config_vars[ $key ][ 'scope' ] = 'Smarty object';
  264. }
  265. }
  266. $tpl_vars = array();
  267. foreach ($obj->tpl_vars as $key => $var) {
  268. foreach ($var as $varkey => $varvalue) {
  269. if ($varkey === 'value') {
  270. $tpl_vars[ $key ][ $varkey ] = $varvalue;
  271. } else {
  272. if ($varkey === 'nocache') {
  273. if ($varvalue === true) {
  274. $tpl_vars[ $key ][ $varkey ] = $varvalue;
  275. }
  276. } else {
  277. if ($varkey !== 'scope' || $varvalue !== 0) {
  278. $tpl_vars[ $key ][ 'attributes' ][ $varkey ] = $varvalue;
  279. }
  280. }
  281. }
  282. }
  283. if ($obj->_isTplObj()) {
  284. $tpl_vars[ $key ][ 'scope' ] = $obj->source->type . ':' . $obj->source->name;
  285. } elseif ($obj->_isDataObj()) {
  286. $tpl_vars[ $key ][ 'scope' ] = $obj->dataObjectName;
  287. } else {
  288. $tpl_vars[ $key ][ 'scope' ] = 'Smarty object';
  289. }
  290. }
  291. if (isset($obj->parent)) {
  292. $parent = $this->get_debug_vars($obj->parent);
  293. foreach ($parent->tpl_vars as $name => $pvar) {
  294. if (isset($tpl_vars[ $name ]) && $tpl_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
  295. $tpl_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
  296. }
  297. }
  298. $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
  299. foreach ($parent->config_vars as $name => $pvar) {
  300. if (isset($config_vars[ $name ]) && $config_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
  301. $config_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
  302. }
  303. }
  304. $config_vars = array_merge($parent->config_vars, $config_vars);
  305. } else {
  306. foreach (Smarty::$global_tpl_vars as $key => $var) {
  307. if (!array_key_exists($key, $tpl_vars)) {
  308. foreach ($var as $varkey => $varvalue) {
  309. if ($varkey === 'value') {
  310. $tpl_vars[ $key ][ $varkey ] = $varvalue;
  311. } else {
  312. if ($varkey === 'nocache') {
  313. if ($varvalue === true) {
  314. $tpl_vars[ $key ][ $varkey ] = $varvalue;
  315. }
  316. } else {
  317. if ($varkey !== 'scope' || $varvalue !== 0) {
  318. $tpl_vars[ $key ][ 'attributes' ][ $varkey ] = $varvalue;
  319. }
  320. }
  321. }
  322. }
  323. $tpl_vars[ $key ][ 'scope' ] = 'Global';
  324. }
  325. }
  326. }
  327. return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
  328. }
  329. /**
  330. * Return key into $template_data for template
  331. *
  332. * @param \Smarty_Internal_Template $template template object
  333. *
  334. * @return string key into $template_data
  335. */
  336. private function get_key(Smarty_Internal_Template $template)
  337. {
  338. static $_is_stringy = array('string' => true, 'eval' => true);
  339. // calculate Uid if not already done
  340. if ($template->source->uid === '') {
  341. $template->source->filepath;
  342. }
  343. $key = $template->source->uid;
  344. if (isset($this->template_data[ $this->index ][ $key ])) {
  345. return $key;
  346. } else {
  347. if (isset($_is_stringy[ $template->source->type ])) {
  348. $this->template_data[ $this->index ][ $key ][ 'name' ] =
  349. '\'' . substr($template->source->name, 0, 25) . '...\'';
  350. } else {
  351. $this->template_data[ $this->index ][ $key ][ 'name' ] = $template->source->filepath;
  352. }
  353. $this->template_data[ $this->index ][ $key ][ 'compile_time' ] = 0;
  354. $this->template_data[ $this->index ][ $key ][ 'render_time' ] = 0;
  355. $this->template_data[ $this->index ][ $key ][ 'cache_time' ] = 0;
  356. $this->template_data[ $this->index ][ $key ][ 'total_time' ] = 0;
  357. return $key;
  358. }
  359. }
  360. /**
  361. * Ignore template
  362. *
  363. * @param \Smarty_Internal_Template $template
  364. */
  365. public function ignore(Smarty_Internal_Template $template)
  366. {
  367. // calculate Uid if not already done
  368. if ($template->source->uid === '') {
  369. $template->source->filepath;
  370. }
  371. $this->ignore_uid[ $template->source->uid ] = true;
  372. }
  373. /**
  374. * handle 'URL' debugging mode
  375. *
  376. * @param Smarty $smarty
  377. */
  378. public function debugUrl(Smarty $smarty)
  379. {
  380. if (isset($_SERVER[ 'QUERY_STRING' ])) {
  381. $_query_string = $_SERVER[ 'QUERY_STRING' ];
  382. } else {
  383. $_query_string = '';
  384. }
  385. if (false !== strpos($_query_string, $smarty->smarty_debug_id)) {
  386. if (false !== strpos($_query_string, $smarty->smarty_debug_id . '=on')) {
  387. // enable debugging for this browser session
  388. setcookie('SMARTY_DEBUG', true);
  389. $smarty->debugging = true;
  390. } elseif (false !== strpos($_query_string, $smarty->smarty_debug_id . '=off')) {
  391. // disable debugging for this browser session
  392. setcookie('SMARTY_DEBUG', false);
  393. $smarty->debugging = false;
  394. } else {
  395. // enable debugging for this page
  396. $smarty->debugging = true;
  397. }
  398. } else {
  399. if (isset($_COOKIE[ 'SMARTY_DEBUG' ])) {
  400. $smarty->debugging = true;
  401. }
  402. }
  403. }
  404. }