<?php

/**
 * DokuWiki AJAX call handler
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 */

if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../../');
require_once(DOKU_INC . 'inc/init.php');

/*
session_start();
$session = json_encode($_SESSION);
$cookie  = json_encode($_COOKIE);
$status  = json_encode(session_status());
*/
//close session
session_write_close();

// default header, ajax call may overwrite it later
header('Content-Type: text/html; charset=utf-8');

//call the requested function
/*
global $INPUT;
if ($INPUT->has('call')) {
    $call = $INPUT->filter([Clean::class, 'stripspecials'])->str('call');
    new Ajax($call);
} else {
    http_status(404);
}*/

/*
$html = "<p>session</p>\n";
$html.= "<code>$session</code>\n";
$html.= "<p>cookie</p>\n";
$html.= "<code>$cookie</code>\n";
$html.= "<p>status</p>\n";
$html.= "<code>$status</code>\n";
*/
$input = file_get_contents('php://input');
// Unpack
$id = substr($input,0,5);
$md5 = substr($input,5,32);
$text =  substr($input,37);
//sleep(5);
// Reject if md5 does not match
if ($md5 != md5($text)) {
    http_response_code(400);
    die();
}
$info = [];
$html = p_render('xhtml', p_get_instructions($text), $info);

echo $id.md5($html).$html;
//echo json_encode(['input'=>$input]);
//echo json_encode(['session'=>$_SESSION]);

