<commit_before><?php

const PUBLIC_DIR    = './public';
const DATA_DIR      = './data/update';

/**
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.
 */
chdir( dirname( __DIR__ ) );

/**
 * Send message(s)
 *
 * @param   int             $result
 * @param   string|array    $messages
 * @return  void
 */
function sendMessage( $result, $messages )
{
    $file = DATA_DIR . '/status.json';

    if ( ! is_array( $messages ) )
    {
        $messages = array( (string) $messages => array() );
    }

 /* foreach ( $messages as $message => $values )
    {
        echo vsprintf( $message, (array) $values ), PHP_EOL;
    } */

    if ( file_exists( $file ) )
    {
        $old = @ json_decode( file_get_contents( $file ), true );

        if ( ! empty( $old['messages'] ) )
        {
            $messages = array_replace( $old['messages'], $messages );
        }
    }

    @ file_put_contents( $file, json_encode( array(
        'messages'  => $messages,
        'result'    => $result,
    ) ) );

    if ( null !== $result )
    {
        if ( is_file( './composer.json.backup' ) )
        {
            if ( $result )
            {
                @ unlink( './composer.json' );
                @ rename( './composer.json.backup', './composer.json' );
            }
            else
            {
                @ unlink( './composer.json.backup' );
            }
        }

        if ( is_file( PUBLIC_DIR . '/maintenance.php' ) )
        {
            @ unlink( PUBLIC_DIR . '/maintenance.php' );
        }

        exit( (int) $result );
    }
}

/**
 * Run a process
 *
 * @param   string  $cmd
 * @param   array   $args
 * @param   array   $env
 * @return  void
 */
function runProcess( $cmd, array $args = array(), array $env = array() )
{
    $outputFilepath = DATA_DIR . '/output.txt';
    $descriptorspec = array(
     // 0 => array( 'pipe', 'r' ), // stdin
        1 => array( 'file', $outputFilepath, 'a' ), // stdout
        2 => array( 'file', $outputFilepath, 'a' ), // stderr
    );

    $pipes = array();
    $cmd   = escapeshellcmd( $cmd );

    foreach ( $args as $arg )
    {
        $cmd .= ' ' . escapeshellarg( $arg );
    }

    $outputFileExists   = is_file( $outputFilepath );
    $outputFile         = @ fopen( $outputFilepath, 'a' );

    if ( is_resource( $outputFile ) )
    {
        if ( $outputFileExists )
        {
            @ fwrite( $outputFile, PHP_EOL . PHP_EOL );
        }

        @ fwrite( $outputFile, '$ ' . $cmd . PHP_EOL );
        @ fclose( $outputFile );
    }

    @ chmod( $outputFile, 0777 );

    if ( empty( $env ) )
    {
        $env = null;
    }
    else
    {
        $env = array_merge( $_ENV, $env );
    }

    $proc = proc_open( $cmd, $descriptorspec, $pipes, getcwd(), $env );

    if ( ! is_resource( $proc ) )
    {
        sendMessage( 2, array(
            'admin.packages.process.cannotRun.%s' => array( $cmd ),
        ) );
    }

    foreach ( $pipes as $pipe )
    {
        @ fclose( $pipe );
    }

    $result = proc_close( $proc );

    if ( $result )
    {
        sendMessage( 3, array(
            'admin.packages.process.returned.%s.%d' => array( $cmd, $result ),
        ) );
    }
}

@ unlink( DATA_DIR . '/output.txt' );
@ unlink( DATA_DIR . '/status.json' );
@ file_put_contents( DATA_DIR . '/status.json', json_encode( array(
    'messages'  => array(),
    'result'    => null,
) ) );

@ chmod( DATA_DIR . '/status.json', 0777 );

if ( ! is_file( PUBLIC_DIR . '/maintenance.php' ) &&
     !  @ copy( PUBLIC_DIR . '/app/maintenance.php', PUBLIC_DIR . '/maintenance.php' ) )
{
    sendMessage( 1, 'admin.packages.maintenance.failed' );
}

if ( is_dir( './.git' ) )
{
    runProcess( 'git', array( 'pull', getcwd() ) );
    sendMessage( null, 'admin.packages.git.pulled' );
}

if ( false === ( $home = getenv( 'HOME' ) ) || ! is_dir( $home ) )
{
    @ mkdir( $home = realpath( './data/cache/composer' ), 0777 );
}

runProcess(
    'php',
    array(
        realpath( './composer.phar' ),
        'update',
        '--no-dev',
        '--no-interaction'
    ),
    array(
        'COMPOSER_HOME' => $home,
    )
);<commit_msg>fix for Process evironment variables<commit_after><?php

const PUBLIC_DIR    = './public';
const DATA_DIR      = './data/update';

/**
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.
 */
chdir( dirname( __DIR__ ) );

