<?PHP 
// Par : Ahmed ZERZIH
//Reoleweb

//Cette classe permet de gérer la table whodidwhat
//
// La partie vue est dans le fichier WhodidwhatHTML.php
namespace Classe;
use views\WriteWhoDidWhatHtml;
class WriteWhoDidWhat{
    public $ic_cmd;
    public $id_user;
    public $hostname;
    public $mode_cmd;
    public $table_cmd;
    public $url_cmd;
    public $sql_cmd;
    public $nrows;
    public $msg;
    public $lastmod;
    private $conn;
    public function __construct() {
        $args = func_get_args();
        $numArgs = func_num_args();
       
        if($numArgs>0){
            if (method_exists($this, $f = '__construct' . $numArgs)) {
                call_user_func_array([$this, $f], $args);
            }
        }

        
        
    }
    public function __construct1($conn){
        $this->conn=$conn;
    }

    public function afficheWDW(){
        $conn= $this->conn;
        
        $sql = "SELECT SQL_CALC_FOUND_ROWS id_cmd, username, hostname, mode_cmd, table_cmd, url_cmd, sql_cmd, nrows, msg, whodidwhat.lastmod 
                FROM whodidwhat
                LEFT JOIN users ON
                whodidwhat.id_user=users.id_user
                WHERE 1";
       
        $conn->exec($sql);
        $result = $conn->data;
       
       WriteWhoDidWhatHTML::AfficheWDW($result,$sql);
    }
    static private function getClientHostname()
    {
        $hostname=strtoupper(gethostbyaddr($_SERVER['REMOTE_ADDR']));
        $hostname=str_replace(".drht.LOCAL", "", $hostname);
        $hostname=str_replace(".drht.RTSS.QC.CA", "", $hostname);
        return $hostname;
    }
    static public function enregistre($id_user, $sqlcmd, $urlinfo, $mydb)
    {
        $mode_cmd = strtok($sqlcmd, " ");
        $tableUpdate = strtok(" ");
        $tableInsertDelete = strtok(" ");
        
        //Get the table name
        if($mode_cmd == "UPDATE"){
            $table_cmd = $tableUpdate;
        }else{
            $table_cmd = $tableInsertDelete;
        }
        $sqlcmd2 = mysqli_real_escape_string($mydb->link_id, $sqlcmd);
        $nrows=$mydb->rows;
        $errorMsg = mysqli_real_escape_string($mydb->link_id, mysqli_error($mydb->link_id));
        $h=WriteWhoDidWhat::getClientHostname();

        //Generate sql command
        $sql = "INSERT INTO whodidwhat (id_user, hostname, mode_cmd, table_cmd, url_cmd, sql_cmd, nrows, msg)
        VALUES ('$id_user', '$h', '$mode_cmd', '$table_cmd', '$urlinfo', '$sqlcmd2', '$nrows', '$errorMsg')";
        $mydb->exec($sql);

        if ( ($nrows<0) && ($_SERVER['SERVER_NAME']=='sgbmapp01') ) {
            $sql="SELECT username FROM users WHERE id_user='$id_user'";
            $mydb->exec($sql);
            $username=$mydb->data[0]['username'];
            $body=$username."\n\n".$urlinfo."\n\n".$sqlcmd."\n\n".$errorMsg;
            ini_set("SMTP","smtp1.drht.local");
            ini_set("sendmail_from","gestionvisiteur@drht-mhi.org");
            mail("ahmed.zerzih@drht-mhi.org","gestionvisiteur - Erreur",$body);
        }
        
    }
   
}
