summaryrefslogtreecommitdiffstats
path: root/lib/migrate.php
blob: 84eafcd4cdce1c04f10441beecdf4834b57f9f97 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<?php
/**
 * ownCloud
 *
 * @author Tom Needham
 * @copyright 2012 Tom Needham tom@owncloud.com
 *
 * 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/>.
 *
 */


/**
 * provides an interface to all search providers
 */
class OC_Migrate{
	
	// Holds the db object
	static private $MDB2=false;
	// Array of OC_Migration_Provider objects
	static private $providers=array();
	// Schema db object
	static private $schema=false;
	// User id of the user to import/export
	static private $uid=false;
	// Path to the sqlite db
	static private $dbpath=false;
	// Holds the ZipArchive object
	static private $zip=false;
	// String path to export
	static private $zippath=false;
	// Stores the type of export
	static private $exporttype=false;
	// Array of temp files to be deleted after zip creation
	static private $tmpfiles=array();
	
	/**
	 * register a new migration provider
	 * @param OC_Migrate_Provider $provider
	 */
	public static function registerProvider($provider){
		self::$providers[]=$provider;
	}
	
	/** 
	* @breif finds and loads the providers
	*/
	static private function findProviders(){
		// Find the providers
		$apps = OC_App::getAllApps();
		
		foreach($apps as $app){
			$path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php';
			if( file_exists( $path ) ){
				include( $path );	
			}	
		}	
	}
	
	/**
	 * @breif creates a migration.db in the users data dir with their app data in
	 * @return bool whether operation was successfull
	 */
	private static function exportAppData( ){
				
		self::connectDB();
		$ok = true;
		$return = array();
		
		// Find the providers
		self::findProviders();
		
		// Foreach provider
		foreach( self::$providers as $provider ){
			$failed = false;
			
			// Does this app use the database?
			if(file_exists(OC::$SERVERROOT.'/apps/'.$provider->id.'/appinfo/database.xml')){
				// Create some app tables
				$tables = self::createAppTables( $provider->id );
				if( is_array( $tables ) ){
					// Save the table names
					foreach($tables as $table){
						$return['apps'][$provider->id]['tables'][] = $table;	
					}	
				} else {
					// It failed to create the tables
					$failed = true;
				}	
			}
			
			// Run the import function?
			if( !$failed ){
				$return['apps'][$provider->id]['success'] = $provider->export( self::$uid );	
			} else {
				$return['apps'][$provider->id]['success'] = false;	
				$return['apps'][$provider->id]['message'] = 'failed to create the app tables';	
			}
			
			// Now add some app info the the return array
			$appinfo = OC_App::getAppInfo( $provider->id );
			$return['apps'][$provider->id]['version'] = $appinfo['version'];
			
		}
		
		return $return;
		
	}
	
	/**
	* @breif creates an export file for the whole system
	* @param optional $exporttype string export type ('instance','system' or 'userfiles')
	* @param optional $path string path to zip destination (with trailing slash)
	* @return path to the zip or false if there was a problem
	*/
	static public function createSysExportFile( $exporttype='instance', $path=null ){
		// Calculate zip name
		$zipname = "oc_export_" . date("y-m-d_H-i-s") . ".zip";
		// Get the data dir
		$datadir = OC_Config::getValue( 'datadirectory' );
		// Calculate destination
		if( !is_null( $path ) ){
			// Path given 
			// Is a directory?
			if( !is_dir( $path ) ){
				OC_Log::write('migration', 'Path supplied to createSysExportFile() is not a directory', OC_Log::ERROR);
				return false;
			}	
			// Is writeable
			if( !is_writeable( $path ) ){
				OC_Log::write('migration', 'Path supplied to createSysExportFile() is not writeable', OC_Log::ERROR);	
				return false;
			}
			self::$zippath = $path . $zipname;
		} else {
			// Save in tmp dir
			self::$zippath = sys_get_temp_dir() . '/' . $zipname;
		}	
		// Create the zip object
		self::$zip = new ZipArchive;
		// Try to create the zip
	    if( !self::createZip() ){
	    	return false;
	    }
		// Handle export types
		$exporttypes = array( 'userfiles', 'instance', 'system' );
		self::$exporttype = in_array( $exporttype, $exporttypes ) ? $exporttype : false;
		if( !self::$exporttype ){
			OC_Log::write( 'migration', 'Export type: '.$exporttype.' is not supported.', OC_Log::ERROR);
			return false;	
		}
		switch( self::$exporttype ){
			case 'instance':
				// Creates a zip that is compatable with the import function
				$dbfile = tempnam( "/tmp", "owncloud_export_data_" );
				OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL');
				
				// Now add in *dbname* and *dbprefix*
				$dbexport = file_get_contents( $dbfile );
				$dbnamestring = "<database>\n\n <name>" . OC_Config::getValue( "dbname", "owncloud" );
				$dbtableprefixstring = "<table>\n\n  <name>" . OC_Config::getValue( "dbtableprefix", "oc_" );
				$dbexport = str_replace( $dbnamestring, "<database>\n\n <name>*dbname*", $dbexport );
				$dbexport = str_replace( $dbtableprefixstring, "<table>\n\n  <name>*dbprefix*", $dbexport );
				// Write the new db export file
				file_put_contents( $dbfile, $dbexport );
				self::$zip->addFile( $dbfile, "dbexport.xml" );
				// Add user data
				foreach(OC_User::getUsers() as $user){
					self::addDirToZip( $datadir . '/' . $user . '/', true, "/userdata/" );	
				}
			break;
			case 'userfiles':
				// Creates a zip with all of the users files
				foreach(OC_User::getUsers() as $user){
					self::addDirToZip( $datadir . '/' . $user . '/', true, "/" );	
				}
			break;
			case 'system':
				// Creates a zip with the owncloud system files
				self::addDirToZip( OC::$SERVERROOT . '/', false, '/');
				foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) {
			    	self::addDirToZip( OC::$SERVERROOT . '/' . $dir, true, "/");
				}
			break;	
		}
		// Add export info
		self::addExportInfo();
		// Close the zip
		if( !self::closeZip() ){
			return false;	
		}
		return self::$zippath;
		
	}
	
	/**
	* @breif adds a json file with infomation on the export to the zips root (used on import)
	* @return bool
	*/
	static private function addExportInfo( $array=array() ){
		$info = array(
						'ocversion' => OC_Util::getVersion(),
						'exporttime' => time(),
						'exportedby' => OC_User::getUser(),
						'exporttype' => self::$exporttype
					);
		// Add hash if user export
		if( self::$exporttype = 'user' ){
			$query = OC_DB::prepare( "SELECT password FROM *PREFIX*users WHERE uid LIKE ?" );
			$result = $query->execute( array( self::$uid ) );
			$row = $result->fetchRow();
			$hash = $row ? $row['password'] : false;
			if( !$hash ){
				OC_Log::write( 'migration', 'Failed to get the users password hash', OC_log::ERROR);
				return false;
			}
			$info['hash'] = $hash; 
			$info['exporteduser'] = self::$uid; 	
		}
		// Merge in other data
		$info = array_merge( $info, $array );
		// Create json
		$json = json_encode( $info );
		$tmpfile = tempnam("/tmp", "oc_export_info_");
		self::$tmpfiles[] = $tmpfile;
		if( !file_put_contents( $tmpfile, $json ) ){
			return false;
		} else {
			self::$zip->addFile( $tmpfile, "/" . self::$uid . "/export_info.json" );
			return true;
		}
	}
	
	
	/**
	* @breif tried to finalise the zip
	* @return bool
	*/
	static private function closeZip(){
		if( !self::$zip->close() ){
			OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR);
			self::cleanup();
			return false;
		} else {
			OC_Log::write('migration', 'Export zip created ok', OC_Log::INFO);
			self::cleanup();
			return true;	
		}	
	}
	
	/**
	* @breif cleans up after the zip
	*/
	static private function cleanup(){
		// Delete tmp files
		foreach(self::$tmpfiles as $i){
			unlink( $i );	
		}	
	}
	
	/**
	* @breif creates a zip user export
	* @param optional $uid string user id of the user to export (defaults to current)
	* @param optional $path string path to folder to create file in (with trailing slash) (defaults to current user's data dir)
	* @return false on failure | string path on success
	*/
	static public function createUserExportFile( $uid=null, $path=null ){
		// User passed?
		$uid = is_null( $uid ) ? OC_User::getUser() : $uid ;
		// Is a database user?
		if( !OC_User_Database::userExists( $uid ) ){
			OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR);
			return false;	
		}
		// Set the uid
		self::$uid = $uid;
		// Create the zip object
		self::$zip = new ZipArchive;
		// Set export type
		self::$exporttype = 'user';
		// Calculate users data dir
		$user = OC_User::getUser();
		$userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . $user . '/';
		// Calculate zip name
		$zipname = "oc_userexport_" . $user . '_' . date("y-m-d_H-i-s") . ".zip";
		// Calculate destination
		if( !is_null( $path ) ){
			// Path given 
			// Is a directory?
			if( !is_dir( $path ) ){
				OC_Log::write('migration', 'Path supplied to createUserExportFile() is not a directory', OC_Log::ERROR);
				return false;
			}	
			// Is writeable
			if( !is_writeable( $path ) ){
				OC_Log::write('migration', 'Path supplied to createUserExportFile() is not writeable', OC_Log::ERROR);	
				return false;
			}
			self::$zippath = $path . $zipname;
		} else {
			// Save in users data dir
			self::$zippath = $userdatadir . $zipname;
		}
		// Try to create the zip
	    if( !self::createZip() ){
	    	return false;
	    }
	    // Export the app info
	    $appinfo = self::exportAppData();
	    // Save the migration results
	    self::addExportInfo( $appinfo );
		// Add the data dir to the zip
		self::addDirToZip( $userdatadir );
	    // Close the zip
		if( !self::closeZip() ){
			return false;	
		}
		// All good
	    return self::$zippath;
	} 
	
	/**
	* @breif tries to create the zip
	* @return bool
	*/
	static private function createZip(){
		// Check if properties are set
		if( !self::$zip || !self::$zippath ){
			OC_Log::write('migration', 'createZip() called but $zip and/or $zippath have not been set', OC_Log::ERROR);
			return false;	
		}
		if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE ) !== TRUE ) {
			OC_Log::write('migration', 'Failed to create the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR);
			return false;
	    } else {
	    	return true;	
	    }	
	}
		
	/**
	* @breif adds a directory to the zip object
	* @param $dir string path of the directory to add
	* @param $recursive bool 
	* @param $internaldir string path of folder to add dir to in zip
	* @return bool
	*/
	static private function addDirToZip($dir, $recursive=true, $internaldir='') {
	    $dirname = basename($dir);
	    self::$zip->addEmptyDir($internaldir . $dirname);
	    $internaldir.=$dirname.='/';
	
	    if ($dirhandle = opendir($dir)) {
			while (false !== ( $file = readdir($dirhandle))) {
	
				if (( $file != '.' ) && ( $file != '..' )) {
		
					if (is_dir($dir . '/' . $file) && $recursive) {
						self::addDirToZip($dir . '/' . $file, $recursive, $internaldir);
					} elseif (is_file($dir . '/' . $file)) {
						self::$zip->addFile($dir . '/' . $file, $internaldir . $file);
					}
				}
			}
			closedir($dirhandle);
	    } else {
			OC_Log::write('admin_export',"Was not able to open directory: " . $dir,OC_Log::ERROR);
			return false;
	    }
	    return true;
	}
	
	/**
	* @breif returns an array of apps that support migration
	* @return array
	*/
	static public function getApps(){
		$allapps = OC_App::getAllApps();
		foreach($allapps as $app){
			$path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php';
			if( file_exists( $path ) ){
				$supportsmigration[] = $app;
			}	
		}
		return $supportsmigration;	
	}
	
	/**
	* @breif imports a new user
	* @param $db string path to migration.db
	* @param $info array of migration ino
	* @param $uid optional uid to use
	* @return bool if the import succedded
	*/
	public static function importAppData( $db, $info, $uid=null ){
				
		self::$uid = !is_null( $uid ) ? $uid : $info->exporteduser;
		
		// Check if the db exists
		if( file_exists( $db ) ){
			// Connect to the db
			if(!self::connectDB( $db )){
				OC_Log::write('migration','Failed to connect to migration.db',OC_Log::ERROR);
				return false;
			}	
		} else {
			OC_Log::write('migration','Migration.db not found at: '.$db, OC_Log::FATAL );	
			return false;
		}
		
		// Find providers
		self::findProviders();

		// Generate importinfo array
		$importinfo = array( 
							'olduid' => $info->exporteduser,
							'newuid' => self::$uid
							);
							
		foreach( self::$providers as $provider){
			// Is the app in the export?
			$id = $provider->id;
			if( isset( $info->apps->$id ) ){
				// Did it succeed?
				if( $info->apps->$id->success ){
					// Then do the import
					$provider->import( $info->apps->$id, $importinfo );	
				}	
			}		
		}
		
		return true;
	
	}
	
	// @breif connects to migration.db, or creates if not found
	// @param $db optional path to migration.db, defaults to user data dir
	// @return bool whether the operation was successful
	private static function connectDB( $dbpath=null ){
		OC_Log::write('migration','connecting to migration.db for user: '.self::$uid,OC_Log::INFO);
		// Fail if no user is set
		if(!self::$uid){
			OC_Log::write('migration','connectDB() called without self::$uid being set',OC_Log::INFO);
			return false;
		}
		// Already connected
		if(!self::$MDB2){
			require_once('MDB2.php');
			
			$datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
			
			self::$dbpath = $datadir.'/'.self::$uid.'/migration.db';//!is_null( $dbpath ) ? $dbpath : $datadir.'/'.self::$uid.'/migration.db';
			
			// Prepare options array
			$options = array(
				'portability' => MDB2_PORTABILITY_ALL & (!MDB2_PORTABILITY_FIX_CASE),
				'log_line_break' => '<br>',
				'idxname_format' => '%s',
				'debug' => true,
				'quote_identifier' => true
				);
			$dsn = array(
				'phptype'  => 'sqlite3',
				'database' => self::$dbpath,
				'mode' => '0644'
			);

			// Try to establish connection
			self::$MDB2 = MDB2::factory( $dsn, $options );
			// Die if we could not connect
			if( PEAR::isError( self::$MDB2 )){
				die(self::$MDB2->getMessage());
				OC_Log::write('migration', 'Failed to create/connect to migration.db',OC_Log::FATAL);
				OC_Log::write('migration',self::$MDB2->getUserInfo(),OC_Log::FATAL);
				OC_Log::write('migration',self::$MDB2->getMessage(),OC_Log::FATAL);
				return false;
			} else {
			}
			// We always, really always want associative arrays
			self::$MDB2->setFetchMode(MDB2_FETCHMODE_ASSOC);
		}
		return true;
		
	}
	
	// @breif prepares the db
	// @param $query the sql query to prepare
	public static function prepare( $query ){
		
		// Optimize the query
		$query = self::processQuery( $query );
		
		// Optimize the query
		$query = self::$MDB2->prepare( $query );
		
		// Die if we have an error (error means: bad query, not 0 results!)
		if( PEAR::isError( $query )) {
			$entry = 'DB Error: "'.$result->getMessage().'"<br />';
			$entry .= 'Offending command was: '.$query.'<br />';
			OC_Log::write('migration',$entry,OC_Log::FATAL);
			return false;
		} else {
			return $query;	
		}
		
	}
	
	// @breif processes the db query
	// @param $query the query to process
	// @return string of processed query
	private static function processQuery( $query ){
		
		self::connectDB();
		$prefix = '';
		
		$query = str_replace( '`', '\'', $query );
		$query = str_replace( 'NOW()', 'datetime(\'now\')', $query );
		$query = str_replace( 'now()', 'datetime(\'now\')', $query );

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

		return $query;
		
	}
	
	// @brief copys rows to migration.db from the main database
	// @param $options array of options.
	// @return bool
	public static function copyRows( $options ){
		if( !array_key_exists( 'table', $options ) ){
			return false;	
		}
		
		$return = array();
					
		// Need to include 'where' in the query?
		if( array_key_exists( 'matchval', $options ) && array_key_exists( 'matchcol', $options ) ){
			
			// If only one matchval, create an array
			if(!is_array($options['matchval'])){
				$options['matchval'] = array( $options['matchval'] );	
			}
			
			foreach( $options['matchval'] as $matchval ){
				// Run the query for this match value (where x = y value)
				$query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?" );
				$results = $query->execute( array( $matchval ) );
				$newreturns = self::insertData( $results, $options );
				$return = array_merge( $return, $newreturns );
			}

		} else {
			// Just get everything
			$query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] );
			$results = $query->execute();
			$return = self::insertData( $results, $options );
	
		}
		
		return $return;
		
	}
	
	// @breif saves a sql data set into migration.db
	// @param $data a sql data set returned from self::prepare()->query()
	// @param $options array of copyRows options
	// @return void
	private static function insertData( $data, $options ){
		$return = array();
		while( $row = $data->fetchRow() ){
			// Now save all this to the migration.db
			$fields = array();
			$values = array();
			foreach($row as $field=>$value){
				$fields[] = $field;
				$values[] = $value;
			}
			
			// Generate some sql
			$sql = "INSERT INTO `" . $options['table'] . '` ( `';
			$fieldssql = implode( '`, `', $fields );
			$sql .= $fieldssql . "` ) VALUES( ";
			$valuessql = substr( str_repeat( '?, ', count( $fields ) ),0,-2 );
			$sql .= $valuessql . " )";
			// Make the query
			$query = self::prepare( $sql );
			if(!$query){
				OC_Log::write('migration','Invalid sql produced: '.$sql,OC_Log::FATAL);	
				return false;
				exit();
			} else {
				$query->execute( $values );
				// Do we need to return some values?
				if( array_key_exists( 'idcol', $options ) ){
					// Yes we do
					$return[] = $row[$options['idcol']];	
				} else {
					// Take a guess and return the first field :)
					$return[] = reset($row);	
				}
			}
		}
		return $return;
	}
	
	// @breif creates the tables in migration.db from an apps database.xml
	// @param $appid string id of the app
	// @return bool whether the operation was successful
	private static function createAppTables( $appid ){
			
		if(!self::connectScheme()){
			return false;	
		}
		
		// There is a database.xml file			
		$content = file_get_contents( OC::$SERVERROOT . '/apps/' . $appid . '/appinfo/database.xml' );
		
		$file2 = 'static://db_scheme';
		$content = str_replace( '*dbname*', self::$uid.'/migration', $content );
		$content = str_replace( '*dbprefix*', '', $content );
		
		$xml = new SimpleXMLElement($content);
		foreach($xml->table as $table){
			$tables[] = (string)$table->name;	
		}	
		
		file_put_contents( $file2, $content );
		
		// Try to create tables
		$definition = self::$schema->parseDatabaseDefinitionFile( $file2 );

		unlink( $file2 );
		
		// Die in case something went wrong
		if( $definition instanceof MDB2_Schema_Error ){
			OC_Log::write('migration','Failed to parse database.xml for: '.$appid,OC_Log::FATAL);
			OC_Log::write('migration',$definition->getMessage().': '.$definition->getUserInfo(),OC_Log::FATAL);
			return false;
		}
		
		$definition['overwrite'] = true;
		
		$ret = self::$schema->createDatabase( $definition );
		// Die in case something went wrong
		
		if( $ret instanceof MDB2_Error ){
			OC_Log::write('migration','Failed to create tables for: '.$appid,OC_Log::FATAL);
			OC_Log::write('migration',$ret->getMessage().': '.$ret->getUserInfo(),OC_Log::FATAL);
			return false;
		}
		return $tables;

	}
	
	
	/**
	 * @brief connects to a MDB2 database scheme
	 * @returns true/false
	 *
	 * Connects to a MDB2 database scheme
	 */
	private static function connectScheme(){
		// We need a mdb2 database connection
		self::connectDB();
		self::$MDB2->loadModule( 'Manager' );
		self::$MDB2->loadModule( 'Reverse' );

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

		return true;
	}
	
	// @breif creates a new user in the database
	// @param $uid string user_id of the user to be created
	// @param $hash string hash of the user to be created
	// @return bool result of user creation
	public static function createUser( $uid, $hash ){
		
		// Check if userid exists
		if(OC_User::userExists( $uid )){
			return false;
		}
		
		// Create the user
		$query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" );
		$result = $query->execute( array( $uid, $hash));
		if( !$result ){
			OC_Log::write('migration', 'Failed to create the new user "'.$uid."");	
		}
		return $result ? true : false;
		
	}

}