Subversion Repositories DevTools

Rev

Rev 1684 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1684 dpurdie 1
########################################################################
2
# Copyright (C) 2010 Vix-ERG Limited, All rights reserved
3
#
4
# Module name   : ZendGuard.pl
5
# Module type   : Makefile system
6
# Compiler(s)   : Perl
7
# Environment(s): jats
8
#
9
# Description   : See: TECHGP-00254 Jats Integration of ZendGuard
10
 
11
#                 JATS wrapper to invoke the ZendGuard GuardEngine
12
#                 to process a directory of PHP files.
13
#
14
#                 Several assumptions
15
#
16
#                 ZendGaurd 5.5 is installed.
17
#
18
#                 EnvVars used:
19
#                   PROGRAMFILES            - Windows 'Program Files'
20
#                   ZENDGUARD_5_5           - Alternate install location
21
#                   ZENDGUARD_IGNORE        - Comma sep list of errors to ignore
22
#
23
#                 GuardEngine generates a 99 error for no license
24
#                 Need to test all of this against a valid copy
25
#
26
#                 Will be invoked from a JATS makefile at 'make' time
27
#                 This is not a stand alone utility. Its designed to be
28
#                 driven from the Jats Directive: MakeZendGuard(...)
29
#
30
#                 The guard.xml file conatins all that is needed.
31
#                 The format of the file MUST
32
#
33
#                   Have a targetDir attribute
34
#
35
#                   Global variable called RootDir
36
#
37
#                   All source paths referenced from RootDir
38
#
39
#                 Notes on the guard.xml file
40
#                 The order of the source directives is important
41
#                 Copy-as-is operations are order sensitive
42
#                 They need to be done AFTER the bulk encode operations
43
#
44
#......................................................................#
45
 
46
require 5.008_002;
47
use strict;
48
use warnings;
49
 
50
use Pod::Usage;
51
use Getopt::Long;
1686 dpurdie 52
use File::Path;
1684 dpurdie 53
 
54
use JatsError;
55
use FileUtils;
56
use JatsSystem;
57
 
58
#
59
#   Option Values
60
#
61
my $opt_verbose = 0;
62
my $opt_clean;
63
my $opt_pkgdir;
64
my $opt_target;
65
my $opt_type;
66
my $opt_script;
67
my $opt_objdir;
68
my $opt_bindir;
69
my $opt_srcdir = '.';
70
my $opt_pkg = '';
71
 
72
#
73
#   Globals
74
#
75
my $workfile;
76
my $logfile;
77
my $outpath;
78
my $no_license_error = 99;      # Note: Only tested on a system without a license
79
 
80
 
81
#-------------------------------------------------------------------------------
82
# Function        : Main entry point
83
#
84
# Description     : Parse user arguments
85
#                   Generate metrics
86
#
87
# Inputs          : None
88
#
89
# Returns         : Error code
90
#
91
 
92
my $result = GetOptions (
93
                "verbose:+"     => \$opt_verbose,           # flag, multiple use allowed
94
                "clean"         => \$opt_clean,
95
                "PackageDir:s"  => \$opt_pkgdir,
96
                "Target:s"      => \$opt_target,
97
                "Type:s"        => \$opt_type,
98
                "script:s"      => \$opt_script,
99
                "ObjDir:s"      => \$opt_objdir,
100
                "BinDir:s"      => \$opt_bindir,
101
                "Src:s"         => \$opt_srcdir,
102
                "Package:s"     => \$opt_pkg,
103
                );
104
 
105
                #
106
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
107
                #
108
 
109
ErrorConfig( 'name'    =>'ZendGuard',
110
             'verbose' => $opt_verbose,
111
            );
112
Error ("Internal: Invalid options passed")
113
    unless ($result);
114
 
115
#
116
#   Determine output path
117
#   If --Package has been specified then it will be within the package
118
#   Otherwise it will be created locally to allow it to be consumed by
119
#   an installer
120
#
121
if ( $opt_pkg ) {
122
    $outpath = join( '/', $opt_pkgdir, 'pkg' ,$opt_pkg );
123
} else {
124
    $outpath = join( '/', $opt_bindir, 'php' );
125
}
126
 
127
#
128
#   Sanity tests and calculated paths
129
#
130
Error ("No script provided") unless ($opt_script);
131
Error ("No Packaging directory provided") unless ($opt_pkgdir);
132
Error ("No Bindir directory provided") unless ($opt_bindir);
133
Error ("Packaging subdirectory cannot have relative path component: $opt_pkg")
134
    if ( $opt_pkg && $opt_pkg =~ m~\.\./~ );
135
 
136
#
137
#   Calc values
138
#
139
$workfile = join( '/', $opt_objdir, 'GuardEngine.xml' );
140
$logfile  = join( '/', $opt_objdir, 'GuardEngine.log' );
141
 
142
#
143
#   Process Clean operations
144
#   Delete every thingwe create
145
#
146
if ( $opt_clean )
147
{
148
    Message ("Cleaning: $workfile, $logfile");
1686 dpurdie 149
    RmDirTree( $workfile );
150
    RmDirTree( $logfile );
1684 dpurdie 151
    Message ("Cleaning: $outpath");
1686 dpurdie 152
    RmDirTree( $outpath );
1684 dpurdie 153
    exit 0;
154
}
155
 
156
#
157
#   Not cleaning
158
#   Perform a little bit ore sanity testing
159
#
160
Verbose("Script : $opt_script");
161
Verbose("Target : $opt_target");
162
Verbose("LogFile: $logfile");
163
Verbose("SrcDir : $opt_srcdir");
164
Verbose("WrkDir : $opt_objdir");
165
Verbose("OutDir : $outpath");
166
 
