MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / lib / componentlib.class.php
blobf1a01dc88af8468a63285c52d3104ca32e4f3456
1 <?php //$Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12 // //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 // This library includes all the necessary stuff to use the one-click
28 // download and install feature of Moodle, used to keep updated some
29 // items like languages, pear, enviroment... i.e, components.
30 //
31 // It has been developed harcoding some important limits that are
32 // explained below:
33 // - It only can check, download and install items under moodledata.
34 // - Every downloadeable item must be one zip file.
35 // - The zip file root content must be 1 directory, i.e, everything
36 // is stored under 1 directory.
37 // - Zip file name and root directory must have the same name (but
38 // the .zip extension, of course).
39 // - Every .zip file must be defined in one .md5 file that will be
40 // stored in the same remote directory than the .zip file.
41 // - The name of such .md5 file is free, although it's recommended
42 // to use the same name than the .zip (that's the default
43 // assumption if no specified).
44 // - Every remote .md5 file will be a comma separated (CVS) file where each
45 // line will follow this format:
46 // - Field 1: name of the zip file (without extension). Mandatory.
47 // - Field 2: md5 of the zip file. Mandatory.
48 // - Field 3: whatever you want (or need). Optional.
49 // -Every local .md5 file will:
50 // - Have the zip file name (without the extension) plus -md5
51 // - Will reside inside the expanded zip file dir
52 // - Will contain the md5 od the latest installed component
53 // With all these details present, the process will perform this tasks:
54 // - Perform security checks. Only admins are allowed to use this for now.
55 // - Perform server checks. fopen must allow to open remote URLs.
56 // - Read the .md5 file from source (1).
57 // - Extract the correct line for the .zip being requested.
58 // - Compare it with the local .md5 file (2).
59 // - If different:
60 // - Download the newer .zip file from source.
61 // - Calculate its md5 (3).
62 // - Compare (1) and (3).
63 // - If equal:
64 // - Delete old directory.
65 // - Uunzip the newer .zip file.
66 // - Create the new local .md5 file.
67 // - Delete the .zip file.
68 // - If different:
69 // - ERROR. Old package won't be modified. We shouldn't
70 // reach here ever.
71 // - If fopen is not available, a message text about how to do
72 // the process manually (remotedownloadnotallowed) must be
73 // built to explain it.
75 // General Usage:
77 // To install one component:
79 // require_once($CFG->libdir.'/componentlib.class.php');
80 // if ($cd = new component_installer('http://download.moodle.org', 'lang16',
81 // 'es_utf8.zip', 'languages.md5', 'lang')) {
82 // $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
83 // switch ($status) {
84 // case ERROR:
85 // if ($cd->get_error() == 'remotedownloadnotallowed') {
86 // $a = new stdClass();
87 // $a->url = 'http://download.moodle.org/lang16/es_utf8.zip';
88 // $a->dest= $CFG->dataroot.'/lang';
89 // error(get_string($cd->get_error(), 'error', $a));
90 // } else {
91 // error(get_string($cd->get_error(), 'error'));
92 // }
93 // break;
94 // case UPTODATE:
95 // //Print error string or whatever you want to do
96 // break;
97 // case INSTALLED:
98 // //Print/do whatever you want
99 // break;
100 // default:
101 // //We shouldn't reach this point
102 // }
103 // } else {
104 // //We shouldn't reach this point
105 // }
107 // To switch of component (maintaining the rest of settings):
109 // $status = $cd->change_zip_file('en_utf8.zip'); //returns boolean false on error
111 // To retrieve all the components in one remote md5 file
113 // $components = $cd->get_all_components_md5(); //returns boolean false on error, array instead
115 // To check if current component needs to be updated
117 // $status = $cd->need_upgrade(); //returns ERROR | UPTODATE | NEEDUPDATE
119 // To get the 3rd field of the md5 file (optional)
121 // $field = $cd->get_extra_md5_field(); //returns string (empty if not exists)
123 // For all the error situations the $cd->get_error() method should return always the key of the
124 // error to be retrieved by one standard get_string() call against the error.php lang file.
126 // That's all!
128 // Some needed constants
129 define('ERROR', 0);
130 define('UPTODATE', 1);
131 define('NEEDUPDATE', 2);
132 define('INSTALLED', 3);
135 * This class is used to check, download and install items from
136 * download.moodle.org to the moodledata directory. It always
137 * return true/false in all their public methods to say if
138 * execution has ended succesfuly or not. If there is any problem
139 * its getError() method can be called, returning one error string
140 * to be used with the standard get/print_string() functions.
142 class component_installer {
144 var $sourcebase; /// Full http URL, base for downloadeable items
145 var $zippath; /// Relative path (from sourcebase) where the
146 /// downloadeable item resides.
147 var $zipfilename; /// Name of the .zip file to be downloaded
148 var $md5filename; /// Name of the .md5 file to be read
149 var $componentname;/// Name of the component. Must be the zip name without
150 /// the extension. And it defines a lot of things:
151 /// the md5 line to search for, the default m5 file name
152 /// and the name of the root dir stored inside the zip file
153 var $destpath; /// Relative path (from moodledata) where the .zip
154 /// file will be expanded.
155 var $errorstring; /// Latest error produced. It will contain one lang string key.
156 var $extramd5info; /// Contents of the optional third field in the .md5 file.
157 var $requisitesok; /// Flag to see if requisites check has been passed ok.
159 var $cachedmd5components; /// Array of cached components to avoid to
160 /// download the same md5 file more than once per request.
163 * Standard constructor of the class. It will initialize all attributes.
164 * without performing any check at all.
166 * @param string Full http URL, base for downloadeable items
167 * @param string Relative path (from sourcebase) where the
168 * downloadeable item resides
169 * @param string Name of the .zip file to be downloaded
170 * @param string Name of the .md5 file to be read (default '' = same
171 * than zipfilename)
172 * @param string Relative path (from moodledata) where the .zip file will
173 * be expanded (default='' = moodledataitself)
174 * @return object
176 function component_installer ($sourcebase, $zippath, $zipfilename, $md5filename='', $destpath='') {
178 $this->sourcebase = $sourcebase;
179 $this->zippath = $zippath;
180 $this->zipfilename = $zipfilename;
181 $this->md5filename = $md5filename;
182 $this->componentname= '';
183 $this->destpath = $destpath;
184 $this->errorstring = '';
185 $this->extramd5info = '';
186 $this->requisitesok = false;
187 $this->cachedmd5components = array();
189 $this->check_requisites();
193 * This function will check if everything is properly set to begin
194 * one installation. It'll check for fopen wrappers enabled and
195 * admin privileges. Also, it will check for required settings
196 * and will fill everything as needed.
198 * @return boolean true/false (plus detailed error in errorstring)
200 function check_requisites() {
201 global $CFG;
203 $this->requisitesok = false;
205 /// Check for fopen remote enabled
206 if (!ini_get('allow_url_fopen')) {
207 $this->errorstring='remotedownloadnotallowed';
208 return false;
210 /// Check that everything we need is present
211 if (empty($this->sourcebase) || empty($this->zippath) || empty($this->zipfilename)) {
212 $this->errorstring='missingrequiredfield';
213 return false;
215 /// Check for correct sourcebase (this will be out in the future)
216 if ($this->sourcebase != 'http://download.moodle.org') {
217 $this->errorstring='wrongsourcebase';
218 return false;
220 /// Check the zip file is a correct one (by extension)
221 if (stripos($this->zipfilename, '.zip') === false) {
222 $this->errorstring='wrongzipfilename';
223 return false;
225 /// Check that exists under dataroot
226 if (!empty($this->destpath)) {
227 if (!file_exists($CFG->dataroot.'/'.$this->destpath)) {
228 $this->errorstring='wrongdestpath';
229 return false;
232 /// Calculate the componentnamea
233 $pos = stripos($this->zipfilename, '.zip');
234 $this->componentname = substr($this->zipfilename, 0, $pos);
235 /// Calculate md5filename if it's empty
236 if (empty($this->md5filename)) {
237 $this->md5filename = $this->componentname.'.md5';
239 /// Set the requisites passed flag
240 $this->requisitesok = true;
241 return true;
245 * This function will perform the full installation if needed, i.e.
246 * compare md5 values, download, unzip, install and regenerate
247 * local md5 file
249 * @return int ERROR | UPTODATE | INSTALLED
251 function install() {
253 global $CFG;
255 /// Check requisites are passed
256 if (!$this->requisitesok) {
257 return ERROR;
259 /// Confirm we need upgrade
260 if ($this->need_upgrade() === ERROR) {
261 return ERROR;
262 } else if ($this->need_upgrade() === UPTODATE) {
263 $this->errorstring='componentisuptodate';
264 return UPTODATE;
266 /// Create temp directory if necesary
267 if (!make_upload_directory('temp', false)) {
268 $this->errorstring='cannotcreatetempdir';
269 return ERROR;
271 /// Download zip file and save it to temp
272 $source = $this->sourcebase.'/'.$this->zippath.'/'.$this->zipfilename;
273 $zipfile= $CFG->dataroot.'/temp/'.$this->zipfilename;
274 if ($contents = file_get_contents($source)) {
275 if ($file = fopen($zipfile, 'w')) {
276 if (!fwrite($file, $contents)) {
277 fclose($file);
278 $this->errorstring='cannotsavezipfile';
279 return ERROR;
281 } else {
282 $this->errorstring='cannotsavezipfile';
283 return ERROR;
285 fclose($file);
286 } else {
287 $this->errorstring='cannotdownloadzipfile';
288 return ERROR;
290 /// Calculate its md5
291 $new_md5 = md5($contents);
292 /// Compare it with the remote md5 to check if we have the correct zip file
293 if (!$remote_md5 = $this->get_component_md5()) {
294 return ERROR;
296 if ($new_md5 != $remote_md5) {
297 $this->errorstring='downloadedfilecheckfailed';
298 return ERROR;
300 /// Move current revision to a safe place
301 $destinationdir = $CFG->dataroot.'/'.$this->destpath;
302 $destinationcomponent = $destinationdir.'/'.$this->componentname;
303 @remove_dir($destinationcomponent.'_old'); //Deleting possible old components before
304 @rename ($destinationcomponent, $destinationcomponent.'_old'); //Moving to a safe place
305 /// Unzip new version
306 if (!unzip_file($zipfile, $destinationdir, false)) {
307 /// Error so, go back to the older
308 @remove_dir($destinationcomponent);
309 @rename ($destinationcomponent.'_old', $destinationcomponent);
310 $this->errorstring='cannotunzipfile';
311 return ERROR;
313 /// Delete old component version
314 @remove_dir($destinationcomponent.'_old');
315 /// Create local md5
316 if ($file = fopen($destinationcomponent.'/'.$this->componentname.'.md5', 'w')) {
317 if (!fwrite($file, $new_md5)) {
318 fclose($file);
319 $this->errorstring='cannotsavemd5file';
320 return ERROR;
322 } else {
323 $this->errorstring='cannotsavemd5file';
324 return ERROR;
326 fclose($file);
327 /// Delete temp zip file
328 @unlink($zipfile);
330 return INSTALLED;
334 * This function will detect if remote component needs to be installed
335 * because it's different from the local one
337 * @return int ERROR | UPTODATE | NEEDUPDATE
339 function need_upgrade() {
341 /// Check requisites are passed
342 if (!$this->requisitesok) {
343 return ERROR;
345 /// Get local md5
346 $local_md5 = $this->get_local_md5();
347 /// Get remote md5
348 if (!$remote_md5 = $this->get_component_md5()) {
349 return ERROR;
351 /// Return result
352 if ($local_md5 == $remote_md5) {
353 return UPTODATE;
354 } else {
355 return NEEDUPDATE;
359 /**
360 * This function will change the zip file to install on the fly
361 * to allow the class to process different components of the
362 * same md5 file without intantiating more objects.
364 * @param string New zip filename to process
365 * @return boolean true/false
367 function change_zip_file($newzipfilename) {
369 $this->zipfilename = $newzipfilename;
370 return $this->check_requisites();
374 * This function will get the local md5 value of the installed
375 * component.
377 * @return string md5 of the local component (false on error)
379 function get_local_md5() {
380 global $CFG;
382 /// Check requisites are passed
383 if (!$this->requisitesok) {
384 return false;
387 $return_value = 'needtobeinstalled'; /// Fake value to force new installation
389 /// Calculate source to read
390 $source = $CFG->dataroot.'/'.$this->destpath.'/'.$this->componentname.'/'.$this->componentname.'.md5';
391 /// Read md5 value stored (if exists)
392 if (file_exists($source)) {
393 if ($temp = file_get_contents($source)) {
394 $return_value = $temp;
397 return $return_value;
401 * This function will download the specified md5 file, looking for the
402 * current componentname, returning its md5 field and storing extramd5info
403 * if present. Also it caches results to cachedmd5components for better
404 * performance in the same request.
406 * @return mixed md5 present in server (or false if error)
408 function get_component_md5() {
410 /// Check requisites are passed
411 if (!$this->requisitesok) {
412 return false;
414 /// Get all components of md5 file
415 if (!$comp_arr = $this->get_all_components_md5()) {
416 $this->errorstring='cannotdownloadcomponents';
417 return false;
419 /// Search for the componentname component
420 if (empty($comp_arr[$this->componentname]) || !$component = $comp_arr[$this->componentname]) {
421 $this->errorstring='cannotfindcomponent';
422 return false;
424 /// Check we have a valid md5
425 if (empty($component[1]) || strlen($component[1]) != 32) {
426 $this->errorstring='invalidmd5';
427 return false;
429 /// Set the extramd5info field
430 if (!empty($component[2])) {
431 $this->extramd5info = $component[2];
433 return $component[1];
436 /**
437 * This function allows you to retrieve the complete array of components found in
438 * the md5filename
440 * @return array array of components in md5 file or false if error
442 function get_all_components_md5() {
444 /// Check requisites are passed
445 if (!$this->requisitesok) {
446 return false;
449 /// Initialize components array
450 $comp_arr = array();
452 /// Define and retrieve the full md5 file
453 $source = $this->sourcebase.'/'.$this->zippath.'/'.$this->md5filename;
455 /// Check if we have downloaded the md5 file before (per request cache)
456 if (!empty($this->cachedmd5components[$source])) {
457 $comp_arr = $this->cachedmd5components[$source];
458 } else {
459 /// Not downloaded, let's do it now
460 $availablecomponents = array();
461 if ($fp = fopen($source, 'r')) {
462 /// Read from URL, each line will be one component
463 while(!feof ($fp)) {
464 $availablecomponents[] = split(',', fgets($fp,1024));
466 fclose($fp);
467 /// If no components have been found, return error
468 if (empty($availablecomponents)) {
469 $this->errorstring='cannotdownloadcomponents';
470 return false;
472 /// Build an associative array of components for easily search
473 /// applying trim to avoid linefeeds and other...
474 $comp_arr = array();
475 foreach ($availablecomponents as $component) {
476 /// Avoid sometimes empty lines
477 if (empty($component[0])) {
478 continue;
480 $component[0]=trim($component[0]);
481 $component[1]=trim($component[1]);
482 if (!empty($component[2])) {
483 $component[2]=trim($component[2]);
485 $comp_arr[$component[0]] = $component;
487 /// Cache components
488 $this->cachedmd5components[$source] = $comp_arr;
489 } else {
490 /// Return error
491 $this->errorstring='cannotdownloadcomponents';
492 return false;
495 /// If there is no commponents, error
496 if (empty($comp_arr)) {
497 $this->errorstring='cannotdownloadcomponents';
498 return false;
500 return $comp_arr;
504 * This function returns the errorstring
506 * @return string the error string
508 function get_error() {
509 return $this->errorstring;
512 /** This function returns the extramd5 field (optional in md5 file)
514 * @return string the extramd5 field
516 function get_extra_md5_field() {
517 return $this->extramd5info;
520 } /// End of component_installer class