summaryrefslogtreecommitdiff
path: root/plugins/data.code.php
blob: 68279777b628e2e0e3a23b1bf9640b4e65b79bf0 (plain)
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
<?php

namespace Bitweaver\Liberty;

use Bitweaver\KernelTools;

/**
 * @version  $Revision$
 * @package  liberty
 * @subpackage plugins_data
 */
// +----------------------------------------------------------------------+
// | Copyright (c) 2004, bitweaver.org
// +----------------------------------------------------------------------+
// | All Rights Reserved. See below for details and a complete list of authors.
// | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
// |
// | For comments, please use phpdocu.sourceforge.net documentation standards!!!
// | -> see http://phpdocu.sourceforge.net/
// +----------------------------------------------------------------------+
// | Author (TikiWiki): Luis Argerich <lrargerich@users.sourceforge.net>
// | Reworked for Bitweaver (& Undoubtedly Screwed-Up)
// | by: StarRider <starrrider@users.sourceforge.net>
// +----------------------------------------------------------------------+
// $Id$

/**
 * definitions
 */
define( 'PLUGIN_GUID_DATACODE', 'datacode' );
global $gLibertySystem;
$pluginParams = [
	'tag'                 => 'CODE',
	'auto_activate'       => true,
	'requires_pair'       => true,
	'load_function'       => 'data_code',
	'title'               => 'Code',
	'help_page'           => 'DataPluginCode',
	'description'         => KernelTools::tra( "Displays the Source Code Snippet between {code} blocks." ),
	'help_function'       => 'data_code_help',
	'syntax'              => "{code source= num= }". KernelTools::tra( "Sorce Code Snippet" ) . "{/code}",
	'plugin_type'         => DATA_PLUGIN,
	'plugin_settings_url' => LIBERTY_PKG_URL.'admin/plugins/data_code.php',
];
$gLibertySystem->registerPlugin( PLUGIN_GUID_DATACODE, $pluginParams );
$gLibertySystem->registerDataTag( $pluginParams['tag'], PLUGIN_GUID_DATACODE );

