|
1
|
|
|
<?php namespace VojtaSvoboda\WebArtisan\Classes; |
|
2
|
|
|
|
|
3
|
|
|
use Artisan; |
|
4
|
|
|
use Config; |
|
5
|
|
|
use VojtaSvoboda\WebArtisan\Models\Settings; |
|
6
|
|
|
|
|
7
|
|
|
class CommandRunner |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var array $allowedCommands List of allowed commands loaded from Config. */ |
|
10
|
|
|
protected $allowedCommands; |
|
11
|
|
|
|
|
12
|
|
|
/** @var array $allowedPluginCommands List of allowed plugin commands. */ |
|
13
|
|
|
protected $allowedPluginCommands; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Settings $settings */ |
|
16
|
|
|
protected $settings; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* CommandRunner constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param Settings $settings |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(Settings $settings) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->settings = $settings; |
|
26
|
|
|
$this->allowedCommands = Config::get('vojtasvoboda.webartisan::allowedCommands'); |
|
27
|
|
|
$this->allowedPluginCommands = Config::get('vojtasvoboda.webartisan::allowedPluginCommands'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Run command and return true if Ok or string with error. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $command |
|
34
|
|
|
* @param string $hash |
|
35
|
|
|
* |
|
36
|
|
|
* @return bool|string |
|
37
|
|
|
*/ |
|
38
|
|
View Code Duplication |
public function run($command, $hash) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
// check command and hash |
|
41
|
|
|
$check = $this->check($command, $hash); |
|
42
|
|
|
if ($check !== true) { |
|
43
|
|
|
return $check; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// run command |
|
47
|
|
|
Artisan::call($command); |
|
48
|
|
|
|
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Run plugin command and return true if Ok or string with error. |
|
54
|
|
|
* |
|
55
|
|
|
* @param $command |
|
56
|
|
|
* @param $plugin |
|
57
|
|
|
* @param $hash |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool|string |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
public function runForPlugin($command, $plugin, $hash) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
// check command, plugin and hash |
|
64
|
|
|
$check = $this->check($command, $hash, $plugin); |
|
65
|
|
|
if ($check !== true) { |
|
66
|
|
|
return $check; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// run command |
|
70
|
|
|
Artisan::call($command, [ |
|
71
|
|
|
'name' => $plugin, |
|
72
|
|
|
]); |
|
73
|
|
|
|
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Run queued command and return true if Ok or string with error. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $command |
|
81
|
|
|
* @param string $hash |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool|string |
|
84
|
|
|
*/ |
|
85
|
|
View Code Duplication |
public function runQueued($command, $hash) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
// check command and hash |
|
88
|
|
|
$check = $this->check($command, $hash); |
|
89
|
|
|
if ($check !== true) { |
|
90
|
|
|
return $check; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// run queued command |
|
94
|
|
|
Artisan::queue($command); |
|
95
|
|
|
|
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Check command and hash. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $command Command identifier. |
|
103
|
|
|
* @param string $hash Security hash. |
|
104
|
|
|
* @param string $plugin Plugin identifier (optional). |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool|string |
|
107
|
|
|
*/ |
|
108
|
|
|
public function check($command, $hash, $plugin = null) |
|
109
|
|
|
{ |
|
110
|
|
|
// check commands whitelist |
|
111
|
|
|
if ($plugin === null && !in_array($command, $this->allowedCommands)) { |
|
112
|
|
|
return 'This command is not allowed.'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// check plugin commands whitelist |
|
116
|
|
|
if ($plugin !== null && !in_array($command, $this->allowedPluginCommands)) { |
|
117
|
|
|
return 'This command is not allowed.'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// get hash |
|
121
|
|
|
$localHash = $this->settings->get('hash'); |
|
122
|
|
|
|
|
123
|
|
|
// if hash not set |
|
124
|
|
|
if (strlen($localHash) < 16) { |
|
125
|
|
|
return 'You have to set controll hash at backend settings and it should be at least 16 characters length.'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// if hash doesn't match |
|
129
|
|
|
if ($hash != $localHash) { |
|
130
|
|
|
return 'Wrong control hash. Check backend settings.'; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return true; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.