improved docs and license headers, adding direct access protection
[moodle.git] / lib / db / install.php
CommitLineData
117bd748 1<?php
1caea91e 2
5b4a78e2 3// This file is part of Moodle - http://moodle.org/
1caea91e 4//
5b4a78e2
PS
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file is executed right after the install.xml
20 *
21 * @package core
22 * @subpackage admin
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
1caea91e 28
c20ce874 29function xmldb_main_install() {
1caea91e 30 global $CFG, $DB, $SITE;
31
4f0c2d00 32 /// make sure system context exists
b4154c2d 33 $syscontext = get_system_context(false);
1caea91e 34 if ($syscontext->id != 1) {
19a4a32e 35 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected system context id created!');
1caea91e 36 }
37
38
4f0c2d00 39 /// create site course
1caea91e 40 $newsite = new object();
41 $newsite->fullname = "";
42 $newsite->shortname = "";
43 $newsite->summary = NULL;
44 $newsite->newsitems = 3;
45 $newsite->numsections = 0;
46 $newsite->category = 0;
47 $newsite->format = 'site'; // Only for this course
48 $newsite->teacher = get_string("defaultcourseteacher");
49 $newsite->teachers = get_string("defaultcourseteachers");
50 $newsite->student = get_string("defaultcoursestudent");
51 $newsite->students = get_string("defaultcoursestudents");
52 $newsite->timemodified = time();
53
54 $DB->insert_record('course', $newsite);
55 $SITE = get_site();
56 if ($SITE->id != 1) {
19a4a32e 57 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected site course id created!');
1caea91e 58 }
59
60
4f0c2d00 61 /// make sure site course context exists
1caea91e 62 get_context_instance(CONTEXT_COURSE, $SITE->id);
63
4f0c2d00 64 /// create default course category
1caea91e 65 $cat = get_course_category();
66
5b8fa09b 67 $defaults = array(
68 'rolesactive' => '0', // marks fully set up system
69 'auth' => 'email',
70 'auth_pop3mailbox' => 'INBOX',
df997f84 71 'enrol_plugins_enabled' => 'manual,guest,self,cohort',
5f0baa43 72 'theme' => theme_config::DEFAULT_THEME,
5b8fa09b 73 'filter_multilang_converted' => 1,
aa282b10 74 'siteidentifier' => random_string(32).get_host_from_url($CFG->wwwroot),
5b8fa09b 75 'backup_version' => 2008111700,
76 'backup_release' => '2.0 dev',
77 'blocks_version' => 2007081300, // might be removed soon
78 'mnet_dispatcher_mode' => 'off',
79 'sessiontimeout' => 7200, // must be present during roles installation
80 'stringfilters' => '', // These two are managed in a strange way by the filters
81 'filterall' => 0, // setting page, so have to be initialised here.
a5747cf8 82 'texteditors' => 'tinymce,textarea',
5b8fa09b 83 );
1caea91e 84 foreach($defaults as $key => $value) {
85 set_config($key, $value);
86 }
87
88
4f0c2d00 89 /// bootstrap mnet
1caea91e 90 $mnethost = new object();
91 $mnethost->wwwroot = $CFG->wwwroot;
92 $mnethost->name = '';
93 $mnethost->name = '';
94 $mnethost->public_key = '';
95
96 if (empty($_SERVER['SERVER_ADDR'])) {
97 // SERVER_ADDR is only returned by Apache-like webservers
98 preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $CFG->wwwroot, $matches);
99 $my_hostname = $matches[1];
100 $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
101 if ($my_ip == $my_hostname) {
102 $mnethost->ip_address = 'UNKNOWN';
103 } else {
104 $mnethost->ip_address = $my_ip;
105 }
106 } else {
107 $mnethost->ip_address = $_SERVER['SERVER_ADDR'];
108 }
109
110 $mnetid = $DB->insert_record('mnet_host', $mnethost);
111 set_config('mnet_localhost_id', $mnetid);
112
c20ce874 113 // Initial insert of mnet applications info
114 $mnet_app = new object();
115 $mnet_app->name = 'moodle';
116 $mnet_app->display_name = 'Moodle';
117 $mnet_app->xmlrpc_server_url = '/mnet/xmlrpc/server.php';
118 $mnet_app->sso_land_url = '/auth/mnet/land.php';
119 $mnet_app->sso_jump_url = '/auth/mnet/land.php';
6f4f82f2 120 $moodleapplicationid = $DB->insert_record('mnet_application', $mnet_app);
c20ce874 121
122 $mnet_app = new object();
123 $mnet_app->name = 'mahara';
124 $mnet_app->display_name = 'Mahara';
125 $mnet_app->xmlrpc_server_url = '/api/xmlrpc/server.php';
126 $mnet_app->sso_land_url = '/auth/xmlrpc/land.php';
127 $mnet_app->sso_jump_url = '/auth/xmlrpc/jump.php';
128 $DB->insert_record('mnet_application', $mnet_app);
129
6f4f82f2
PS
130 // Set up the probably-to-be-removed-soon 'All hosts' record
131 $mnetallhosts = new object();
132 $mnetallhosts->wwwroot = '';
133 $mnetallhosts->ip_address = '';
134 $mnetallhosts->public_key = '';
135 $mnetallhosts->public_key_expires = 0;
136 $mnetallhosts->last_connect_time = 0;
137 $mnetallhosts->last_log_id = 0;
138 $mnetallhosts->deleted = 0;
139 $mnetallhosts->name = 'All Hosts';
140 $mnetallhosts->applicationid = $moodleapplicationid;
141 $mnetallhosts->id = $DB->insert_record('mnet_host', $mnetallhosts, true);
142 set_config('mnet_all_hosts_id', $mnetallhosts->id);
88582df4 143
4f0c2d00 144 /// insert log entries - replaces statements section in install.xml
0d6b8715
SH
145 update_log_display_entry('user', 'view', 'user', $DB->sql_concat('firstname', "' '" , 'lastname'));
146 update_log_display_entry('course', 'user report', 'user', $DB->sql_concat('firstname', "' '" , 'lastname'));
0cb93a7e 147 update_log_display_entry('course', 'view', 'course', 'fullname');
148 update_log_display_entry('course', 'update', 'course', 'fullname');
df997f84
PS
149 update_log_display_entry('course', 'enrol', 'user', 'course', 'fullname'); // there should be some way to store user id of the enrolled user!
150 update_log_display_entry('course', 'unenrol', 'user', 'course', 'fullname'); // there should be some way to store user id of the enrolled user!
0cb93a7e 151 update_log_display_entry('course', 'report log', 'course', 'fullname');
152 update_log_display_entry('course', 'report live', 'course', 'fullname');
153 update_log_display_entry('course', 'report outline', 'course', 'fullname');
154 update_log_display_entry('course', 'report participation', 'course', 'fullname');
155 update_log_display_entry('course', 'report stats', 'course', 'fullname');
0d6b8715
SH
156 update_log_display_entry('message', 'write', 'user', $DB->sql_concat('firstname', "' '" , 'lastname'));
157 update_log_display_entry('message', 'read', 'user', $DB->sql_concat('firstname', "' '" , 'lastname'));
158 update_log_display_entry('message', 'add contact', 'user', $DB->sql_concat('firstname', "' '" , 'lastname'));
159 update_log_display_entry('message', 'remove contact', 'user', $DB->sql_concat('firstname', "' '" , 'lastname'));
160 update_log_display_entry('message', 'block contact', 'user', $DB->sql_concat('firstname', "' '" , 'lastname'));
161 update_log_display_entry('message', 'unblock contact', 'user', $DB->sql_concat('firstname', "' '" , 'lastname'));
0cb93a7e 162 update_log_display_entry('group', 'view', 'groups', 'name');
b89e4ad8 163 update_log_display_entry('tag', 'update', 'tag', 'name');
c20ce874 164
1caea91e 165
4f0c2d00 166 /// Create guest record - do not assign any role, guest user get's the default guest role automatically on the fly
88582df4 167 $guest = new object();
168 $guest->auth = 'manual';
169 $guest->username = 'guest';
170 $guest->password = hash_internal_user_password('guest');
171 $guest->firstname = get_string('guestuser');
172 $guest->lastname = ' ';
173 $guest->email = 'root@localhost';
174 $guest->description = get_string('guestuserinfo');
175 $guest->mnethostid = $CFG->mnet_localhost_id;
176 $guest->confirmed = 1;
177 $guest->lang = $CFG->lang;
178 $guest->timemodified= time();
179 $guest->id = $DB->insert_record('user', $guest);
180
181
4f0c2d00 182 /// Now create admin user
88582df4 183 $admin = new object();
184 $admin->auth = 'manual';
185 $admin->firstname = get_string('admin');
186 $admin->lastname = get_string('user');
187 $admin->username = 'admin';
188 $admin->password = 'adminsetuppending';
1deff123 189 $admin->email = '';
88582df4 190 $admin->confirmed = 1;
191 $admin->mnethostid = $CFG->mnet_localhost_id;
192 $admin->lang = $CFG->lang;
193 $admin->maildisplay = 1;
194 $admin->timemodified = time();
aa282b10 195 $admin->lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr(); // installation hijacking prevention
88582df4 196 $admin->id = $DB->insert_record('user', $admin);
4f0c2d00
PS
197 /// Store list of admins
198 set_config('siteadmins', $admin->id);
88582df4 199
1caea91e 200
4f0c2d00
PS
201 /// Install the roles system.
202 $managerrole = create_role(get_string('manager', 'role'), 'manager', get_string('managerdescription', 'role'), 'manager');
203 $coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator', get_string('coursecreatorsdescription'), 'coursecreator');
204 $editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher', get_string('defaultcourseteacherdescription'), 'editingteacher');
205 $noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher', get_string('noneditingteacherdescription'), 'teacher');
206 $studentrole = create_role(get_string('defaultcoursestudent'), 'student', get_string('defaultcoursestudentdescription'), 'student');
207 $guestrole = create_role(get_string('guest'), 'guest', get_string('guestdescription'), 'guest');
208 $userrole = create_role(get_string('authenticateduser'), 'user', get_string('authenticateduserdescription'), 'user');
209 $frontpagerole = create_role(get_string('frontpageuser', 'role'), 'frontpage', get_string('frontpageuserdescription', 'role'), 'frontpage');
88582df4 210
211 /// Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles
88582df4 212 update_capabilities('moodle');
c976e271 213 external_update_descriptions('moodle');
88582df4 214
4f0c2d00
PS
215 /// Default allow assign
216 $defaultallowassigns = array(
217 array($managerrole, $managerrole),
218 array($managerrole, $coursecreatorrole),
219 array($managerrole, $editteacherrole),
220 array($managerrole, $noneditteacherrole),
221 array($managerrole, $studentrole),
88582df4 222
4f0c2d00
PS
223 array($editteacherrole, $noneditteacherrole),
224 array($editteacherrole, $studentrole),
c468795c 225 );
4f0c2d00
PS
226 foreach ($defaultallowassigns as $allow) {
227 list($fromroleid, $toroleid) = $allow;
c468795c 228 allow_assign($fromroleid, $toroleid);
4f0c2d00
PS
229 }
230
231 /// Default allow override
232 $defaultallowoverrides = array(
233 array($managerrole, $managerrole),
234 array($managerrole, $coursecreatorrole),
235 array($managerrole, $editteacherrole),
236 array($managerrole, $noneditteacherrole),
237 array($managerrole, $studentrole),
238 array($managerrole, $guestrole),
239 array($managerrole, $userrole),
240 array($managerrole, $frontpagerole),
241
242 array($editteacherrole, $noneditteacherrole),
243 array($editteacherrole, $studentrole),
244 array($editteacherrole, $guestrole),
245 );
246 foreach ($defaultallowoverrides as $allow) {
247 list($fromroleid, $toroleid) = $allow;
c468795c 248 allow_override($fromroleid, $toroleid); // There is a rant about this in MDL-15841.
4f0c2d00
PS
249 }
250
251 /// Default allow switch.
252 $defaultallowswitch = array(
253 array($managerrole, $editteacherrole),
254 array($managerrole, $noneditteacherrole),
255 array($managerrole, $studentrole),
256 array($managerrole, $guestrole),
257
258 array($editteacherrole, $noneditteacherrole),
259 array($editteacherrole, $studentrole),
260 array($editteacherrole, $guestrole),
261
262 array($noneditteacherrole, $studentrole),
263 array($noneditteacherrole, $guestrole),
264 );
265 foreach ($defaultallowswitch as $allow) {
266 list($fromroleid, $toroleid) = $allow;
c468795c 267 allow_switch($fromroleid, $toroleid);
268 }
88582df4 269
270 /// Set up the context levels where you can assign each role.
4f0c2d00 271 set_role_contextlevels($managerrole, get_default_contextlevels('manager'));
88582df4 272 set_role_contextlevels($coursecreatorrole, get_default_contextlevels('coursecreator'));
273 set_role_contextlevels($editteacherrole, get_default_contextlevels('editingteacher'));
274 set_role_contextlevels($noneditteacherrole, get_default_contextlevels('teacher'));
275 set_role_contextlevels($studentrole, get_default_contextlevels('student'));
276 set_role_contextlevels($guestrole, get_default_contextlevels('guest'));
277 set_role_contextlevels($userrole, get_default_contextlevels('user'));
ac173d3e 278
4f0c2d00 279 // Init themes
78946b9b 280 set_config('themerev', 1);
b64b465c
PS
281
282 // Install licenses
283 require_once($CFG->libdir . '/licenselib.php');
284 license_manager::install_licenses();
03d9401e
MD
285
286 /// Add two lines of data into this new table
287 $mypage = new object();
288 $mypage->userid = NULL;
289 $mypage->name = '__default';
290 $mypage->private = 0;
291 $mypage->sortorder = 0;
292 if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>0))) {
293 $DB->insert_record('my_pages', $mypage);
294 }
295 $mypage->private = 1;
296 if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>1))) {
297 $DB->insert_record('my_pages', $mypage);
298 }
117bd748 299}