167
Error ("Script not found: $opt_script") unless (-f $opt_script);
168
Error ("Work directory not found") unless (-d $opt_objdir);
169
 
170
#
171
#   Ensure that the required version of ZendGaurd can be found
172
#       Try User EnvVar ZENDGUARD_5_5
173
#       Else Default install location
174
#
175
my $guard5_env_path = $ENV{ZENDGUARD_5_5} || '';
176
my $default_guard5_path = $ENV{'PROGRAMFILES'} . '/Zend/Zend Guard - 5.5.0';
177
my $guardengine_path = $guard5_env_path || $default_guard5_path;
178
$guardengine_path =~ tr~\\~/~s;
179
Verbose ("GuardPath: $guardengine_path");
180
if ( ! -d $guardengine_path )
181
{
182
    Error( "ZendGuard not installed",
183
           "Zendguard tools requires that Version 5.5 of ZendGard be installed.",
184
           "Non default installation may be configured via EnvVar",
185
           "EnvVar ZENDGUARD_5_5: $guard5_env_path",
186
           "Default Path: $default_guard5_path");
187
 
188
}
189
 
190
#
191
#   Now get the subdir for the guard executable
192
#
193
$guardengine_path .= '/plugins/com.zend.guard.core.resources.win32.x86_5.5.0/resources';
194
Error ("Plugin directory not found", $guardengine_path) unless ( -d $guardengine_path );
195
 
196
$guardengine_path .= '/GuardEngine.exe';
197
Error ("Guard Engine not found", $guardengine_path) unless ( -f $guardengine_path );
198
Verbose2 ("GuardEngine: $guardengine_path");
199
 
200
#
201
#   Convert some paths to absolute paths
202
#   ZendGuard does appear to be worried about the use of '/' intead of '\'
203
#   My guess is that its happy with both - so use / as its easier
204
#
205
InitFileUtils();
206
$outpath = FullPath($outpath);
207
$opt_srcdir = FullPath($opt_srcdir);
208
 
209
#
210
#   Process the script file. Need to:
211
#       1) set the output directory
212
#       2) Set source directories
213
#       3) Capture unexpected styles
214
#
215
#   The script is an XML file, but parsing it as an XML file and then
216
#   reconstructing the file is hard work. Would need to know all about the xsd
217
#   of the file too.
218
#
219
#   Try the simple approach first
220
#   Asume lines are well formed and not split
221
#
222
my @bad_paths;
223
my $root_dir_seen;
224
open (my $out,'>', $workfile )    || Error ("Can't open output: $workfile. $!");
225
open (my $in, '<', $opt_script ) || Error ("Can't open input: $opt_script: $!");
226
while ( <$in> )
227
{
228
    #
229
    #   Chomp white space at the end of the line
230
    #   Remove unix and windows lined endings
231
    #
232
    s~\s+$~~;
233
 
234
    #
235
    #   Target Dir
236
    #   Force to be the output directory
237
    #
238
    if ( m~(targetDir=")(.*?)(")~ )
239
    {
240
        Verbose("TargetDir: $2");
241
        s~(targetDir=")(.*?)(")~$1$outpath$3~;
242
    }
243
 
244
    #
245
    #   Source Path directives
246
    #   Must start with $(RootDir)
247
    #
248
    if ( m~<source path="(.*?)">~  )
249
    {
250
        Verbose("PATH: $1");
251
        if ( $1 !~ m~^\$\(RootDir\)~ )
252
        {
253
            push @bad_paths, $_;
254
        }
255
    }
256
 
257
    #
258
    #   Global Variables
259
    #   Only interested in RootDir
260
    #
261
    #
262
    if ( m~<variable name="(.*?)">(.*?)</variable>~ )
263
    {
264
        Verbose("Variable: $1, $2");
265
        if ( $1 eq 'RootDir' )
266
        {
267
            s~(<variable name=".*?">).*?(</variable>)~$1$opt_srcdir$2~;
268
            $root_dir_seen = 1;
269
        }
270
    }
271
 
272
    #
273
    #   Output the modified line
274
    #
275
    print $out $_ . "\n";
276
}
277
close $in;
278
close $out;
279
 
280
#
281
#   Report any error that were seen during the file re-write
282
#
283
ReportError("Expected a global variables called 'RootDir'", "Not was seen")
284
    unless ( $root_dir_seen  );
285
 
286
ReportError('The source paths must be specified relative to $(RootDir)',
287
            'The following elements did not have relative references',
288
            @bad_paths ) if (@bad_paths );
289
ErrorDoExit();
290
 
291
 
292
#
293
#   Create the target directory
294
#
295
Verbose2("Create packing path: $outpath");
1686 dpurdie 296
mkpath ( $outpath );
297
RmDirTree( $logfile );
1684 dpurdie 298
 
299
#
300
#   Run the required program
301
#
302
my $rv = System ('--NoExit','--Shell' ,$guardengine_path, '--xml-file', $workfile, "2>$logfile" );
303
 
304
#
305
#   Dump the log file if required
306
#
307
System ('cat', $logfile ) if ( ($rv && $rv != $no_license_error) || IsVerbose(2) );
308
 
309
#
310
#   Error reported
311
#   See if we allow this error
312
#
313
if ( $rv )
314
{
315
    Warning( '-' x 80, 'Licence may have expired', '-' x 80) if ( $rv == $no_license_error );
316
    my $elist = $ENV{ZENDGUARD_IGNORE} || '';
317
    if ( grep ($rv eq $_, split(/,/,$elist)) )
318
    {
319
        Warning("GaurdEngine error ignored: $rv");
320
    }
321
    else
322
    {
323
        Error ("GuardEngine error previously reported: $rv");
324
    }
325
}
326
 
327
 
328
Message ("Done");
329
1;