|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @title Tool Controller |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pierre-Henry Soria <[email protected]> |
|
6
|
|
|
* @copyright (c) 2012-2019, Pierre-Henry Soria. All Rights Reserved. |
|
7
|
|
|
* @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory. |
|
8
|
|
|
* @package PH7 / App / System / Module / Admin / Controller |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace PH7; |
|
12
|
|
|
|
|
13
|
|
|
use PH7\Framework\Cache\Cache; |
|
14
|
|
|
use PH7\Framework\Date\CDateTime; |
|
15
|
|
|
use PH7\Framework\Layout\Gzip\Gzip; |
|
16
|
|
|
use PH7\Framework\Layout\Html\Design; |
|
17
|
|
|
use PH7\Framework\Layout\Html\Security as HtmlSecurity; |
|
18
|
|
|
use PH7\Framework\Layout\Tpl\Engine\PH7Tpl\PH7Tpl; |
|
19
|
|
|
use PH7\Framework\Mvc\Model\Engine\Db; |
|
|
|
|
|
|
20
|
|
|
use PH7\Framework\Mvc\Model\Engine\Util\Backup; |
|
21
|
|
|
use PH7\Framework\Mvc\Router\Uri; |
|
22
|
|
|
use PH7\Framework\Security\CSRF\Token; |
|
23
|
|
|
use PH7\Framework\Url\Header; |
|
24
|
|
|
|
|
25
|
|
|
class ToolController extends Controller |
|
26
|
|
|
{ |
|
27
|
|
|
const BACKUP_FILE_EXTS = ['.sql', '.gz']; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
private $sTitle; |
|
31
|
|
|
|
|
32
|
|
|
public function index() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->sTitle = t('General Tools'); |
|
35
|
|
|
$this->view->page_title = $this->sTitle; |
|
36
|
|
|
$this->view->h1_title = $this->sTitle; |
|
37
|
|
|
|
|
38
|
|
|
$this->output(); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function cache() |
|
42
|
|
|
{ |
|
43
|
|
|
// Add a CSRF token for the remove ajax cache request |
|
44
|
|
|
$this->view->csrf_token = (new Token)->generate('cache'); |
|
45
|
|
|
|
|
46
|
|
|
$this->addGeneralCssFile(); |
|
47
|
|
|
|
|
48
|
|
|
// Add JS file for the ajax cache feature |
|
49
|
|
|
$this->design->addJs( |
|
50
|
|
|
PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_JS, |
|
51
|
|
|
'common.js' |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$this->sTitle = t('Caches Management'); |
|
55
|
|
|
$this->view->page_title = $this->sTitle; |
|
56
|
|
|
$this->view->h1_title = $this->sTitle; |
|
57
|
|
|
|
|
58
|
|
|
$this->view->aChartData = [ |
|
59
|
|
|
['title' => t('Database and Other Data'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . Cache::CACHE_DIR)], |
|
60
|
|
|
['title' => t('Server Code Template'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . PH7Tpl::COMPILE_DIR)], |
|
61
|
|
|
['title' => t('HTML Template'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . PH7Tpl::CACHE_DIR)], |
|
62
|
|
|
['title' => t('Static Files'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . Gzip::CACHE_DIR)] |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
$this->output(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function cacheConfig() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->sTitle = t('Cache Settings'); |
|
71
|
|
|
$this->view->page_title = $this->sTitle; |
|
72
|
|
|
$this->view->h1_title = $this->sTitle; |
|
73
|
|
|
|
|
74
|
|
|
$this->output(); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function freeSpace() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->addGeneralCssFile(); |
|
80
|
|
|
|
|
81
|
|
|
$this->sTitle = t('Free Space of Server'); |
|
82
|
|
|
$this->view->page_title = $this->sTitle; |
|
83
|
|
|
$this->view->h1_title = $this->sTitle; |
|
84
|
|
|
|
|
85
|
|
|
$this->view->aChartData = [ |
|
86
|
|
|
['title' => t('Public Root'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_ROOT)], |
|
87
|
|
|
['title' => t('Public data'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_PUBLIC_DATA)], |
|
88
|
|
|
['title' => t('Protected Root'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_PROTECTED)], |
|
89
|
|
|
['title' => t('Protected data'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_DATA)] |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
$this->output(); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function envMode() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->sTitle = t('Environment Mode'); |
|
98
|
|
|
$this->view->page_title = $this->sTitle; |
|
99
|
|
|
$this->view->h1_title = $this->sTitle; |
|
100
|
|
|
|
|
101
|
|
|
$this->output(); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function blockCountry() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->view->page_title = t('Country Blacklist'); |
|
107
|
|
|
$this->view->h1_title = t('Block Countries'); |
|
108
|
|
|
|
|
109
|
|
|
$this->output(); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function backup() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->addGeneralCssFile(); |
|
115
|
|
|
|
|
116
|
|
|
$this->view->designSecurity = new HtmlSecurity; // Security Design Class |
|
117
|
|
|
|
|
118
|
|
|
$this->sTitle = t('Backup Management'); |
|
119
|
|
|
$this->view->page_title = $this->sTitle; |
|
120
|
|
|
$this->view->h1_title = $this->sTitle; |
|
121
|
|
|
|
|
122
|
|
|
$aDumpList = $this->file->getFileList(PH7_PATH_BACKUP_SQL, static::BACKUP_FILE_EXTS); |
|
123
|
|
|
|
|
124
|
|
|
// Removing the path |
|
125
|
|
|
$aDumpList = array_map(function ($sValue) { |
|
126
|
|
|
return str_replace(PH7_PATH_BACKUP_SQL, '', $sValue); |
|
127
|
|
|
}, $aDumpList); |
|
128
|
|
|
$this->view->aDumpList = $aDumpList; |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
$oSecurityToken = new Token; |
|
132
|
|
|
|
|
133
|
|
|
if ($this->httpRequest->postExists('backup')) { |
|
134
|
|
|
if (!$oSecurityToken->check('backup')) { |
|
135
|
|
|
$this->design->setFlashMsg(Form::errorTokenMsg(), Design::ERROR_TYPE); |
|
136
|
|
|
} else { |
|
137
|
|
|
// Clean the site name to avoid bug with the backup path |
|
138
|
|
|
$sSiteName = str_replace([' ', '/', '\\'], '_', $this->registry->site_name); |
|
139
|
|
|
$sCurrentDate = (new CDateTime)->get()->date(); |
|
140
|
|
|
|
|
141
|
|
|
switch ($this->httpRequest->post('backup_type')) { |
|
142
|
|
|
case 'server': |
|
143
|
|
|
$sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . $sCurrentDate . '.sql'; |
|
144
|
|
|
(new Backup($sFullPath))->back()->save(); |
|
145
|
|
|
$this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath); |
|
146
|
|
|
break; |
|
147
|
|
|
|
|
148
|
|
|
case 'server_archive': |
|
149
|
|
|
$sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . $sCurrentDate . '.sql.gz'; |
|
150
|
|
|
(new Backup($sFullPath))->back()->saveArchive(); |
|
151
|
|
|
$this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath); |
|
152
|
|
|
break; |
|
153
|
|
|
|
|
154
|
|
|
case 'client': |
|
155
|
|
|
(new Backup($sSiteName . '_' . $sCurrentDate . '.sql'))->back()->download(); |
|
156
|
|
|
break; |
|
157
|
|
|
|
|
158
|
|
|
case 'client_archive': |
|
159
|
|
|
(new Backup($sSiteName . '_' . $sCurrentDate . '.sql.gz'))->back()->downloadArchive(); |
|
160
|
|
|
break; |
|
161
|
|
|
|
|
162
|
|
|
case 'show': |
|
163
|
|
|
$this->view->sql_content = (new Backup)->back()->show(); |
|
164
|
|
|
break; |
|
165
|
|
|
|
|
166
|
|
|
default: |
|
167
|
|
|
$this->design->setFlashMsg( |
|
168
|
|
|
t('Please select a field.'), |
|
169
|
|
|
Design::ERROR_TYPE |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if ($this->httpRequest->postExists('restore_dump')) { |
|
176
|
|
|
if (!$oSecurityToken->check('backup')) { |
|
177
|
|
|
$this->design->setFlashMsg(Form::errorTokenMsg(), Design::ERROR_TYPE); |
|
178
|
|
|
} else { |
|
179
|
|
|
$sDumpFile = $this->httpRequest->post('dump_file'); |
|
180
|
|
|
|
|
181
|
|
|
if (!empty($sDumpFile)) { |
|
182
|
|
|
if ($this->file->getFileExt($sDumpFile) === Backup::SQL_FILE_EXT) { |
|
183
|
|
|
$mStatus = (new Backup($sDumpFile))->restore(); |
|
184
|
|
|
} elseif ($this->file->getFileExt($sDumpFile) === Backup::ARCHIVE_FILE_EXT) { |
|
185
|
|
|
$mStatus = (new Backup(PH7_PATH_BACKUP_SQL . $sDumpFile))->restoreArchive(); |
|
186
|
|
|
} else { |
|
187
|
|
|
$mStatus = t('Dump file must be a SQL type (extension ".sql" or compressed archive ".gz")'); |
|
188
|
|
|
} |
|
189
|
|
|
} else { |
|
190
|
|
|
$mStatus = t('Please select a dump file.'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$sMsg = ($mStatus === true) ? t('Data successfully restored from server!') : $mStatus; |
|
194
|
|
|
$sMsgType = ($mStatus === true) ? Design::SUCCESS_TYPE : Design::ERROR_TYPE; |
|
195
|
|
|
$this->design->setFlashMsg($sMsg, $sMsgType); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if ($this->httpRequest->postExists('remove_dump')) { |
|
200
|
|
|
if (!$oSecurityToken->check('backup')) { |
|
201
|
|
|
$this->design->setFlashMsg(Form::errorTokenMsg(), Design::ERROR_TYPE); |
|
202
|
|
|
} else { |
|
203
|
|
|
$sDumpFile = $this->httpRequest->post('dump_file'); |
|
204
|
|
|
|
|
205
|
|
|
if (!empty($sDumpFile)) { |
|
206
|
|
|
$this->file->deleteFile(PH7_PATH_BACKUP_SQL . $sDumpFile); |
|
207
|
|
|
$this->design->setFlashMsg(t('Dump file successfully deleted!')); |
|
208
|
|
|
} else { |
|
209
|
|
|
$this->design->setFlashMsg( |
|
210
|
|
|
t('Please select a dump file.'), |
|
211
|
|
|
Design::ERROR_TYPE |
|
212
|
|
|
); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
unset($oSecurityToken); |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
if ($this->httpRequest->postExists('restore_sql_file')) { |
|
221
|
|
|
if (!empty($_FILES['sql_file']['tmp_name'])) { |
|
222
|
|
|
$sNameFile = $_FILES['sql_file']['name']; |
|
223
|
|
|
$sTmpFile = $_FILES['sql_file']['tmp_name']; |
|
224
|
|
|
|
|
225
|
|
|
if ($this->file->getFileExt($sNameFile) === Backup::SQL_FILE_EXT) { |
|
226
|
|
|
$mStatus = (new Backup($sTmpFile))->restore(); |
|
227
|
|
|
} elseif ($this->file->getFileExt($sNameFile) === Backup::ARCHIVE_FILE_EXT) { |
|
228
|
|
|
$mStatus = (new Backup($sTmpFile))->restoreArchive(); |
|
229
|
|
|
} else { |
|
230
|
|
|
$mStatus = t('Dump file must be a SQL type (extension ".sql" or compressed archive ".gz")'); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
// Remove the temporary file |
|
234
|
|
|
$this->file->deleteFile($sTmpFile); |
|
235
|
|
|
} else { |
|
236
|
|
|
$mStatus = t('Please select a dump SQL file.'); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$sMsg = ($mStatus === true) ? t('Data successfully restored from desktop!') : $mStatus; |
|
240
|
|
|
$sMsgType = ($mStatus === true) ? Design::SUCCESS_TYPE : Design::ERROR_TYPE; |
|
241
|
|
|
$this->design->setFlashMsg($sMsg, $sMsgType); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$this->output(); |
|
|
|
|
|
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function optimize() |
|
248
|
|
|
{ |
|
249
|
|
|
$this->checkPost(); |
|
250
|
|
|
|
|
251
|
|
|
Db::optimize(); |
|
252
|
|
|
Header::redirect( |
|
253
|
|
|
Uri::get(PH7_ADMIN_MOD, 'tool', 'index'), |
|
254
|
|
|
t('All tables have been optimized!') |
|
255
|
|
|
); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
public function repair() |
|
259
|
|
|
{ |
|
260
|
|
|
$this->checkPost(); |
|
261
|
|
|
|
|
262
|
|
|
Db::repair(); |
|
263
|
|
|
Header::redirect( |
|
264
|
|
|
Uri::get(PH7_ADMIN_MOD, 'tool', 'index'), |
|
265
|
|
|
t('All tables have been repaired!') |
|
266
|
|
|
); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Includes the CSS file for the chart and/or for backup textarea field size. |
|
271
|
|
|
* |
|
272
|
|
|
* @return void |
|
273
|
|
|
*/ |
|
274
|
|
|
private function addGeneralCssFile() |
|
275
|
|
|
{ |
|
276
|
|
|
$this->design->addCss( |
|
277
|
|
|
PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_CSS, |
|
278
|
|
|
'general.css' |
|
279
|
|
|
); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Checks and stops the script if the method is not POST. |
|
284
|
|
|
* |
|
285
|
|
|
* @return string The text by exit() function. |
|
286
|
|
|
*/ |
|
287
|
|
|
private function checkPost() |
|
288
|
|
|
{ |
|
289
|
|
|
if (!$this->isPost()) { |
|
290
|
|
|
exit(Form::wrongRequestMethodMsg('POST')); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Checks if the request been made by the post method. |
|
296
|
|
|
* |
|
297
|
|
|
* @return bool |
|
298
|
|
|
*/ |
|
299
|
|
|
private function isPost() |
|
300
|
|
|
{ |
|
301
|
|
|
return $this->httpRequest->postExists('is'); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: