Base for a static organization website

ToolbarAccessController.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since DebugKit 1.1
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. App::uses('Security', 'Utility');
  15. App::uses('DebugKitAppController', 'DebugKit.Controller');
  16. /**
  17. * DebugKit ToolbarAccess Controller
  18. *
  19. * Allows retrieval of information from the debugKit internals.
  20. *
  21. * @since DebugKit 1.1
  22. */
  23. class ToolbarAccessController extends DebugKitAppController {
  24. /**
  25. * name
  26. *
  27. * @var string
  28. */
  29. public $name = 'ToolbarAccess';
  30. /**
  31. * Helpers
  32. *
  33. * @var array
  34. */
  35. public $helpers = array(
  36. 'DebugKit.Toolbar' => array('output' => 'DebugKit.HtmlToolbar'),
  37. 'Js', 'Number', 'DebugKit.SimpleGraph'
  38. );
  39. /**
  40. * Components
  41. *
  42. * @var array
  43. */
  44. public $components = array('RequestHandler', 'DebugKit.Toolbar');
  45. /**
  46. * Uses
  47. *
  48. * @var array
  49. */
  50. public $uses = array('DebugKit.ToolbarAccess');
  51. /**
  52. * beforeFilter callback
  53. *
  54. * @return void
  55. */
  56. public function beforeFilter() {
  57. parent::beforeFilter();
  58. if (isset($this->Toolbar)) {
  59. $this->Components->disable('Toolbar');
  60. }
  61. $this->helpers['DebugKit.Toolbar']['cacheKey'] = $this->Toolbar->cacheKey;
  62. $this->helpers['DebugKit.Toolbar']['cacheConfig'] = 'debug_kit';
  63. if (isset($this->Auth) && method_exists($this->Auth, 'mapActions')) {
  64. $this->Auth->mapActions(array(
  65. 'read' => array('history_state', 'sql_explain')
  66. ));
  67. }
  68. }
  69. /**
  70. * Get a stored history state from the toolbar cache.
  71. *
  72. * @param null $key
  73. * @return void
  74. */
  75. public function history_state($key = null) {
  76. if (Configure::read('debug') == 0) {
  77. return $this->redirect($this->referer());
  78. }
  79. $oldState = $this->Toolbar->loadState($key);
  80. $this->set('toolbarState', $oldState);
  81. $this->set('debugKitInHistoryMode', true);
  82. $this->viewClass = null;
  83. $this->layout = null;
  84. }
  85. /**
  86. * Run SQL explain/profiling on queries. Checks the hash + the hashed queries,
  87. * if there is mismatch a 404 will be rendered. If debug == 0 a 404 will also be
  88. * rendered. No explain will be run if a 404 is made.
  89. *
  90. * @throws BadRequestException
  91. * @return void
  92. */
  93. public function sql_explain() {
  94. if (
  95. !$this->request->is('post') ||
  96. empty($this->request->data['log']['sql']) ||
  97. empty($this->request->data['log']['ds']) ||
  98. empty($this->request->data['log']['hash']) ||
  99. Configure::read('debug') == 0
  100. ) {
  101. throw new BadRequestException('Invalid parameters');
  102. }
  103. $hash = Security::hash($this->request->data['log']['sql'] . $this->request->data['log']['ds'], 'sha1', true);
  104. if ($hash !== $this->request->data['log']['hash']) {
  105. throw new BadRequestException('Invalid parameters');
  106. }
  107. $result = $this->ToolbarAccess->explainQuery($this->request->data['log']['ds'], $this->request->data['log']['sql']);
  108. $this->set(compact('result'));
  109. }
  110. }