<?php
/*
 * Ajout d'un client à l'OPAC (Ajax)
 */

header('Content-type: text/xml');
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><root></root>');

if ($_GET['action'] == 'addclient') {
    if (!isset($_GET['idclient']) && !isset($_GET['keyclient']) && !isset($_GET['ipclient'])) {
        $xml->addChild('result', 0);
        $xml->addChild('errorMsg', 'Paramètres manquants');
    }
    else {
        try {
            if (!file_put_contents('../serveur/config/clients.ini', "\r\n".'['.strtolower($_GET['idclient']).']'."\r\n".'key = '.strtoupper($_GET['keyclient'])."\r\n".'ip_agent = '.$_GET['ipclient']."\r\n", FILE_APPEND)) throw new Exception('Impossible d\'écrire dans le fichier de configuration');
            if (isset($_GET['userFTP']) && isset($_GET['passFTP']) && isset($_GET['hostBDD']) && isset($_GET['userBDD']) && isset($_GET['passBDD']) && isset($_GET['hostBDDCentral']) && isset($_GET['userBDDCentral']) && isset($_GET['passBDDCentral'])) {
                $mysqli = @new mysqli($_GET['hostBDD'], $_GET['userBDD'], $_GET['passBDD']);
                if ($mysqli->connect_errno) {
                    $xml->addChild('result', 0);
                    $xml->addChild('errorMsg', 'Echec de la connexion MySQL Vsftpd: '.$mysqli->connect_error);
                }
                else {
                    if (!$mysqli->query("CREATE DATABASE IF NOT EXISTS vsftpd")) throw new Exception('Erreur SQL : '.$mysqli->error);
                    if (!$mysqli->select_db('vsftpd')) throw new Exception('Erreur SQL : '.$mysqli->error);
                    if (!$mysqli->query("CREATE TABLE IF NOT EXISTS `log` (
                                          `id_log` int(11) NOT NULL AUTO_INCREMENT,
                                          `login` varchar(50) NOT NULL,
                                          `message` varchar(200) NOT NULL,
                                          `pid` varchar(10) NOT NULL,
                                          `host` varchar(30) NOT NULL,
                                          `time` datetime DEFAULT NULL,
                                          PRIMARY KEY (`id_log`)
                                        ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;")) throw new Exception('Erreur SQL : '.$mysqli->error);
                    if (!$mysqli->query("CREATE TABLE IF NOT EXISTS `users` (
                                          `id_user` int(11) NOT NULL AUTO_INCREMENT,
                                          `login` varchar(50) NOT NULL,
                                          `password` varchar(100) NOT NULL,
                                          `active` int(11) NOT NULL,
                                          PRIMARY KEY (`id_user`)
                                        ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;")) throw new Exception('Erreur SQL : '.$mysqli->error);
                    if (!$mysqli->query("INSERT INTO users (login, password, active) VALUES ('".$mysqli->real_escape_string($_GET['userFTP'])."', MD5('".$mysqli->real_escape_string($_GET['passFTP'])."'), '1')")) throw new Exception('Erreur SQL : '.$mysqli->error);
                }
                $mysqli_central = @new mysqli($_GET['hostBDDCentral'], $_GET['userBDDCentral'], $_GET['passBDDCentral']);
                if ($mysqli_central->connect_errno) {
                    $xml->addChild('result', 0);
                    $xml->addChild('errorMsg', 'Echec de la connexion MySQL Central: '.$mysqli_central->connect_error);
                }
                else {
                    if (!$mysqli_central->select_db('opac_central')) throw new Exception('Erreur SQL : '.$mysqli_central->error);
                    if (!$mysqli_central->query("INSERT INTO institution_allowed (code, active) VALUES ('".$mysqli_central->real_escape_string($_GET['idclient'])."', 1)")) throw new Exception('Erreur SQL : '.$mysqli_central->error);
                }
            }
            $xml->addChild('result', 1);
            $xml->addChild('errorMsg', 0);
        }
        catch (Exception $e) {
            $xml->addChild('result', 0);
            $xml->addChild('errorMsg', $e->getMessage());
        }
    }
}
else {
    $xml->addChild('result', 0);
    $xml->addChild('errorMsg', 'Requête non définie');
}

echo $xml->asXML();

?>
