1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application\Controller\MembersArea; |
4
|
|
|
|
5
|
|
|
use Application\Tool\Helpers; |
6
|
|
|
use Silex\Application; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Borut Balažek <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ToolsController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param Request $request |
17
|
|
|
* @param Application $app |
18
|
|
|
* |
19
|
|
|
* @return Response |
20
|
|
|
*/ |
21
|
|
|
public function indexAction(Request $request, Application $app) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
if ( |
24
|
|
|
!$app['security']->isGranted('ROLE_TOOLS') && |
25
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
26
|
|
|
) { |
27
|
|
|
$app->abort(403); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return new Response( |
31
|
|
|
$app['twig']->render( |
32
|
|
|
'contents/members-area/tools/index.html.twig' |
33
|
|
|
) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Request $request |
39
|
|
|
* @param Application $app |
40
|
|
|
* |
41
|
|
|
* @return Response |
42
|
|
|
*/ |
43
|
|
|
public function databaseBackupAction(Request $request, Application $app) |
44
|
|
|
{ |
45
|
|
|
if ( |
46
|
|
|
!$app['security']->isGranted('ROLE_TOOLS') && |
47
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
48
|
|
|
) { |
49
|
|
|
$app->abort(403); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$data = array(); |
53
|
|
|
|
54
|
|
|
$backups = array(); |
55
|
|
|
$action = $request->query->get('action'); |
56
|
|
|
$selectedBackup = $request->query->get('backup'); |
57
|
|
|
$backupData = null; |
58
|
|
|
$backupsDirectory = STORAGE_DIR.'/backups/database'; |
59
|
|
|
|
60
|
|
|
if ($action == 'new') { |
61
|
|
|
$username = $app['database_options']['default']['user']; |
62
|
|
|
$password = $app['database_options']['default']['password']; |
63
|
|
|
$database = $app['database_options']['default']['dbname']; |
64
|
|
|
$file = $backupsDirectory.'/'.date('Ymd_His').'_new.sql'; |
65
|
|
|
|
66
|
|
|
shell_exec('mysqldump -u '.$username.' -p'.$password.' '.$database.' > '.$file); |
67
|
|
|
|
68
|
|
|
$app['flashbag']->add( |
69
|
|
|
'success', |
70
|
|
|
'A new backup was successfully created!' |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
return $app->redirect( |
74
|
|
|
$app['url_generator']->generate( |
75
|
|
|
'members-area.tools.database-backup' |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($action == 'restore') { |
81
|
|
|
$username = $app['database_options']['default']['user']; |
82
|
|
|
$password = $app['database_options']['default']['password']; |
83
|
|
|
$database = $app['database_options']['default']['dbname']; |
84
|
|
|
$file = $backupsDirectory.'/'.$selectedBackup.'.sql'; |
85
|
|
|
$fileOld = $backupsDirectory.'/'.date('Ymd_His').'_before_restore.sql'; |
86
|
|
|
|
87
|
|
|
// First create a new backup! |
88
|
|
|
shell_exec('mysqldump -u '.$username.' -p'.$password.' '.$database.' > '.$fileOld); |
89
|
|
|
|
90
|
|
|
// Then restore it! |
91
|
|
|
shell_exec('mysqldump -u '.$username.' -p'.$password.' '.$database.' < '.$file); |
92
|
|
|
|
93
|
|
|
$app['flashbag']->add( |
94
|
|
|
'success', |
95
|
|
|
'The database has been successfully restored.' |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return $app->redirect( |
99
|
|
|
$app['url_generator']->generate( |
100
|
|
|
'members-area.tools.database-backup' |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$backupsArray = Helpers::rglob($backupsDirectory.'/*.sql'); |
106
|
|
|
if (!empty($backupsArray)) { |
107
|
|
|
foreach ($backupsArray as $backupFilePath) { |
108
|
|
|
$backupFileName = str_replace( |
109
|
|
|
$backupsDirectory.'/', |
110
|
|
|
'', |
111
|
|
|
$backupFilePath |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$backups[] = array( |
115
|
|
|
'name' => $backupFileName, |
116
|
|
|
'path' => $backupFilePath, |
117
|
|
|
'size' => filesize($backupFilePath), |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$backups = array_reverse($backups); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($selectedBackup) { |
125
|
|
|
$backupData = file_get_contents($backupsDirectory.'/'.$selectedBackup); |
126
|
|
|
|
127
|
|
|
if (strlen($backupData) > 5000) { |
128
|
|
|
$backupData = substr($backupData, 0, 5000).' ...'; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$data['backups'] = $backups; |
133
|
|
|
$data['selectedBackup'] = $selectedBackup; |
134
|
|
|
$data['backupData'] = $backupData; |
135
|
|
|
|
136
|
|
|
return new Response( |
137
|
|
|
$app['twig']->render( |
138
|
|
|
'contents/members-area/tools/database-backup.html.twig', |
139
|
|
|
$data |
140
|
|
|
) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.