-
Notifications
You must be signed in to change notification settings - Fork 156
/
control.php
608 lines (521 loc) · 19.8 KB
/
control.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<?php
// Copyright (C) 2014 Universitätsbibliothek Mannheim
// See file LICENSE for license details.
// Authors: Alexander Wagner, Stefan Weil
// Test whether the script was called directly (used for unit test). Use some
// heuristics to detect whether we are not running in a web application.
if (isset($unittest)) {
$unittest = array();
}
$unittest[__FILE__] = !isset($_SERVER['SERVER_NAME']);
require_once('globals.php');
if (!$unittest[__FILE__]) {
trace("QUERY_STRING=" . $_SERVER['QUERY_STRING']);
}
// Connect to database and get configuration constants.
require_once('DBConnector.class.php');
$db = new palma\DBConnector();
function displayCommand($cmd)
{
if (defined('CONFIG_SSH')) {
$cmd = CONFIG_SSH . " 'DISPLAY=" . CONFIG_DISPLAY . " $cmd'";
} else {
$cmd = "DISPLAY=" . CONFIG_DISPLAY . " HOME=/var/www $cmd";
}
monitor("control.php: displayCommand $cmd");
// add directories with palma-browser to PATH
$result = shell_exec('PATH=/usr/lib/palma:./scripts:$PATH '.$cmd);
trace("displayCommand $cmd, result=$result");
return $result;
}
function wmClose($id)
{
monitor("control.php: wmClose");
// Close window gracefully.
displayCommand("wmctrl -i -c $id");
}
function wmHide($id)
{
monitor("control.php: wmHide");
// Hide window. This is done by moving it to desktop 1.
displayCommand("wmctrl -i -r $id -t 1");
}
function wmShow($id)
{
monitor("control.php: wmShow");
// Show window on current desktop.
displayCommand("wmctrl -i -R $id");
}
function windowListOnScreen()
{
monitor("control.php: windowListOnScreen");
$list = array();
$windows = explode("\n", displayCommand('wmctrl -l'));
foreach ($windows as $w) {
$field = explode(' ', $w);
$id = $field[0];
if ($id != '') {
array_push($list, $id);
}
}
return $list;
}
// simple list with content from database
function windowList()
{
monitor("control.php: windowList");
$list = array();
global $db;
// Get ordered list of all windows from the database.
$windows = $db->getWindows();
foreach ($windows as $w) {
$id = $w['win_id'];
if ($id != '') {
array_push($list, $id);
}
}
return $list;
}
function closeAll()
{
monitor("control.php: closeAll");
global $db;
$windows_on_screen = windowListOnScreen();
foreach ($windows_on_screen as $id) {
wmClose($id);
if ($db->getWindowState($id) != null) {
$db->deleteWindow($id);
}
}
// Remove any remaining window entries in database.
$db->exec('DELETE FROM window');
// Remove any remaining files in the upload directory.
clearUploadDir();
}
function doLogout($username)
{
monitor("control.php: doLogout");
global $db;
if ($username == 'ALL') {
// Terminate all user connections and reset system.
closeAll();
$db->resetTables();
}
}
function clearUploadDir()
{
monitor("control.php: clearUploadDir");
# Remove all files in the upload directory.
if (is_dir(CONFIG_UPLOAD_DIR)) {
if ($dh = opendir(CONFIG_UPLOAD_DIR)) {
while (($file = readdir($dh)) !== false) {
if ($file != "." and $file != "..") {
unlink(CONFIG_UPLOAD_DIR . "/$file");
}
}
closedir($dh);
}
}
}
function setLayout($layout)
{
monitor("control.php: setLayout $layout");
// Set layout of team display. Layouts are specified by their name.
// We use names like g1x1, g2x1, g1x2, ...
// Restore the last layout if the function is called with a null argument.
global $db;
trace("layout $layout");
$geom = array();
$geom['g1x1'] = array(
array(0, 0, 1, 1)
);
$geom['g2x1'] = array(
array(0, 0, 2, 1), array(1, 0, 2, 1)
);
$geom['g1x2'] = array(
array(0, 0, 1, 2), array(0, 1, 1, 2)
);
$geom['g1a2'] = array(
array(0, 0, 2, 1), array(1, 0, 2, 2),
array(1, 1, 2, 2)
);
$geom['g2x2'] = array(
array(0, 0, 2, 2), array(1, 0, 2, 2),
array(0, 1, 2, 2), array(1, 1, 2, 2)
);
if ($layout == null) {
$layout = $db->querySingle("SELECT value FROM setting WHERE key='layout'");
}
// Make sure $layout is valid
if (!array_key_exists($layout, $geom)) {
trace("layout invalid!");
} else {
$db->exec("UPDATE setting SET value='$layout' WHERE key='layout'");
$dim = $geom[$layout];
// Make sure that desktop 0 is selected.
displayCommand('wmctrl -s 0');
// Get width and height of desktop.
$desktops = displayCommand("wmctrl -d");
// $desktop looks like this.
// 0 * DG: 1600x900 VP: 0,0 WA: 0,27 1600x873 Arbeitsfläche 1
$fields = preg_split("/[\n ]+/", $desktops);
$geom = preg_split("/x/", $fields[3]);
$screenWidth = $geom[0];
$screenHeight = $geom[1];
// Show all windows for the current layout which are not disabled.
$maxSection = count($dim);
// Get ordered list of all windows from the database.
$windows = $db->getWindows();
foreach ($windows as $w) {
$id = $w['win_id'];
$enabled = $w['state'] == 'active';
$section = $w['section'];
if ($section >= 1 && $section <= $maxSection && $enabled) {
// Show window, set size and position.
$wi = $section - 1;
$dx = $screenWidth / $dim[$wi][2];
$dy = $screenHeight / $dim[$wi][3];
$x = $dim[$wi][0] * $dx;
$y = $dim[$wi][1] * $dy;
wmShow($id);
displayCommand("wmctrl -i -r $id -e 0,$x,$y,$dx,$dy");
} else {
// Hide window.
wmHide($id);
}
}
}
}
function activateControls($windowhex)
{
global $db;
$fhandler = $db->querySingle("SELECT handler FROM window WHERE win_id='$windowhex'");
trace("activateControls for handler $fhandler");
monitor("control.php: activateControls $fhandler");
}
function addNewWindow($db, $new)
{
// Add a new window to the monitor. This window either uses the first
// unused section or it will be hidden.
monitor('control.php: addNewWindow ' . serialize($new));
trace('addNewWindow ' . serialize($new));
// '$new' already contains 'file', 'handler' and 'date', as well as the
// username for VNC connections only.
// 'win_id', 'section' have to be defined afterwards.
// Get new window. Wait up to 10 s for it.
$t_total = 0;
do {
$window_ids_on_screen = windowListOnScreen();
$windows_in_db = $db->getWindows();
$existing_ids = array();
$new_window_id = '';
if (count($windows_in_db) > 0) {
// Add db windows to existing_ids.
foreach ($windows_in_db as $win) {
$existing_ids[] = $win['win_id'];
}
$new_window = array_diff($window_ids_on_screen, $existing_ids);
foreach ($new_window as $win_id) {
if ($win_id != "") {
$new_window_id = $win_id;
}
}
} elseif (!empty($window_ids_on_screen)) {
$new_window_id = $window_ids_on_screen[0];
}
} while (!$new_window_id && $t_total++ <= 10 && !sleep(1));
if (!$new_window_id) {
trace('warning: no new window found');
return;
}
trace("new window $new_window_id");
// Determine last assigned monitor section.
//~ $max_section = $db->querySingle('SELECT MAX(section) FROM window');
// Get first unused monitor section.
$section = $db->nextID();
// If all information is available, create window object.
$new['id'] = $section;
$new['section'] = $section;
if ($section <= 4) {
$new['state'] = "active";
} else {
// All sections are used, so there is no free one for the new window.
$new['state'] = "inactive";
// We could hide the new window immediately, but don't do it here:
// Each new window will be shown in the middle of the screen.
//~ wmHide($new_window_id);
//~ trace("hide new window $new_window_id");
}
// $new['file'] = $active_window; (?)
// TODO: check how to insert the userid for all content, not just vnc.
// Perhaps better add to array in upload.php ?
$userid = "";
$queryid = $db->querySingle('SELECT user.userid FROM user WHERE user.name="' . $new['userid'] .'"');
if (!empty($queryid)) {
$userid = $queryid;
} else {
$userid = "all";
}
$myWindow = array(
$new['id'],
$new_window_id,
$new['section'],
$new['state'],
$new['file'],
$new['handler'],
$userid,
$new['date']
);
// Save window in database.
$db->insertWindow($myWindow);
}
function createNewWindowSafe($db, $w)
{
$inFile = $w['file'];
if (!file_exists($inFile)) {
trace("".escapeshellarg($inFile)." is not a file, aborting display");
return;
}
require_once('FileHandler.class.php');
list ($handler, $targetFile) =
palma\FileHandler::getFileHandler($inFile);
trace("file is now $targetFile, its handler is $handler");
$window = array(
"id" => "",
"win_id" => "",
"name" => "",
"state" => "",
"file" => $targetFile,
"handler" => $handler,
"userid" => "",
"date" => $w['date']);
createNewWindow($db, $window);
}
function createNewWindow($db, $w)
{
// '$w' already contains 'file', 'handler' and 'date'.
// 'win_id', 'section' have to be defined afterwards.
$handler = $w['handler'];
// TODO: use escapeshellarg() for filename.
$filename = $w['file'];
$cmd = "$handler ".escapeshellarg($filename);
displayCommand("/usr/bin/nohup $cmd >/dev/null 2>&1 &");
addNewWindow($db, $w);
monitor("control.php: createNewWindow");
}
function processRequests($db)
{
monitor("control.php: processRequests");
if (array_key_exists('window', $_REQUEST)) {
// All windows related commands must start with window=.
$windownumber = escapeshellcmd($_REQUEST['window']);
$windowname = false;
$windowhex = 0;
// TODO: $win_id und $windowname können vermutlich zusammengefasst werden.
$win_id = 0;
if ($windownumber != 'vncwin') {
// This is the normal case.
// Special handling is needed when called with window=vncwin, see below.
$window = $windownumber - 1;
$win_id = $db->getWindowIDBySection($windownumber);
$windowlist = windowList();
if (count($windowlist) == 0) {
trace("no window found for command");
} else if (is_numeric($window) && count($windowlist) <= $window) {
trace("window $window is out of bounds");
} else if (!is_numeric($window)) {
trace("unhandled window $window");
} else {
$windowname = $windowlist[$window];
$windowhex = hexdec($windowname);
}
}
if ($windowname && array_key_exists('key', $_REQUEST)) {
$key = escapeshellcmd($_REQUEST['key']);
trace("key '$key' in window '$windownumber'");
wmShow($windowname);
// activateControls($windowhex);
// displayCommand("xdotool windowfocus $windowhex key $key");
// trying mousemove and click for better vnc control
displayCommand("xdotool mousemove --window $windowhex 100 100 " .
"key $key");
}
if ($windowname && array_key_exists('keydown', $_REQUEST)) {
// TODO: keydown is currently mapped to key because we had problems
// with sticking keys (no keyup seen). This should be fixed by a
// better event handling.
$key = escapeshellcmd($_REQUEST['keydown']);
trace("keydown '$key' in window '$windownumber'");
wmShow($windowname);
// activateControls($windowhex);
// displayCommand("xdotool windowfocus $windowhex key $key");
// trying mousemove and click for better vnc control
displayCommand("xdotool mousemove --window $windowhex 100 100 " .
"key $key");
//~ displayCommand("xdotool windowfocus $windowhex keydown $key");
}
if ($windowname && array_key_exists('keyup', $_REQUEST)) {
// TODO: keyup is currently ignored, see comment above.
$key = escapeshellcmd($_REQUEST['keyup']);
trace("keyup '$key' in window '$windownumber'");
// activateControls($windowhex);
//~ wmShow($windowname);
//~ displayCommand("xdotool windowfocus $windowhex keyup $key");
}
if (array_key_exists('delete', $_REQUEST)) {
$delete = addslashes($_REQUEST['delete']);
trace("delete='$delete', close window $windownumber");
// Restrict deletion to files known in the db.
// TODO: check if given file and section match the values in the DB,
// but currently, both those values can be ambiguos
$file_in_db = $db->querySingle("SELECT id FROM window WHERE file='$delete'");
$delete = str_replace(" ", "\ ", $delete);
trace("file in db: $file_in_db");
if ($file_in_db) {
if (file_exists($delete)) {
trace("+++ DELETE FILE FROM WEBINTERFACE +++");
unlink($delete);
} elseif ($delete == "VNC") {
trace("+++ DELETE VNC Client FROM DAEMON +++");
// call via daemon: ?window=vncwin&delete=VNC&vncid=123
trace("vnc delete in control");
$win_id = escapeshellcmd($_REQUEST['vncid']); // = hexWindow in database, but not on screen
trace("VNC via Daemon ... id=$win_id");
} elseif (strstr($delete, "http")) {
trace("+++ DELETE Browserwindow +++");
} elseif (preg_match('/(^\w{3,}@\w{1,})/', $delete)) {
trace("+++ DELETE VNC Client FROM WEBINTERFACE +++");
// call via webinterface
$win_id = $db->querySingle("SELECT win_id FROM window WHERE file='$delete' AND handler='vnc'");
trace("DELETE VNC Window with ID=$win_id FROM Database ::
SELECT win_id FROM window WHERE file='$delete' AND handler='vnc'");
} else {
trace("Unhandled delete for '$delete'");
}
wmClose($win_id);
$db->deleteWindow($win_id);
} else {
trace("Given file not present in database!");
}
}
if (array_key_exists('closeOrphans', $_REQUEST)) {
// win_ids in db
$windows_in_db = $db->getWindows();
$db_ids = array();
if (count($windows_in_db) > 0) {
foreach ($windows_in_db as $win) {
array_push($db_ids, $win['win_id']);
}
}
// win_ids on screen
$screen_ids = windowListOnScreen();
// orphaned windows
$orphan_ids = array_diff($screen_ids, $db_ids);
if (count($orphan_ids) > 0) {
// close windows on screen not existing in database
foreach ($orphan_ids as $id) {
wmClose($id);
}
}
}
if (array_key_exists('toggle', $_REQUEST)) {
// Change window state from visible to invisible and vice versa.
$state = $db->getWindowState($win_id);
trace("toggle window $windownumber, id=$win_id, state=$state");
if ($state == "active") {
wmHide($win_id);
$db->setWindowState($win_id, "inactive");
} else {
wmShow($win_id);
$db->setWindowState($win_id, "active");
}
}
} elseif (array_key_exists('layout', $_REQUEST)) {
setLayout(escapeshellcmd($_REQUEST['layout']));
} elseif (array_key_exists('logout', $_REQUEST)) {
doLogout($_REQUEST['logout']);
} elseif (array_key_exists('newVncWindow', $_REQUEST)) {
// TODO: Better write new code for VNC window.
addNewWindow($db, unserialize(urldecode($_REQUEST['newVncWindow'])));
} elseif (array_key_exists('newWindow', $_REQUEST)) {
createNewWindowSafe($db, unserialize(urldecode($_REQUEST['newWindow'])));
}
if (array_key_exists('switchWindows', $_REQUEST)) {
$before = escapeshellcmd($_REQUEST['before']);
$after = escapeshellcmd($_REQUEST['after']);
trace("switching $before and $after");
// exchange section
$win_id1 = $db->getWindowIDBySection($before);
$win_id2 = $db->getWindowIDBySection($after);
$db->updateWindow($win_id1, 'section', $after);
$db->updateWindow($win_id2, 'section', $before);
trace("++updating database $win_id1 section=$after");
trace("++updating database $win_id2 section=$before");
// Update display (no layout change).
setLayout(null);
}
if (array_key_exists('openURL', $_REQUEST)) {
$openURL = escapeshellcmd($_REQUEST['openURL']);
trace("openURL $openURL");
// If URL leads to pdf file, download it and treat as upload
$headers = get_headers($openURL, 1);
if ($headers["Content-Type"] == "application/pdf") {
trace("url seems to lead to a pdf file, so downloading it...");
$temp_name = basename($openURL);
$temp_dir = "/tmp";
file_put_contents("$temp_dir/$temp_name", file_get_contents($openURL));
$mimetype = mime_content_type("$temp_dir/$temp_name");
trace("mimetype is $mimetype");
if ($mimetype == "application/pdf") {
$_FILES['file']['name'] = "$temp_name";
$_FILES['file']['tmp_name'] = "$temp_dir/$temp_name";
$_FILES['file']['error'] = "downloaded_from_url";
trace("Handing over to upload.php");
include 'upload.php';
} else {
trace("Deleting file!");
unlink("$temp_dir/$temp_name");
}
} else {
$dt = new DateTime();
$date = $dt->format('Y-m-d H:i:s');
$window = array(
"id" => "",
"win_id" => "",
"section" => "",
"state" => "",
"file" => $openURL,
"handler" => "palma-browser",
"userid" => "",
"date" => $date
);
createNewWindow($db, $window);
}
}
// TODO: check if query redundant?
if (array_key_exists('closeAll', $_REQUEST)) {
$close = $_REQUEST['closeAll'];
trace("close all windows $close");
closeAll();
}
} // processRequests
if ($db->checkPermission() || $unittest[__FILE__]) {
processRequests($db);
}
if ($unittest[__FILE__]) {
// Experimental: Get function call from startx.
parse_str(implode('&', array_slice($argv, 1)), $_GET);
if (isset($_GET) && count($_GET) > 0) {
foreach ($_GET as $key => $value) {
// Only defined actions allowed.
if ($key == "doLogout") {
doLogout($value);
}
}
} else {
// Run unit test.
echo("<p>Running unit test</p>");
trace("Running unit test for " . __FILE__);
trace("Finished unit test");
}
}