| 12345 |
anikendra |
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 0.1
|
|
|
12 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
13 |
*/
|
|
|
14 |
|
|
|
15 |
App::uses('ToolbarHelper', 'DebugKit.View/Helper');
|
|
|
16 |
App::uses('Security', 'Utility');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Html Toolbar Helper
|
|
|
20 |
*
|
|
|
21 |
* Injects the toolbar elements into HTML layouts.
|
|
|
22 |
* Contains helper methods for
|
|
|
23 |
*
|
|
|
24 |
* @since DebugKit 0.1
|
|
|
25 |
*/
|
|
|
26 |
class HtmlToolbarHelper extends ToolbarHelper {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* helpers property
|
|
|
30 |
*
|
|
|
31 |
* @var array
|
|
|
32 |
*/
|
|
|
33 |
public $helpers = array('Html', 'Form');
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* settings property
|
|
|
37 |
*
|
|
|
38 |
* @var array
|
|
|
39 |
*/
|
|
|
40 |
public $settings = array('format' => 'html', 'forceEnable' => false);
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Recursively goes through an array and makes neat HTML out of it.
|
|
|
44 |
*
|
|
|
45 |
* @param mixed $values Array to make pretty.
|
|
|
46 |
* @param integer $openDepth Depth to add open class
|
|
|
47 |
* @param integer $currentDepth current depth.
|
|
|
48 |
* @param boolean $doubleEncode
|
|
|
49 |
* @return string
|
|
|
50 |
*/
|
|
|
51 |
public function makeNeatArray($values, $openDepth = 0, $currentDepth = 0, $doubleEncode = false) {
|
|
|
52 |
static $printedObjects = null;
|
|
|
53 |
if ($currentDepth === 0) {
|
|
|
54 |
$printedObjects = new SplObjectStorage();
|
|
|
55 |
}
|
|
|
56 |
$className = "neat-array depth-$currentDepth";
|
|
|
57 |
if ($openDepth > $currentDepth) {
|
|
|
58 |
$className .= ' expanded';
|
|
|
59 |
}
|
|
|
60 |
$nextDepth = $currentDepth + 1;
|
|
|
61 |
$out = "<ul class=\"$className\">";
|
|
|
62 |
if (!is_array($values)) {
|
|
|
63 |
if (is_bool($values)) {
|
|
|
64 |
$values = array($values);
|
|
|
65 |
}
|
|
|
66 |
if ($values === null) {
|
|
|
67 |
$values = array(null);
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
if (empty($values)) {
|
|
|
71 |
$values[] = '(empty)';
|
|
|
72 |
}
|
|
|
73 |
foreach ($values as $key => $value) {
|
|
|
74 |
$out .= '<li><strong>' . $key . '</strong>';
|
|
|
75 |
if (is_array($value) && count($value) > 0) {
|
|
|
76 |
$out .= '(array)';
|
|
|
77 |
} elseif (is_object($value)) {
|
|
|
78 |
$out .= '(object)';
|
|
|
79 |
}
|
|
|
80 |
if ($value === null) {
|
|
|
81 |
$value = '(null)';
|
|
|
82 |
}
|
|
|
83 |
if ($value === false) {
|
|
|
84 |
$value = '(false)';
|
|
|
85 |
}
|
|
|
86 |
if ($value === true) {
|
|
|
87 |
$value = '(true)';
|
|
|
88 |
}
|
|
|
89 |
if (empty($value) && $value != 0) {
|
|
|
90 |
$value = '(empty)';
|
|
|
91 |
}
|
|
|
92 |
if ($value instanceof Closure) {
|
|
|
93 |
$value = 'function';
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$isObject = is_object($value);
|
|
|
97 |
if ($isObject && $printedObjects->contains($value)) {
|
|
|
98 |
$isObject = false;
|
|
|
99 |
$value = ' - recursion';
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if ($isObject) {
|
|
|
103 |
$printedObjects->attach($value);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
if (
|
|
|
107 |
(
|
|
|
108 |
$value instanceof ArrayAccess ||
|
|
|
109 |
$value instanceof Iterator ||
|
|
|
110 |
is_array($value) ||
|
|
|
111 |
$isObject
|
|
|
112 |
) && !empty($value)
|
|
|
113 |
) {
|
|
|
114 |
$out .= $this->makeNeatArray($value, $openDepth, $nextDepth, $doubleEncode);
|
|
|
115 |
} else {
|
|
|
116 |
$out .= h($value, $doubleEncode);
|
|
|
117 |
}
|
|
|
118 |
$out .= '</li>';
|
|
|
119 |
}
|
|
|
120 |
$out .= '</ul>';
|
|
|
121 |
return $out;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Create an HTML message
|
|
|
126 |
*
|
|
|
127 |
* @param string $label label content
|
|
|
128 |
* @param string $message message content
|
|
|
129 |
* @return string
|
|
|
130 |
*/
|
|
|
131 |
public function message($label, $message) {
|
|
|
132 |
return sprintf('<p><strong>%s</strong> %s</p>', $label, $message);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Start a panel.
|
|
|
137 |
* Make a link and anchor.
|
|
|
138 |
*
|
|
|
139 |
* @param $title
|
|
|
140 |
* @param $anchor
|
|
|
141 |
* @return string
|
|
|
142 |
*/
|
|
|
143 |
public function panelStart($title, $anchor) {
|
|
|
144 |
$link = $this->Html->link($title, '#' . $anchor);
|
|
|
145 |
return $link;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Create a table.
|
|
|
150 |
*
|
|
|
151 |
* @param array $rows Rows to make.
|
|
|
152 |
* @param array $headers Optional header row.
|
|
|
153 |
* @return string
|
|
|
154 |
*/
|
|
|
155 |
public function table($rows, $headers = array()) {
|
|
|
156 |
$out = '<table class="debug-table">';
|
|
|
157 |
if (!empty($headers)) {
|
|
|
158 |
$out .= $this->Html->tableHeaders($headers);
|
|
|
159 |
}
|
|
|
160 |
$out .= $this->Html->tableCells($rows, array('class' => 'odd'), array('class' => 'even'), false, false);
|
|
|
161 |
$out .= '</table>';
|
|
|
162 |
return $out;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Send method
|
|
|
167 |
*
|
|
|
168 |
* @return void
|
|
|
169 |
*/
|
|
|
170 |
public function send() {
|
|
|
171 |
if (!$this->settings['forceEnable'] && Configure::read('debug') == 0) {
|
|
|
172 |
return;
|
|
|
173 |
}
|
|
|
174 |
$view = $this->_View;
|
|
|
175 |
$head = '';
|
|
|
176 |
if (isset($view->viewVars['debugToolbarCss']) && !empty($view->viewVars['debugToolbarCss'])) {
|
|
|
177 |
$head .= $this->Html->css($view->viewVars['debugToolbarCss']);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
$js = sprintf('window.DEBUGKIT_JQUERY_URL = "%s";', $this->webroot('/debug_kit/js/jquery.js'));
|
|
|
181 |
$head .= $this->Html->scriptBlock($js);
|
|
|
182 |
|
|
|
183 |
if (isset($view->viewVars['debugToolbarJavascript'])) {
|
|
|
184 |
foreach ($view->viewVars['debugToolbarJavascript'] as $script) {
|
|
|
185 |
if ($script) {
|
|
|
186 |
$head .= $this->Html->script($script);
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
if (preg_match('#</head>#', $view->output)) {
|
|
|
191 |
$view->output = preg_replace('#</head>#', $head . "\n</head>", $view->output, 1);
|
|
|
192 |
}
|
|
|
193 |
$toolbar = $view->element('debug_toolbar', array('disableTimer' => true), array('plugin' => 'DebugKit'));
|
|
|
194 |
if (preg_match('#</body>#', $view->output)) {
|
|
|
195 |
$view->output = preg_replace('#</body>#', $toolbar . "\n</body>", $view->output, 1);
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* Generates a SQL explain link for a given query
|
|
|
201 |
*
|
|
|
202 |
* @param string $sql SQL query string you want an explain link for.
|
|
|
203 |
* @param $connection
|
|
|
204 |
* @return string Rendered Html link or '' if the query is not a select/describe
|
|
|
205 |
*/
|
|
|
206 |
public function explainLink($sql, $connection) {
|
|
|
207 |
if (!preg_match('/^[\s()]*SELECT/i', $sql)) {
|
|
|
208 |
return '';
|
|
|
209 |
}
|
|
|
210 |
$sql = str_replace(array("\n", "\t"), ' ', $sql);
|
|
|
211 |
$hash = Security::hash($sql . $connection, 'sha1', true);
|
|
|
212 |
$url = array(
|
|
|
213 |
'plugin' => 'debug_kit',
|
|
|
214 |
'controller' => 'toolbar_access',
|
|
|
215 |
'action' => 'sql_explain'
|
|
|
216 |
);
|
|
|
217 |
foreach (Router::prefixes() as $prefix) {
|
|
|
218 |
$url[$prefix] = false;
|
|
|
219 |
}
|
|
|
220 |
$this->explainLinkUid = (isset($this->explainLinkUid) ? $this->explainLinkUid + 1 : 0);
|
|
|
221 |
$uid = $this->explainLinkUid . '_' . rand(0, 10000);
|
|
|
222 |
$form = $this->Form->create('log', array('url' => $url, 'id' => "logForm{$uid}"));
|
|
|
223 |
$form .= $this->Form->hidden('log.ds', array('id' => "logDs{$uid}", 'value' => $connection));
|
|
|
224 |
$form .= $this->Form->hidden('log.sql', array('id' => "logSql{$uid}", 'value' => $sql));
|
|
|
225 |
$form .= $this->Form->hidden('log.hash', array('id' => "logHash{$uid}", 'value' => $hash));
|
|
|
226 |
$form .= $this->Form->submit(__d('debug_kit', 'Explain'), array(
|
|
|
227 |
'div' => false,
|
|
|
228 |
'class' => 'sql-explain-link'
|
|
|
229 |
));
|
|
|
230 |
$form .= $this->Form->end();
|
|
|
231 |
return $form;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
}
|