-
Notifications
You must be signed in to change notification settings - Fork 3
/
phpLive.php
2859 lines (2637 loc) · 94.2 KB
/
phpLive.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
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
/*
* phpLive PHP Library v1.0.0-alpha
* http://phplive.org/
*
* To see the documentation, visit:
* http://phplive.org/docs
*
* To report/view bugs, visit:
* http://bugs.phplive.org
*
* Copyright 2011, Ryan Naddy
* GPL Version 3 licenses.
* http://www.gnu.org/licenses/gpl-3.0.html
*
* Date: Thu Jan 06 2012
*/
/*
* Define some constants
*/
// List of extraction constants
define("EXTRACT_SYMBOL", 1);
define("EXTRACT_NUMBER", 2);
define("EXTRACT_LETTER", 3);
define("EXTRACT_UPPER", 4);
define("EXTRACT_LOWER", 5);
define("EXTRACT_PHONE", 6);
define("EXTRACT_EMAIL", 7);
define("EXTRACT_LINKS", 8);
define("EXTRACT_NUMBER_LETTER", 9);
// List of removal constants
define("REMOVE_SYMBOL", 1);
define("REMOVE_WHITE_SPACE", 2);
define("REMOVE_NUMBER", 3);
define("REMOVE_LETTER", 4);
define("REMOVE_UPPER", 5);
define("REMOVE_LOWER", 6);
// List of validation constants
define("VALID_EMAIL", 1);
define("VALID_DATE", 2);
define("VALID_TIME", 3);
define("VALID_DATETIME", 4);
define("VALID_URL", 5);
// List of return type constants
define("RETURN_INT", 1);
define("RETURN_ARRAY", 2);
define("RETURN_OBJECT", 3);
define("RETURN_STRING", 4);
define("RETURN_BOOL", 5);
// List of data constants
define("DATA_HTML", 1);
define("DATA_CLEAN", 2);
// List of input types
define("INPUT_STRING", 1);
define("INPUT_FILE", 2);
define("MAIL_HTML", 1);
define("MAIL_ATTACHMENT", 2);
define("MAIL_PLAIN", 3);
define("DOWNLOAD_FILE", 1);
define("DOWNLOAD_STRING", 2);
define("PHP_TMPFILE", 1);
define("ZIP_NEW", 1);
define("HIGHLIGHT_PHP", 1);
define("HIGHLIGHT_HTML", 2);
define("HIGHLIGHT_CSS", 3);
define("IMAGE_JPG", 1);
define("IMAGE_PNG", 2);
define("IMAGE_GIF", 3);
// Begin phpLive Class
class phpLive{
// Protected Properties
protected
$phpLiveDomain = 'http://www.phplive.org',
$version = "1.0.0-alpha",
$location = '',
$images = array("jpg", "jpeg", "gif", "png"),
$conn_id = 0,
$exit = array(),
$threads = array(),
$thread_count = 0,
$errors = array(),
$filename = '',
$filebasename = "",
$newFilename = '',
$phones = array("lg-", "htc", "sie", "mot-", "iphone", "android", "webos", "blackberry", "ipod", "nokia", "samsung", "sonyericsson"),
$sockets = array(),
$sockId = 0,
$clients = array(),
$zip = array(),
$zipId = 0,
$memorySize = 2,
$tmpFile = "";
// Private Read-Only Properties
private
$url, $ch, $links, $cleanData, $info, $title, $endingUrl, $httpCode, $loadTime,
$processing = false, $urlQuery,
$extension = array();
// Public Properties
public
$thumbDir,
$content,
$colors = array("#ffffff", "#eeeeee"),
$coreLoaded = false,
$db = array(),
$port = 80,
$host = 'localhost',
$list = array(),
$post = array(),
$functionName = null,
$string = "";
public function __construct(){
$this->location = dirname(__FILE__);
parse_str($this->qString(), $this->urlQuery);
}
public function __get($name){
$this->location = dirname(__FILE__);
switch($name){
case 'links': $ret = $this->links;
break;
case 'location':
if(empty($this->location))
$ret = $this->location;
break;
case 'data': $ret = $this->content;
break;
case 'url': $ret = $this->url;
break;
case 'info': $ret = $this->info;
break;
case 'endingUrl': $ret = $this->endingUrl;
break;
case 'httpCode': $ret = $this->httpCode;
break;
case 'loadTime': $ret = $this->loadTime;
break;
case 'cleanData': $ret = $this->cleanData;
break;
case 'title': $ret = $this->title;
break;
case 'exit': $ret = $this->exit;
break;
case 'processing': $this->poll();
$ret = $this->processing;
break;
case 'errors': $ret = $this->errors;
break;
case 'urlQuery': $ret = $this->urlQuery;
break;
default:
if(array_key_exists($name, $this->extension)){
$ret = $this->extension[$name];
}else{
$ret = false;
}
break;
}
return $ret;
}
public function __set($name, $value){
$this->extension[$name] = $value;
}
public function __call($name, $args){
if(isset($this->$name) && is_callable($this->$name)){
return call_user_func_array($this->$name, $args);
}
if(isset($this->extension[$name]) && is_callable($this->extension[$name])){
return call_user_func_array($this->extension[$name], $args);
}
throw new BadFunctionCallException(sprintf('Undefined function %s.', $name));
}
public function __tostring(){
$opt = "";
switch($this->functionName){
case "each":
if(is_array($this->list))
$opt = implode("", $this->list);
else
$opt = $opt = $this->string;
break;
case "qRemove":
case "qAdd":
$opt = http_build_query($this->urlQuery);
parse_str($this->qString(), $this->urlQuery);
break;
default:
$opt = $this->string;
break;
}
return (string)$opt;
}
public function toString(){
return $this->__tostring();
}
/**
*
* @param string $string
* @return \phpLive
*
* Converts an input string to lowercase.
* If no input string is given, use the global string
*/
public function toLower($string = null){
if(is_string($string))
$this->string = $string;
$this->string = strtolower($this->string);
$this->functionName = __FUNCTION__;
return $this;
}
/**
*
* @param string $string
* @return \phpLive
*
* Converts an input string to uppercase
* If no input string is given, use the global string
*/
public function toUpper($string = null){
if(is_string($string))
$this->string = $string;
$this->string = strtoupper($this->string);
$this->functionName = __FUNCTION__;
return $this;
}
/**
*
* @return int
*
* Takes the gloabl string and retuns it as an int
*/
public function toInt(){
return (int)$this->__tostring();
}
/**
*
* @return boolean
*
* Takes the global string and returns it as a boolean
*/
public function toBool(){
return (bool)$this->__tostring();
}
/**
*
* @param string $string
* @return string
*
*
*/
public function string(&$string = null){
if($string != null)
$string = $string;
return $this->string;
}
/**
* @return string
*
* Returns the current phpLive version
*/
public function version(){
$this->functionName = __FUNCTION__;
echo $this->version;
}
/**
*
* @return string
*
* Gets the class that called this method
*/
public function getCalledClass(){
if(function_exists('get_called_class')){
return get_called_class();
}
$t = debug_backtrace();
$t = $t[0];
$this->functionName = __FUNCTION__;
if(isset($t['object']) && $t['object'] instanceof $t['class'])
return get_class($t['object']);
return "";
}
/**
* Not in use
*
* Should return which plugin called this method
*/
public function getCalledPlugin(){
$this->getCalledClass();
$this->allPluginSettings();
//var_dump(get_class());
print_r(debug_backtrace());
}
/**
*
* @param object $class
* @return \phpLive
*
* Gets the number of methods within an object
*/
public function numMethods($class){
$this->functionName = __FUNCTION__;
$this->string = count(get_class_methods($class));
return $this;
}
/**
*
* @param string $class
* @param array $info
* @return boolean
*
* Giving a class name and ini settings for a plugin
* This class will load the desired class into phpLive.
* The class is then saved as so:
* $this->$instance = new $class();
*/
public function loadPlugin($class, $info){
$this->functionName = __FUNCTION__;
$info = (object)$info;
$file = $this->location . "/plugins/" . $info->root . "/" . $info->fileName;
if(is_file($file)){
require_once $file;
$instance = (string)$info->instanceName;
$info = (isset($info->information)) ? $info->information : "";
//$this->$instance = new $class($info);
$reflection = new ReflectionClass($class);
$this->$instance = $reflection->newInstanceArgs(array($info));
$this->extension[$instance] = $this->$instance;
return $this->$instance;
}
return false;
}
/**
*
* @param string|array $loadPlugins
* @param string $separator
* @return boolean
*
* If a string is given it converts it to an array using a comma separator by default
* Once the array is passed in or built it loads the plugin using $this->loadPlugin()
*/
public function loadPlugins($loadPlugins = null, $separator = ","){
if(is_string($loadPlugins)){
$loadPlugins = explode($separator, $loadPlugins);
}
if(!is_array($loadPlugins) && $loadPlugins != null){
return false;
}
$ini = $this->allPluginSettings();
if($ini){
if($loadPlugins === null){
foreach($ini as $sectionClass => $section){
$this->loadPlugin($sectionClass, $section);
}
}else{
foreach($loadPlugins as $sectionClass){
$section = $ini[trim($sectionClass)];
$this->loadPlugin($sectionClass, $section);
}
}
$this->functionName = __FUNCTION__;
return true;
}
$this->functionName = __FUNCTION__;
return false;
}
/**
*
* @return boolean
*
* Gets the settings for all plugins
*/
public function allPluginSettings(){
$this->functionName = __FUNCTION__;
if(empty($this->location))
$this->location = dirname(__FILE__);
$file = $this->location . "/plugins/plugins.php";
if(is_file($file)){
//return parse_ini_file($file, true);
require_once $file;
return $plugins;
}
else
return false;
}
/**
*
* @param string $class
* @param boolean $return_object
* @return object|array
*/
public function pluginSettings($class = null, $return_object = true){
$ini = $this->allPluginSettings();
if($class === null)
$class = $this->getCalledClass();
$this->functionName = __FUNCTION__;
if($return_object)
return (object)$ini[$class];
else
return $ini[$class];
}
/* public function extend($callback){
$object = (object)$callback;
var_dump(get_class_vars($object));
} */
/**
*
* @param string $url
* @param type $other_params
* @return \phpLive
*
* Gets a page from the web, and saves information into its parameters.
*
* $this->endingUrl the final url when the page was loaded (This can be different from your start url due to http redirects)
* $this->httpCode is the code in wich it returned (200, 400, 500, etc)
* $this->loadTime is the time it took for the page to load fully
* $this->title the title of the page
* $this->content the html content of the page
* $this->cleanData the cleaned up version of content
* $this->links an array of links (other than javascript links) that were found on the page
*/
public function getHttp($url = null, $other_params = null){
if($url === null){
if(filter_var($this->string, FILTER_VALIDATE_URL)){
$this->url = $this->string;
}
}else{
$this->url = $url;
}
$url = $this->url;
$this->filebasename = basename($url);
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
if(!empty($this->post)){
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->post);
}
if(isset($_SERVER['HTTP_USER_AGENT']))
curl_setopt($this->ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
else
curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatable; phpLiveBot/$this->version; +$this->phpLiveDomain)");
if(is_array($other_params)){
foreach($other_params as $key => $val){
curl_setopt($this->ch, $key, $val);
}
}
$this->content = curl_exec($this->ch);
$this->info = (object)curl_getinfo($this->ch);
$this->endingUrl = $this->info->url;
$this->httpCode = $this->info->http_code;
$this->loadTime = $this->info->total_time;
curl_close($this->ch);
$this->string = $this->content;
$this->functionName = __FUNCTION__;
return $this;
}
/**
*
* @param string $data
* @return \phpLive
*
* Cleans an html page leaving only the text that is displayed on the page.
* If no input data is given use the global string
*/
public function getCleanData($data = null){
if($data === null){
$data = $this->string;
}
$clean = preg_replace("/\<(script|style).*\>.*\<\/(script|style)\>/isU", " ", $data);
$clean = preg_replace("/\<title.*\>.*\<\/title\>/isU", " ", $clean);
$clean = preg_replace("/\<\!--.*\--\>/isU", " ", $clean);
$clean = preg_replace("/\</isU", " <", $clean);
$clean = strip_tags($clean);
$clean = preg_replace("/(\r|\n|\t)/", " ", $clean);
$clean = preg_replace("/\s\s+/", " ", $clean);
$clean = $this->convertSmart($clean);
$clean = trim($clean);
$this->cleanData = $clean;
$this->string = $clean;
$this->functionName = __FUNCTION__;
return $this;
}
/**
*
* @param string $data
* @return boolean|\phpLive
*
* Gets the title from an HTML string.
* If no data is pass in, use the global string
*/
public function getTitle($data = null){
if($data === null){
$data = $this->string;
}
preg_match("/\<title.*\>(.*)\<\/title\>/isU", $data, $matches);
if(isset($matches[1]))
$this->title = $matches[1];
else
return false;
$this->list = $this->title;
$this->string = $this->title;
$this->functionName = __FUNCTION__;
return $this;
}
/*
* get_urls will find all link tags and return their href attribues
* if $data is a string, the new internal data value will be set to that, and this method will search that
*/
/**
*
* @param string $data
* @return \phpLive
*
* Gets all the non JavaScript links on the page
* If no data is passed, use the global string
*/
public function getLinks($data = null){
if($data === null){
$data = $this->string;
}
preg_match_all("/\<a.+?href=(\"|')(?!javascript:|#)(.+?)(\"|')/i", $data, $matches);
$this->links = $matches[2];
$this->list = $this->links;
$this->functionName = __FUNCTION__;
return $this;
}
/**
*
* @param string $format
* @return \phpLive
*
* Make a string based on the format provided
* %x = Unix Timestamp
* accepts php dates (Y-m-d) prefixed with a % (example: %Y-%m-%d)
* rand(#) = a random set of characters a-zA-Z0-9 (example: rand(5) = j4Bn6)
* /hash = This comes very last. replace hash with the hash of your choosing (example: rand(5)%x/md5 = c5d45fc6350db247f9d3de8e6ec8d98e)
*/
public function mkstr($format){
$name = preg_replace("/%x/", time(), $format);
$name = preg_replace("/(%([a-zA-Z])){1}/e", 'date("$2")', $name);
if(preg_match("/rand\(([0-9]+)\)/", $name, $matches)){
$total = $matches[1];
$str = "";
$letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
for($i = 0; $i < $total; $i++){
$pos = mt_rand(0, strlen($letters));
$str .= $letters[$pos];
}
$name = preg_replace("/rand\(([0-9]+)\)/", $str, $name);
}
if(preg_match("/\/(.+)$/", $name, $matches)){
$type = $matches[1];
if(in_array($type, hash_algos())){
eval('$name = hash("' . $type . '", $name);');
}
}
$this->string = $name;
$this->functionName = __FUNCTION__;
return $this;
}
public function jsonEncode(){
$this->string = json_encode($this->list);
return $this;
}
public function jsonDecode(){
$this->list = json_decode($this->string);
return $this;
}
/**
*
* @param mixed $content
* @param string $type
* @return \phpLive
*
* convert a string to a to a hash value
*/
public function hash($content = null, $type = 'md5'){
$this->functionName = __FUNCTION__;
if($content === null)
$content = $this->string;
if(in_array($type, hash_algos())){
$this->string = hash($type, $content);
}
return $this;
}
/**
*
* @param string $start
* @param string $end
* @param string $data
* @param long $dataType
* @param boolean $include_start_end
* @param boolean $htmlFormat
* @return \phpLive
*
* Puts a list of found values that were found between start and end into the global list
*/
public function getBetween($start, $end, $data = null, $dataType = DATA_HTML, $include_start_end = false, $htmlFormat = false){
$this->functionName = __FUNCTION__;
$start = preg_quote($start, "/");
$end = preg_quote($end, "/");
if($data === null){
if($dataType == DATA_CLEAN)
$data = $this->cleanData;
elseif($dataType == DATA_HTML)
$data = $this->content;
else
return $this;
}
$good = (bool)preg_match_all($search = "/$start(.+)$end/uisU", $data, $matches);
$ret = array();
if($good){
if(!$htmlFormat){
if($include_start_end){
foreach($matches[0] as $start_end){
$ret[] = htmlentities($start_end);
}
}else{
foreach($matches[1] as $no_start_end){
$ret[] = htmlentities($no_start_end);
}
}
}else{
if(!$include_start_end){
foreach($matches[0] as $start_end){
$ret[] = $start_end;
}
}else{
foreach($matches[1] as $no_start_end){
$ret[] = $no_start_end;
}
}
}
$this->list = $ret;
return $this;
}
return $this;
}
/**
*
* @param string $glue
* @param array $array
* @return \phpLive
*
* Exact same thing as implode()
* If no array is given, use the global list
*/
public function implode($glue = " ", $array = null){
if($array === null)
$array = $this->list;
if(!is_array($array))
return $this;
$this->string = implode($glue, $array);
$this->functionName = __FUNCTION__;
return $this;
}
/**
*
* @param int $length
* @param type $difference
* @param type $end
* @param type $string
* @return \phpLive
*/
public function maxlen($length, $difference = 3, $end = "...", $string = null){
if($string === null)
$string = $this->string;
$length = (int)$length;
$words = explode(" ", $string);
$numwords = count($words);
$diff = $numwords - $length;
$i = 0;
$new = array();
while($i < $length){
$new[] = $words[$i];
$i++;
}
if($diff <= $difference){
$c = 1;
while($c <= $difference){
$new[] = $words[$i + $c];
$c++;
}
$length += $difference;
}
$this->string = implode(" ", $new);
if($length < $numwords)
$this->string .= $end;
return $this;
}
/**
*
* @param string $string
* @param string $spaces
* @return \phpLive
*
* Converts spaces to tabs
*/
public function spToTab($spaces = 4, $string = null){
if($string === null)
$string = $this->string;
$this->string = preg_replace("/( ){" . $spaces . "}| {" . $spaces . "}/", "\t", $string);
$this->functionName = __FUNCTION__;
return $this;
}
/**
*
* @param string $string
* @param string $spaces
* @return \phpLive
*
* Converts tabs to spaces
*/
public function tabToSp($spaces = 4, $string = null){
if($string === null)
$string = $this->string;
$this->string = preg_replace("/\t/", str_repeat(" ", $spaces), $string);
$this->functionName = __FUNCTION__;
return $this;
}
/**
*
* @param string $string
* @param long $input
* @return \phpLive
*
* Counts the number of lines in a string or file.
*/
public function lineCount($string = null){
if(is_file($string)){
$string = file_get_contents($string);
}else{
if($string === null)
$string = $this->string;
}
$this->string = count(explode("\n", $string));
$this->functionName = __FUNCTION__;
return $this;
}
/**
*
* @param type $content
* @param type $highlight
* @return \phpLive
*
* Syntax highlights php/html/css
*/
public function highlight($content = null, $highlight = HIGHLIGHT_PHP){
if($content === null)
$content = $this->string;
$this->string = "";
switch($highlight){
case HIGHLIGHT_PHP:
if(is_file($content)){
$this->string = highlight_file($content, true);
}else{
$this->string = highlight_string($content, true);
}
break;
case HIGHLIGHT_HTML:
if(is_file($content)){
$content = file_get_contents($content);
}
$this->string = $this->highlightHTML($content);
break;
case HIGHLIGHT_CSS:
if(is_file($content))
$this->string = file_get_contents($content);
else
$this->string = $content;
$this->highlightCSS();
break;
}
$this->functionName = __FUNCTION__;
return $this;
}
public function highlightHTML($content){
$iscomment = false;
$tag = "#0000ff";
$att = "#ff0000";
$val = "#8000ff";
$com = "#34803a";
$doc = "#bf9221";
$tmpStr = "";
$sp = preg_split('/(<!--.*?-->|<style.*?>.*?<\/style>)/s', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach($sp as $split){
$split = htmlentities($split, ENT_QUOTES, "UTF-8", false);
if(preg_match("/<!--/i", $split)){
$tmpStr .= '<span style="color:' . $com . ';font-style:italic;">' . $split . '</span>';
}elseif(preg_match("/<style/i", $split)){
//print_r($split);exit;
$spl = preg_split("/(<style.*?>|<\/style>)/i", $split, -1, PREG_SPLIT_DELIM_CAPTURE);
//print_r($spl);
$tmpStr .= preg_replace(array(
'~(\s[a-z].*?=)~',
'~(<([a-z?]|!DOCTYPE).*?>)~'
), array(
'<span style="color:' . $att . ';">$1</span>',
'<span style="color:' . $tag . ';">$1</span>'
), $spl[1]);
$tmpStr .= $this->highlight($spl[2], HIGHLIGHT_CSS);
$tmpStr .= preg_replace("~(</[a-zA-Z].*?>)~", '<span style="color:' . $tag . ';">$1</span>', $spl[3]);
}else{
$find = array(
'~(\s[a-z].*?=)~', // Highlight the attributes
'~("[a-zA-Z0-9\/].*?")~', // Highlight the values
'~(<([a-z?]|!DOCTYPE).*?>)~', // Highlight the beginning of the opening tag
'~(</[a-zA-Z].*?>)~', // Highlight the closing tag
'~(&.*?;)~', // Stylize HTML entities
'~(<!DOCTYPE.*?>)~', // DOCTYPE
);
$replace = array(
'<span style="color:' . $att . ';">$1</span>',
'<span style="color:' . $val . ';">$1</span>',
'<span style="color:' . $tag . ';">$1</span>',
'<span style="color:' . $tag . ';">$1</span>',
'<span style="font-style:italic;">$1</span>',
'<span style="color:' . $doc . ';">$1</span>',
);
$tmpStr .= preg_replace($find, $replace, $split);
}
$iscomment = !$iscomment;
}
return $tmpStr;
}
/**
*
* @param string $css
* @param boolean $pre
* @return \phpLive
*
* This is a css lexer that tokenizes and colorizes css
*/
public function highlightCSS($css = null, $pre = false){
if($css === null)
$css = $this->string;
$this->functionName = __FUNCTION__;
$tokens = array();
$len = strlen($css);
$i = 0;
$state = 'selector';
$prevState = "";
$tokenValue = '';
$commenting = false;
$isvalue = false;
$isstring = false;
$openStr = "";
while($i < $len){
switch($css[$i]){
case '{':
if(!$commenting){
$tokens[] = array('type' => $state, 'value' => $tokenValue);
$tokens[] = array('type' => 'ruleset-begin', 'value' => '{');
$state = 'ruleset';
$tokenValue = '';
}else{
$tokenValue .= $css[$i];
}
break;
case '}':
if(!$commenting){
$tokens[] = array('type' => $state, 'value' => $tokenValue);
$tokens[] = array('type' => 'ruleset-end', 'value' => '}');
$state = 'selector';
$tokenValue = '';
}else{
$tokenValue .= $css[$i];
}
break;
default:
if($css[$i] == ":" && !$commenting && $state == "ruleset"){
$isvalue = true;
$prevState = $state;
$tokenValue .= $css[$i];
$tokens[] = array('type' => "ruleset", 'value' => $tokenValue);
$state = "value";
$tokenValue = "";
$i++;
}
if(isset($css[$i + 1]) && $css[$i] . $css[$i + 1] == "/*" && !$commenting){
$commenting = true;
$prevState = $state;
$state = "comment";
}
if(($css[$i] == "'" || $css[$i] == '"') && !$commenting){
if(!$isstring){
$isstring = true;
$openStr = $css[$i];
$tokens[] = array('type' => $state, 'value' => $tokenValue);
$prevState = $state;
$state = "string";
$tokenValue = "";
}else{
if($css[$i] == $openStr){
$isstring = false;
$openStr = "";
$tokenValue .= $css[$i];
$tokens[] = array('type' => "string", 'value' => $tokenValue);
$state = $prevState;
$tokenValue = "";
$i++;
}
}
}
if(isset($css[$i + 1]) && $css[$i] . $css[$i + 1] == "*/" && $commenting){
$commenting = false;
$tokens[] = array('type' => $state, 'value' => $tokenValue . "*/");
$state = $prevState;
$tokenValue = "";
$i++;
}else{
if($state == "value" && $css[$i] == ";" && $isvalue){
$isvalue = false;
$tokens[] = array('type' => $state, 'value' => $tokenValue);
$tokens[] = array('type' => "ruleset", 'value' => ";");
$tokenValue = "";
$state = "ruleset";
}else{
$tokenValue .= $css[$i];
}
}
}
$i++;
}
if(!empty($tokenValue)){
$tokens[] = array('type' => $state, 'value' => $tokenValue);
}
$this->string = "";
$styles = array(
'selector' => 'font-weight: bold;color: #007c00',
'ruleset' => 'color: #0000ff;',
'ruleset-begin' => 'orange',
'ruleset-end' => 'orange',
'comment' => 'color: #999999;font-style: italic;',
'value' => 'color: #000000;',
'string' => 'color: #ce7b00;'
);
if((bool)$pre)
$this->string .= "<pre>";
$this->string .= "<span style=\"color: #000000;\">";
foreach($tokens as $tok){
$style = $styles[$tok['type']];
$this->string .= '<span style="' . $style . '">' . $tok['value'] . '</span>';