Some commonly used functions accumulated php

This link 
? <PHP 

// the above mentioned id: ecffe70d3af54df9bad97b61918ace7d 
, Ltd. Free Join $ ct_path, $ ct_log_path; 
$ log_path = "test_php.txt"; // whether to log into the buffer, and then written to the file through a one-time CT_flush ()
 $ ct_log_buffer = to true; 
$ CT_off = to true; 
$ request_num = of uniqid (); 
$ CT_format = ""; 
IF ($ ct_path) { 
    $ the dir = dirname ($ ct_path); 
    (! IF the file_exists ($ the dir)) {
         mkdir ($ the dir, 0777, to true ); 
    } 
    $ File = the fopen ($ ct_path, "A +"); 
    $ file_ct_log = $ File; 
} 
IF ($ ct_log_path) { 
    $ the dir = dirname

($ct_log_path);
    if (!file_exists($dir)) {
        mkdir($dir, 0777, true);
    }
    $file_ct_log = fopen($ct_log_path, "a+");
}

$ct_buffer = [];

$path_my = __DIR__ . "/common.my.php";
if (is_file($path_my)) {
    require $path_my;
}

function clear_log() {
   Global $log_path;
   unlink($log_path);
}

function clog($content, $with_lf = true) {
   Global $log_path;
   $file = fopen($log_path,"a+");
   fwrite($file, $content);
   CT_log($content);
   if($with_lf) {
      fwrite($file, "\n");
      CT_log("\n");
   }
   fclose($file);

}

function get_safe($obj, $key, $def = NULL) {
   if(isset($obj[$key])) {
      return $obj[$key];
   }
   return $def;
}

function get_stack_trace($title = "") {
   $html = "=================stack trace:".$title."\n";
   $array =debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
   foreach($array as $row) {
      $html .= sprintf("file:%s, line:%d, class:%s, function:%s\n",
         get_safe($row, 'file'), get_safe($row, 'line'), get_safe($row, 'class'), get_safe($row, 'function'));
   }
   return $html;
}

function log_stack_trace($title = "") {
   clog(get_stack_trace($title));
}

// error handler function with stack trace.
// use like this:
// $old_error_handler = set_error_handler("err_handler");
function err_handler($errno, $errstr, $errfile, $errline)
{
   $errno_map = array(1 => "E_ERROR", 2 => "E_WARNING", 4 => "E_PARSE", 8 => "E_NOTICE",
      16 => "E_CORE_ERROR", 32 => "E_CORE_WARNING", 64 => "E_COMPILE_ERROR",
      128 => "E_COMPILE_WARNING", 256 => "E_USER_ERROR", 512 => "E_USER_WARNING", 
      1024 => "E_USER_NOTICE", 2048 => "E_STRICT", 4096 => "E_RECOVERABLE_ERROR",
      8192 => "E_DEPRECATED", 16384 => "E_USER_DEPRECATED", 32767 => "E_ALL");
   clog(sprintf("------------ %s(%d), msg:%s", $errno_map[$errno], $errno, $errstr));
   log_stack_trace("");
   /* Don't execute PHP internal error handler */
   return true;
}

