summaryrefslogtreecommitdiffstats
path: root/application/libraries/ProcRunner.php
blob: 6aaaa1f204c210c86460a9530119a07e5892c497 (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
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
<?php
/*
 * Copyright 2015 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace libraries;

class ProcRunner {
	private $cmd;
	private $input = NULL;
	private $forbid_nonzero = false;
	private $forbid_stderr = false;

	/**
	 * This function automatically escapes all parameters before executing the command.
	 *
	 * @param cmd array with the command and it's arguments
	 */
	function __construct($cmd)
	{
		assert(is_array($cmd));
		$this->cmd = implode(" ", array_map('escapeshellarg', $cmd));
	}

	/**
	 * Set stdin. You will have to set this to NULL if you call exec() a second
	 * time and don't want stdin to be sent again
	 *
	 * @param input string to send via stdin
	 */
	function input($input)
	{
		$this->input = $input;
		return $this;
	}

	function forbid_nonzero()
	{
		$this->forbid_nonzero = true;
		return $this;
	}


	function forbid_stderr()
	{
		$this->forbid_stderr = true;
		return $this;
	}

	/**
	 * Run the command.
	 *
	 * @return array with keys return_code, stdout, stderr
	 */
	function exec()
	{
		$descriptorspec = array(
			1 => array('pipe', 'w'),
			2 => array('pipe', 'w'),
		);

		if ($this->input !== NULL) {
			$descriptorspec[0] = array('pipe', 'r');
		}

		$proc = proc_open($this->cmd, $descriptorspec, $pipes);

		if ($proc === false) {
			throw new \exceptions\ApiException('procrunner/proc_open-failed',
				'Failed to run process',
				array($this->cmd, $this->input)
			);
		}

		if ($this->input !== NULL) {
			fwrite($pipes[0], $this->input);
			fclose($pipes[0]);
		}

		$stdout = stream_get_contents($pipes[1]);
		fclose($pipes[1]);
		assert($stdout !== false);

		$stderr = stream_get_contents($pipes[2]);
		fclose($pipes[2]);
		assert($stderr !== false);

		$return_code = proc_close($proc);

		$ret = array(
			"return_code" => $return_code,
			"stdout" => $stdout,
			"stderr" => $stderr,
		);

		if ($this->forbid_nonzero && $return_code !== 0) {
			throw new \exceptions\ApiException('procrunner/non-zero-exit',
				'Process exited with a non-zero status',
				array($this->cmd, $this->input, $ret)
			);
		}

		if ($this->forbid_stderr && $stderr !== "") {
			throw new \exceptions\ApiException('procrunner/stderr',
				'Output on stderr not allowed but received',
				array($this->cmd, $this->input, $ret)
			);
		}

		return $ret;
	}

	/**
	 * Run the command and enable some sanity checks such as empty stderr and
	 * zero exit status. Might enable more in the future.
	 *
	 * @See exec
	 */
	function execSafe()
	{
		$this->forbid_stderr();
		$this->forbid_nonzero();
		return $this->exec();
	}
}