Пример #1
0
function LinkBracketLink($bracketlink)
{
    // $bracketlink will start and end with brackets; in between will
    // be either a page name, a URL or both separated by a pipe.
    $wikicreolesyntax = false;
    if (string_starts_with($bracketlink, "[[") or string_starts_with($bracketlink, "#[[")) {
        $wikicreolesyntax = true;
        $bracketlink = str_replace("[[", "[", $bracketlink);
        $bracketlink = str_replace("]]", "]", $bracketlink);
    }
    // Strip brackets and leading space
    // bug#1904088  Some brackets links on 2 lines cause the parser to crash
    preg_match('/(\\#?) \\[\\s* (?: (.*?) \\s* (?<!' . ESCAPE_CHAR . ')(\\|) )? \\s* (.+?) \\s*\\]/x', str_replace("\n", " ", $bracketlink), $matches);
    if (count($matches) < 4) {
        // "[ personal\ninformation manager | PhpWiki:PersonalWiki ]"
        trigger_error(_("Invalid [] syntax ignored") . ": " . $bracketlink, E_USER_WARNING);
        return new Cached_Link();
    }
    list(, $hash, $label, $bar, $rawlink) = $matches;
    if ($wikicreolesyntax and $label) {
        $temp = $label;
        $label = $rawlink;
        $rawlink = $temp;
    }
    // Mediawiki compatibility: allow "Image:" and "File:"
    // as synonyms of "Upload:"
    if (string_starts_with($rawlink, "Image:")) {
        $rawlink = str_replace("Image:", "Upload:", $rawlink);
    }
    if (string_starts_with($rawlink, "File:")) {
        $rawlink = str_replace("File:", "Upload:", $rawlink);
    }
    $label = UnWikiEscape($label);
    /*
     * Check if the user has typed a explicit URL. This solves the
     * problem where the URLs have a ~ character, which would be stripped away.
     *   "[http:/server/~name/]" will work as expected
     *   "http:/server/~name/"   will NOT work as expected, will remove the ~
     */
    if (string_starts_with($rawlink, "http://") or string_starts_with($rawlink, "https://")) {
        $link = $rawlink;
        // Mozilla Browser URI Obfuscation Weakness 2004-06-14
        //   http://www.securityfocus.com/bid/10532/
        //   goodurl+"%2F%20%20%20."+badurl
        if (preg_match("/%2F(%20)+\\./i", $rawlink)) {
            $rawlink = preg_replace("/%2F(%20)+\\./i", "%2F.", $rawlink);
        }
    } else {
        $link = UnWikiEscape($rawlink);
    }
    /* Relatives links by Joel Schaubert.
     * Recognize [../bla] or [/bla] as relative links, without needing http://
     * but [ /link ] only if SUBPAGE_SEPERATOR is not "/".
     * Normally /Page links to the subpage /Page.
     */
    if (SUBPAGE_SEPARATOR == '/') {
        if (preg_match('/^\\.\\.\\//', $link)) {
            return new Cached_ExternalLink($link, $label);
        }
    } else {
        if (preg_match('/^(\\.\\.\\/|\\/)/', $link)) {
            return new Cached_ExternalLink($link, $label);
        }
    }
    // Handle "[[SandBox|{{image.jpg}}]]" and "[[SandBox|{{image.jpg|alt text}}]]"
    if (string_starts_with($label, "{{")) {
        $imgurl = substr($label, 2, -2);
        // Remove "{{" and "}}"
        $pipe = strpos($imgurl, '|');
        if ($pipe === false) {
            $label = LinkImage(getUploadDataPath() . $imgurl, $link);
        } else {
            list($img, $alt) = explode("|", $imgurl);
            $label = LinkImage(getUploadDataPath() . $img, $alt);
        }
    } else {
        // [label|link]
        // If label looks like a url to an image or object, we want an image link.
        if (isImageLink($label)) {
            $imgurl = $label;
            $intermap = getInterwikiMap();
            if (preg_match("/^" . $intermap->getRegexp() . ":/", $label)) {
                $imgurl = $intermap->link($label);
                $imgurl = $imgurl->getAttr('href');
            } elseif (!preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $imgurl)) {
                // local theme linkname like 'images/next.gif'.
                global $WikiTheme;
                $imgurl = $WikiTheme->getImageURL($imgurl);
            }
            // for objects (non-images) the link is taken as alt tag,
            // which is in return taken as alternative img
            $label = LinkImage($imgurl, $link);
        }
    }
    if ($hash) {
        // It's an anchor, not a link...
        $id = MangleXmlIdentifier($link);
        return HTML::a(array('name' => $id, 'id' => $id), $bar ? $label : $link);
    }
    if (preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $link)) {
        // if it's an image, embed it; otherwise, it's a regular link
        if (isImageLink($link) and empty($label)) {
            // patch #1348996 by Robert Litwiniec
            return LinkImage($link, $label);
        } else {
            return new Cached_ExternalLink($link, $label);
        }
    } elseif (substr($link, 0, 8) == 'phpwiki:') {
        return new Cached_PhpwikiURL($link, $label);
    } elseif (preg_match("/^ (\\w+) (:[:=]) (.*) \$/x", $link) and !isImageLink($link)) {
        return new Cached_SemanticLink($link, $label);
    } elseif (substr($link, 0, 1) == ':') {
        return new Cached_WikiLink($link, $label);
    } elseif (strstr($link, ':') and $intermap = getInterwikiMap() and preg_match("/^" . $intermap->getRegexp() . ":/", $link)) {
        // trigger_error("label: $label link: $link", E_USER_WARNING);
        if (empty($label) and isImageLink($link)) {
            // if without label => inlined image [File:xx.gif]
            $imgurl = $intermap->link($link);
            return LinkImage($imgurl->getAttr('href'), $link);
        }
        return new Cached_InterwikiLink($link, $label);
    } else {
        // Split anchor off end of pagename.
        if (preg_match('/\\A(.*)(?<!' . ESCAPE_CHAR . ')#(.*?)\\Z/', $rawlink, $m)) {
            list(, $rawlink, $anchor) = $m;
            $pagename = UnWikiEscape($rawlink);
            $anchor = UnWikiEscape($anchor);
            if (!$label) {
                $label = $link;
            }
        } else {
            $pagename = $link;
            $anchor = false;
        }
        return new Cached_WikiLink($pagename, $label, $anchor);
    }
}
Пример #2
0
function LinkBracketLink($bracketlink)
{
    // $bracketlink will start and end with brackets; in between will
    // be either a page name, a URL or both separated by a pipe.
    // strip brackets and leading space
    // FIXME: \n inside [] will lead to errors
    preg_match('/(\\#?) \\[\\s* (?: (.*?) \\s* (?<!' . ESCAPE_CHAR . ')(\\|) )? \\s* (.+?) \\s*\\]/x', $bracketlink, $matches);
    if (count($matches) < 4) {
        trigger_error(_("Invalid [] syntax ignored") . ": " . $bracketlink, E_USER_WARNING);
        return new Cached_Link();
    }
    list(, $hash, $label, $bar, $rawlink) = $matches;
    $label = UnWikiEscape($label);
    /*
     * Check if the user has typed a explicit URL. This solves the
     * problem where the URLs have a ~ character, which would be stripped away.
     *   "[http:/server/~name/]" will work as expected
     *   "http:/server/~name/"   will NOT work as expected, will remove the ~
     */
    if (strstr($rawlink, "http://") or strstr($rawlink, "https://")) {
        $link = $rawlink;
        // Mozilla Browser URI Obfuscation Weakness 2004-06-14
        //   http://www.securityfocus.com/bid/10532/
        //   goodurl+"%2F%20%20%20."+badurl
        if (preg_match("/%2F(%20)+\\./i", $rawlink)) {
            $rawlink = preg_replace("/%2F(%20)+\\./i", "%2F.", $rawlink);
        }
    } else {
        $link = UnWikiEscape($rawlink);
    }
    /* Relatives links by Joel Schaubert.
     * Recognize [../bla] or [/bla] as relative links, without needing http://
     */
    /* if (preg_match('/^(\.\.\/|\/)/', $link)) {
           return new Cached_ExternalLink($link, $label);
       }*/
    // [label|link]
    // if label looks like a url to an image, we want an image link.
    if (isImageLink($label)) {
        $imgurl = $label;
        $intermap = getInterwikiMap();
        if (preg_match("/^" . $intermap->getRegexp() . ":/", $label)) {
            $imgurl = $intermap->link($label);
            $imgurl = $imgurl->getAttr('href');
        } elseif (!preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $imgurl)) {
            // local theme linkname like 'images/next.gif'.
            global $WikiTheme;
            $imgurl = $WikiTheme->getImageURL($imgurl);
        }
        $label = LinkImage($imgurl, $link);
    }
    if ($hash) {
        // It's an anchor, not a link...
        $id = MangleXmlIdentifier($link);
        return HTML::a(array('name' => $id, 'id' => $id), $bar ? $label : $link);
    }
    if (preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $link)) {
        // if it's an image, embed it; otherwise, it's a regular link
        if (isImageLink($link)) {
            return LinkImage($link, $label);
        } else {
            return new Cached_ExternalLink($link, $label);
        }
    } elseif (preg_match("/^phpwiki:/", $link)) {
        return new Cached_PhpwikiURL($link, $label);
    } elseif (strstr($link, ':') and $intermap = getInterwikiMap() and preg_match("/^" . $intermap->getRegexp() . ":/", $link)) {
        // trigger_error("label: $label link: $link", E_USER_WARNING);
        if (empty($label) and isImageLink($link)) {
            // if without label => inlined image [File:xx.gif]
            $imgurl = $intermap->link($link);
            return LinkImage($imgurl->getAttr('href'), $label);
        }
        return new Cached_InterwikiLink($link, $label);
    } else {
        // Split anchor off end of pagename.
        if (preg_match('/\\A(.*)(?<!' . ESCAPE_CHAR . ')#(.*?)\\Z/', $rawlink, $m)) {
            list(, $rawlink, $anchor) = $m;
            $pagename = UnWikiEscape($rawlink);
            $anchor = UnWikiEscape($anchor);
            if (!$label) {
                $label = $link;
            }
        } else {
            $pagename = $link;
            $anchor = false;
        }
        return new Cached_WikiLink($pagename, $label, $anchor);
    }
}