// Help Function
function data_code_help(): string {
	$help =
		'<table class="data help">'
			.'<tr>'
				.'<th>' . KernelTools::tra( "Key" ) . '</th>'
				.'<th>' . KernelTools::tra( "Type" ) . '</th>'
				.'<th>' . KernelTools::tra( "Comments" ) . '</th>'
			.'</tr>'
			.'<tr class="odd">'
				.'<td>source</td>'
				.'<td>' . KernelTools::tra( "key-word") . '<br />' . KernelTools::tra("(optional)") . '</td>'
				.'<td>' . KernelTools::tra( "Defines the format of the Source Code Snippet. Possible values are:");
	if( file_exists( UTIL_PKG_INCLUDE_PATH.'geshi/geshi.php' ) ) {
		$help .= '<br />
			<strong>ActionScript</strong> &bull;
			<strong>Ada</strong> &bull;
			Apache Log File = <strong>Apache</strong> &bull;
			<strong>AppleScript</strong> &bull;
			ASM (NASM based) = <strong>Asm</strong> &bull;
			<strong>ASP</strong> &bull;
			AutoCAD DCL = <strong>CadDcl</strong> &bull;
			AutoCAD LISP = <strong>CadLisp</strong> &bull;
			<strong>Bash</strong> &bull;
			<strong>BLITZ BASIC</strong> &bull;
			<strong>C</strong> &bull;
			C++ = <strong>Cpp</strong> &bull;
			C# = <strong>CSharp</strong> &bull;
			C for Macs = <strong>C_Mac</strong> &bull;
			<strong>CSS</strong> &bull;
			<strong>D</strong> &bull;
			<strong>Delphi</strong> &bull;
			Diff Output = <strong>Diff</strong> &bull;
			<strong>DIV</strong> &bull;
			<strong>DOS</strong> &bull;
			<strong>Eiffel</strong> &bull;
			<strong>FreeBasic</strong> &bull;
			<strong>GML</strong> &bull;
			HTML (4.0.1) = <strong>Html4Strict</strong> &bull;
			<strong>ini</strong> &bull;
			<strong>Inno</strong> &bull;
			<strong>Java</strong> &bull;
			<strong>JavaScript</strong> &bull;
			<strong>Lisp</strong> &bull;
			<strong>Lua</strong> &bull;
			<strong>MatLab</strong> &bull;
			<strong>MpAsm</strong> &bull;
			<strong>MySQL</strong> &bull;
			NullSoft Installer = <strong>Niss</strong> &bull;
			Objective C = <strong>ObjC</strong> &bull;
			<strong>OCaml</strong> &bull;
			OpenOffice.org Basic = <strong>OoBas</strong> &bull;
			<strong>Oracle8</strong> &bull;
			<strong>Pascal</strong> &bull;
			<strong>Perl</strong> &bull;
			<strong>Php</strong> &bull;
			<strong>Php_Brief</strong> &bull;
			<strong>Python</strong> &bull;
			QuickBasic = <strong>QBasic</strong> &bull;
			<strong>Ruby</strong> &bull;
			<strong>Scheme</strong> &bull;
			<strong>Smarty</strong> &bull;
			<strong>SQL</strong> &bull;
			VB.NET = <strong>VbNet</strong> &bull;
			<strong>VHDL</strong> &bull;
			<strong>Visual Basic</strong> &bull;
			VisualBasic = <strong>Vb</strong> &bull;
			<strong>VisualFoxPro</strong> &bull;
			<strong>XML</strong>';
	} else {
		$help .= '<strong>HTML</strong> or <strong>PHP</strong>. ';
	}
	$help = $help . '<br />' . KernelTools::tra("The Default = ") . '<strong>PHP</strong></td>'
			.'</tr>'
			.'<tr class="even">'
				.'<td>title</td>'
				.'<td>'.KernelTools::tra( "string").'<br />'.KernelTools::tra("(optional)").'</td>'
				.'<td>'.KernelTools::tra( "Give the codelisting a title.").'</td>'
			.'</tr>'
			.'<tr class="odd">'
				.'<td>num</td>'
				.'<td>' .KernelTools::tra( "boolean/number") .'<br />'. KernelTools::tra("(optional)") . '</td>'
				.'<td>' .KernelTools::tra( "Determins if Line Numbers are displayed with the code. Specifing:")
					.'<strong>true / ON / YES /</strong> or a <strong>Number</strong> '
					.KernelTools::tra("will turn <strong>Line Numbering On</strong>. When a Number is specified - the Number is used for the first ")
					.KernelTools::tra("line instead of <strong>1</strong>. Any other value will turn <strong>Line Numbering OFF</strong> ")
					.KernelTools::tra("and only the <strong>Code</strong> will be displayed.")
					.'<br />' . KernelTools::tra("The Default =") .' <strong>false</strong> ' .KernelTools::tra("Line Numbers are <strong>Not</strong> displayed.")
				.'</td>'
			.'</tr>'
		.'</table>'
		. KernelTools::tra("Example: ") . "{code source='php' num='on'}" . KernelTools::tra("Sorce Code Snippet") . "{/code}";
	return $help;
}

if( !function_exists( 'unHtmlEntities' )) { // avoid name collisions
	function unHtmlEntities( $pStr ) {
		$tTbl = get_html_translation_table( HTML_ENTITIES );
		$tTbl = array_flip( $tTbl );
		return strtr( $pStr, $tTbl );
	}
}

if( !function_exists( 'deCodeHTML' )) { // avoid name collisions
	function deCodeHTML( $pStr ) {
		$pStr = strtr( $pStr, array_flip( get_html_translation_table( HTML_ENTITIES )));
		$pStr = preg_replace_callback( "/&#([0-9]+);/m", function($matches){ foreach($matches as $match){ return chr($match); } }, $pStr );
		return $pStr;
	}
}

