1<?php 2// $Header: /cvsroot/html2ps/utils_url.php,v 1.9 2006/07/09 09:07:46 Konstantin Exp $ 3 4function guess_url($path, $baseurl) { 5 // Check if path is absolute 6 // 'Path' is starting with protocol identifier? 7 if (preg_match("!^[a-zA-Z]+://.*!",$path)) { 8 return $path; 9 }; 10 11 $data = parse_url($baseurl); 12 13 $default_host = array( 14 'http' => 'localhost', 15 'https' => 'localhost', 16 'file' => '' 17 ); 18 19 $base_scheme = isset($data['scheme']) ? $data['scheme'] : "http"; 20 $base_port = isset($data['port']) ? ":".$data['port'] : ""; 21 $base_user = isset($data['user']) ? $data['user'] : ""; 22 $base_pass = isset($data['pass']) ? $data['pass'] : ""; 23 $base_host = isset($data['host']) ? $data['host'] : (isset($default_host[$base_scheme]) ? $default_host[$base_scheme] : ""); 24 $base_path = isset($data['path']) ? $data['path'] : "/"; 25 26 /** 27 * Workaround: Some PHP versions do remove the leading slash from the 28 * 'file://' URLs with empty host name, while some do not. 29 * 30 * An example of such URL is: file:///D:/path/dummy.html 31 * The path should be: /D:/path/dummy.html 32 * 33 * Here we check if the leading slash is present and 34 * add it if it is missing. 35 */ 36 if ($base_scheme == "file" && PHP_OS == "WINNT") { 37 if (strlen($base_path) > 0) { 38 if ($base_path{0} != "/") { 39 $base_path = "/".$base_path; 40 }; 41 }; 42 }; 43 44 $base_user_pass = ""; 45 if ($base_user || $base_pass) { 46 $base_user_pass = sprintf("%s:%s@", $base_user, $base_pass); 47 } 48 49 // 'Path' is starting at scheme? 50 if (substr($path,0,2) == "//") { 51 $guessed = $base_scheme . ':' . $path; 52 return $guessed; 53 } 54 55 // 'Path' is starting at root? 56 if (substr($path,0,1) == "/") { 57 $guessed = $base_scheme . '://' . $base_user_pass . $base_host . $base_port . $path; 58 return $guessed; 59 }; 60 61 // 'Path' is relative from the current position 62 if (preg_match("#^(/.*)/[^/]*$#", $base_path, $matches)) { 63 $base_path_dir = $matches[1]; 64 } else { 65 $base_path_dir = ""; 66 }; 67 $guessed = $base_scheme . '://' . $base_user_pass . $base_host . $base_port . $base_path_dir . '/' . $path; 68 return $guessed; 69}; 70 71?>