delete.php 2.46 KB
Newer Older
moodler's avatar
 
moodler committed
1
2
3
4
5
<?PHP //$Id$

// Deletes the moodledata directory, COMPLETELY!!
// BE VERY CAREFUL USING THIS!

6
    require_once('../config.php');
7
    require_once($CFG->libdir.'/adminlib.php');
8
9

    admin_externalpage_setup('purgemoodledata');
moodler's avatar
 
moodler committed
10
11
12

    require_login();

skodak's avatar
skodak committed
13
14
    $sure       = optional_param('sure', 0, PARAM_BOOL);
    $reallysure = optional_param('reallysure', 0, PARAM_BOOL);
moodler's avatar
moodler committed
15

16
    require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
moodler's avatar
 
moodler committed
17
18
19

    $deletedir = $CFG->dataroot;   // The directory to delete!

20
    admin_externalpage_print_header();
21
    echo $OUTPUT->heading('Purge moodledata');
22

moodler's avatar
moodler committed
23
    if (empty($sure)) {
24
25
26
        $optionsyes = array('sure'=>'yes', 'sesskey'=>sesskey());
        notice_yesno ('Are you completely sure you want to delete everything inside the directory '. $deletedir .' ?',
            'delete.php', 'index.php', $optionsyes, NULL, 'post', 'get');
27
        echo $OUTPUT->footer();
moodler's avatar
 
moodler committed
28
29
30
        exit;
    }

31
32
33
34
    if (!data_submitted() or empty($reallysure)) {
        $optionsyes = array('sure'=>'yes', 'sesskey'=>sesskey(), 'reallysure'=>'yes');
        notice_yesno ('Are you REALLY REALLY completely sure you want to delete everything inside the directory '. $deletedir .' (this includes all user images, and any other course files that have been created) ?',
            'delete.php', 'index.php', $optionsyes, NULL, 'post', 'get');
35
        echo $OUTPUT->footer();
moodler's avatar
 
moodler committed
36
37
38
        exit;
    }

moodler's avatar
moodler committed
39
    if (!confirm_sesskey()) {
40
        print_error('wrongcall', 'error');
moodler's avatar
moodler committed
41
42
    }

moodler's avatar
 
moodler committed
43
44
45
46
    /// OK, here goes ...

    delete_subdirectories($deletedir);

47
    echo '<h1 align="center">Done!</h1>';
moodler's avatar
 
moodler committed
48
    print_continue($CFG->wwwroot);
49
    echo $OUTPUT->footer();
moodler's avatar
 
moodler committed
50
51
52
53
54
55
56
    exit;


function delete_subdirectories($rootdir) {

    $dir = opendir($rootdir);

57
    while (false !== ($file = readdir($dir))) {
58
59
60
        if ($file != '.' and $file != '..') {
            $fullfile = $rootdir .'/'. $file;
            if (filetype($fullfile) == 'dir') {
moodler's avatar
 
moodler committed
61
                delete_subdirectories($fullfile);
62
                echo 'Deleting '. $fullfile .' ... ';
moodler's avatar
 
moodler committed
63
                if (rmdir($fullfile)) {
64
                    echo 'Done.<br />';
moodler's avatar
 
moodler committed
65
                } else {
66
                    echo 'FAILED.<br />';
moodler's avatar
 
moodler committed
67
68
                }
            } else {
69
70
71
                echo 'Deleting '. $fullfile .' ... ';
                if (unlink($fullfile)) {
                    echo 'Done.<br />';
moodler's avatar
 
moodler committed
72
                } else {
73
                    echo 'FAILED.<br />';
moodler's avatar
 
moodler committed
74
75
76
77
78
79
                }
            }
        }
    }
    closedir($dir);
}
80

moodler's avatar
moodler committed
81
?>