-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
cli.php
executable file
·304 lines (256 loc) · 10.3 KB
/
cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
require_once('vendor/autoload.php');
require_once('whitelabeler.php');
$cli = new League\CLImate\CLImate;
$whitelabeler = new Whitelabeler;
if ( count($argv) > 1 ) {
if ( $argv[1] == '--whitelabel' ) {
if ( isset($argv[2]) ) {
$config_file = explode('=', $argv[2]);
if ( !empty($config_file) && $config_file[0] == '--config' ) {
if ( file_exists($config_file[1]) ) {
$config = $whitelabeler->validateConfigValues($config_file[1]);
} else {
$cli->error('Config file not found.');
exit();
}
} else {
if ( file_exists(__DIR__.'/assets/'.$argv[2]) ) {
$config = $whitelabeler->validateConfigValues($argv[2]);
} else {
$cli->error('Config file not found.');
exit();
}
}
} else {
if ( file_exists(__DIR__.'/assets/config.json') ) {
$config = $whitelabeler->validateConfigValues();
} else {
$cli->error('Config file not found.');
exit();
}
}
if ( !empty($config['errors'] ) ) {
foreach( $config['errors'] as $error ) {
$cli->red($error);
}
} else {
$cli->magenta('Whitelabeling...');
// Replace CSS colors
$cli->out('Updating colors...');
$config = $config['config'];
$version = $whitelabeler->mauticVersion($config['path']);
$colors = $whitelabeler->colors(
$config['path'],
$version['version'],
$config['logo_bg'],
$config['primary'],
$config['hover'],
$config['sidebar_bg'],
$config['sidebar_submenu_bg'],
$config['sidebar_link'],
$config['sidebar_link_hover'],
$config['active_icon'],
$config['divider_left'],
$config['sidebar_divider'],
$config['submenu_bullet_bg'],
$config['submenu_bullet_shadow']
);
if ( $colors['status'] == 1 ) {
$cli->green($colors['message']);
} else {
$cli->error($colors['message']);
exit();
}
// Update company name in templates
$cli->out('Updating company name in templates...');
$company_name = $whitelabeler->companyName(
$config['path'],
$version['version'],
$config['company'],
$config['footer_prefix'],
$config['footer']
);
if ( $company_name['status'] == 1 ) {
$cli->green($company_name['message']);
} else {
foreach( $company_name['message'] as $error ) {
$cli->error($error);
}
exit();
}
// Update logo images
$cli->out('Updating logo files...');
if ( $config['favicon'] ) {
$favicon = $config['favicon'];
} else {
$favicon = $config['login_logo'];
}
$logos = $whitelabeler->replaceImages(
$config['path'],
$config['url'],
$version['version'],
__DIR__.'/assets/'.$config['sidebar_logo'],
$config['sidebar_logo_width'],
array(
'top' => $config['sidebar_logo_margin_top'],
'right' => $config['sidebar_logo_margin_right'],
'left' => $config['sidebar_logo_margin_left']
),
__DIR__.'/assets/'.$config['login_logo'],
$config['login_logo_width'],
array(
'top' => $config['login_logo_margin_top'],
'bottom' => $config['login_logo_margin_bottom']
),
__DIR__.'/assets/'.$favicon
);
if ( $logos['status'] == 1 ) {
$cli->green($logos['message']);
} else {
$cli->error($logos['message']);
exit();
}
// Clear Mautic cache and rebuild assets
$cli->out('Clearing Mautic cache...');
$clear_cache = $whitelabeler->clearMauticCache($config['path']);
if ( $clear_cache['status'] == 1 ) {
$cli->out('Rebuilding Mautic assets...');
$whitelabeler->rebuildAssets($config['path']);
$cli->green('Finished!');
$cli->out('Make sure to clear your browser\'s cache if your Mautic styles aren\'t updated after a browser refresh!');
} else {
$cli->error($clear_cache['message']);
}
}
} elseif ( $argv[1] == '--backup' ) {
if ( $whitelabeler->mauticVersion(dirname(__DIR__, 1))['status'] == 1 ) {
$mautic_path = dirname(__DIR__, 1);
$backups_dir = __DIR__.'/backups';
$cli->out('Backing up Mautic... ');
// Create backups folder if it doesn't exist
if ( !file_exists($backups_dir) ) {
mkdir($backups_dir, 0755, true);
}
// Get the name of the top directory that Mautic is in
$mautic_dir_name = explode('/', $mautic_path);
$mautic_dir_name = end($mautic_dir_name);
$backup_name = $mautic_dir_name.'_backup_'.date('Y-m-d_H-i-s',time());
echo shell_exec('cd '.$mautic_path.'; tar --exclude=./'.basename(__DIR__).' -zcvf '.$mautic_path.'/'.basename(__DIR__).'/backups/'.$backup_name.'.tgz .');
if ( !file_exists($backups_dir.'/'.$backup_name.'.tgz') ) {
$cli->red('There was a problem creating the backup.');
} else {
$cli->green('Backup complete!');
}
} else {
$cli->yellow('Mautic not found. Make sure the whitelabeler is placed in the Mautic root directory.');
}
} elseif ( $argv[1] == '--restore' ) {
if ( !is_dir(__DIR__.'/backups') ) {
$cli->yellow('No backups found. Use "php cli.php --backup --path=/path/to/mautic" to backup a Mautic installation');
exit();
}
// Look for backups
$backups = array();
foreach (new DirectoryIterator(__DIR__.'/backups') as $file) {
if ( $file->isDot() ) continue;
if ( $file->isFile() ) {
$file_extension = explode('.', $file);
$file_extension = end($file_extension);
if ( $file_extension == 'tgz' ) {
array_push($backups, $file->getFilename());
}
}
}
if ( !empty($backups) ) {
$input = $cli->radio('Select a backup to use:', $backups);
$restore_backup = __DIR__.'/backups/'.$input->prompt();
} else {
$cli->yellow('No backups found. Use "php cli.php --backup --path=/path/to/mautic" to backup a Mautic installation');
exit();
}
if ( $whitelabeler->mauticVersion(dirname(__DIR__, 1))['status'] == 1 ) {
$mautic_path = dirname(__DIR__, 1);
$cli->out('Mautic found.');
} else {
$input = $cli->yellow('Couldn\'t automatically find your Mautic files in '. dirname(__DIR__, 1));
$input = $cli->input('What\'s the absolute path to your Mautic files?');
$mautic_path = $input->prompt();
if ( $whitelabeler->mauticVersion($mautic_path)['status'] == 1 ) {
$cli->green('Ok, found Mautic.');
} else {
$input = $cli->red('Couldn\'t find your Mautic files in '.$mautic_path);
exit();
}
}
// Do the restore
$backup_name = explode('/', $restore_backup);
$backup_name = end($backup_name);
$mautic_dir_name = explode('/', $mautic_path);
$mautic_dir_name = end($mautic_dir_name);
$cli->out('Extracting backup files to Mautic directory...');
echo shell_exec('cd '.$mautic_path.'; tar --strip-components=1 -zxvf '.$mautic_path.'/'.basename(__DIR__).'/backups/'.$backup_name);
$cli->out('Setting ownership of the Mautic directory to www-data:www-data user/group...');
shell_exec('chown -R www-data:www-data '.$mautic_path);
$cli->green('Restore complete!');
} elseif ( $argv[1] == '--compare' ) {
$errors = array();
if ( isset($argv[2]) && substr($argv[2], 0, 7) == '--path1' ) {
$mautic_path_1 = explode('=', $argv[2]);
$mautic_path_1 = $mautic_path_1[1];
} else {
$errors[] = 'Define the path to Mautic installation 1.';
}
if ( isset($argv[3]) && substr($argv[3], 0, 7) == '--path2' ) {
$mautic_path_2 = explode('=', $argv[3]);
$mautic_path_2 = $mautic_path_2[1];
} else {
$errors[] = 'Define the path to Mautic installation 2.';
}
if (!empty($errors)) {
foreach($errors as $error) {
$cli->error($error);
}
exit();
}
if ( !file_exists($mautic_path_1.'/app/version.txt') ) {
$errors[] = 'Not able to find Mautic at path 1.';
}
if ( !file_exists($mautic_path_2.'/app/version.txt') ) {
$errors[] = 'Not able to find Mautic at path 2.';
}
if (empty($errors)) {
$cli->dump($whitelabeler->compareMauticVersions($mautic_path_1, $mautic_path_2));
} else {
foreach($errors as $error) {
$cli->error($error);
}
}
}
} else {
$cli->addArt(__DIR__.'/lib/ascii');
$cli->magenta()->draw('title');
echo PHP_EOL;
$cli->out('This utility looks for configuration values in assets/config.json to use for whitelabeling Mautic. You can also use it to easily make backups of your Mautic files and restore your installation from these backups.');
echo PHP_EOL;
$cli->yellow('Command line options:');
echo PHP_EOL;
$cli->out('<bold>php cli.php --whitelabel');
$cli->out('Looks for config.json in assets folder, validates JSON, and begins whitelabeling process.');
echo PHP_EOL;
$cli->out('<bold>php cli.php --whitelabel specific_file.json');
$cli->out('Looks for specific_file.json in assets folder, validates JSON, and begins whitelabeling process.');
echo PHP_EOL;
$cli->out('<bold>php cli.php --whitelabel --config=/path/to/specific_file.json');
$cli->out('Looks for JSON file at specific absolute path, validates JSON, and begins whitelabeling process.');
echo PHP_EOL;
$cli->out('<bold>php cli.php --backup</bold>');
$cli->out('Creates a compressed backup of your Mautic files and saves it into the whitelabeler backup directory.');
echo PHP_EOL;
$cli->out('<bold>php cli.php --restore');
$cli->out('Select a backup and extract files back into the Mautic directory.');
echo PHP_EOL;
$cli->out('<bold>php cli.php --compare --path1=/mautic1 --path2=/mautic2');
$cli->out('Compare the template files between two versions of Mautic. This helps us determine what template files need to be updated in order for the whitelabeler to work with new versions of Mautic.');
echo PHP_EOL;
}