aboutsummaryrefslogtreecommitdiffstats
path: root/lib/db.php
blob: 8d7c76756c117dfdffc1b38792f695c054c093d9 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
 * ownCloud
 *
 * @author Frank Karlitschek
 * @copyright 2010 Frank Karlitschek karlitschek@kde.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * This class manages the access to the database. It basically is a wrapper for
 * MDB2 with some adaptions.
 */
class OC_DB {
	static private $DBConnection=false;
	static private $schema=false;
	static private $affected=0;
	static private $result=false;

	/**
	 * @brief connects to the database
	 * @returns true if connection can be established or nothing (die())
	 *
	 * Connects to the database as specified in config.php
	 */
	static public function connect(){
		// The global data we need
		$CONFIG_DBNAME = OC_CONFIG::getValue( "dbname", "owncloud" );;
		$CONFIG_DBHOST = OC_CONFIG::getValue( "dbhost", "" );;
		$CONFIG_DBUSER = OC_CONFIG::getValue( "dbuser", "" );;
		$CONFIG_DBPASSWORD = OC_CONFIG::getValue( "dbpassword", "" );;
		$CONFIG_DBTYPE = OC_CONFIG::getValue( "dbtype", "sqlite" );;
		global $SERVERROOT;
		$datadir=OC_CONFIG::getValue( "datadirectory", "$SERVERROOT/data" );

		// do nothing if the connection already has been established
		if(!self::$DBConnection){
			// Require MDB2.php (not required in the head of the file so we only load it when needed)
			require_once('MDB2.php');

			// Prepare options array
			$options = array(
			  'portability' => MDB2_PORTABILITY_ALL,
			  'log_line_break' => '<br>',
			  'idxname_format' => '%s',
			  'debug' => true,
			  'quote_identifier' => true  );

			// Add the dsn according to the database type
			if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
				// sqlite
				$dsn = array(
				  'phptype'  => $CONFIG_DBTYPE,
				  'database' => "$datadir/$CONFIG_DBNAME.db",
				  'mode' => '0644' );
			}
			elseif( $CONFIG_DBTYPE == 'mysql' ){
				// MySQL
				$dsn = array(
				  'phptype'  => 'mysql',
				  'username' => $CONFIG_DBUSER,
				  'password' => $CONFIG_DBPASSWORD,
				  'hostspec' => $CONFIG_DBHOST,
				  'database' => $CONFIG_DBNAME );
			}
			elseif( $CONFIG_DBTYPE == 'pgsql' ){
				// PostgreSQL
				$dsn = array(
				  'phptype'  => 'pgsql',
				  'username' => $CONFIG_DBUSER,
				  'password' => $CONFIG_DBPASSWORD,
				  'hostspec' => $CONFIG_DBHOST,
				  'database' => $CONFIG_DBNAME );
			}

			// Try to establish connection
			self::$DBConnection = MDB2::factory( $dsn, $options );

			// Die if we could not connect
			if( PEAR::isError( self::$DBConnection )){
				echo( '<b>can not connect to database, using '.$CONFIG_DBTYPE.'. ('.self::$DBConnection->getUserInfo().')</center>');
				$error = self::$DBConnection->getMessage();
				error_log( $error);
				error_log( self::$DBConnection->getUserInfo());
				die( $error );
			}

			// We always, really always want associative arrays
			self::$DBConnection->setFetchMode(MDB2_FETCHMODE_ASSOC);

			//we need to function module for query pre-procesing
			self::$DBConnection->loadModule('Function');
		}

