forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.php
169 lines (134 loc) · 6.23 KB
/
cron.php
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?PHP // $Id$
/// This script looks through all the module directories for cron.php files
/// and runs them. These files can contain cleanup functions, email functions
/// or anything that needs to be run on a regular basis.
///
/// This file is best run from cron on the host system (ie outside PHP).
/// The script can either be invoked via the web server or via a standalone
/// version of PHP compiled for CGI.
///
/// eg wget -q -O /dev/null 'http://moodle.somewhere.edu/admin/cron.php'
/// or php /web/moodle/admin/cron.php
$FULLME = "cron";
$starttime = microtime();
/// The current directory in PHP version 4.3.0 and above isn't necessarily the
/// directory of the script when run from the command line. The require_once()
/// would fail, so we'll have to chdir()
if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) {
chdir(dirname($_SERVER['argv'][0]));
}
require_once("../config.php");
if (!$alreadyadmin = isadmin()) {
unset($_SESSION['USER']);
unset($USER);
unset($_SESSION['SESSION']);
unset($SESSION);
$USER = get_admin(); /// Temporarily, to provide environment for this script
}
echo "<pre>\n";
$timenow = time();
echo "Server Time: ".date('r',$timenow)."\n\n";
/// Run all cron jobs for each module
if ($mods = get_records_select("modules", "cron > 0 AND (($timenow - lastcron) > cron)")) {
foreach ($mods as $mod) {
$libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
if (file_exists($libfile)) {
include_once($libfile);
$cron_function = $mod->name."_cron";
if (function_exists($cron_function)) {
if ($cron_function()) {
if (! set_field("modules", "lastcron", $timenow, "id", $mod->id)) {
echo "Error: could not update timestamp for $mod->fullname\n";
}
}
}
}
}
}
/// Run all core cron jobs, but not every time since they aren't too important.
/// These don't have a timer to reduce load, so we'll use a random number
/// to randomly choose the percentage of times we should run these jobs.
srand ((double) microtime() * 10000000);
$random100 = rand(0,100);
if ($random100 < 20) { // Approximately 20% of the time.
echo "Running clean-up tasks...\n";
/// Unenrol users who haven't logged in for $CFG->longtimenosee
if ($CFG->longtimenosee) { // value in days
$longtime = $timenow - ($CFG->longtimenosee * 3600 * 24);
if ($students = get_users_longtimenosee($longtime)) {
foreach ($students as $student) {
if (unenrol_student($student->userid, $student->course)) {
echo "Deleted student enrolment for user $student->userid from course $student->course\n";
}
}
}
}
/// Delete users who haven't confirmed within required period
$oneweek = $timenow - ($CFG->deleteunconfirmed * 3600);
if ($users = get_users_unconfirmed($oneweek)) {
foreach ($users as $user) {
if (delete_records("user", "id", $user->id)) {
echo "Deleted unconfirmed user for ".fullname($user, true)." ($user->id)\n";
}
}
}
/// Delete duplicate enrolments (don't know what causes these yet - expired sessions?)
if ($users = get_records_select("user_students", "userid > 0 GROUP BY course, userid ".
"HAVING count(*) > 1", "", "*,count(*)")) {
foreach ($users as $user) {
delete_records_select("user_students", "userid = '$user->userid' ".
"AND course = '$user->course' AND id <> '$user->id'");
}
}
/// Delete old logs to save space (this might need a timer to slow it down...)
if (!empty($CFG->loglifetime)) { // value in days
$loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
delete_records_select("log", "time < '$loglifetime'");
}
/// Delete old cached texts
if (!empty($CFG->cachetext)) { // Defined in config.php
$cachelifetime = time() - $CFG->cachetext;
delete_records_select("cache_text", "timemodified < '$cachelifetime'");
}
}
if (file_exists("$CFG->dataroot/cronextra.php")) {
include("$CFG->dataroot/cronextra.php");
}
if (!isset($CFG->disablescheduledbackups)) { // Defined in config.php
//Execute backup's cron
//Perhaps a long time and memory could help in large sites
@set_time_limit(0);
ini_set("memory_limit","56M");
if (file_exists("$CFG->dirroot/backup/backup_scheduled.php") and
file_exists("$CFG->dirroot/backup/backuplib.php") and
file_exists("$CFG->dirroot/backup/lib.php") and
file_exists("$CFG->libdir/blocklib.php")) {
include_once("$CFG->dirroot/backup/backup_scheduled.php");
include_once("$CFG->dirroot/backup/backuplib.php");
include_once("$CFG->dirroot/backup/lib.php");
require_once ("$CFG->libdir/blocklib.php");
echo "Running backups if required...\n";
flush();
if (! schedule_backup_cron()) {
echo "Something went wrong while performing backup tasks!!!\n";
} else {
echo "Backup tasks finished\n";
}
}
}
if (!empty($CFG->enablerssfeeds)) { //Defined in admin/variables page
if (file_exists("$CFG->dirroot/rss/rsslib.php")) {
include_once("$CFG->dirroot/rss/rsslib.php");
echo "Running rssfeeds if required...\n";
flush();
if ( ! cron_rss_feeds()) {
echo "Something went wrong while generating rssfeeds!!!\n";
} else {
echo "Rssfeeds finished\n";
}
}
}
echo "Cron script completed correctly\n";
$difftime = microtime_diff($starttime, microtime());
echo "Execution took ".$difftime." seconds\n";
?>