// Load Function
function data_code( $pData, $pParams ) { // Pre-Clyde Changes
	global $gBitSystem;
	extract( $pParams, EXTR_SKIP );

	if( !empty( $colors ) and ( $colors == 'php' )) {
		$source = 'php';
	}

	if( !empty( $in )) {
		$source = $in;
	}

	$source = isset( $source ) ? strtolower( $source ) : $gBitSystem->getConfig( 'liberty_plugin_code_default_source', 'php' );

	if( !empty( $num ) && !is_numeric( $num )) {
		switch( strtoupper( $num )) {
		case 'true': case 'ON': case 'YES':
			$num = 1;
			break;
		default:
			$num = 0;
			break;
		}
	}
	$num = $num ?? false;

	// trim any trailing spaces
	$code = '';
	$lines = explode( "\n", $pData );
	foreach( $lines as $line ) {
		$code .=  rtrim( $line )."\n";
	}

	$code = unHtmlEntities( $code );

	// Trim any leading blank lines
	$code = preg_replace( '/^[\n\r]+/', "", $code );
	// Trim any trailing blank lines
	$code = file_exists( UTIL_PKG_INCLUDE_PATH.'geshi/geshi.php' )
		? preg_replace('/[\n\r]+$/', "", $code )
		: preg_replace('/[\n\r]+$/', "\n", $code );

	if( file_exists( UTIL_PKG_INCLUDE_PATH.'geshi/geshi.php' ) ) {
		// Include the GeSHi library
		include_once UTIL_PKG_INCLUDE_PATH.'geshi/geshi.php';
		$geshi = new \GeSHi($code, $source, UTIL_PKG_INCLUDE_PATH.'geshi/geshi' );
		if( $num ) { // Line Numbering has been requested
			$geshi->enable_line_numbers( GESHI_FANCY_LINE_NUMBERS );
			if( is_numeric( $num )) {
				$geshi->start_line_numbers_at( $num );
			}
		}
		$code = deCodeHTML( htmlentities( $geshi->parse_code() ));
	} else {
		// Line Numbering has been requested
		if( $num ) {
			$lines = explode( "\n", $code );
			$code = '';
			//Line Number
			$i = ( is_numeric( $num )) ? $num : 1;
			foreach( $lines as $line ) {
				if( strlen( $line ) > 1 ) {
					$code .= sprintf( "%3d", $i ).": ".$line."\n";
					$i++;
				}
			}
		}

		switch( strtoupper( $source )) {
			case 'HTML':
				$code = highlight_string( deCodeHTML( $code ), true );
				// Remove the first <code>" tags
				if( substr( $code, 0, 6 ) == '<code>') {
					$code = substr( $code, 6, strlen( $code ) - 13);
				}
				break;
			case 'PHP':
				// Check it if code starts with PHP tags, if not: add 'em.
				if( !preg_match( '/^[ 0-9:]*<\?/i', $code )) {
					// The require these tags to function
					$code = "<?php\n".$code."?>";
				}
				$code = highlight_string( $code, true );
				// Replacement-map to replace Colors
				$convmap = [
					// The Default Color
					'#000000">' => '#004A4A">',
					// Color for Functions/Variables/Numbers/&/Constants
					'#006600">' => '#2020FF">',
					// Color for KeyWords
					'#0000CC">' => '#209020">',
					// Color for Constants
					'#FF9900">' => '#BB4040">',
					// Color for Strings
					'#CC0000">' => '#903030">',
				];
				// <-- # Assigned by HighLight_String / --> # Color to be Displayed
				// NOTE: The colors assigned by HighLight_String have changed with different versions of PHP - these are for PHP 4.3.4
				// Change the Colors
				$code = strtr( $code, $convmap );
				break;
			default:
				$code = highlight_string( $code, true );
				break;
		}

		// highlight_string() already wraps in <pre><code> in PHP 8.x
		if( strpos( $code, '<pre>' ) === false ) {
			$code = "<pre>$code</pre>";
		}
	}

	return ( !empty( $title ) ? '<p class="codetitle">'.$title.'</p>' : "" )."<div class='codelisting'>".$code."</div>";
}