Completed
Push — master ( c9546d...95f607 )
by Julito
09:41
created

public/main/admin/settings.php (5 issues)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use ChamiloSession as Session;
6
7
/**
8
 * With this tool you can easily adjust non critical configuration settings.
9
 * Non critical means that changing them will not result in a broken campus.
10
 *
11
 * @author Patrick Cool
12
 * @author Julio Montoya - Multiple URL site
13
 */
14
15
// Resetting the course id.
16
$cidReset = true;
17
18
require_once __DIR__.'/../inc/global.inc.php';
19
require_once 'settings.lib.php';
20
21
// Setting the section (for the tabs).
22
$this_section = SECTION_PLATFORM_ADMIN;
23
$_SESSION['this_section'] = $this_section;
24
25
// Access restrictions.
26
api_protect_admin_script();
27
28
 // Submit stylesheets.
29
if (isset($_POST['save']) && isset($_GET['category']) && 'Stylesheets' === $_GET['category']) {
30
    storeStylesheets();
31
    Display::addFlash(Display::return_message(get_lang('Saved.')));
32
}
33
34
// Settings to avoid
35
$settings_to_avoid = [
36
    'use_session_mode' => 'true',
37
    'gradebook_enable' => 'false',
38
    // ON by default - now we have this option when  we create a course
39
    'example_material_course_creation' => 'true',
40
];
41
42
$convert_byte_to_mega_list = [
43
    'dropbox_max_filesize',
44
    'message_max_upload_filesize',
45
    'default_document_quotum',
46
    'default_group_quotum',
47
];
48
49
if (isset($_POST['style'])) {
50
    Display::$preview_style = $_POST['style'];
51
}
52
53
// Database table definitions.
54
$table_settings_current = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
55
56
// Setting breadcrumbs.
57
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
58
59
// Setting the name of the tool.
60
$tool_name = get_lang('Configuration settings');
61
if (empty($_GET['category'])) {
62
    $_GET['category'] = 'Platform';
63
}
64
$watermark_deleted = false;
65
if (isset($_GET['delete_watermark'])) {
66
    $watermark_deleted = PDF::delete_watermark();
0 ignored issues
show
Bug Best Practice introduced by
The method PDF::delete_watermark() is not static, but was called statically. ( Ignorable by Annotation )

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

66
    /** @scrutinizer ignore-call */ 
67
    $watermark_deleted = PDF::delete_watermark();
Loading history...
67
    Display::addFlash(Display::return_message(get_lang('File deleted')));
68
}
69
70
if (isset($_GET['action']) && 'delete_grading' == $_GET['action']) {
71
    $id = intval($_GET['id']);
72
    api_delete_setting_option($id);
73
}
74
75
$url_id = api_get_current_access_url_id();
76
77
$settings = null;
78
79
// Build the form.
80
if (!empty($_GET['category']) &&
81
    !in_array($_GET['category'], ['Plugins', 'stylesheets', 'Search'])
82
) {
83
    $my_category = isset($_GET['category']) ? $_GET['category'] : null;
84
    $settings_array = getCategorySettings($my_category);
85
    $settings = $settings_array['settings'];
86
    $settings_by_access_list = $settings_array['settings_by_access_list'];
87
    $form = generateSettingsForm($settings, $settings_by_access_list);
88
89
    if ($form->validate()) {
90
        $values = $form->exportValues();
91
92
        $mark_all = false;
93
        $un_mark_all = false;
94
95
        if (api_is_multiple_url_enabled()) {
96
            if (isset($values['buttons_in_action_right']) &&
97
                isset($values['buttons_in_action_right']['mark_all'])
98
            ) {
99
                $mark_all = true;
100
            }
101
102
            if (isset($values['buttons_in_action_right']) &&
103
                isset($values['buttons_in_action_right']['unmark_all'])
104
            ) {
105
                $un_mark_all = true;
106
            }
107
        }
108
109
        if ($mark_all || $un_mark_all) {
110
            if (api_is_global_platform_admin()) {
111
                $locked_settings = api_get_locked_settings();
112
                foreach ($values as $key => $value) {
113
                    if (!in_array($key, $locked_settings)) {
114
                        $changeable = 0;
115
                        if ($mark_all) {
116
                            $changeable = 1;
117
                        }
118
119
                        $params = ['variable = ?' => [$key]];
120
                        $data = api_get_settings_params($params);
121
122
                        if (!empty($data)) {
123
                            foreach ($data as $item) {
124
                                $params = [
125
                                    'id' => $item['id'],
126
                                    'access_url_changeable' => $changeable,
127
                                ];
128
                                api_set_setting_simple($params);
129
                            }
130
                        }
131
                    }
132
                }
133
                // Reload settings
134
                $settings_array = getCategorySettings($my_category);
135
                $settings = $settings_array['settings'];
136
                $settings_by_access_list = $settings_array['settings_by_access_list'];
137
                $form = generateSettingsForm(
138
                    $settings,
139
                    $settings_by_access_list
140
                );
141
            }
142
        }
143
        if (!empty($_FILES['pdf_export_watermark_path'])) {
144
            $pdf_export_watermark_path = $_FILES['pdf_export_watermark_path'];
145
        }
146
147
        if (isset($pdf_export_watermark_path) && !empty($pdf_export_watermark_path['name'])) {
148
            $pdf_export_watermark_path_result = PDF::upload_watermark(
0 ignored issues
show
Bug Best Practice introduced by
The method PDF::upload_watermark() is not static, but was called statically. ( Ignorable by Annotation )

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

148
            /** @scrutinizer ignore-call */ 
149
            $pdf_export_watermark_path_result = PDF::upload_watermark(
Loading history...
149
                $pdf_export_watermark_path['name'],
150
                $pdf_export_watermark_path['tmp_name']
151
            );
152
            if ($pdf_export_watermark_path_result) {
153
                Display::addFlash(Display::return_message(get_lang('File upload succeeded!')));
154
            } else {
155
                $message = get_lang('The uploaded file could not be saved (perhaps a permission problem?)').' '.get_lang('Folder').': '.api_get_path(SYS_CODE_PATH).'default_course_document/images';
156
                Display::addFlash(Display::return_message($message), 'warning');
157
            }
158
            unset($update_values['pdf_export_watermark_path']);
159
        }
160
161
        // Set true for allow_message_tool variable if social tool is actived
162
        foreach ($convert_byte_to_mega_list as $item) {
163
            if (isset($values[$item])) {
164
                $values[$item] = round($values[$item] * 1024 * 1024);
165
            }
166
        }
167
168
        if (isset($values['allow_social_tool']) && 'true' == $values['allow_social_tool']) {
169
            $values['allow_message_tool'] = 'true';
170
        }
171
172
        foreach ($settings as $item) {
0 ignored issues
show
The expression $settings of type null is not traversable.
Loading history...
173
            $key = $item['variable'];
174
            if ('prevent_multiple_simultaneous_login' === $key) {
175
                Session::write('first_user_login', 1);
176
            }
177
            if (in_array($key, $settings_to_avoid)) {
178
                continue;
179
            }
180
            if ('search_field' == $key || 'submit_fixed_in_bottom' == $key) {
181
                continue;
182
            }
183
            $key = Database::escape_string($key);
184
            $sql = "UPDATE $table_settings_current
185
                    SET selected_value = 'false'
186
                    WHERE
187
                        variable = '".$key."' AND
188
                        access_url = ".intval($url_id)." AND
189
                        type IN ('checkbox', 'radio') ";
190
            $res = Database::query($sql);
191
        }
192
193
        // Save the settings.
194
        $keys = [];
195
196
        foreach ($values as $key => $value) {
197
            if (0 === strcmp($key, 'MAX_FILE_SIZE')) {
198
                continue;
199
            }
200
            if (in_array($key, $settings_to_avoid)) {
201
                continue;
202
            }
203
            // Avoid form elements which have nothing to do with settings
204
            if ('search_field' == $key || 'submit_fixed_in_bottom' == $key) {
205
                continue;
206
            }
207
208
            // Treat gradebook values in separate function.
209
            //if (strpos($key, 'gradebook_score_display_custom_values') === false) {
210
            if (!is_array($value)) {
211
                $old_value = api_get_setting($key);
212
                switch ($key) {
213
                    case 'header_extra_content':
214
                        file_put_contents(api_get_home_path().'header_extra_content.txt', $value);
0 ignored issues
show
The function api_get_home_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

214
                        file_put_contents(/** @scrutinizer ignore-call */ api_get_home_path().'header_extra_content.txt', $value);
Loading history...
215
                        $value = api_get_home_path().'header_extra_content.txt';
216
                        break;
217
                    case 'footer_extra_content':
218
                        file_put_contents(api_get_home_path().'footer_extra_content.txt', $value);
219
                        $value = api_get_home_path().'footer_extra_content.txt';
220
                        break;
221
                    case 'InstitutionUrl':
222
                    case 'course_validation_terms_and_conditions_url':
223
                        // URL validation for some settings.
224
                        $value = trim(Security::remove_XSS($value));
225
                        if ('' != $value) {
226
                            // Here we accept absolute URLs only.
227
                            if (false === strpos($value, '://')) {
228
                                $value = 'http://'.$value;
229
                            }
230
                            if (!api_valid_url($value, true)) {
231
                                // If the new (non-empty) URL value is invalid, then the old URL value stays.
232
                                $value = $old_value;
233
                            }
234
                        }
235
                        // If the new URL value is empty, then it will be stored (i.e. the setting will be deleted).
236
                        break;
237
                    case 'emailAdministrator':
238
                        // Validation against e-mail address for some settings.
239
                        $value = trim(Security::remove_XSS($value));
240
                        if ('' != $value && !api_valid_email($value)) {
241
                            // If the new (non-empty) e-mail address is invalid, then the old e-mail address stays.
242
                            // If the new e-mail address is empty, then it will be stored (i.e. the setting will be deleted).
243
                            $value = $old_value;
244
                        }
245
                        break;
246
                }
247
                if ($old_value != $value) {
248
                    $keys[] = $key;
249
                }
250
                $result = api_set_setting($key, $value, null, null, $url_id);
251
            } else {
252
                $sql = "SELECT subkey FROM $table_settings_current
253
                        WHERE variable = '$key'";
254
                $res = Database::query($sql);
255
256
                while ($row_subkeys = Database::fetch_array($res)) {
257
                    // If subkey is changed:
258
                    if ((isset($value[$row_subkeys['subkey']]) && 'false' == api_get_setting($key, $row_subkeys['subkey'])) ||
259
                        (!isset($value[$row_subkeys['subkey']]) && 'true' == api_get_setting($key, $row_subkeys['subkey']))
260
                    ) {
261
                        $keys[] = $key;
262
                        break;
263
                    }
264
                }
265
266
                foreach ($value as $subkey => $subvalue) {
267
                    $result = api_set_setting($key, 'true', $subkey, null, $url_id);
268
                }
269
            }
270
        }
271
272
        // Add event configuration settings category to the system log.
273
        $user_id = api_get_user_id();
274
        $category = $_GET['category'];
275
        Event::addEvent(
0 ignored issues
show
The method addEvent() does not exist on Event. ( Ignorable by Annotation )

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

275
        Event::/** @scrutinizer ignore-call */ 
276
               addEvent(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
276
            LOG_CONFIGURATION_SETTINGS_CHANGE,
277
            LOG_CONFIGURATION_SETTINGS_CATEGORY,
278
            $category,
279
            api_get_utc_datetime(),
280
            $user_id
281
        );
282
283
        // Add event configuration settings variable to the system log.
284
        if (is_array($keys) && count($keys) > 0) {
285
            foreach ($keys as $variable) {
286
                if (in_array($key, $settings_to_avoid)) {
287
                    continue;
288
                }
289
                Event::addEvent(
290
                    LOG_CONFIGURATION_SETTINGS_CHANGE,
291
                    LOG_CONFIGURATION_SETTINGS_VARIABLE,
292
                    $variable,
293
                    api_get_utc_datetime(),
294
                    $user_id
295
                );
296
            }
297
        }
298
299
        Display::addFlash(Display::return_message(get_lang('Update successful')));
300
301
        header('Location: '.api_get_self().'?category='.Security::remove_XSS($my_category));
302
        exit;
303
    }
304
}
305
$htmlHeadXtra[] = '<script>    
306
    var hide_icon = "'.api_get_path(WEB_IMG_PATH).'/icons/32/shared_setting_na.png";
307
    var show_icon = "'.api_get_path(WEB_IMG_PATH).'/icons/32/shared_setting.png";
308
    var url       = "'.api_get_path(WEB_AJAX_PATH).'admin.ajax.php?a=update_changeable_setting";
309
310
    $(function() {
311
        $(".share_this_setting").on("click", function() {
312
            var my_img = $(this).find("img");
313
            var link = $(this);
314
            $.ajax({
315
                url: url,
316
                data: {
317
                    changeable: $(this).attr("data_status"),
318
                    id: $(this).attr("data_to_send")
319
                },
320
                success: function(data) {
321
                    if (data == 1) {
322
                        if (link.attr("data_status") == 1) {
323
                            my_img.attr("src", show_icon);
324
                            link.attr("data_status", 0);
325
                        } else {
326
                            my_img.attr("src", hide_icon);
327
                            link.attr("data_status", 1);
328
                        }
329
                    }
330
                }
331
            });
332
        });
333
    });
334
</script>';
335
336
// The action images.
337
$action_images['platform'] = 'platform.png';
338
$action_images['course'] = 'course.png';
339
$action_images['session'] = 'session.png';
340
$action_images['tools'] = 'tools.png';
341
$action_images['user'] = 'user.png';
342
$action_images['gradebook'] = 'gradebook.png';
343
$action_images['ldap'] = 'ldap.png';
344
$action_images['cas'] = 'cas.png';
345
$action_images['security'] = 'security.png';
346
$action_images['languages'] = 'languages.png';
347
$action_images['tuning'] = 'tuning.png';
348
$action_images['templates'] = 'template.png';
349
$action_images['search'] = 'search.png';
350
$action_images['editor'] = 'html_editor.png';
351
$action_images['timezones'] = 'timezone.png';
352
$action_images['extra'] = 'wizard.png';
353
$action_images['tracking'] = 'statistics.png';
354
$action_images['gradebook'] = 'gradebook.png';
355
$action_images['search'] = 'search.png';
356
$action_images['stylesheets'] = 'stylesheets.png';
357
$action_images['templates'] = 'template.png';
358
$action_images['plugins'] = 'plugins.png';
359
$action_images['shibboleth'] = 'shibboleth.png';
360
$action_images['facebook'] = 'facebook.png';
361
$action_images['crons'] = 'crons.png';
362
$action_images['webservices'] = 'webservices.png';
363
364
$action_array = [];
365
$resultcategories = [];
366
367
$resultcategories[] = ['category' => 'Platform'];
368
$resultcategories[] = ['category' => 'Course'];
369
$resultcategories[] = ['category' => 'Session'];
370
$resultcategories[] = ['category' => 'Languages'];
371
$resultcategories[] = ['category' => 'User'];
372
$resultcategories[] = ['category' => 'Tools'];
373
$resultcategories[] = ['category' => 'Editor'];
374
$resultcategories[] = ['category' => 'Security'];
375
$resultcategories[] = ['category' => 'Tuning'];
376
$resultcategories[] = ['category' => 'Gradebook'];
377
$resultcategories[] = ['category' => 'Timezones'];
378
$resultcategories[] = ['category' => 'Tracking'];
379
$resultcategories[] = ['category' => 'Search'];
380
$resultcategories[] = ['category' => 'Stylesheets'];
381
$resultcategories[] = ['category' => 'Templates'];
382
$resultcategories[] = ['category' => 'Plugins'];
383
$resultcategories[] = ['category' => 'LDAP'];
384
$resultcategories[] = ['category' => 'CAS'];
385
$resultcategories[] = ['category' => 'Shibboleth'];
386
$resultcategories[] = ['category' => 'Facebook'];
387
$resultcategories[] = ['category' => 'Crons'];
388
$resultcategories[] = ['category' => 'WebServices'];
389
390
foreach ($resultcategories as $row) {
391
    $url = [];
392
    $url['url'] = api_get_self()."?category=".$row['category'];
393
    $url['content'] = Display::return_icon(
394
        $action_images[strtolower($row['category'])],
395
        api_ucfirst(get_lang($row['category'])),
396
        [],
397
        ICON_SIZE_MEDIUM
398
    );
399
    if (strtolower($row['category']) == strtolower($_GET['category'])) {
400
        $url['active'] = true;
401
    }
402
    $action_array[] = $url;
403
}
404
405
ob_start();
406
if (!empty($_GET['category'])) {
407
    switch ($_GET['category']) {
408
        case 'Regions':
409
            handleRegions();
410
            break;
411
        case 'Plugins':
412
            // Displaying the extensions: Plugins.
413
            // This will be available to all the sites (access_urls).
414
            $securityToken = isset($_GET['sec_token']) ? Security::remove_XSS($_GET['sec_token']) : null;
415
            if (isset($_POST['submit_dashboard_plugins']) && Security::check_token($securityToken)) {
416
                Security::clear_token();
417
                $affected_rows = DashboardManager::store_dashboard_plugins($_POST);
418
                if ($affected_rows) {
419
                    // add event to system log
420
                    $user_id = api_get_user_id();
421
                    $category = $_GET['category'];
422
                    Event::addEvent(
423
                        LOG_CONFIGURATION_SETTINGS_CHANGE,
424
                        LOG_CONFIGURATION_SETTINGS_CATEGORY,
425
                        $category,
426
                        api_get_utc_datetime(),
427
                        $user_id
428
                    );
429
                    echo Display::return_message(get_lang('Dashboard pluginsUpdate successfulSuccessfully'), 'confirmation');
430
                }
431
            }
432
433
            echo '<div class="tab_wrapper">';
434
            echo '<ul class="nav nav-tabs" id="tabs" role="tablist">';
435
            echo '<li class="nav-item"><a id="plugin-tab-1" class="nav-link active" href="#tab1" aria-controls="tab1" aria-selected="true">'.get_lang('Plugins').'</a></li>';
436
            echo '<li class="nav-item"><a id="plugin-tab-2" class="nav-link" href="#tab2" aria-controls="tab2" aria-selected="false">'.get_lang('Dashboard plugins').'</a></li>';
437
            echo '<li class="nav-item"><a id="plugin-tab-3" class="nav-link" href="#tab3" aria-controls="tab3" aria-selected="false">'.get_lang('Configure extensions').'</a></li>';
438
            echo '</ul>';
439
440
            echo '<div class="tab-content" id="tabs-content">';
441
            echo '<div class="tab-pane fade show active" id="tab1" role="tabpanel" aria-labelledby="plugin-tab-1">';
442
            handlePlugins();
443
            echo '</div>';
444
445
            echo '<div class="tab-pane fade" id="tab2" role="tabpanel" aria-labelledby="plugin-tab-2">';
446
            DashboardManager::handle_dashboard_plugins();
447
            echo '</div>';
448
449
            echo '<div class="tab-pane fade" id="tab3" role="tabpanel" aria-labelledby="plugin-tab-3">';
450
            handleExtensions();
451
            echo '</div>';
452
            echo '</div>';
453
            echo '</div>';
454
            break;
455
        case 'Stylesheets':
456
            // Displaying the extensions: Stylesheets.
457
            handleStylesheets();
458
            break;
459
        case 'Search':
460
            handleSearch();
461
            break;
462
        case 'Templates':
463
            handleTemplates();
464
            break;
465
        default:
466
            api_not_allowed(true);
467
            break;
468
    }
469
}
470
$content = ob_get_clean();
471
472
// Including the header (banner).
473
Display::display_header($tool_name);
474
475
echo $content;
476
477
Display::display_footer();
478