<?php
/* 
 * Mise à jour de l'OPAC (Ajax)
 */

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

if ($_GET['action'] == 'majglobal') {
    if (file_exists($_GET['updatorpath'])) {
        if (file_exists('cache/log_maj.log')) unlink('cache/log_maj.log');
        //Changement des séparateurs Windows
        $new_path = str_replace("\\", "/", $_GET['updatorpath']);
        $prev_dir = getcwd();
        $dir = substr($new_path, 0, strrpos($new_path, "/") + 1);
        chdir($dir);
        $output = array();
        try {
            set_time_limit(300);
            if (!exec($new_path, &$output, &$return_val)) throw new Exception('Erreur d\'execution');
            chdir($prev_dir);
            $new_output = implode("<br />", $output);
            if (!file_exists('cache/')) mkdir('cache');
            file_put_contents('cache/log_maj.log', $new_output);
            $xml->addChild('result', 1);
            $xml->addChild('infosMsg', (strlen($new_output) > 4000 ? '<a href="#" onclick="javascript:window.open(\'popup_log.php\', \'MAJ\', config=\'height=600, width=400, scrollbars=yes\');return false;">Voir le log</a>' : $new_output));
        }
        catch (Exception $e) {
            $xml->addChild('result', 0);
            $xml->addChild('errorMsg', $e->getMessage());
        }
    }
    else {
        $xml->addChild('result', 0);
        $xml->addChild('errorMsg', 'Fichier inexistant');
    }
}
else {
    $xml->addChild('result', 0);
    $xml->addChild('errorMsg', 'Requête non définie');
}

echo $xml->asXML();

?>
