<?php 
/**
* 
*/
class Common
{
    if(!function_exists('is_php'))
    {
        function is_php($version = '5.0.0')
        {
            static $_is_php;
            $version = (string)$version;

            if(!isset($_is_php[$version]))
            {
                $_is_php[$version] = (version_compare(PHP_VERSION, $version));
            }

            return $_is_php[$version];
        }
    }

    function is_really_writable($file)
    {
        if(DIRECTORY_SEPARATOR == '/' AND @ini_get('safe_mode') == FALSE)
        {
            return is_writable($file);
        }

        if(is_dir($file))
        {
            $file = rtrim($file, '/').'/'.md5(mt_rand(1, 100).mt_rand(1, 100));
            //mt_rand更好的生成随机数
            if(($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
            {
                return FALSE;
            }
            fclose($fp);
            @chmod($file, DIR_WRITE_MODE);
            @unlink($file);
            return TRUE;
        }
        elseif(!is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
        {
            return FALSE;
        }
        fclose($fp);
        return TRUE;
    }

    function set_status_header($code = 200, $text = '')
    {
        $static = array(
            200 => 'OK',
            201 => 'Created',
            202 => 'Accepted',
            203 => 'Non-Authoritative Infomation',
            204 => 'No Content',
            205 => 'Reset Content',
            206 => 'Partial Content',

            300 => 'Multiple Choices',
            301 => 'Moved Permanently',
            302 => 'Found',
            305 => 'Use Proxy',
            307 => 'Temporary Redirect',

            400 => 'Bad Request',
            401 => 'Unauthorized',
            403 => 'Forbidden',
            404 => 'Not Fund',
            405 => 'Method Not Allowed',
            406    => 'Not Acceptable',
            407    => 'Proxy Authentication Required',
            408    => 'Request Timeout',
            409    => 'Conflict',
            410    => 'Gone',
            411    => 'Length Required',
            412    => 'Precondition Failed',
            413    => 'Request Entity Too Large',
            414    => 'Request-URI Too Long',
            415    => 'Unsupported Media Type',
            416    => 'Requested Range Not Satisfiable',
            417    => 'Expectation Failed',

            500    => 'Internal Server Error',
            501    => 'Not Implemented',
            502    => 'Bad Gateway',
            503    => 'Service Unavailable',
            504    => 'Gateway Timeout',
            505    => 'HTTP Version Not Supported'
            );
        if($code == '' OR !is_numeric($code))
        {

        }

        if(isset($static[$code]) AND $text == '')
        {
            $text = $static[$code];
        }

        $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL']:FALSE;

        if(substr(php_sapi_name(), 0, 3) == 'cgi')
        {
            header("Status: {$code} {$text}". TRUE);
        }
    }

    function remove_invisible_characters($str, $url_encoded = TRUE)
    {
        $non_displables = array();

        if($url_encoded)
        {
            $non_displables[] = '/%0[0-8bcef]/';
            $non_displables[] = '/%1[0-9a-f]/';
        }

        $non_displables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';

        do
        {
            $str = preg_replace($non_displables, '', $str, -1, $count);
        }
        while($count);

        return $str;
    }

    function html_escape($var)
    {
        if(is_array($var))
        {
            return array_map('html_escape', $var);
        }
        else
        {
            return htmlspecialchars($var, ENT_QUOTES, $carsets);
        }
    }

    function change_list_key_sort($key, array $arr, $order = 'asc')
    {
        $result = array();
        foreach( $arr as $item)
        {
            if(false == is_array($item) || false == array_key_exists($key, $item))
            {
                break;
                $result = array();
            }
            $result[$item[$key]] = $item;
        }

        $order == 'asc' ? ksort($result) : krsort($result);
        return $result;
    }
}