/** * Gets a url. * * A robust method of getting url, with many options depending on the paramters. * With no parameters, it gets the current url. * With a single string parameter * starts with "www.": return scheme with the parameter * already a url: returns the url * a FILE path: url to file, or just the filename if the file is inaccessible to the DOCUMENT_ROOT * the path after the domain: full url with the path replace with the given path * equals 'http' or 'https': converts current url to http or https * * Multiple parameters (see parameters for details). Multiple parameters may also be passed * as an associative array. * * Any parameter may be skipped, though they must be in order. * * Some parameters require prior parameters to be passed (such as argSeparator) otherwise * it may be mistaken for an eariler parameter. * * @param string $webpath The path following the domain name. * @param string|bool $https Whether the url should be secure or not. Valid values: true, false, 'http', 'https'. * @param int $port The port number that should be used (ex. http://www.google.com:57/). * @param string|array $get The query string that should be appended to the url. * @param string $argSeparator The separator that should splite arguements in the query string. * * @return string Returns the url, or just the filename for inaccessible file paths. */ public static function cache_url() { // get the arguements $args = func_get_args(); $noArgs = count($args) == 0 || count($args) == 1 && is_bool($args[0]); $fallback = null; if (count($args) == 1 && is_string($args[0])) { if (strlen($args[0]) > 4 && strtolower(substr($args[0], 0, 4)) == 'www.') { return 'http' . (is::ssl() ? 's' : '') . '://' . $args[0]; } $file_url = self::file_url($args[0]); if (is::url($file_url)) { return $file_url; } //if( file_exists($args[0]) || file_exists(MUNLA_APP_DIR.$args[0]) ) return self::file_url($args[0];//file url if (is::url($args[0])) { return $args[0]; } $fallback = $args[0]; } $webpath = null; $https = null; $port = null; $get = null; $argSeparator = '&'; if (count($args) == 1 && is_array($args[0])) { $vargs = array('webpath', 'https', 'port', 'get', 'argSeparator'); $is_assoc = false; //must use this method rather than is::assoc_array because GET can be an assoc array foreach ($vargs as $v) { if (is::existset($args[0], $v)) { $is_assoc = true; break; } } if ($is_assoc) { //arguements were passed as associative array if (is::existset($args[0], 'webpath')) { $webpath = $args[0]['webpath']; } if (is::existset($args[0], 'https')) { $https = $args[0]['https'] == 'https' || $args[0]['https'] === true; } if (is::existset($args[0], 'port')) { $port = $args[0]['port']; } if (is::existset($args[0], 'get')) { $get = $args[0]['get']; } if (is::existset($args[0], 'argSeparator')) { $argSeparator = $args[0]['argSep']; } } else { $get = $args[0]; } //not an associative array, the array is meant for get } else { // cycle through the arguments and assign them based on type // remember to not go out of order // webpath, https, port, get, argSeparator // string , bool|string, int, string|array, string $argPos = 0; while (count($args) > 0) { $arg = array_shift($args); if (is_string($arg)) { $argl = strtolower($arg); if ($argPos <= 1 && ($argl == 'https' || $argl == 'http')) { $argPos = 2; $https = $argl == 'https'; continue; } if ($argPos > 0 && $argPos <= 3) { if (strlen($arg) > 1) { $get = $arg; } $argPos = 4; continue; } if ($argPos > 3) { $argSeparator = $arg; break; } if ($argPos < 1) { $webpath = $arg; $argPos = 1; continue; } } if ($argPos <= 1 && is_bool($arg)) { $https = $arg; $argPos = 2; } if ($argPos <= 2 && is_int($arg)) { $port = $arg; $argPos = 3; } if ($argPos <= 3 && is_array($arg)) { $get = $arg; $argPos = 4; } } } //var_dump(array('webpath' => $webpath, 'https' => $https, 'port' => $port, 'get' => $get, 'argSeparator' => $argSeparator)); if (!isset($https)) { $https = is::ssl(); } if (isset($webpath) && is::url($webpath)) { if (strlen($webpath) > 4 && strtolower(substr($webpath, 0, 4)) == 'www.') { return 'http' . ($https ? 's' : '') . '://' . $webpath; } if (is::url($webpath)) { //convert http to https or https to http if (strtolower(substr($webpath, 0, 6)) == 'http:/' && $https) { return 'https:/' . substr($webpath, 6); } if (strtolower(substr($webpath, 0, 6)) == 'https:' && !$https) { return 'http:' . substr($webpath, 6); } return $webpath; } } //get the port number to use - only use if it doesn't match the default $portNum = isset($port) && ctype_digit((string) $port) ? ':' . $port : ''; if ($portNum == '') { if ($https) { if (config::$https_port != 443) { $portNum = ':' . config::$https_port; } } else { if (config::$http_port != 80) { $portNum = ':' . config::$http_port; } } } // convert get into an array if ($noArgs && !isset($get)) { if (is::existset($_SERVER, 'QUERY_STRING')) { parse_str($_SERVER['QUERY_STRING'], $get); } } else { if (isset($get) && is_string($get)) { $gs = $get; $get = array(); parse_str($gs, $get); } } if (!isset($get) || !is_array($get)) { $get = array(); } // get the domain $domain = $_SERVER['SERVER_NAME']; if ($https && isset(config::$https_domain) && $https != is::ssl() && $_SERVER['SERVER_NAME'] != config::$https_domain) { //shared ssl domain, pass original domain to GET $domain = config::$https_domain; $get['r_domain'] = get::subdomain(null, 'www'); //$_SERVER['SERVER_NAME']; } elseif (!$https && $https != is::ssl()) { //non-ssl find non-ssl domain (for shared SSL domains) $found = false; if (is::existset($_SERVER, 'QUERY_STRING')) { parse_str($_SERVER['QUERY_STRING'], $sqs); if (is::existset($sqs, 'r_domain')) { $domain = get::fulldomain($sqs['r_domain']); $found = true; } } //fallback to config if set if (!$found && isset(config::$http_domain)) { $domain = config::$http_domain; } if (isset($get['r_domain'])) { unset($get['r_domain']); } } elseif ($https && isset(config::$https_domain) && !isset($get['r_domain'])) { if (is::existset($_SERVER, 'QUERY_STRING')) { parse_str($_SERVER['QUERY_STRING'], $sqs); if (is::existset($sqs, 'r_domain')) { $get['r_domain'] = $sqs['r_domain']; } } } // if webpath isn't set - use the current webpath if (!isset($webpath)) { $qs = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; $webpath = substr($_SERVER['REQUEST_URI'], 0, strlen($_SERVER['REQUEST_URI']) - strlen($qs)); if (substr($webpath, -1) == '?') { $webpath = substr($webpath, 0, -1); } } // if the webpath doesn't start with the web root, then prepend the web root if (strlen($webpath) > 0 && substr($webpath, 0, 1) != '/') { $webpath = '/' . $webpath; } //must run twice for proper root check if (substr($webpath, 0, strlen(MUNLA_WEB_ROOT)) != MUNLA_WEB_ROOT) { $webpath = MUNLA_WEB_ROOT . (substr($webpath, 0, 1) == '/' ? substr($webpath, 1) : $webpath); } $webpath = preg_replace('/^(\\/?index(\\.php)?)(\\/?index(\\.php)?)?/i', '', $webpath); if (strlen($webpath) > 0 && substr($webpath, 0, 1) != '/') { $webpath = '/' . $webpath; } // if a query string modifier is present - append it to the query string if (defined('QS_APPEND')) { if (!isset($get)) { $get = QS_APPEND; } elseif (is_array($get)) { if (is_array(QS_APPEND)) { $get = $get + QS_APPEND; } else { $prefix = array(); parse_str(QS_APPEND, $prefix); $get = $get + $prefix; } } elseif (is_string($get)) { $pfx = strlen($get) > 0 ? $argSeparator : ''; if (!is_array(QS_APPEND)) { $get = $argSeparator . QS_APPEND; } else { $get .= $argSeparator . http_build_query(QS_APPEND, '', $argSeparator); } } } //build the query string $qs = isset($get) && is_array($get) && count($get) ? '?' . http_build_query($get, '', $argSeparator) : (isset($get) && is_string($get) ? '?' . $get : ''); //if built url is not valid - return fallback $webpath = implode('/', array_map('urlencode', explode('/', $webpath))); $url = sprintf('%s://%s%s%s%s', $https ? 'https' : 'http', $domain, $portNum, $webpath, $qs); if (!is::url($url)) { if (!isset($fallback)) { return sprintf('%s://%s%s', $https ? 'https' : 'http', $domain, $portNum); } return $fallback; } return $url; }