7e13be08 |
1 | <?php // $Id$ |
2 | |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.com // |
9 | // // |
ddaadc7c |
10 | // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // |
7e13be08 |
11 | // // |
12 | // This program is free software; you can redistribute it and/or modify // |
13 | // it under the terms of the GNU General Public License as published by // |
14 | // the Free Software Foundation; either version 2 of the License, or // |
15 | // (at your option) any later version. // |
16 | // // |
17 | // This program is distributed in the hope that it will be useful, // |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20 | // GNU General Public License for more details: // |
21 | // // |
22 | // http://www.gnu.org/copyleft/gpl.html // |
23 | // // |
24 | /////////////////////////////////////////////////////////////////////////// |
25 | |
eef868d1 |
26 | /// This library contains all the Data Manipulation Language (DML) functions |
7e13be08 |
27 | /// used to interact with the DB. All the dunctions in this library must be |
28 | /// generic and work against the major number of RDBMS possible. This is the |
29 | /// list of currently supported and tested DBs: mysql, postresql, mssql, oracle |
30 | |
9f948d11 |
31 | /// This library is automatically included by Moodle core so you never need to |
32 | /// include it yourself. |
7e13be08 |
33 | |
34 | /// For more info about the functions available in this library, please visit: |
35 | /// http://docs.moodle.org/en/DML_functions |
36 | /// (feel free to modify, improve and document such page, thanks!) |
37 | |
8aff8482 |
38 | require_once($CFG->libdir.'/dml/moodle_database.php'); |
39 | |
49459eb0 |
40 | /** |
41 | * DML exception class, use instead of error() in dml code. |
42 | */ |
43 | class dml_exception extends moodle_exception { |
44 | function __construct($errorcode, $a=NULL, $debuginfo=null) { |
45 | parent::__construct($errorcode, '', '', $a, $debuginfo); |
46 | } |
47 | } |
48 | |
8aff8482 |
49 | /** |
50 | * Sets up global $DB moodle_database instance |
51 | * @return void |
52 | */ |
53 | function setup_DB() { |
54 | global $CFG, $DB; |
55 | |
56 | if (isset($DB)) { |
57 | return; |
58 | } |
59 | |
60 | if (!isset($CFG->dbuser)) { |
61 | $CFG->dbuser = ''; |
62 | } |
63 | |
64 | if (!isset($CFG->dbpass)) { |
65 | $CFG->dbpass = ''; |
66 | } |
67 | |
68 | if (!isset($CFG->dbname)) { |
69 | $CFG->dbname = ''; |
70 | } |
71 | |
8aff8482 |
72 | if (!isset($CFG->dblibrary)) { |
73 | $CFG->dblibrary = 'adodb'; |
74 | } |
75 | |
76 | if (!isset($CFG->dboptions)) { |
77 | $CFG->dboptions = array(); |
78 | } |
79 | |
beaa43db |
80 | if (isset($CFG->dbpersist)) { |
81 | $CFG->dboptions['dbpersist'] = $CFG->dbpersist; |
82 | } |
83 | |
84 | |
8aff8482 |
85 | $DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary); |
86 | |
87 | $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now |
88 | |
89 | $driverstatus = $DB->driver_installed(); |
90 | |
91 | if ($driverstatus !== true) { |
1fe1d104 |
92 | throw new dml_exception('dbdriverproblem', $driverstatus); |
8aff8482 |
93 | } |
94 | |
95 | if (debugging('', DEBUG_ALL)) { |
96 | // catch errors |
97 | ob_start(); |
98 | } else { |
99 | $prevdebug = error_reporting(0); |
100 | } |
beaa43db |
101 | if (!$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions)) { |
8aff8482 |
102 | if (debugging('', DEBUG_ALL)) { |
103 | if ($dberr = ob_get_contents()) { |
104 | $dberr = '<p><em>'.$dberr.'</em></p>'; |
105 | } |
106 | ob_end_clean(); |
107 | } else { |
108 | $dberr = ''; |
109 | } |
110 | if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) { |
111 | if (file_exists($CFG->dataroot.'/emailcount')){ |
112 | $fp = fopen($CFG->dataroot.'/emailcount', 'r'); |
113 | $content = fread($fp, 24); |
114 | fclose($fp); |
115 | if((time() - (int)$content) > 600){ |
116 | @mail($CFG->emailconnectionerrorsto, |
117 | 'WARNING: Database connection error: '.$CFG->wwwroot, |
118 | 'Connection error: '.$CFG->wwwroot); |
119 | $fp = fopen($CFG->dataroot.'/emailcount', 'w'); |
120 | fwrite($fp, time()); |
121 | } |
122 | } else { |
123 | @mail($CFG->emailconnectionerrorsto, |
124 | 'WARNING: Database connection error: '.$CFG->wwwroot, |
125 | 'Connection error: '.$CFG->wwwroot); |
126 | $fp = fopen($CFG->dataroot.'/emailcount', 'w'); |
127 | fwrite($fp, time()); |
128 | } |
129 | } |
1fe1d104 |
130 | throw new dml_exception('dbconnectionfailed', $dberr); |
8aff8482 |
131 | } |
132 | if (debugging('', DEBUG_ALL)) { |
133 | ob_end_clean(); |
134 | } else { |
135 | error_reporting($prevdebug); |
136 | } |
137 | |
138 | return true; |
139 | } |