#!/usr/local/cpanel/3rdparty/bin/php
<?php
require_once __DIR__ . '/vendor/autoload.php';
// composes Common::ActionLogger w/ Common::Process
function run_detached_action($actions, $action_name, $cmd, $data_for_child = NULL) {
$actions->action_start($action_name);
$response = \Common\Process::run_process($cmd, $data_for_child);
// log STDERR to the action
if (!empty($response['warnings'])) {
$actions->add_note($response['warnings'], 'warnings');
}
// get info to log to the action (could be a JSON action trace or a raw response)
$details = !empty($response['trace']) ? $response['trace'] : $response['output'];
$actions->action_end($response['error'] ? false : true, $details);
return $response;
}
// convenience function
function exit_with_trace($actions, $file_manager = NULL) {
// roll back changes
if ( !empty($file_manager) ) {
$file_manager->rollback();
}
print(json_encode([
'success' => false,
'trace' => $actions->actions()
]));
exit(1);
}
// start the action logger for this parent process
$actions = new \Common\ActionLogger();
$file_manager = NULL;
try {
// fetch the installer arguments
$actions->action_start('arg_parse');
$args = (\WpInstaller\Parameter::get());
$username = $args['cpanel_user'];
$path = $args['install_path'];
// change our process to run as the target user
$actions->action_start('user_delegate');
\Common\Process::su($username);
// create install path
$actions->action_start('install_path_create');
$filemanager = new \WpInstaller\FileManager();
$filemanager->mkdir($path);
// change to new directory
$actions->action_start('install_path_chdir');
if (chdir($path) !== true) {
throw new \Exception("Failed to chdir to $path");
}
// encode so we can pass these as parameters to exec()
$json_args = json_encode($args);
// run install as separate process
$install_command = '/usr/local/cpanel/3rdparty/bin/php --define display_errors=stderr -f /usr/local/cpanel/3rdparty/wpinstaller/install.php -';
$install_response = run_detached_action($actions, 'install_process_run', $install_command, $json_args);
// check for error during install process
if ($install_response['error']) {
exit_with_trace($actions, $file_manager);
}
// copy over the decoded JSON
$results = $install_response['json_output'];
// run updates as a second process
$update_command = '/usr/local/cpanel/3rdparty/bin/php --define display_errors=stderr -f /usr/local/cpanel/3rdparty/wpinstaller/update.php';
$update_response = run_detached_action($actions, 'update_process_run', $update_command);
// check for error during update process
$results['auto_updated'] = !$update_response['error'];
// show trace if the caller wants it or if something happened during updates
if ($args['show_trace'] === 'true' || $update_response['error']) {
$results['trace'] = $actions->actions();
}
// return the results as a JSON response
print (json_encode($results));
// exit successfully
exit();
} catch(\Exception $e) {
// log the exception in the actions trace
$actions->action_end(false, $e);
exit_with_trace($actions, $file_manager);
}
?>