|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Plugin\AbstractPackage; |
|
6
|
|
|
use Lagdo\DbAdmin\Ajax\App\Admin; |
|
7
|
|
|
|
|
8
|
|
|
use function in_array; |
|
9
|
|
|
use function is_array; |
|
10
|
|
|
use function realpath; |
|
11
|
|
|
use function Jaxon\cl; |
|
12
|
|
|
use function Jaxon\rq; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Jaxon DbAdmin package |
|
16
|
|
|
*/ |
|
17
|
|
|
class DbAdminPackage extends AbstractPackage |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Get the path to the config file |
|
21
|
|
|
* |
|
22
|
|
|
* @return string|array |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function config(): string |
|
25
|
|
|
{ |
|
26
|
|
|
return realpath(__DIR__ . '/../config/dbadmin.php'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the database servers |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getServers(): array |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->getOption('servers', []); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the name of a given server |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $server The server name in the configuration |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getServerName(string $server): string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->getOption("servers.$server.name", ''); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get a given server options |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $server The server name in the configuration |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getServerOptions(string $server): array |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->getOption("servers.$server", []); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the driver of a given server |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $server The server name in the configuration |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getServerDriver(string $server): string |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->getOption("servers.$server.driver", ''); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Check if the user has access to a server |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $server The database server |
|
79
|
|
|
* |
|
80
|
|
|
* return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getServerAccess(string $server): bool |
|
83
|
|
|
{ |
|
84
|
|
|
// Check in server options |
|
85
|
|
|
$serverAccess = $this->getOption("servers.$server.access.server", null); |
|
86
|
|
|
if($serverAccess === true || $serverAccess === false) |
|
87
|
|
|
{ |
|
88
|
|
|
return $serverAccess; |
|
89
|
|
|
} |
|
90
|
|
|
// Check in global options |
|
91
|
|
|
return $this->getOption('access.server', true) === true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function hasLoggingDatabase(): bool |
|
98
|
|
|
{ |
|
99
|
|
|
$options = $this->getOption('logging.database'); |
|
100
|
|
|
return is_array($options) && isset($options['driver']) && |
|
101
|
|
|
in_array($options['driver'], ['pgsql', 'mysql', 'sqlite']); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the HTML tags to include CSS code and files into the page |
|
106
|
|
|
* |
|
107
|
|
|
* The code must be enclosed in the appropriate HTML tags. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getCss(): string |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->view()->render('dbadmin::codes::css.html') . "\n<style>\n" . |
|
114
|
|
|
$this->view()->render('dbadmin::codes::styles.css') . "\n</style>\n"; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the HTML tags to include javascript code and files into the page |
|
119
|
|
|
* |
|
120
|
|
|
* The code must be enclosed in the appropriate HTML tags. |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getJs(): string |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->view()->render('dbadmin::codes::js.html'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get the javascript code to include into the page |
|
131
|
|
|
* |
|
132
|
|
|
* The code must NOT be enclosed in HTML tags. |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getScript(): string |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->view()->render('dbadmin::codes::script.js'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get the javascript code to include into the page |
|
143
|
|
|
* |
|
144
|
|
|
* The code must NOT be enclosed in HTML tags. |
|
145
|
|
|
* |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getReadyScript(): string |
|
149
|
|
|
{ |
|
150
|
|
|
$defaultServer = $this->getOption('default'); |
|
151
|
|
|
return !$defaultServer || |
|
152
|
|
|
!$this->getOption("servers.$defaultServer") ? '' : |
|
153
|
|
|
rq(Admin::class)->server($defaultServer); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get the HTML code of the package home page |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getHtml(): string |
|
162
|
|
|
{ |
|
163
|
|
|
return cl(Admin::class)->html(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|