Пример #1
0
function realUrl($targetUrl, $baseUrl = null)
{
    if (isAbsoluteUrl($targetUrl)) {
        return $targetUrl;
    }
    ### get baseUrl
    if (!$baseUrl) {
        // default baseUrl to currently running web script
        $baseUrl = isHTTPS() ? 'https://' : 'http://';
        $baseUrl .= coalesce(@$_SERVER['HTTP_HOST'], parse_url(@$GLOBALS['SETTINGS']['adminUrl'], PHP_URL_HOST));
        $baseUrl .= inCLI() ? '/' : $_SERVER['SCRIPT_NAME'];
        // SCRIPT_NAME has filepath not web path for command line scripts so just set to /
        $baseUrl = str_replace(' ', '%20', $baseUrl);
    }
    if (!isAbsoluteUrl($baseUrl)) {
        $baseUrl = realUrl($baseUrl);
    }
    // make sure supplied $baseUrl is absolute. if it's not, make it so, relative to currently running script
    // parse baseurl into parts
    $baseUrlParts = parse_url($baseUrl);
    $baseUrlDomain = $baseUrlParts['scheme'] . '://';
    // figure out $baseUrlDomain (e.g. http://domain)
    if (@$baseUrlParts['user']) {
        $baseUrlDomain .= $baseUrlParts['user'];
        if (@$baseUrlParts['pass']) {
            $baseUrlDomain .= ':' . $baseUrlParts['pass'];
        }
        $baseUrlDomain .= '@';
    }
    $baseUrlDomain .= $baseUrlParts['host'];
    if (@$baseUrlParts['port']) {
        $baseUrlDomain .= ':' . $baseUrlParts['port'];
    }
    // if no target,  nothing specified for target, use baseUrl
    if ($targetUrl == '') {
        $absoluteUrl = $baseUrl;
    } elseif (strpos($targetUrl, '/') === 0) {
        $absoluteUrl = $baseUrlDomain . $targetUrl;
    } else {
        if (strpos($targetUrl, '?') === 0) {
            $absoluteUrl = $baseUrlDomain . $baseUrlParts['path'] . $targetUrl;
        } else {
            if (strpos($targetUrl, '#') === 0) {
                $absoluteUrl = $baseUrlDomain . $baseUrlParts['path'];
                if (@$baseUrlParts['query']) {
                    $absoluteUrl .= '?' . $baseUrlParts['query'];
                }
                $absoluteUrl = $absoluteUrl . $targetUrl;
            } else {
                $basePath = $baseUrlParts['path'];
                if (!$basePath) {
                    trigger_error("Error getting path from realUrl('{$targetUrl}', '{$baseUrl}')!\n");
                }
                // v2.62 - if basepath is webroot ("/") or webroot file ("/example.php") then remote all parent references (../) from targetUrl
                $isPathRootDir = in_array(dirname($baseUrlParts['path']), array('\\', '/'));
                if ($isPathRootDir) {
                    $targetUrl = preg_replace("|(\\.\\./)+|", '', $targetUrl);
                }
                // if the baseUrl includes a file (e.g. 'dir/admin.php'), strip it (e.g. 'dir/')
                if (!endsWith('/', $basePath)) {
                    $basePath = dirname($basePath) . '/';
                    $basePath = fixSlashes($basePath);
                    // root paths return / only, so prevent // (or \/ on windows) from the above code
                }
                $absoluteUrl = $baseUrlDomain . $basePath . $targetUrl;
            }
        }
    }
    // collapse "dir/../"s
    $madeReplacement = true;
    while ($madeReplacement) {
        // keep making replacements until we can't anymore
        $absoluteUrl = preg_replace('@[^/]+/\\.\\./@', '', $absoluteUrl, 1, $madeReplacement);
    }
    // collapse "/./"s
    $absoluteUrl = preg_replace('@/\\./@', '/', $absoluteUrl, -1);
    // url encode spaces
    $absoluteUrl = str_replace(' ', '%20', $absoluteUrl);
    // url encoded spaces
    return $absoluteUrl;
}
function getUploadDirAndUrl($fieldSchema, $settingsUploadDir = null, $settingsUploadUrl = null, $returnOnErrors = false)
{
    if ($settingsUploadDir === null) {
        $settingsUploadDir = $GLOBALS['SETTINGS']['uploadDir'];
    }
    if ($settingsUploadUrl === null) {
        $settingsUploadUrl = $GLOBALS['SETTINGS']['uploadUrl'];
    }
    // get upload dir and url
    if (@$fieldSchema['useCustomUploadDir']) {
        // if field is configured to use custom upload paths, use them
        $uploadDir = $fieldSchema['customUploadDir'];
        $uploadUrl = $fieldSchema['customUploadUrl'];
        list($baseDir, $baseUrl) = getUploadDirAndUrl(array());
        // path for resolving CUSTOM dirs/urls: Uses upload dir/url from general settings made absolute using CMS dir/url
    } else {
        // default to using global SETTINGS upload paths
        $uploadDir = $settingsUploadDir;
        $uploadUrl = $settingsUploadUrl;
        $baseDir = SCRIPT_DIR;
        // paths for resolving relative dirs and urls (CMS Script Dir)
        $baseUrl = dirname(@$GLOBALS['SETTINGS']['adminUrl']) . '/';
        // paths for resolving relative dirs and urls (CMS Script Dir URL)
    }
    $uploadDir = applyFilters('upload_uploadDir', $uploadDir, $fieldSchema);
    $uploadUrl = applyFilters('upload_uploadUrl', $uploadUrl, $fieldSchema);
    doAction('upload_dirAndUrl', $uploadDir, $uploadUrl);
    // make path absolute (and format/normalize it)
    $uploadDir = absPath($uploadDir, $baseDir);
    if (!endsWith('/', $uploadDir)) {
        $uploadDir .= '/';
    }
    // absPath doesn't return trailing slash, but we require that for user entered values and expect it in the code
    // make urls absolute, starting with either http:// or /
    if (!isAbsoluteUrl($uploadUrl) && !preg_match("|^/|", $uploadUrl)) {
        $uploadUrl = coalesce($uploadUrl, './');
        // added ./ so blank upload url gets DIR of $baseUrl, not baseurl with admin.php on the end
        $uploadUrl = realUrl($uploadUrl, $baseUrl);
        $uploadUrl = preg_replace("|^\\w+://[^/]+|", '', $uploadUrl);
        // remove scheme://hostname
    }
    if (!endsWith('/', $uploadUrl)) {
        $uploadUrl .= '/';
    }
    //
    return array($uploadDir, $uploadUrl);
}