smarty_internal_debug.php 15 KB

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