-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFirePHP.class.php4
More file actions
1359 lines (1186 loc) · 41.1 KB
/
FirePHP.class.php4
File metadata and controls
1359 lines (1186 loc) · 41.1 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// Authors:
// - cadorn, Christoph Dorn <christoph@christophdorn.com>, Copyright 2007, New BSD License
// - qbbr, Michael Day <manveru.alma@gmail.com>, Copyright 2008, New BSD License
// - cadorn, Christoph Dorn <christoph@christophdorn.com>, Copyright 2011, MIT License
// - kmcs, Timo Kiefer <timo.kiefer@kmcs.de>, Copyright 2012, MIT License
/**
* *** BEGIN LICENSE BLOCK *****
*
* [MIT License](http://www.opensource.org/licenses/mit-license.php)
*
* Copyright (c) 2007+ [Christoph Dorn](http://www.christophdorn.com/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ***** END LICENSE BLOCK *****
*
* This verion of FirePHPCore is for use with PHP4. If you do not require PHP4
* compatibility, it is suggested you use FirePHPCore.class.php instead.
*
* @copyright Copyright (C) 2007+ Christoph Dorn
* @author Christoph Dorn <christoph@christophdorn.com>
* @author Michael Day <manveru.alma@gmail.com>
* @license [MIT License](http://www.opensource.org/licenses/mit-license.php)
* @package FirePHPCore
*/
/**
* FirePHP version
*
* @var string
*/
define('FirePHP_VERSION', '0.3'); // @pinf replace '0.3' with '%%VERSION%%'
/**
* Firebug LOG level
*
* Logs a message to firebug console
*
* @var string
*/
define('FirePHP_LOG', 'LOG');
/**
* Firebug INFO level
*
* Logs a message to firebug console and displays an info icon before the message
*
* @var string
*/
define('FirePHP_INFO', 'INFO');
/**
* Firebug WARN level
*
* Logs a message to firebug console, displays a warning icon before the message and colors the line turquoise
*
* @var string
*/
define('FirePHP_WARN', 'WARN');
/**
* Firebug ERROR level
*
* Logs a message to firebug console, displays an error icon before the message and colors the line yellow. Also increments the firebug error count.
*
* @var string
*/
define('FirePHP_ERROR', 'ERROR');
/**
* Dumps a variable to firebug's server panel
*
* @var string
*/
define('FirePHP_DUMP', 'DUMP');
/**
* Displays a stack trace in firebug console
*
* @var string
*/
define('FirePHP_TRACE', 'TRACE');
/**
* Displays a table in firebug console
*
* @var string
*/
define('FirePHP_TABLE', 'TABLE');
/**
* Starts a group in firebug console
*
* @var string
*/
define('FirePHP_GROUP_START', 'GROUP_START');
/**
* Ends a group in firebug console
*
* @var string
*/
define('FirePHP_GROUP_END', 'GROUP_END');
/**
* Sends the given data to the FirePHP Firefox Extension.
* The data can be displayed in the Firebug Console or in the
* "Server" request tab.
*
* For more information see: http://www.firephp.org/
*
* @copyright Copyright (C) 2007+ Christoph Dorn
* @author Christoph Dorn <christoph@christophdorn.com>
* @author Michael Day <manveru.alma@gmail.com>
* @license [MIT License](http://www.opensource.org/licenses/mit-license.php)
* @package FirePHPCore
*/
class FirePHP {
/**
* Wildfire protocol message index
*
* @var int
*/
var $messageIndex = 1;
/**
* Options for the library
*
* @var array
*/
var $options = array('maxObjectDepth' => 5,
'maxArrayDepth' => 5,
'useNativeJsonEncode' => true,
'includeLineNumbers' => true,
'useGzipEncode' => false);
/**
* Filters used to exclude object members when encoding
*
* @var array
*/
var $objectFilters = array();
/**
* A stack of objects used to detect recursion during object encoding
*
* @var object
*/
var $objectStack = array();
/**
* Flag to enable/disable logging
*
* @var boolean
*/
var $enabled = true;
/**
* sent byes
* @var integer
*/
var $sentBytes = 0;
/**
* max sent bytes
* @var integer
*/
var $maxBytesToSent = 32000; //~32k
/**
* The object constructor
*/
function FirePHP() {
}
/**
* When the object gets serialized only include specific object members.
*
* @return array
*/
function __sleep() {
return array('options','objectFilters','enabled');
}
/**
* Gets singleton instance of FirePHP
*
* @param boolean $AutoCreate
* @return FirePHP
*/
function &getInstance($AutoCreate=false) {
global $FirePHP_Instance;
if($AutoCreate===true && !$FirePHP_Instance) {
$FirePHP_Instance = new FirePHP();
}
return $FirePHP_Instance;
}
/**
* Enable and disable logging to Firebug
*
* @param boolean $Enabled TRUE to enable, FALSE to disable
* @return void
*/
function setEnabled($Enabled) {
$this->enabled = $Enabled;
}
/**
* Check if logging is enabled
*
* @return boolean TRUE if enabled
*/
function getEnabled() {
return $this->enabled;
}
/**
* Specify a filter to be used when encoding an object
*
* Filters are used to exclude object members.
*
* @param string $Class The class name of the object
* @param array $Filter An array of members to exclude
* @return void
*/
function setObjectFilter($Class, $Filter) {
$this->objectFilters[strtolower($Class)] = $Filter;
}
/**
* Set some options for the library
*
* Options:
* - maxObjectDepth: The maximum depth to traverse objects (default: 5)
* - maxArrayDepth: The maximum depth to traverse arrays (default: 5)
* - useNativeJsonEncode: If true will use json_encode() (default: true)
* - includeLineNumbers: If true will include line numbers and filenames (default: true)
*
* @param array $Options The options to be set
* @return void
*/
function setOptions($Options) {
$this->options = array_merge($this->options,$Options);
}
/**
* Get options from the library
*
* @return array The currently set options
*/
function getOptions() {
return $this->options;
}
/**
* Register FirePHP as your error handler
*
* Will use FirePHP to log each php error.
*
* @return mixed Returns a string containing the previously defined error handler (if any)
*/
function registerErrorHandler()
{
//NOTE: The following errors will not be caught by this error handler:
// E_ERROR, E_PARSE, E_CORE_ERROR,
// E_CORE_WARNING, E_COMPILE_ERROR,
// E_COMPILE_WARNING, E_STRICT
return set_error_handler(array($this,'errorHandler'));
}
/**
* FirePHP's error handler
*
* Logs each php error that will occur.
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @param array $errcontext
*/
function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
{
global $FirePHP_Instance;
// Don't log error if error reporting is switched off
if (error_reporting() == 0) {
return;
}
// Only log error for errors we are asking for
if (error_reporting() & $errno) {
$FirePHP_Instance->group($errstr);
$FirePHP_Instance->error("{$errfile}, line $errline");
$FirePHP_Instance->groupEnd();
}
}
/**
* Register FirePHP driver as your assert callback
*
* @return mixed Returns the original setting
*/
function registerAssertionHandler()
{
return assert_options(ASSERT_CALLBACK, array($this, 'assertionHandler'));
}
/**
* FirePHP's assertion handler
*
* Logs all assertions to your firebug console and then stops the script.
*
* @param string $file File source of assertion
* @param int $line Line source of assertion
* @param mixed $code Assertion code
*/
function assertionHandler($file, $line, $code)
{
$this->fb($code, 'Assertion Failed', FirePHP_ERROR, array('File'=>$file,'Line'=>$line));
}
/**
* Set custom processor url for FirePHP
*
* @param string $URL
*/
function setProcessorUrl($URL)
{
$this->setHeader('X-FirePHP-ProcessorURL', $URL);
}
/**
* Set custom renderer url for FirePHP
*
* @param string $URL
*/
function setRendererUrl($URL)
{
$this->setHeader('X-FirePHP-RendererURL', $URL);
}
/**
* Start a group for following messages.
*
* Options:
* Collapsed: [true|false]
* Color: [#RRGGBB|ColorName]
*
* @param string $Name
* @param array $Options OPTIONAL Instructions on how to log the group
* @return true
* @throws Exception
*/
function group($Name, $Options=null) {
if(!$Name) {
trigger_error('You must specify a label for the group!');
}
if($Options) {
if(!is_array($Options)) {
trigger_error('Options must be defined as an array!');
}
if(array_key_exists('Collapsed', $Options)) {
$Options['Collapsed'] = ($Options['Collapsed'])?'true':'false';
}
}
return $this->fb(null, $Name, FirePHP_GROUP_START, $Options);
}
/**
* Ends a group you have started before
*
* @return true
* @throws Exception
*/
function groupEnd() {
return $this->fb(null, null, FirePHP_GROUP_END);
}
/**
* Log object with label to firebug console
*
* @see FirePHP::LOG
* @param mixes $Object
* @param string $Label
* @return true
* @throws Exception
*/
function log($Object, $Label=null) {
return $this->fb($Object, $Label, FirePHP_LOG);
}
/**
* Log object with label to firebug console
*
* @see FirePHP::INFO
* @param mixes $Object
* @param string $Label
* @return true
* @throws Exception
*/
function info($Object, $Label=null) {
return $this->fb($Object, $Label, FirePHP_INFO);
}
/**
* Log object with label to firebug console
*
* @see FirePHP::WARN
* @param mixes $Object
* @param string $Label
* @return true
* @throws Exception
*/
function warn($Object, $Label=null) {
return $this->fb($Object, $Label, FirePHP_WARN);
}
/**
* Log object with label to firebug console
*
* @see FirePHP::ERROR
* @param mixes $Object
* @param string $Label
* @return true
* @throws Exception
*/
function error($Object, $Label=null) {
return $this->fb($Object, $Label, FirePHP_ERROR);
}
/**
* Dumps key and variable to firebug server panel
*
* @see FirePHP::DUMP
* @param string $Key
* @param mixed $Variable
* @return true
* @throws Exception
*/
function dump($Key, $Variable) {
return $this->fb($Variable, $Key, FirePHP_DUMP);
}
/**
* Log a trace in the firebug console
*
* @see FirePHP::TRACE
* @param string $Label
* @return true
* @throws Exception
*/
function trace($Label) {
return $this->fb($Label, FirePHP_TRACE);
}
/**
* Log a table in the firebug console
*
* @see FirePHP::TABLE
* @param string $Label
* @param string $Table
* @return true
* @throws Exception
*/
function table($Label, $Table) {
return $this->fb($Table, $Label, FirePHP_TABLE);
}
/**
* Check if FirePHP is installed on client
*
* @return boolean
*/
function detectClientExtension() {
// Check if FirePHP is installed on client via User-Agent header
if(@preg_match_all('/\sFirePHP\/([\.\d]*)\s?/si',$this->getUserAgent(),$m) &&
version_compare($m[1][0],'0.0.6','>=')) {
if(!version_compare($m[1][0], '0.7', '>=')) {
$this->setOption('useGzipEncode', false);
}
return true;
} else
// Check if FirePHP is installed on client via X-FirePHP-Version header
if(@preg_match_all('/^([\.\d]*)$/si',$this->getRequestHeader("X-FirePHP-Version"),$m) &&
version_compare($m[1][0],'0.0.6','>=')) {
return true;
}
return false;
}
/**
* Log varible to Firebug
*
* @see http://www.firephp.org/Wiki/Reference/Fb
* @param mixed $Object The variable to be logged
* @return true Return TRUE if message was added to headers, FALSE otherwise
* @throws Exception
*/
function fb($Object) {
if(!$this->enabled) {
return false;
}
if (headers_sent($filename, $linenum)) {
trigger_error('Headers already sent in '.$filename.' on line '.$linenum.'. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.');
}
$Type = null;
$Label = null;
$Options = array();
if(func_num_args()==1) {
} else
if(func_num_args()==2) {
switch(func_get_arg(1)) {
case FirePHP_LOG:
case FirePHP_INFO:
case FirePHP_WARN:
case FirePHP_ERROR:
case FirePHP_DUMP:
case FirePHP_TRACE:
case FirePHP_TABLE:
case FirePHP_GROUP_START:
case FirePHP_GROUP_END:
$Type = func_get_arg(1);
break;
default:
$Label = func_get_arg(1);
break;
}
} else
if(func_num_args()==3) {
$Type = func_get_arg(2);
$Label = func_get_arg(1);
} else
if(func_num_args()==4) {
$Type = func_get_arg(2);
$Label = func_get_arg(1);
$Options = func_get_arg(3);
} else {
trigger_error('Wrong number of arguments to fb() function!');
}
if(!$this->detectClientExtension()) {
return false;
}
$meta = array();
$skipFinalObjectEncode = false;
if($Type==FirePHP_TRACE) {
$trace = debug_backtrace();
if(!$trace) return false;
for( $i=0 ; $i<sizeof($trace) ; $i++ ) {
if(isset($trace[$i]['class'])
&& isset($trace[$i]['file'])
&& ($trace[$i]['class']=='FirePHP'
|| $trace[$i]['class']=='FB')
&& (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php'
|| substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) {
/* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */
} else
if(isset($trace[$i]['class'])
&& isset($trace[$i+1]['file'])
&& $trace[$i]['class']=='FirePHP'
&& substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') {
/* Skip fb() */
} else
if($trace[$i]['function']=='fb'
|| $trace[$i]['function']=='trace'
|| $trace[$i]['function']=='send') {
$Object = array('Class'=>isset($trace[$i]['class'])?$trace[$i]['class']:'',
'Type'=>isset($trace[$i]['type'])?$trace[$i]['type']:'',
'Function'=>isset($trace[$i]['function'])?$trace[$i]['function']:'',
'Message'=>$trace[$i]['args'][0],
'File'=>isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'',
'Line'=>isset($trace[$i]['line'])?$trace[$i]['line']:'',
'Args'=>isset($trace[$i]['args'])?$this->encodeObject($trace[$i]['args']):'',
'Trace'=>$this->_escapeTrace(array_splice($trace,$i+1)));
$skipFinalObjectEncode = true;
$meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'';
$meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:'';
break;
}
}
} else
if($Type==FirePHP_TABLE) {
if(isset($Object[0]) && is_string($Object[0])) {
$Object[1] = $this->encodeTable($Object[1]);
} else {
$Object = $this->encodeTable($Object);
}
$skipFinalObjectEncode = true;
} else
if($Type==FirePHP_GROUP_START) {
if(!$Label) {
trigger_error('You must specify a label for the group!');
}
} else {
if($Type===null) {
$Type = FirePHP_LOG;
}
}
if($this->options['includeLineNumbers']) {
if(!isset($meta['file']) || !isset($meta['line'])) {
$trace = debug_backtrace();
for( $i=0 ; $trace && $i<sizeof($trace) ; $i++ ) {
if(isset($trace[$i]['class'])
&& isset($trace[$i]['file'])
&& ($trace[$i]['class']=='FirePHP'
|| $trace[$i]['class']=='FB'
|| $trace[$i]['class']==__CLASS__)
&& (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php'
|| substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php'
|| $this->_standardizePath($trace[$i]['file'])==__FILE__)) {
/* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */
} else
if(isset($trace[$i]['class'])
&& isset($trace[$i+1]['file'])
&& $trace[$i]['class']=='FirePHP'
&& substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') {
/* Skip fb() */
} else
if(isset($trace[$i]['file'])
&& substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php') {
/* Skip FB::fb() */
} else {
$meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'';
$meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:'';
break;
}
}
}
} else {
unset($meta['file']);
unset($meta['line']);
}
$this->setHeader('X-Wf-Protocol-1','http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
if($this->options['useGzipEncode'] && function_exists('gzencode')) {
$this->setHeader('X-Wf-Option-gzip', 'true');
}
$this->setHeader('X-Wf-1-Plugin-1','http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/'.FirePHP_VERSION);
$structure_index = 1;
if($Type==FirePHP_DUMP) {
$structure_index = 2;
$this->setHeader('X-Wf-1-Structure-2','http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1');
} else {
$this->setHeader('X-Wf-1-Structure-1','http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
}
if($Type==FirePHP_DUMP) {
$msg = '{"'.$Label.'":'.$this->jsonEncode($Object, $skipFinalObjectEncode).'}';
} else {
$msg_meta = $Options;
$msg_meta['Type'] = $Type;
if($Label!==null) {
$msg_meta['Label'] = $Label;
}
if(isset($meta['file']) && !isset($msg_meta['File'])) {
$msg_meta['File'] = $meta['file'];
}
if(isset($meta['line']) && !isset($msg_meta['Line'])) {
$msg_meta['Line'] = $meta['line'];
}
$msg = '['.$this->jsonEncode($msg_meta).','.$this->jsonEncode($Object, $skipFinalObjectEncode).']';
}
if($this->options['useGzipEncode'] && function_exists('gzencode')) {
$msg = base64_encode(gzencode($msg));
}
if($this->maxBytesToSent < ($this->sentBytes + strlen($msg))) {
return;
}
$parts = explode("\n",chunk_split($msg, 5000, "\n"));
for( $i=0 ; $i<count($parts) ; $i++) {
$part = $parts[$i];
if ($part) {
if(count($parts)>2) {
// Message needs to be split into multiple parts
$this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex,
(($i==0)?strlen($msg):'')
. '|' . $part . '|'
. (($i<count($parts)-2)?'\\':''));
} else {
$this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex,
strlen($part) . '|' . $part . '|');
}
$this->messageIndex++;
if ($this->messageIndex > 99999) {
trigger_error('Maximum number (99,999) of messages reached!');
}
}
}
$this->sentBytes += strlen($msg);
$this->setHeader('X-Wf-1-Index',$this->messageIndex-1);
return true;
}
/**
* Standardizes path for windows systems.
*
* @param string $Path
* @return string
*/
function _standardizePath($Path) {
return preg_replace('/\\\\+/','/',$Path);
}
/**
* Escape trace path for windows systems
*
* @param array $Trace
* @return array
*/
function _escapeTrace($Trace) {
if(!$Trace) return $Trace;
for( $i=0 ; $i<sizeof($Trace) ; $i++ ) {
if(isset($Trace[$i]['file'])) {
$Trace[$i]['file'] = $this->_escapeTraceFile($Trace[$i]['file']);
}
if(isset($Trace[$i]['args'])) {
$Trace[$i]['args'] = $this->encodeObject($Trace[$i]['args']);
}
}
return $Trace;
}
/**
* Escape file information of trace for windows systems
*
* @param string $File
* @return string
*/
function _escapeTraceFile($File) {
/* Check if we have a windows filepath */
if(strpos($File,'\\')) {
/* First strip down to single \ */
$file = preg_replace('/\\\\+/','\\',$File);
return $file;
}
return $File;
}
/**
* Send header
*
* @param string $Name
* @param string_type $Value
*/
function setHeader($Name, $Value) {
return header($Name.': '.$Value);
}
/**
* Get user agent
*
* @return string|false
*/
function getUserAgent() {
if(!isset($_SERVER['HTTP_USER_AGENT'])) return false;
return $_SERVER['HTTP_USER_AGENT'];
}
/**
* Get all request headers
*
* @return array
*/
function getAllRequestHeaders() {
$headers = array();
if(function_exists('getallheaders')) {
foreach( getallheaders() as $name => $value ) {
$headers[strtolower($name)] = $value;
}
} else {
foreach($_SERVER as $name => $value) {
if(substr($name, 0, 5) == 'HTTP_') {
$headers[strtolower(str_replace(' ', '-', str_replace('_', ' ', substr($name, 5))))] = $value;
}
}
}
return $headers;
}
/**
* Get a request header
*
* @return string|false
*/
function getRequestHeader($Name)
{
$headers = $this->getAllRequestHeaders();
if (isset($headers[strtolower($Name)])) {
return $headers[strtolower($Name)];
}
return false;
}
/**
* Encode an object into a JSON string
*
* Uses PHP's jeson_encode() if available
*
* @param object $Object The object to be encoded
* @return string The JSON string
*/
function jsonEncode($Object, $skipObjectEncode=false)
{
if(!$skipObjectEncode) {
$Object = $this->encodeObject($Object);
}
if(function_exists('json_encode')
&& $this->options['useNativeJsonEncode']!=false) {
return json_encode($Object);
} else {
return $this->json_encode($Object);
}
}
/**
* Encodes a table by encoding each row and column with encodeObject()
*
* @param array $Table The table to be encoded
* @return array
*/
function encodeTable($Table) {
if(!$Table) return $Table;
$new_table = array();
foreach($Table as $row) {
if(is_array($row)) {
$new_row = array();
foreach($row as $item) {
$new_row[] = $this->encodeObject($item);
}
$new_table[] = $new_row;
}
}
return $new_table;
}
/**
* Encodes an object
*
* @param Object $Object The object to be encoded
* @param int $Depth The current traversal depth
* @return array All members of the object
*/
function encodeObject($Object, $ObjectDepth = 1, $ArrayDepth = 1)
{
$return = array();
if (is_resource($Object)) {
return '** '.(string)$Object.' **';
} else
if (is_object($Object)) {
if ($ObjectDepth > $this->options['maxObjectDepth']) {
return '** Max Object Depth ('.$this->options['maxObjectDepth'].') **';
}
foreach ($this->objectStack as $refVal) {
if ($refVal === $Object) {
return '** Recursion ('.get_class($Object).') **';
}
}
array_push($this->objectStack, $Object);
$return['__className'] = $class = get_class($Object);
$class_lower = strtolower($class);
$members = (array)$Object;
// Include all members that are not defined in the class
// but exist in the object
foreach( $members as $raw_name => $value ) {
$name = $raw_name;
if ($name{0} == "\0") {
$parts = explode("\0", $name);
$name = $parts[2];
}
if(!isset($properties[$name])) {
$name = 'undeclared:'.$name;
if(!(isset($this->objectFilters[$class_lower])
&& is_array($this->objectFilters[$class_lower])
&& in_array($raw_name,$this->objectFilters[$class_lower]))) {
$return[$name] = $this->encodeObject($value, $ObjectDepth + 1, 1);
} else {
$return[$name] = '** Excluded by Filter **';
}
}
}
array_pop($this->objectStack);
} elseif (is_array($Object)) {
if ($ArrayDepth > $this->options['maxArrayDepth']) {
return '** Max Array Depth ('.$this->options['maxArrayDepth'].') **';
}
foreach ($Object as $key => $val) {
// Encoding the $GLOBALS PHP array causes an infinite loop
// if the recursion is not reset here as it contains
// a reference to itself. This is the only way I have come up
// with to stop infinite recursion in this case.
if($key=='GLOBALS'
&& is_array($val)
&& array_key_exists('GLOBALS',$val)) {
$val['GLOBALS'] = '** Recursion (GLOBALS) **';
}
$return[$key] = $this->encodeObject($val, 1, $ArrayDepth + 1);
}
} else {
if($this->is_utf8($Object)) {
return $Object;
} else {
return utf8_encode($Object);
}
}
return $return;
}
/**
* Returns true if $string is valid UTF-8 and false otherwise.
*
* @param mixed $str String to be tested
* @return boolean
*/