File: backup.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (132 lines) | stat: -rw-r--r-- 4,773 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?PHP  // $Id: backup.php,v 1.13 2005/08/03 01:24:28 patrickslee Exp $
       // backup.php - allows admin to edit all configuration variables for scheduled backups

    require_once("../config.php");
    require_once("../backup/lib.php");
    require_once("../backup/backup_scheduled.php");

    require_login();

    if (!isadmin()) {
        error("Only an admin can use this page");
    }

    if (!$site = get_site()) {
        error("Site isn't defined!");
    }

    //Initialise error variables
    $error = false;
    $sche_destination_error = "";

    /// If data submitted, then process and store.

    if (($config = data_submitted()) && confirm_sesskey()) {

        //First of all we check that everything is correct
        //Check for trailing slash and backslash in backup_sche_destination
        if (!empty($backup_sche_destination) and 
            (substr($backup_sche_destination,-1) == "/" or substr($backup_sche_destination,-1) == "\\")) {
            $error = true;
            $sche_destination_error = get_string("pathslasherror");
        //Now check that backup_sche_destination dir exists
        } else if (!empty($backup_sche_destination) and
            !is_dir($backup_sche_destination)) {
            $error = true;
            $sche_destination_error = get_string("pathnotexists");
        }

        //We need to do some weekdays conversions prior to continue
        $i = 0;
        $temp = "";
        $a_config = (array)$config;
        while ($i<7) {
            $tocheck = "dayofweek_".$i;
            if (isset($a_config[$tocheck])) {
                $temp .= "1";
            } else {
                $temp .= "0"; 
            }
            unset($a_config[$tocheck]);
            $i++;
        }
        $a_config['backup_sche_weekdays'] = $temp;
        $config = (object)$a_config;
        //weekdays conversions done. Continue

        foreach ($config as $name => $value) {
            backup_set_config($name, $value);
        }

        //And now, we execute schedule_backup_next_execution() for each course in the server to have the next
        //execution time updated automatically everytime it's changed.
        $status = true;
        //get admin
        $admin = get_admin();
        if (!$admin) {
            $status = false;
        }
        //get backup config
        if (! $backup_config =  backup_get_config()) {
            $status = false;
        }
        if ($status) {
            //get courses
            if ($courses = get_records('course', '', '', '', 'id,shortname')) {
                //For each course, we check (insert, update) the backup_course table
                //with needed data
                foreach ($courses as $course) {
                    //We check if the course exists in backup_course
                    $backup_course = get_record("backup_courses","courseid",$course->id);
                    //If it doesn't exist, create 
                    if (!$backup_course) {
                        $temp_backup_course->courseid = $course->id;
                        $newid = insert_record("backup_courses",$temp_backup_course);
                        //And get it from db
                        $backup_course = get_record("backup_courses","id",$newid);
                    }
                    //Now, calculate next execution of the course
                    $nextstarttime = schedule_backup_next_execution ($backup_course,$backup_config,time(),$admin->timezone);
                    //Save it to db
                    set_field("backup_courses","nextstarttime",$nextstarttime,"courseid",$backup_course->courseid);
                }
            }
        }

        if (!$error) {
            redirect("$CFG->wwwroot/$CFG->admin/index.php", get_string("changessaved"), 1);
            exit;
        }
    }

/// Otherwise print the form.

    $stradmin = get_string("administration");
    $strconfiguration = get_string("configuration");
    $strbackup = get_string("backup");

    print_header("$site->shortname: $strconfiguration: $strbackup", $site->fullname,
                  "<a href=\"index.php\">$stradmin</a> -> ".
                  "<a href=\"configure.php\">$strconfiguration</a> -> ".
                  $strbackup);

    echo "<p align=\"right\"><a href=\"../backup/log.php\">".get_string("logs")."</a></p>";

    print_heading($strbackup);

    print_simple_box("<center>".get_string("adminhelpbackup")."</center>", "center", "50%");
    echo "<br />";

    print_simple_box_start("center");

    //Check for required functions...
    if (!function_exists('utf8_encode')) {
        notify("You need to add XML support to your PHP installation");
    } 
    include ("$CFG->dirroot/backup/config.html");

    print_simple_box_end();

    print_footer();

?>