<?php
date_default_timezone_set('America/Mazatlan');
ini_set('max_execution_time', 0);
ini_set('memory_limit', '-1');
class DBManager2
{
    public $conect;

    public $BaseDatos;
    public $Servidor;
    public $Usuario;
    public $Clave;
    private $conn;

    public function DBManager2()
    {
        $this->BaseDatos = "enlacegmm";
        // $this->Servidor   = "culiacan.morsa.com.mx";
        $this->Servidor   = "10.0.1.13";
        $this->Usuario    = "kerberox";
        $this->Clave      = "204N1tN3L@V19";
    }

    public function connectionWithPDO2()
    {
        $this->conn = null;
        try {
            $this->conn = new PDO("pgsql:host=" . $this->Servidor . ";dbname=" . $this->BaseDatos, $this->Usuario, $this->Clave);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $execute = $this->conn->prepare("SET CLIENT_ENCODING TO 'UTF8';");
            $execute->execute();
            $this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
        } catch (PDOException $e) {
            echo 'error';
        }

        return $this->conn;
    }

}
function notificador($error){
    foreach($error as $key=>$value)
    {
        $module=$error['$module'];
        $option=$error['$option'];
        $detail=$error['$detail'];
        $squery=$error['$query'];
        $values=$error['$values'];
        $users=$error['$user'];
        $message=$error['$message'];
    }
    session_start();
    $Referer=$_SERVER['HTTP_REFERER'];
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $data = array(
                    "text"=> "*`".$message."`*",
                    "mrkdwn"=> true,
                    "attachments"=> array(
                        [
                            "fields"=> array(
                                [
                                    "title"=> "```User```",
                                    "value"=> $users,
                                    "short"=> true
                                ],
                                [
                                    "title"=> "```Name```",
                                    "value"=> $_SESSION["user_name"],
                                    "short"=> true
                                ]
                            ),
                            "color"=> "#FF0000"
                        ],
                        [
                            "fields"=> array(
                                [
                                    "title"=> "```Module```",
                                    "value"=> $module,
                                    "short"=> true
                                ],
                                [
                                    "title"=> "```Option```",
                                    "value"=> $option,
                                    "short"=> true
                                ],
                                [
                                    "title"=> "```Detail```",
                                    "value"=> $detail,
                                    "short"=> true
                                ],
                                [
                                    "title"=> "```Excecute```",
                                    "value"=> $squery,
                                    "short"=> true
                                ],
                                [
                                    "title"=> "```All Values```",
                                    "value"=> $values,
                                    "short"=> true
                                ],
                                [
                                    "title"=> "```Browser```",
                                    "value"=> getBrowser($user_agent),
                                    "short"=> true
                                ],
                                [
                                    "title"=> "```URL Referer```",
                                    "value"=> $Referer,
                                    "short"=> true
                                ],
                                [
                                    "title"=> "```Status Code```",
                                    "value"=> http_response_code(),
                                    "short"=> true
                                ]
                            ),
                            "color"=> "#0000FF"
                        ]
                    )
                );
    $data_string = json_encode($data);
    $ch = curl_init('https://hooks.slack.com/services/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');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
    $result = curl_exec($ch);
    
    $DB = new DBManager2();
    $conn = $DB->connectionWithPDO2();
    $query = "INSERT INTO log_sam (module,option,detail,query,values,users,message) VALUES(?,?,?,?,?,?,?);";
    $stm = $conn->prepare($query);
    $stm->bindParam(1, $module, PDO::PARAM_STR);
    $stm->bindParam(2, $option, PDO::PARAM_STR);
    $stm->bindParam(3, $detail, PDO::PARAM_STR);
    $stm->bindParam(4, $squery, PDO::PARAM_STR);
    $stm->bindParam(5, $values, PDO::PARAM_STR);
    $stm->bindParam(6, $users, PDO::PARAM_STR);
    $stm->bindParam(7, $message, PDO::PARAM_STR);
    $result = $stm->execute();
    
}

function getBrowser($user_agent){

    if(strpos($user_agent, 'MSIE') !== FALSE)
        return 'Internet explorer';
    elseif(strpos($user_agent, 'Edge') !== FALSE) //Microsoft Edge
        return 'Microsoft Edge';
    elseif(strpos($user_agent, 'Trident') !== FALSE) //IE 11
        return 'Internet explorer';
    elseif(strpos($user_agent, 'Opera Mini') !== FALSE)
        return "Opera Mini";
    elseif(strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR') !== FALSE)
        return "Opera";
    elseif(strpos($user_agent, 'Firefox') !== FALSE)
        return 'Mozilla Firefox';
    elseif(strpos($user_agent, 'Chrome') !== FALSE)
        return 'Google Chrome';
    elseif(strpos($user_agent, 'Safari') !== FALSE)
        return "Safari";
    else
        return 'No hemos podido detectar su navegador';
};
?>