-
Notifications
You must be signed in to change notification settings - Fork 14
/
geshi.php
4776 lines (4357 loc) · 200 KB
/
geshi.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
/**
* GeSHi - Generic Syntax Highlighter
*
* The GeSHi class for Generic Syntax Highlighting. Please refer to the
* documentation at http://qbnz.com/highlighter/documentation.php for more
* information about how to use this class.
*
* For changes, release notes, TODOs etc, see the relevant files in the docs/
* directory.
*
* This file is part of GeSHi.
*
* GeSHi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GeSHi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GeSHi; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @package geshi
* @subpackage core
* @author Nigel McNie <[email protected]>, Benny Baumann <[email protected]>
* @copyright (C) 2004 - 2007 Nigel McNie, (C) 2007 - 2008 Benny Baumann
* @license http://gnu.org/copyleft/gpl.html GNU GPL
*
*/
//
// GeSHi Constants
// You should use these constant names in your programs instead of
// their values - you never know when a value may change in a future
// version
//
/** The version of this GeSHi file */
define('GESHI_VERSION', '1.0.8.12');
// Define the root directory for the GeSHi code tree
if (!defined('GESHI_ROOT')) {
/** The root directory for GeSHi */
define('GESHI_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
}
/** The language file directory for GeSHi
@access private */
define('GESHI_LANG_ROOT', GESHI_ROOT . 'geshi' . DIRECTORY_SEPARATOR);
// Define if GeSHi should be paranoid about security
if (!defined('GESHI_SECURITY_PARANOID')) {
/** Tells GeSHi to be paranoid about security settings */
define('GESHI_SECURITY_PARANOID', false);
}
// Line numbers - use with enable_line_numbers()
/** Use no line numbers when building the result */
define('GESHI_NO_LINE_NUMBERS', 0);
/** Use normal line numbers when building the result */
define('GESHI_NORMAL_LINE_NUMBERS', 1);
/** Use fancy line numbers when building the result */
define('GESHI_FANCY_LINE_NUMBERS', 2);
// Container HTML type
/** Use nothing to surround the source */
define('GESHI_HEADER_NONE', 0);
/** Use a "div" to surround the source */
define('GESHI_HEADER_DIV', 1);
/** Use a "pre" to surround the source */
define('GESHI_HEADER_PRE', 2);
/** Use a pre to wrap lines when line numbers are enabled or to wrap the whole code. */
define('GESHI_HEADER_PRE_VALID', 3);
/**
* Use a "table" to surround the source:
*
* <table>
* <thead><tr><td colspan="2">$header</td></tr></thead>
* <tbody><tr><td><pre>$linenumbers</pre></td><td><pre>$code></pre></td></tr></tbody>
* <tfooter><tr><td colspan="2">$footer</td></tr></tfoot>
* </table>
*
* this is essentially only a workaround for Firefox, see sf#1651996 or take a look at
* https://bugzilla.mozilla.org/show_bug.cgi?id=365805
* @note when linenumbers are disabled this is essentially the same as GESHI_HEADER_PRE
*/
define('GESHI_HEADER_PRE_TABLE', 4);
// Capatalisation constants
/** Lowercase keywords found */
define('GESHI_CAPS_NO_CHANGE', 0);
/** Uppercase keywords found */
define('GESHI_CAPS_UPPER', 1);
/** Leave keywords found as the case that they are */
define('GESHI_CAPS_LOWER', 2);
// Link style constants
/** Links in the source in the :link state */
define('GESHI_LINK', 0);
/** Links in the source in the :hover state */
define('GESHI_HOVER', 1);
/** Links in the source in the :active state */
define('GESHI_ACTIVE', 2);
/** Links in the source in the :visited state */
define('GESHI_VISITED', 3);
// Important string starter/finisher
// Note that if you change these, they should be as-is: i.e., don't
// write them as if they had been run through htmlentities()
/** The starter for important parts of the source */
define('GESHI_START_IMPORTANT', '<BEGIN GeSHi>');
/** The ender for important parts of the source */
define('GESHI_END_IMPORTANT', '<END GeSHi>');
/**#@+
* @access private
*/
// When strict mode applies for a language
/** Strict mode never applies (this is the most common) */
define('GESHI_NEVER', 0);
/** Strict mode *might* apply, and can be enabled or
disabled by {@link GeSHi->enable_strict_mode()} */
define('GESHI_MAYBE', 1);
/** Strict mode always applies */
define('GESHI_ALWAYS', 2);
// Advanced regexp handling constants, used in language files
/** The key of the regex array defining what to search for */
define('GESHI_SEARCH', 0);
/** The key of the regex array defining what bracket group in a
matched search to use as a replacement */
define('GESHI_REPLACE', 1);
/** The key of the regex array defining any modifiers to the regular expression */
define('GESHI_MODIFIERS', 2);
/** The key of the regex array defining what bracket group in a
matched search to put before the replacement */
define('GESHI_BEFORE', 3);
/** The key of the regex array defining what bracket group in a
matched search to put after the replacement */
define('GESHI_AFTER', 4);
/** The key of the regex array defining a custom keyword to use
for this regexp's html tag class */
define('GESHI_CLASS', 5);
/** Used in language files to mark comments */
define('GESHI_COMMENTS', 0);
/** Used to work around missing PHP features **/
define('GESHI_PHP_PRE_433', !(version_compare(PHP_VERSION, '4.3.3') === 1));
/** make sure we can call stripos **/
if (!function_exists('stripos')) {
// the offset param of preg_match is not supported below PHP 4.3.3
if (GESHI_PHP_PRE_433) {
/**
* @ignore
*/
function stripos($haystack, $needle, $offset = null) {
if (!is_null($offset)) {
$haystack = substr($haystack, $offset);
}
if (preg_match('/'. preg_quote($needle, '/') . '/', $haystack, $match, PREG_OFFSET_CAPTURE)) {
return $match[0][1];
}
return false;
}
}
else {
/**
* @ignore
*/
function stripos($haystack, $needle, $offset = null) {
if (preg_match('/'. preg_quote($needle, '/') . '/', $haystack, $match, PREG_OFFSET_CAPTURE, $offset)) {
return $match[0][1];
}
return false;
}
}
}
/** some old PHP / PCRE subpatterns only support up to xxx subpatterns in
regular expressions. Set this to false if your PCRE lib is up to date
@see GeSHi->optimize_regexp_list()
**/
define('GESHI_MAX_PCRE_SUBPATTERNS', 500);
/** it's also important not to generate too long regular expressions
be generous here... but keep in mind, that when reaching this limit we
still have to close open patterns. 12k should do just fine on a 16k limit.
@see GeSHi->optimize_regexp_list()
**/
define('GESHI_MAX_PCRE_LENGTH', 12288);
//Number format specification
/** Basic number format for integers */
define('GESHI_NUMBER_INT_BASIC', 1); //Default integers \d+
/** Enhanced number format for integers like seen in C */
define('GESHI_NUMBER_INT_CSTYLE', 2); //Default C-Style \d+[lL]?
/** Number format to highlight binary numbers with a suffix "b" */
define('GESHI_NUMBER_BIN_SUFFIX', 16); //[01]+[bB]
/** Number format to highlight binary numbers with a prefix % */
define('GESHI_NUMBER_BIN_PREFIX_PERCENT', 32); //%[01]+
/** Number format to highlight binary numbers with a prefix 0b (C) */
define('GESHI_NUMBER_BIN_PREFIX_0B', 64); //0b[01]+
/** Number format to highlight octal numbers with a leading zero */
define('GESHI_NUMBER_OCT_PREFIX', 256); //0[0-7]+
/** Number format to highlight octal numbers with a prefix 0o (logtalk) */
define('GESHI_NUMBER_OCT_PREFIX_0O', 512); //0[0-7]+
/** Number format to highlight octal numbers with a leading @ (Used in HiSofts Devpac series). */
define('GESHI_NUMBER_OCT_PREFIX_AT', 1024); //@[0-7]+
/** Number format to highlight octal numbers with a suffix of o */
define('GESHI_NUMBER_OCT_SUFFIX', 2048); //[0-7]+[oO]
/** Number format to highlight hex numbers with a prefix 0x */
define('GESHI_NUMBER_HEX_PREFIX', 4096); //0x[0-9a-fA-F]+
/** Number format to highlight hex numbers with a prefix $ */
define('GESHI_NUMBER_HEX_PREFIX_DOLLAR', 8192); //$[0-9a-fA-F]+
/** Number format to highlight hex numbers with a suffix of h */
define('GESHI_NUMBER_HEX_SUFFIX', 16384); //[0-9][0-9a-fA-F]*h
/** Number format to highlight floating-point numbers without support for scientific notation */
define('GESHI_NUMBER_FLT_NONSCI', 65536); //\d+\.\d+
/** Number format to highlight floating-point numbers without support for scientific notation */
define('GESHI_NUMBER_FLT_NONSCI_F', 131072); //\d+(\.\d+)?f
/** Number format to highlight floating-point numbers with support for scientific notation (E) and optional leading zero */
define('GESHI_NUMBER_FLT_SCI_SHORT', 262144); //\.\d+e\d+
/** Number format to highlight floating-point numbers with support for scientific notation (E) and required leading digit */
define('GESHI_NUMBER_FLT_SCI_ZERO', 524288); //\d+(\.\d+)?e\d+
//Custom formats are passed by RX array
// Error detection - use these to analyse faults
/** No sourcecode to highlight was specified
* @deprecated
*/
define('GESHI_ERROR_NO_INPUT', 1);
/** The language specified does not exist */
define('GESHI_ERROR_NO_SUCH_LANG', 2);
/** GeSHi could not open a file for reading (generally a language file) */
define('GESHI_ERROR_FILE_NOT_READABLE', 3);
/** The header type passed to {@link GeSHi->set_header_type()} was invalid */
define('GESHI_ERROR_INVALID_HEADER_TYPE', 4);
/** The line number type passed to {@link GeSHi->enable_line_numbers()} was invalid */
define('GESHI_ERROR_INVALID_LINE_NUMBER_TYPE', 5);
/**#@-*/
/**
* The GeSHi Class.
*
* Please refer to the documentation for GeSHi 1.0.X that is available
* at http://qbnz.com/highlighter/documentation.php for more information
* about how to use this class.
*
* @package geshi
* @author Nigel McNie <[email protected]>, Benny Baumann <[email protected]>
* @copyright (C) 2004 - 2007 Nigel McNie, (C) 2007 - 2008 Benny Baumann
*/
class GeSHi {
/**#@+
* @access private
*/
/**
* The source code to highlight
* @var string
*/
var $source = '';
/**
* The language to use when highlighting
* @var string
*/
var $language = '';
/**
* The data for the language used
* @var array
*/
var $language_data = array();
/**
* The path to the language files
* @var string
*/
var $language_path = GESHI_LANG_ROOT;
/**
* The error message associated with an error
* @var string
* @todo check err reporting works
*/
var $error = false;
/**
* Possible error messages
* @var array
*/
var $error_messages = array(
GESHI_ERROR_NO_SUCH_LANG => 'GeSHi could not find the language {LANGUAGE} (using path {PATH})',
GESHI_ERROR_FILE_NOT_READABLE => 'The file specified for load_from_file was not readable',
GESHI_ERROR_INVALID_HEADER_TYPE => 'The header type specified is invalid',
GESHI_ERROR_INVALID_LINE_NUMBER_TYPE => 'The line number type specified is invalid'
);
/**
* Whether highlighting is strict or not
* @var boolean
*/
var $strict_mode = false;
/**
* Whether to use CSS classes in output
* @var boolean
*/
var $use_classes = false;
/**
* The type of header to use. Can be one of the following
* values:
*
* - GESHI_HEADER_PRE: Source is outputted in a "pre" HTML element.
* - GESHI_HEADER_DIV: Source is outputted in a "div" HTML element.
* - GESHI_HEADER_NONE: No header is outputted.
*
* @var int
*/
var $header_type = GESHI_HEADER_PRE;
/**
* Array of permissions for which lexics should be highlighted
* @var array
*/
var $lexic_permissions = array(
'KEYWORDS' => array(),
'COMMENTS' => array('MULTI' => true),
'REGEXPS' => array(),
'ESCAPE_CHAR' => true,
'BRACKETS' => true,
'SYMBOLS' => false,
'STRINGS' => true,
'NUMBERS' => true,
'METHODS' => true,
'SCRIPT' => true
);
/**
* The time it took to parse the code
* @var double
*/
var $time = 0;
/**
* The content of the header block
* @var string
*/
var $header_content = '';
/**
* The content of the footer block
* @var string
*/
var $footer_content = '';
/**
* The style of the header block
* @var string
*/
var $header_content_style = '';
/**
* The style of the footer block
* @var string
*/
var $footer_content_style = '';
/**
* Tells if a block around the highlighted source should be forced
* if not using line numbering
* @var boolean
*/
var $force_code_block = false;
/**
* The styles for hyperlinks in the code
* @var array
*/
var $link_styles = array();
/**
* Whether important blocks should be recognised or not
* @var boolean
* @deprecated
* @todo REMOVE THIS FUNCTIONALITY!
*/
var $enable_important_blocks = false;
/**
* Styles for important parts of the code
* @var string
* @deprecated
* @todo As above - rethink the whole idea of important blocks as it is buggy and
* will be hard to implement in 1.2
*/
var $important_styles = 'font-weight: bold; color: red;'; // Styles for important parts of the code
/**
* Whether CSS IDs should be added to the code
* @var boolean
*/
var $add_ids = false;
/**
* Lines that should be highlighted extra
* @var array
*/
var $highlight_extra_lines = array();
/**
* Styles of lines that should be highlighted extra
* @var array
*/
var $highlight_extra_lines_styles = array();
/**
* Styles of extra-highlighted lines
* @var string
*/
var $highlight_extra_lines_style = 'background-color: #ffc;';
/**
* The line ending
* If null, nl2br() will be used on the result string.
* Otherwise, all instances of \n will be replaced with $line_ending
* @var string
*/
var $line_ending = null;
/**
* Number at which line numbers should start at
* @var int
*/
var $line_numbers_start = 1;
/**
* The overall style for this code block
* @var string
*/
var $overall_style = 'font-family:monospace;';
/**
* The style for the actual code
* @var string
*/
var $code_style = 'font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;';
/**
* The overall class for this code block
* @var string
*/
var $overall_class = '';
/**
* The overall ID for this code block
* @var string
*/
var $overall_id = '';
/**
* Line number styles
* @var string
*/
var $line_style1 = 'font-weight: normal; vertical-align:top;';
/**
* Line number styles for fancy lines
* @var string
*/
var $line_style2 = 'font-weight: bold; vertical-align:top;';
/**
* Style for line numbers when GESHI_HEADER_PRE_TABLE is chosen
* @var string
*/
var $table_linenumber_style = 'width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;';
/**
* Flag for how line numbers are displayed
* @var boolean
*/
var $line_numbers = GESHI_NO_LINE_NUMBERS;
/**
* Flag to decide if multi line spans are allowed. Set it to false to make sure
* each tag is closed before and reopened after each linefeed.
* @var boolean
*/
var $allow_multiline_span = true;
/**
* The "nth" value for fancy line highlighting
* @var int
*/
var $line_nth_row = 0;
/**
* The size of tab stops
* @var int
*/
var $tab_width = 8;
/**
* Should we use language-defined tab stop widths?
* @var int
*/
var $use_language_tab_width = false;
/**
* Default target for keyword links
* @var string
*/
var $link_target = '';
/**
* The encoding to use for entity encoding
* NOTE: Used with Escape Char Sequences to fix UTF-8 handling (cf. SF#2037598)
* @var string
*/
var $encoding = 'utf-8';
/**
* Should keywords be linked?
* @var boolean
*/
var $keyword_links = true;
/**
* Currently loaded language file
* @var string
* @since 1.0.7.22
*/
var $loaded_language = '';
/**
* Wether the caches needed for parsing are built or not
*
* @var bool
* @since 1.0.8
*/
var $parse_cache_built = false;
/**
* Work around for Suhosin Patch with disabled /e modifier
*
* Note from suhosins author in config file:
* <blockquote>
* The /e modifier inside <code>preg_replace()</code> allows code execution.
* Often it is the cause for remote code execution exploits. It is wise to
* deactivate this feature and test where in the application it is used.
* The developer using the /e modifier should be made aware that he should
* use <code>preg_replace_callback()</code> instead
* </blockquote>
*
* @var array
* @since 1.0.8
*/
var $_kw_replace_group = 0;
var $_rx_key = 0;
/**
* some "callback parameters" for handle_multiline_regexps
*
* @since 1.0.8
* @access private
* @var string
*/
var $_hmr_before = '';
var $_hmr_replace = '';
var $_hmr_after = '';
var $_hmr_key = 0;
/**#@-*/
/**
* Creates a new GeSHi object, with source and language
*
* @param string The source code to highlight
* @param string The language to highlight the source with
* @param string The path to the language file directory. <b>This
* is deprecated!</b> I've backported the auto path
* detection from the 1.1.X dev branch, so now it
* should be automatically set correctly. If you have
* renamed the language directory however, you will
* still need to set the path using this parameter or
* {@link GeSHi->set_language_path()}
* @since 1.0.0
*/
function __construct($source = '', $language = '', $path = '') {
if (!empty($source)) {
$this->set_source($source);
}
if (!empty($language)) {
$this->set_language($language);
}
$this->set_language_path($path);
}
/**
* Returns the version of GeSHi
*
* @return string
* @since 1 0.8.11
*/
function get_version()
{
return GESHI_VERSION;
}
/**
* Returns an error message associated with the last GeSHi operation,
* or false if no error has occured
*
* @return string|false An error message if there has been an error, else false
* @since 1.0.0
*/
function error() {
if ($this->error) {
//Put some template variables for debugging here ...
$debug_tpl_vars = array(
'{LANGUAGE}' => $this->language,
'{PATH}' => $this->language_path
);
$msg = str_replace(
array_keys($debug_tpl_vars),
array_values($debug_tpl_vars),
$this->error_messages[$this->error]);
return "<br /><strong>GeSHi Error:</strong> $msg (code {$this->error})<br />";
}
return false;
}
/**
* Gets a human-readable language name (thanks to Simon Patterson
* for the idea :))
*
* @return string The name for the current language
* @since 1.0.2
*/
function get_language_name() {
if (GESHI_ERROR_NO_SUCH_LANG == $this->error) {
return $this->language_data['LANG_NAME'] . ' (Unknown Language)';
}
return $this->language_data['LANG_NAME'];
}
/**
* Sets the source code for this object
*
* @param string The source code to highlight
* @since 1.0.0
*/
function set_source($source) {
$this->source = $source;
$this->highlight_extra_lines = array();
}
/**
* Sets the language for this object
*
* @note since 1.0.8 this function won't reset language-settings by default anymore!
* if you need this set $force_reset = true
*
* @param string The name of the language to use
* @since 1.0.0
*/
function set_language($language, $force_reset = false) {
if ($force_reset) {
$this->loaded_language = false;
}
//Clean up the language name to prevent malicious code injection
$language = preg_replace('#[^a-zA-Z0-9\-_]#', '', $language);
$language = strtolower($language);
//Retreive the full filename
$file_name = $this->language_path . $language . '.php';
if ($file_name == $this->loaded_language) {
// this language is already loaded!
return;
}
$this->language = $language;
$this->error = false;
$this->strict_mode = GESHI_NEVER;
//Check if we can read the desired file
if (!is_readable($file_name)) {
$this->error = GESHI_ERROR_NO_SUCH_LANG;
return;
}
// Load the language for parsing
$this->load_language($file_name);
}
/**
* Sets the path to the directory containing the language files. Note
* that this path is relative to the directory of the script that included
* geshi.php, NOT geshi.php itself.
*
* @param string The path to the language directory
* @since 1.0.0
* @deprecated The path to the language files should now be automatically
* detected, so this method should no longer be needed. The
* 1.1.X branch handles manual setting of the path differently
* so this method will disappear in 1.2.0.
*/
function set_language_path($path) {
if(strpos($path,':')) {
//Security Fix to prevent external directories using fopen wrappers.
if(DIRECTORY_SEPARATOR == "\\") {
if(!preg_match('#^[a-zA-Z]:#', $path) || false !== strpos($path, ':', 2)) {
return;
}
} else {
return;
}
}
if(preg_match('#[^/a-zA-Z0-9_\.\-\\\s:]#', $path)) {
//Security Fix to prevent external directories using fopen wrappers.
return;
}
if(GESHI_SECURITY_PARANOID && false !== strpos($path, '/.')) {
//Security Fix to prevent external directories using fopen wrappers.
return;
}
if(GESHI_SECURITY_PARANOID && false !== strpos($path, '..')) {
//Security Fix to prevent external directories using fopen wrappers.
return;
}
if ($path) {
$this->language_path = ('/' == $path[strlen($path) - 1]) ? $path : $path . '/';
$this->set_language($this->language); // otherwise set_language_path has no effect
}
}
/**
* Get supported langs or an associative array lang=>full_name.
* @param boolean $longnames
* @return array
*/
function get_supported_languages($full_names=false)
{
// return array
$back = array();
// we walk the lang root
$dir = dir($this->language_path);
// foreach entry
while (false !== ($entry = $dir->read()))
{
$full_path = $this->language_path.$entry;
// Skip all dirs
if (is_dir($full_path)) {
continue;
}
// we only want lang.php files
if (!preg_match('/^([^.]+)\.php$/', $entry, $matches)) {
continue;
}
// Raw lang name is here
$langname = $matches[1];
// We want the fullname too?
if ($full_names === true)
{
if (false !== ($fullname = $this->get_language_fullname($langname)))
{
$back[$langname] = $fullname; // we go associative
}
}
else
{
// just store raw langname
$back[] = $langname;
}
}
$dir->close();
return $back;
}
/**
* Get full_name for a lang or false.
* @param string $language short langname (html4strict for example)
* @return mixed
*/
function get_language_fullname($language)
{
//Clean up the language name to prevent malicious code injection
$language = preg_replace('#[^a-zA-Z0-9\-_]#', '', $language);
$language = strtolower($language);
// get fullpath-filename for a langname
$fullpath = $this->language_path.$language.'.php';
// we need to get contents :S
if (false === ($data = file_get_contents($fullpath))) {
$this->error = sprintf('Geshi::get_lang_fullname() Unknown Language: %s', $language);
return false;
}
// match the langname
if (!preg_match('/\'LANG_NAME\'\s*=>\s*\'((?:[^\']|\\\')+?)\'/', $data, $matches)) {
$this->error = sprintf('Geshi::get_lang_fullname(%s): Regex can not detect language', $language);
return false;
}
// return fullname for langname
return stripcslashes($matches[1]);
}
/**
* Sets the type of header to be used.
*
* If GESHI_HEADER_DIV is used, the code is surrounded in a "div".This
* means more source code but more control over tab width and line-wrapping.
* GESHI_HEADER_PRE means that a "pre" is used - less source, but less
* control. Default is GESHI_HEADER_PRE.
*
* From 1.0.7.2, you can use GESHI_HEADER_NONE to specify that no header code
* should be outputted.
*
* @param int The type of header to be used
* @since 1.0.0
*/
function set_header_type($type) {
//Check if we got a valid header type
if (!in_array($type, array(GESHI_HEADER_NONE, GESHI_HEADER_DIV,
GESHI_HEADER_PRE, GESHI_HEADER_PRE_VALID, GESHI_HEADER_PRE_TABLE))) {
$this->error = GESHI_ERROR_INVALID_HEADER_TYPE;
return;
}
//Set that new header type
$this->header_type = $type;
}
/**
* Sets the styles for the code that will be outputted
* when this object is parsed. The style should be a
* string of valid stylesheet declarations
*
* @param string The overall style for the outputted code block
* @param boolean Whether to merge the styles with the current styles or not
* @since 1.0.0
*/
function set_overall_style($style, $preserve_defaults = false) {
if (!$preserve_defaults) {
$this->overall_style = $style;
} else {
$this->overall_style .= $style;
}
}
/**
* Sets the overall classname for this block of code. This
* class can then be used in a stylesheet to style this object's
* output
*
* @param string The class name to use for this block of code
* @since 1.0.0
*/
function set_overall_class($class) {
$this->overall_class = $class;
}
/**
* Sets the overall id for this block of code. This id can then
* be used in a stylesheet to style this object's output
*
* @param string The ID to use for this block of code
* @since 1.0.0
*/
function set_overall_id($id) {
$this->overall_id = $id;
}
/**
* Sets whether CSS classes should be used to highlight the source. Default
* is off, calling this method with no arguments will turn it on
*
* @param boolean Whether to turn classes on or not
* @since 1.0.0
*/
function enable_classes($flag = true) {
$this->use_classes = ($flag) ? true : false;
}
/**
* Sets the style for the actual code. This should be a string
* containing valid stylesheet declarations. If $preserve_defaults is
* true, then styles are merged with the default styles, with the
* user defined styles having priority
*
* Note: Use this method to override any style changes you made to
* the line numbers if you are using line numbers, else the line of
* code will have the same style as the line number! Consult the
* GeSHi documentation for more information about this.
*
* @param string The style to use for actual code
* @param boolean Whether to merge the current styles with the new styles
* @since 1.0.2
*/
function set_code_style($style, $preserve_defaults = false) {
if (!$preserve_defaults) {
$this->code_style = $style;
} else {
$this->code_style .= $style;
}
}
/**
* Sets the styles for the line numbers.
*
* @param string The style for the line numbers that are "normal"
* @param string|boolean If a string, this is the style of the line
* numbers that are "fancy", otherwise if boolean then this
* defines whether the normal styles should be merged with the
* new normal styles or not
* @param boolean If set, is the flag for whether to merge the "fancy"
* styles with the current styles or not
* @since 1.0.2
*/
function set_line_style($style1, $style2 = '', $preserve_defaults = false) {
//Check if we got 2 or three parameters
if (is_bool($style2)) {
$preserve_defaults = $style2;
$style2 = '';
}
//Actually set the new styles
if (!$preserve_defaults) {
$this->line_style1 = $style1;
$this->line_style2 = $style2;
} else {
$this->line_style1 .= $style1;
$this->line_style2 .= $style2;
}
}
/**
* Sets whether line numbers should be displayed.
*
* Valid values for the first parameter are:
*
* - GESHI_NO_LINE_NUMBERS: Line numbers will not be displayed
* - GESHI_NORMAL_LINE_NUMBERS: Line numbers will be displayed
* - GESHI_FANCY_LINE_NUMBERS: Fancy line numbers will be displayed
*
* For fancy line numbers, the second parameter is used to signal which lines
* are to be fancy. For example, if the value of this parameter is 5 then every
* 5th line will be fancy.
*
* @param int How line numbers should be displayed
* @param int Defines which lines are fancy
* @since 1.0.0
*/
function enable_line_numbers($flag, $nth_row = 5) {
if (GESHI_NO_LINE_NUMBERS != $flag && GESHI_NORMAL_LINE_NUMBERS != $flag
&& GESHI_FANCY_LINE_NUMBERS != $flag) {
$this->error = GESHI_ERROR_INVALID_LINE_NUMBER_TYPE;
}
$this->line_numbers = $flag;
$this->line_nth_row = $nth_row;
}
/**
* Sets wether spans and other HTML markup generated by GeSHi can
* span over multiple lines or not. Defaults to true to reduce overhead.
* Set it to false if you want to manipulate the output or manually display
* the code in an ordered list.
*
* @param boolean Wether multiline spans are allowed or not
* @since 1.0.7.22
*/
function enable_multiline_span($flag) {
$this->allow_multiline_span = (bool) $flag;
}
/**
* Get current setting for multiline spans, see GeSHi->enable_multiline_span().