|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Applications\GibsonREST; |
|
3
|
|
|
|
|
4
|
|
|
class Request extends \PHPDaemon\HTTPRequest\Generic |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
protected $result; |
|
8
|
|
|
protected $cmd; |
|
9
|
|
|
protected $args; |
|
10
|
|
|
protected $performed = false; |
|
11
|
|
|
|
|
12
|
|
|
/* |
|
13
|
|
|
* Constructor. |
|
14
|
|
|
* @return void |
|
15
|
|
|
*/ |
|
16
|
|
|
public function init() |
|
17
|
|
|
{ |
|
18
|
|
|
try { |
|
19
|
|
|
$this->header('Content-Type: text/plain'); |
|
20
|
|
|
//$this->header('Content-Type: application/x-json'); |
|
|
|
|
|
|
21
|
|
|
} catch (\Exception $e) { |
|
22
|
|
|
} |
|
23
|
|
|
if (!$this->importCmdArgs()) { |
|
24
|
|
|
return; |
|
25
|
|
|
} |
|
26
|
|
|
$this->sleep(5, true); // setting timeout 5 seconds */ |
|
27
|
|
|
$this->onSessionStart(function () { |
|
|
|
|
|
|
28
|
|
|
$this->wakeup(); |
|
29
|
|
|
if ($this->cmd === 'LOGIN') { |
|
30
|
|
|
if (sizeof($this->args) !== 2) { |
|
31
|
|
|
$this->result = ['$err' => 'You must pass exactly 2 arguments.']; |
|
32
|
|
|
$this->wakeup(); |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
if ((hash_equals($this->appInstance->config->username->value, |
|
36
|
|
|
$this->args[0]) + hash_equals($this->appInstance->config->password->value, |
|
37
|
|
|
$this->args[1])) < 2) { |
|
38
|
|
|
$this->result = ['$err' => 'Wrong username and/or password.']; |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
$this->attrs->session['logged'] = $this->appInstance->config->credver; |
|
42
|
|
|
$this->result = ['$ok' => 1]; |
|
43
|
|
|
$this->wakeup(); |
|
44
|
|
|
return; |
|
45
|
|
|
} elseif ($this->cmd === 'LOGOUT') { |
|
46
|
|
|
unset($this->attrs->session['logged']); |
|
47
|
|
|
$this->result = ['$ok' => 1]; |
|
48
|
|
|
$this->wakeup(); |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
if (!isset($this->attrs->session['logged']) || $this->attrs->session['logged'] < $this->appInstance->config->credver) { |
|
|
|
|
|
|
52
|
|
|
$this->result = ['$err' => 'You must be authenticated.']; |
|
53
|
|
|
$this->wakeup(); |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
}); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/* |
|
60
|
|
|
* Import command name and arguments from input |
|
61
|
|
|
* @return void |
|
|
|
|
|
|
62
|
|
|
*/ |
|
63
|
|
|
protected function importCmdArgs() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->cmd = static::getString($_GET['cmd']); |
|
66
|
|
|
if ($this->cmd === '') { |
|
67
|
|
|
$e = explode('/', |
|
68
|
|
|
isset($this->attrs->server['SUBPATH']) ? $this->attrs->server['SUBPATH'] : $this->attrs->server['DOCUMENT_URI']); |
|
|
|
|
|
|
69
|
|
|
$this->cmd = array_shift($e); |
|
70
|
|
|
$this->args = sizeof($e) ? array_map('urldecode', $e) : []; |
|
71
|
|
|
} |
|
72
|
|
|
if (!$this->appInstance->gibson->isCommand($this->cmd) && $this->cmd !== 'LOGIN' && $this->cmd !== 'LOGOUT') { |
|
|
|
|
|
|
73
|
|
|
$this->result = ['$err' => 'Unrecognized command']; |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/* |
|
80
|
|
|
* Import command name and arguments from input |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Called when request iterated. |
|
86
|
|
|
* @return integer Status. |
|
|
|
|
|
|
87
|
|
|
*/ |
|
88
|
|
|
public function run() |
|
89
|
|
|
{ |
|
90
|
|
|
if (!$this->performed && $this->result === null) { |
|
91
|
|
|
$this->performed = true; |
|
92
|
|
|
$this->importCmdArgsFromPost(); |
|
93
|
|
|
$this->performCommand(); |
|
94
|
|
|
if ($this->result === null) { |
|
95
|
|
|
$this->sleep(5); |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
echo json_encode($this->result); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/* |
|
103
|
|
|
* Performs command |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
|
|
107
|
|
|
protected function importCmdArgsFromPost() |
|
108
|
|
|
{ |
|
109
|
|
|
if ($this->result === null) { |
|
110
|
|
|
foreach (static::getArray($_POST['args']) as $arg) { |
|
111
|
|
|
if (is_string($arg)) { |
|
112
|
|
|
$this->args[] = $arg; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function performCommand() |
|
119
|
|
|
{ |
|
120
|
|
|
$args = $this->args; |
|
121
|
|
|
$args[] = function ($conn) { |
|
122
|
|
|
if (!$conn->isFinal()) { |
|
123
|
|
|
return; |
|
124
|
|
|
} |
|
125
|
|
|
$this->result = $conn->result; |
|
126
|
|
|
$this->wakeup(); |
|
127
|
|
|
}; |
|
128
|
|
|
$func = [$this->appInstance->gibson, $this->cmd]; |
|
|
|
|
|
|
129
|
|
|
$func(...$args); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.