Completed
Push — 1.10.x ( ba0bf0...97c0d2 )
by Angel Fernando Quiroz
44:06
created

settings.lib.php ➔ handle_stylesheets()   F

Complexity

Conditions 44
Paths > 20000

Size

Total Lines 261
Code Lines 169

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 44
eloc 169
nc 169344
nop 0
dl 0
loc 261
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Library of the settings.php file
6
 *
7
 * @author Julio Montoya <[email protected]>
8
 * @author Guillaume Viguier <[email protected]>
9
 *
10
 * @since Chamilo 1.8.7
11
 * @package chamilo.admin
12
 */
13
14
define('CSS_UPLOAD_PATH', api_get_path(SYS_APP_PATH).'Resources/public/css/themes/');
15
16
use Symfony\Component\Filesystem\Filesystem;
17
18
/**
19
 * This function allows easy activating and inactivating of regions
20
 * @author Julio Montoya <[email protected]> Beeznest 2012
21
 */
22
function handle_regions()
23
{
24
    if (isset($_POST['submit_plugins'])) {
25
        store_regions();
26
        // Add event to the system log.
27
        $user_id = api_get_user_id();
28
        $category = $_GET['category'];
29
        Event::addEvent(
30
            LOG_CONFIGURATION_SETTINGS_CHANGE,
31
            LOG_CONFIGURATION_SETTINGS_CATEGORY,
32
            $category,
33
            api_get_utc_datetime(),
34
            $user_id
35
        );
36
        Display :: display_confirmation_message(get_lang('SettingsStored'));
37
    }
38
39
    $plugin_obj = new AppPlugin();
40
    $possible_plugins  = $plugin_obj->read_plugins_from_path();
41
    $installed_plugins = $plugin_obj->get_installed_plugins();
42
43
    echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
44
    echo '<table class="data_table">';
45
    echo '<tr>';
46
    echo '<th width="400px">';
47
    echo get_lang('Plugin');
48
    echo '</th><th>';
49
    echo get_lang('Regions');
50
    echo '</th>';
51
    echo '</th>';
52
    echo '</tr>';
53
54
    /* We display all the possible plugins and the checkboxes */
55
56
    $plugin_region_list = array();
57
    $my_plugin_list = $plugin_obj->get_plugin_regions();
58
    foreach ($my_plugin_list as $plugin_item) {
59
        $plugin_region_list[$plugin_item] = $plugin_item;
60
    }
61
62
    // Removing course tool
63
    unset($plugin_region_list['course_tool_plugin']);
64
65
    foreach ($installed_plugins as $plugin) {
66
        $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$plugin.'/plugin.php';
67
68
        if (file_exists($plugin_info_file)) {
69
            $plugin_info = array();
70
            require $plugin_info_file;
71
            if (isset($_GET['name']) && $_GET['name'] == $plugin) {
72
                echo '<tr class="row_selected">';
73
            } else {
74
                echo '<tr>';
75
            }
76
            echo '<td>';
77
            echo '<h4>'.$plugin_info['title'].' <small>v'.$plugin_info['version'].'</small></h4>';
78
            echo '<p>'.$plugin_info['comment'].'</p>';
79
            echo '</td><td>';
80
            $selected_plugins = $plugin_obj->get_areas_by_plugin($plugin);
81
82
            if (isset($plugin_info['is_course_plugin']) && $plugin_info['is_course_plugin']) {
83
                $region_list = array('course_tool_plugin' => 'course_tool_plugin');
84
            } else {
85
                $region_list = $plugin_region_list;
86
            }
87
            echo Display::select('plugin_'.$plugin.'[]', $region_list, $selected_plugins, array('multiple' => 'multiple', 'style' => 'width:500px'), true, get_lang('None'));
88
            echo '</td></tr>';
89
        }
90
    }
91
    echo '</table>';
92
    echo '<br />';
93
    echo '<button class="btn btn-success" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
94
}
95
96
function handle_extensions()
97
{
98
    echo Display::page_subheader(get_lang('ConfigureExtensions'));
99
    echo '<a class="btn btn-success" href="configure_extensions.php?display=ppt2lp" role="button">'.get_lang('Ppt2lp').'</a>';
100
101
}
102
/**
103
 * This function allows easy activating and inactivating of plugins
104
 * @todo: a similar function needs to be written to activate or inactivate additional tools.
105
 * @author Patrick Cool <[email protected]>, Ghent University
106
 * @author Julio Montoya <[email protected]> Beeznest 2012
107
 */
108
function handle_plugins()
109
{
110
    $plugin_obj = new AppPlugin();
111
    $token = Security::get_token();
112
    if (isset($_POST['submit_plugins'])) {
113
        store_plugins();
114
        // Add event to the system log.
115
        $user_id = api_get_user_id();
116
        $category = $_GET['category'];
117
        Event::addEvent(
118
            LOG_CONFIGURATION_SETTINGS_CHANGE,
119
            LOG_CONFIGURATION_SETTINGS_CATEGORY,
120
            $category,
121
            api_get_utc_datetime(),
122
            $user_id
123
        );
124
        Display :: display_confirmation_message(get_lang('SettingsStored'));
125
    }
126
127
    $all_plugins = $plugin_obj->read_plugins_from_path();
128
    $installed_plugins = $plugin_obj->get_installed_plugins();
129
130
    //Plugins NOT installed
131
    echo Display::page_subheader(get_lang('Plugins'));
132
    echo '<form class="form-horizontal" name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'&sec_token=' . $token . '">';
133
    echo '<table class="data_table">';
134
    echo '<tr>';
135
    echo '<th width="20px">';
136
    echo get_lang('Action');
137
    echo '</th><th>';
138
    echo get_lang('Description');
139
    echo '</th>';
140
    echo '</tr>';
141
142
    $plugin_list = array();
143
    $my_plugin_list = $plugin_obj->get_plugin_regions();
144
    foreach($my_plugin_list as $plugin_item) {
145
        $plugin_list[$plugin_item] = $plugin_item;
146
    }
147
148
    foreach ($all_plugins as $plugin) {
149
        $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$plugin.'/plugin.php';
150
151
        if (file_exists($plugin_info_file)) {
152
            $plugin_info = array();
153
            require $plugin_info_file;
154
155
            if (in_array($plugin, $installed_plugins)) {
156
                echo '<tr class="row_selected">';
157
            } else {
158
                echo '<tr>';
159
            }
160
            echo '<td>';
161
            //Checkbox
162
            if (in_array($plugin, $installed_plugins)) {
163
                echo '<input type="checkbox" name="plugin_'.$plugin.'[]" checked="checked">';
164
165
            } else {
166
                echo '<input type="checkbox" name="plugin_'.$plugin.'[]">';
167
            }
168
            echo '</td><td>';
169
170
            echo '<h4>'.$plugin_info['title'].' <small>v '.$plugin_info['version'].'</small></h4>';
171
            echo '<p>'.$plugin_info['comment'].'</p>';
172
            echo '<p>'.get_lang('Author').': '.$plugin_info['author'].'</p>';
173
174
            echo '<div class="btn-group">';
175
            if (in_array($plugin, $installed_plugins)) {
176
                echo Display::url('<em class="fa fa-cogs"></em> '.get_lang('Configure'), 'configure_plugin.php?name='.$plugin, array('class' => 'btn btn-default'));
177
                echo Display::url('<em class="fa fa-th-large"></em> '.get_lang('Regions'), 'settings.php?category=Regions&name='.$plugin, array('class' => 'btn btn-default'));
178
            }
179
180
            if (file_exists(api_get_path(SYS_PLUGIN_PATH).$plugin.'/readme.txt')) {
181
                echo Display::url(
182
                    "<em class='fa fa-file-text-o'></em> readme.txt",
183
                    api_get_path(WEB_PLUGIN_PATH) . $plugin . "/readme.txt",
184
                    [
185
                        'class' => 'btn btn-default ajax',
186
                        'data-title' => $plugin_info['title'],
187
                        'data-size' => 'lg',
188
                        '_target' => '_blank'
189
                    ]
190
                );
191
            }
192
            echo '</div>';
193
            echo '</td></tr>';
194
        }
195
    }
196
    echo '</table>';
197
198
    echo '<div class="form-actions bottom_actions">';
199
    echo '<button class="btn btn-success" type="submit" name="submit_plugins">'.
200
            get_lang('EnablePlugins').'</button>';
201
    echo '</div>';
202
    echo '</form>';
203
}
204
205
/**
206
 * This function allows the platform admin to choose the default stylesheet
207
 * @author Patrick Cool <[email protected]>, Ghent University
208
 * @author Julio Montoya <[email protected]>, Chamilo
209
 */
