1package CallExternals;
2
3#*************************************************************************
4#
5# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6#
7# Copyright 2000, 2010 Oracle and/or its affiliates.
8#
9# OpenOffice.org - a multi-platform office productivity suite
10#
11# This file is part of OpenOffice.org.
12#
13# OpenOffice.org is free software: you can redistribute it and/or modify
14# it under the terms of the GNU Lesser General Public License version 3
15# only, as published by the Free Software Foundation.
16#
17# OpenOffice.org is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU Lesser General Public License version 3 for more details
21# (a copy is included in the LICENSE file that accompanied this code).
22#
23# You should have received a copy of the GNU Lesser General Public License
24# version 3 along with OpenOffice.org.  If not, see
25# <http://www.openoffice.org/license.html>
26# for a copy of the LGPLv3 License.
27#
28#*************************************************************************
29
30use English;
31use warnings;
32use strict;
33use loghelper;
34
35BEGIN {
36    use Exporter   ();
37    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
38
39    $VERSION     = 1.00;
40    # if using RCS/CVS, this may be preferred
41    $VERSION = do { my @r = (q$Revision: 1.29 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
42    @ISA         = qw(Exporter);
43    @EXPORT      = qw(&callphp &getPHPExecutable &ExecSQL &callperl &getPerlExecutable &calljava &setJavaExecutable &getJavaExecutable &setToolsPath &quote &quoteIfNeed &set_logfile &close_logfile );
44    %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
45    # your exported package globals go here,
46    # as well as any optionally exported functions
47    @EXPORT_OK   = ( ); # qw($Var1 %Hashit &func3);
48}
49
50# ------------------------------------------------------------------------------
51# small helper, which replaces the return code
52sub errorAdaption($)
53{
54    my $error = shift;
55    if ($error != 0)
56    {
57        $error = $error / 256;
58    }
59    if ($error > 127)
60    {
61        $error = $error - 256;
62    }
63    return $error;
64}
65# ------------------------------------------------------------------------------
66# helper to call external php with popen
67sub callphp($$$)
68{
69    local *IN_FILE;
70    my $phpexe = shift;
71    my $phpprogram = shift;
72    my $sParams = shift;
73    my $line;
74    my $error;
75    my @result;
76
77    # print "Will send: $phpexe $sParams\n";
78    # log_print("CALLPHP: $phpexe $phpprogram $sParams\n");
79#    if (open(IN_FILE, "$phpexe $sParams 2>&1 |"))
80    if (open(IN_FILE, "$phpexe $phpprogram $sParams |"))
81    {
82        while ($line = <IN_FILE>)
83        {
84            chomp($line);
85            # $line .= " ";
86            push(@result, $line);
87            # print "callphp output: $line\n";
88        }
89        close(IN_FILE);
90        $error = errorAdaption($?);
91    }
92    else
93    {
94        print "callphp(): Can't popen '$phpexe' with parameter: '$sParams'\n";
95        $error = 1;
96    }
97    return $error, @result;
98}
99
100# ------------------------------------------------------------------------------
101sub getPHPExecutable()
102{
103    my $phpexe;
104    if ($OSNAME eq "solaris")
105    {
106        $phpexe = "php5";
107    }
108    elsif ($OSNAME eq "linux")
109    {
110        if ( -e "/usr/bin/php5") # Suse :-(
111        {
112            $phpexe = "php5";
113        }
114        elsif ( -e "/usr/bin/php") # Gentoo
115        {
116            $phpexe = "php";
117        }
118        else
119        {
120            print "getPHPExecutable(): no php exec found.\n";
121        }
122    }
123    elsif ( $OSNAME eq "MSWin32" )
124    {
125        $phpexe = "C:/programme/php/php.exe";
126        # add second try (xampp)
127        if (! -e $phpexe)
128        {
129            $phpexe = "C:/xampp/php/php.exe";
130        }
131    }
132    elsif ( $OSNAME eq "cygwin" )
133    {
134        $phpexe = "/cygdrive/c/programme/php/php";
135    }
136    else
137    {
138        print "getPHPExecutable(): unknown environment. ($OSNAME)\n";
139    }
140    if (! $phpexe)
141    {
142        print "getPHPExecutable(): ERROR: php executable not found.\n";
143        exit(1);
144    }
145    return $phpexe;
146}
147# ------------------------------------------------------------------------------
148# helper to call external java with popen
149sub calljava($$$)
150{
151    local *IN_FILE;
152    my $javaexe = shift;
153    my $sParams = shift;
154    my $sDebug = shift;
155    my $line;
156    my $error = 1;
157
158    if (! $javaexe)
159    {
160        log_print("ERROR: javaexe not set.\n");
161        return;
162    }
163    if (! $sDebug)
164    {
165        $sDebug = "";
166    }
167    $javaexe = quoteIfNeed($javaexe);
168    log_print ("CALLJAVA: $javaexe $sDebug $sParams\n");
169    if (open(IN_FILE, "$javaexe $sDebug $sParams 2>&1 |"))
170    {
171        while ($line = <IN_FILE>)
172        {
173            chomp($line);
174            log_print ("- $line\n");
175        }
176        close(IN_FILE);
177        $error = errorAdaption($?);
178    }
179    else
180    {
181        log_print ("calljava(): Can't popen '$javaexe' with parameter '$sParams'\n");
182        $error = 1;
183    }
184    return $error;
185}
186
187# ------------------------------------------------------------------------------
188sub getPerlExecutable()
189{
190    my $perlexe;
191    if ( $ENV{PERL} )
192    {
193        $perlexe = $ENV{PERL};
194    }
195    elsif ( $ENV{PERLEXE} )
196    {
197        $perlexe = $ENV{PERLEXE};
198    }
199    else
200    {
201        if ($OSNAME eq "MSWin32")
202        {
203            $perlexe="C:/xampp/perl/bin/perl.exe";
204            if (! -e $perlexe)
205            {
206                $perlexe="r:/btw/perl/bin/perl";
207            }
208            if (! -e $perlexe)
209            {
210                $perlexe="C:/Programme/Perl/bin/perl.exe";
211            }
212        }
213        elsif ($OSNAME eq "cygwin")
214        {
215            $perlexe = "perl";
216        }
217        elsif ($OSNAME eq "solaris")
218        {
219            $perlexe="/so/env/bt_solaris_intel/bin/perl";
220        }
221        elsif ($OSNAME eq "linux")
222        {
223            $perlexe="/so/env/bt_linux_libc2.32/DEV300/bin/perl";
224        }
225        else
226        {
227            log_print "WARNING: Use only the fallback of perl executable.\n";
228            $perlexe = "perl"; # FALLBACK
229        }
230    }
231    if ( ! -e $perlexe)
232    {
233        log_print "getPerlExecutable(): There exist no perl executable.\n";
234        exit(1);
235    }
236    return $perlexe;
237}
238# ------------------------------------------------------------------------------
239# helper to call external perl with popen
240sub callperl($$$)
241{
242    local *IN_FILE;
243    my $perlexe = shift;
244    my $perlprogram = shift;
245    my $sParams = shift;
246    my $line;
247    my $error;
248
249    log_print("CALLPERL: $perlexe $perlprogram $sParams\n");
250#    if (open(IN_FILE, "$perlexe $sParams 2>&1 |"))
251    if (open(IN_FILE, "$perlexe $perlprogram $sParams |"))
252    {
253        while ($line = <IN_FILE>)
254        {
255            chomp($line);
256            log_print ("- $line\n");
257        }
258        close(IN_FILE);
259        $error = errorAdaption($?);
260    }
261    else
262    {
263        log_print ("Can't popen '$perlexe' with parameter: '$sParams'\n");
264        $error = 1;
265    }
266    return $error;
267}
268# ------------------------------------------------------------------------------
269our $sJavaExecutable;
270sub setJavaExecutable($)
271{
272    $sJavaExecutable = shift;
273}
274
275# sub getJava14()
276# {
277#     my $sJava14;
278#     if ($OSNAME eq "MSWin32")
279#     {
280#         if ($sJavaExecutable)
281#         {
282#             $sJava14 = $sJavaExecutable;
283#         }
284#         else
285#         {
286#             # HARDCODE!
287#             $sJava14 = "C:\\Programme\\Java\\j2re1.4.2_10\\bin\\java.exe";
288#         }
289#     }
290#     else
291#     {
292#         if ($sJavaExecutable)
293#         {
294#             $sJava14 = $sJavaExecutable;
295#         }
296#         else
297#         {
298#             # HARDCODE!
299#             $sJava14 = "/opt/java14/bin/java";
300#         }
301#     }
302#     if ( ! -e $sJava14 )
303#     {
304#         log_print ("Java14 not found. Is searched in '$sJava14'\n");
305#         # exit(1);
306#         return "";
307#     }
308#     return $sJava14;
309# }
310# ------------------------------------------------------------------------------
311sub getJava15()
312{
313    my $sJava15;
314    if ($sJavaExecutable)
315    {
316        $sJava15 = $sJavaExecutable;
317    }
318    else
319    {
320        if ($OSNAME eq "MSWin32")
321        {
322            # HARDCODE!
323            $sJava15 = "C:\\Programme\\Java\\jre1.5.0_22\\bin\\java.exe";
324            if ( ! -e $sJava15)
325            {
326                $sJava15 = "C:\\Program Files\\Java\\jre6\\bin\\java.exe";
327            }
328            if ( ! -e $sJava15)
329            {
330                $sJava15 = "C:\\Java\\jdk1.6\\bin\\java.exe";
331            }
332        }
333        elsif ($OSNAME eq "cygwin")
334        {
335            $sJava15 = "java";
336        }
337        else
338        {
339            # HARDCODE!
340            if ($OSNAME eq "solaris")
341            {
342                $sJava15 = "/usr/bin/java";
343            }
344            else
345            {
346                $sJava15 = "/usr/bin/java";
347                if ( ! -e $sJava15 )
348                {
349                    $sJava15 = "/opt/java15/bin/java";
350                }
351            }
352        }
353        if ( ! -e $sJava15 )
354        {
355            log_print ("Java15 not found. Is searched in '$sJava15'\n");
356            # exit(1);
357            return "";
358        }
359    }
360    return $sJava15;
361}
362# ------------------------------------------------------------------------------
363sub getJava16()
364{
365    my $sJava16;
366    if ($sJavaExecutable)
367    {
368        $sJava16 = $sJavaExecutable;
369    }
370    else
371    {
372        if ($OSNAME eq "MSWin32")
373        {
374            # HARDCODE!
375            $sJava16 = "C:\\Programme\\Java\\jre1.6.0_16\\bin\\java.exe";
376            if ( ! -e $sJava16)
377            {
378                $sJava16 = "C:\\Program Files\\Java\\jre6\\bin\\java.exe";
379            }
380            if ( ! -e $sJava16)
381            {
382                $sJava16 = "C:\\Java\\jdk1.6\\bin\\java.exe";
383            }
384            # }
385        }
386        elsif ($OSNAME eq "cygwin")
387        {
388            # $sJava16 = "java";
389            $sJava16 = "C:/Program Files/Java/jdk1.6.0_16/bin/java.exe";
390        }
391        else
392        {
393            # HARDCODE!
394            if ($OSNAME eq "solaris")
395            {
396                $sJava16 = "/usr/bin/java";
397            }
398            else
399            {
400                $sJava16 = "/usr/bin/java";
401                if ( ! -e $sJava16 )
402                {
403                    $sJava16 = "/opt/java16/bin/java";
404                }
405            }
406        }
407        if ( ! -e $sJava16 )
408        {
409            log_print ("Java16 not found. Is searched in '$sJava16'\n");
410            # exit(1);
411            return "";
412        }
413    }
414    return $sJava16;
415}
416
417# ------------------------------------------------------------------------------
418sub getJavaExecutable()
419{
420    return getJava16();
421}
422
423# ------------------------------------------------------------------------------
424# this function is a helper for parameters
425# if quotes the whole string with 'STR' or "STR" and replace quotes in it's content for the right.
426sub singleQuote($)
427{
428    my $sStr = shift;
429    if ( $OSNAME eq "MSWin32")
430    {
431        # we are MSWin32 (quote \" stronger)
432        # $sStr =~ s/\'/\"/g;
433        $sStr =~ s/\'/\\\"/g;
434        return "\"" . $sStr . "\"";
435    }
436    else
437    {
438        if (index($sStr, "'") >= 0)
439        {
440            # replace all single quotes ("'") by "\""
441            $sStr =~ s/\'/\"/g;
442        }
443    }
444    return "'" . $sStr . "'";
445}
446
447sub quote($)
448{
449    my $sName = shift;
450    return "\"" . $sName . "\"";
451}
452
453sub quoteIfNeed($)
454{
455    my $sName = shift;
456    if (-1 != index($sName, " "))
457    {
458        return quote($sName);
459    }
460    return $sName;
461}
462
463
464# ------------------------------------------------------------------------------
465our $sToolsPath;
466sub setToolsPath($)
467{
468    my $sNewPath = shift;
469    $sToolsPath = $sNewPath;
470}
471
472sub ExecSQL($)
473{
474    my $sSQL = shift;
475
476    my $error;
477    my @aResult;
478    my $sSQLDirect;
479    if ($sToolsPath)
480    {
481        $sSQLDirect = $sToolsPath;
482        $sSQLDirect .= "/";
483    }
484    $sSQLDirect .= "sql_direct.php";
485
486    # select(undef, undef, undef, 0.060);
487    # log_print("ExecSQL: $sSQL\n");
488    # sleep (1);
489    ($error, @aResult) = callphp(getPHPExecutable(), $sSQLDirect, singleQuote($sSQL));
490    if ($error)
491    {
492        log_print ("ExecSQL: An Error occured.\n");
493        log_print ("PHP: " . getPHPExecutable() . "\n");
494        log_print ("SQL Statement: " . singleQuote($sSQL) . "\n");
495        # exit(1);
496    }
497    # select(undef, undef, undef, 0.125);
498    # sleep (1);
499    return @aResult;
500}
501
502# ------------------------------------------------------------------------------
503# helper to call external php with popen
504# sub callexe($$$)
505# {
506#     local *IN_FILE;
507#     my $exe = shift;
508#     my $program = shift;
509#     my $sParams = shift;
510#     my $line;
511#     my $error;
512#     my @result;
513#
514#     $exe = quoteIfNeed($exe);
515#     $program = quoteIfNeed($program);
516#
517#     # print "Will send: $exe $sParams\n";
518#     # log_print("CALLEXE: $exe $program $sParams\n");
519#     if (open(IN_FILE, "$exe $program $sParams |"))
520#     {
521#         while ($line = <IN_FILE>)
522#         {
523#             chomp($line);
524#             # $line .= " ";
525#             push(@result, $line);
526#             # print "callphp output: $line\n";
527#         }
528#         close(IN_FILE);
529#         $error = errorAdaption($?);
530#     }
531#     else
532#     {
533#         print "Can't popen '$exe' with parameter: '$sParams'\n";
534#         $error = 1;
535#     }
536#     return $error, @result;
537# }
538
5391;
540