Issues (2160)

plugin/vchamilo/views/manage.controller.php (5 issues)

Labels
Severity
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
$table = Database::get_main_table('vchamilo');
5
6
if (!defined('CHAMILO_INTERNAL')) {
7
    exit('You cannot use this script this way');
8
}
9
10
$vidlist = isset($_REQUEST['vids']) ? implode("','", array_map('intval', $_REQUEST['vids'])) : '';
11
12
switch ($action) {
13
    case 'upgrade':
14
        Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/upgrade.php?vid='.$vidlist);
15
        break;
16
    case 'import':
17
        Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/import.php');
18
        break;
19
    case 'newinstance':
20
    case 'instance':
21
        $registeronly = isset($_REQUEST['registeronly']) ? $_REQUEST['registeronly'] : 0;
22
        Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/editinstance.php?registeronly='.$registeronly);
23
        break;
24
    case 'editinstance':
25
    case 'updateinstance':
26
        $vid = $_REQUEST['vid'];
27
        Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/editinstance.php?vid='.$vid);
28
        break;
29
    case 'deleteinstances':
30
    case 'disableinstances':
31
        if (!empty($vidlist)) {
32
            Display::addFlash(Display::return_message("Disabling instance"));
33
            // Make it not visible.
34
35
            $sql = "UPDATE $table SET visible = 0 WHERE id IN ('$vidlist')";
36
            Database::query($sql);
37
        }
38
        Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php');
39
        break;
40
    case 'enableinstances':
41
        if (!empty($vidlist)) {
42
            Display::addFlash(Display::return_message("Enabling instance"));
43
            $sql = "UPDATE $table SET visible = 1 WHERE id IN ('$vidlist') ";
44
            Database::query($sql);
45
        }
46
        Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php');
47
        break;
48
    case 'fulldeleteinstances':
49
        $todelete = [];
50
        // Removes everything.
51
        if (empty($automation)) {
52
            if (!empty($vidlist)) {
53
                $todelete = Database::select('*', 'vchamilo', ['where' => ["id IN ('$vidlist')" => []]]);
54
            }
55
        } else {
56
            $todelete = Database::select(
57
                '*',
58
                'vchamilo',
59
                ['where' => ["root_web = '{$n->root_web}' " => []]]
60
            );
61
        }
62
63
        if ($todelete) {
64
            foreach ($todelete as $fooid => $instance) {
65
                $slug = $instance['slug'];
66
67
                if (!empty($slug)) {
68
                    // Remove all files and eventual symlinks
69
                    $absalternatecourse = Virtual::getConfig('vchamilo', 'course_real_root');
70
                    $coursedir = $absalternatecourse.$slug;
0 ignored issues
show
Are you sure $absalternatecourse of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                    $coursedir = /** @scrutinizer ignore-type */ $absalternatecourse.$slug;
Loading history...
71
72
                    Display::addFlash(Display::return_message("Deleting $coursedir"));
73
74
                    removeDir($coursedir);
75
76
                    if ($absalternatehome = Virtual::getConfig('vchamilo', 'home_real_root')) {
77
                        $homedir = $absalternatehome.'/'.$slug;
78
79
                        Display::addFlash(Display::return_message("Deleting $homedir"));
80
                        removeDir($homedir);
81
                    }
82
83
                    // delete archive
84
                    if ($absalternatearchive = Virtual::getConfig('vchamilo', 'archive_real_root')) {
85
                        $archivedir = $absalternatearchive.'/'.$slug;
86
87
                        Display::addFlash(Display::return_message("Deleting $archivedir"));
88
                        removeDir($archivedir);
89
                    }
90
91
                    // Delete upload
92
                    if ($dir = Virtual::getConfig('vchamilo', 'upload_real_root')) {
93
                        $dir = $dir.'/'.$slug;
94
95
                        Display::addFlash(Display::return_message("Deleting $dir"));
96
                        removeDir($dir);
97
                    }
98
                }
99
100
                $sql = "DELETE FROM {$table} WHERE id = ".$instance['id'];
101
                Database::query($sql);
102
103
                Display::addFlash(Display::return_message("Removing instance: ".$instance['root_web']));
104
105
                Virtual::dropDatabase((object) $instance);
106
            }
107
        }
108
        break;
109
    case 'snapshotinstance':
110
        $interbreadcrumb[] = ['url' => 'manage.php', 'name' => get_lang('VChamilo')];
111
112
        $vid = isset($_REQUEST['vid']) ? $_REQUEST['vid'] : '';
113
        if ($vid) {
114
            $vhosts = Database::select('*', 'vchamilo', ['where' => ['id = ?' => $vid]]);
115
            $vhost = (object) array_pop($vhosts);
116
        } else {
117
            $vhost = (object) $_configuration;
118
            $vhost->slug = Virtual::getSlugFromUrl($vhost->root_web);
119
            $vhost->id = 0;
120
        }
121
122
        // Parsing url for building the template name.
123
        $wwwroot = $vhost->root_web;
124
        $vchamilostep = isset($_REQUEST['step']) ? $_REQUEST['step'] : '';
125
126
        // Make template directory (files and SQL).
127
        $separator = DIRECTORY_SEPARATOR;
128
        $dirMode = api_get_permissions_for_new_directories();
129
130
        $backupDir = api_get_path(SYS_PATH).'plugin'.$separator.'vchamilo'.$separator.'templates'.$separator.$vhost->slug.$separator;
131
132
        $absolute_datadir = $backupDir.'data';
133
        $absolute_sqldir = $backupDir.'dump.sql';
134
135
        if (!is_dir($backupDir)) {
136
            $result = mkdir($backupDir, $dirMode, true);
137
            if ($result) {
138
                Display::addFlash(
139
                    Display::return_message('Directory created: '.$backupDir)
140
                );
141
            } else {
142
                Display::addFlash(
143
                    Display::return_message("Cannot create directory: $backupDir check the folder permissions", 'error')
144
                );
145
            }
146
        }
147
148
        if ($vchamilostep == 0) {
149
            // Create directories, if necessary.
150
            if (!is_dir($absolute_datadir)) {
151
                mkdir($absolute_datadir, $dirMode, true);
152
                mkdir($absolute_datadir.'/home', $dirMode, true);
153
            }
154
155
            if (empty($fullautomation)) {
156
                $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
157
                $content = '<form name"single" action="'.$actionurl.'">';
158
                $content .= '<input type="hidden" name="what" value="snapshotinstance" />';
159
                $content .= '<input type="hidden" name="vid" value="'.$vhost->id.'" />';
160
                $content .= '<input type="hidden" name="step" value="1" />';
161
                $content .= '<input type="submit" class="btn btn-primary"  name="go_btn" value="'.$plugin->get_lang('continue').'" />';
162
                $content .= '</form>';
163
                $content .= '</div>';
164
165
                $tpl = new Template(get_lang('Snapshot'), true, true, false, true, false);
166
                $tpl->assign('message', '<h4>'.$plugin->get_lang('vchamilosnapshot1').'</h4>');
167
                $tpl->assign('content', $content);
168
                $tpl->display_one_col_template();
169
170
                exit;
171
            } else {
172
                // continue next step
173
                $vchamilostep = 1;
174
            }
175
        }
176
177
        if ($vchamilostep >= 1) {
178
            if ($wwwroot == $_configuration['root_web']) {
179
                // Make fake Vchamilo record.
180
                $vchamilo = Virtual::makeThis();
181
                $coursePath = api_get_path(SYS_COURSE_PATH);
182
                $homePath = api_get_path(SYS_HOME_PATH);
183
                $archivePath = api_get_path(SYS_ARCHIVE_PATH);
184
                $uploadPath = api_get_path(SYS_UPLOAD_PATH);
185
            } else {
186
                // Get Vchamilo known record.
187
                $vchamilo = Database::select('*', 'vchamilo', ['where' => ['root_web = ?' => [$wwwroot]]], 'first');
188
                $vchamilo = (object) $vchamilo;
189
                $coursePath = Virtual::getConfig('vchamilo', 'course_real_root');
190
                $homePath = Virtual::getConfig('vchamilo', 'home_real_root');
191
                $archivePath = Virtual::getConfig('vchamilo', 'archive_real_root');
192
                $uploadPath = Virtual::getConfig('vchamilo', 'upload_real_root');
193
194
                $coursePath = $coursePath.'/'.$vchamilo->slug;
0 ignored issues
show
Are you sure $coursePath of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
                $coursePath = /** @scrutinizer ignore-type */ $coursePath.'/'.$vchamilo->slug;
Loading history...
195
                $homePath = $homePath.'/'.$vchamilo->slug;
0 ignored issues
show
Are you sure $homePath of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

195
                $homePath = /** @scrutinizer ignore-type */ $homePath.'/'.$vchamilo->slug;
Loading history...
196
                $archivePath = $archivePath.'/'.$vchamilo->slug;
0 ignored issues
show
Are you sure $archivePath of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

196
                $archivePath = /** @scrutinizer ignore-type */ $archivePath.'/'.$vchamilo->slug;
Loading history...
197
                $uploadPath = $uploadPath.'/'.$vchamilo->slug;
0 ignored issues
show
Are you sure $uploadPath of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

197
                $uploadPath = /** @scrutinizer ignore-type */ $uploadPath.'/'.$vchamilo->slug;
Loading history...
198
            }
199
200
            $content = '';
201
            if ($vchamilostep == 1) {
202
                // Auto dump the databases in a master template folder.
203
                // this will create three files : dump.sql
204
                $result = Virtual::backupDatabase($vchamilo, $absolute_sqldir);
205
206
                if (empty($fullautomation)) {
207
                    if (!$result) {
208
                        $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
209
                        $content .= '<p><form name"single" action="'.$actionurl.'">';
210
                        $content .= '<input type="submit" name="go_btn" value="'.$plugin->get_lang('cancel').'" />';
211
                        $content .= '</form></p>';
212
                    } else {
213
                        $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
214
215
                        $message = $plugin->get_lang('vchamilosnapshot2');
216
217
                        Display::addFlash(
218
                            Display::return_message('Database file created: '.$absolute_sqldir)
219
                        );
220
221
                        $content .= '<form name"single" action="'.$actionurl.'">';
222
                        $content .= '<input type="hidden" name="what" value="snapshotinstance" />';
223
                        $content .= '<input type="hidden" name="vid" value="'.$vhost->id.'" />';
224
                        $content .= '<input type="hidden" name="step" value="2" />';
225
                        $content .= '<input class="btn btn-primary"  type="submit" name="go_btn" value="'.$plugin->get_lang('continue').'" />';
226
                        $content .= '</form>';
227
                    }
228
229
                    $tpl = new Template(get_lang('Snapshot'), true, true, false, true, false);
230
                    $tpl->assign('message', '<h4>'.$message.'</h4>');
231
                    $tpl->assign('content', $content);
232
                    $tpl->display_one_col_template();
233
234
                    exit;
235
                }
236
            }
237
238
            Display::addFlash(Display::return_message("Copying from '$homePath' to '{$absolute_datadir}/home' "));
239
            copyDirTo($homePath, $absolute_datadir.'/home/', false);
240
241
            Display::addFlash(Display::return_message("Copying from '$coursePath' to '$absolute_datadir/courses' "));
242
            copyDirTo($coursePath, $absolute_datadir.'/courses/', false);
243
244
            Display::addFlash(Display::return_message("Copying from '$uploadPath' to '$absolute_datadir/upload' "));
245
            copyDirTo($uploadPath, $absolute_datadir.'/upload/', false);
246
247
            // Store original hostname and some config info for further database or filestore replacements.
248
            $FILE = fopen($backupDir.$separator.'manifest.php', 'w');
249
            fwrite($FILE, '<'.'?php ');
250
            fwrite($FILE, "\$templatewwwroot = '".$wwwroot."';\n");
251
            fwrite($FILE, '?'.'>');
252
            fclose($FILE);
253
254
            // Every step was SUCCESS.
255
            if (empty($fullautomation)) {
256
                Display::addFlash(
257
                    Display::return_message(
258
                        $plugin->get_lang('successfinishedcapture'),
259
                        'success'
260
                    )
261
                );
262
263
                if (empty($vid)) {
264
                    $template = Virtual::getConfig('vchamilo', 'default_template');
265
                    if (empty($template)) {
266
                        Display::addFlash(
267
                            Display::return_message('Set default template as <b>'.$vhost->slug.'</b>', 'success', false)
268
                        );
269
                        $params = [
270
                            'subkey' => 'vchamilo',
271
                            'title' => 'default_template',
272
                            'type' => 'setting',
273
                            'category' => 'Plugins',
274
                            'variable' => 'vchamilo_default_template',
275
                            'selected_value' => $vhost->slug,
276
                            'access_url_changeable' => 0,
277
                        ];
278
                        api_set_setting_simple($params);
279
                    } else {
280
                        Display::addFlash(
281
                            Display::return_message('Default template is: <b>'.$vhost->slug.'</b>', 'success', false)
282
                        );
283
                    }
284
                }
285
286
                $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
287
                $content .= '<form name"single" action="'.$actionurl.'">';
288
                $content .= '<input class="btn btn-primary" type="submit" name="go_btn" value="'.$plugin->get_lang('backtoindex').'" />';
289
                $content .= '</form>';
290
291
                $tpl = new Template(get_lang('Snapshot'), true, true, false, true, false);
292
                $tpl->assign('message', $plugin->get_lang('vchamilosnapshot3'));
293
                $tpl->assign('content', $content);
294
                $tpl->display_one_col_template();
295
296
                exit;
297
            }
298
        }
299
        break;
300
    case 'clearcache':
301
        // Removes cache directory.
302
        if (empty($automation)) {
303
            if (array_key_exists('vids', $_REQUEST)) {
304
                $toclear = Database::select('*', 'vchamilo', ['where' => ["id IN ('$vidlist')" => []]]);
305
            } else {
306
                $vid = isset($_REQUEST['vid']) ? $_REQUEST['vid'] : 0;
307
                if ($vid) {
308
                    $vhosts = Database::select('*', 'vchamilo', ['where' => ['id = ?' => $vid]]);
309
                    $vhost = (object) array_pop($vhosts);
310
                    $toclear[$vhost->id] = $vhost;
311
                } else {
312
                    $toclear[0] = (object) $_configuration;
313
                }
314
            }
315
        } else {
316
            $toclear = Database::select(
317
                '*',
318
                'vchamilo',
319
                ['where' => ["root_web = '{$n->root_web}' " => []]]
320
            );
321
        }
322
323
        foreach ($toclear as $fooid => $instance) {
324
            if ($fooid == 0) {
325
                $templatepath = api_get_path(SYS_ARCHIVE_PATH).'twig';
326
                Display::addFlash(Display::return_message("Deleting master cache $templatepath \n"));
327
                removeDir($templatepath);
328
            } else {
329
                $coursePath = Virtual::getConfig('vchamilo', 'course_real_root');
330
                $homePath = Virtual::getConfig('vchamilo', 'home_real_root');
331
                $archivePath = Virtual::getConfig('vchamilo', 'archive_real_root');
332
                //$uploadPath = Virtual::getConfig('vchamilo', 'upload_real_root');
333
334
                // Get instance archive
335
                $archivepath = api_get_path(SYS_ARCHIVE_PATH, (array) $instance);
336
                $templatepath = $archivePath.'/'.$instance['slug'].'/twig';
337
                Display::addFlash(Display::return_message("Deleting cache $templatepath \n"));
338
                removeDir($templatepath);
339
            }
340
        }
341
        break;
342
    case 'setconfigvalue':
343
        $select = '<select name="preset" onchange="setpreset(this.form, this)">';
344
        $settings = api_get_settings();
345
        foreach ($settings as $setting) {
346
            $select .= '<option name="'.$setting['variable'].'/'.$setting['subkey'].'">'.
347
                $setting['variable'].' - '.$setting['subkey'].
348
            '</option>';
349
        }
350
        $select .= '</select>';
351
352
        if (empty($vidlist)) {
353
            api_not_allowed(true, 'No virtual chamilo selected');
354
        }
355
356
        Display::display_header();
357
        echo '<h2>'.$plugin->get_lang('sendconfigvalue').'</h2>';
358
        echo '<form name="setconfigform">';
359
        echo '<input type="hidden" name="vidlist" value="'.$vidlist.'" />';
360
        echo '<input type="hidden" name="confirm" value="1" />';
361
        echo '<table>';
362
        echo '<tr><td>'.get_lang('variable').'</td><td>'.get_lang('subkey').'</td></tr>';
363
        echo '<tr><td><input type="text" name="variable" value="" size="30" /></td>';
364
        echo '<td><input type="text" name="subkey" value="" size="30" /></td></tr>';
365
        echo '<tr><td colspan="2">'.$select.'</td></tr>';
366
        echo '<tr><td colspan="2">';
367
        echo '<input class="btn btn-primary" type="submit" name="go_btn" value="'.$plugin->get_lang('distributevalue').'"</td></tr>';
368
        echo '</table>';
369
        echo '</form>';
370
        Display::display_footer();
371
        exit;
372
        break;
373
}
374