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 | |
9214025e |
49 | /** |
50 | * DML read exception - triggered by SQL syntax errors, missing tables, etc. |
51 | */ |
52 | class dml_read_exception extends dml_exception { |
53 | public $error; |
54 | public $sql; |
55 | public $params; |
56 | |
57 | function __construct($error, $sql=null, array $params=null) { |
58 | $this->error = $error; |
59 | $this->sql = $sql; |
60 | $this->params = $params; |
61 | $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']'; |
62 | parent::__construct('dmlreadexception', NULL, $errorinfo); |
63 | } |
64 | } |
65 | |
66 | /** |
67 | * DML read exception - triggered by SQL syntax errors, missing tables, etc. |
68 | */ |
69 | class dml_write_exception extends dml_exception { |
70 | public $error; |
71 | public $sql; |
72 | public $params; |
73 | |
74 | function __construct($error, $sql=null, array $params=null) { |
75 | $this->error = $error; |
76 | $this->sql = $sql; |
77 | $this->params = $params; |
78 | $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']'; |
79 | parent::__construct('dmlwriteexception', NULL, $errorinfo); |
80 | } |
81 | } |
82 | |
8aff8482 |
83 | /** |
84 | * Sets up global $DB moodle_database instance |
85 | * @return void |
86 | */ |
87 | function setup_DB() { |
88 | global $CFG, $DB; |
89 | |
90 | if (isset($DB)) { |
91 | return; |
92 | } |
93 | |
94 | if (!isset($CFG->dbuser)) { |
95 | $CFG->dbuser = ''; |
96 | } |
97 | |
98 | if (!isset($CFG->dbpass)) { |
99 | $CFG->dbpass = ''; |
100 | } |
101 | |
102 | if (!isset($CFG->dbname)) { |
103 | $CFG->dbname = ''; |
104 | } |
105 | |
8aff8482 |
106 | if (!isset($CFG->dblibrary)) { |
3eae57b7 |
107 | switch ($CFG->dbtype) { |
108 | case 'postgres7' : |
109 | $CFG->dbtype = 'pgsql'; |
110 | // continue, no break here |
111 | case 'pgsql' : |
112 | $CFG->dblibrary = 'native'; |
113 | break; |
114 | |
115 | case 'mysql' : |
116 | $CFG->dbtype = 'mysqli'; |
117 | // continue, no break here |
118 | case 'mysqli' : |
119 | $CFG->dblibrary = 'native'; |
120 | break; |
121 | |
122 | default: |
123 | // the rest of drivers is not converted yet - keep adodb for now |
124 | $CFG->dblibrary = 'adodb'; |
125 | } |
8aff8482 |
126 | } |
127 | |
128 | if (!isset($CFG->dboptions)) { |
129 | $CFG->dboptions = array(); |
130 | } |
131 | |
beaa43db |
132 | if (isset($CFG->dbpersist)) { |
133 | $CFG->dboptions['dbpersist'] = $CFG->dbpersist; |
134 | } |
135 | |
136 | |
3eae57b7 |
137 | if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) { |
138 | throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype"); |
139 | } |
8aff8482 |
140 | |
141 | $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now |
142 | |
143 | $driverstatus = $DB->driver_installed(); |
144 | |
145 | if ($driverstatus !== true) { |
1fe1d104 |
146 | throw new dml_exception('dbdriverproblem', $driverstatus); |
8aff8482 |
147 | } |
148 | |
149 | if (debugging('', DEBUG_ALL)) { |
150 | // catch errors |
151 | ob_start(); |
152 | } else { |
153 | $prevdebug = error_reporting(0); |
154 | } |
3eae57b7 |
155 | |
156 | $connected = false; |
157 | try { |
158 | $connected = $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions); |
159 | } Catch (Exception $e) { |
160 | $connected = false; |
161 | } |
162 | |
163 | if (!$connected) { |
164 | $dberr = ''; |
8aff8482 |
165 | if (debugging('', DEBUG_ALL)) { |
166 | if ($dberr = ob_get_contents()) { |
167 | $dberr = '<p><em>'.$dberr.'</em></p>'; |
168 | } |
169 | ob_end_clean(); |
8aff8482 |
170 | } |
171 | if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) { |
172 | if (file_exists($CFG->dataroot.'/emailcount')){ |
173 | $fp = fopen($CFG->dataroot.'/emailcount', 'r'); |
174 | $content = fread($fp, 24); |
175 | fclose($fp); |
176 | if((time() - (int)$content) > 600){ |
177 | @mail($CFG->emailconnectionerrorsto, |
178 | 'WARNING: Database connection error: '.$CFG->wwwroot, |
179 | 'Connection error: '.$CFG->wwwroot); |
180 | $fp = fopen($CFG->dataroot.'/emailcount', 'w'); |
181 | fwrite($fp, time()); |
182 | } |
183 | } else { |
184 | @mail($CFG->emailconnectionerrorsto, |
185 | 'WARNING: Database connection error: '.$CFG->wwwroot, |
186 | 'Connection error: '.$CFG->wwwroot); |
187 | $fp = fopen($CFG->dataroot.'/emailcount', 'w'); |
188 | fwrite($fp, time()); |
189 | } |
190 | } |
1fe1d104 |
191 | throw new dml_exception('dbconnectionfailed', $dberr); |
8aff8482 |
192 | } |
193 | if (debugging('', DEBUG_ALL)) { |
194 | ob_end_clean(); |
195 | } else { |
196 | error_reporting($prevdebug); |
197 | } |
198 | |
199 | return true; |
200 | } |