db backup prior to drupal security update
[yaffs-website] / vendor / symfony / var-dumper / Dumper / HtmlDumper.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\VarDumper\Dumper;
13
14 use Symfony\Component\VarDumper\Cloner\Cursor;
15 use Symfony\Component\VarDumper\Cloner\Data;
16
17 /**
18  * HtmlDumper dumps variables as HTML.
19  *
20  * @author Nicolas Grekas <p@tchwork.com>
21  */
22 class HtmlDumper extends CliDumper
23 {
24     public static $defaultOutput = 'php://output';
25
26     protected $dumpHeader;
27     protected $dumpPrefix = '<pre class=sf-dump id=%s data-indent-pad="%s">';
28     protected $dumpSuffix = '</pre><script>Sfdump("%s")</script>';
29     protected $dumpId = 'sf-dump';
30     protected $colors = true;
31     protected $headerIsDumped = false;
32     protected $lastDepth = -1;
33     protected $styles = array(
34         'default' => 'background-color:#18171B; color:#FF8400; line-height:1.2em; font:12px Menlo, Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:99999; word-break: normal',
35         'num' => 'font-weight:bold; color:#1299DA',
36         'const' => 'font-weight:bold',
37         'str' => 'font-weight:bold; color:#56DB3A',
38         'note' => 'color:#1299DA',
39         'ref' => 'color:#A0A0A0',
40         'public' => 'color:#FFFFFF',
41         'protected' => 'color:#FFFFFF',
42         'private' => 'color:#FFFFFF',
43         'meta' => 'color:#B729D9',
44         'key' => 'color:#56DB3A',
45         'index' => 'color:#1299DA',
46     );
47
48     /**
49      * {@inheritdoc}
50      */
51     public function __construct($output = null, $charset = null)
52     {
53         AbstractDumper::__construct($output, $charset);
54         $this->dumpId = 'sf-dump-'.mt_rand();
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function setStyles(array $styles)
61     {
62         $this->headerIsDumped = false;
63         $this->styles = $styles + $this->styles;
64     }
65
66     /**
67      * Sets an HTML header that will be dumped once in the output stream.
68      *
69      * @param string $header An HTML string
70      */
71     public function setDumpHeader($header)
72     {
73         $this->dumpHeader = $header;
74     }
75
76     /**
77      * Sets an HTML prefix and suffix that will encapse every single dump.
78      *
79      * @param string $prefix The prepended HTML string
80      * @param string $suffix The appended HTML string
81      */
82     public function setDumpBoundaries($prefix, $suffix)
83     {
84         $this->dumpPrefix = $prefix;
85         $this->dumpSuffix = $suffix;
86     }
87
88     /**
89      * {@inheritdoc}
90      */
91     public function dump(Data $data, $output = null)
92     {
93         parent::dump($data, $output);
94         $this->dumpId = 'sf-dump-'.mt_rand();
95     }
96
97     /**
98      * Dumps the HTML header.
99      */
100     protected function getDumpHeader()
101     {
102         $this->headerIsDumped = null !== $this->outputStream ? $this->outputStream : $this->lineDumper;
103
104         if (null !== $this->dumpHeader) {
105             return $this->dumpHeader;
106         }
107
108         $line = <<<'EOHTML'
109 <script>
110 Sfdump = window.Sfdump || (function (doc) {
111
112 var refStyle = doc.createElement('style'),
113     rxEsc = /([.*+?^${}()|\[\]\/\\])/g,
114     idRx = /\bsf-dump-\d+-ref[012]\w+\b/,
115     keyHint = 0 <= navigator.platform.toUpperCase().indexOf('MAC') ? 'Cmd' : 'Ctrl',
116     addEventListener = function (e, n, cb) {
117         e.addEventListener(n, cb, false);
118     };
119
120 (doc.documentElement.firstElementChild || doc.documentElement.children[0]).appendChild(refStyle);
121
122 if (!doc.addEventListener) {
123     addEventListener = function (element, eventName, callback) {
124         element.attachEvent('on' + eventName, function (e) {
125             e.preventDefault = function () {e.returnValue = false;};
126             e.target = e.srcElement;
127             callback(e);
128         });
129     };
130 }
131
132 function toggle(a, recursive) {
133     var s = a.nextSibling || {}, oldClass = s.className, arrow, newClass;
134
135     if ('sf-dump-compact' == oldClass) {
136         arrow = '▼';
137         newClass = 'sf-dump-expanded';
138     } else if ('sf-dump-expanded' == oldClass) {
139         arrow = '▶';
140         newClass = 'sf-dump-compact';
141     } else {
142         return false;
143     }
144
145     a.lastChild.innerHTML = arrow;
146     s.className = newClass;
147
148     if (recursive) {
149         try {
150             a = s.querySelectorAll('.'+oldClass);
151             for (s = 0; s < a.length; ++s) {
152                 if (a[s].className !== newClass) {
153                     a[s].className = newClass;
154                     a[s].previousSibling.lastChild.innerHTML = arrow;
155                 }
156             }
157         } catch (e) {
158         }
159     }
160
161     return true;
162 };
163
164 return function (root) {
165     root = doc.getElementById(root);
166
167     function a(e, f) {
168         addEventListener(root, e, function (e) {
169             if ('A' == e.target.tagName) {
170                 f(e.target, e);
171             } else if ('A' == e.target.parentNode.tagName) {
172                 f(e.target.parentNode, e);
173             }
174         });
175     };
176     function isCtrlKey(e) {
177         return e.ctrlKey || e.metaKey;
178     }
179     addEventListener(root, 'mouseover', function (e) {
180         if ('' != refStyle.innerHTML) {
181             refStyle.innerHTML = '';
182         }
183     });
184     a('mouseover', function (a) {
185         if (a = idRx.exec(a.className)) {
186             try {
187                 refStyle.innerHTML = 'pre.sf-dump .'+a[0]+'{background-color: #B729D9; color: #FFF !important; border-radius: 2px}';
188             } catch (e) {
189             }
190         }
191     });
192     a('click', function (a, e) {
193         if (/\bsf-dump-toggle\b/.test(a.className)) {
194             e.preventDefault();
195             if (!toggle(a, isCtrlKey(e))) {
196                 var r = doc.getElementById(a.getAttribute('href').substr(1)),
197                     s = r.previousSibling,
198                     f = r.parentNode,
199                     t = a.parentNode;
200                 t.replaceChild(r, a);
201                 f.replaceChild(a, s);
202                 t.insertBefore(s, r);
203                 f = f.firstChild.nodeValue.match(indentRx);
204                 t = t.firstChild.nodeValue.match(indentRx);
205                 if (f && t && f[0] !== t[0]) {
206                     r.innerHTML = r.innerHTML.replace(new RegExp('^'+f[0].replace(rxEsc, '\\$1'), 'mg'), t[0]);
207                 }
208                 if ('sf-dump-compact' == r.className) {
209                     toggle(s, isCtrlKey(e));
210                 }
211             }
212
213             if (doc.getSelection) {
214                 try {
215                     doc.getSelection().removeAllRanges();
216                 } catch (e) {
217                     doc.getSelection().empty();
218                 }
219             } else {
220                 doc.selection.empty();
221             }
222         }
223     });
224
225     var indentRx = new RegExp('^('+(root.getAttribute('data-indent-pad') || '  ').replace(rxEsc, '\\$1')+')+', 'm'),
226         elt = root.getElementsByTagName('A'),
227         len = elt.length,
228         i = 0,
229         t = [];
230
231     while (i < len) t.push(elt[i++]);
232
233     elt = root.getElementsByTagName('SAMP');
234     len = elt.length;
235     i = 0;
236
237     while (i < len) t.push(elt[i++]);
238
239     root = t;
240     len = t.length;
241     i = t = 0;
242
243     while (i < len) {
244         elt = root[i];
245         if ("SAMP" == elt.tagName) {
246             elt.className = "sf-dump-expanded";
247             a = elt.previousSibling || {};
248             if ('A' != a.tagName) {
249                 a = doc.createElement('A');
250                 a.className = 'sf-dump-ref';
251                 elt.parentNode.insertBefore(a, elt);
252             } else {
253                 a.innerHTML += ' ';
254             }
255             a.title = (a.title ? a.title+'\n[' : '[')+keyHint+'+click] Expand all children';
256             a.innerHTML += '<span>▼</span>';
257             a.className += ' sf-dump-toggle';
258             if ('sf-dump' != elt.parentNode.className) {
259                 toggle(a);
260             }
261         } else if ("sf-dump-ref" == elt.className && (a = elt.getAttribute('href'))) {
262             a = a.substr(1);
263             elt.className += ' '+a;
264
265             if (/[\[{]$/.test(elt.previousSibling.nodeValue)) {
266                 a = a != elt.nextSibling.id && doc.getElementById(a);
267                 try {
268                     t = a.nextSibling;
269                     elt.appendChild(a);
270                     t.parentNode.insertBefore(a, t);
271                     if (/^[@#]/.test(elt.innerHTML)) {
272                         elt.innerHTML += ' <span>▶</span>';
273                     } else {
274                         elt.innerHTML = '<span>▶</span>';
275                         elt.className = 'sf-dump-ref';
276                     }
277                     elt.className += ' sf-dump-toggle';
278                 } catch (e) {
279                     if ('&' == elt.innerHTML.charAt(0)) {
280                         elt.innerHTML = '…';
281                         elt.className = 'sf-dump-ref';
282                     }
283                 }
284             }
285         }
286         ++i;
287     }
288 };
289
290 })(document);
291 </script><style>
292 pre.sf-dump {
293     display: block;
294     white-space: pre;
295     padding: 5px;
296 }
297 pre.sf-dump span {
298     display: inline;
299 }
300 pre.sf-dump .sf-dump-compact {
301     display: none;
302 }
303 pre.sf-dump abbr {
304     text-decoration: none;
305     border: none;
306     cursor: help;
307 }
308 pre.sf-dump a {
309     text-decoration: none;
310     cursor: pointer;
311     border: 0;
312     outline: none;
313 }
314 EOHTML;
315
316         foreach ($this->styles as $class => $style) {
317             $line .= 'pre.sf-dump'.('default' !== $class ? ' .sf-dump-'.$class : '').'{'.$style.'}';
318         }
319
320         return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
321     }
322
323     /**
324      * {@inheritdoc}
325      */
326     public function enterHash(Cursor $cursor, $type, $class, $hasChild)
327     {
328         parent::enterHash($cursor, $type, $class, false);
329
330         if ($hasChild) {
331             if ($cursor->refIndex) {
332                 $r = Cursor::HASH_OBJECT !== $type ? 1 - (Cursor::HASH_RESOURCE !== $type) : 2;
333                 $r .= $r && 0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->refIndex;
334
335                 $this->line .= sprintf('<samp id=%s-ref%s>', $this->dumpId, $r);
336             } else {
337                 $this->line .= '<samp>';
338             }
339             $this->dumpLine($cursor->depth);
340         }
341     }
342
343     /**
344      * {@inheritdoc}
345      */
346     public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
347     {
348         $this->dumpEllipsis($cursor, $hasChild, $cut);
349         if ($hasChild) {
350             $this->line .= '</samp>';
351         }
352         parent::leaveHash($cursor, $type, $class, $hasChild, 0);
353     }
354
355     /**
356      * {@inheritdoc}
357      */
358     protected function style($style, $value, $attr = array())
359     {
360         if ('' === $value) {
361             return '';
362         }
363
364         $v = esc($value);
365
366         if ('ref' === $style) {
367             if (empty($attr['count'])) {
368                 return sprintf('<a class=sf-dump-ref>%s</a>', $v);
369             }
370             $r = ('#' !== $v[0] ? 1 - ('@' !== $v[0]) : 2).substr($value, 1);
371
372             return sprintf('<a class=sf-dump-ref href=#%s-ref%s title="%d occurrences">%s</a>', $this->dumpId, $r, 1 + $attr['count'], $v);
373         }
374
375         if ('const' === $style && isset($attr['value'])) {
376             $style .= sprintf(' title="%s"', esc(is_scalar($attr['value']) ? $attr['value'] : json_encode($attr['value'])));
377         } elseif ('public' === $style) {
378             $style .= sprintf(' title="%s"', empty($attr['dynamic']) ? 'Public property' : 'Runtime added dynamic property');
379         } elseif ('str' === $style && 1 < $attr['length']) {
380             $style .= sprintf(' title="%d%s characters"', $attr['length'], $attr['binary'] ? ' binary or non-UTF-8' : '');
381         } elseif ('note' === $style && false !== $c = strrpos($v, '\\')) {
382             return sprintf('<abbr title="%s" class=sf-dump-%s>%s</abbr>', $v, $style, substr($v, $c + 1));
383         } elseif ('protected' === $style) {
384             $style .= ' title="Protected property"';
385         } elseif ('private' === $style) {
386             $style .= sprintf(' title="Private property defined in class:&#10;`%s`"', esc($attr['class']));
387         }
388
389         $map = static::$controlCharsMap;
390         $style = "<span class=sf-dump-{$style}>";
391         $v = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $style) {
392             $s = '</span>';
393             $c = $c[$i = 0];
394             do {
395                 $s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', ord($c[$i]));
396             } while (isset($c[++$i]));
397
398             return $s.$style;
399         }, $v, -1, $cchrCount);
400
401         if ($cchrCount && '<' === $v[0]) {
402             $v = substr($v, 7);
403         } else {
404             $v = $style.$v;
405         }
406         if ($cchrCount && '>' === substr($v, -1)) {
407             $v = substr($v, 0, -strlen($style));
408         } else {
409             $v .= '</span>';
410         }
411
412         return $v;
413     }
414
415     /**
416      * {@inheritdoc}
417      */
418     protected function dumpLine($depth, $endOfValue = false)
419     {
420         if (-1 === $this->lastDepth) {
421             $this->line = sprintf($this->dumpPrefix, $this->dumpId, $this->indentPad).$this->line;
422         }
423         if ($this->headerIsDumped !== (null !== $this->outputStream ? $this->outputStream : $this->lineDumper)) {
424             $this->line = $this->getDumpHeader().$this->line;
425         }
426
427         if (-1 === $depth) {
428             $this->line .= sprintf($this->dumpSuffix, $this->dumpId);
429         }
430         $this->lastDepth = $depth;
431
432         $this->line = mb_convert_encoding($this->line, 'HTML-ENTITIES', 'UTF-8');
433
434         if (-1 === $depth) {
435             AbstractDumper::dumpLine(0);
436         }
437         AbstractDumper::dumpLine($depth);
438     }
439 }
440
441 function esc($str)
442 {
443     return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
444 }