e107inc/e107

View on GitHub
e107_core/bbcodes/link.bb

Summary

Maintainability
Test Coverage
// $Id$
//<?


$class = "bbcode ".e107::getBB()->getClass('link');
global $pref, $parm;

/**
 *    e107 BBCodes
 *
 *    @package    e107
 *    @subpackage    bbcode
 *    @version     $Id$;
 *
 *    @todo try and avoid URLs with a language in [..]
 */

/**
    [link=$parm $extras]$code_text[/link]
    Correct Usage:
    [link=http://mysite.com external]My text[/link]
    [link=http://mysite.com rel=external]My text[/link]
    [link=external]http://mysite.com[/link]
    [link]http://mysite.com[/link]
    [link=mailto:myemail@email.com]My name[/link]
    Historic usage:
    [link=external=http://mysite.com]My text[/link]
*/

    $tp = e107::getParser();

    $parm =  $tp->filter(trim($parm));

    /* Fix for people using link=external= */
    if(strpos($parm,"external=") !== FALSE)
    {
        list($extras,$parm) = explode("=",$parm,2);
        $parm = $parm." ".$extras;
    }

    if(strpos($parm, 'mailto') === 0)
    {
        list($pre,$email) = explode(":",$parm);
        list($p1,$p2) = explode("@",$email);
        $p2=rawurlencode($p2);            // Primarily to pick up spaces, which are not allowed
        return "<a class='bbcode' rel='external' href='javascript:window.location=\"mai\"+\"lto:\"+\"$p1\"+\"@\"+\"$p2\";self.close();' onmouseover='window.status=\"mai\"+\"lto:\"+\"$p1\"+\"@\"+\"$p2\"; return true;' onmouseout='window.status=\"\";return true;'>".$code_text."</a>";
    }

    if (substr($code_text,0,1) === ']')
    {    // Special fix for E107 urls including a language (not nice, really)
        $code_text = substr($code_text,1);
        $parm .= ']';
    }

    list($link,$extras) = array_pad(explode(" ",$parm), 2, null);

    if(!$parm) $link = $code_text;

    if($link == "external" && empty($extras))
    {
        $link = $code_text;
        $extras = "rel=external";
    }

    $extras = (string) $extras;
    if($extras == "external" || strpos($extras,"rel=external")!==FALSE)
    {
        $insert = "rel='external' ";
    }
    else
    {
        $insert = ($pref['links_new_window'] && strpos($link,"{e_")===FALSE && substr($link,0,1) != "#" && substr($link,0,1) != "/" && strpos($extras,"rel=internal")===FALSE) ? "rel='external' " : "";
    }
    if (strtolower(substr($link,0,11)) == 'javascript:') return '';
    return "<a class='{$class}' href='".e107::getParser() -> toAttribute($link)."' ".$insert.">".$code_text."</a>";