1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Applications; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\HTTPRequest\Generic; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @property mixed stream |
8
|
|
|
* @package Applications |
9
|
|
|
* @subpackage CGI |
10
|
|
|
* |
11
|
|
|
* @author Vasily Zorin <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class CGIRequest extends Generic |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
public $terminateOnAbort = false; |
19
|
|
|
/** |
20
|
|
|
* @var \PHPDaemon\Core\ShellCommand |
21
|
|
|
*/ |
22
|
|
|
public $proc; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function init() |
29
|
|
|
{ |
30
|
|
|
$this->header('Content-Type: text/html'); // default header. |
31
|
|
|
|
32
|
|
|
$this->proc = new \PHPDaemon\Core\ShellCommand(); |
33
|
|
|
$this->proc->readPacketSize = $this->appInstance->readPacketSize; |
|
|
|
|
34
|
|
|
$this->proc->onReadData([$this, 'onReadData']); |
|
|
|
|
35
|
|
|
$this->proc->onWrite([$this, 'onWrite']); |
|
|
|
|
36
|
|
|
$this->proc->binPath = $this->appInstance->binPath; |
|
|
|
|
37
|
|
|
$this->proc->chroot = $this->appInstance->chroot; |
|
|
|
|
38
|
|
|
|
39
|
|
|
if (isset($this->attrs->server['BINPATH'])) { |
40
|
|
|
if (isset($this->appInstance->binAliases[$this->attrs->server['BINPATH']])) { |
41
|
|
|
$this->proc->binPath = $this->appInstance->binAliases[$this->attrs->server['BINPATH']]; |
|
|
|
|
42
|
|
|
} elseif ($this->appInstance->config->allowoverridebinpath->value) { |
43
|
|
|
$this->proc->binPath = $this->attrs->server['BINPATH']; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
if (isset($this->attrs->server['CHROOT']) && $this->appInstance->config->allowoverridechroot->value) { |
|
|
|
|
48
|
|
|
$this->proc->chroot = $this->attrs->server['CHROOT']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
if (isset($this->attrs->server['SETUSER']) && $this->appInstance->config->allowoverrideuser->value) { |
|
|
|
|
52
|
|
|
$this->proc->setUser = $this->attrs->server['SETUSER']; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
if (isset($this->attrs->server['SETGROUP']) && $this->appInstance->config->allowoverridegroup->value) { |
|
|
|
|
56
|
|
|
$this->proc->setGroup = $this->attrs->server['SETGROUP']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (isset($this->attrs->server['CWD']) && $this->appInstance->config->allowoverridecwd->value) { |
60
|
|
|
$this->proc->cwd = $this->attrs->server['CWD']; |
61
|
|
|
} elseif ($this->appInstance->config->cwd->value !== null) { |
62
|
|
|
$this->proc->cwd = $this->appInstance->config->cwd->value; |
63
|
|
|
} else { |
64
|
|
|
$this->proc->cwd = dirname($this->attrs->server['SCRIPT_FILENAME']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->proc->setArgs([$this->attrs->server['SCRIPT_FILENAME']]); |
68
|
|
|
$this->proc->setEnv($this->attrs->server); |
69
|
|
|
$this->proc->execute(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Called when request iterated. |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function run() |
77
|
|
|
{ |
78
|
|
|
if (!$this->proc) { |
79
|
|
|
$this->out('Couldn\'t execute CGI proccess.'); |
80
|
|
|
$this->finish(); |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
if (!$this->proc->eof()) { |
84
|
|
|
$this->sleep(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Called when the request aborted. |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function onAbort() |
93
|
|
|
{ |
94
|
|
|
if ($this->terminateOnAbort && $this->stream) { |
95
|
|
|
$this->stream->close(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Called when new data received from process. |
101
|
|
|
* @param object $process Process pointer. |
102
|
|
|
* @param string $data Data. |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function onReadData($process, $data) |
106
|
|
|
{ |
107
|
|
|
$this->combinedOut($data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Called when new piece of request's body is received. |
112
|
|
|
* @param string $c Piece of request's body. |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function stdin($c) |
116
|
|
|
{ |
117
|
|
|
if ($c === '') { |
118
|
|
|
$this->onWrite($this->proc); |
119
|
|
|
} else { |
120
|
|
|
$this->proc->write($c); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Called when the request aborted. |
126
|
|
|
* @param \PHPDaemon\Core\ShellCommand $process |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function onWrite($process) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
if ($this->attrs->stdin_done && ($this->proc->writeState === false)) { |
|
|
|
|
132
|
|
|
$this->proc->closeWrite(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.