These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @title Tool Controller |
||
4 | * |
||
5 | * @author Pierre-Henry Soria <[email protected]> |
||
6 | * @copyright (c) 2012-2017, 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 | * @version 1.1 |
||
10 | */ |
||
11 | namespace PH7; |
||
12 | |||
13 | use PH7\Framework\Mvc\Model\Engine as D; |
||
14 | use PH7\Framework\Security\CSRF\Token; |
||
15 | use PH7\Framework\Layout\Html\Security as HtmlSecurity; |
||
16 | use PH7\Framework\Date\CDateTime; |
||
17 | use PH7\Framework\Cache\Cache; |
||
18 | use PH7\Framework\Layout\Tpl\Engine\PH7Tpl\PH7Tpl; |
||
19 | use PH7\Framework\Layout\Gzip\Gzip; |
||
20 | use PH7\Framework\Url\Header; |
||
21 | use PH7\Framework\Mvc\Router\Uri; |
||
22 | |||
23 | class ToolController extends Controller |
||
24 | { |
||
25 | /** @var string */ |
||
26 | private $sTitle; |
||
27 | |||
28 | public function index() |
||
29 | { |
||
30 | $this->sTitle = t('General Tools'); |
||
31 | $this->view->page_title = $this->sTitle; |
||
32 | $this->view->h1_title = $this->sTitle; |
||
33 | |||
34 | $this->output(); |
||
35 | } |
||
36 | |||
37 | public function cache() |
||
38 | { |
||
39 | // Adding a CSRF token for the remove ajax cache. |
||
40 | $this->view->csrf_token = (new Token)->generate('cache'); |
||
41 | |||
42 | // Adding the common CSS and JS files for the ajax cache and the chart. |
||
43 | $this->design->addCss(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_CSS, 'general.css'); |
||
44 | $this->design->addJs(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_JS, 'common.js'); |
||
45 | |||
46 | $this->sTitle = t('Caches Management'); |
||
47 | $this->view->page_title = $this->sTitle; |
||
48 | $this->view->h1_title = $this->sTitle; |
||
49 | |||
50 | $this->view->aChartData = [ |
||
51 | ['title' => t('Database and Other Data'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . Cache::CACHE_DIR)], |
||
52 | ['title' => t('Server Code Template'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . PH7Tpl::COMPILE_DIR)], |
||
53 | ['title' => t('HTML Template'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . PH7Tpl::CACHE_DIR)], |
||
54 | ['title' => t('Static Files'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . Gzip::CACHE_DIR)] |
||
55 | ]; |
||
56 | |||
57 | $this->output(); |
||
58 | } |
||
59 | |||
60 | public function cacheConfig() |
||
61 | { |
||
62 | $this->sTitle = t('Cache Settings'); |
||
63 | $this->view->page_title = $this->sTitle; |
||
64 | $this->view->h1_title = $this->sTitle; |
||
65 | |||
66 | $this->output(); |
||
67 | } |
||
68 | |||
69 | public function freeSpace() |
||
70 | { |
||
71 | // Adding the common CSS for the chart. |
||
72 | $this->design->addCss( |
||
73 | PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_CSS, |
||
74 | 'general.css' |
||
75 | ); |
||
76 | |||
77 | $this->sTitle = t('Free Space of Server'); |
||
78 | $this->view->page_title = $this->sTitle; |
||
79 | $this->view->h1_title = $this->sTitle; |
||
80 | |||
81 | $this->view->aChartData = [ |
||
82 | ['title' => t('Public Root'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_ROOT)], |
||
83 | ['title' => t('Public data'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_PUBLIC_DATA)], |
||
84 | ['title' => t('Protected Root'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_PROTECTED)], |
||
85 | ['title' => t('Protected data'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_DATA)] |
||
86 | ]; |
||
87 | |||
88 | $this->output(); |
||
89 | } |
||
90 | |||
91 | public function envMode() |
||
92 | { |
||
93 | $this->sTitle = t('Environment Mode'); |
||
94 | $this->view->page_title = $this->sTitle; |
||
95 | $this->view->h1_title = $this->sTitle; |
||
96 | |||
97 | $this->output(); |
||
98 | } |
||
99 | |||
100 | public function backup() |
||
101 | { |
||
102 | $this->view->designSecurity = new HtmlSecurity; // Security Design Class |
||
103 | |||
104 | $this->sTitle = t('Backup Management'); |
||
105 | $this->view->page_title = $this->sTitle; |
||
106 | $this->view->h1_title = $this->sTitle; |
||
107 | |||
108 | $aDumpList = $this->file->getFileList(PH7_PATH_BACKUP_SQL, array('.sql', '.gz')); |
||
109 | // Removing the path |
||
110 | $aDumpList = array_map(function ($sValue) { return str_replace(PH7_PATH_BACKUP_SQL, '', $sValue); }, $aDumpList); |
||
111 | $this->view->aDumpList = $aDumpList; |
||
112 | |||
113 | |||
114 | $oSecurityToken = new Token; |
||
115 | |||
116 | if ($this->httpRequest->postExists('backup')) |
||
117 | { |
||
118 | if (!$oSecurityToken->check('backup')) |
||
119 | { |
||
120 | $this->design->setFlashMsg(Form::errorTokenMsg(), 'error'); |
||
121 | } |
||
122 | else |
||
123 | { |
||
124 | // Clean the site name to avoid bug with the backup path |
||
125 | $sSiteName = str_replace(array(' ', '/', '\\'), '_', $this->registry->site_name); |
||
126 | $sCurrentDate = (new CDateTime)->get()->date(); |
||
127 | |||
128 | switch ($this->httpRequest->post('backup_type')) |
||
129 | { |
||
130 | case 'server': |
||
131 | $sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . $sCurrentDate . '.sql'; |
||
132 | (new D\Util\Backup($sFullPath))->back()->save(); |
||
133 | $this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath); |
||
134 | break; |
||
135 | |||
136 | case 'server_archive': |
||
137 | $sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . $sCurrentDate . '.sql.gz'; |
||
138 | (new D\Util\Backup($sFullPath))->back()->saveArchive(); |
||
139 | $this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath); |
||
140 | break; |
||
141 | |||
142 | case 'client': |
||
143 | (new D\Util\Backup($sSiteName . '_' . $sCurrentDate . '.sql'))->back()->download(); |
||
144 | break; |
||
145 | |||
146 | case 'client_archive': |
||
147 | (new D\Util\Backup($sSiteName . '_' . $sCurrentDate . '.sql.gz'))->back()->downloadArchive(); |
||
148 | break; |
||
149 | |||
150 | case 'show': |
||
151 | $this->view->sql_content = (new D\Util\Backup)->back()->show(); |
||
152 | break; |
||
153 | |||
154 | default: |
||
155 | $this->design->setFlashMsg(t('Please select a field.'), 'error'); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | if ($this->httpRequest->postExists('restore_dump')) |
||
161 | { |
||
162 | if (!$oSecurityToken->check('backup')) |
||
163 | { |
||
164 | $this->design->setFlashMsg(Form::errorTokenMsg(), 'error'); |
||
165 | } |
||
166 | else |
||
167 | { |
||
168 | $sDumpFile = $this->httpRequest->post('dump_file'); |
||
169 | |||
170 | if (!empty($sDumpFile)) |
||
171 | { |
||
172 | if ($this->file->getFileExt($sDumpFile) == 'sql') |
||
173 | { |
||
174 | $mStatus = (new D\Util\Backup($sDumpFile))->restore(); |
||
175 | } |
||
176 | elseif ($this->file->getFileExt($sDumpFile) == 'gz') |
||
177 | { |
||
178 | $mStatus = (new D\Util\Backup(PH7_PATH_BACKUP_SQL . $sDumpFile))->restoreArchive(); |
||
179 | } |
||
180 | else |
||
181 | { |
||
182 | $mStatus = t('Dump file must be a SQL type (extension ".sql" or compressed archive ".gz")'); |
||
183 | } |
||
184 | } |
||
185 | else |
||
186 | { |
||
187 | $mStatus = t('Please select a dump file.'); |
||
188 | } |
||
189 | |||
190 | $sMsg = ($mStatus === true) ? t('Data successfully restored from server!') : $mStatus; |
||
191 | $sMsgType = ($mStatus === true) ? 'success' : 'error'; |
||
192 | $this->design->setFlashMsg($sMsg, $sMsgType); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | if ($this->httpRequest->postExists('remove_dump')) |
||
197 | { |
||
198 | if (!$oSecurityToken->check('backup')) |
||
199 | { |
||
200 | $this->design->setFlashMsg(Form::errorTokenMsg(), 'error'); |
||
201 | } |
||
202 | else |
||
203 | { |
||
204 | $sDumpFile = $this->httpRequest->post('dump_file'); |
||
205 | |||
206 | if (!empty($sDumpFile)) |
||
207 | { |
||
208 | $this->file->deleteFile(PH7_PATH_BACKUP_SQL . $sDumpFile); |
||
209 | $this->design->setFlashMsg(t('Dump file successfully deleted!')); |
||
210 | } |
||
211 | else |
||
212 | { |
||
213 | $this->design->setFlashMsg(t('Please select a dump file.'), 'error'); |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | unset($oSecurityToken); |
||
219 | |||
220 | |||
221 | if ($this->httpRequest->postExists('restore_sql_file')) |
||
222 | { |
||
223 | if (!empty($_FILES['sql_file']['tmp_name'])) |
||
224 | { |
||
225 | $sNameFile = $_FILES['sql_file']['name']; |
||
226 | $sTmpFile = $_FILES['sql_file']['tmp_name']; |
||
227 | |||
228 | if ($this->file->getFileExt($sNameFile) == 'sql') |
||
229 | { |
||
230 | $mStatus = (new D\Util\Backup($sTmpFile))->restore(); |
||
231 | } |
||
232 | elseif ($this->file->getFileExt($sNameFile) == 'gz') |
||
233 | { |
||
234 | $mStatus = (new D\Util\Backup($sTmpFile))->restoreArchive(); |
||
235 | } |
||
236 | else |
||
237 | { |
||
238 | $mStatus = t('Dump file must be a SQL type (extension ".sql" or compressed archive ".gz")'); |
||
239 | } |
||
240 | |||
241 | // Remove the temporary file |
||
242 | $this->file->deleteFile($sTmpFile); |
||
243 | } |
||
244 | else |
||
245 | { |
||
246 | $mStatus = t('Please select a dump SQL file.'); |
||
247 | } |
||
248 | |||
249 | $sMsg = ($mStatus === true) ? t('Data successfully restored from desktop!') : $mStatus; |
||
250 | $sMsgType = ($mStatus === true) ? 'success' : 'error'; |
||
251 | $this->design->setFlashMsg($sMsg, $sMsgType); |
||
252 | } |
||
253 | |||
254 | $this->output(); |
||
255 | } |
||
256 | |||
257 | public function optimize() |
||
258 | { |
||
259 | $this->_checkPost(); |
||
260 | |||
261 | D\Db::optimize(); |
||
262 | Header::redirect(Uri::get(PH7_ADMIN_MOD, 'tool', 'index'), t('All tables have been optimized!')); |
||
263 | } |
||
264 | |||
265 | public function repair() |
||
266 | { |
||
267 | $this->_checkPost(); |
||
268 | |||
269 | D\Db::repair(); |
||
270 | Header::redirect(Uri::get(PH7_ADMIN_MOD, 'tool', 'index'), t('All tables have been repaired!')); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Checks and stops the script if the method is not POST. |
||
275 | * |
||
276 | * @return string The text by exit() function. |
||
277 | */ |
||
278 | private function _checkPost() |
||
279 | { |
||
280 | if (!$this->_isPost()) { |
||
281 | exit(Form::wrongRequestMethodMsg('POST')); |
||
0 ignored issues
–
show
|
|||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Checks if the request been made ​​by the post method. |
||
287 | * |
||
288 | * @return boolean |
||
289 | */ |
||
290 | private function _isPost() |
||
291 | { |
||
292 | return $this->httpRequest->postExists('is'); |
||
293 | } |
||
294 | } |
||
295 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.