<?php
function logging() {
if (($_COOKIE[$pre."username"]) && ($_COOKIE[$pre."password"])) {
$updatea = sprintf("UPDATE ".$pre."users SET logged = '".time()."' WHERE username = '%s' AND password = '%s'",
mysql_real_escape_string($_COOKIE[$pre."username"]),
mysql_real_escape_string($_COOKIE[$pre."password"]));
mysql_query($updatea);
}
}

function phpa($string) {
$match_count = preg_match_all('#\[php\](.*?)\[\/php\]#si', $string, $matches);

for($i = 0; $i < $match_count; $i++) {
    $after_replace = $matches[1][$i];
    
    
    $after_replace = highlight_string(htmlspecialchars2(nl2br2($after_replace)), TRUE);
    $after_replace = str_replace('  ', '&nbsp; ', $after_replace);
    $after_replace = str_replace('  ', ' &nbsp;', $after_replace);
    $after_replace = str_replace("\t", '&nbsp; &nbsp;', $after_replace);
        
      return $after_replace;
    }
}
    function nl2br2($text) {
    return str_replace('<br />', '', $text);
}

function htmlspecialchars2($text) {
    static $patterns, $replaces;
    
    if(!$patterns) {
        $patterns = array('#&lt;#', '#&gt;#', '#&amp;#', '#&quot;#');
        $replaces = array('<', '>', '&', '"');
    }

    return preg_replace($patterns, $replaces, $text);
}

function bbcode($string) {
//$string = phpa($string);

$patterns = array(
'`\[b\](.+?)\[/b\]`is',
'`\[i\](.+?)\[/i\]`is',
'`\[u\](.+?)\[/u\]`is',
'`\[strike\](.+?)\[/strike\]`is',
'`\[color=#([0-9a-fA-F]{6})](.+?)[/color]`is',
'`\[email\](.+?)\[/email\]`is',
'`\[img\](.+?)\[/img\]`is',
'`\[url=([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\](.*?)\[/url\]`si',
'`\[url\]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]`si',
'`\[url\]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]`si',
'`\[flash=([0-9]+),([0-9]+)\](.+?)\[/flash\]`is',
'`\[quote\](.+?)\[/quote\]`is',
'`\[code\](.+?)\[/code\]`is',
'`\[indent](.+?)\[/indent\]`is',
'`\[code=([a-z]+)](.*?)[/code]`si',
'`\[size=([1-6]+)\](.+?)\[/size\]`is'
);

$replaces = array(
'<strong>\\1</strong>',
'<em>\\1</em>',
'<span style="border-bottom: 1px dotted">\\1</span>',
'<strike>\\1</strike>',
'<span style="color:#\1;">\2</span>',
'<a href="mailto:\1">\1</a>',
'<img src="\1" alt="" style="border:0px;" />',
'<a href="\1\2">\6</a>',
'<a href="\1\2">\1\2</a>',
'<a href="http://\1">\1</a>',
'<object width="\1" height="\2"><param name="movie" value="\3" /><embed src="\3" width="\1" height="\2"></embed></object>',
'<table cellspacing="0" cellpadding="3" border="0" align="center" width="90%"><tr><td class="quote1"><b>Quote:</b></td></tr><tr><td class="quote">\1</td></tr></table>',
'<table cellspacing="0" cellpadding="3" border="0" align="center" width="90%"><tr><td class="code1"><b>Code:</b></td></tr><tr><td class="code">\1</td></tr></table>',
'<pre>\\1</pre>',
'<ul><li class=”code”>\1<br/>\2</li></ul>',
'<h\1>\2</h\1>'                               
);

$prev_string = “”;
while ($prev_string != $string) {
  $prev_string = $string;
  $string = preg_replace($patterns, $replaces, $string);
}
return $string;
}

function simple_bbcode($t) {
$bb_codes = array(
    # bold
  '[b]' => '<span style="font-weight:bold">',
  '[/b]' => '</span>',
    # italic
  '[i]' => '<span style="font-style:italic">',
  '[/i]' => '</span>',
    # underline
  '[u]' => '<span style="text-decoration:underline">',
  '[/u]' => '</span>'
);

$search = array_keys($bb_codes);
$t = str_replace($search, $bb_codes, $t);
return $t;
}

function alinks($str) { // thanks to bza of phpfreaks.com forums
$str = preg_replace("/((f|ht)tp:\/\/[a-zæøå0-9~#%@\&:=?\/\.,_-]+[a-z0-9~#%@\&=?\+\/_-]+)/i", "<a href=\"\\1\">\\1</a>", $str); //http

$str = preg_replace("/([[:space:]a-z0-9{()\"'\[~#%@\&:=?\._-])(www.[a-zæøå0-9~#%@\&:=?\/\.,_-]+[a-z0-9~#%@\&=?\+\/_-]+)/i",
"\\1<a href=\"http://\\2\">\\2</a>", $str); // www.

$str = preg_replace("/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/i", "<a href=\"mailto:\\1\">\\1</a>", $str); // mail

return $str;
}

function rankcount($id) {
$count = "0";
$query = mysql_query("SELECT * FROM ".$pre."users ORDER BY `id` DESC");
while($p = mysql_fetch_array($query)) {

$idfind = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."profile WHERE username = '".$p[username]."'"));
$rank = mysql_fetch_row(mysql_query("SELECT points FROM ".$pre."ranks WHERE id = '".$id."'"));
$postcount = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."posts WHERE uid = '".$idfind[0]."'"));

if ($idfind[0]) {
if (($rank[0] < $postcount) or ($rank[0] == $postcount)) {
$count = $count + 1;
$rcount .= "$id|$idfind[0]|$count|$postcount<br>";
}
}
}
return $count;
}
?>