public static function run($r)
 {
     foreach (pts_types::identifiers_to_test_profile_objects($r, true, true) as $test_profile) {
         echo 'Checking: ' . $test_profile . PHP_EOL;
         foreach (pts_test_install_request::read_download_object_list($test_profile) as $test_file_download) {
             foreach ($test_file_download->get_download_url_array() as $url) {
                 $stream_context = pts_network::stream_context_create();
                 stream_context_set_params($stream_context, array('notification' => 'pts_stream_status_callback'));
                 $file_pointer = @fopen($url, 'r', false, $stream_context);
                 //fread($file_pointer, 1024);
                 if ($file_pointer == false) {
                     echo PHP_EOL . 'BAD URL: ' . $test_file_download->get_filename() . ' / ' . $url . PHP_EOL;
                 } else {
                     @fclose($file_pointer);
                 }
             }
         }
     }
 }
 public static function validate_test_profile(&$test_profile)
 {
     if ($test_profile->get_file_location() == null) {
         echo PHP_EOL . 'ERROR: The file location of the XML test profile source could not be determined.' . PHP_EOL;
         return false;
     }
     // Validate the XML against the XSD Schemas
     libxml_clear_errors();
     // Now re-create the pts_test_profile object around the rewritten XML
     $test_profile = new pts_test_profile($test_profile->get_identifier());
     $valid = $test_profile->validate();
     if ($valid == false) {
         echo PHP_EOL . 'Errors occurred parsing the main XML.' . PHP_EOL;
         pts_validation::process_libxml_errors();
         return false;
     }
     // Rewrite the main XML file to ensure it is properly formatted, elements are ordered according to the schema, etc...
     $test_profile_writer = new pts_test_profile_writer();
     $test_profile_writer->rebuild_test_profile($test_profile);
     $test_profile_writer->save_xml($test_profile->get_file_location());
     // Now re-create the pts_test_profile object around the rewritten XML
     $test_profile = new pts_test_profile($test_profile->get_identifier());
     $valid = $test_profile->validate();
     if ($valid == false) {
         echo PHP_EOL . 'Errors occurred parsing the main XML.' . PHP_EOL;
         pts_validation::process_libxml_errors();
         return false;
     } else {
         echo PHP_EOL . 'Test Profile XML Is Valid.' . PHP_EOL;
     }
     // Validate the downloads file
     $download_xml_file = $test_profile->get_file_download_spec();
     if (empty($download_xml_file) == false) {
         $writer = new pts_test_profile_downloads_writer();
         $writer->rebuild_download_file($test_profile);
         $writer->save_xml($download_xml_file);
         $downloads_parser = new pts_test_downloads_nye_XmlReader($download_xml_file);
         $valid = $downloads_parser->validate();
         if ($valid == false) {
             echo PHP_EOL . 'Errors occurred parsing the downloads XML.' . PHP_EOL;
             pts_validation::process_libxml_errors();
             return false;
         } else {
             echo PHP_EOL . 'Test Downloads XML Is Valid.' . PHP_EOL;
         }
         // Validate the individual download files
         echo PHP_EOL . 'Testing File Download URLs.' . PHP_EOL;
         $files_missing = 0;
         $file_count = 0;
         foreach (pts_test_install_request::read_download_object_list($test_profile) as $download) {
             foreach ($download->get_download_url_array() as $url) {
                 $stream_context = pts_network::stream_context_create();
                 stream_context_set_params($stream_context, array('notification' => 'pts_stream_status_callback'));
                 $file_pointer = fopen($url, 'r', false, $stream_context);
                 if ($file_pointer == false) {
                     echo 'File Missing: ' . $download->get_filename() . ' / ' . $url . PHP_EOL;
                     $files_missing++;
                 } else {
                     fclose($file_pointer);
                 }
                 $file_count++;
             }
         }
         if ($files_missing > 0) {
             return false;
         }
     }
     // Validate the parser file
     $parser_file = $test_profile->get_file_parser_spec();
     if (empty($parser_file) == false) {
         $writer = self::rebuild_result_parser_file($parser_file);
         $writer->saveXMLFile($parser_file);
         $parser = new pts_parse_results_nye_XmlReader($parser_file);
         $valid = $parser->validate();
         if ($valid == false) {
             echo PHP_EOL . 'Errors occurred parsing the results parser XML.' . PHP_EOL;
             pts_validation::process_libxml_errors();
             return false;
         } else {
             echo PHP_EOL . 'Test Results Parser XML Is Valid.' . PHP_EOL;
         }
     }
     // Make sure no extra files are in there
     $allowed_files = pts_validation::test_profile_permitted_files();
     foreach (pts_file_io::glob($test_profile->get_resource_dir() . '*') as $tp_file) {
         if (!is_file($tp_file) || !in_array(basename($tp_file), $allowed_files)) {
             echo PHP_EOL . basename($tp_file) . ' is not allowed in the test package.' . PHP_EOL;
             return false;
         }
     }
     return true;
 }
 public static function stream_download($download, $download_to, $stream_context_parameters = null, $callback_function = array('pts_network', 'stream_status_callback'))
 {
     $stream_context = pts_network::stream_context_create($stream_context_parameters);
     if (function_exists('stream_context_set_params')) {
         // HHVM 2.1 doesn't have stream_context_set_params()
         stream_context_set_params($stream_context, array('notification' => $callback_function));
     }
     /*
     if(strpos($download, 'https://openbenchmarking.org/') !== false)
     {
     	stream_context_set_option($stream_context, 'ssl', 'local_cert', PTS_CORE_STATIC_PATH . 'certificates/openbenchmarking-server.pem');
     }
     else if(strpos($download, 'https://www.phoromatic.com/') !== false)
     {
     	stream_context_set_option($stream_context, 'ssl', 'local_cert', PTS_CORE_STATIC_PATH . 'certificates/phoromatic-com.pem');
     }
     */
     $file_pointer = @fopen($download, 'r', false, $stream_context);
     if (is_resource($file_pointer) && file_put_contents($download_to, $file_pointer)) {
         return true;
     }
     return false;
 }
 public function scan_download_caches($local_download_caches, $remote_download_caches, $remote_files, $phoromatic_server_caches, $skip_hash_checks = false)
 {
     $download_location = $this->test_profile->get_install_dir();
     $main_download_cache = pts_strings::add_trailing_slash(pts_strings::parse_for_home_directory(pts_config::read_user_config('PhoronixTestSuite/Options/Installation/CacheDirectory', PTS_DOWNLOAD_CACHE_PATH)));
     foreach ($this->test_files as &$download_package) {
         $package_filename = $download_package->get_filename();
         if (is_file($download_location . $package_filename)) {
             // File is already there in the test/destination directory, must have been previously downloaded
             // Could add an MD5 check here to ensure validity, but if it made it here it was already valid unless user modified it
             if ($download_package->get_filesize() == 0) {
                 $download_package->set_filesize(filesize($download_location . $package_filename));
             }
             $download_package->set_download_location('IN_DESTINATION_DIR');
         } else {
             if (is_file($main_download_cache . $package_filename)) {
                 // In main download cache
                 if ($download_package->get_filesize() == 0) {
                     $download_package->set_filesize(filesize($main_download_cache . $package_filename));
                 }
                 $download_package->set_download_location('MAIN_DOWNLOAD_CACHE', array($main_download_cache . $package_filename));
             } else {
                 if (is_file(PTS_SHARE_PATH . 'download-cache/' . $package_filename)) {
                     // In system's /usr/share download cache
                     if ($download_package->get_filesize() == 0) {
                         $download_package->set_filesize(filesize(PTS_SHARE_PATH . 'download-cache/' . $package_filename));
                     }
                     $download_package->set_download_location('MAIN_DOWNLOAD_CACHE', array(PTS_SHARE_PATH . 'download-cache/' . $package_filename));
                 } else {
                     // Scan the local download caches
                     foreach ($local_download_caches as &$cache_directory) {
                         if (is_file($cache_directory . $package_filename) && ($skip_hash_checks || $download_package->check_file_hash($cache_directory . $package_filename))) {
                             if ($download_package->get_filesize() == 0) {
                                 $download_package->set_filesize(filesize($cache_directory . $package_filename));
                             }
                             $download_package->set_download_location('LOCAL_DOWNLOAD_CACHE', array($cache_directory . $package_filename));
                             break;
                         }
                     }
                     // Look-aside download cache copy
                     // Check to see if the same package name with the same package check-sum is already present in another test installation
                     $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));
                     }
                     // Check Phoromatic server caches
                     if ($download_package->get_download_location_type() == null && $phoromatic_server_caches) {
                         foreach ($phoromatic_server_caches as $server_url => $repo) {
                             if (isset($repo[$package_filename]) && ($skip_hash_checks || $repo[$package_filename]['md5'] == $download_package->get_md5() || $repo[$package_filename]['sha256'] == $download_package->get_sha256() || $download_package->get_sha256() == null && $download_package->get_md5() == null)) {
                                 $download_package->set_download_location('REMOTE_DOWNLOAD_CACHE', array($server_url . '/download-cache.php?download=' . $package_filename));
                                 break;
                             }
                         }
                     }
                     // If still not found, check remote download caches
                     if ($download_package->get_download_location_type() == null) {
                         if (isset($remote_files[$package_filename])) {
                             $download_package->set_download_location('REMOTE_DOWNLOAD_CACHE', $remote_files[$package_filename]);
                         } else {
                             if (!empty($remote_download_caches)) {
                                 // Check for files manually
                                 foreach ($remote_download_caches as $remote_dir) {
                                     $remote_file = $remote_dir . $package_filename;
                                     $stream_context = pts_network::stream_context_create();
                                     $file_pointer = fopen($remote_file, 'r', false, $stream_context);
                                     if ($file_pointer !== false) {
                                         $download_package->set_download_location('REMOTE_DOWNLOAD_CACHE', $remote_file);
                                         break;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }