<?php
/**
 * Here is your custom functions.
 */
use think\facade\Db;

function getUrl(){
    // $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $request = request();
    $protocol = 'http://';
    $http = $request->host();

    return $protocol.$http;
}

function AdminLog($admin_id, $content){
    
    if(!$admin_id){
        $user_name = '';
    }else{
        $user_name = Db::name('admin')
        ->where('id', $admin_id)->value('username');    
    }

    $data = [
        "admin_id" => $admin_id,
        "admin_name" => $user_name,
        "content" => $content,
        "time" => date("Y-m-d h:i:s",time()),
        "ip" => getRealIp()
    ];

    Db::name('admin_log')->insert($data);
}

function getNameById($table,$field,$id){
    return Db::name($table)->where('id', $id)->value($field);
}

function getRealIp(){

    $forwarded = null;

    if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
        $forwarded = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

    if($forwarded){
        return $forwarded;
    }

    if(isset($_SERVER['REMOTE_ADDR'])){
        return $_SERVER['REMOTE_ADDR'];
    }

    return '';
}