210
function handle_stylesheets()
211
{
212
    global $_configuration;
213
214
    // Current style.
215
    $currentstyle = api_get_setting('stylesheets');
216
217
    $is_style_changeable = false;
218
219
    if ($_configuration['access_url'] != 1) {
220
        $style_info = api_get_settings('stylesheets', '', 1, 0);
221
        $url_info = api_get_access_url($_configuration['access_url']);
222
        if ($style_info[0]['access_url_changeable'] == 1 && $url_info['active'] == 1) {
223
            $is_style_changeable = true;
224
        }
225
    } else {
226
        $is_style_changeable = true;
227
    }
228
229
    $form = new FormValidator(
230
        'stylesheet_upload',
231
        'post',
232
        'settings.php?category=Stylesheets#tabs-3'
233
    );
234
    $form->addElement('text', 'name_stylesheet', get_lang('NameStylesheet'), array('size' => '40', 'maxlength' => '40'));
235
    $form->addRule('name_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
236
    $form->addElement('file', 'new_stylesheet', get_lang('UploadNewStylesheet'));
237
    $allowed_file_types = array('css', 'zip', 'jpeg', 'jpg', 'png', 'gif', 'ico', 'psd', 'xcf', 'svg', 'webp', 'woff', 'woff2');
238
239
    $form->addRule('new_stylesheet', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
240
    $form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
241
    $form->addButtonUpload(get_lang('Upload'), 'stylesheet_upload');
242
243
    $show_upload_form = false;
244
245
    if (!is_writable(CSS_UPLOAD_PATH)) {
246
        Display::display_error_message(CSS_UPLOAD_PATH.get_lang('IsNotWritable'));
247
    } else {
248
        // Uploading a new stylesheet.
249
        if ($_configuration['access_url'] == 1) {
250
            $show_upload_form = true;
251
        } else {
252
            if ($is_style_changeable) {
253
                $show_upload_form = true;
254
            }
255
        }
256
    }
257
258
    // Stylesheet upload.
259
260
    if (isset($_POST['stylesheet_upload'])) {
261
        if ($form->validate()) {
262
            $values = $form->exportValues();
263
            $picture_element = $form->getElement('new_stylesheet');
264
            $picture = $picture_element->getValue();
265
            $result = upload_stylesheet($values, $picture);
266
267
            // Add event to the system log.
268
            $user_id = api_get_user_id();
269
            $category = $_GET['category'];
270
            Event::addEvent(
271
                LOG_CONFIGURATION_SETTINGS_CHANGE,
272
                LOG_CONFIGURATION_SETTINGS_CATEGORY,
273
                $category,
274
                api_get_utc_datetime(),
275
                $user_id
276
            );
277
278
            if ($result) {
279
                Display::display_confirmation_message(get_lang('StylesheetAdded'));
280
            }
281
        }
282
    }
283
284
    $form_change = new FormValidator(
285
        'stylesheet_upload',
286
        'post',
287
        api_get_self().'?category=Stylesheets',
288
        null,
289
        array('id' => 'stylesheets_id')
290
    );
291
292
    $list_of_names  = array();
293
    $selected = '';
294
    $dirpath = '';
295
    $safe_style_dir = '';
296
297
    if ($handle = @opendir(CSS_UPLOAD_PATH)) {
298
        $counter = 1;
299
        while (false !== ($style_dir = readdir($handle))) {
300
            if (substr($style_dir, 0, 1) == '.') {
301
                // Skip directories starting with a '.'
302
                continue;
303
            }
304
            $dirpath = CSS_UPLOAD_PATH.$style_dir;
305
306
            if (is_dir($dirpath)) {
307
                if ($style_dir != '.' && $style_dir != '..') {
308
                    if (isset($_POST['style']) &&
309
                        (isset($_POST['preview']) || isset($_POST['download'])) &&
310
                        $_POST['style'] == $style_dir
311
                    ) {
312
                        $safe_style_dir = $style_dir;
313
                    } else {
314
                        if ($currentstyle == $style_dir || ($style_dir == 'chamilo' && !$currentstyle)) {
315
                            if (isset($_POST['style'])) {
316
                                $selected = Database::escape_string($_POST['style']);
317
                            } else {
318
                                $selected = $style_dir;
319
                            }
320
                        }
321
                    }
322
                    $show_name = ucwords(str_replace('_', ' ', $style_dir));
323
324
                    if ($is_style_changeable) {
325
                        $list_of_names[$style_dir]  = $show_name;
326
                    }
327
                    $counter++;
328
                }
329
            }
330
        }
331
        closedir($handle);
332
    }
333
334
    // Sort styles in alphabetical order.
335
    asort($list_of_names);
336
    $select_list = array();
337
    foreach ($list_of_names as $style_dir => $item) {
338
        $select_list[$style_dir] = $item;
339
    }
340
341
    $styles = &$form_change->addElement('select', 'style', get_lang('NameStylesheet'), $select_list);
342
    $styles->setSelected($selected);
343
344
    if ($form_change->validate()) {
345
        // Submit stylesheets.
346
        if (isset($_POST['save'])) {
347
            store_stylesheets();
348
            Display::display_normal_message(get_lang('Saved'));
349
        }
350
        if (isset($_POST['download'])) {
351
            $arch = api_get_path(SYS_ARCHIVE_PATH).$safe_style_dir.'.zip';
352
            $dir = api_get_path(SYS_CSS_PATH).'themes/'.$safe_style_dir;
353
            if (is_dir($dir)) {
354
                $zip = new PclZip($arch);
355
                // Remove path prefix except the style name and put file on disk
356
                $zip->create($dir, PCLZIP_OPT_REMOVE_PATH, substr($dir,0,-strlen($safe_style_dir)));
357
                //@TODO: use more generic script to download.
358
                $str = '<a class="btn btn-primary btn-large" href="' . api_get_path(WEB_CODE_PATH) . 'course_info/download.php?archive=' . str_replace(api_get_path(SYS_ARCHIVE_PATH), '', $arch) . '">'.get_lang('ClickHereToDownloadTheFile').'</a>';
359
                Display::display_normal_message($str, false);
360
            } else {
361
                Display::addFlash(Display::return_message(get_lang('FileNotFound'), 'warning'));
362
            }
363
        }
364
    }
365
    
366
    $logoForm = new FormValidator(
367
        'logo_upload',
368
        'post',
369
        'settings.php?category=Stylesheets#tabs-2'
370
    );
371
    
372
    $logoForm->addHtml(Display::return_message(sprintf(get_lang('TheLogoMustBeSizeXAndFormatY'), '250 x 70', 'PNG'), 'info'));
373
    
374
    $dir = api_get_path(SYS_PUBLIC_PATH).'css/themes/' . $selected . '/images/';
375
    $url = api_get_path(WEB_CSS_PATH).'themes/' . $selected . '/images/';
376
    $logoFileName = 'header-logo.png';
377
    $newLogoFileName = 'header-logo-custom.png';
378
    
379
    if (is_file($dir.$newLogoFileName)) {
380
        $logoForm->addLabel(get_lang('CurrentLogo'), '<img id="header-logo-custom" src="'. $url . $newLogoFileName .'?'. time() . '">'); 
381
    } else {
382
        $logoForm->addLabel(get_lang('CurrentLogo'), '<img id="header-logo-custom" src="'. $url . $logoFileName .'?'. time() . '">');
383
    }
384
    
385
    $logoForm->addFile('new_logo', get_lang('UpdateLogo'));
386
    $allowedFileTypes = ['png'];
387
    
388
    if (isset($_POST['logo_reset'])) {
389
        if (is_file($dir.$newLogoFileName)) {
390
            unlink($dir.$newLogoFileName);
391
            Display::display_normal_message(get_lang('ResetToTheOriginalLogo'));
392
            echo '<script>'
393
                . '$("#header-logo").attr("src","'.$url.$logoFileName.'");'
394
            . '</script>';
395
        }
396
    } elseif (isset($_POST['logo_upload'])) {
397
        
398
        $logoForm->addRule('new_logo', get_lang('InvalidExtension').' ('.implode(',', $allowedFileTypes).')', 'filetype', $allowedFileTypes);
399
        $logoForm->addRule('new_logo', get_lang('ThisFieldIsRequired'), 'required');
400
        
401
        if ($logoForm->validate()) {
402
            
403
            $imageInfo = getimagesize($_FILES['new_logo']['tmp_name']);
404
            $width = $imageInfo[0];
405
            $height = $imageInfo[1];
406
            if ($width <= 250 && $height <= 70 ) {
407
                if (is_file($dir.$newLogoFileName)) {
408
                    unlink($dir.$newLogoFileName);
409
                }
410
                
411
                $status = move_uploaded_file($_FILES['new_logo']['tmp_name'], $dir.$newLogoFileName);
412
413
                if ($status) {
414
                    Display::display_normal_message(get_lang('NewLogoUpdated'));
415
                    echo '<script>'
416
                            . '$("#header-logo").attr("src","'.$url.$newLogoFileName.'");'
417
                        . '</script>';
418
                } else {
419
                    Display::display_error_message('Error - '.get_lang('UplNoFileUploaded'));
420
                }
421
            } else {
422
                Display::display_error_message('Error - '.get_lang('InvalidImageDimensions'));
423
            }
424
        }
425
    }
426
427
    if ($is_style_changeable) {
428
        $group = [
429
            $form_change->addButtonSave(get_lang('SaveSettings'), 'save', true),
430
            $form_change->addButtonPreview(get_lang('Preview'), 'preview', true),
431
            $form_change->addButtonDownload(get_lang('Download'), 'download', true)
432
        ];
433
434
        $form_change->addGroup($group);
435
        
436
        $logoGroup = [
437
            $logoForm->addButtonUpload(get_lang('Upload'), 'logo_upload', true),
438
            $logoForm->addButtonCancel(get_lang('Reset'), 'logo_reset', true)
439
        ];
440
        
441
        $logoForm->addGroup($logoGroup);
442
443
        if ($show_upload_form) {
444
            echo '<script>
445
            $(function() {
446
                $( "#tabs" ).tabs();
447
            });
448
            </script>';
449
            echo Display::tabs(
450
                array(get_lang('Update'),get_lang('UpdateLogo'), get_lang('UploadNewStylesheet')),
451
                array($form_change->return_form(), $logoForm->return_form(), $form->return_form())
0 ignored issues
show
Deprecated Code introduced by
The method FormValidator::return_form() has been deprecated with message: use returnForm()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
452
            );
453
        } else {
454
            $form_change->display();
455
        }
456
        
457
        //Little hack to update the logo image in update form when submiting
458
        if (isset($_POST['logo_reset'])) {
459
            echo '<script>'
460
                    . '$("#header-logo-custom").attr("src","'.$url.$logoFileName.'");'
461
                . '</script>';
462
        } elseif (isset($_POST['logo_upload']) && is_file($dir.$newLogoFileName)) {
463
            echo '<script>'
464
                    . '$("#header-logo-custom").attr("src","'.$url.$newLogoFileName.'");'
465
                . '</script>';
466
        }
467
    } else {
468
        $form_change->freeze();
469
    }
470
}
471
472
/**
473
 * Creates the folder (if needed) and uploads the stylesheet in it
474
 *
475
 * @param array $values the values of the form
476
 * @param array $picture the values of the uploaded file
477
 *
478
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
479
 * @version May 2008
480
 * @since Dokeos 1.8.5
481
 */
482
function upload_stylesheet($values, $picture)
483
{
484
    $result = false;
485
    // Valid name for the stylesheet folder.
486
    $style_name = api_preg_replace('/[^A-Za-z0-9]/', '', $values['name_stylesheet']);
487
    $cssToUpload = CSS_UPLOAD_PATH;
488
489
    // Create the folder if needed.
490
491
    if (!is_dir($cssToUpload.$style_name.'/')) {
492
        mkdir($cssToUpload.$style_name.'/', api_get_permissions_for_new_directories());
493
    }
494
495
    $info = pathinfo($picture['name']);
496
497
    if ($info['extension'] == 'zip') {
498
        // Try to open the file and extract it in the theme.
499
        $zip = new ZipArchive();
500
        if ($zip->open($picture['tmp_name'])) {
501
            // Make sure all files inside the zip are images or css.
502
            $num_files = $zip->numFiles;
503
            $valid = true;
504
            $single_directory = true;
505
            $invalid_files = array();
506
507
            $allowedFiles = array(
508
                'jpg',
509
                'jpeg',
510
                'png',
511
                'gif',
512
                'css',
513
                'ico',
514
                'psd',
515
                'woff',
516
                'woff2',
517
                'xcf',
518
                'svg',
519
                'webp'
520
            );
521
522
            for ($i = 0; $i < $num_files; $i++) {
523
                $file = $zip->statIndex($i);
524
                if (substr($file['name'], -1) != '/') {
525
                    $path_parts = pathinfo($file['name']);
526
                    if (!in_array($path_parts['extension'], $allowedFiles)) {
527
                        $valid = false;
528
                        $invalid_files[] = $file['name'];
529
                    }
530
                }
531
532
                if (strpos($file['name'], '/') === false) {
533
                    $single_directory = false;
534
                }
535
            }
536
            if (!$valid) {
537
                $error_string = '<ul>';
538
                foreach ($invalid_files as $invalid_file) {
539
                    $error_string .= '<li>'.$invalid_file.'</li>';
540
                }
541
                $error_string .= '</ul>';
542
                Display::display_error_message(
543
                    get_lang('ErrorStylesheetFilesExtensionsInsideZip').$error_string,
544
                    false
545
                );
546
            } else {
547
                // If the zip does not contain a single directory, extract it.
548
                if (!$single_directory) {
549
                    // Extract zip file.
550
                    $zip->extractTo($cssToUpload.$style_name.'/');
551
                    $result = true;
552
                } else {
553
                    $extraction_path = $cssToUpload.$style_name.'/';
554
                    for ($i = 0; $i < $num_files; $i++) {
555
                        $entry = $zip->getNameIndex($i);
556
                        if (substr($entry, -1) == '/')
557
                            continue;
558
559
                        $pos_slash = strpos($entry, '/');
560
                        $entry_without_first_dir = substr($entry, $pos_slash + 1);
561
                        // If there is still a slash, we need to make sure the directories are created.
562
                        if (strpos($entry_without_first_dir, '/') !== false) {
563 View Code Duplication
                            if (!is_dir($extraction_path.dirname($entry_without_first_dir))) {
564
                                // Create it.
565
                                @mkdir($extraction_path.dirname($entry_without_first_dir), $mode = 0777, true);
566
                            }
567
                        }
568
569
                        $fp = $zip->getStream($entry);
570
                        $ofp = fopen($extraction_path.dirname($entry_without_first_dir).'/'.basename($entry), 'w');
571
572
                        while (!feof($fp)) {
573
                            fwrite($ofp, fread($fp, 8192));
574
                        }
575
576
                        fclose($fp);
577
                        fclose($ofp);
578
                    }
579
                    $result = true;
580
                }
581
            }
582
            $zip->close();
583
        } else {
584
            Display::display_error_message(get_lang('ErrorReadingZip').$info['extension'], false);
585
        }
586
    } else {
587
        // Simply move the file.
588
        move_uploaded_file($picture['tmp_name'], $cssToUpload.$style_name.'/'.$picture['name']);
589
        $result = true;
590
    }
591
592
    if ($result) {
593
        $fs = new Filesystem();
594
        $fs->mirror($cssToUpload, api_get_path(SYS_PATH).'web/css/themes/');
595
    }
596
597
    return $result;
598
}
599
600
/**
601
 * Store plugin regions.
602
 */
603
function store_regions()
604
{
605
    $plugin_obj = new AppPlugin();
606
607
    // Get a list of all current 'Plugins' settings
608
    $installed_plugins = $plugin_obj->get_installed_plugins();
609
610
    $shortlist_installed = array();
611
    if (!empty($installed_plugins)) {
612
        foreach ($installed_plugins as $plugin) {
613
            if (isset($plugin['subkey'])) {
614
                $shortlist_installed[] = $plugin['subkey'];
615
            }
616
        }
617
    }
618
    $shortlist_installed = array_flip(array_flip($shortlist_installed));
619
620
    $plugin_list = $plugin_obj->read_plugins_from_path();
621
622
    foreach ($plugin_list as $plugin) {
623
        if (isset($_POST['plugin_'.$plugin])) {
624
            $areas_to_installed = $_POST['plugin_'.$plugin];
625
            if (!empty($areas_to_installed)) {
626
                $plugin_obj->remove_all_regions($plugin);
627
                foreach ($areas_to_installed as $region) {
628
                    if (!empty($region) && $region != '-1' ) {
629
                        $plugin_obj->add_to_region($plugin, $region);
630
                    }
631
                }
632
            }
633
        }
634
    }
635
}
636
637
/**
638
 * This function allows easy activating and inactivating of plugins
639
 * @author Patrick Cool <[email protected]>, Ghent University
640
 */
641
function store_plugins()
642
{
643
    $appPlugin = new AppPlugin();
644
645
    // Get a list of all current 'Plugins' settings
646
    $plugin_list = $appPlugin->read_plugins_from_path();
647
648
    $installed_plugins = array();
649
650
    foreach ($plugin_list as $plugin) {
651
        if (isset($_POST['plugin_'.$plugin])) {
652
            $appPlugin->install($plugin);
653
            $installed_plugins[] = $plugin;
654
        }
655
    }
656
657
    if (!empty($installed_plugins)) {
658
        $remove_plugins = array_diff($plugin_list, $installed_plugins);
659
    } else {
660
        $remove_plugins = $plugin_list;
661
    }
662
663
    foreach ($remove_plugins as $plugin) {
664
        $appPlugin->uninstall($plugin);
665
    }
666
}
667
668
/**
669
 * This function allows the platform admin to choose which should be the default stylesheet
670
 * @author Patrick Cool <[email protected]>, Ghent University
671
 */
672
function store_stylesheets()
673
{
674
    // Insert the stylesheet.
675
    if (is_style($_POST['style'])) {
676
        api_set_setting(
677
            'stylesheets',
678
            $_POST['style'],
679
            null,
680
            'stylesheets',
681
            api_get_current_access_url_id()
682
        );
683
    }
684
    return true;
685
}
686
687
/**
688
 * This function checks if the given style is a recognize style that exists in the css directory as
689
 * a standalone directory.
690
 * @param string    Style
691
 * @return bool     True if this style is recognized, false otherwise
692
 */
693
function is_style($style)
694
{
695
    $dir = CSS_UPLOAD_PATH;
696
    $dirs = scandir($dir);
697
    $style = str_replace(array('/', '\\'), array('', ''), $style); // Avoid slashes or backslashes.
698
    if (in_array($style, $dirs) && is_dir($dir.$style)) {
699
        return true;
700
    }
701
    return false;
702
}
703
704
/**
705
 * Search options
706
 * TODO: support for multiple site. aka $_configuration['access_url'] == 1
707
 * @author Marco Villegas <[email protected]>
708
 */
709
function handle_search()
710
{
711
    global $SettingsStored, $_configuration;
712
713
    require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
714
    $search_enabled = api_get_setting('search_enabled');
715
716
    $form = new FormValidator('search-options', 'post', api_get_self().'?category=Search');
717
    $values = api_get_settings_options('search_enabled');
718
    $form->addElement('header', null, get_lang('SearchEnabledTitle'));
719
720
    $group = array ();
721
    if (is_array($values)) {
722 View Code Duplication
        foreach ($values as $key => $value) {
723
            $element = & $form->createElement('radio', 'search_enabled', '', get_lang($value['display_text']), $value['value']);
724
            $group[] = $element;
725
        }
726
    }
727
    //SearchEnabledComment
728
    $form->addGroup($group, 'search_enabled', array(get_lang('SearchEnabledTitle'), get_lang('SearchEnabledComment')), '<br />', false);
729
730
    $search_enabled = api_get_setting('search_enabled');
731
732
    if ($form->validate()) {
733
        $formvalues = $form->exportValues();
734
        $r = api_set_settings_category('Search', 'false', $_configuration['access_url']);
735
        // Save the settings.
736
        foreach ($formvalues as $key => $value) {
737
            $result = api_set_setting($key, $value, null, null);
738
        }
739
        $search_enabled = $formvalues['search_enabled'];
740
        Display::display_confirmation_message($SettingsStored);
741
    }
742
    $specific_fields = get_specific_field_list();
743
744
    if ($search_enabled == 'true') {
745
        $values = api_get_settings_options('search_show_unlinked_results');
746
        $group = array ();
747 View Code Duplication
        foreach ($values as $key => $value) {
748
            $element = & $form->createElement('radio', 'search_show_unlinked_results', '', get_lang($value['display_text']), $value['value']);
749
            $group[] = $element;
750
        }
751
        $form->addGroup($group, 'search_show_unlinked_results', array(get_lang('SearchShowUnlinkedResultsTitle'),get_lang('SearchShowUnlinkedResultsComment')), '', false);
752
        $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
753
754
        $sf_values = array();
755
        foreach ($specific_fields as $sf) {
756
            $sf_values[$sf['code']] = $sf['name'];
757
        }
758
        $group = array();
759
        $url =  Display::div(Display::url(get_lang('AddSpecificSearchField'), 'specific_fields.php'), array('class'=>'sectioncomment'));
760
        if (empty($sf_values)) {
761
            $form->addElement('label', [get_lang('SearchPrefilterPrefix'), $url]);
762
        } else {
763
            $form->addElement('select', 'search_prefilter_prefix', array(get_lang('SearchPrefilterPrefix'), $url), $sf_values, '');
764
            $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
765
        }
766
    }
767
768
    $default_values['search_enabled'] = $search_enabled;
769
770
    $form->addButtonSave(get_lang('Save'));
771
    $form->setDefaults($default_values);
772
773
    echo '<div id="search-options-form">';
774
    $form->display();
775
    echo '</div>';
776
777
    if ($search_enabled == 'true') {
778
        $xapian_path = api_get_path(SYS_UPLOAD_PATH).'plugins/xapian/searchdb';
779
780
        /*
781
        @todo Test the Xapian connection
782
        if (extension_loaded('xapian')) {
783
            require_once 'xapian.php';
784
            try {
785
                $db = new XapianDatabase($xapian_path.'/');
786
            } catch (Exception $e) {
787
                var_dump($e->getMessage());
788
            }
789
790
            require_once api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php';
791
            require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
792
            require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
793
794
            $indexable = new IndexableChunk();
795
            $indexable->addValue("content", 'Test');
796
797
            $di = new DokeosIndexer();
798
            $di->connectDb(NULL, NULL, 'english');
799
            $di->addChunk($indexable);
800
            $did = $di->index();
801
        }
802
        */
803
804
        $xapian_loaded = Display::return_icon('bullet_green.png', get_lang('Ok'));
805
        $dir_exists = Display::return_icon('bullet_green.png', get_lang('Ok'));
806
        $dir_is_writable = Display::return_icon('bullet_green.png', get_lang('Ok'));
807
        $specific_fields_exists = Display::return_icon('bullet_green.png', get_lang('Ok'));
808
809
        //Testing specific fields
810
        if (empty($specific_fields)) {
811
            $specific_fields_exists = Display::return_icon('bullet_red.png', get_lang('AddSpecificSearchField'));
812
        }
813
        //Testing xapian extension
814
        if (!extension_loaded('xapian')) {
815
            $xapian_loaded = Display::return_icon('bullet_red.png', get_lang('Error'));
816
        }
817
        //Testing xapian searchdb path
818
        if (!is_dir($xapian_path)) {
819
            $dir_exists = Display::return_icon('bullet_red.png', get_lang('Error'));
820
        }
821
        //Testing xapian searchdb path is writable
822
        if (!is_writable($xapian_path)) {
823
            $dir_is_writable = Display::return_icon('bullet_red.png', get_lang('Error'));
824
        }
825
826
        $data[] = array(get_lang('XapianModuleInstalled'),$xapian_loaded);
827
        $data[] = array(get_lang('DirectoryExists').' - '.$xapian_path,$dir_exists);
828
        $data[] = array(get_lang('IsWritable').' - '.$xapian_path,$dir_is_writable);
829
        $data[] = array(get_lang('SpecificSearchFieldsAvailable') ,$specific_fields_exists);
830
831
        echo Display::tag('h3', get_lang('Settings'));
832
        $table = new SortableTableFromArray($data);
833
        $table->set_header(0, get_lang('Setting'), false);
834
        $table->set_header(1, get_lang('Status'), false);
835
        echo  $table->display();
836
837
        //@todo windows support
838
        if (api_is_windows_os() == false) {
839
            $list_of_programs = array('pdftotext','ps2pdf', 'catdoc','html2text','unrtf', 'catppt', 'xls2csv');
840
841
            foreach($list_of_programs as $program) {
842
                $output = [];
843
                $ret_val = null;
844
                exec("which $program", $output, $ret_val);
845
846
                if (!$output) {
847
                    $output[] = '';
848
                }
849
850
                $icon = Display::return_icon('bullet_red.png', get_lang('NotInstalled'));
851
                if (!empty($output[0])) {
852
                    $icon = Display::return_icon('bullet_green.png', get_lang('Installed'));
853
                }
854
                $data2[]= array($program, $output[0], $icon);
855
            }
856
            echo Display::tag('h3', get_lang('ProgramsNeededToConvertFiles'));
857
            $table = new SortableTableFromArray($data2);
858
            $table->set_header(0, get_lang('Program'), false);
859
            $table->set_header(1, get_lang('Path'), false);
860
            $table->set_header(2, get_lang('Status'), false);
861
            echo  $table->display();
862
        } else {
863
            Display::display_warning_message(
864
                get_lang('YouAreUsingChamiloInAWindowsPlatformSadlyYouCantConvertDocumentsInOrderToSearchTheContentUsingThisTool')
865
            );
866
        }
867
    }
868
}
869
870
/**
871
 * Wrapper for the templates
872
 *
873
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
874
 * @author Julio Montoya.
875
 * @version August 2008
876
 * @since Dokeos 1.8.6
877
 */
878
function handle_templates() {
879
    /* Drive-by fix to avoid undefined var warnings, without repeating
880
     * isset() combos all over the place. */
881
    $action = isset($_GET['action']) ? $_GET['action'] : "invalid";
882
883
    if ($action != 'add') {
884
        echo '<div class="actions" style="margin-left: 1px;">';
885
        echo '<a href="settings.php?category=Templates&action=add">'.
886
                Display::return_icon('new_template.png', get_lang('AddTemplate'),'',ICON_SIZE_MEDIUM).'</a>';
887
        echo '</div>';
888
    }
889
890
    if ($action == 'add' || ($action == 'edit' && is_numeric($_GET['id']))) {
891
        add_edit_template();
892
893
        // Add event to the system log.
894
        $user_id = api_get_user_id();
895
        $category = $_GET['category'];
896
        Event::addEvent(
897
            LOG_CONFIGURATION_SETTINGS_CHANGE,
898
            LOG_CONFIGURATION_SETTINGS_CATEGORY,
899
            $category,
900
            api_get_utc_datetime(),
901
            $user_id
902
        );
903
    } else {
904
        if ($action == 'delete' && is_numeric($_GET['id'])) {
905
            delete_template($_GET['id']);
906
907
            // Add event to the system log
908
            $user_id = api_get_user_id();
909
            $category = $_GET['category'];
910
            Event::addEvent(
911
                LOG_CONFIGURATION_SETTINGS_CHANGE,
912
                LOG_CONFIGURATION_SETTINGS_CATEGORY,
913
                $category,
914
                api_get_utc_datetime(),
915
                $user_id
916
            );
917
        }
918
        display_templates();
919
    }
920
}
921
922
/**
923
 * Display a sortable table with all the templates that the platform administrator has defined.
924
 *
925
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
926
 * @version August 2008
927
 * @since Dokeos 1.8.6
928
 */
929
function display_templates()
930
{
931
    $table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data', 1);
932
    $table->set_additional_parameters(array('category' => Security::remove_XSS($_GET['category'])));
933
    $table->set_header(0, get_lang('Image'), true, array('style' => 'width: 101px;'));
934
    $table->set_header(1, get_lang('Title'));
935
    $table->set_header(2, get_lang('Actions'), false, array('style' => 'width:50px;'));
936
    $table->set_column_filter(2, 'actions_filter');
937
    $table->set_column_filter(0, 'image_filter');
938
    $table->display();
939
}
940
941
/**
942
 * Gets the number of templates that are defined by the platform admin.
943
 *
944
 * @return integer
945
 *
946
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
947
 * @version August 2008
948
 * @since Dokeos 1.8.6
949
 */
950
function get_number_of_templates() {
951
    // Database table definition.
952
    $table_system_template = Database :: get_main_table('system_template');
953
954
    // The sql statement.
955
    $sql = "SELECT COUNT(id) AS total FROM $table_system_template";
956
    $result = Database::query($sql);
957
    $row = Database::fetch_array($result);
958
959
    // Returning the number of templates.
960
    return $row['total'];
961
}
962
963
/**
964
 * Gets all the template data for the sortable table.
965
 *
966
 * @param integer $from the start of the limit statement
967
 * @param integer $number_of_items the number of elements that have to be retrieved from the database
968
 * @param integer $column the column that is
969
 * @param string $direction the sorting direction (ASC or DESC�
970
 * @return array
971
 *
972
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
973
 * @version August 2008
974
 * @since Dokeos 1.8.6
975
 */
976
function get_template_data($from, $number_of_items, $column, $direction) {
977
    // Database table definition.
978
    $table_system_template = Database :: get_main_table('system_template');
979
980
    // The sql statement.
981
    $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
982
    $sql .= " ORDER BY col$column $direction ";
983
    $sql .= " LIMIT $from,$number_of_items";
984
    $result = Database::query($sql);
985
    $return = array();
986
    while ($row = Database::fetch_array($result)) {
987
        $row['1'] = get_lang($row['1']);
988
        $return[] = $row;
989
    }
990
    // Returning all the information for the sortable table.
991
    return $return;
992
}
993
994
/**
995
 * display the edit and delete icons in the sortable table
996
 *
997
 * @param integer $id the id of the template
998
 * @return html code for the link to edit and delete the template
999
 *
1000
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
1001
 * @version August 2008
1002
 * @since Dokeos 1.8.6
1003
 */
1004
function actions_filter($id) {
1005
    $return = '<a href="settings.php?category=Templates&action=edit&id='.Security::remove_XSS($id).'">'.Display::return_icon('edit.png', get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>';
1006
    $return .= '<a href="settings.php?category=Templates&action=delete&id='.Security::remove_XSS($id).'" onClick="javascript:if(!confirm('."'".get_lang('ConfirmYourChoice')."'".')) return false;">'.Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>';
1007
    return $return;
1008
}
1009
1010
/**
1011
 * Display the image of the template in the sortable table
1012
 *
1013
 * @param string $image the image
1014
 * @return html code for the image
1015
 *
1016
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
1017
 * @version August 2008
1018
 * @since Dokeos 1.8.6
1019
 */
1020
function image_filter($image) {
1021
    if (!empty($image)) {
1022
        return '<img src="'.api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/'.$image.'" alt="'.get_lang('TemplatePreview').'"/>';
1023
    } else {
1024
        return '<img src="'.api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>';
1025
    }
1026
}
1027
1028
/**
1029
 * Add (or edit) a template. This function displays the form and also takes
1030
 * care of uploading the image and storing the information in the database
1031
 *
1032
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
1033
 * @version August 2008
1034
 * @since Dokeos 1.8.6
1035
 */
1036
function add_edit_template() {
1037
    // Initialize the object.
1038
    $id = isset($_GET['id']) ? '&id='.Security::remove_XSS($_GET['id']) : '';
1039
    $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.Security::remove_XSS($_GET['action']).$id);
1040
1041
    // Setting the form elements: the header.
1042
    if ($_GET['action'] == 'add') {
1043
        $title = get_lang('AddTemplate');
1044
    } else {
1045
        $title = get_lang('EditTemplate');
1046
    }
1047
    $form->addElement('header', '', $title);
1048
1049
    // Setting the form elements: the title of the template.
1050
    $form->addText('title', get_lang('Title'), false);
1051
1052
    // Setting the form elements: the content of the template (wysiwyg editor).
1053
    $form->addElement('html_editor', 'template_text', get_lang('Text'), null, array('ToolbarSet' => 'AdminTemplates', 'Width' => '100%', 'Height' => '400'));
1054
1055
    // Setting the form elements: the form to upload an image to be used with the template.
1056
    $form->addElement('file','template_image',get_lang('Image'),'');
1057
1058
    // Setting the form elements: a little bit information about the template image.
1059
    $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
1060
1061
    // Getting all the information of the template when editing a template.
1062
    if ($_GET['action'] == 'edit') {
1063
        // Database table definition.
1064
        $table_system_template = Database :: get_main_table('system_template');
1065
        $sql = "SELECT * FROM $table_system_template WHERE id = ".intval($_GET['id'])."";
1066
        $result = Database::query($sql);
1067
        $row = Database::fetch_array($result);
1068
1069
        $defaults['template_id']    = intval($_GET['id']);
1070
        $defaults['template_text']  = $row['content'];
1071
        // Forcing get_lang().
1072
        $defaults['title']          = get_lang($row['title']);
1073
1074
        // Adding an extra field: a hidden field with the id of the template we are editing.
1075
        $form->addElement('hidden', 'template_id');
1076
1077
        // Adding an extra field: a preview of the image that is currently used.
1078
        if (!empty($row['image'])) {
1079
            $form->addElement('static', 'template_image_preview', '', '<img src="'.api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/'.$row['image'].'" alt="'.get_lang('TemplatePreview').'"/>');
1080
        } else {
1081
            $form->addElement('static', 'template_image_preview', '', '<img src="'.api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>');
1082
        }
1083
1084
        // Setting the information of the template that we are editing.
1085
        $form->setDefaults($defaults);
1086
    }
1087
    // Setting the form elements: the submit button.
1088
    $form->addButtonSave(get_lang('Ok'), 'submit');
1089
1090
    // Setting the rules: the required fields.
1091
    $form->addRule('template_image', get_lang('ThisFieldIsRequired'), 'required');
1092
    $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
1093
    $form->addRule('template_text', get_lang('ThisFieldIsRequired'), 'required');
1094
1095
    // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
1096
    if ($form->validate()) {
1097
1098
        $check = Security::check_token('post');
1099
        if ($check) {
1100
            // Exporting the values.
1101
            $values = $form->exportValues();
1102
            // Upload the file.
1103
            if (!empty($_FILES['template_image']['name'])) {
1104
                $upload_ok = process_uploaded_file($_FILES['template_image']);
1105
1106
                if ($upload_ok) {
1107
                    // Try to add an extension to the file if it hasn't one.
1108
                    $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
1109
1110
                    // The upload directory.
1111
                    $upload_dir = api_get_path(SYS_APP_PATH).'home/default_platform_document/template_thumb/';
1112
1113
                    // Create the directory if it does not exist.
1114
                    if (!is_dir($upload_dir)) {
1115
                        mkdir($upload_dir, api_get_permissions_for_new_directories());
1116
                    }
1117
1118
                    // Resize the preview image to max default and upload.
1119
                    $temp = new Image($_FILES['template_image']['tmp_name']);
1120
                    $picture_info = $temp->get_image_info();
1121
1122
                    $max_width_for_picture = 100;
1123
1124
                    if ($picture_info['width'] > $max_width_for_picture) {
1125
                        $temp->resize($max_width_for_picture);
1126
                    }
1127
                    $temp->send_image($upload_dir.$new_file_name);
1128
                }
1129
            }
1130
1131
            // Store the information in the database (as insert or as update).
1132
            $table_system_template = Database :: get_main_table('system_template');
1133
            if ($_GET['action'] == 'add') {
1134
                $content_template =  Security::remove_XSS($values['template_text'], COURSEMANAGERLOWSECURITY);
1135
                $params = [
1136
                    'title' =>  $values['title'],
1137
                    'content' => $content_template,
1138
                    'image' => $new_file_name
1139
                ];
1140
                Database::insert($table_system_template, $params);
1141
1142
                // Display a feedback message.
1143
                Display::display_confirmation_message(get_lang('TemplateAdded'));
1144
                echo '<a href="settings.php?category=Templates&action=add">'.Display::return_icon('new_template.png', get_lang('AddTemplate'),'',ICON_SIZE_MEDIUM).'</a>';
1145
            } else {
1146
                $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
1147
                $sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."', content = '".$content_template."'";
1148
                if (!empty($new_file_name)) {
1149
                    $sql .= ", image = '".Database::escape_string($new_file_name)."'";
1150
                }
1151
                $sql .= " WHERE id = ".intval($_GET['id'])."";
1152
                Database::query($sql);
1153
1154
                // Display a feedback message.
1155
                Display::display_confirmation_message(get_lang('TemplateEdited'));
1156
            }
1157
        }
1158
        Security::clear_token();
1159
        display_templates();
1160
    } else {
1161
        $token = Security::get_token();
1162
        $form->addElement('hidden','sec_token');
1163
        $form->setConstants(array('sec_token' => $token));
1164
        // Display the form.
1165
        $form->display();
1166
    }
1167
}
1168
1169
/**
1170
 * Delete a template
1171
 *
1172
 * @param integer $id the id of the template that has to be deleted
1173
 *
1174
 * @author Patrick Cool <[email protected]>, Ghent University, Belgium
1175
 * @version August 2008
1176
 * @since Dokeos 1.8.6
1177
 */
1178
function delete_template($id) {
1179
    // First we remove the image.
1180
    $table_system_template = Database :: get_main_table('system_template');
1181
    $sql = "SELECT * FROM $table_system_template WHERE id = ".intval($id)."";
1182
    $result = Database::query($sql);
1183
    $row = Database::fetch_array($result);
1184
    if (!empty($row['image'])) {
1185
        @unlink(api_get_path(SYS_APP_PATH).'home/default_platform_document/template_thumb/'.$row['image']);
1186
    }
1187
1188
    // Now we remove it from the database.
1189
    $sql = "DELETE FROM $table_system_template WHERE id = ".intval($id)."";
1190
    Database::query($sql);
1191
1192
    // Display a feedback message.
1193
    Display::display_confirmation_message(get_lang('TemplateDeleted'));
1194
}
1195
1196
/**
1197
 * Returns the list of timezone identifiers used to populate the select
1198
 * This function is called through a call_user_func() in the generate_settings_form function.
1199
 * @return array List of timezone identifiers
1200
 *
1201
 * @author Guillaume Viguier <[email protected]>
1202
 * @since Chamilo 1.8.7
1203
 */
1204
function select_timezone_value() {
1205
    return api_get_timezones();
1206
}
1207
1208
/**
1209
 * Returns an array containing the list of options used to populate the gradebook_number_decimals variable
1210
 * This function is called through a call_user_func() in the generate_settings_form function.
1211
 * @return array List of gradebook_number_decimals options
1212
 *
1213
 * @author Guillaume Viguier <[email protected]>
1214
 */
1215
function select_gradebook_number_decimals() {
1216
    return array('0', '1', '2');
1217
}
1218
1219
function select_gradebook_default_grade_model_id() {
1220
    $grade_model = new GradeModel();
1221
    $models = $grade_model->get_all();
1222
    $options = array();
1223
    $options[-1] = get_lang('None');
1224
    if (!empty($models)) {
1225
        foreach ($models as $model) {
1226
            $options[$model['id']] = $model['name'];
1227
        }
1228
    }
1229
    return $options;
1230
}
1231
1232
/**
1233
 * Updates the gradebook score custom values using the scoredisplay class of the
1234
 * gradebook module
1235
 *
1236
 * @param array List of gradebook score custom values
1237
 *
1238
 * @author Guillaume Viguier <[email protected]>
1239
 */
1240
function update_gradebook_score_display_custom_values($values) {
1241
    $scoredisplay = ScoreDisplay::instance();
1242
    $scores = $values['gradebook_score_display_custom_values_endscore'];
1243
    $displays = $values['gradebook_score_display_custom_values_displaytext'];
1244
    $nr_displays = count($displays);
1245
    $final = array();
1246
    for ($i = 1; $i < $nr_displays; $i++) {
1247
        if (!empty($scores[$i]) && !empty($displays[$i])) {
1248
            $final[$i]['score'] = $scores[$i];
1249
            $final[$i]['display'] = $displays[$i];
1250
        }
1251
    }
1252
    $scoredisplay->update_custom_score_display_settings($final);
1253
}
1254
1255
function generate_settings_form($settings, $settings_by_access_list)
1256
{
1257
    global $_configuration, $settings_to_avoid, $convert_byte_to_mega_list;
1258
    $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1259
1260
    $form = new FormValidator('settings', 'post', 'settings.php?category='.Security::remove_XSS($_GET['category']));
1261
1262
    $form->addElement('hidden', 'search_field', (!empty($_GET['search_field'])?Security::remove_XSS($_GET['search_field']):null));
1263
1264
    $url_id = api_get_current_access_url_id();
1265
1266
    if (!empty($_configuration['multiple_access_urls']) && api_is_global_platform_admin() && $url_id == 1) {
1267
        $group = array();
1268
        $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll'));
1269
        $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll'));
1270
        $form->addGroup($group, 'buttons_in_action_right');
1271
    }
1272
1273
    $default_values = array();
1274
    $url_info = api_get_access_url($url_id);
1275
    $i = 0;
1276
    $addedSettings = [];
1277
    foreach ($settings as $row) {
1278
        if (in_array($row['variable'], array_keys($settings_to_avoid))) {
1279
            continue;
1280
        }
1281
1282
        if (in_array($row['variable'], $addedSettings)) {
1283
            continue;
1284
        }
1285
1286
        $addedSettings[] = $row['variable'];
1287
1288
        if (!empty($_configuration['multiple_access_urls'])) {
1289
            if (api_is_global_platform_admin()) {
1290
                if ($row['access_url_locked'] == 0) {
1291
                    if ($url_id == 1) {
1292
                        if ($row['access_url_changeable'] == '1') {
1293
                            $form->addElement('html', '<div style="float: right;"><a class="share_this_setting" data_status = "0"  data_to_send = "'.$row['variable'].'" href="javascript:void(0);">'.
1294
                                Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting')).'</a></div>');
1295
                        } else {
1296
                            $form->addElement('html', '<div style="float: right;"><a class="share_this_setting" data_status = "1" data_to_send = "'.$row['variable'].'" href="javascript:void(0);">'.
1297
                                Display::return_icon('shared_setting_na.png', get_lang('ChangeSharedSetting')).'</a></div>');
1298
                        }
1299
                    } else {
1300
                        if ($row['access_url_changeable'] == '1') {
1301
                            $form->addElement('html', '<div style="float: right;">'.
1302
                                Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting')).'</div>');
1303
                        } else {
1304
                            $form->addElement('html', '<div style="float: right;">'.
1305
                                Display::return_icon('shared_setting_na.png', get_lang('ChangeSharedSetting')).'</div>');
1306
                        }
1307
                    }
1308
                }
1309
            }
1310
        }
1311
1312
        $hideme = array();
1313
        $hide_element = false;
1314
1315
        if ($_configuration['access_url'] != 1) {
1316
            if ($row['access_url_changeable'] == 0) {
1317
                // We hide the element in other cases (checkbox, radiobutton) we 'freeze' the element.
1318
                $hide_element = true;
1319
                $hideme = array('disabled');
1320
            } elseif ($url_info['active'] == 1) {
1321
                // We show the elements.
1322
                if (empty($row['variable']))
1323
                    $row['variable'] = 0;
1324
                if (empty($row['subkey']))
1325
                    $row['subkey'] = 0;
1326
                if (empty($row['category']))
1327
                    $row['category'] = 0;
1328
1329
                if (is_array($settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ])) {
1330
                    // We are sure that the other site have a selected value.
1331
                    if ($settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ]['selected_value'] != '')
1332
                        $row['selected_value'] = $settings_by_access_list[$row['variable']] [$row['subkey']] [$row['category']]['selected_value'];
1333
                }
1334
                // There is no else{} statement because we load the default $row['selected_value'] of the main Chamilo site.
1335
            }
1336
        }
1337
1338
        switch ($row['type']) {
1339
            case 'textfield':
1340
                if (in_array($row['variable'], $convert_byte_to_mega_list)) {
1341
                    $form->addElement(
1342
                        'text',
1343
                        $row['variable'],
1344
                        array(
1345
                            get_lang($row['title']),
1346
                            get_lang($row['comment']),
1347
                            get_lang('MB'),
1348
                        ),
1349
                        array('maxlength' => '8')
1350
                    );
1351
                    $form->applyFilter($row['variable'], 'html_filter');
1352
                    $default_values[$row['variable']] = round($row['selected_value']/1024/1024, 1);
1353
                } elseif ($row['variable'] == 'account_valid_duration') {
1354
                    $form->addElement(
1355
                        'text',
1356
                        $row['variable'],
1357
                        array(
1358
                            get_lang($row['title']),
1359
                            get_lang($row['comment']),
1360
                        ),
1361
                        array('maxlength' => '5')
1362
                    );
1363
                    $form->applyFilter($row['variable'], 'html_filter');
1364
                    $default_values[$row['variable']] = $row['selected_value'];
1365
1366
                    // For platform character set selection: Conversion of the textfield to a select box with valid values.
1367
                } elseif ($row['variable'] == 'platform_charset') {
1368
                    continue;
1369
                } else {
1370
                    $hideme['class'] = 'col-md-4';
1371
                    $form->addElement(
1372
                        'text',
1373
                        $row['variable'],
1374
                        array(
1375
                            get_lang($row['title']),
1376
                            get_lang($row['comment']),
1377
                        ),
1378
                        $hideme
1379
                    );
1380
                    $form->applyFilter($row['variable'],'html_filter');
1381
                    $default_values[$row['variable']] = $row['selected_value'];
1382
                }
1383
                break;
1384
            case 'textarea':
1385
                if ($row['variable'] == 'header_extra_content') {
1386
                    $file = api_get_path(SYS_PATH).api_get_home_path().'header_extra_content.txt';
1387
                    $value = '';
1388
                    if (file_exists($file)) {
1389
                        $value = file_get_contents($file);
1390
                    }
1391
                    $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])) , array('rows'=>'10'), $hideme);
1392
                    $default_values[$row['variable']] = $value;
1393
                } elseif ($row['variable'] == 'footer_extra_content') {
1394
                    $file = api_get_path(SYS_PATH).api_get_home_path().'footer_extra_content.txt';
1395
                    $value = '';
1396
                    if (file_exists($file)) {
1397
                        $value = file_get_contents($file);
1398
                    }
1399
                    $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])) , array('rows'=>'10'), $hideme);
