summaryrefslogtreecommitdiff
path: root/shell/exec.php
blob: 9c22e5b81c6a575ab73c2159d71feeddcb7a07ef (plain)
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
<?php

function lts_chdir($dir) {
	$ret = chdir($dir);
	echo '<input type="hidden" name="d" value="'.getcwd().'" />';
	if ($ret == false) { echo 'chdir: unable to change directories: `'.$dir."'\n";
	return $ret;
}

abstract class prog { public static abstract function main($args, $env); }

function lts_shell_exec($com, $env) {
	if ($env['CWD'] != '') { lts_chdir($env['CWD']); }
	if ($com=='') { return 0; }

	$coms = array();
	$stdout_dest = array();
	
	// Parse command(s)
	$a = 0; $c = 0; $q = '';
	while ($com != '') {
		$char = substr($com,0,1);
		 $com = substr($com,1);
		if (substr_count ('\'',$char)!==0) {
			if (substr($q,0,1)===$char) {
				$q = substr($q,1);
			} else {
				$q = $char.$q;
			}
		} elseif ($q != '') {
			$coms[$c][$a].=$char;
		} elseif (substr_count ($env['IFS'],$char)!==0) {
			if (isset($coms[$c][$a])) {
				$a++;
			}
		} elseif ($char==';') {
			if (!isset($stdout_dest[$c])) {
				$stdout_dest[$c] = '/dev/stdout';
			}
			$c++; $a=0;
		} elseif ($char=='|') {
			$stdout_dest[$c] = '/dev/stdin';
			$c++; $a=0;
		} else {
			$coms[$c][$a].=$char;
		}
	}
	if (!isset($stdout_dest[$c])) {
		$stdout_dest[$c] = '/dev/stdout';
	}
	
	// execude commands
	$ret=0;
	foreach ($coms as $key => $args) {
		if ($stdout_dest[$key] != '/dev/stdout') {
			ob_start();
		}
		
		lts_exec($args, $env);
		
		if ($stdout_dest[$key] == '/dev/stdout') {
			unset($_POST['stdin']);
		} else {
			switch ($stdout_dest[$key]) {
				case '/dev/stdin': $_POST['stdin']=ob_get_contents(); break;
				default: file_put_contents($stdout_dest[$key],ob_get_contents()); break;
			}
			ob_end_clean();
		}
	}
	return $ret;
}

function lts_exec($args, $env) {
	if (!class_exists('p_'.$args[0])) {
		$file=$env['PATH'].'/'.$args[0].'.php';
		if (file_exists($file)) {
			include($file);
		}
	}
	if (class_exists('p_'.$args[0])) {
		$ret = call_user_func(array('p_'.$args[0],'main'),$args,$env);
	} else {
		echo 'lts_exec: command not found: `'.$args[0]."'\n";
		$ret = 1;
	}
	return $ret;
}