public static function run($r)
 {
     $force_options = array('PhoronixTestSuite/Options/OpenBenchmarking/AnonymousUsageReporting' => 'FALSE');
     if (pts_network::internet_support_available() == false) {
         $force_options['PhoronixTestSuite/Options/Networking/NoInternetCommunication'] = 'TRUE';
     }
     pts_config::user_config_generate($force_options);
     $pso = pts_storage_object::recover_from_file(PTS_CORE_STORAGE);
     $pso->add_object('user_agreement_cs', 'enterprise-agree');
     $pso->save_to_file(PTS_CORE_STORAGE);
     echo PHP_EOL . 'Enterprise setup tasks executed.' . PHP_EOL . PHP_EOL;
 }
 public static function __pre_option_process()
 {
     // Once a day check for new version
     if (IS_FIRST_RUN_TODAY && pts_network::internet_support_available()) {
         // Check For pts-core updates
         $latest_reported_version = pts_network::http_get_contents('http://www.phoronix-test-suite.com/LATEST');
         $current_e = explode('.', PTS_VERSION);
         $latest_e = explode('.', $latest_reported_version);
         if ($latest_reported_version != PTS_VERSION && $latest_e[0] >= $current_e[0] && ($latest_e[1] > $current_e[1] || $latest_e[1] == $current_e[1] && $latest_e[2] >= $current_e[2])) {
             // New version of PTS is available
             pts_client::$display->generic_heading('An outdated version of the Phoronix Test Suite is installed.' . PHP_EOL . 'The version in use is v' . PTS_VERSION . ', but the latest is v' . $latest_reported_version . '.' . PHP_EOL . 'Visit http://www.phoronix-test-suite.com/ to update this software.');
         }
     }
     return pts_module::MODULE_UNLOAD;
     // This module doesn't have anything else to do
 }
 protected static function download_test_files(&$test_install_request, $download_location = false, $no_prompts = false)
 {
     // Download needed files for a test
     if ($test_install_request->get_download_object_count() == 0) {
         return true;
     }
     $identifier = $test_install_request->test_profile->get_identifier();
     pts_client::$display->test_install_downloads($test_install_request);
     if ($download_location == false) {
         $download_location = $test_install_request->test_profile->get_install_dir();
     }
     pts_file_io::mkdir($download_location);
     $module_pass = array($identifier, $test_install_request->get_download_objects());
     pts_module_manager::module_process('__pre_test_download', $module_pass);
     foreach ($test_install_request->get_download_objects() as $download_package) {
         $package_filename = $download_package->get_filename();
         $download_destination = $download_location . $package_filename;
         $download_destination_temp = $download_destination . '.pts';
         if ($download_package->get_download_location_type() == null) {
             // Attempt a possible last-minute look-aside copy cache in case a previous test in the install queue downloaded this file already
             $lookaside_copy = pts_test_install_manager::file_lookaside_test_installations($download_package);
             if ($lookaside_copy) {
                 if ($download_package->get_filesize() == 0) {
                     $download_package->set_filesize(filesize($lookaside_copy));
                 }
                 $download_package->set_download_location('LOOKASIDE_DOWNLOAD_CACHE', array($lookaside_copy));
             }
         }
         switch ($download_package->get_download_location_type()) {
             case 'IN_DESTINATION_DIR':
                 pts_client::$display->test_install_download_file('FILE_FOUND', $download_package);
                 continue;
             case 'REMOTE_DOWNLOAD_CACHE':
                 $download_tries = 0;
                 do {
                     foreach ($download_package->get_download_location_path() as $remote_download_cache_file) {
                         pts_client::$display->test_install_download_file('DOWNLOAD_FROM_CACHE', $download_package);
                         pts_network::download_file($remote_download_cache_file, $download_destination_temp);
                         if (!is_file($download_destination_temp) || filesize($download_destination_temp) == 0) {
                             self::test_install_error(null, $test_install_request, 'The file failed to download from the cache.');
                             pts_file_io::unlink($download_destination_temp);
                             break;
                         } else {
                             if ($download_package->check_file_hash($download_destination_temp)) {
                                 rename($download_destination_temp, $download_destination);
                                 break;
                             } else {
                                 self::test_install_error(null, $test_install_request, 'The check-sum of the downloaded file failed.');
                                 pts_file_io::unlink($download_destination_temp);
                             }
                         }
                     }
                     $download_tries++;
                 } while (!is_file($download_destination) && $download_tries < 2);
                 if (is_file($download_destination)) {
                     continue;
                 }
             case 'MAIN_DOWNLOAD_CACHE':
             case 'LOCAL_DOWNLOAD_CACHE':
             case 'LOOKASIDE_DOWNLOAD_CACHE':
                 $download_cache_file = pts_arrays::last_element($download_package->get_download_location_path());
                 if (is_file($download_cache_file)) {
                     if (pts_config::read_bool_config('PhoronixTestSuite/Options/Installation/SymLinkFilesFromCache', 'FALSE') && $download_package->get_download_location_type() != 'LOOKASIDE_DOWNLOAD_CACHE') {
                         // For look-aside copies never symlink (unless a pre-packaged LiveCD) in case the other test ends up being un-installed
                         // SymLinkFilesFromCache is disabled by default
                         pts_client::$display->test_install_download_file('LINK_FROM_CACHE', $download_package);
                         symlink($download_cache_file, $download_destination);
                     } else {
                         // File is to be copied
                         // Try up to two times to copy a file
                         $attempted_copies = 0;
                         do {
                             pts_client::$display->test_install_download_file('COPY_FROM_CACHE', $download_package);
                             // $context = stream_context_create();
                             // stream_context_set_params($context, array('notification' => array('pts_network', 'stream_status_callback')));
                             // TODO: get the context working correctly for this copy()
                             copy($download_cache_file, $download_destination_temp);
                             pts_client::$display->test_install_progress_completed();
                             // Verify that the file was copied fine
                             if ($download_package->check_file_hash($download_destination_temp)) {
                                 rename($download_destination_temp, $download_destination);
                                 break;
                             } else {
                                 self::test_install_error(null, $test_install_request, 'The check-sum of the copied file failed.');
                                 pts_file_io::unlink($download_destination_temp);
                             }
                             $attempted_copies++;
                         } while ($attempted_copies < 2);
                     }
                     if (is_file($download_destination)) {
                         continue;
                     }
                 }
             default:
                 $package_urls = $download_package->get_download_url_array();
                 // Download the file
                 if (!is_file($download_destination) && count($package_urls) > 0 && $package_urls[0] != null) {
                     $fail_count = 0;
                     do {
                         if (pts_network::internet_support_available()) {
                             if (!$no_prompts && pts_config::read_bool_config('PhoronixTestSuite/Options/Installation/PromptForDownloadMirror', 'FALSE') && count($package_urls) > 1) {
                                 // Prompt user to select mirror
                                 do {
                                     echo PHP_EOL . 'Available Download Mirrors:' . PHP_EOL . PHP_EOL;
                                     $url = pts_user_io::prompt_text_menu('Select Preferred Mirror', $package_urls, false);
                                 } while (pts_strings::is_url($url) == false);
                             } else {
                                 // Auto-select mirror
                                 shuffle($package_urls);
                                 do {
                                     $url = array_pop($package_urls);
                                 } while (pts_strings::is_url($url) == false && !empty($package_urls));
                             }
                             pts_client::$display->test_install_download_file('DOWNLOAD', $download_package);
                             $download_start = time();
                             pts_network::download_file($url, $download_destination_temp);
                             $download_end = time();
                         } else {
                             self::test_install_error(null, $test_install_request, 'Internet support is needed and it\'s disabled or not available.');
                             return false;
                         }
                         if ($download_package->check_file_hash($download_destination_temp)) {
                             // Download worked
                             if (is_file($download_destination_temp)) {
                                 rename($download_destination_temp, $download_destination);
                             }
                             if ($download_package->get_filesize() > 0 && $download_end != $download_start) {
                                 pts_download_speed_manager::update_download_speed_average($download_package->get_filesize(), $download_end - $download_start);
                             }
                         } else {
                             // Download failed
                             if (is_file($download_destination_temp) && filesize($download_destination_temp) < 500 && (stripos(file_get_contents($download_destination_temp), 'not found') !== false || strpos(file_get_contents($download_destination_temp), 404) !== false)) {
                                 self::test_install_error(null, $test_install_request, 'File Not Found: ' . $url);
                                 $md5_failed = false;
                             } else {
                                 if (is_file($download_destination_temp) && filesize($download_destination_temp) > 0) {
                                     self::test_install_error(null, $test_install_request, 'Checksum Failed: ' . $url);
                                     $md5_failed = true;
                                 } else {
                                     self::test_install_error(null, $test_install_request, 'Download Failed: ' . $url);
                                     $md5_failed = false;
                                 }
                             }
                             pts_file_io::unlink($download_destination_temp);
                             $fail_count++;
                             if ($fail_count > 3) {
                                 $try_again = false;
                             } else {
                                 if (count($package_urls) > 0 && $package_urls[0] != null) {
                                     self::test_install_error(null, $test_install_request, 'Attempting to download from alternate mirror.');
                                     $try_again = true;
                                 } else {
                                     if ($no_prompts) {
                                         $try_again = false;
                                     } else {
                                         if ($md5_failed) {
                                             $try_again = pts_user_io::prompt_bool_input('Try downloading the file again', true, 'TRY_DOWNLOAD_AGAIN');
                                         } else {
                                             $try_again = false;
                                         }
                                     }
                                     if ($try_again) {
                                         $package_urls[] = $url;
                                     }
                                 }
                             }
                             if (!$try_again) {
                                 //self::test_install_error(null, $test_install_request, 'Download of Needed Test Dependencies Failed!');
                                 return false;
                             }
                         }
                     } while (!is_file($download_destination));
                 }
                 pts_module_manager::module_process('__interim_test_download', $module_pass);
         }
     }
     pts_module_manager::module_process('__post_test_download', $identifier);
     return true;
 }
 public static function retrieve_gsid()
 {
     if (!pts_network::internet_support_available()) {
         return false;
     }
     // If the GSID_E and GSID_P are not known due to being from an old client
     $json = pts_openbenchmarking::make_openbenchmarking_request('retrieve_gsid', array());
     $json = json_decode($json, true);
     return isset($json['openbenchmarking']['gsid']) ? $json['openbenchmarking']['gsid'] : false;
 }
 public function post_execution_process()
 {
     if ($this->do_save_results()) {
         if ($this->result_file->get_test_count() == 0 && $this->is_new_result_file) {
             pts_file_io::delete(PTS_SAVE_RESULTS_PATH . $this->get_file_name());
             return false;
         }
         pts_file_io::delete(PTS_SAVE_RESULTS_PATH . $this->get_file_name() . '/test-logs/active/', null, true);
         if ($this->is_new_result_file || $this->result_already_contains_identifier() == false) {
             // nothing to do here now
         }
         echo PHP_EOL;
         pts_module_manager::module_process('__event_results_process', $this);
         pts_client::save_test_result($this->get_file_name() . '/composite.xml', $this->result_file->get_xml(), true, $this->results_identifier);
         pts_module_manager::module_process('__event_results_saved', $this);
         //echo PHP_EOL . 'Results Saved To: ; . PTS_SAVE_RESULTS_PATH . $this->get_file_name() . ;/composite.xml' . PHP_EOL;
         if (!$this->auto_mode) {
             if ($this->batch_mode) {
                 if ($this->batch_mode['OpenBrowser']) {
                     pts_client::display_web_page(PTS_SAVE_RESULTS_PATH . $this->get_file_name() . '/index.html', null, true, true);
                 }
             } else {
                 pts_client::display_web_page(PTS_SAVE_RESULTS_PATH . $this->get_file_name() . '/index.html', null, true, false);
             }
         }
         if ($this->allow_sharing_of_results && pts_network::internet_support_available()) {
             if ($this->auto_upload_to_openbenchmarking || pts_openbenchmarking_client::auto_upload_results() || pts_config::read_bool_config('PhoronixTestSuite/Options/Testing/AlwaysUploadResultsToOpenBenchmarking', 'FALSE')) {
                 $upload_results = true;
             } else {
                 if ($this->batch_mode) {
                     $upload_results = $this->batch_mode['UploadResults'];
                 } else {
                     if (!$this->auto_mode) {
                         $upload_results = pts_user_io::prompt_bool_input('Would you like to upload the results to OpenBenchmarking.org', true);
                     } else {
                         $upload_results = false;
                     }
                 }
             }
             if ($upload_results) {
                 $this->openbenchmarking_results_data = pts_openbenchmarking::upload_test_result($this, true);
                 if ($this->get_results_url()) {
                     if (!$this->auto_mode && !$this->batch_mode && pts_openbenchmarking_client::auto_upload_results() == false) {
                         pts_client::display_web_page($this->get_results_url(), 'Do you want to launch OpenBenchmarking.org', true);
                     }
                 } else {
                     echo PHP_EOL . 'Results Failed To Upload.' . PHP_EOL;
                 }
             }
         }
     }
 }
 public static function download_test_suite($qualified_identifier)
 {
     if (is_file(PTS_TEST_SUITE_PATH . $qualified_identifier . '/suite-definition.xml')) {
         return true;
     }
     $file = PTS_OPENBENCHMARKING_SCRATCH_PATH . $qualified_identifier . '.zip';
     if (pts_network::internet_support_available()) {
         $hash_json = pts_openbenchmarking::make_openbenchmarking_request('suite_hash', array('i' => $qualified_identifier));
         $hash_json = json_decode($hash_json, true);
         $hash_check = isset($hash_json['openbenchmarking']['suite']['hash']) ? $hash_json['openbenchmarking']['suite']['hash'] : null;
         // should also check for ['openbenchmarking']['suite']['error'] problems
     }
     if (!is_file($file)) {
         if (pts_network::internet_support_available()) {
             $test_suite = pts_openbenchmarking::make_openbenchmarking_request('download_suite', array('i' => $qualified_identifier));
             if ($test_suite != null && ($hash_check == null || $hash_check == sha1($test_suite))) {
                 // save it
                 file_put_contents($file, $test_suite);
                 $hash_check = null;
             } else {
                 if (is_file('/var/cache/phoronix-test-suite/openbenchmarking.org/' . $qualified_identifier . '.zip') && ($hash_check == null || sha1_file('/var/cache/phoronix-test-suite/openbenchmarking.org/' . $qualified_identifier . '.zip') == $hash_check)) {
                     copy('/var/cache/phoronix-test-suite/openbenchmarking.org/' . $qualified_identifier . '.zip', $file);
                 }
             }
         }
         if (!is_file($file) && ($test_suite = self::phoromatic_server_ob_cache_request('suite', substr($qualified_identifier, 0, strpos($qualified_identifier, '/')), substr($qualified_identifier, strpos($qualified_identifier, '/') + 1)))) {
             if ($b64 = base64_decode($test_suite)) {
                 $test_suite = $b64;
             }
             file_put_contents($file, $test_suite);
         }
         if (PTS_IS_CLIENT && !is_file($file)) {
             trigger_error('Network support is needed to obtain ' . $qualified_identifier . ' data.' . PHP_EOL, E_USER_ERROR);
             return false;
         }
     }
     if (!is_file(PTS_TEST_SUITE_PATH . $qualified_identifier . '/suite-definition.xml') && is_file($file) && ($hash_check == null || is_file($file) && sha1_file($file) == $hash_check)) {
         // extract it
         pts_file_io::mkdir(PTS_TEST_SUITE_PATH . dirname($qualified_identifier));
         pts_file_io::mkdir(PTS_TEST_SUITE_PATH . $qualified_identifier);
         pts_compression::zip_archive_extract($file, PTS_TEST_SUITE_PATH . $qualified_identifier);
         if (is_file(PTS_TEST_SUITE_PATH . $qualified_identifier . '/suite-definition.xml')) {
             return true;
         } else {
             unlink($file);
             return false;
         }
     }
     return false;
 }
 public static function run($r)
 {
     pts_client::$display->generic_heading('Available Tests');
     $available_tests = pts_openbenchmarking::available_tests(false);
     $available_suites = pts_openbenchmarking::available_suites(false);
     $test_count = count($available_tests);
     $suite_count = count($available_suites);
     $total_count = $test_count + $suite_count;
     $total_cache_count = 0;
     $total_cache_size = 0;
     if ($test_count == 0 || !pts_network::internet_support_available()) {
         echo PHP_EOL . 'No tests found. Please check that you have Internet connectivity to download test profile data from OpenBenchmarking.org. The Phoronix Test Suite has documentation on configuring the network setup, proxy settings, and PHP network options. Please contact Phoronix Media if you continuing to experience problems.' . PHP_EOL . PHP_EOL;
         return false;
     }
     $terminal_width = pts_client::terminal_width();
     // Cache test profiles
     foreach ($available_tests as $i => $identifier) {
         $repo = substr($identifier, 0, strpos($identifier, '/'));
         $test = substr($identifier, strlen($repo) + 1);
         $repo_index = pts_openbenchmarking::read_repository_index($repo);
         echo $i . '/' . $total_count . ': ' . ($repo_index['tests'][$test]['title'] != null ? $repo_index['tests'][$test]['title'] . ' [' . $repo_index['tests'][$test]['test_type'] . ']' : null) . PHP_EOL;
         foreach ($repo_index['tests'][$test]['versions'] as $version) {
             $qualified_identifier = $repo . '/' . $test . '-' . $version;
             echo $qualified_identifier;
             $success = pts_openbenchmarking::download_test_profile($repo . '/' . $test . '-' . $version);
             if ($success && is_file(PTS_OPENBENCHMARKING_SCRATCH_PATH . $qualified_identifier . '.zip')) {
                 $file_size = round(filesize(PTS_OPENBENCHMARKING_SCRATCH_PATH . $qualified_identifier . '.zip') / 1024, 2);
                 $info = $file_size . 'KB - ' . sha1_file(PTS_OPENBENCHMARKING_SCRATCH_PATH . $qualified_identifier . '.zip');
                 $r_size = $terminal_width - strlen($qualified_identifier) - 3 - strlen($info);
                 if ($r_size > 0) {
                     echo ' ' . str_repeat('.', $terminal_width - strlen($qualified_identifier) - 3 - strlen($info)) . ' ' . $info . PHP_EOL;
                 }
                 $total_cache_count++;
                 $total_cache_size += $file_size;
             }
         }
         echo PHP_EOL;
     }
     // Cache test suites
     foreach ($available_suites as $i => $identifier) {
         $repo = substr($identifier, 0, strpos($identifier, '/'));
         $test = substr($identifier, strlen($repo) + 1);
         $repo_index = pts_openbenchmarking::read_repository_index($repo);
         echo $i + $test_count . '/' . $total_count . ': ' . $repo_index['suites'][$test]['title'] . PHP_EOL;
         foreach ($repo_index['suites'][$test]['versions'] as $version) {
             $qualified_identifier = $repo . '/' . $test . '-' . $version;
             echo $qualified_identifier;
             $success = pts_openbenchmarking::download_test_suite($repo . '/' . $test . '-' . $version);
             if ($success && is_file(PTS_OPENBENCHMARKING_SCRATCH_PATH . $qualified_identifier . '.zip')) {
                 $file_size = round(filesize(PTS_OPENBENCHMARKING_SCRATCH_PATH . $qualified_identifier . '.zip') / 1024, 2);
                 $info = $file_size . 'KB - ' . sha1_file(PTS_OPENBENCHMARKING_SCRATCH_PATH . $qualified_identifier . '.zip');
                 $dot_size = $terminal_width - strlen($qualified_identifier) - 3 - strlen($info);
                 echo ' ' . str_repeat('.', $dot_size >= 0 ? $dot_size : 0) . ' ' . $info . PHP_EOL;
                 $total_cache_count++;
                 $total_cache_size += $file_size;
             }
         }
         echo PHP_EOL;
     }
     echo PHP_EOL . $total_cache_count . ' Files Cached' . PHP_EOL . $test_count . ' Test Profiles' . PHP_EOL . $suite_count . ' Test Suites' . PHP_EOL . $total_cache_size . 'KB Total Cache Size' . PHP_EOL . PHP_EOL;
 }
 public function post_execution_process()
 {
     if ($this->do_save_results()) {
         if ($this->result_file_writer->get_result_count() == 0 && !pts_result_file::is_test_result_file($this->get_file_name()) && pts_c::$test_flags ^ pts_c::is_recovering && pts_c::$test_flags ^ pts_c::remote_mode) {
             pts_file_io::delete(PTS_SAVE_RESULTS_PATH . $this->get_file_name());
             return false;
         }
         pts_file_io::delete(PTS_SAVE_RESULTS_PATH . $this->get_file_name() . '/test-logs/active/', null, true);
         if (pts_c::$test_flags ^ pts_c::is_recovering && (!pts_result_file::is_test_result_file($this->get_file_name()) || $this->result_already_contains_identifier() == false)) {
             $this->result_file_writer->add_test_notes(pts_test_notes_manager::generate_test_notes($this->tests_to_run), $this->generate_json_system_attributes());
         }
         echo PHP_EOL;
         pts_module_manager::module_process('__event_results_process', $this);
         pts_client::save_result_file($this->result_file_writer, $this->get_file_name());
         pts_module_manager::module_process('__event_results_saved', $this);
         //echo PHP_EOL . 'Results Saved To: ; . PTS_SAVE_RESULTS_PATH . $this->get_file_name() . ;/composite.xml' . PHP_EOL;
         if (!(pts_c::$test_flags & pts_c::auto_mode)) {
             if (pts_c::$test_flags & pts_c::batch_mode) {
                 if (self::$batch_mode_options['OpenBrowser']) {
                     pts_client::display_web_page(PTS_SAVE_RESULTS_PATH . $this->get_file_name() . '/index.html', null, true, true);
                 }
             } else {
                 pts_client::display_web_page(PTS_SAVE_RESULTS_PATH . $this->get_file_name() . '/index.html', null, true, false);
             }
         }
         if ($this->allow_sharing_of_results && pts_network::internet_support_available()) {
             if ($this->auto_upload_to_openbenchmarking || pts_openbenchmarking_client::auto_upload_results() || pts_flags::upload_to_openbenchmarking()) {
                 $upload_results = true;
             } else {
                 if (pts_c::$test_flags & pts_c::batch_mode) {
                     $upload_results = self::$batch_mode_options['UploadResults'];
                 } else {
                     if (!(pts_c::$test_flags & pts_c::auto_mode)) {
                         $upload_results = pts_user_io::prompt_bool_input('Would you like to upload the results to OpenBenchmarking.org', true);
                     } else {
                         $upload_results = false;
                     }
                 }
             }
             if ($upload_results) {
                 $this->openbenchmarking_results_data = pts_openbenchmarking::upload_test_result($this, true);
                 if ($this->get_results_url()) {
                     if (!(pts_c::$test_flags & pts_c::auto_mode) && pts_openbenchmarking_client::auto_upload_results() == false) {
                         pts_client::display_web_page($this->get_results_url(), 'Do you want to launch OpenBenchmarking.org', true);
                     }
                 } else {
                     echo PHP_EOL . 'Results Failed To Upload.' . PHP_EOL;
                 }
             }
         }
     }
 }