<?php
    function cookie($name, $default = null)    {
        if (!isset($_COOKIE[$name]) || is_array($_COOKIE[$name]))    {
            return $default;
        }
        return $_COOKIE[$name];
    }

    function files($name, $default = null) {
        if (!isset($_FILES[$name]))    {
            return $default;
        }
        return $_FILES[$name];
    }

    function get($name, $default = null) {
        if (!isset($_GET[$name]) || is_array($_GET[$name]))    {
            return $default;
        }
        return $_GET[$name];
    }

    function post($name, $default = null)    {
        if (!isset($_POST[$name]) || is_array($_POST[$name]))    {
            return $default;
        }
        return $_POST[$name];
    }
    
    function server($name, $default = null)    {
        if (!isset($_SERVER[$name]) || is_array($_SERVER[$name]))    {
            return $default;
        }
        return $_SERVER[$name];
    }

    function nice_date($datetime, $params = 'D M j, Y @ g:ia', $seconds = 0)
    {
        return date($params, mktime(intval(substr($datetime, 11, 2)), intval(substr($datetime, 14, 2)), intval(substr($datetime, 17, 2)) + $seconds, intval(substr($datetime, 5, 2)), intval(substr($datetime, 8, 2)), intval(substr($datetime, 0, 4))));
    }
    
    function get_username($id, $status = 0)
    {
        $status_code = '<span style="font-weight: bold;">&raquo;</span> ';
        $members = mysqli_query_logged("SELECT * FROM members WHERE id = '" . $id . "'");
        if ($members_row = mysqli_fetch_assoc($members))
        {
            if ($status)
            {
                $members_laston = mysqli_query_logged("SELECT * FROM members_laston WHERE user_id = '" . $id . "'");
                if ($members_laston_row = mysqli_fetch_assoc($members_laston))
                {
                    $seconds = (strtotime(date('Y-m-d H:i:s')) - strtotime($members_laston_row['laston']));
                    if ($seconds < 300)
                    {
                        $status_code = '<span class="notice_good" style="font-weight: bold;">&raquo;</span> ';
                    }
                    elseif ($seconds < 600)
                    {
                        $status_code = '<span class="notice_attention" style="font-weight: bold;">&raquo;</span> ';
                    }
                    elseif ($seconds < 900)
                    {
                        $status_code = '<span class="notice_error" style="font-weight: bold;">&raquo;</span> ';
                    }
                }
            }
            return ($status ? $status_code . '<a href="?s=profile&u=' . $members_row['username'] . '">' : '') . capitalize($members_row['username']) . ($status ? '</a>' : '');
        }
        else
        {
            return ($status ? $status_code : '') . 'System';
        }
    }
    
    function capitalize($name)
    {
        $name = strtolower($name);
        $name = ucwords($name);
        $name = join("'", array_map('ucwords', explode("'", $name)));
        $name = join("-", array_map('ucwords', explode("-", $name)));
        $name = join("_", array_map('ucwords', explode("_", $name)));
        $name = join("Mac", array_map('ucwords', explode("Mac", $name)));
        $name = join("Mc", array_map('ucwords', explode("Mc", $name)));
        return $name;
    }
    function get_user_id($user)
    {
        $members = mysqli_query_logged("SELECT * FROM members WHERE username = '" . $user . "'");
        if ($members_row = mysqli_fetch_assoc($members))
        {
            return $members_row['id'];
        }
        else
        {
            return 0;
        }
    }
    
    function get_main_image_id($id)
    {
        $members = mysqli_query_logged("SELECT * FROM members WHERE id = '" . $id . "'");
        if ($members_row = mysqli_fetch_assoc($members))
        {
            return $members_row['main_image_id'];
        }
        else
        {
            return 0;
        }
    }
    
    function make_cache_code() 
    {
        $p = '';
        for($i = 1; $i <= 18; $i++) 
        {
            $p .= chr(rand(48, 57));
        }
        return $p;
    }
    
    function has_permission($user_id, $permission)
    {
        if ($perm = mysqli_fetch_assoc(mysqli_query_logged("SELECT ip FROM permissions WHERE user_id = '" . $user_id . "' AND permission='" . $permission . "'")))
        {
            if ($perm['ip'] == '' || $perm['ip'] == $_SERVER['REMOTE_ADDR'])
            {
                return true;
            }
        }
        return false;
    }

    function make_cookie($name, $value, $time = 365)
    {
        if ($time)
        {
            $newtime = time() + 60 * 60 * 24 * $time;
        }
        else
        {
            $newtime = 0;
        }
        setcookie($name, $value, $newtime, '/', str_replace('www', '', strtolower($_SERVER['HTTP_HOST'])));    
    }
    
    function kill_cookie($name)
    {
        setcookie($name, '', time() - 3600, '/', str_replace('www', '', strtolower($_SERVER['HTTP_HOST'])));
    }
    
    function sq($value)
    {
        if (!is_numeric($value))
        {
            $value = "'" . mysqli_real_escape_string($GLOBALS['mysqli'], $value ?: '') . "'";
        }
        return $value;
    }
    
    function rq($value)
    {
        return $value;
    }
    
    function language()
    {
        if (isset($GLOBALS['auth']['language']))
        {
            if ($GLOBALS['auth']['language'] == 'english' || $GLOBALS['auth']['language'] == 'french')
            {
                return $GLOBALS['auth']['language'];
            }
        }
        $language = (isset($_COOKIE['language']) && ($_COOKIE['language'] == 'english' || $_COOKIE['language'] == 'french')) ? $_COOKIE['language'] : 'english';
        return $language;
    }
    
    function make_clickable_rep($str, $bold = 1)
    {
        $str = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1[url]\\2[/url]", $str);
        $str = preg_replace("#(^|[\n ])((www)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1[url]http://\\2[/url]", $str);
        if ($bold)
        {
            $str = preg_replace("/\[url\]((http|https|ftp|mailto):\/\/([a-z0-9\.\-@:]+)[a-z0-9;\/\?:@=\&\$\-_\.\+!*'\(\),\#%~ ]*?)\[\/url\]/is", "[<b><a href=\"$1\">$3</a></b>]", $str);
        }
        else
        {
            $str = preg_replace("/\[url\]((http|https|ftp|mailto):\/\/([a-z0-9\.\-@:]+)[a-z0-9;\/\?:@=\&\$\-_\.\+!*'\(\),\#%~ ]*?)\[\/url\]/is", "<a href=\"$1\">$3</a>", $str);
        }
        return $str;
    }

    function block($title, $information, $width = '', $top = 0, $padding = '', $cool_block = '')
    {
        if ($width == '')
        {
            $width = 150;
        }
        if ($padding == '')
        {
            $padding = '5px';
        }
        $return = '';
        $return .= "\r\n\r\n";
        if ($cool_block)
        {
            $return .= box_inside_top();
        }
        $return .= '<div style="padding: ' . $padding . ';">';
        $return .= '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>';
        if ($title != 'none')
        {
            $return .= '<td width="' . $width . '" align="right"' . ($top ? ' valign="top"' : '') . ' nowrap style="padding-right: 3px;">' . ($title ? $title . (strstr($title, '>') === false ? ':' : '') : '') . '</td>';
        }
        $return .= '<td valign="top">' . $information . '</td>';
        $return .= '</tr></table>';
        $return .= '</div>' ;
        if ($cool_block)
        {
            $return .= box_inside_bottom();
        }
        $return .= "\r\n\r\n";
        return $return;
    }

    function script_time($format = 'Y-m-d H:i:s')
    {
        return date($format, $_SERVER['REQUEST_TIME']);
    }

    function remove_hacks($text)
    {
        $text = str_ireplace('<script', '', $text);
        $text = str_ireplace('<javascript', '', $text);
        $text = str_ireplace('.innerhtml', '', $text);
        $text = str_ireplace('document.write', '', $text);
        return $text;
    }
?>