public function __construct($web_initiated, $params, $do_not_init = false) { if (!$do_not_init) { $this->org_start_time = time(); $this->start_time = $this->org_start_time; if ($this->supports_feature('login')) { $this->optional_settings = array_merge($this->optional_settings, array('login_user', 'login_password')); } $this->web_initiated = $web_initiated; if ($this->web_initiated) { if (!isset($params['token'])) { $this->exit_err('Fatal error: $web_initiated was true but $params did not contain a "token" key.', __FILE__, __METHOD__, __LINE__); } $this->token = $params['token']; $this->settings_filename = make_settings_filename($this->token); $this->output_dirname = make_output_dirname($this->token); $this->errs_filename = make_errs_filename($this->token); } else { if (!isset($params['settings_filename'])) { $this->exit_err('Fatal error: $web_initiated was false but $params did not contain a "settings_filename" key.', __FILE__, __METHOD__, __LINE__); } $this->settings_filename = $params['settings_filename']; if (!isset($params['output_dirname'])) { $this->exit_err('Fatal error: $web_initiated was false but $params did not contain a "output_dirname" key.', __FILE__, __METHOD__, __LINE__); } $this->output_dirname = $params['output_dirname']; $len = strlen($this->output_dirname); // Make sure user-supplied (commandline interface) output directories end in a slash, // because when we generate them (web interface) we make sure they end in a slash, // and this way we can rely on them ending in a slash in all contexts. Note that here // we assume an empty output directory to refer to the root directory. if ($len <= 0 || $this->output_dirname[$len - 1] != '/') { $this->output_dirname .= '/'; } $this->quiet = $params['quiet']; } if (FUPS_CHAIN_DURATION == -1) { $max_execution_time = ini_get('max_execution_time'); if (is_numeric($max_execution_time) && $max_execution_time > 0) { $this->FUPS_CHAIN_DURATION = $max_execution_time * 3 / 4; } else { $this->FUPS_CHAIN_DURATION = FUPS_FALLBACK_FUPS_CHAIN_DURATION; } } else { $this->FUPS_CHAIN_DURATION = FUPS_CHAIN_DURATION; } $this->write_status('Reading settings.'); $default_settings = $this->get_default_settings(); $raw_settings = $this->read_settings_raw_s($this->settings_filename); foreach ($raw_settings as $setting => $value) { if (in_array($setting, $this->required_settings) || in_array($setting, $this->optional_settings)) { $this->settings[$setting] = $value; } } $missing = array_diff($this->required_settings, array_keys($this->settings)); if ($missing) { $this->exit_err("The following settings were missing: " . implode(', ', $missing) . '.', __FILE__, __METHOD__, __LINE__); } foreach ($default_settings as $setting => $default) { if (empty($this->settings[$setting])) { $this->settings[$setting] = $default; } } date_default_timezone_set($this->settings['php_timezone']); // This timezone only matters when converting the earliest time setting. if (!empty($this->settings['start_from_date'])) { $this->settings['earliest'] = $this->strtotime_intl($this->settings['start_from_date']); if ($this->settings['earliest'] === false) { $this->write_err("Error: failed to convert 'start_from_date' ({$this->settings['start_from_date']}) into a UNIX timestamp."); } } $this->dbg = in_array($this->settings['debug'], array('true', '1')) ? true : false; if ($this->dbg) { $this->write_err('SETTINGS:'); $this->write_err(var_export($this->settings, true)); } $this->write_status('Finished reading settings.'); $this->validate_settings(); // Create output directory, appending .1 or .2 etc if necessary. // Do this last so we don't create it if settings validation fails. $max_attempts = 10000; $appendix = 0; // Strip off the trailing slash $dirname = substr($this->output_dirname, 0, strlen($this->output_dirname) - 1); while (file_exists($dirname) && $appendix <= $max_attempts) { $dirname = $this->output_dirname . '.' . ++$appendix; } if ($appendix > $max_attempts) { $this->exit_err('Output directory "' . $this->output_dirname . '" already exists. Exceeded maximum attempts (' . $max_attempts . ') in finding an alternative that does not exist. Tried "' . $this->output_dirname . '.1", "' . $this->output_dirname . '.2", "' . $this->output_dirname . '.3", etc.', __FILE__, __METHOD__, __LINE__); } if (!mkdir($dirname, 0775, true)) { $this->exit_err('Failed to create output directory "' . $dirname . '".', __FILE__, __METHOD__, __LINE__); } $this->output_dirname = $dirname . '/'; if ($this->web_initiated) { $this->output_dirname_web = make_output_dirname($this->token, true, $appendix == 0 ? '' : $appendix); } } }
} else { $token = $argv[$i + 1]; } $i += 2; break; case '-c': $chained = true; $i++; break; default: FUPSBase::exit_err_s('Fatal error: unknown commandline argument specified: "' . $argv[$i] . '".', __FILE__, __METHOD__, __LINE__); break; } } if ($web_initiated) { $settings_filename = make_settings_filename($token); } else { if ($web_initiated === false) { if (!$settings_filename || !$output_dirname) { FUPSBase::exit_err_s('Fatal error: no ' . (!$settings_filename ? 'settings' : 'output') . ' filename specified in commandline arguments.', __FILE__, __METHOD__, __LINE__); } } else { FUPSBase::exit_err_s('Fatal error: $web_initiated is uninitialised after parsing commandline arguments (this error should never occur, and indicates a bug).', __FILE__, __METHOD__, __LINE__); } } } $forum_type = FUPSBase::read_forum_type_from_settings_file_s($settings_filename); $valid_forum_types = FUPSBase::get_valid_forum_types(); if (!isset($valid_forum_types[$forum_type])) { FUPSBase::exit_err_s('Fatal error: missing or invalid forum_type in settings file "' . $settings_filename . '": "' . $forum_type . '".', __FILE__, __METHOD__, __LINE__); }
} else { $token = $_GET['token']; $op_info_filename = make_output_info_filename($token); if (is_file($op_info_filename)) { $output_info = json_decode(file_get_contents($op_info_filename), true); if (is_array($output_info)) { $output_dir = null; foreach ($output_info as $opv) { try_delete_file($opv['filepath'], '"' . $opv['filepath'] . '"', false, $err, $num_files_deleted, false); $output_dir = dirname($opv['filepath']); } @rmdir($output_dir); } } if (validate_token($token, $err)) { try_delete_file(make_settings_filename($token), 'settings', true, $err, $num_files_deleted); try_delete_file(make_status_filename($token), 'status', false, $err, $num_files_deleted); try_delete_file(make_errs_filename($token), 'error', false, $err, $num_files_deleted); try_delete_file(make_errs_admin_filename($token), 'errors (admin)', false, $err, $num_files_deleted, false); try_delete_file(make_output_info_filename($token), 'output info', false, $err, $num_files_deleted, false); try_delete_file(make_serialize_filename($token), 'serialisation', true, $err, $num_files_deleted); try_delete_file(make_cookie_filename($token), 'cookie', true, $err, $num_files_deleted, false); try_delete_file(make_cancellation_filename($token), 'cancellation', true, $err, $num_files_deleted, false); } } function try_delete_file($filename, $name, $sensitive, &$err, &$num_files_deleted, $add_err_if_file_not_present = true) { global $fups_url_homepage; if (!is_file($filename)) { if ($add_err_if_file_not_present) { $err .= $err ? ' Another' : 'An';