/**
 * Send message(s)
 *
 * @param   int             $result
 * @param   string|array    $messages
 * @return  void
 */
function sendMessage( $result, $messages )
{
    $file = DATA_DIR . '/status.json';

    if ( ! is_array( $messages ) )
    {
        $messages = array( (string) $messages => array() );
    }

 /* foreach ( $messages as $message => $values )
    {
        echo vsprintf( $message, (array) $values ), PHP_EOL;
    } */

    if ( file_exists( $file ) )
    {
        $old = @ json_decode( file_get_contents( $file ), true );

        if ( ! empty( $old['messages'] ) )
        {
            $messages = array_replace( $old['messages'], $messages );
        }
    }

    @ file_put_contents( $file, json_encode( array(
        'messages'  => $messages,
        'result'    => $result,
    ) ) );

    if ( null !== $result )
    {
        if ( is_file( './composer.json.backup' ) )
        {
            if ( $result )
            {
                @ unlink( './composer.json' );
                @ rename( './composer.json.backup', './composer.json' );
            }
            else
            {
                @ unlink( './composer.json.backup' );
            }
        }

        if ( is_file( PUBLIC_DIR . '/maintenance.php' ) )
        {
            @ unlink( PUBLIC_DIR . '/maintenance.php' );
        }

        exit( (int) $result );
    }
}

/**
 * Send to output
 *
 * @param   string  $text
 * @return  void
 */
function sendOutput( $text )
{
    $filePath   = DATA_DIR . '/output.txt';
    $fileExists = is_file( $filePath );
    $file       = @ fopen( $filePath, 'a' );

    if ( is_resource( $file ) )
    {
        if ( $fileExists )
        {
            @ fwrite( $file, PHP_EOL );
        }

        @ fwrite( $file, $text );
        @ fclose( $file );
        @ chmod( $filePath, 0777 );
    }
}

/**
 * Run a process
 *
 * @param   string  $cmd
 * @param   array   $args
 * @param   array   $env
 * @return  void
 */
function runProcess( $cmd, array $args = array(), array $env = array() )
{
    $outputFilepath = DATA_DIR . '/output.txt';
    $descriptorspec = array(
     // 0 => array( 'pipe', 'r' ), // stdin
        1 => array( 'file', $outputFilepath, 'a' ), // stdout
        2 => array( 'file', $outputFilepath, 'a' ), // stderr
    );

    $pipes = array();
    $cmd   = escapeshellcmd( $cmd );

    foreach ( $args as $arg )
    {
        $cmd .= ' ' . escapeshellarg( $arg );
    }

    sendOutput( '$ ' . $cmd . PHP_EOL );

    if ( empty( $env ) )
    {
        $env = null;
    }
    else
    {
        $env = array_merge( $_ENV, $env );
    }

    $proc = proc_open( $cmd, $descriptorspec, $pipes, getcwd(), $env );

    if ( ! is_resource( $proc ) )
    {
        sendMessage( 2, array(
            'admin.packages.process.cannotRun.%s' => array( $cmd ),
        ) );
    }

    foreach ( $pipes as $pipe )
    {
        @ fclose( $pipe );
    }

    $result = proc_close( $proc );

    if ( $result )
    {
        sendMessage( 3, array(
            'admin.packages.process.returned.%s.%d' => array( $cmd, $result ),
        ) );
    }
}

@ unlink( DATA_DIR . '/output.txt' );
@ unlink( DATA_DIR . '/status.json' );
@ file_put_contents( DATA_DIR . '/status.json', json_encode( array(
    'messages'  => array(),
    'result'    => null,
) ) );

@ chmod( DATA_DIR . '/status.json', 0777 );

if ( ! is_file( PUBLIC_DIR . '/maintenance.php' ) &&
     !  @ copy( PUBLIC_DIR . '/app/maintenance.php', PUBLIC_DIR . '/maintenance.php' ) )
{
    sendMessage( 1, 'admin.packages.maintenance.failed' );
}

if ( is_dir( './.git' ) )
{
    runProcess( 'git', array( 'pull', getcwd() ) );
    sendMessage( null, 'admin.packages.git.pulled' );
}

if ( false === ( $home = getenv( 'HOME' ) ) || ! is_dir( $home ) )
{
    @ mkdir( $home = realpath( './data/cache/composer' ), 0777 );
}

sendOutput( 'HOME=' . escapeshellarg( $home ) . PHP_EOL );

runProcess(
    'php',
    array(
        realpath( './composer.phar' ),
        'update',
        '--no-dev',
        '--no-interaction'
    ),
    array(
        'COMPOSER_HOME' => $home,
    )
);
{'fraction_non_alphanumeric': 0.17765546316898934, 'fraction_numerical': 0.004215950345473709, 'mean_word_length': 1.8409846972721224, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4175003', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2908, 'n_words': 630}