1400
                    $default_values[$row['variable']] = $value;
1401
                } else {
1402
                    $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])) , array('rows'=>'10'), $hideme);
1403
                    $default_values[$row['variable']] = $row['selected_value'];
1404
                }
1405
                break;
1406
            case 'radio':
1407
                $values = api_get_settings_options($row['variable']);
1408
                $group = array ();
1409
                if (is_array($values)) {
1410
                    foreach ($values as $key => $value) {
1411
                        $element = &$form->createElement(
1412
                            'radio',
1413
                            $row['variable'],
1414
                            '',
1415
                            get_lang($value['display_text']),
1416
                            $value['value']
1417
                        );
1418
                        if ($hide_element) {
1419
                            $element->freeze();
1420
                        }
1421
                        $group[] = $element;
1422
                    }
1423
                }
1424
                $form->addGroup(
1425
                    $group,
1426
                    $row['variable'],
1427
                    array(get_lang($row['title']), get_lang($row['comment'])),
1428
                    '',
1429
                    false
1430
                );
1431
                $default_values[$row['variable']] = $row['selected_value'];
1432
                break;
1433
            case 'checkbox';
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1434
                // 1. We collect all the options of this variable.
1435
                $sql = "SELECT * FROM $table_settings_current
1436
                        WHERE variable='".$row['variable']."' AND access_url =  1";
