1<?php
2include_once "gda-utils.php";
3include_once "gda-config.php";
4
5/*
6 * REM: no session is started in this script because sessions would serialize the
7 * execution of the gda-front.php and gda-worker.php scripts. Only the session ID, transmitted
8 * in the URL is used here.
9 */
10
11header ('Content-type: text/plain; charset=UTF-8');
12
13$datafile = null;
14
15try {
16	global $datafile;
17	$text = $GLOBALS["HTTP_RAW_POST_DATA"];
18	if ($text == "")
19		throw new Exception ("Empty command");
20
21	/* Send command */
22	$cmdfile = get_command_filename ($_GET['PHPSESSID']);
23	if (! file_exists ($cmdfile))
24		throw new Exception ("Bad setup $cmdfile");
25
26	$datafile = tempnam (session_save_path (), "GDACommand");
27	//echo "DATAFILE [$datafile]\n"; flush (); ob_flush();
28	if (file_put_contents ($datafile, $text) == false)
29		throw new Exception ("Can't create command file ".$cmdfile);
30
31	if (isset ($log))
32		$log->lwrite ("COMMAND: [$text]");
33
34	$file = fopen ($cmdfile, "w");
35	if (fwrite ($file, $datafile) == false) // block until there is a reader
36		throw new Exception ("Can't send command");
37	fclose ($file);
38
39	/* wait for reply, and destroy the reading file */
40	$text = "";
41	$replyfile = get_reply_filename ($_GET['PHPSESSID']);
42	if (! file_exists ($replyfile))
43		throw new Exception ("Bad setup");
44	$file = fopen ($replyfile, "r");
45	$datafile = fread ($file, 8192);
46	fclose ($file);
47	//echo "DATAFILE [$datafile]\n"; flush (); ob_flush();
48
49	if (filesize ($datafile) == 0)
50		throw new Exception ("No reply");
51
52	$file = fopen ($datafile, 'rb');
53	fpassthru ($file);
54	if (isset ($log)) {
55		$tmp = file_get_contents ($datafile);
56		$log->lwrite ("RESPONSE: [$tmp]");
57	}
58
59	@unlink ($datafile);
60
61	echo $text;
62}
63catch (Exception $e) {
64	$reply = new SimpleXMLElement("<reply></reply>");
65	$node = $reply->addChild ('status', "ERROR");
66	$node->addAttribute ("error", $e->getMessage());
67
68	global $init_shared;
69	echo gda_add_hash ($init_shared, $reply->asXml());
70
71	global $datafile;
72	if (isset ($datafile))
73		@unlink ($datafile);
74}
75
76?>
77