<?php
require_once("../../base/php/startSession.php");
require_once("../../base/php/Environnement.php" );
require_once( "../../base/php/Functions.php" );
require_once("../../base/php/classDatabase.php");

global $last_encours_active;
global $last_encours_endded;
global $pmsiProvider;
global $pmsiProvider2;

$httpString = "";
$httpString = $httpString . "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
$httpString = $httpString . "\n<HTTP_SERVICE>";

$httpString = $httpString . getPmsiProviders();

$database = new Database("iCTI");

$sqlcmd = "SELECT oid, texte, date_import, etat, etat_en_cours, traitement_a_faire, 
		finess, date_debut, date_fin, 'ePMSI'::text , 0::numeric, nb_rss, nb_rum, nb_rsf, nb_erreurs, nb_avertissements , 
		p_imports_data_rss.file_path AS rss_file_path, p_imports_data_rss.file_size AS rss_data_size, 
		p_imports_data_rsf.file_path AS rsf_file_path, p_imports_data_rsf.file_size AS rsf_data_size, 
		p_imports_data_fcp.file_path AS fcp_file_path, p_imports_data_fcp.file_size AS fcp_data_size, 
		p_imports_data_med.file_path AS med_file_path, p_imports_data_med.file_size AS med_data_size, 
		p_imports_data_rssmed.file_path AS rssmed_file_path, p_imports_data_rssmed.file_size AS rssmed_data_size, 
		p_imports_data_rssnom.file_path AS rssnom_file_path, p_imports_data_rssnom.file_size AS rssnom_data_size, 
		p_imports_data_actmed.file_path AS actmed_file_path, p_imports_data_actmed.file_size AS actmed_data_size,
		p_imports_data_zin.file_path AS zin_file_path, p_imports_data_zin.file_size AS zin_data_size,
		p_imports_data_zou.file_path AS zou_file_path, p_imports_data_zou.file_size AS zou_data_size,
		p_imports_data_zif.file_path AS zif_file_path, p_imports_data_zif.file_size AS zif_data_size,
		p_imports_data_vsj.file_path AS vsj_file_path, p_imports_data_vsj.file_size AS vsj_data_size,
		p_imports_data_vse.file_path AS vse_file_path, p_imports_data_vse.file_size AS vse_data_size,
		p_imports_data_tsp.file_path AS tsp_file_path, p_imports_data_tsp.file_size AS tsp_data_size 
	FROM pmsi.p_imports  
	LEFT JOIN pmsi.p_imports_data p_imports_data_rss ON (p_imports.oid = p_imports_data_rss.import_id AND p_imports_data_rss.data_type = 'RSS' AND p_imports_data_rss.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_rsf ON (p_imports.oid = p_imports_data_rsf.import_id AND p_imports_data_rsf.data_type = 'RSF' AND p_imports_data_rsf.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_fcp ON (p_imports.oid = p_imports_data_fcp.import_id AND p_imports_data_fcp.data_type = 'FCP' AND p_imports_data_fcp.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_med ON (p_imports.oid = p_imports_data_med.import_id AND p_imports_data_med.data_type = 'MED' AND p_imports_data_med.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_rssmed ON (p_imports.oid = p_imports_data_rssmed.import_id AND p_imports_data_rssmed.data_type = 'RSSMED' AND p_imports_data_rssmed.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_rssnom ON (p_imports.oid = p_imports_data_rssnom.import_id AND p_imports_data_rssnom.data_type = 'RSSNOM' AND p_imports_data_rssnom.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_actmed ON (p_imports.oid = p_imports_data_actmed.import_id AND p_imports_data_actmed.data_type = 'ACTMED' AND p_imports_data_actmed.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_zin ON (p_imports.oid = p_imports_data_zin.import_id AND p_imports_data_zin.data_type = 'ZIN' AND p_imports_data_zin.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_zou ON (p_imports.oid = p_imports_data_zou.import_id AND p_imports_data_zou.data_type = 'ZOU' AND p_imports_data_zou.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_zif ON (p_imports.oid = p_imports_data_zif.import_id AND p_imports_data_zif.data_type = 'ZIF' AND p_imports_data_zif.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_vsj ON (p_imports.oid = p_imports_data_vsj.import_id AND p_imports_data_vsj.data_type = 'VSJ' AND p_imports_data_vsj.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_vse ON (p_imports.oid = p_imports_data_vse.import_id AND p_imports_data_vse.data_type = 'VSE' AND p_imports_data_vse.data_num = 0) 
	LEFT JOIN pmsi.p_imports_data p_imports_data_tsp ON (p_imports.oid = p_imports_data_tsp.import_id AND p_imports_data_tsp.data_type = 'TSP' AND p_imports_data_tsp.data_num = 0) 
	ORDER BY oid DESC";
$result  =  $database->exec($sqlcmd);


if ($result != false) {

    $ok = TRUE;

    while ($ok == TRUE) {
        $ok = FALSE;

        $record = $database->nextRecordInto();
        if ($record != FALSE) {

            $ok = TRUE;

            $oid = $record[0];
            $texte = toHTML(trim($record[1]));
            $date_import = trim($record[2]);
            $etat = trim($record[3]);
            $etat_en_cours = trim($record[4]);
            $traitement_a_faire = trim($record[5]);
            $finess = trim($record[6]);
            $date_debut = trim($record[7]);
            $date_fin = trim($record[8]);
            $nb_rss = trim($record[11]);
            $nb_rum = trim($record[12]);
            $nb_rsf = trim($record[13]);
            $nb_erreurs = trim($record[14]);
            $nb_avertissements = trim($record[15]);
            $rss_file_path = toHTML(trim($record[16]));
            $rss_data_size = trim($record[17]);
            $rsf_file_path = toHTML(trim($record[18]));
            $rsf_data_size = trim($record[19]);
            $fcp_file_path = toHTML(trim($record[20]));
            $fcp_data_size = trim($record[21]);

            $med_file_path = toHTML(trim($record[22]));
            $med_data_size = trim($record[23]);
            $rssmed_file_path = toHTML(trim($record[24]));
            $rssmed_data_size = trim($record[25]);
            $rssnom_file_path = toHTML(trim($record[26]));
            $rssnom_data_size = trim($record[27]);
            $actmed_file_path = toHTML(trim($record[28]));
            $actmed_data_size = trim($record[29]);
            $zin_file_path = toHTML(trim($record[30]));
            $zin_data_size = trim($record[31]);
            $zou_file_path = toHTML(trim($record[32]));
            $zou_data_size = trim($record[33]);
            $zif_file_path = toHTML(trim($record[34]));
            $zif_data_size = trim($record[35]);
            $vsj_file_path = toHTML(trim($record[36]));
            $vsj_data_size = trim($record[37]);
            $vse_file_path = toHTML(trim($record[38]));
            $vse_data_size = trim($record[39]);
            $tsp_file_path = toHTML(trim($record[39]));
            $tsp_data_size = trim($record[40]);


            $httpString = $httpString . "\r\n<IMPORT ";
            $httpString = $httpString . "oid=\"$oid\" ";
            $httpString = $httpString . "texte=\"$texte\" ";
            $httpString = $httpString . "date_import=\"$date_import\" ";
            $httpString = $httpString . "etat=\"$etat\" ";
            $httpString = $httpString . "etat_en_cours=\"$etat_en_cours\" ";
            $httpString = $httpString . "traitement_a_faire=\"$traitement_a_faire\" ";
            $httpString = $httpString . "finess=\"$finess\" ";
            $httpString = $httpString . "date_debut=\"$date_debut\" ";
            $httpString = $httpString . "date_fin=\"$date_fin\" ";
            $httpString = $httpString . "nb_rss=\"$nb_rss\" ";
            $httpString = $httpString . "nb_rum=\"$nb_rum\" ";
            $httpString = $httpString . "nb_rsf=\"$nb_rsf\" ";
            $httpString = $httpString . "nb_erreurs=\"$nb_erreurs\" ";
            $httpString = $httpString . "nb_avertissements=\"$nb_avertissements\" ";
            $httpString = $httpString . "rss_file_path=\"$rss_file_path\" ";
            $httpString = $httpString . "rss_data_size=\"$rss_data_size\" ";
            $httpString = $httpString . "rsf_file_path=\"$rsf_file_path\" ";
            $httpString = $httpString . "rsf_data_size=\"$rsf_data_size\" ";
            $httpString = $httpString . "fcp_file_path=\"$fcp_file_path\" ";
            $httpString = $httpString . "fcp_data_size=\"$fcp_data_size\" ";
            $httpString = $httpString . "med_file_path=\"$med_file_path\" ";
            $httpString = $httpString . "med_data_size=\"$med_data_size\" ";
            $httpString = $httpString . "rssmed_file_path=\"$rssmed_file_path\" ";
            $httpString = $httpString . "rssmed_data_size=\"$rssmed_data_size\" ";
            $httpString = $httpString . "rssnom_file_path=\"$rssnom_file_path\" ";
            $httpString = $httpString . "rssnom_data_size=\"$rssnom_data_size\" ";
            $httpString = $httpString . "actmed_file_path=\"$actmed_file_path\" ";
            $httpString = $httpString . "actmed_data_size=\"$actmed_data_size\" ";
            $httpString = $httpString . "zin_file_path=\"$zin_file_path\" ";
            $httpString = $httpString . "zin_data_size=\"$zin_data_size\" ";
            $httpString = $httpString . "zou_file_path=\"$zou_file_path\" ";
            $httpString = $httpString . "zou_data_size=\"$zou_data_size\" ";
            $httpString = $httpString . "zif_file_path=\"$zif_file_path\" ";
            $httpString = $httpString . "zif_data_size=\"$zif_data_size\" ";
            $httpString = $httpString . "vsj_file_path=\"$vsj_file_path\" ";
            $httpString = $httpString . "vsj_data_size=\"$vsj_data_size\" ";
            $httpString = $httpString . "vse_file_path=\"$vse_file_path\" ";
            $httpString = $httpString . "vse_data_size=\"$vse_data_size\" ";
            $httpString = $httpString . "tsp_file_path=\"$tsp_file_path\" ";
            $httpString = $httpString . "tsp_data_size=\"$tsp_data_size\" ";

            $httpString = $httpString . "/>";

        }

    }

}

// Historique des travaux
// MHA : commenter car tres energivore pour peu de plus-value
// travaux actifs
//$httpString = $httpString . getJobs("active");
//// travaux terminés
//$httpString = $httpString . getJobs("endded");



// Licence
$httpString = $httpString . "\n<ENCOURS ";
if (check_licence_activite() == TRUE) {
    $httpString = $httpString . "licence_activite=\"true\" ";
}
else {
    $httpString = $httpString . "licence_activite=\"false\"  ";
}
$httpString = $httpString . "last_encours_active=\"$last_encours_active\" last_encours_endded=\"$last_encours_endded\" />";

// Config php
$httpString = $httpString . "\r\n<APACHEPHP ";
$post_max_size = str_replace("M","",ini_get('post_max_size'));
$upload_max_filesize = str_replace("M","",ini_get('upload_max_filesize'));
$httpString = $httpString . "post_max_size=\"$post_max_size\" ";
$httpString = $httpString . "upload_max_filesize=\"$upload_max_filesize\" ";
$httpString = $httpString . "/>";

$httpString = $httpString . "\n</HTTP_SERVICE>";



$httpString = gzcompress($httpString,9);
$httpString = "_c_" . base64_encode($httpString);

echo "<CTICONTENT>$httpString</CTICONTENT>";



// recherche historique des travaux
function getJobs($type) {

    $httpString = "";

    global $jobs;
    global $jobsDate;
    global $jobsType;
    global $last_encours_active;
    global $last_encours_endded;

    if ($type == "active") {
        $path = rootDir() . "system/batch/activejobs";
        $last_encours_active = "";
        $last_encours_active_date = "";
    }
    else {
        $path = rootDir() . "system/batch/jobs";
        $last_encours_endded = "";
        $last_encours_endded_date = "";
    }

    // lecture
    $rep=@opendir($path);
    if ($rep > 0) {
        while ($file = readdir($rep)){
            if($file != '..' && $file !='.' && $file !=''  ){
                $suffix = strtolower(substr($file,strlen($file)-4));
                if ($suffix == ".pid") {
                    $longFile = $path . '/' . $file;

                    $pid="";
                    $name="";
                    $module="";
                    $title="";
                    $start="";
                    $end="";
                    $endStatus="";
                    $duration="";
                    $log="";

                    if ($type == "active") {
                        $endStatus="En cours";
                        $end="En-cours";
                    }


                    $handle = @fopen($longFile, "r");
                    if ($handle) {
                        $contents = "";
                        while (!feof($handle)) {
                            $contents = @fgets($handle, 300);
                            if (substr($contents, 0, 4) == 'pid=') {
                                $pid = trim(substr($contents, 4, strlen($contents)-4));
                            }
                            if (substr($contents, 0, 5) == 'name=') {
                                $name = trim(substr($contents, 5, strlen($contents)-5));
                            }
                            if (substr($contents, 0, 7) == 'module=') {
                                $module = trim(substr($contents, 7, strlen($contents)-7));
                            }
                            if (substr($contents, 0, 6) == 'title=') {
                                $title = trim(substr($contents, 6, strlen($contents)-6));
                            }
                            if (substr($contents, 0, 6) == 'start=') {
                                $start = trim(substr($contents, 6, strlen($contents)-6));
                            }
                            if (substr($contents, 0, 4) == 'end=' && $type != "active") {
                                $end = trim(substr($contents, 4, strlen($contents)-4));
                            }
                            if (substr($contents, 0, 9) == 'duration=') {
                                $duration = trim(substr($contents, 9, strlen($contents)-9));
                                $duration = mktime(0, 0, $duration, 1, 1, 2007);
                                $duration = date("G:i:s", $duration);
                            }
                            if (substr($contents, 0, 10) == 'endStatus=' && $type != "active") {
                                $endStatus = trim(substr($contents, 10, strlen($contents)-10));
                            }
                            if (substr($contents, 0, 4) == 'log=') {
                                $log = trim(substr($contents, 4, strlen($contents)-4));
                            }

                        }
                        @fclose($handle);




                        $fmt_start_date_time = substr($start , 8 , 2) . "/" . substr($start , 5 , 2) . "/" . substr($start , 0 , 4) . " à " . substr($start , 11 , 8);
                        if ($pid != "" && $name == "iCTI_import_rss_rsf_en_cours") {
                            if ($type == "active") {
                                if ($start > $last_encours_active_date) {
                                    $last_encours_active_date = $start;
                                    $last_encours_active = "Travail actif depuis le " . $fmt_start_date_time;
                                }
                            }
                            if ($type == "endded") {
                                if ($start > $last_encours_endded_date) {
                                    $last_encours_endded_date = $start;
                                    $last_encours_endded = "Dernier import (durée : $duration) lancé le " . $fmt_start_date_time;
                                }
                            }
                        }


                        if ($pid != "" && $name == "iCTI_import_rss_rsf") {
                            $httpString = $httpString . "\n<JOB pid=\"$pid\" logFile=\"$log\" name=\"$name\" module=\"$module\" title=\"$title\" start=\"$start\" end=\"$end\" duration=\"$duration\" endStatus=\"$endStatus\" pidfile=\"$longFile\" />";
                        }
                        if ($pid != "" && $name == "iCTI_import_rss_rsf_en_cours") {
                            $httpString = $httpString . "\n<JOB pid=\"$pid\" logFile=\"$log\" name=\"$name\" module=\"$module\" title=\"$title\" start=\"$start\" end=\"$end\" duration=\"$duration\" endStatus=\"$endStatus\" pidfile=\"$longFile\" />";
                        }

                    }
                }
            }
        }
    }
    @closedir($rep);

    return $httpString;


}

function getPmsiProviders() {

    global $pmsiProvider;
    global $pmsiProvider2;

    $pmsiProvider = "";
    $pmsiProvider2 = "";

    $pmsiProviders = "<PMSIPROVIDERS ";

    $file = "../../../settings/settings.XML";
    $settings_xml = simplexml_load_file($file);

    foreach ($settings_xml->ENVIRONMENT as $environmentNode) {
        foreach ($environmentNode->PROPERTY as $propertyNode) {
            if ($propertyNode['name'] == 'PMSIPROVIDER') {
                $pmsiProviders = $pmsiProviders . "provider1=\"" . $propertyNode['value'] . "\" provider1label=\"" . getPmsiProviderLabel($propertyNode['value']) . "\" ";
                $pmsiProvider = getPmsiProviderShortLabel($propertyNode['value']);
            }
            if ($propertyNode['name'] == 'PMSIPROVIDER2') {
                $pmsiProviders = $pmsiProviders . "provider2=\"" . $propertyNode['value'] . "\" provider2label=\"" . getPmsiProviderLabel($propertyNode['value']) . "\" ";
                $pmsiProvider2 = getPmsiProviderShortLabel($propertyNode['value']);
            }
        }
    }

    $pmsiProviders = $pmsiProviders . " />";

    return $pmsiProviders;

}

function getPmsiProviderLabel($code) {
    switch ($code) {
        case "auditio":
            return "Auditio";
        case "cegi":
            return "Cegi";
        case "qsp":
            return "QSP";
        case "r2i":
            return "Softway (R2I)";
        case "shs":
            return "SHS";
        case "sigems":
            return "Sigems";
        case "csys":
            return "CSYS";
        case "web100t":
            return "WEB100T";
        case "sim":
            return "SIM";
    }
    return "?";
}


function getPmsiProviderShortLabel($code) {
    switch ($code) {
        case "auditio":
            return "AUD";
        case "cegi":
            return "CEG";
        case "qsp":
            return "QSP";
        case "r2i":
            return "STW";
        case "shs":
            return "SHS";
        case "sigems":
            return "SIG";
        case "csys":
            return "CSYS";
        case "web100t":
            return "W100";
        case "sim":
            return "SIM";
    }
    return "";
}

function check_licence_activite() {

    global $finess;
    global $serverName;
    global $checkServerName;
    global $isCtiLicenced;


    $current_serverName = $serverName;

    $file = "../../../settings/settings.XML";
    $settings_xml = simplexml_load_file($file);

    $ENV_ADMPROVIDER = "";
    foreach ($settings_xml->ENVIRONMENT as $environmentNode) {
        foreach ($environmentNode->PROPERTY as $propertyNode) {
            if ($propertyNode['name'] == 'ADMPROVIDER') {
                $ENV_ADMPROVIDER = $propertyNode['value'];
            }
        }
    }

    if (file_exists("../../../modules/activite")) {
        return FALSE;
    }

    if ($isCtiLicenced == "true") {
        return TRUE;
    }


    $path = "../../../licences";

    // lecture
    $i = -1;
    $rep=@opendir($path);
    if ($rep > 0) {
        while ($file = readdir($rep)){
            if($file != '..' && $file !='.' && $file !=''  ){
                if (strtolower(substr($file,strlen($file)-4)) == ".lic") {
                    $longFile = $path . '/' . $file;

                    $licence_xmlString = @file_get_contents($longFile);
                    $licence_xml = @simplexml_load_string($licence_xmlString);
                    if ($licence_xml) {
                        $licence_key = @$licence_xml['key'];

                        $decodeKey =  @base64_decode($licence_key);
                        $decompressKey = @substr(gzuncompress($decodeKey),2);

                        $licArray = explode("|", $decompressKey);
                        $licence_machineName = strtoupper(trim($licArray[0]));
                        $licence_finess = trim($licArray[1]);
                        $licence_softCode = trim($licArray[2]);
                        $licence_date = trim($licArray[3]);

                        //echo "\r\n$licence_machineName $licence_finess $licence_softCode $licence_date";
                        if ($licence_softCode == "iCTI_activite") {
                            if ($licence_machineName == $current_serverName || $licence_machineName == $checkServerName) {
                                if ($licence_finess == $finess) {
                                    if ($licence_date >= date("Ymd")) {
                                        return TRUE;
                                    }
                                    else {
                                        //echo "\r\n$licence_date inférieure";
                                    }
                                }
                                else {
                                    //echo "\r\n$licence_finess différent de $ENV_FINESS";
                                }
                            }
                            else {
                                //echo "\r\n$licence_machineName différent de $current_serverName";
                            }
                        }
                        else {
                            //echo "\r\n$licence_softCode autre";
                        }
                    }
                }
            }
        }
    }

    return FALSE;
}

?>