1437
1438
                $result = Database::query($sql);
1439
                $group = array ();
1440
                while ($rowkeys = Database::fetch_array($result)) {
1441
                    // Profile tab option should be hidden when the social tool is enabled.
1442
                    if (api_get_setting('allow_social_tool') == 'true') {
1443
                        if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_profile') {
1444
                            continue;
1445
                        }
1446
                    }
1447
1448
                    // Hiding the gradebook option.
1449
                    if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_gradebook') {
1450
                        continue;
1451
                    }
1452
1453
                    $element = &$form->createElement(
1454
                        'checkbox',
1455
                        $rowkeys['subkey'],
1456
                        '',
1457
                        get_lang($rowkeys['subkeytext'])
1458
                    );
1459
1460
                    if ($row['access_url_changeable'] == 1) {
1461
                        // 2. We look into the DB if there is a setting for a specific access_url.
1462
                        $access_url = $_configuration['access_url'];
1463
                        if (empty($access_url)) {
1464
                            $access_url = 1;
1465
                        }
1466
                        $sql = "SELECT selected_value FROM $table_settings_current
1467
                                WHERE
1468
                                    variable='".$rowkeys['variable']."' AND
1469
                                    subkey='".$rowkeys['subkey']."' AND
1470
                                    subkeytext='".$rowkeys['subkeytext']."' AND
1471
                                    access_url =  $access_url";
1472
                        $result_access = Database::query($sql);
1473
                        $row_access = Database::fetch_array($result_access);
1474
                        if ($row_access['selected_value'] == 'true' && !$form->isSubmitted()) {
1475
                            $element->setChecked(true);
1476
                        }
1477
                    } else {
1478
                        if ($rowkeys['selected_value'] == 'true' && !$form->isSubmitted()) {
1479
                            $element->setChecked(true);
1480
                        }
1481
                    }