// 获取当前系统时间,返回float格式,单位:秒
function get_time() {
   date_default_timezone_set('Asia/Shanghai');
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

function get_prefix() {
    return "";
    Global $ip, $pid;
    if(!isset($ip)) {
        $pid = getmypid();
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $pid.' '.$ip.' '.date("m-d H:i:s ");
}

function CT($content) {
    Global $CT_off, $file;
    if($CT_off || !$file)
        return;

    Global $last_time, $first_time, $is_first, $ct_log_buffer, $ct_buffer, $request_num, $CT_format;
    if ($CT_format == "raw") {
        $all_out = $content . "\n";
    } else {
        // 通过stack trace计算缩进
        $array =debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
        $ignore_count = 0;
        $count = count($array);
        $ignore_names = ["call_user_func_array", "call_user_func", "spl_autoload_call"];
        $ignore_classes = ["ReflectionClass"];
        for ($i = 2; $i < $count; $i++) {
            $frame = $array[$i];
            if (in_array($frame["function"], $ignore_names) || isset($frame["class"]) && in_array($frame["class"], $ignore_classes)) {
                $ignore_count++;
            }
        }
        $all_out = get_prefix() . str_pad("", $count - 2 - $ignore_count, " ") . $request_num . " " . $content;
        $cur_time=get_time();
        if(!$is_first) {
            $is_first = true;
            $last_time = $first_time = $cur_time;
        }
        $total_time=$cur_time-$first_time;
        $delta_time=$cur_time-$last_time;
        $overtime_flag = "";if ($ delta_time * 1000> 10// add timeout mark
        
        
            overtime_flag = $ "---- Overtime"; 

        $ $ all_out all_out = "CUR_TIME: $ CUR_TIME, TOTAL_TIME: $ TOTAL_TIME, delta_time: $ $ delta_time overtime_flag \ n-";. 
        $ $ = Last_time CUR_TIME; 
    } 

    IF (= $ ct_log_buffer to true ==) { 
        $ ct_buffer [] = $ all_out; 
    } the else { fwrite (file $, $ all_out); 
    } 
} / ** 
 * CT the buffer contents written to the file 
 * @param Boolean turn_off_buffer, is closed after the completion buffer to ensure that the output can be called by a function like register_shutdown_function 
 * / function CT_flush ($ turn_off_buffer) 
{ 
    Global File $, $ ct_buffer, $ ct_log_buffer; 
    IF ($ File!) { 
        return; 
    }
        



     fwrite ($ File, the Join ( "", $ ct_buffer)); 
    $ ct_buffer = []; 
    ! $ ct_log_buffer = $ turn_off_buffer; 
} 

function CT_log ($ Content = "", $ path = NULL) { 
    Free Join $ file_ct_log; 
    IF (! file_ct_log path && $ $) {! 
        return; 
    } 
    $ get_prefix Content = (). print_r ($ Content, to true) "\ n-";. 
    IF ($ path) {
        file_create_path($path);
        $file = fopen($path, "a+");
        if($file) {
            fwrite($file, $content);
            fclose($file);
        }
    }
    else {
        fwrite($file_ct_log, $content);
    }
}

/**
 * 日志输出,使用info level
 * @param $content
 * @param array $params
 * @param string $logger
 */
function slog($content, $params = [], $logger = "default")
{
    SeasLog::info($content, $params, $logger);
}

/**
 * Log output, using Level Debug 
 * @param $ Content 
 * @param Array the params $ 
 * @param String $ Logger 
 * /
function slog_debug($content, $params = [], $logger = "default")
{ 
    SeasLog :: Debug ($ Content, the params $, $ Logger); 
} / ** 
 * log output, using Level error 
 * @param $ Content 
 * @param Array the params $ 
 * @param String $ Logger 
 * / function slog_error (Content $, $ the params = [], Logger $ = "default") 
{ 
    SeasLog :: error (Content $, $ the params, $ Logger); 
} / ** 
 * log output, using Level warning 
 *




 @param $ Content 
 * @param Array params $ 
 * @param String $ Logger 
 * /
function slog_warning($content, $params = [], $logger = "default")
{
    SeasLog::warning($content, $params, $logger);
}

/*
    获取指定的http response header 值。
    eg:
    HTTP/1.1 200 OK
    Server: Tengine/2.1.2
    Date: Sun, 02 Apr 2017 02:49:34 GMT
    Content-Type: text/html; charset=gb2312
    Content-Length: 124378
    Connection: keep-alive
    Cache-Control: private
    X-AspNetMvc-Version: 4.0
    X-AspNet-Version: 4.0.30319
*/
function curl_get_header($ch, $response, $key)
{
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    // header processing
    $header_arr = explode("\r\n", $header);

    $value = "";
    $key .= ":";
    foreach ($header_arr as $entry) {
        if (!strncmp($entry, $key, strlen($key))) {
            $value = trim(substr($entry, strlen($key)));
            break;
        }
    }
    return $value;
}

// 获取response状态码
function curl_get_status($ch, $response)
{
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    $header_arr = explode("\r\n", $header);
    return explode(" ", $header_arr[0])[1];
}

function startsWith($haystack, $needle)
{
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

IS the Handled HTTP 301 //.
 Function http_get_core (URL $, $ & Status = null) 
{ 
    CT_log ( "----------- the http_get:." $ URL);
    $ch = curl_init (); 
    $ Status = -1; // 3 nested up to 301 times.
    for ($ I = 0; $ I <. 3; $ I ++) { 
        $ = Options Array ( CURLOPT_HEADER =>. 1,
             CURLOPT_POST => 0,             // request mode is the POST
             CURLOPT_URL to => $ URL,       // request the URL
             CURLOPT_RETURNTRANSFER => 1,   // Get request result
             CURLOPT_TIMEOUT_MS => 30000,     // timeout (MS)
             CURLOPT_POSTFIELDS => http_build_query (Array ()), // injection interface parameters
             CURLOPT_SSL_VERIFYPEER => 0,   // no verification certificate
         );
        

    
            curl_setopt_array ($ CH, $ Options);
         curl_setopt ($ CH, CURLOPT_ENCODING , "gzip, the deflate"); // Baidu does not support 
        curl_setopt ($ CH, CURLOPT_CUSTOMREQUEST , 'GET'); 

        IF ((the Response = $ curl_exec ($ CH ))) {
             // some websites header, <head> coding inconsistencies specified will result in garbled. So if there is coded information, transfers it to the client.
            curl_get_header the content_type = $ ($ CH, $ Response, "the Type-the Content"); 
            IF (the content_type $) { header ( "the Type-the Content:" $ the content_type.); 
            } 
            $ header_size = curl_getinfo ($ CH, CURLINFO_HEADER_SIZE );
                

            $status = $code = curl_get_status($ch, $response);
            if ($code == 301 || $code == 302) {
                $redirect_url = curl_get_header($ch, $response, "Location");
                $parsed_re = parse_url($redirect_url);
                if(isset($parsed_re["host"])) {
                    $url = $redirect_url;
                }
                else {
                    $parsed = parse_url($url);
                    if(startsWith($redirect_url, "/")) {
                        $url = $parsed["schema"]. "://" . $parsed["host"] . $redirect_url;
                    }
                    else {
                        // TODO 相对路径拼接
                        $content =  "relative path TODO";
                        curl_close($ch);
                        return $content;
                    }
                }
                continue;
            } else if ($code == 404) {
                header("Status: 404 Not Found");
                $msg = array(
                    "status" => 404
                );
                $content = "<p id='http_util_message_block' style='display: none'>" . json_encode($msg) . "</p>";
                $content .= "404, not found!";
            } else if ($code == 200) {
                $content = substr($response, $header_size);
            } else {
                $content = "http error, code=" . $code . "\n" . substr($response, $header_size);
            }
        } else {
            $msg = array(
                "status" => -1
            );
            $content = "<p id='http_util_message_block' style='display: none'>" . json_encode($msg) . "</p>";
            $content .= "invoke error[" . curl_error($ch) . "]";
        }curl_close($ch);($ path);dirname
    $ the dir =
{function file_create_path ($ path)// recursively create the necessary path for the path

}
    }
        
        return $content;


    if ($dir && !file_exists($dir)) {
        mkdir($dir, 0755, true);
    }
}

// $save_path 需要gbk编码
function file_save($content, $save_path, $append)
{
    file_create_path($save_path);
    if ($append) {
        $file = fopen($save_path, "a+");
        if ($file) {
            fwrite($file, $content);
            fclose($file);
        }
    } else {
        file_put_contents(the save_path $, $ Content); 
    } 
} // default encapsulation curl
 function curl_do (URL $, $ = close_after_use to true) 
{


    CT_log ( "curl_do:." $ Url); 
    $ CH = curl_init (); 

    $ Options = Array ( CURLOPT_HEADER => 0,
         CURLOPT_POST => 0,             // request method is POST
         CURLOPT_URL => $ url,       // request the URL of
         CURLOPT_RETURNTRANSFER => 1,   // Get request result
         CURLOPT_TIMEOUT_MS => 30000,     // timeout (MS)
         CURLOPT_POSTFIELDS => http_build_query (Array ()), // injection interface parameters
         ( $ ch, $ options);
    
        CURLOPT_SSL_VERIFYPEER => 0,  // 不验证证书
    );curl_setopt_arraycurl_setopt($ch, CURLOPT_ENCODING, "gzip");
    $response = curl_exec($ch);
    $err = curl_error($ch);
    if ($err) {
        CT_log("curl error: " . $err);
    }
    if ($close_after_use) {curl_close($ch);
    }
    return array("handle" => $ch, "response" => $response, "err" => $err);@param
 *$ URL@param
 *
 * detailed definition see unit test
 * URL stitching, no processing user, pass two Components/ **
}

    
        

 $ Base 
 * @return String 
 * /
function get_absolute_url($url, $base)
{
    // 两种情况直接返回$url:
    if (!$base)
        return $url;
    $url_host = parse_url($url, PHP_URL_HOST);
    if ($url_host) {
        return $url;
    }

    $base_parsed = parse_url($base);
    $base_scheme = get_safe($base_parsed, "scheme", "");
    $base_host = get_safe($base_parsed, "host", "");
    $base_port = isset($base_parsed["port"]) ? ":" . $base_parsed["port"] : "";
    $base_path = get_safe($base_parsed, "path");

    if ($base_host) {
        $base_calc = $base_scheme . "://" . $base_host . $base_port;
        if (startsWith($url, "/")) {
            return $base_calc . $url;
        } else if ($base_path) {
            $pos = strrpos($base_path, "/");
            if ($pos !== false) {
                $dir = substr($base_path, 0, $pos + 1); // with last "/"
                return $base_calc . $dir . $url;
            } else {
                return $base_calc . "/" . $url;
            }
        } else {
            return $base_calc . "/" . $url;
        }
    } else {
        if (startsWith($url, "/")) {
            return $url;
        } else if ($base_path) {
            $pos = strrpos($base_path, "/");
            if ($pos !== false) {
                $dir = substr($base_path, 0, $pos + 1); // with last "/"
                return $dir . $url;
            } else {
                return $url;
            }
        } else {
            return $url;
        }
    }
}

function get_absolute_url_tests()
{
    // empty tests
    $tests[] = [null, null, null];
    $tests[] = ["", null, ""];
    $tests[] = [null, "", null];
    $tests[] = ["", "", ""];

    $tests[] = [null, "/", "/"];
    $tests[] = [null, "/a", "/"];
    $tests[] = [null, "/a/", "/a/"];

    $tests[] = ["a.html", "b", "a.html"];
    $tests[] = ["a.html", "", "a.html"];
    $tests[] = ["a.html", "/", "/a.html"];
    $tests[] = ["a/b/c/a.html", "http://1.1.1.1", "http://1.1.1.1/a/b/c/a.html"];
    $tests[] = ["a.html", "http://1.1.1.1:83", "http://1.1.1.1:83/a.html"];
    $tests[] = ["a.html", "http://1.1.1.1:83/", "http://1.1.1.1:83/a.html"];
    $tests[] = ["a.html", "http://1.1.1.1:83/a/b", "http://1.1.1.1:83/a/a.html"];
    $tests[] = ["a.html", "http://1.1.1.1:83/?", "http://1.1.1.1:83/a.html"];
    $tests[] = ["a.html", "http://1.1.1.1:83?", "http://1.1.1.1:83/a.html"];
    $tests[] = ["a.html", "http://1.1.1.1:83?a=b", "http://1.1.1.1:83/a.html"];
    $tests[] = ["a.html", "https://1.1.1.1:83?a=b#1", "https://1.1.1.1:83/a.html"];
    $tests[] = ["a.html", "www.baidu.com?a=b#1", "a.html"]; // www被认为是path

    // starts with "/"
    $tests[] = ["/a.html", "b", "/a.html"];
    $tests[] = ["/a.html", "", "/a.html"];
    $tests[] = ["/a.html", "/", "/a.html"];
    $tests[] = ["/a/b/c/a.html", "http://1.1.1.1", "http://1.1.1.1/a/b/c/a.html"];
    $tests[] = ["/a.html", "http://1.1.1.1:83", "http://1.1.1.1:83/a.html"];
    $tests[] = ["/a.html", "http://1.1.1.1:83/", "http://1.1.1.1:83/a.html"];
    $tests[] = ["/a.html", "http://1.1.1.1:83/a/b", "http://1.1.1.1:83/a.html"];
    $tests[] = ["/a.html", "http://1.1.1.1:83/?", "http://1.1.1.1:83/a.html"];
    $tests[] = ["/a.html", "http://1.1.1.1:83?", "http://1.1.1.1:83/a.html"];
    $tests[] = ["/a.html", "http://1.1.1.1:83?a=b", "http://1.1.1.1:83/a.html"];
    $tests[] = ["/a.html", "https://1.1.1.1:83?a=b#1", "https://1.1.1.1:83/a.html"];
    $tests[] = ["/a.html", "www.baidu.com?a=b#1", "/a.html"]; // www被认为是path

    $r = true;
    foreach ($tests as $test) {
        $abs = get_absolute_url($test[0], $test[1]);
        echo $abs . "\n";
        if ($abs !== $test[2]) {
            $r = false;
            break;
        }
    }
    echo "pass: " . $r . "\n";

    // return self tests
    $tests_self = [];
    $tests_self[] = ["http://test.com/a.html", "b", ""];
    $tests_self[] = ["http://test.com/a.html", "", ""];
    $tests_self[] = ["http://test.com/a.html", "/", ""];
    $tests_self[] = ["http://test.com/a/b/c/a.html", "http://1.1.1.1", ""];
    $tests_self[] = ["http://test.com/a.html", "http://1.1.1.1:83", ""];
    $tests_self[] = ["http://test.com/a.html", "http://1.1.1.1:83/", ""];
    $tests_self[] = ["http://test.com/a.html", "http://1.1.1.1:83/a/b", ""];
    $tests_self[] = ["http://test.com/a.html", "http://1.1.1.1:83/?", ""];
    $tests_self[] = ["http://test.com/a.html", "http://1.1.1.1:83?", ""];
    $tests_self[] = ["http://test.com/a.html", "http://1.1.1.1:83?a=b", ""];
    $tests_self[] = ["http://test.com/a.html", "https://1.1.1.1:83?a=b#1", ""];
    $tests_self[] = ["http://test.com/a.html", "www.baidu.com?a=b#1", ""]; // www被认为是path

    echo "-------------------return self test--------:\n";
    $r = true;
    foreach ($tests_self as $test) {
        $abs = get_absolute_url($test[0], $test[1]);
        echo $abs . "\n";
        if ($abs !== $test[0]) {
            = R & lt to false $; 
            BREAK; 
        } 
    } 
    echo "Pass:.". R & lt $ "\ n-"; 
} / ** 
 * path normalization. 
 EG *: 
 * B = A /../> B 
 * = ../a/../../b> ../b 
 * ./a/../b => ./b 
 * @param $ path 
 * @return Mixed | String 
 * / function normalize_path ($ path) 
    IF (! $ path) { 
        return $ path; 
    } 
    $ path = str_replace ( "\\", "/", $ path); 
    $ dotdotCount = 0; // to ".." at the beginning of the process
     $ ARR = the explode ( "/", $ path);



{

    the foreach (ARR AS $ $ ELE) { 
        IF {(ELE == $ ".."!) the array_push (pathStack $, $ ELE); 
        } {the else 
            IF ( COUNT ($ pathStack) === 0) { 
                $ dotdotCount ++; 
            the else {} array_pop ($ pathStack); 
            } 
        } 
    } 
    the unset ($ ELE); 
    $ = R & lt str_pad
            
                
("", $dotdotCount, "../");
    the foreach ($ $ pathStack AS path) { 
        IF ($ R & lt === "") { 
            $ $ path = R & lt; 
        } {the else 
            . R & lt $ =. "/" $ path; 
        } 
    } 
    return $ R & lt; 
} // try gbk, utf-8 two coding; try incoming priority encoding
 function is_file_ex ($ path) 
{ 
    IF ( is_file ($ path)) { 
        return to true; 
    } 
    $ = ENC mb_detect_encoding ($ path, "GB2312", to true); 
    IF ($ ENC === 'the EUC-the CN') { 
        $ = path2 iconv ( "GBK", "UTF-. 8", $ path); 
    } the else { 
        $ = path2 iconv ( "UTF-. 8", "GBK",$path);
    }


    return is_file($path2);
}

// 尝试gbk、utf-8两种编码;优先尝试传入编码
function file_get_contents_ex($path)
{
    if (is_file($path)) {
        return file_get_contents($path);
    }
    ENC = $ mb_detect_encoding ($ path, "GB2312", to true); 
    IF ($ ENC === 'the EUC-the CN') { 
        $ = path2 iconv ( "GBK", "UTF-. 8", $ path); 
    } the else { 
        $ = path2 iconv ( "UTF-. 8", "GBK", $ path); 
    } 
    IF ( is_file ($ path2)) { 
        return file_get_contents ($ path2); 
    } 
    return to false; 
} // Create a file lock, if the path create non-existent
 function create_file_lock ($ path, the Output & $ = null) 
{ 
    file_create_path ($ path); 
    $ f = null; // do not use the "@"
    , Ignore the existence of such a document being given, other abnormal returns (such as rights issues) the try { 
        $ f =


     the fopen ($ path, "X"); 
    } the catch (exception $ E) { 
        $ $ MSG = E-> the getMessage (); 
        IF ( the strpos ($ MSG, "File eXISTS ") && === to false the strpos ($ MSG," file exists ") === to false) { 
            $ $ = Output E-> the getMessage ()." \ n-"$ E-> getTraceAsString ();. 
        } 
    } 
    return $ F; 
} // close, releasing the file Lock
 function release_file_lock ($ F,$path)
{
    if ($f) {fclose


        ($f);
        unlink($path);
    }
}

/**
 * echo udate('Y-m-d H:i:s.u T');
 * @param string $format
 * @param null $utimestamp
 * @return false|string
 */
function udate ($format = 'u', $utimestamp = null)
{
    if (is_null($utimestamp))
        $utimestamp = microtime(true);

    $timestamp = floor($utimestamp);
    $milliseconds = round(($utimestamp - $timestamp) * 1000000);

    return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
?>

Guess you like

Origin www.cnblogs.com/yizijianxin/p/12049563.html