code.fastix.org

Dateiansicht:

Datei:Projekte -> PHP:executeAlone -> executeCaller.php
md5:9c74d5a8bbaaa798e7425e11e478cd79
sha1:ac10f0c1022d9898d1f105ac53942394e8616615
Download-Link:Download
  1. <?php
  2. header( 'content-type: text/plain' );
  3.  
  4. # General config:
  5. $scriptFile             = 'executeAlone.php';
  6. $storageDir             = __DIR__ . '/executeStorage';
  7. $stripFirstPhpOpenLine  = true; # delete the first Line in $scriptFile('<?php ...'). (shold bee true)
  8.                                # It is saver to use this, if executeAlone.php ($scriptFile) is callable via httpd.
  9. # Config: a row of system-vars
  10. if ( isset( $_ENV ) ) {
  11.     $executeExportVars['_ENV'] = true;
  12. }
  13. $executeExportVars['_SERVER']  = true;
  14. $executeExportVars['_GET']     = true;
  15. $executeExportVars['_POST']    = true;
  16. $executeExportVars['_COOKIE']  = true;
  17. $executeExportVars['_SESSION'] = true;
  18.  
  19. # Config: examples for other vars:
  20. $executeExportVars['foo'] = true; $foo='FOO';
  21. $executeExportVars['bar'] = true; $bar='BAR';
  22. $_POST['mailto'] = 'fastix';
  23.  
  24. # The skript-interpreter (php or hhvm with path)
  25. $phpBinary = '/usr/bin/php';
  26. #$phpBinary = '/etc/alternatives/php';
  27. #$phpBinary = '/usr/bin/hhvm'; # alternativ for the "hip hop virtual machine"
  28.  
  29.  
  30. # Program:
  31.  
  32. if ( PHP_SESSION_ACTIVE != session_status() ) {
  33.     session_start();
  34. }
  35. $_SESSION['id']   = session_id();
  36. $_SESSION['test'] = 'Test';
  37.  
  38. $script = "<?php\n";
  39.  
  40. foreach ( $executeExportVars as $key => $value ) {
  41.     if ( isset( $$key ) && $value ) {
  42.         $var = $$key;
  43.         echo "$$key wird exportiert: PHP: var_export( $$key, true )\n";
  44.         $script .= '$' . "$key = " . var_export( $var, true ) . ";\n\n";
  45.     }
  46. }
  47.  
  48.  
  49. if ( $stripFirstPhpOpenLine ) {
  50.     $flag = ( ! $stripFirstPhpOpenLine );
  51.     $rows = file( $scriptFile );
  52.     foreach ( $rows as $row ) {
  53.         if ( ! $flag ) {
  54.             $flag = true;
  55.         } else {
  56.             $script .= $row;
  57.         }
  58.     }
  59. } else {
  60.     $script .= file_get_contents( $scriptFile );
  61. }
  62.  
  63.  
  64. $scriptName = tempnam( $storageDir, 'php_' );
  65. file_put_contents( $scriptName, $script );
  66.  
  67. $errorFile = tempnam( $storageDir, 'error_' );
  68. $command = "echo 'export MAIL=\"\" && $phpBinary $scriptName;' | batch 2> $errorFile; echo $?";
  69.  
  70. $return = 0;
  71. $return = `$command`;
  72.  
  73. if ( 0 == $return ) {
  74.     echo "Der Job '$phpBinary $scriptName' wurde erfolgreich in die Queue gestellt und wird ausgeführt sobald die Prozessorlast unter 80% liegt. (Meistens: sofort)\n";
  75.     unlink( $errorFile );
  76. } else {
  77.     $error = file_get_contents( $errorFile );
  78.     unlink( $errorFile );
  79.     trigger_error( $error, E_USER_ERROR );
  80. }