<?php
namespace WpInstaller;
class Parameter
{
public static function get()
{
return self::loadArguments(self::parameters());
}
private static function parameters() {
return [
'cpanel_user' => ['builder' => true],
'install_path' => ['required' => true],
'admin_user_email' => ['required' => true],
'admin_user' => ['builder' => true],
'admin_pass' => ['builder' => true],
'site_title' => ['default' => 'Welcome'],
// Really should be in 'options' but allowed in toplevel for now.
'site_tagline' => ['default' => ''],
'site_url' => ['builder' => true],
// really should be in 'plugin' => ['options'] but allowed in toplevel for now.
'mm_brand' => [],
'mm_host_id' => [],
'options' => ['supplement' => true],
'plugins' => ['default' => [] ],
// debug_options
'show_trace' => ['default' => 'false'],
];
}
public static function loadArguments($install_options)
{
$json_arguments;
global $argv;
if (@$argv[1] === 'json') {
$json_arguments = json_decode($argv[2], true);
} else if (@$argv[1] === '-') {
// else if we can parse the args from STDIN
$json_arguments = json_decode(fgets(STDIN), true);
}
// Setup to allow fetching of CLI arguments:
// http://php.net/manual/en/function.getopt.php
$cli_opt_validation = array();
foreach ($install_options as $option => $def) {
$flag = (key_exists('required', $def) && $def['required']) ? ':' : '::';
array_push($cli_opt_validation, $option . $flag);
}
$cli_opts = getopt("", $cli_opt_validation);
// Fetch arguments from wherever they may be present
$install_arguments = [];
foreach ($install_options as $option => $def) {
$required = key_exists('required', $def) && $def['required'];
// Get args from ENV
if (key_exists($option, $_ENV)) {
$install_arguments[$option] = $_ENV[$option];
// Get args from POST
} elseif (key_exists($option, $_POST)) {
$install_arguments[$option] = $_POST[$option];
// Get args from CLI
} elseif (isset($cli_opts) && key_exists($option, $cli_opts)) {
$install_arguments[$option] = $cli_opts[$option];
// Get args from CLI in JSON format
} elseif (isset($json_arguments) && key_exists($option, $json_arguments)) {
$install_arguments[$option] = $json_arguments[$option];
// Make sure we have required args, and apply any defaults.
} else {
if ($required) {
throw new \Exception("$option is required.");
}
// If there's a default value, use it
if (key_exists('default', $def)) {
$install_arguments[$option] = $def['default'];
$install_arguments[$option] = is_callable($def['default'])
? $def['default']($install_arguments)
: $def['default'];
}
// Otherwise, if there's a builder, use it to derive a default
elseif (key_exists('builder', $def) && $def['builder'] === true) {
$builder_method_name = "build_$option";
$install_arguments[$option] = self::$builder_method_name($install_arguments);
}
// Otherwise, it's empty
else {
$install_arguments[$option] = '';
}
}
# Supplement functions merge defaults with given values in complex structures.
if (key_exists('supplement', $def) && $def['supplement'] === true) {
$supplement_method_name = "supplement_$option";
$install_arguments[$option] = self::$supplement_method_name($install_arguments);
}
// Apply any specific validation rules that may exist
self::validateOption($option, $install_arguments[$option], $install_arguments);
}
return $install_arguments;
}
public static function validateOption($option, $value, $options)
{
$validationClass = '\WpInstaller\Parameter\Validator';
$validationFunction = "validate_{$option}";
if (is_callable([$validationClass, $validationFunction])) {
$toCall = [$validationClass, $validationFunction];
return $toCall($value, $options);
}
}
private static function build_cpanel_user($processed_args) {
$e_username = posix_getpwuid(posix_geteuid())['name'];
if ($e_username == 'root') {
throw new \Exception('cpanel_user is required when running as root');
}
return $e_username;
}
private static function build_admin_user($processed_args) {
// if the admin_user is given, then use that
if (key_exists('admin_user', $processed_args)) {
return $processed_args['admin_user'];
} else if (!key_exists('admin_user_email', $processed_args)) {
throw new \Exception('admin_user was not given and admin_user_email arg is not yet present');
} else {
// else build it from the admin_user_email arg
$email = $processed_args['admin_user_email'];
return substr($email, 0, strpos($email, '@'));
}
}
private static function build_admin_pass($processed_args) {
// if the admin_pass is given to us, then use that
if (key_exists('admin_pass', $processed_args)) {
return $processed_args['admin_pass'];
} else {
// otherwise build it for them
return \WpInstaller\StringGenerator::gen_password();
}
}
private static function build_site_url($processed_args) {
if (!key_exists('cpanel_user', $processed_args)) {
throw new \Exception('cpanel_user arg is not yet present');
}
if (!key_exists('install_path', $processed_args)) {
throw new \Exception('install_path arg is not yet present');
}
# Determine the path relative to public_html
$captures;
preg_match('/public_html(\/.*)/', $processed_args['install_path'], $captures);
$web_path = count($captures) > 1 ? $captures[1] : '/';
$hostname = explode('.',gethostname())[0];
return "http://{$hostname}.temp.domains/~{$processed_args['cpanel_user']}{$web_path}";
}
private static function supplement_options ($processed_args) {
// Args we allow being passed at the toplevel
$site_url = $processed_args['site_url'];
$tagline = $processed_args['site_tagline'];
unset($processed_args['site_url']);
unset($processed_args['site_tagline']);
$core_options = [
'siteurl' => $site_url,
'home' => $site_url,
'blogdescription' => $tagline,
];
if (!is_array($processed_args['options'])) {
$processed_args['options'] = [];
}
$options = $processed_args['options'];
// Use the given value if it exists. Default otherwise
foreach ($core_options as $key => $val) {
$options[$key] = array_key_exists($key, $options)
? $options[$key]
: $val;
}
return $options;
}
}