Completed
Push — master ( a729dc...acd68c )
by Yannick
08:59
created

create_db   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
C import_file() 0 30 7
A import_all_db() 0 10 3
C create_database() 0 29 7
1
<?php
2
require_once(dirname(__FILE__).'/../require/settings.php');
3
require_once(dirname(__FILE__).'/../require/class.Connection.php');
4
5
class create_db {
6
	public static function import_file($filename) {
7
		$filename = filter_var($filename,FILTER_SANITIZE_STRING);
8
		$Connection = new Connection();
9
		//Connection::$db->beginTransaction();
10
		$templine = '';
11
		$handle = @fopen($filename,"r");
12
		if ($handle) {
13
			//$lines = file($filename);
14
			//foreach ($lines as $line)
15
			while (($line = fgets($handle,4096)) !== false)
16
			{
17
				if (substr($line,0,2) == '--' || $line == '') continue;
18
				$templine .= $line;
19
				if (substr(trim($line), -1,1) == ';')
20
				{
21
					try {
22
						$sth = $Connection->db->prepare($templine);
23
						$sth->execute();
24
					} catch(PDOException $e) {
25
						return "error (import ".$filename.") : ".$e->getMessage()."\n";
26
					}
27
					$templine = '';
28
				}
29
			}
30
			fclose($handle);
31
		}
32
                //Connection::$db->commit();
33
                $Connection->db = null;
34
                return '';
35
	}
36
37
	public static function import_all_db($directory) {
38
		$error = '';
39
		$dh = opendir($directory);
40
		//foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $filename)
41
		while(false !== ($filename = readdir($dh)))
42
		{
43
		    if (preg_match('/\.sql$/',$filename)) $error .= create_db::import_file($directory.$filename);
44
		}
45
		return $error;
46
	}
47
48
	public static function create_database($root,$root_pass,$user,$pass,$db,$db_type,$host) {
49
		$root = filter_var($root,FILTER_SANITIZE_STRING);
50
		$root_pass = filter_var($root_pass,FILTER_SANITIZE_STRING);
51
		$user = filter_var($user,FILTER_SANITIZE_STRING);
52
		$password = filter_var($pass,FILTER_SANITIZE_STRING);
53
		$db = filter_var($db,FILTER_SANITIZE_STRING);
54
		$db_type = filter_var($db_type,FILTER_SANITIZE_STRING);
55
		$host = filter_var($host,FILTER_SANITIZE_STRING);
56
		// Dirty hack
57
		if ($host != 'localhost' && $host != '127.0.0.1') {
58
		    $grantright = $_SERVER['SERVER_ADDR'];
59
		} else $grantright = 'localhost';
60
		try {
61
			$dbh = new PDO($db_type.':host='.$host,$root,$root_pass);
62
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
63
			if ($db_type == 'mysql') {
64
				$dbh->exec('CREATE DATABASE IF NOT EXISTS `'.$db.'`;GRANT ALL ON `'.$db."`.* TO '".$user."'@'".$grantright."' IDENTIFIED BY '".$password."';FLUSH PRIVILEGES;");
65
				if ($grantright == 'localhost') $dbh->exec('GRANT ALL ON `'.$db."`.* TO '".$user."'@'127.0.0.1' IDENTIFIED BY '".$password."';FLUSH PRIVILEGES;");
66
			} else if ($db_type == 'pgsql') {
67
				$dbh->exec("CREATE DATABASE ".$db.";");
68
				$dbh->exec("CREATE USER ".$user." WITH PASSWORD '".$password."';
69
					GRANT ALL PRIVILEGES ON DATABASE ".$db." TO ".$user.";");
70
			}
71
		} catch(PDOException $e) {
72
			$dbh = null;
0 ignored issues
show
Unused Code introduced by
$dbh is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
73
			return "error : ".$e->getMessage();
74
		}
75
		$dbh = null;
0 ignored issues
show
Unused Code introduced by
$dbh is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
	}
77
	
78
}
79
?>