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