<?php
/* 
 * Interface d'administration du Boker
 */

class adminBroker {

    public function readConfig($sFile) {
        try {
            $tabConfig = @parse_ini_file($sFile, true);
        }
        catch (Exception $e) {
            return false;
        }
        return $tabConfig;
    }

    public function writeConfig($aTabConfig) {
        $config_save = '';
        foreach ($aTabConfig as $section => $groupe_valeurs) {
            $config_save .= "\r\n\r\n[".$section."]";
            foreach($groupe_valeurs as $key => $valeur) {
                $config_save .= "\r\n".$key."=".$valeur;
            }
        }
        $config_save = substr($config_save, 2);
        return $config_save;
    }

    public function sendConfig($typeconfig, $idclient, $sURL, $sFile) {
        $retour = $this->curl_send_file($typeconfig, $idclient, $sURL, $sFile);
        if ($retour[0]) return array(true, $retour[1]);
        else return array(false, $retour[1]);
    }

    public function encrypt($text, $sPartKey) {
    	$key = (strlen($sPartKey) < 11 ? $sPartKey : substr($sPartKey, -11)).date('Ymd').'OPAC2'; //Taille max de la clé : 24
    	$iv = '12345678';

    	$cipher = mcrypt_module_open(MCRYPT_3DES,'','cbc','');

        mcrypt_generic_init($cipher, $key, $iv);
        $encrypted = mcrypt_generic($cipher,$text);
        $encrypted = base64_encode($encrypted);
        mcrypt_generic_deinit($cipher);

        return $encrypted;
    }

    public function decrypt($text, $sPartKey) {
        $key = $sPartKey.date('Ymd').'OPAC2';
        $iv = '12345678';

        $cipher = mcrypt_module_open(MCRYPT_3DES,'','cbc','');

        mcrypt_generic_init($cipher, $key, $iv);
        $temp = base64_decode($text);
        $decrypted = trim(mdecrypt_generic($cipher,$temp));
        mcrypt_generic_deinit($cipher);

        return $decrypted;
    }

    //Envoi de fichier via CURL
    private function curl_send_file($typeconfig, $idclient, $sURL, $sFile) {
        if (strpos($sURL, ":")) {
            $ip = substr($sURL, 0, strpos($sURL, ":"));
        }
        elseif (strpos($sURL, "/")) {
            $ip = substr($sURL, 0, strpos($sURL, "/"));
        }
        else $ip = $sURL;
        $sURL = 'http://'.$sURL.'/webservices/client/client.php';
        $sFile = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], "/") + 1).$sFile;
        try {
            if (!$ch = @curl_init()) throw new Exception('curl_init');
            if (!@curl_setopt($ch, CURLOPT_HEADER, true)) throw new Exception('curl option : CURLOPT_HEADER');
            if (!@curl_setopt($ch, CURLOPT_BINARYTRANSFER, true)) throw new Exception('curl option : CURLOPT_BINARYTRANSFER');
            if (!@curl_setopt($ch, CURLOPT_VERBOSE, true)) throw new Exception('curl option : CURLOPT_VERBOSE');
            if (!@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)) throw new Exception('curl option : CURLOPT_RETURNTRANSFER');
            if (!@curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)")) throw new Exception('curl option : CURLOPT_USERAGENT');
            if (!@curl_setopt($ch, CURLOPT_URL, $sURL)) throw new Exception('curl option : CURLOPT_URL');
            if (!@curl_setopt($ch, CURLOPT_INFILESIZE, filesize($sFile))) throw new Exception('curl option : CURLOPT_INFILESIZE');
            if (!@curl_setopt($ch, CURLOPT_POST, true)) throw new Exception('curl option : CURLOPT_POST');
            $post = array(
                    'idclient' => $idclient,
                    'mode' => 'config',
                    'typeconfig' => $typeconfig,
                    'ip' => $ip,
                    'file' => new CURLFile($sFile, 'text/plain', 'config.ini')
            );
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            $response = curl_exec($ch);
            if (!$response) return array(false, 'Erreur');
            else return array(true, $response);
        }
        catch (Exception $e) {
            return array(false, $e->getMessage());
        }
    }

}

?>
