/var/www/hkosl.com/nick/codeigniter/application/vendor/maximebf/debugbar/src/DebugBar/DebugBar.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php
/*
 * This file is part of the DebugBar package.
 *
 * (c) 2013 Maxime Bouroumeau-Fuseau
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace DebugBar;

use 
ArrayAccess;
use 
DebugBar\DataCollector\DataCollectorInterface;
use 
DebugBar\Storage\StorageInterface;

/**
 * Main DebugBar object
 *
 * Manages data collectors. DebugBar provides an array-like access
 * to collectors by name.
 *
 * <code>
 *     $debugbar = new DebugBar();
 *     $debugbar->addCollector(new DataCollector\MessagesCollector());
 *     $debugbar['messages']->addMessage("foobar");
 * </code>
 */
class DebugBar implements ArrayAccess
{
    public static 
$useOpenHandlerWhenSendingDataHeaders false;

    protected 
$collectors = array();

    protected 
$data;

    protected 
$jsRenderer;

    protected 
$requestIdGenerator;

    protected 
$requestId;

    protected 
$storage;

    protected 
$httpDriver;

    protected 
$stackSessionNamespace 'PHPDEBUGBAR_STACK_DATA';

    protected 
$stackAlwaysUseSessionStorage false;

    
/**
     * Adds a data collector
     *
     * @param DataCollectorInterface $collector
     *
     * @throws DebugBarException
     * @return $this
     */
    
public function addCollector(DataCollectorInterface $collector)
    {
        if (
$collector->getName() === '__meta') {
            throw new 
DebugBarException("'__meta' is a reserved name and cannot be used as a collector name");
        }
        if (isset(
$this->collectors[$collector->getName()])) {
            throw new 
DebugBarException("'{$collector->getName()}' is already a registered collector");
        }
        
$this->collectors[$collector->getName()] = $collector;
        return 
$this;
    }

    
/**
     * Checks if a data collector has been added
     *
     * @param string $name
     * @return boolean
     */
    
public function hasCollector($name)
    {
        return isset(
$this->collectors[$name]);
    }

    
/**
     * Returns a data collector
     *
     * @param string $name
     * @return DataCollectorInterface
     * @throws DebugBarException
     */
    
public function getCollector($name)
    {
        if (!isset(
$this->collectors[$name])) {
            throw new 
DebugBarException("'$name' is not a registered collector");
        }
        return 
$this->collectors[$name];
    }

    
/**
     * Returns an array of all data collectors
     *
     * @return array[DataCollectorInterface]
     */
    
public function getCollectors()
    {
        return 
$this->collectors;
    }

    
/**
     * Sets the request id generator
     *
     * @param RequestIdGeneratorInterface $generator
     * @return $this
     */
    
public function setRequestIdGenerator(RequestIdGeneratorInterface $generator)
    {
        
$this->requestIdGenerator $generator;
        return 
$this;
    }

    
/**
     * @return RequestIdGeneratorInterface
     */
    
public function getRequestIdGenerator()
    {
        if (
$this->requestIdGenerator === null) {
            
$this->requestIdGenerator = new RequestIdGenerator();
        }
        return 
$this->requestIdGenerator;
    }

    
/**
     * Returns the id of the current request
     *
     * @return string
     */
    
public function getCurrentRequestId()
    {
        if (
$this->requestId === null) {
            
$this->requestId $this->getRequestIdGenerator()->generate();
        }
        return 
$this->requestId;
    }

    
/**
     * Sets the storage backend to use to store the collected data
     *
     * @param StorageInterface $storage
     * @return $this
     */
    
public function setStorage(StorageInterface $storage null)
    {
        
$this->storage $storage;
        return 
$this;
    }

    
/**
     * @return StorageInterface
     */
    
public function getStorage()
    {
        return 
$this->storage;
    }

    
/**
     * Checks if the data will be persisted
     *
     * @return boolean
     */
    
public function isDataPersisted()
    {
        return 
$this->storage !== null;
    }

    
/**
     * Sets the HTTP driver
     *
     * @param HttpDriverInterface $driver
     * @return $this
     */
    
public function setHttpDriver(HttpDriverInterface $driver)
    {
        
$this->httpDriver $driver;
        return 
$this;
    }

    
/**
     * Returns the HTTP driver
     *
     * If no http driver where defined, a PhpHttpDriver is automatically created
     *
     * @return HttpDriverInterface
     */
    
public function getHttpDriver()
    {
        if (
$this->httpDriver === null) {
            
$this->httpDriver = new PhpHttpDriver();
        }
        return 
$this->httpDriver;
    }

    
/**
     * Collects the data from the collectors
     *
     * @return array
     */
    
public function collect()
    {
        if (
php_sapi_name() === 'cli') {
            
$ip gethostname();
            if (
$ip) {
                
$ip gethostbyname($ip);
            } else {
                
$ip '127.0.0.1';
            }
            
$request_variables = array(
                
'method' => 'CLI',
                
'uri' => isset($_SERVER['SCRIPT_FILENAME']) ? realpath($_SERVER['SCRIPT_FILENAME']) : null,
                
'ip' => $ip
            
);
        } else {
            
$request_variables = array(
                
'method' => isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null,
                
'uri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null,
                
'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null
            
);
        }
        
$this->data = array(
            
'__meta' => array_merge(
                array(
                    
'id' => $this->getCurrentRequestId(),
                    
'datetime' => date('Y-m-d H:i:s'),
                    
'utime' => microtime(true)
                ),
                
$request_variables
            
)
        );

        foreach (
$this->collectors as $name => $collector) {
            
$this->data[$name] = $collector->collect();
        }

        
// Remove all invalid (non UTF-8) characters
        
array_walk_recursive($this->data, function (&$item) {
                if (
is_string($item) && !mb_check_encoding($item'UTF-8')) {
                    
$item mb_convert_encoding($item'UTF-8''UTF-8');
                }
            });

        if (
$this->storage !== null) {
            
$this->storage->save($this->getCurrentRequestId(), $this->data);
        }

        return 
$this->data;
    }

    
/**
     * Returns collected data
     *
     * Will collect the data if none have been collected yet
     *
     * @return array
     */
    
public function getData()
    {
        if (
$this->data === null) {
            
$this->collect();
        }
        return 
$this->data;
    }

    
/**
     * Returns an array of HTTP headers containing the data
     *
     * @param string $headerName
     * @param integer $maxHeaderLength
     * @return array
     */
    
public function getDataAsHeaders($headerName 'phpdebugbar'$maxHeaderLength 4096$maxTotalHeaderLength 250000)
    {
        
$data rawurlencode(json_encode(array(
            
'id' => $this->getCurrentRequestId(),
            
'data' => $this->getData()
        )));

        if (
strlen($data) > $maxTotalHeaderLength) {
            
$data rawurlencode(json_encode(array(
                
'error' => 'Maximum header size exceeded'
            
)));
        }

        
$chunks = array();

        while (
strlen($data) > $maxHeaderLength) {
            
$chunks[] = substr($data0$maxHeaderLength);
            
$data substr($data$maxHeaderLength);
        }
        
$chunks[] = $data;

        
$headers = array();
        for (
$i 0$c count($chunks); $i $c$i++) {
            
$name $headerName . ($i "-$i'');
            
$headers[$name] = $chunks[$i];
        }

        return 
$headers;
    }

    
/**
     * Sends the data through the HTTP headers
     *
     * @param bool $useOpenHandler
     * @param string $headerName
     * @param integer $maxHeaderLength
     * @return $this
     */
    
public function sendDataInHeaders($useOpenHandler null$headerName 'phpdebugbar'$maxHeaderLength 4096)
    {
        if (
$useOpenHandler === null) {
            
$useOpenHandler self::$useOpenHandlerWhenSendingDataHeaders;
        }
        if (
$useOpenHandler && $this->storage !== null) {
            
$this->getData();
            
$headerName .= '-id';
            
$headers = array($headerName => $this->getCurrentRequestId());
        } else {
            
$headers $this->getDataAsHeaders($headerName$maxHeaderLength);
        }
        
$this->getHttpDriver()->setHeaders($headers);
        return 
$this;
    }

    
/**
     * Stacks the data in the session for later rendering
     */
    
public function stackData()
    {
        
$http $this->initStackSession();

        
$data null;
        if (!
$this->isDataPersisted() || $this->stackAlwaysUseSessionStorage) {
            
$data $this->getData();
        } elseif (
$this->data === null) {
            
$this->collect();
        }

        
$stack $http->getSessionValue($this->stackSessionNamespace);
        
$stack[$this->getCurrentRequestId()] = $data;
        
$http->setSessionValue($this->stackSessionNamespace$stack);
        return 
$this;
    }

    
/**
     * Checks if there is stacked data in the session
     *
     * @return boolean
     */
    
public function hasStackedData()
    {
        try {
            
$http $this->initStackSession();
        } catch (
DebugBarException $e) {
            return 
false;
        }
        return 
count($http->getSessionValue($this->stackSessionNamespace)) > 0;
    }

    
/**
     * Returns the data stacked in the session
     *
     * @param boolean $delete Whether to delete the data in the session
     * @return array
     */
    
public function getStackedData($delete true)
    {
        
$http $this->initStackSession();
        
$stackedData $http->getSessionValue($this->stackSessionNamespace);
        if (
$delete) {
            
$http->deleteSessionValue($this->stackSessionNamespace);
        }

        
$datasets = array();
        if (
$this->isDataPersisted() && !$this->stackAlwaysUseSessionStorage) {
            foreach (
$stackedData as $id => $data) {
                
$datasets[$id] = $this->getStorage()->get($id);
            }
        } else {
            
$datasets $stackedData;
        }

        return 
$datasets;
    }

    
/**
     * Sets the key to use in the $_SESSION array
     *
     * @param string $ns
     * @return $this
     */
    
public function setStackDataSessionNamespace($ns)
    {
        
$this->stackSessionNamespace $ns;
        return 
$this;
    }

    
/**
     * Returns the key used in the $_SESSION array
     *
     * @return string
     */
    
public function getStackDataSessionNamespace()
    {
        return 
$this->stackSessionNamespace;
    }

    
/**
     * Sets whether to only use the session to store stacked data even
     * if a storage is enabled
     *
     * @param boolean $enabled
     * @return $this
     */
    
public function setStackAlwaysUseSessionStorage($enabled true)
    {
        
$this->stackAlwaysUseSessionStorage $enabled;
        return 
$this;
    }

    
/**
     * Checks if the session is always used to store stacked data
     * even if a storage is enabled
     *
     * @return boolean
     */
    
public function isStackAlwaysUseSessionStorage()
    {
        return 
$this->stackAlwaysUseSessionStorage;
    }

    
/**
     * Initializes the session for stacked data
     * @return HttpDriverInterface
     * @throws DebugBarException
     */
    
protected function initStackSession()
    {
        
$http $this->getHttpDriver();
        if (!
$http->isSessionStarted()) {
            throw new 
DebugBarException("Session must be started before using stack data in the debug bar");
        }

        if (!
$http->hasSessionValue($this->stackSessionNamespace)) {
            
$http->setSessionValue($this->stackSessionNamespace, array());
        }

        return 
$http;
    }

    
/**
     * Returns a JavascriptRenderer for this instance
     * @param string $baseUrl
     * @param string $basePath
     * @return JavascriptRenderer
     */
    
public function getJavascriptRenderer($baseUrl null$basePath null)
    {
        if (
$this->jsRenderer === null) {
            
$this->jsRenderer = new JavascriptRenderer($this$baseUrl$basePath);
        }
        return 
$this->jsRenderer;
    }

    
// --------------------------------------------
    // ArrayAccess implementation

    
public function offsetSet($key$value)
    {
        throw new 
DebugBarException("DebugBar[] is read-only");
    }

    public function 
offsetGet($key)
    {
        return 
$this->getCollector($key);
    }

    public function 
offsetExists($key)
    {
        return 
$this->hasCollector($key);
    }

    public function 
offsetUnset($key)
    {
        throw new 
DebugBarException("DebugBar[] is read-only");
    }
}