<?php

function img_preg($arr) {
    if (IS_WEB) {
        return '<a href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?img_url=' . urlencode(html_entity_decode($arr[1])) . '"><img src="http://' . $_SERVER['HTTP_HOST'] . '/go.php?img_url=' . urlencode(html_entity_decode($arr[1])) . '" alt="img" style="max-width: 500px;"/></a>';
    } else {
        return '<a href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?img_url=' . urlencode(html_entity_decode($arr[1])) . '"><img src="http://' . $_SERVER['HTTP_HOST'] . '/go.php?img_url=' . urlencode(html_entity_decode($arr[1])) . '" alt="img" style="max-width: 200px;"/></a>';
    }
}

function links_preg1($arr) {
    global $set;

    if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#', $arr[1]) || !preg_match('#://#', $arr[1])) {
        return '<a href="' . $arr[1] . '">' . $arr[2] . '</a>';
    } else {
        return '<a' . (IS_WEB ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?ext_url=' . urlencode(html_entity_decode($arr[1])) . '">' . $arr[2] . '</a>';
    }
}

function links_preg2($arr) {
    global $set;
    if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#', $arr[2])) {
        return $arr[1] . '<a href="' . $arr[2] . '">' . $arr[2] . '</a>' . $arr[4];
    } else {
        return $arr[1] . '<a' . (IS_WEB ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?ext_url=' . urlencode(html_entity_decode($arr[2])) . '">Ссылка</a>' . $arr[4];
    }
}

function links($msg) {
    global $set;
    if ($set['bb_img']) {
        $msg = preg_replace_callback('/\[img\](.+)\[\/img\]/isU', 'img_preg', $msg);
    }
    if ($set['bb_url']) {
        $msg = preg_replace_callback('/\[url=(.+)\](.+)\[\/url\]/isU', 'links_preg1', $msg);
    }
    if ($set['bb_http']) {
        $msg = preg_replace_callback('~(^|\s)([a-z]+://([^ \r\n\t`\'"]+))(\s|$)~iu', 'links_preg2', $msg);
    }
    return $msg;
}
