zoukankan      html  css  js  c++  java
  • common.php

    <?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-x08x0Bx0Cx0E-x1Fx7F]+/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;
        }
    }
  • 相关阅读:
    MongoDB Schema Design
    WinDBG中的poi是做什么用的?
    如何在Visual Studio中运行和调试汇编代码?
    [翻译图书] 未完工 Moving Applications to the Cloud on the Microsoft Windows Azure Platform 4
    在Word中生成随机的样本文本
    Quiz Win32内存表示与数值大小
    rep stos dword ptr es:[edi] 是做什么的?
    Windows Azure中虚拟机无法启动, 报错RoleStateUnknown的解决方案
    COM基础介绍
    64位的dump里如何寻找第一个到第四个参数?
  • 原文地址:https://www.cnblogs.com/echoshao/p/6511178.html
Copyright © 2011-2022 走看看