Passed
Push — master ( cc68cc...50453a )
by Julito
09:11
created

public/main/admin/archive_cleanup.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
$cidReset = true;
6
7
require_once __DIR__.'/../inc/global.inc.php';
8
9
ini_set('memory_limit', -1);
10
ini_set('max_execution_time', 0);
11
12
// setting the section (for the tabs)
13
$this_section = SECTION_PLATFORM_ADMIN;
14
15
// Access restrictions
16
api_protect_admin_script(true);
17
18
// setting breadcrumbs
19
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
20
21
$form = new FormValidator(
22
    'archive_cleanup_form',
23
    'post',
24
    '',
25
    '',
26
    [],
27
    FormValidator::LAYOUT_BOX
28
);
29
$form->addButtonSend(get_lang('Proceed with cleanup'));
30
31
if ($form->validate()) {
32
    if (function_exists('opcache_reset')) {
33
        opcache_reset();
34
    }
35
36
    $file = api_get_path(SYS_PUBLIC_PATH).'build/main.js';
37
    if (file_exists($file)) {
38
        unlink($file);
39
    }
40
    $dir = api_get_path(SYS_PUBLIC_PATH).'build';
41
    $files = scandir($dir);
42
    foreach ($files as $file) {
43
        if (preg_match('/main\..*\.js/', $file)) {
44
            unlink($dir.'/'.$file);
45
        }
46
    }
47
48
    $archive_path = api_get_path(SYS_ARCHIVE_PATH);
49
    $htaccess = @file_get_contents($archive_path.'.htaccess');
50
    $result = rmdirr($archive_path, true, true);
51
    if (false === $result) {
52
        Display::addFlash(Display::return_message(get_lang('Cleanup of cache and temporary filesFailed'), 'error'));
53
    } else {
54
        Display::addFlash(Display::return_message(get_lang('The app/cache/ directory cleanup has been executed successfully.')));
55
    }
56
57
    try {
58
        \Chamilo\CoreBundle\Composer\ScriptHandler::dumpCssFiles();
59
        Display::addFlash(Display::return_message(get_lang('The styles and assets in the web/ folder have been refreshed.')));
60
    } catch (Exception $e) {
61
        Display::addFlash(Display::return_message(get_lang('The styles and assets in the web/ folder could not be refreshed, probably due to a permissions problem. Make sure the web/ folder is writeable by your web server.'), 'error'));
62
        error_log($e->getMessage());
63
    }
64
65
    if (!empty($htaccess)) {
66
        @file_put_contents($archive_path.'/.htaccess', $htaccess);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for file_put_contents(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

66
        /** @scrutinizer ignore-unhandled */ @file_put_contents($archive_path.'/.htaccess', $htaccess);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
67
    }
68
69
    header('Location: '.api_get_self());
70
    exit;
71
}
72
73
Display::display_header(get_lang('Cleanup of cache and temporary files'));
74
echo Display::return_message(get_lang('Cleanup of cache and temporary filesDescr'), 'warning');
75
$form->display();
76
Display::display_footer();
77