<?php
    function quotes($start, $finish, $string)
    {
        if ($string && $start && $finish)
        {
            while (stristr($string, $start) && stristr($string, $finish))
            {
                $pos_finish = stripos($string, $finish);
                $pos_start = strripos($string, substr($start, 0, $pos_finish));
                if ($pos_finish !== false && $pos_start !== false)
                {
                    $string = substr($string, 0, $pos_finish) . '</td></tr></table>' . substr($string, $pos_finish + strlen($finish));
                    $pos_start = strripos($string, substr($start, 0, $pos_finish));
                    $string = substr($string, 0, $pos_start) . '<table width="100%" border="1" cellpadding="4" cellspacing="0" style="background: #; border-collapse: collapse" bordercolor="#"><tr><td>' . substr($string, $pos_start + strlen($start));
                }
                else
                {
                    break;
                }
            }
        }
        $string = str_ireplace($start, '', $string);
        $string = str_ireplace($finish, '', $string);
        return $string;
    }

    function replace_embeded_tags($str, $start_tag = '[quote]', $end_tag = '[/quote]', $start_replacement = '</div>', $end_replacement = '<div style="border: 1px solid; padding: 1em; -moz-border-radius: 7px;">')
    {
        $start_tag_length = strlen($start_tag);
        $end_tag_length = strlen($end_tag);

        $offset = 0;
        $continue = true;
        
        while (true)
        {        
            $start_tag_loc     = stripos($str, $start_tag, $offset);
            $end_tag_loc     = stripos($str, $end_tag, $start_tag_loc);

            if ($start_tag_loc === false || $end_tag_loc === false)
                break;
            
            //check if there's an open tag between this open and the first found close
            $second_start_tag_loc = stripos($str, $start_tag, $start_tag_loc + 1);
            if ($second_start_tag_loc !== false && $second_start_tag_loc < $end_tag_loc)
            {
                $offset = $second_start_tag_loc;
                continue;
            }
                
            $str = substr($str, 0, $end_tag_loc) . $start_replacement . substr($str, $end_tag_loc + $end_tag_length);
            $str = substr($str, 0, $start_tag_loc) . $end_replacement . substr($str, $start_tag_loc + $start_tag_length);
            
            $offset = 0;
        }
        
        $str = str_ireplace($start_tag, '', $str);
        $str = str_ireplace($end_tag, '', $str);
        
        return $str;
    }     
    
    function smileys($str)
    {
        $smileys = mysqli_query_logged("SELECT name, filename FROM community_smileys");
        while ($smiley = mysqli_fetch_array($smileys))
        {
            $str = str_ireplace($smiley['name'], '<img src="images/smileys/' . $smiley['filename'] . '" valign="middle" border="0" alt="" />', $str);
        }
        return $str;
    }    
    
    function images_code($b)
    {
        $i = 0;
        $b = ' ' . $b;
        while ($start = strpos($b, '[', $i))
        {
            if ($end = strpos($b, ']', $start))
            {
                $id = substr($b, $start + 1, $end - $start - 1);
                if (is_numeric($id))
                {
                    if ($id > 25)
                    {
                        $images = mysqli_query_logged('SELECT width, height FROM images WHERE id="' . $id . '"');
                        if ($images_row = mysqli_fetch_array($images))
                        {
                            $b = str_replace('[' . $id . ']', '<a href="?s=gallery_image&i=' . $id . '"><img src="?g=full&i=' . $id . '" width="' . $images_row['width'] . '" height="' . $images_row['height'] . '" alt="Image #' . $id . '" border="1" /></a>', $b);
                        }
                    }
                }
            }
            $i = $start + 1;
        }
        return $b;        
    }
    
    function bbcodes($body)
    {
        $body = str_ireplace("[b]","<b>", $body);
        $body = str_ireplace("[/b]","</b>", $body);
        $body = str_ireplace("[i]","<i>", $body);
        $body = str_ireplace("[/i]","</i>", $body);
        $body = str_ireplace("[u]","<u>", $body);
        $body = str_ireplace("[/u]","</u>", $body);
        $body = str_ireplace("[img]http://","[img]", $body);
        $body = str_ireplace("[video]http://","[video]", $body);
        $body = str_ireplace("[video]www.youtube.com/watch?v=", "[video]www.youtube.com/v/", $body);
        $body = str_ireplace('&amp;autoplay=1', '', $body);
        $body = str_ireplace("[url=http://","[url=", $body);
        $body = str_ireplace("[link=http://","[link=", $body);
        $body = str_ireplace("[color=#","[color=", $body);
        $body = preg_replace("/\[link\](.*?)\[\/link\]/si", "\\1", $body); 
        $body = preg_replace("/\[LINK\](.*?)\[\/LINK\]/si", "\\1", $body); 
        $body = str_ireplace("style=","", $body);
        $body = addlinks($body);
        //$body = quotes('[QUOTE]', '[/QUOTE]', $body);
        $body = replace_embeded_tags($body);
        $body = preg_replace("/\[code\](.*?)\[\/code\]/si", "<pre>\\1</pre>", $body); 
        $body = preg_replace("/\[CODE\](.*?)\[\/CODE\]/si", "<pre>\\1</pre>", $body); 
        $body = preg_replace("/\[img\](.*?)\[\/img\]/si", "<img src=\"http://\\1\" border=\"0\">", $body); 
        $body = preg_replace("/\[IMG\](.*?)\[\/IMG\]/si", "<img src=\"http://\\1\" border=\"0\">", $body); 
        $body = preg_replace("/\[VIDEO\](.*?)\[\/VIDEO\]/si", "<object width=\"425\" height=\"350\"><param name=\"movie\" value=\"http://\\1\"></param><embed src=\"http://\\1\" type=\"application/x-shockwave-flash\" width=\"425\" height=\"350\"></embed></object>", $body); 
        $body = preg_replace("/\[video\](.*?)\[\/video\]/si", "<object width=\"425\" height=\"350\"><param name=\"movie\" value=\"http://\\1\"></param><embed src=\"http://\\1\" type=\"application/x-shockwave-flash\" width=\"425\" height=\"350\"></embed></object>", $body); 
        $body = preg_replace("/\[url=(.*)](.*)\[\/url\]/si", "<a href=\"http://\\1\">\\2</a>", $body);
        $body = preg_replace("/\[link=(.*)](.*)\[\/link\]/si", "<a href=\"http://\\1\">\\2</a>", $body);
        $body = preg_replace("/\[color=(.*)](.*)\[\/color\]/si", "", $body);
        $body = preg_replace("/\[size=(.*)](.*)\[\/size\]/si", "", $body);
        $body = preg_replace("/\[font=(.*)](.*)\[\/font\]/si", "", $body);
        $body = str_ireplace('[url]', '', $body);
        $body = str_ireplace('[/url]', '', $body);
        $body = str_ireplace('[link]', '', $body);
        $body = str_ireplace('[/link]', '', $body);
        $body = str_ireplace('[email]', '', $body);
        $body = str_ireplace('[/email]', '', $body);
        return trim($body);
    }
    
    function sitechange($body)
    {
        $sites = array('ravewave.com', 'rave.ca', 'raves.ca', 'afterhour.ca', 'clubkid.ca', 'clubber.ca');
        foreach ($sites AS $value)
        {
            $body = str_ireplace('.' . $value, '.' . str_ireplace('www.', '', $_SERVER['HTTP_HOST']), $body);
            $body = str_ireplace('/' . $value, '/' . str_ireplace('www.', '', $_SERVER['HTTP_HOST']), $body);
        }
        return $body;
    }

    function addlinks($string)
    {
        //$string = eregi_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]@/]", "<a href=\"\\0\">\\0</a>", $string);
        //$string = eregi_replace ('[_a-zA-z0-9\-]+(\.[_a-zA-z0-9\-]+)*\@' . '[_a-zA-z0-9\-]+(\.[a-zA-z]{1,4})+', '<a href="mailto:\\0">\\0</a>', $string);
        $string = make_clickable_rep($string);
        return $string;
    }

?>