Subversion Repositories DevTools

Rev

Go to most recent revision | Details | 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;
52
use File::Path qw(make_path remove_tree);
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");
149
    remove_tree(  $workfile, $logfile );
150
    Message ("Cleaning: $outpath");
151
    remove_tree( $outpath );
152
    exit 0;
153
}
154
 
155
#
156
#   Not cleaning
157
#   Perform a little bit ore sanity testing
158
#
159
Verbose("Script : $opt_script");
160
Verbose("Target : $opt_target");
161
Verbose("LogFile: $logfile");
162
Verbose("SrcDir : $opt_srcdir");
163
Verbose("WrkDir : $opt_objdir");
164
Verbose("OutDir : $outpath");
165
 
166
Error ("Script not found: $opt_script") unless (-f $opt_script);
167
Error ("Work directory not found") unless (-d $opt_objdir);
168
 
169
#
170
#   Ensure that the required version of ZendGaurd can be found
171
#       Try User EnvVar ZENDGUARD_5_5
172
#       Else Default install location
173
#
174
my $guard5_env_path = $ENV{ZENDGUARD_5_5} || '';
175
my $default_guard5_path = $ENV{'PROGRAMFILES'} . '/Zend/Zend Guard - 5.5.0';
176
my $guardengine_path = $guard5_env_path || $default_guard5_path;
177
$guardengine_path =~ tr~\\~/~s;
178
Verbose ("GuardPath: $guardengine_path");
179
if ( ! -d $guardengine_path )
180
{
181
    Error( "ZendGuard not installed",
182
           "Zendguard tools requires that Version 5.5 of ZendGard be installed.",
183
           "Non default installation may be configured via EnvVar",
184
           "EnvVar ZENDGUARD_5_5: $guard5_env_path",
185
           "Default Path: $default_guard5_path");
186
 
187
}
188
 
189
#
190
#   Now get the subdir for the guard executable
191
#
192
$guardengine_path .= '/plugins/com.zend.guard.core.resources.win32.x86_5.5.0/resources';
193
Error ("Plugin directory not found", $guardengine_path) unless ( -d $guardengine_path );
194
 
195
$guardengine_path .= '/GuardEngine.exe';
196
Error ("Guard Engine not found", $guardengine_path) unless ( -f $guardengine_path );
197
Verbose2 ("GuardEngine: $guardengine_path");
198
 
199
#
200
#   Convert some paths to absolute paths
201
#   ZendGuard does appear to be worried about the use of '/' intead of '\'
202
#   My guess is that its happy with both - so use / as its easier
203
#
204
InitFileUtils();
205
$outpath = FullPath($outpath);
206
$opt_srcdir = FullPath($opt_srcdir);
207
 
208
#
209
#   Process the script file. Need to:
210
#       1) set the output directory
211
#       2) Set source directories
212
#       3) Capture unexpected styles
213
#
214
#   The script is an XML file, but parsing it as an XML file and then
215
#   reconstructing the file is hard work. Would need to know all about the xsd
216
#   of the file too.
217
#
218
#   Try the simple approach first
219
#   Asume lines are well formed and not split
220
#
221
my @bad_paths;
222
my $root_dir_seen;
223
open (my $out,'>', $workfile )    || Error ("Can't open output: $workfile. $!");
224
open (my $in, '<', $opt_script ) || Error ("Can't open input: $opt_script: $!");
225
while ( <$in> )
226
{
227
    #
228
    #   Chomp white space at the end of the line
229
    #   Remove unix and windows lined endings
230
    #
231
    s~\s+$~~;
232
 
233
    #
234
    #   Target Dir
235
    #   Force to be the output directory
236
    #
237
    if ( m~(targetDir=")(.*?)(")~ )
238
    {
239
        Verbose("TargetDir: $2");
240
        s~(targetDir=")(.*?)(")~$1$outpath$3~;
241
    }
242
 
243
    #
244
    #   Source Path directives
245
    #   Must start with $(RootDir)
246
    #
247
    if ( m~<source path="(.*?)">~  )
248
    {
249
        Verbose("PATH: $1");
250
        if ( $1 !~ m~^\$\(RootDir\)~ )
251
        {
252
            push @bad_paths, $_;
253
        }
254
    }
255
 
256
    #
257
    #   Global Variables
258
    #   Only interested in RootDir
259
    #
260
    #
261
    if ( m~<variable name="(.*?)">(.*?)</variable>~ )
262
    {
263
        Verbose("Variable: $1, $2");
264
        if ( $1 eq 'RootDir' )
265
        {
266
            s~(<variable name=".*?">).*?(</variable>)~$1$opt_srcdir$2~;
267
            $root_dir_seen = 1;
268
        }
269
    }
270
 
271
    #
272
    #   Output the modified line
273
    #
274
    print $out $_ . "\n";
275
}
276
close $in;
277
close $out;
278
 
279
#
280
#   Report any error that were seen during the file re-write
281
#
282
ReportError("Expected a global variables called 'RootDir'", "Not was seen")
283
    unless ( $root_dir_seen  );
284
 
285
ReportError('The source paths must be specified relative to $(RootDir)',
286
            'The following elements did not have relative references',
287
            @bad_paths ) if (@bad_paths );
288
ErrorDoExit();
289
 
290
 
291
#
292
#   Create the target directory
293
#
294
Verbose2("Create packing path: $outpath");
295
make_path ( $outpath );
296
remove_tree( $logfile );
297
 
298
#
299
#   Run the required program
300
#
301
my $rv = System ('--NoExit','--Shell' ,$guardengine_path, '--xml-file', $workfile, "2>$logfile" );
302
 
303
#
304
#   Dump the log file if required
305
#
306
System ('cat', $logfile ) if ( ($rv && $rv != $no_license_error) || IsVerbose(2) );
307
 
308
#
309
#   Error reported
310
#   See if we allow this error
311
#
312
if ( $rv )
313
{
314
    Warning( '-' x 80, 'Licence may have expired', '-' x 80) if ( $rv == $no_license_error );
315
    my $elist = $ENV{ZENDGUARD_IGNORE} || '';
316
    if ( grep ($rv eq $_, split(/,/,$elist)) )
317
    {
318
        Warning("GaurdEngine error ignored: $rv");
319
    }
320
    else
321
    {
322
        Error ("GuardEngine error previously reported: $rv");
323
    }
324
}
325
 
326
 
327
Message ("Done");
328
1;