fckpackager.php
21.4 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
#!/usr/bin/php -q
<?php
/*
* FCKpackager - JavaScript Packager and Compressor - http://www.fckeditor.net
* Copyright (C) 2003-2008 Frederico Caldeira Knabben
*
* == BEGIN LICENSE ==
*
* Licensed under the terms of any of the following licenses at your
* choice:
*
* - GNU General Public License Version 2 or later (the "GPL")
* http://www.gnu.org/licenses/gpl.html
*
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
* http://www.gnu.org/licenses/lgpl.html
*
* - Mozilla Public License Version 1.1 or later (the "MPL")
* http://www.mozilla.org/MPL/MPL-1.1.html
*
* == END LICENSE ==
*
* This is the main file of FCKpackager.
*
* You can call it through command line with "php fckpackager.php".
*/
echo( "\n" ) ;
echo( 'FCKpackager - JavaScript Packager and Compressor - Version 1.0' . "\n" ) ;
echo( 'Copyright 2004-2008 Frederico Caldeira Knabben - All rights reserved' . "\n" ) ;
echo( "\n" ) ;
// Check the number of arguments passed. The first one is the script name.
if ( count( $argv ) > 2 )
ExitError( 'Invalid arguments. Operation aborted.' ) ;
// Get the package definition file
$xmlFileName = 'fckpackager.xml' ;
if ( isset( $argv[1] ) )
$xmlFileName = $argv[1] ;
$packager = new FCKPackager() ;
$packager->LoadDefinitionFile( $xmlFileName ) ;
$packager->Run() ;
?>
๏ปฟ<?php
function ExitError( $message, $errorNumber = 1 )
{
user_error( $message ) ;
exit( $errorNumber ) ;
}
function StrEndsWith( $str, $sub )
{
return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub ) ;
}
function GetXmlAttribute( $element, $attName, $defValue = '' )
{
if ( !isset( $element->Attributes[ $attName ] ) )
return $defValue ;
return $element->Attributes[ $attName ] ;
}
function CreateDir($path, $rights = 0777)
{
$dirParts = explode( '/', $path ) ;
$currentDir = '' ;
foreach ( $dirParts as $dirPart )
{
$currentDir .= $dirPart . '/' ;
if ( strlen( $dirPart ) > 0 && !is_dir( $currentDir ) )
mkdir( $currentDir, $rights ) ;
}
}
function SaveStringToFile( $strData, $filePath, $includeUtf8Bom = FALSE )
{
$f = fopen( $filePath, 'wb' ) ;
if ( !$f )
return FALSE ;
if ( $includeUtf8Bom )
fwrite( $f, "\xEF\xBB\xBF" ) ; // BOM
fwrite( $f, StripUtf8Bom( $strData ) ) ;
fclose( $f ) ;
return TRUE ;
}
function StripUtf8Bom( $data )
{
if ( substr( $data, 0, 3 ) == "\xEF\xBB\xBF" )
return substr_replace( $data, '', 0, 3 ) ;
return $data ;
}
function GetMicrotime()
{
$timeParts = explode( ' ', microtime() ) ;
return $timeParts[0] + $timeParts[1] ;
}
?>
<?php
class FCKConstantProcessor
{
// Public properties.
var $RemoveDeclaration ;
var $HasConstants ;
// Private properties.
var $_Constants ;
var $_ContantsRegexPart ;
function FCKConstantProcessor()
{
$this->RemoveDeclaration = TRUE ;
$this->HasConstants = FALSE ;
$this->_Constants = array() ;
$this->_ContantsRegexPart = '' ;
}
function AddConstant( $name, $value )
{
if ( strlen( $this->_ContantsRegexPart ) > 0 )
$this->_ContantsRegexPart .= '|' ;
$this->_ContantsRegexPart .= $name ;
$this->_Constants[ $name ] = $value ;
$this->HasConstants = TRUE ;
}
function Process( $script )
{
if ( !$this->HasConstants )
return $script;
$output = $script ;
if ( $this->RemoveDeclaration )
{
// /var\s+(?:BASIC_COLOR_RED|BASIC_COLOR_BLUE)\s*=.+?;/
$output = preg_replace(
'/var\\s+(?:' . $this->_ContantsRegexPart . ')\\s*=.+?;/m',
'', $output ) ;
}
$output = preg_replace_callback(
'/(?<!(var |...\.))(?:' . $this->_ContantsRegexPart . ')(?!(?:\s*=)|\w)/',
array( &$this, '_Contant_Replace_Evaluator' ), $output ) ;
return $output ;
}
function _Contant_Replace_Evaluator( $match )
{
$constantName = $match[0] ;
if ( isset( $this->_Constants[ $constantName ] ) )
return $this->_Constants[ $constantName ] ;
else
return $constantName ;
}
}
?>
<?php
class FCKFunctionProcessor
{
var $_Function ;
var $_Parameters ;
var $_VarChars = array( 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','w','x','y','z' ) ;
var $_VarCharsLastIndex ;
var $_VarPrefix ;
var $_LastCharIndex ;
var $_NextPrefixIndex ;
var $_IsGlobal ;
function FCKFunctionProcessor( $function, $parameters, $isGlobal )
{
$this->_Function = $function ;
$this->_Parameters = $isGlobal ? NULL : $parameters ;
$this->_VarPrefix = $isGlobal ? '_' : '' ;
$this->_IsGlobal = $isGlobal ;
$this->_LastCharIndex = 0;
$this->_NextPrefixIndex = 0;
$this->_VarCharsLastIndex = count( $this->_VarChars ) - 1 ;
}
function Process()
{
$processed = $this->_Function ;
if ( !$this->_IsGlobal )
$processed = $this->_ProcessVars( $processed, $this->_Parameters ) ;
// Match "var" declarations.
$numVarMatches = preg_match_all( '/\bvar\b\s+((?:({(?:(?>[^{}]*)|(?2))*})|[^;])+?)(?=(?:\bin\b)|;)/', $processed, $varsMatches ) ;
if ( $numVarMatches > 0 )
{
$vars = array() ;
for ( $i = 0 ; $i < $numVarMatches ; $i++ )
{
$varsMatch = $varsMatches[1][$i];
// Removed all (...), [...] and {...} blocks from the var
// statement to avoid problems with commas inside them.
$varsMatch = preg_replace( '/(\((?:(?>[^\(\)]*)|(?1))*\))+/', '', $varsMatch ) ;
$varsMatch = preg_replace( '/(\[(?:(?>[^\[\]]*)|(?1))*\])+/', '', $varsMatch ) ;
$varsMatch = preg_replace( '/({(?:(?>[^{}]*)|(?1))*})+/', '', $varsMatch ) ;
$numVarNameMatches = preg_match_all( '/(?:^|,)\s*([^\s=,]+)/', $varsMatch, $varNameMatches ) ;
for ( $j = 0 ; $j < $numVarNameMatches ; $j++ )
{
$vars[] = $varNameMatches[1][$j] ;
}
}
$processed = $this->_ProcessVars( $processed, $vars ) ;
}
return $processed ;
}
function _ProcessVars( $source, $vars )
{
foreach ( $vars as $var )
{
if ( strlen( $var) > 1 )
$source = preg_replace( '/(?<!\w|\d|\.)' . preg_quote( $var ) . '(?!\w|\d)/', $this->_GetVarName(), $source ) ;
}
return $source ;
}
function _GetVarName()
{
if ( $this->_LastCharIndex == $this->_VarCharsLastIndex )
{
$this->_RenewPrefix() ;
$this->_LastCharIndex = 0 ;
}
$var = $this->_VarPrefix . $this->_VarChars[ $this->_LastCharIndex++ ] ;
if ( preg_match( '/(?<!\w|\d|\.)' . preg_quote( $var ) . '(?!\w|\d)/', $this->_Function ) )
return $this->_GetVarName() ;
else
return $var ;
}
function _RenewPrefix()
{
if ( strlen( $this->_VarPrefix) > 0 && $this->_VarPrefix != "_" )
{
if ( $this->_NextPrefixIndex > $this->_VarCharsLastIndex )
$this->_NextPrefixIndex = 0 ;
else
$this->_VarPrefix = substr_replace( $this->_VarPrefix, '', strlen( $this->_VarPrefix ) - 1, 1 ) ;
}
$this->_VarPrefix .= $this->_VarChars[ $this->_NextPrefixIndex ] ;
$this->_NextPrefixIndex++;
}
}
?>
<?php
class FCKJavaScriptCompressor
{
function FCKJavaScriptCompressor()
{}
// Call it statically. E.g.: FCKJavaScriptCompressor::Compress( ... )
function Compress( $script, $constantsProcessor )
{
// Concatenates all string with escaping new lines strings (ending with \).
$script = preg_replace(
'/\\\\[\n\r]+/s',
'\n', $script ) ;
$stringsProc = new FCKStringsProcessor() ;
// Protect the script strings.
$script = $stringsProc->ProtectStrings( $script ) ;
// Remove "/* */" comments
$script = preg_replace(
'/(?<!\/)\/\*.*?\*\//s',
'', $script ) ;
// Remove "//" comments
$script = preg_replace(
'/\/\/.*$/m',
'', $script ) ;
// Remove spaces before the ";" at the end of the lines
$script = preg_replace(
'/\s*(?=;\s*$)/m',
'', $script ) ;
// Remove spaces next to "="
$script = preg_replace(
'/^([^"\'\r\n]*?)\s*=\s*/m',
'$1=', $script ) ;
// Remove spaces on "()": "( content )" = "(content)"
$script = preg_replace(
'/^([^\r\n""\']*?\()\s+(.*?)\s+(?=\)[^\)]*$)/m',
'$1$2', $script ) ;
// Concatenate lines that doesn't end with [;{}] using a space
$script = preg_replace(
'/(?<![;{}\n\r\s])\s*[\n\r]+\s*(?![\s\n\r{}])/s',
' ', $script ) ;
// Concatenate lines that end with "}" using a ";", except for "else",
// "while", "catch" and "finally" cases, or when followed by, "'", ";",
// "}" or ")".
$script = preg_replace(
'/\s*}\s*[\n\r]+\s*(?!\s*(else|catch|finally|while|[}\),;]))/s',
'};', $script ) ;
// Remove blank lines, spaces at the begining or the at the end and \n\r
$script = preg_replace(
'/(^\s*$)|(^\s+)|(\s+$\n)/m',
'', $script ) ;
// Remove the spaces between statements.
$script = FCKJavaScriptCompressor::_RemoveInnerSpaces( $script ) ;
// Process constants. // CHECK
if ( $constantsProcessor->HasConstants )
$script = $constantsProcessor->Process( $script );
// Replace "new Object()".
$script = preg_replace(
'/new Object\(\)/',
'{}', $script ) ;
// Replace "new Array()".
$script = preg_replace(
'/new Array\(\)/',
'[]', $script ) ;
// Process function contents, renaming parameters and variables.
$script = FCKJavaScriptCompressor::_ProcessFunctions( $script ) ;
// Join consecutive string concatened with a "+".
$script = $stringsProc->ConcatProtectedStrings( $script );
// Restore the protected script strings.
$script = $stringsProc->RestoreStrings( $script );
return $script ;
}
function _RemoveInnerSpaces( $script )
{
return preg_replace_callback(
'/(?:\s*[=?:+\-*\/&,;><|!]\s*)|(?:[(\[]\s+)|(?:\s+[)\]])/',
array( 'FCKJavaScriptCompressor', '_RemoveInnerSpacesMatch' ), $script ) ;
}
function _RemoveInnerSpacesMatch( $match )
{
return trim( $match[0] ) ;
}
function _ProcessFunctions( $script )
{
return preg_replace_callback(
'/function(?:\s+\w+)?\s*\(\s*([^\)]*?)\s*\)\s*({(?:(?>[^{}]*)|(?2))*})+/',
array( 'FCKJavaScriptCompressor', '_ProcessFunctionMatch' ), $script ) ;
}
function _ProcessFunctionMatch( $match )
{
// Creates an array with the parameters names ($match[1]).
if ( strlen( trim( $match[1] ) ) == 0 )
$parameters = array() ;
else
$parameters = preg_split( '/\s*,\s*/', trim( $match[1] ) ) ;
$hasfuncProcessor = isset( $GLOBALS['funcProcessor'] ) ;
if ( $hasfuncProcessor != TRUE )
$GLOBALS['funcProcessor'] = new FCKFunctionProcessor( $match[0], $parameters, false ) ;
else
{
$GLOBALS['funcProcessor']->_Function = $match[0];
$GLOBALS['funcProcessor']->_Parameters = $parameters;
}
$processed = $GLOBALS['funcProcessor']->Process() ;
$processed = substr_replace( $processed, '', 0, 8 ) ;
$processed = FCKJavaScriptCompressor::_ProcessFunctions( $processed ) ;
if ( $hasfuncProcessor != TRUE )
unset( $GLOBALS['funcProcessor'] ) ;
return 'function'. $processed ;
}
}
?>
<?php
class FCKPackageFile
{
// Public properties.
var $CompactJavaScript ;
var $RenameGlobals ;
var $Header ;
var $ConstantsProcessor ;
// Private properties.
var $_OutputPath ;
var $_Files ;
function FCKPackageFile( $outputPath )
{
$this->CompactJavaScript = TRUE ;
$this->RenameGlobals = FALSE ;
$this->Header = '' ;
$this->_OutputPath = $outputPath ;
$this->_Files = array() ;
}
function AddFile( $sourceFilePath )
{
$this->_Files[] = $sourceFilePath ;
}
function CreateFile()
{
echo 'Packaging file ' . basename( $this->_OutputPath ) . "\n" ;
// Extract the directory from the output file path.
$destDir = dirname( $this->_OutputPath );
// Create the directory if it doesn't exist.
if ( !@is_dir( $destDir ) )
CreateDir( $destDir ) ;
// Create the StringBuilder that will hold the output data.
$outputData = '' ;
$uncompressedSize = 0 ;
// Loop through the files.
foreach ( $this->_Files as $file )
{
// Read the file.
$data = file_get_contents( $file ) ;
// Strip the UTF-8 BOM, if available.
$data = StripUtf8Bom( $data ) ;
$dataSize = strlen( $data ) ;
$uncompressedSize += $dataSize ;
echo ' Adding ' . basename( $file ) . "\n" ;
// Compress (if needed) and process its contents.
if ( $this->CompactJavaScript )
$outputData .= FCKJavaScriptCompressor::Compress( FCKPreProcessor::Process( $data ), $this->ConstantsProcessor ) ;
else
$outputData .= FCKPreProcessor::Process( $data ) ;
// Each file terminates with a CRLF, even if compressed.
$outputData .= "\r\n" ;
}
// Replace global vars.
if ( $this->RenameGlobals )
{
$funcProcessor = new FCKFunctionProcessor( $outputData, NULL, true ) ;
$outputData = $funcProcessor->Process() ;
}
// Write the output file.
if ( strlen( $this->Header ) > 0 )
$outputData = $this->Header . "\r\n" . $outputData ;
if ( !SaveStringToFile( $outputData, $this->_OutputPath, TRUE ) )
ExitError( 'It was not possible to save the file "' . $this->_OutputPath . '".' ) ;
echo( "\n" );
echo( ' Number of files processed: ' . count( $this->_Files ) . "\n" ) ;
echo( ' Original size............: ' . number_format( $uncompressedSize ) . ' bytes' . "\n" ) ;
echo( ' Output file size.........: ' . number_format( strlen( $outputData ) ) . ' bytes (' . round( strlen( $outputData ) / $uncompressedSize * 100, 2 ) . '% of original)' . "\n" ) ;
echo( "\n" );
}
}
?>
<?php
class FCKPackager
{
var $PackageFiles ;
var $RemoveDeclaration ;
var $_ConstantProcessor ;
var $_TotalFiles ;
function FCKPackager()
{
$this->PackageFiles = array() ;
$this->RemoveDeclaration = true ;
$this->_ConstantProcessor = new FCKConstantProcessor() ;
$this->_TotalFiles = 0 ;
}
function LoadDefinitionFile( $packageDefinitionXmlPath )
{
$XML = new FCKXmlDocument() ;
if ( !$XML->LoadFile( $packageDefinitionXmlPath ) )
ExitError( 'Could not load XML definition file "' . $packageDefinitionXmlPath . '"' ) ;
$this->LoadDefinitionFileXmlDocument( $XML ) ;
}
function LoadDefinitionXml( $packageDefinitionXml )
{
$XML = new FCKXmlDocument() ;
if ( !$XML->LoadXml( $packageDefinitionXml ) )
ExitError( 'Could not load XML data' ) ;
$this->RunXmlDocument( $XML ) ;
}
function LoadDefinitionFileXmlDocument( $packageDefinitionXmlDocument )
{
// Get the root "Package" element.
$packageNode = &$packageDefinitionXmlDocument->Children[ 'PACKAGE' ][0] ;
// Get the Header text.
if ( isset( $packageNode->Children[ 'HEADER' ] ) )
$header = $packageNode->Children[ 'HEADER' ][0]->Value ;
else
$header = 0 ;
// Get the constants (if defined).
$constantsNode = &$packageNode->Children[ 'CONSTANTS' ][0] ;
if ( isset( $constantsNode ) )
{
$this->_ConstantProcessor->RemoveDeclaration = ( GetXmlAttribute( $constantsNode, 'REMOVEDECLARATION', 'true' ) == 'true' ) ;
$constantNodes = &$constantsNode->Children[ 'CONSTANT' ] ;
// Add the constants to the constants processor.
foreach ( $constantNodes as $constantNode )
{
$this->_ConstantProcessor->AddConstant(
$constantNode->Attributes[ 'NAME' ],
$constantNode->Attributes[ 'VALUE' ] ) ;
}
}
// Get the Package Files definitions.
$packageFileNodes = $packageNode->Children[ 'PACKAGEFILE' ] ;
if ( isset( $packageFileNodes ) )
{
$this->_TotalFiles += count( $packageFileNodes ) ;
// Loop through the package files.
foreach ( $packageFileNodes as $packageFileNode )
{
// Create the package file instance.
$file = new FCKPackageFile( $packageFileNode->Attributes[ 'PATH' ] ) ;
$file->CompactJavaScript = ( GetXmlAttribute( $packageFileNode, 'COMPACTJAVASCRIPT', 'true' ) == 'true' ) ;
$file->RenameGlobals = ( GetXmlAttribute( $packageFileNode, 'RENAMEGLOBALS', 'false' ) == 'true' ) ;
$file->Header = $header ;
$file->ConstantsProcessor = &$this->_ConstantProcessor ;
// Get all files defined for that package file.
$fileNodes = $packageFileNode->Children[ 'FILE' ] ;
if ( isset( $fileNodes ) )
{
// Loop throwgh the files.
foreach ( $fileNodes as $fileNode )
{
$file->AddFile( $fileNode->Attributes[ 'PATH' ] ) ;
}
}
$this->PackageFiles[] = $file ;
}
}
}
function Run()
{
$startTime = GetMicrotime() ;
foreach ( $this->PackageFiles as $packageFile )
{
$packageFile->CreateFile() ;
}
$execTime = GetMicrotime() - $startTime ;
$execTime = number_format( $execTime, 10 ) ;
switch ( $this->_TotalFiles )
{
case 0 :
echo( 'No files defined' ) ;
break;
case 1 :
echo( 'The generation of the package file has been completed in ' . $execTime . ' seconds.' ) ;
break;
default :
echo( 'The generation of ' . $this->_TotalFiles . ' files has been completed in ' . $execTime . ' seconds.' ) ;
break;
}
}
}
?>
<?php
class FCKPreProcessor
{
function FCKPreProcessor()
{}
// Call it statically. E.g.: FCKPreProcessor::ProcessFile( ... )
function ProcessFile( $sourceFilePath, $destinationFilePath, $onlyHeader = FALSE )
{
SaveStringToFile(
FCKPreProcessor::Process( file_get_contents( $sourceFilePath ), $onlyHeader ),
$destinationFilePath,
( StrEndsWith( $sourceFilePath, '.asp' ) || StrEndsWith( $sourceFilePath, '.js' ) ) ) ; // Only ASP and JavaScript files require the BOM.
// Set the destination file Last Access and Last Write times.
// It seams we can't change the creation time with PHP.
touch( $destinationFilePath, filemtime( $sourceFilePath ), fileatime( $sourceFilePath ) ) ;
}
// Call it statically. E.g.: FCKPreProcessor::Process( ... )
function Process( $data, $onlyHeader = false )
{
if ( ! $onlyHeader )
{
// Remove everything between the @Packager.Remove.Start and
// @Packager.Remove.End clauses including the clauses lines.
$data = preg_replace(
'/(?m-s:^.*?@Packager\.Remove\.Start).*?(?m-s:@Packager\.Remove\.End.*?$\n?)/is',
'', $data ) ;
// Remove all lines containing the @Packager.RemoveLine clause.
$data = preg_replace(
'/^.*@Packager\.RemoveLine.*$\n?/im',
'', $data ) ;
}
// Fix invalid line breaks (must be all CRLF).
$data = preg_replace(
'/(?:(?<!\r)\n)|(?:\r(?!\n))/im',
"\r\n", $data ) ;
return $data ;
}
}
?>
<?php
class FCKStringsProcessor
{
var $_ProtectedStrings ;
function FCKStringsProcessor()
{
$_ProtectedStrings = array() ;
}
function ProtectStrings( $source )
{
// Catches string literals, regular expressions and conditional comments.
return preg_replace_callback(
'/(?:("|\').*?(?<!\\\\)\1)|(?:(?<![\*\/\\\\])\/[^\/\*].*?(?<!\\\\)\/(?=([\.\w])|(\s*[,;}\)])))|(?s:\/\*@(?:cc_on|if|elif|else|end).*?@\*\/)/',
array( &$this, '_ProtectStringsMatch' ), $source ) ;
}
function _ProtectStringsMatch( $match )
{
$this->_ProtectedStrings[] = $match[0] ;
return '@' . ( count( $this->_ProtectedStrings ) - 1 ) . '@' ;
}
function ConcatProtectedStrings( $source )
{
return preg_replace_callback(
'/@\d+@(?>@\d+@|\+)+@\d+@/',
array( &$this, '_ConcatProtectedStringsMatch' ), $source ) ;
}
function _ConcatProtectedStringsMatch( $match )
{
// $match[0] is something like @2@+@3@+@4@+@5@
$indexes = explode( '@+@', trim( $match[0], '@') ) ;
$leftIndex = (int)$indexes[0] ;
$rightPosition = 1 ;
$output = '@' . $leftIndex . '@' ;
while( $rightPosition < count( $indexes ) )
{
$rightIndex = (int)$indexes[ $rightPosition ] ;
$left = $this->_ProtectedStrings[ $leftIndex ] ;
$right = $this->_ProtectedStrings[ $rightIndex ] ;
if ( strncmp( $left, $right, 1 ) == 0 )
{
$left = substr_replace( $left, '', strlen( $left ) - 1, 1 ) ;
$right = substr_replace( $right, '', 0, 1 ) ;
$this->_ProtectedStrings[ $leftIndex ] = $left . $right ;
$this->_ProtectedStrings[ $rightIndex ] = '' ;
}
else
{
$leftIndex = $rightIndex ;
$output .= '+@' . $leftIndex . '@' ;
}
$rightPosition++ ;
}
return $output ;
}
function RestoreStrings( $source )
{
return preg_replace_callback(
'/@(\d+)@/',
array( &$this, '_RestoreStringsMatch' ), $source ) ;
}
function _RestoreStringsMatch( $match )
{
return $this->_ProtectedStrings[ (int)$match[1] ] ;
}
}
?>
<?php
class FCKXmlDocument
{
// Public properties.
var $Children ;
// Private properties.
var $_XmlParser ;
var $_CurrentNode ;
function FCKXmlDocument()
{
$this->Children = array() ;
}
function LoadFile( $filePath )
{
$this->Children = array() ;
$this->_CurrentNode = &$this ;
return $this->LoadXml( file_get_contents( $filePath ) ) ;
}
function LoadXml( $xml )
{
// Create the XML Parser.
$this->_XmlParser = xml_parser_create( '' ) ;
// Setup the parser.
xml_parser_set_option( $this->_XmlParser, XML_OPTION_SKIP_WHITE, 1 ) ;
xml_set_object( $this->_XmlParser, $this ) ;
xml_set_element_handler( $this->_XmlParser, '_ElementOpen', '_ElementClosed' ) ;
xml_set_character_data_handler( $this->_XmlParser, '_ElementData' ) ;
// Parse it.
if( !xml_parse( $this->_XmlParser, $xml ) )
{
ExitError( sprintf( "XML error: %s at line %d",
xml_error_string(xml_get_error_code( $this->_XmlParser ) ),
xml_get_current_line_number( $this->_XmlParser ) ) ) ;
}
// Release the parser.
xml_parser_free( $this->_XmlParser ) ;
unset( $this->_XmlParser ) ;
unset( $this->_CurrentNode ) ;
// For debug purposes:
// SaveStringToFile( print_r( $this, TRUE ), 'parsed.txt' ) ;
// print_r( $this ) ;
// exit ;
return TRUE ;
}
function _ElementOpen( $parser, $name, $attrs )
{
$newNode = (object)array(
'Parent' => &$this->_CurrentNode,
'Name' => $name,
'Attributes' => $attrs,
'Value' => '',
'Children' => array() ) ;
$this->_CurrentNode->Children[ $name ][] = &$newNode ;
$this->_CurrentNode = &$newNode ;
}
function _ElementClosed( $parser, $name )
{
$this->_CurrentNode = &$this->_CurrentNode->Parent ;
}
function _ElementData( $parser, $data )
{
$this->_CurrentNode->Value .= $data ;
}
}
?>