1482
                    if ($hide_element) {
1483
                        $element->freeze();
1484
                    }
1485
                    $group[] = $element;
1486
                }
1487
                $form->addGroup(
1488
                    $group,
1489
                    $row['variable'],
1490
                    array(get_lang($row['title']), get_lang($row['comment'])),
1491
                    ''
1492
                );
1493
                break;
1494
            case 'link':
1495
                $form->addElement('static', null, array(get_lang($row['title']), get_lang($row['comment'])), get_lang('CurrentValue').' : '.$row['selected_value'], $hideme);
1496
                break;
1497
            case 'select':
1498
                /*
1499
                * To populate the list of options, the select type dynamically calls a function that must be called select_ + the name of the variable being displayed.
1500
                * The functions being called must be added to the file settings.lib.php.
1501
                */
1502
                $form->addElement('select', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), call_user_func('select_'.$row['variable']), $hideme);
1503
                $default_values[$row['variable']] = $row['selected_value'];
1504
                break;
1505
            case 'custom':
1506
                break;
1507
        }
1508
1509
        switch ($row['variable']) {
1510
            case 'pdf_export_watermark_enable':
1511
                $url =  PDF::get_watermark(null);
1512
1513
                if ($url != false) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $url of type string|false against false; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
1514
                    $delete_url = '<a href="?delete_watermark">'.get_lang('DelImage').' '.Display::return_icon('delete.png',get_lang('DelImage')).'</a>';
1515
                    $form->addElement('html', '<div style="max-height:100px; max-width:100px; margin-left:162px; margin-bottom:10px; clear:both;"><img src="'.$url.'" style="margin-bottom:10px;" />'.$delete_url.'</div>');
1516
                }