		// we are done. great!
		return true;
	}

	/**
	 * @brief SQL query
	 * @param $query Query string
	 * @returns result as MDB2_Result
	 *
	 * SQL query via MDB2 query()
	 */
	static public function query( $query ){
		// Optimize the query
		$query = self::processQuery( $query );

		self::connect();
		//fix differences between sql versions

		// return the result
		$result = self::$DBConnection->exec( $query );

		// Die if we have an error (error means: bad query, not 0 results!)
		if( PEAR::isError($result)) {
			$entry = 'DB Error: "'.$result->getMessage().'"<br />';
			$entry .= 'Offending command was: '.$cmd.'<br />';
			error_log( $entry );
			die( $entry );
		}

		return $result;
	}

	/**
	 * @brief Prepare a SQL query
	 * @param $query Query string
	 * @returns prepared SQL query
	 *
	 * SQL query via MDB2 prepare(), needs to be execute()'d!
	 */
	static public function prepare( $query ){
		// Optimize the query
		$query = self::processQuery( $query );

		self::connect();
		// return the result
		$result = self::$DBConnection->prepare( $query );

		// Die if we have an error (error means: bad query, not 0 results!)
		if( PEAR::isError($result)) {
			$entry = 'DB Error: "'.$result->getMessage().'"<br />';
			$entry .= 'Offending command was: '.$query.'<br />';
			error_log( $entry );
			die( $entry );
		}

		return $result;
	}

	/**
	 * @brief gets last value of autoincrement
	 * @returns id
	 *
	 * MDB2 lastInsertID()
	 *
	 * Call this method right after the insert command or other functions may
	 * cause trouble!
	 */
	public static function insertid(){
		self::connect();
		return self::$DBConnection->lastInsertID();
	}

	/**
	 * @brief Disconnect
	 * @returns true/false
	 *
	 * This is good bye, good bye, yeah!
	 */
	public static function disconnect(){
		// Cut connection if required
		if(self::$DBConnection){
			self::$DBConnection->disconnect();
			self::$DBConnection=false;
		}

		return true;
	}

	/**
	 * @brief Escapes bad characters
	 * @param $string string with dangerous characters
	 * @returns escaped string
	 *
	 * MDB2 escape()
	 */
	public static function escape( $string ){
		self::connect();
		return self::$DBConnection->escape( $string );
	}

	/**
	 * @brief saves database scheme to xml file
	 * @param $file name of file
	 * @returns true/false
	 *
	 * TODO: write more documentation
	 */
	public static function getDbStructure( $file ){
		self::connectScheme();

		// write the scheme
		$definition = self::$schema->getDefinitionFromDatabase();
		$dump_options = array(
			'output_mode' => 'file',
			'output' => $file,
			'end_of_line' => "\n"
		);
		self::$schema->dumpDatabase( $definition, $dump_options, MDB2_SCHEMA_DUMP_STRUCTURE );

		return true;
	}

	/**
	 * @brief Creates tables from XML file
	 * @param $file file to read structure from
	 * @returns true/false
	 *
	 * TODO: write more documentation
	 */
	public static function createDbFromStructure( $file ){
		$CONFIG_DBNAME  = OC_CONFIG::getValue( "dbname", "owncloud" );
		$CONFIG_DBTABLEPREFIX = OC_CONFIG::getValue( "dbtableprefix", "oc_" );

		self::connectScheme();

		// read file
		$content = file_get_contents( $file );
		
		// Make changes and save them to a temporary file
		$file2 = tempnam( sys_get_temp_dir(), 'oc_db_scheme_' );
		$content = str_replace( '*dbname*', $CONFIG_DBNAME, $content );
		$content = str_replace( '*dbprefix*', $CONFIG_DBTABLEPREFIX, $content );
		file_put_contents( $file2, $content );

		// Try to create tables
		$definition = self::$schema->parseDatabaseDefinitionFile( $file2 );
		
		// Delete our temporary file
		unlink( $file2 );

		// Die in case something went wrong
		if( $definition instanceof MDB2_Schema_Error ){
			die( $definition->getMessage().': '.$definition->getUserInfo());
		}
// 		if(OC_CONFIG::getValue('dbtype','sqlite')=='sqlite'){
// 			$definition['overwrite']=true;//always overwrite for sqlite
// 		}
		$ret=self::$schema->createDatabase( $definition );

		// Die in case something went wrong
		if( $ret instanceof MDB2_Error ){
			die ($ret->getMessage() . ': ' . $ret->getUserInfo());
		}

		return true;
	}

	/**
	 * @brief connects to a MDB2 database scheme
	 * @returns true/false
	 *
	 * Connects to a MDB2 database scheme
	 */
	private static function connectScheme(){
		// We need a database connection
		self::connect();

		// Connect if this did not happen before
		if(!self::$schema){
			require_once('MDB2/Schema.php');
			self::$schema=MDB2_Schema::factory(self::$DBConnection);
		}

		return true;
	}

	/**
	 * @brief does minor chages to query
	 * @param $query Query string
	 * @returns corrected query string
	 *
	 * This function replaces *PREFIX* with the value of $CONFIG_DBTABLEPREFIX
	 * and replaces the ` woth ' or " according to the database driver.
	 */
	private static function processQuery( $query ){
		self::connect();
		// We need Database type and table prefix
		$CONFIG_DBTYPE = OC_CONFIG::getValue( "dbtype", "sqlite" );
		$CONFIG_DBTABLEPREFIX = OC_CONFIG::getValue( "dbtableprefix", "oc_" );
		
		// differences is getting the current timestamp
		$query = str_replace( 'NOW()', self::$DBConnection->now(), $query );
		$query = str_replace( 'now()', self::$DBConnection->now(), $query );
		
		// differences in escaping of table names (` for mysql)
		// Problem: what if there is a ` in the value we want to insert?
		if( $CONFIG_DBTYPE == 'sqlite' ){
			$query = str_replace( '`', '\'', $query );
		}
		elseif( $CONFIG_DBTYPE == 'pgsql' ){
			$query = str_replace( '`', '"', $query );
		}

		// replace table name prefix
		$query = str_replace( '*PREFIX*', $CONFIG_DBTABLEPREFIX, $query );

		return $query;
	}
	
	/**
	 * @brief drop a table
	 * @param string $tableNamme the table to drop
	 */
	public static function dropTable($tableName){
		self::connect();
		self::$DBConnection->loadModule('Manager');
		self::$DBConnection->dropTable($tableName);
	}
	
	/**
	 * remove all tables defined in a database structure xml file
	 * @param string $file the xml file describing the tables
	 */
	public static function removeDBStructure($file){
		$CONFIG_DBNAME  = OC_CONFIG::getValue( "dbname", "owncloud" );
		$CONFIG_DBTABLEPREFIX = OC_CONFIG::getValue( "dbtableprefix", "oc_" );
		self::connectScheme();

		// read file
		$content = file_get_contents( $file );

		// Make changes and save them to a temporary file
		$file2 = tempnam( sys_get_temp_dir(), 'oc_db_scheme_' );
		$content = str_replace( '*dbname*', $CONFIG_DBNAME, $content );
		$content = str_replace( '*dbprefix*', $CONFIG_DBTABLEPREFIX, $content );
		file_put_contents( $file2, $content );

		// get the tables
		$definition = self::$schema->parseDatabaseDefinitionFile( $file2 );
		
		// Delete our temporary file
		unlink( $file2 );
		foreach($definition['tables'] as $name=>$table){
			self::dropTable($name);
		}
	}
}
?>