public static function prompt_bool_input($question, $default = true, $question_id = 'UNKNOWN')
 {
     // Prompt user for yes/no question
     if (pts_c::$test_flags & pts_c::batch_mode) {
         switch ($question_id) {
             default:
                 $auto_answer = 'true';
                 break;
         }
         $answer = pts_strings::string_bool($auto_answer);
     } else {
         $question .= ' (' . ($default == true ? 'Y/n' : 'y/N') . '): ';
         do {
             pts_client::$display->generic_prompt($question);
             $input = strtolower(pts_user_io::read_user_input());
         } while ($input != 'y' && $input != 'n' && $input != '');
         switch ($input) {
             case 'y':
                 $answer = true;
                 break;
             case 'n':
                 $answer = false;
                 break;
             default:
                 $answer = $default;
                 break;
         }
     }
     return $answer;
 }
 protected static function install_test_process(&$test_install_request, $no_prompts)
 {
     // Install a test
     $identifier = $test_install_request->test_profile->get_identifier();
     $test_install_directory = $test_install_request->test_profile->get_install_dir();
     pts_file_io::mkdir(dirname($test_install_directory));
     pts_file_io::mkdir($test_install_directory);
     $installed = false;
     if (ceil(disk_free_space($test_install_directory) / 1048576) < $test_install_request->test_profile->get_download_size() + 128) {
         self::test_install_error(null, $test_install_request, 'There is not enough space at ' . $test_install_directory . ' for the test files.');
     } else {
         if (ceil(disk_free_space($test_install_directory) / 1048576) < $test_install_request->test_profile->get_environment_size(false) + 128) {
             self::test_install_error(null, $test_install_request, 'There is not enough space at ' . $test_install_directory . ' for this test.');
         } else {
             pts_test_installer::setup_test_install_directory($test_install_request, true);
             // Download test files
             $download_test_files = pts_test_installer::download_test_files($test_install_request, false, $no_prompts);
             if ($download_test_files == false) {
                 self::test_install_error(null, $test_install_request, 'Downloading of needed test files failed.');
                 return false;
             }
             if ($test_install_request->test_profile->get_file_installer() != false) {
                 self::create_compiler_mask($test_install_request);
                 pts_module_manager::module_process('__pre_test_install', $identifier);
                 pts_client::$display->test_install_begin($test_install_request);
                 $pre_install_message = $test_install_request->test_profile->get_pre_install_message();
                 $post_install_message = $test_install_request->test_profile->get_post_install_message();
                 $install_agreement = $test_install_request->test_profile->get_installation_agreement_message();
                 if (!empty($install_agreement)) {
                     if (pts_strings::is_url($install_agreement)) {
                         $install_agreement = pts_network::http_get_contents($install_agreement);
                         if (empty($install_agreement)) {
                             self::test_install_error(null, $test_install_request, 'The user agreement could not be found. Test installation aborted.');
                             return false;
                         }
                     }
                     echo $install_agreement . PHP_EOL;
                     if (!$no_prompts) {
                         $user_agrees = pts_user_io::prompt_bool_input('Do you agree to these terms', false, 'INSTALL_AGREEMENT');
                         if (!$user_agrees) {
                             self::test_install_error(null, $test_install_request, 'User agreement failed; this test will not be installed.');
                             return false;
                         }
                     }
                 }
                 pts_client::$display->display_interrupt_message($pre_install_message);
                 $install_time_length_start = microtime(true);
                 $install_log = pts_tests::call_test_script($test_install_request->test_profile, 'install', null, $test_install_directory, $test_install_request->special_environment_vars, false);
                 $test_install_request->install_time_duration = ceil(microtime(true) - $install_time_length_start);
                 pts_client::$display->display_interrupt_message($post_install_message);
                 if (!empty($install_log)) {
                     file_put_contents($test_install_directory . 'install.log', $install_log);
                     pts_file_io::unlink($test_install_directory . 'install-failed.log');
                     pts_client::$display->test_install_output($install_log);
                 }
                 if (is_file($test_install_directory . 'install-exit-status')) {
                     // If the installer writes its exit status to ~/install-exit-status, if it's non-zero the install failed
                     $install_exit_status = pts_file_io::file_get_contents($test_install_directory . 'install-exit-status');
                     unlink($test_install_directory . 'install-exit-status');
                     if ($install_exit_status != 0 && phodevi::is_windows() == false) {
                         $install_error = null;
                         // TODO: perhaps better way to handle this than to remove pts-install.xml
                         pts_file_io::unlink($test_install_directory . 'pts-install.xml');
                         if (is_file($test_install_directory . 'install.log')) {
                             $install_log = pts_file_io::file_get_contents($test_install_directory . 'install.log');
                             $install_error = pts_tests::scan_for_error($install_log, $test_install_directory);
                             copy($test_install_directory . 'install.log', $test_install_directory . 'install-failed.log');
                         }
                         //pts_test_installer::setup_test_install_directory($test_install_request, true); // Remove installed files from the bunked installation
                         self::test_install_error(null, $test_install_request, 'The installer exited with a non-zero exit status.');
                         if ($install_error != null) {
                             $test_install_request->install_error = pts_tests::pretty_error_string($install_error);
                             if ($test_install_request->install_error != null) {
                                 self::test_install_error(null, $test_install_request, 'ERROR: ' . $test_install_request->install_error);
                             }
                         }
                         pts_client::$display->test_install_error('LOG: ' . str_replace(pts_core::user_home_directory(), '~/', $test_install_directory) . 'install-failed.log' . PHP_EOL);
                         if (pts_client::do_anonymous_usage_reporting()) {
                             // If anonymous usage reporting enabled, report test install failure to OpenBenchmarking.org
                             pts_openbenchmarking_client::upload_usage_data('test_install_failure', array($test_install_request, $install_error));
                         }
                         return false;
                     }
                 }
                 pts_module_manager::module_process('__post_test_install', $identifier);
                 $installed = true;
                 if (pts_config::read_bool_config('PhoronixTestSuite/Options/Installation/RemoveDownloadFiles', 'FALSE')) {
                     // Remove original downloaded files
                     foreach ($test_install_request->get_download_objects() as $download_object) {
                         pts_file_io::unlink($test_install_directory . $download_object->get_filename());
                     }
                 }
             } else {
                 pts_client::$display->test_install_error('No installation script found.');
                 $installed = true;
             }
             // Additional validation checks?
             $custom_validated_output = pts_tests::call_test_script($test_install_request->test_profile, 'validate-install', PHP_EOL . 'Validating Installation...' . PHP_EOL, $test_install_directory, null, false);
             if (!empty($custom_validated_output) && !pts_strings::string_bool($custom_validated_output)) {
                 $installed = false;
             }
         }
     }
     echo PHP_EOL;
     return $installed;
 }
 public static function read_bool_config($xml_pointer, $predefined_value = false, &$nye_xml = null)
 {
     $value = self::read_user_config($xml_pointer, $predefined_value, $nye_xml);
     return pts_strings::string_bool($value);
 }
 public function allow_results_sharing()
 {
     return pts_strings::string_bool($this->xg('TestProfile/AllowResultsSharing', 'TRUE'));
 }
 protected static function process_user_config_external_hook_process($process)
 {
     // Check to not run the same process
     $last_call_file = pts_module::save_dir() . self::$context . '.last-call';
     if (is_file($last_call_file)) {
         $check = pts_file_io::file_get_contents($last_call_file);
         if ($process == $check) {
             unlink($last_call_file);
             return false;
         }
     }
     $process != 'post_run' && file_put_contents($last_call_file, $process);
     if (self::$ini['set_context'][$process]) {
         $command = self::find_file(self::$ini['set_context'][$process]) ? self::find_file(self::$ini['set_context'][$process]) : self::$ini['set_context'][$process];
         $descriptor_spec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
         $env_vars = null;
         pts_client::$display->test_run_instance_error('Running ' . $process . ' set-context script.');
         if (is_executable($command)) {
             // Pass the context as the first argument to the string
             $command .= ' ' . self::$context;
         } else {
             // Else find $MATISK_CONTEXT in the command string
             $command = str_replace('$MATISK_CONTEXT', self::$context, $command);
         }
         $proc = proc_open($command, $descriptor_spec, $pipes, null, $env_vars);
         echo $std_output = stream_get_contents($pipes[1]);
         $return_value = proc_close($proc);
         if (pts_strings::string_bool(self::$ini['set_context']['log_context_outputs'])) {
             file_put_contents(pts_module::save_dir() . $ini['workload']['save_name'] . '/' . self::$context . '-' . $process . '.txt', $std_output);
         }
         switch ($return_value) {
             case 8:
                 exit(0);
             case 9:
                 self::$skip_test_set = true;
                 break;
         }
     }
 }
 public static function run_connection($args)
 {
     if (pts_client::create_lock(PTS_USER_PATH . 'phoromatic_lock') == false) {
         trigger_error('Phoromatic is already running.', E_USER_ERROR);
         return false;
     }
     define('PHOROMATIC_PROCESS', true);
     if (pts_client::$pts_logger == false) {
         pts_client::$pts_logger = new pts_logger();
     }
     pts_client::$pts_logger->log(pts_title(true) . ' [' . PTS_CORE_VERSION . '] starting Phoromatic client');
     if (phodevi::system_uptime() < 60) {
         echo 'PHOROMATIC: Sleeping for 60 seconds as system freshly started.' . PHP_EOL;
         pts_client::$pts_logger->log('Sleeping for 60 seconds as system freshly started');
         sleep(60);
     }
     $server_setup = self::setup_server_addressing($args);
     //$http_comm = new phoromatic_client_comm_http();
     if (!$server_setup) {
         if (PTS_IS_DAEMONIZED_SERVER_PROCESS) {
             if (pts_client::executable_in_path('reboot')) {
                 shell_exec('reboot');
                 sleep(5);
             }
         }
         return false;
     }
     $times_failed = 0;
     $has_success = false;
     $do_exit = false;
     $just_started = true;
     self::setup_system_environment();
     pts_client::$pts_logger->log('SYSTEM HARDWARE: ' . phodevi::system_hardware(true));
     pts_client::$pts_logger->log('SYSTEM SOFTWARE: ' . phodevi::system_software(true));
     while ($do_exit == false) {
         $server_response = phoromatic::upload_to_remote_server(array('r' => 'start'));
         if ($server_response == false) {
             $times_failed++;
             pts_client::$pts_logger->log('Server response failed');
             if ($times_failed >= 2) {
                 trigger_error('Communication with server failed.', E_USER_ERROR);
                 if (PTS_IS_DAEMONIZED_SERVER_PROCESS == false && $times_failed > 5) {
                     return false;
                 } else {
                     if (PTS_IS_DAEMONIZED_SERVER_PROCESS && $times_failed > 10) {
                         if (pts_client::executable_in_path('reboot')) {
                             shell_exec('reboot');
                             sleep(5);
                         }
                     }
                 }
             }
         } else {
             if (substr($server_response, 0, 1) == '[') {
                 // Likely a notice/warning from server
                 echo PHP_EOL . substr($server_response, 0, strpos($server_response, PHP_EOL)) . PHP_EOL;
             } else {
                 if (substr($server_response, 0, 1) == '{') {
                     $times_failed = 0;
                     $json = json_decode($server_response, true);
                     if ($has_success == false) {
                         $has_success = true;
                         pts_module::save_file('last-phoromatic-server', self::$server_address . ':' . self::$server_http_port . '/' . self::$account_id);
                     }
                     if ($json != null) {
                         if (isset($json['phoromatic']['error']) && !empty($json['phoromatic']['error'])) {
                             trigger_error($json['phoromatic']['error'], E_USER_ERROR);
                         }
                         if (isset($json['phoromatic']['response']) && !empty($json['phoromatic']['response'])) {
                             echo PHP_EOL . $json['phoromatic']['response'] . PHP_EOL;
                         }
                     }
                     if ($just_started) {
                         if (PTS_IS_DAEMONIZED_SERVER_PROCESS) {
                             $pid = pcntl_fork();
                             if ($pid == 0) {
                                 // Start the tick thread
                                 self::tick_thread();
                             }
                         }
                         $just_started = false;
                     }
                     if (isset($json['phoromatic']['pre_set_sys_env_vars']) && !empty($json['phoromatic']['pre_set_sys_env_vars'])) {
                         // pre_set_sys_env_vars was added during PTS 5.8 development
                         // Sets environment variables on client as specified via the Phoromatic Server's systems page
                         foreach (explode(';', $json['phoromatic']['pre_set_sys_env_vars']) as $i => $v_string) {
                             $var = explode('=', $v_string);
                             if (count($var) == 2) {
                                 putenv($var[0] . '=' . $var[1]);
                             }
                         }
                     }
                     switch (isset($json['phoromatic']['task']) ? $json['phoromatic']['task'] : null) {
                         case 'install':
                             phoromatic::update_system_status('Installing Tests');
                             pts_suite_nye_XmlReader::set_temporary_suite('pre-seed', $json['phoromatic']['test_suite']);
                             pts_test_installer::standard_install('pre-seed');
                             break;
                         case 'benchmark':
                             // Make sure all latest tests are available
                             pts_openbenchmarking::refresh_repository_lists(null, true);
                             $benchmark_timer = time();
                             self::$is_running_as_phoromatic_node = true;
                             $test_flags = pts_c::auto_mode | pts_c::batch_mode;
                             $suite_identifier = sha1(time() . rand(2, 1000));
                             pts_suite_nye_XmlReader::set_temporary_suite($suite_identifier, $json['phoromatic']['test_suite']);
                             self::$p_save_identifier = $json['phoromatic']['trigger_id'];
                             $phoromatic_results_identifier = self::$p_save_identifier;
                             $phoromatic_save_identifier = $json['phoromatic']['save_identifier'];
                             self::$p_schedule_id = isset($json['phoromatic']['schedule_id']) ? $json['phoromatic']['schedule_id'] : false;
                             self::$p_trigger_id = self::$p_save_identifier;
                             $benchmark_ticket_id = isset($json['phoromatic']['benchmark_ticket_id']) ? $json['phoromatic']['benchmark_ticket_id'] : null;
                             phoromatic::update_system_status('Running Benchmarks For: ' . $phoromatic_save_identifier);
                             if (pts_strings::string_bool($json['phoromatic']['settings']['RunInstallCommand'])) {
                                 if (isset($json['phoromatic']['pre_install_set_context'])) {
                                     phoromatic::set_user_context($json['phoromatic']['pre_install_set_context'], self::$p_trigger_id, self::$p_schedule_id, 'PRE_INSTALL');
                                 }
                                 if (pts_strings::string_bool($json['phoromatic']['settings']['ForceInstallTests'])) {
                                     $test_flags |= pts_c::force_install;
                                 }
                                 pts_client::set_test_flags($test_flags);
                                 pts_test_installer::standard_install($suite_identifier);
                                 if (isset($json['phoromatic']['post_install_set_context'])) {
                                     phoromatic::set_user_context($json['phoromatic']['post_install_set_context'], self::$p_trigger_id, self::$p_schedule_id, 'POST_INSTALL');
                                 }
                             }
                             $env_vars = isset($json['phoromatic']['environment_variables']) ? pts_strings::parse_value_string_vars($json['phoromatic']['environment_variables']) : array();
                             // Do the actual running
                             phodevi::clear_cache();
                             if (pts_test_run_manager::initial_checks($suite_identifier, 0, 'SHORT')) {
                                 self::$test_run_manager = new pts_test_run_manager($test_flags);
                                 pts_test_run_manager::set_batch_mode(array('UploadResults' => isset($json['phoromatic']['settings']['UploadResultsToOpenBenchmarking']) && pts_strings::string_bool($json['phoromatic']['settings']['UploadResultsToOpenBenchmarking']), 'SaveResults' => true, 'RunAllTestCombinations' => false, 'OpenBrowser' => false));
                                 // Load the tests to run
                                 if (self::$test_run_manager->load_tests_to_run($suite_identifier)) {
                                     phoromatic::update_system_status('Tests In Run Queue: ' . implode(', ', self::$test_run_manager->get_tests_to_run_identifiers()));
                                     if (isset($json['phoromatic']['pre_run_set_context'])) {
                                         phoromatic::set_user_context($json['phoromatic']['pre_run_set_context'], self::$p_trigger_id, self::$p_schedule_id, 'PRE_RUN');
                                     }
                                     if (isset($json['phoromatic']['settings']['UploadResultsToOpenBenchmarking']) && pts_strings::string_bool($json['phoromatic']['settings']['UploadResultsToOpenBenchmarking'])) {
                                         self::$test_run_manager->auto_upload_to_openbenchmarking();
                                         pts_openbenchmarking_client::override_client_setting('UploadSystemLogsByDefault', pts_strings::string_bool($json['phoromatic']['settings']['UploadSystemLogs']));
                                     }
                                     // Save results?
                                     // Run the actual tests
                                     if (isset($env_vars['PTS_CONCURRENT_TEST_RUNS']) && $env_vars['PTS_CONCURRENT_TEST_RUNS'] > 1) {
                                         $total_loop_time = isset($env_vars['TOTAL_LOOP_TIME']) ? $env_vars['TOTAL_LOOP_TIME'] : false;
                                         pts_client::$pts_logger->log('STRESS / MULTI-TEST EXECUTION STARTED @ ' . date('Y-m-d H:i:s'));
                                         pts_client::$pts_logger->log('CONCURRENT RUNS = ' . $env_vars['PTS_CONCURRENT_TEST_RUNS'] . ' TOTAL LOOP TIME = ' . $total_loop_time);
                                         $r = self::$test_run_manager->multi_test_stress_run_execute($env_vars['PTS_CONCURRENT_TEST_RUNS'], $total_loop_time);
                                         if ($r == false) {
                                             return;
                                         }
                                         pts_client::$pts_logger->log('STRESS / MULTI-TEST EXECUTION ENDED @ ' . date('Y-m-d H:i:s'));
                                     } else {
                                         self::$test_run_manager->auto_save_results($phoromatic_save_identifier, $phoromatic_results_identifier, isset($json['phoromatic']['test_description']) ? $json['phoromatic']['test_description'] : 'A Phoromatic run.');
                                         self::$test_run_manager->pre_execution_process();
                                         self::$test_run_manager->call_test_runs();
                                     }
                                     phoromatic::update_system_status('Benchmarks Completed For: ' . $phoromatic_save_identifier);
                                     self::$test_run_manager->post_execution_process();
                                     $elapsed_benchmark_time = time() - $benchmark_timer;
                                     // Handle uploading data to server
                                     $result_file = new pts_result_file(self::$test_run_manager->get_file_name());
                                     $upload_system_logs = pts_strings::string_bool($json['phoromatic']['settings']['UploadSystemLogs']);
                                     $server_response = self::upload_test_result($result_file, $upload_system_logs, isset($json['phoromatic']['schedule_id']) ? $json['phoromatic']['schedule_id'] : null, $phoromatic_save_identifier, $json['phoromatic']['trigger_id'], $elapsed_benchmark_time, $benchmark_ticket_id);
                                     //pts_client::$pts_logger->log('DEBUG RESPONSE MESSAGE: ' . $server_response);
                                     if (!pts_strings::string_bool($json['phoromatic']['settings']['ArchiveResultsLocally'])) {
                                         pts_client::remove_saved_result_file(self::$test_run_manager->get_file_name());
                                     }
                                 }
                                 if (isset($json['phoromatic']['post_install_set_context'])) {
                                     phoromatic::set_user_context($json['phoromatic']['post_install_set_context'], self::$p_trigger_id, self::$p_schedule_id, 'POST_RUN');
                                 }
                             }
                             self::$p_schedule_id = null;
                             self::$is_running_as_phoromatic_node = false;
                             break;
                         case 'reboot':
                             echo PHP_EOL . 'Phoromatic received a remote command to reboot.' . PHP_EOL;
                             phoromatic::update_system_status('Attempting System Reboot');
                             if (pts_client::executable_in_path('reboot')) {
                                 shell_exec('reboot');
                                 sleep(5);
                             }
                             break;
                         case 'shutdown-if-supports-wake':
                             $supports_wol = false;
                             foreach (pts_network::get_network_wol() as $net_device) {
                                 if (strpos($net_device, 'g') !== false) {
                                     $supports_wol = true;
                                     break;
                                 }
                             }
                             if (!$supports_wol) {
                                 break;
                             }
                         case 'shutdown':
                             if (isset($json['phoromatic']['client_update_script']) && !empty($json['phoromatic']['client_update_script'])) {
                                 self::run_client_update_script($json['phoromatic']['client_update_script']);
                                 sleep(10);
                             }
                             echo PHP_EOL . 'Phoromatic received a remote command to shutdown.' . PHP_EOL;
                             phoromatic::update_system_status('Attempting System Shutdown');
                             if (pts_client::executable_in_path('poweroff')) {
                                 shell_exec('poweroff');
                                 sleep(5);
                             }
                             break;
                         case 'maintenance':
                             echo PHP_EOL . 'Idling, system maintenance mode set by Phoromatic Server.' . PHP_EOL;
                             phoromatic::update_system_status('Maintenance Mode');
                             sleep(60);
                             break;
                         case 'idle':
                             if (isset($json['phoromatic']['client_update_script']) && !empty($json['phoromatic']['client_update_script'])) {
                                 self::run_client_update_script($json['phoromatic']['client_update_script']);
                             }
                             //echo PHP_EOL . 'Idling, waiting for task.' . PHP_EOL;
                             phoromatic::update_system_status('Idling, Waiting For Task');
                             break;
                         case 'exit':
                             echo PHP_EOL . 'Phoromatic received a remote command to exit.' . PHP_EOL;
                             phoromatic::update_system_status('Exiting Phoromatic');
                             $do_exit = true;
                             break;
                     }
                 }
             }
         }
         if (!$do_exit) {
             if ($server_response == false) {
                 sleep(rand(10, 30));
             } else {
                 sleep(60);
             }
         }
     }
     pts_client::release_lock(PTS_USER_PATH . 'phoromatic_lock');
 }
 public static function __startup()
 {
     $halt_screensaver = pts_module::read_variable('HALT_SCREENSAVER');
     if (!empty($halt_screensaver) && !pts_strings::string_bool($halt_screensaver) || phodevi::read_property('system', 'display-server') == null) {
         return pts_module::MODULE_UNLOAD;
     }
     if (phodevi::is_macosx()) {
         // Right now there doesn't appear to be a better way to disable OS X screensaver automatically...
         return pts_module::MODULE_UNLOAD;
     }
     // GNOME Screensaver?
     if (($gt = pts_client::executable_in_path('gconftool')) != false || ($gt = pts_client::executable_in_path('gconftool-2')) != false) {
         self::$gnome_gconftool = $gt;
     }
     if (self::$gnome_gconftool != false) {
         $is_gnome_screensaver_enabled = trim(shell_exec(self::$gnome_gconftool . ' -g /apps/gnome-screensaver/idle_activation_enabled 2>&1'));
         if ($is_gnome_screensaver_enabled == 'true') {
             // Stop the GNOME Screensaver
             shell_exec(self::$gnome_gconftool . ' --type bool --set /apps/gnome-screensaver/idle_activation_enabled false 2>&1');
             self::$gnome2_screensaver_halted = true;
         }
         $sleep_display_ac = trim(shell_exec(self::$gnome_gconftool . ' -g /apps/gnome-power-manager/timeout/sleep_display_ac 2>&1'));
         if ($sleep_display_ac != 0) {
             // Don't sleep the display when on AC power
             shell_exec(self::$gnome_gconftool . ' --type int --set /apps/gnome-power-manager/timeout/sleep_display_ac 0 2>&1');
             self::$sleep_display_ac = $sleep_display_ac;
         }
     }
     if (pts_client::executable_in_path('qdbus')) {
         // KDE Screensaver?
         $is_kde_screensaver_enabled = trim(shell_exec('qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.GetActive 2>&1'));
         if ($is_kde_screensaver_enabled == 'true') {
             // Stop the KDE Screensaver
             shell_exec('qdbus org.freedesktop.ScreenSaver  /ScreenSaver SimulateUserActivity 2>&1');
             self::$kde_screensaver_halted = true;
         }
     }
     if (self::$gnome2_screensaver_halted == false && pts_client::executable_in_path('gsettings')) {
         // GNOME 3.x Screensaver?
         $is_gnome3_screensaver_enabled = trim(shell_exec('gsettings get org.gnome.desktop.session idle-delay 2>&1'));
         if (stripos($is_gnome3_screensaver_enabled, 'no such key') === false && pts_strings::last_in_string($is_gnome3_screensaver_enabled) > 0) {
             // Stop the GNOME 3.x Screensaver
             shell_exec('gsettings set org.gnome.desktop.session idle-delay 0 2>&1');
             self::$gnome3_screensaver_halted = pts_strings::last_in_string($is_gnome3_screensaver_enabled);
         }
         // GNOME 3.x Lock-Screen
         $is_gnome3_lockscreen_enabled = trim(shell_exec('gsettings get org.gnome.desktop.lockdown disable-lock-screen 2>&1'));
         if (stripos($is_gnome3_lockscreen_enabled, 'no such key') === false && pts_strings::last_in_string($is_gnome3_lockscreen_enabled) == 'false') {
             // Stop the GNOME 3.x Lock Screen
             shell_exec('gsettings set org.gnome.desktop.lockdown disable-lock-screen true 2>&1');
             self::$gnome3_lockscreen_disabled = true;
         }
         // This GNOME3 GSettings method is deprecated on distributions like GNOME 3.8 with Fedora 19
         $is_gnome3_screensaver_enabled_old = trim(shell_exec('gsettings get org.gnome.desktop.screensaver idle-activation-enabled 2>&1'));
         if ($is_gnome3_screensaver_enabled_old == 'true') {
             // Stop the GNOME 3.x Screensaver
             shell_exec('gsettings set org.gnome.desktop.screensaver idle-activation-enabled false 2>&1');
             self::$gnome3_screensaver_halted_old = true;
         }
         // GNOME 3.x Sleep Dispaly?
         $is_gnome3_sleep = trim(shell_exec('gsettings get org.gnome.settings-daemon.plugins.power sleep-display-ac 2>&1'));
         if ($is_gnome3_sleep > 0) {
             // Stop the GNOME 3.x Display Sleep
             shell_exec('gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac 0 2>&1');
             self::$sleep_display_ac = $is_gnome3_sleep;
         }
     }
     if (pts_client::executable_in_path('xfconf-query')) {
         $is_xfce_screensaver_enabled = stripos(shell_exec('xfconf-query -c xfce4-session -p /startup/screensaver/enabled 2>&1'), 'false') !== false;
         if ($is_xfce_screensaver_enabled) {
             shell_exec('xfconf-query -c xfce4-session -n -t bool -p /startup/screensaver/enabled -s false 2>&1');
             self::$xfce_screensaver_halted = true;
         }
     }
     if (getenv('DISPLAY') != false && (self::$xset = pts_client::executable_in_path('xset'))) {
         shell_exec('xset s off 2>&1');
     } else {
         if (getenv('DISPLAY') == false && pts_client::executable_in_path('setterm')) {
             shell_exec('setterm -powersave off -blank 0 2>&1');
         }
     }
     if (self::$gnome2_screensaver_halted || self::$gnome3_screensaver_halted || self::$gnome3_screensaver_halted_old || self::$kde_screensaver_halted || self::$xfce_screensaver_halted) {
         self::$screensaver_halted = true;
     }
     if (($xdg = pts_client::executable_in_path('xdg-screensaver')) == false) {
         self::$xdg_screensaver_available = $xdg;
     }
     if ($xscreensaver = pts_client::executable_in_path('xscreensaver-command')) {
         shell_exec($xscreensaver . ' -exit 2>&1');
     }
 }
 public static function user_system_process()
 {
     $last_communication_minute = date('i');
     $communication_attempts = 0;
     static $current_hw = null;
     static $current_sw = null;
     if (define('PHOROMATIC_START', true)) {
         echo PHP_EOL . 'Registering Status With Phoromatic Server @ ' . date('H:i:s') . PHP_EOL;
         $times_tried = 0;
         do {
             if ($times_tried > 0) {
                 echo PHP_EOL . 'Connection to server failed. Trying again in 60 seconds...' . PHP_EOL;
                 sleep(60);
             }
             $update_sd = phoromatic::update_system_details();
             $times_tried++;
         } while (!$update_sd && $times_tried < 5);
         if (!$update_sd) {
             echo 'Server connection still failed. Exiting...' . PHP_EOL;
             return false;
         }
         $current_hw = phodevi::system_hardware(true);
         $current_sw = phodevi::system_software(true);
         echo PHP_EOL . 'Idling 30 seconds for system to settle...' . PHP_EOL;
         sleep(30);
     }
     do {
         $exit_loop = false;
         echo PHP_EOL . 'Checking Status From Phoromatic Server @ ' . date('H:i:s');
         if ($last_communication_minute == date('i') && $communication_attempts > 2) {
             // Something is wrong, Phoromatic shouldn't be communicating with server more than three times a minute
             $response = M_PHOROMATIC_RESPONSE_IDLE;
         } else {
             $server_response = phoromatic::upload_to_remote_server(array('r' => 'status_check'));
             $xml_parser = new nye_XmlReader($server_response);
             $response = $xml_parser->getXMLValue(M_PHOROMATIC_GEN_RESPONSE);
             if (date('i') != $last_communication_minute) {
                 $last_communication_minute = date('i');
                 $communication_attempts = 0;
             }
             $communication_attempts++;
         }
         echo ' [' . $response . ']' . PHP_EOL;
         switch ($response) {
             case M_PHOROMATIC_RESPONSE_RUN_TEST:
                 $test_flags = pts_c::auto_mode | pts_c::recovery_mode;
                 do {
                     $suite_identifier = 'phoromatic-' . rand(1000, 9999);
                 } while (is_file(PTS_TEST_SUITE_PATH . 'local/' . $suite_identifier . '/suite-definition.xml'));
                 file_put_contents(PTS_TEST_SUITE_PATH . 'local/' . $suite_identifier . '/suite-definition.xml', $server_response);
                 $phoromatic_schedule_id = $xml_parser->getXMLValue(M_PHOROMATIC_ID);
                 $phoromatic_results_identifier = $xml_parser->getXMLValue(M_PHOROMATIC_SYS_NAME);
                 $phoromatic_trigger = $xml_parser->getXMLValue(M_PHOROMATIC_TRIGGER);
                 if (pts_strings::string_bool($xml_parser->getXMLValue(M_PHOROMATIC_RUN_INSTALL_COMMAND, M_PHOROMATIC_RESPONSE_TRUE))) {
                     phoromatic::set_user_context($xml_parser->getXMLValue(M_PHOROMATIC_SET_CONTEXT_PRE_INSTALL), $phoromatic_trigger, $phoromatic_schedule_id, 'INSTALL');
                     pts_client::set_test_flags($test_flags);
                     pts_test_installer::standard_install($suite_identifier);
                 }
                 phoromatic::set_user_context($xml_parser->getXMLValue(M_PHOROMATIC_SET_CONTEXT_PRE_RUN), $phoromatic_trigger, $phoromatic_schedule_id, 'INSTALL');
                 // Do the actual running
                 if (pts_test_run_manager::initial_checks($suite_identifier)) {
                     $test_run_manager = new pts_test_run_manager($test_flags);
                     // Load the tests to run
                     if ($test_run_manager->load_tests_to_run($suite_identifier)) {
                         if (pts_strings::string_bool($xml_parser->getXMLValue(M_PHOROMATIC_UPLOAD_TO_GLOBAL, 'FALSE'))) {
                             $test_run_manager->auto_upload_to_openbenchmarking();
                         }
                         // Save results?
                         $test_run_manager->auto_save_results(date('Y-m-d H:i:s'), $phoromatic_results_identifier, 'A Phoromatic run.');
                         // Run the actual tests
                         $test_run_manager->pre_execution_process();
                         $test_run_manager->call_test_runs();
                         $test_run_manager->post_execution_process();
                         // Upload to Phoromatic
                         pts_file_io::unlink(PTS_TEST_SUITE_PATH . 'local/' . $suite_identifier . '/suite-definition.xml');
                         // Upload test results
                         if (is_file(PTS_SAVE_RESULTS_PATH . $save_identifier . '/composite.xml')) {
                             phoromatic::update_system_status('Uploading Test Results');
                             $times_tried = 0;
                             do {
                                 if ($times_tried > 0) {
                                     echo PHP_EOL . 'Connection to server failed. Trying again in 60 seconds...' . PHP_EOL;
                                     sleep(60);
                                 }
                                 $uploaded_test_results = phoromatic::upload_test_results($save_identifier, $phoromatic_schedule_id, $phoromatic_results_identifier, $phoromatic_trigger);
                                 $times_tried++;
                             } while ($uploaded_test_results == false && $times_tried < 5);
                             if ($uploaded_test_results == false) {
                                 echo 'Server connection failed. Exiting...' . PHP_EOL;
                                 return false;
                             }
                             if (pts_strings::string_bool($xml_parser->getXMLValue(M_PHOROMATIC_ARCHIVE_RESULTS_LOCALLY, M_PHOROMATIC_RESPONSE_TRUE)) == false) {
                                 pts_client::remove_saved_result_file($save_identifier);
                             }
                         }
                     }
                 }
                 break;
             case M_PHOROMATIC_RESPONSE_EXIT:
                 echo PHP_EOL . 'Phoromatic received a remote command to exit.' . PHP_EOL;
                 phoromatic::update_system_status('Exiting Phoromatic');
                 pts_client::release_lock(PTS_USER_PATH . 'phoromatic_lock');
                 $exit_loop = true;
                 break;
             case M_PHOROMATIC_RESPONSE_SERVER_MAINTENANCE:
                 // The Phoromatic server is down for maintenance, so don't bother updating system status and wait longer before checking back
                 echo PHP_EOL . 'The Phoromatic server is currently down for maintenance. Waiting for service to be restored.' . PHP_EOL;
                 sleep((15 - date('i') % 15) * 60);
                 break;
             case M_PHOROMATIC_RESPONSE_SHUTDOWN:
                 echo PHP_EOL . 'Shutting down the system.' . PHP_EOL;
                 $exit_loop = true;
                 shell_exec('poweroff');
                 // Currently assuming root
                 break;
             case M_PHOROMATIC_RESPONSE_REBOOT:
                 echo PHP_EOL . 'Rebooting the system.' . PHP_EOL;
                 $exit_loop = true;
                 shell_exec('reboot');
                 // Currently assuming root
                 break;
             case M_PHOROMATIC_RESPONSE_IDLE:
             default:
                 phoromatic::update_system_status('Idling, Waiting For Task');
                 sleep((10 - date('i') % 10) * 60);
                 // Check with server every 10 minutes
                 break;
         }
         if (phodevi::system_hardware(true) != $current_hw || phodevi::system_software(true) != $current_sw) {
             // Hardware and/or software has changed while PTS/Phoromatic has been running, update the Phoromatic Server
             echo 'Updating Installed Hardware / Software With Phoromatic Server' . PHP_EOL;
             phoromatic::update_system_details();
             $current_hw = phodevi::system_hardware(true);
             $current_sw = phodevi::system_software(true);
         }
     } while ($exit_loop == false);
     phoromatic::update_system_status('Offline');
 }
 public function allow_results_sharing()
 {
     return pts_strings::string_bool($this->xml_parser->getXMLValue('PhoronixTestSuite/TestProfile/AllowResultsSharing', 'TRUE'));
 }