1517
1518
                $form->addElement('file', 'pdf_export_watermark_path', get_lang('AddWaterMark'));
1519
                $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif');
1520
                $form->addRule('pdf_export_watermark_path', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
1521
1522
                break;
1523
            case 'timezone_value':
1524
                $timezone = $row['selected_value'];
1525
                if (empty($timezone)) {
1526
                    $timezone = _api_get_timezone();
1527
                }
1528
                $form->addElement('html', sprintf(get_lang('LocalTimeUsingPortalTimezoneXIsY'), $timezone, api_get_local_time()));
1529
                break;
1530
        }
1531
    } // end for
1532
1533
    if (!empty($settings)) {
1534
        $form->setDefaults($default_values);
1535
    }
1536
    $form->addHtml('<div class="bottom_actions">');
1537
    $form->addButtonSave(get_lang('SaveSettings'));
1538
    $form->addHtml('</div>');
1539
    return $form;
1540
}
1541
1542
/**
1543
 * Searches a platform setting in all categories except from the Plugins category
1544
 * @param string $search
1545
 * @return array
1546
 */
1547
function search_setting($search)
1548
{
1549
    if (empty($search)) {
1550
        return array();
1551
    }
1552
    $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1553
    $sql = "SELECT * FROM $table_settings_current
1554
            WHERE category <> 'Plugins' ORDER BY id ASC ";
1555
    $result = Database::store_result(Database::query($sql), 'ASSOC');
0 ignored issues
show
Bug introduced by
It seems like \Database::query($sql) can be null; however, store_result() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
1556
    $settings = array();
1557
1558
    $search = api_strtolower($search);
1559
1560
    if (!empty($result)) {
1561
        foreach ($result as $setting) {
1562
            $found = false;
1563
1564
            $title = api_strtolower(get_lang($setting['title']));
1565
            // try the title
1566
            if (strpos($title, $search) === false) {
1567
                $comment = api_strtolower(get_lang($setting['comment']));
1568
                //Try the comment
1569
                if (strpos($comment, $search) === false) {
1570
                    //Try the variable name
1571
                    if (strpos($setting['variable'], $search) === false) {
1572
                        continue;
1573
                    } else {
1574
                        $found = true;
1575
                    }
1576
                } else {
1577
                    $found = true;
1578
                }
1579
1580
            } else {
1581
                $found = true;
1582
            }
1583
            if ($found) {
1584
                $settings[] = $setting;
1585
            }
1586
        }
1587
    }
1588
    return $settings;
1589
}
1590