Completed
Push — master ( 91ef4a...f9a26b )
by Yannick
23:48
created

create_db::import_all_db()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
7
	/**
8
	 * @param string $filename
9
	 */
10
	public static function import_file($filename) {
11
		$filename = filter_var($filename,FILTER_SANITIZE_STRING);
12
		$Connection = new Connection();
13
		//Connection::$db->beginTransaction();
14
		$templine = '';
15
		$handle = @fopen($filename,"r");
16
		if ($handle) {
17
			//$lines = file($filename);
18
			//foreach ($lines as $line)
19
			while (($line = fgets($handle,4096)) !== false)
20
			{
21
				if (substr($line,0,2) == '--' || $line == '') continue;
22
				$templine .= $line;
23
				if (substr(trim($line), -1,1) == ';')
24
				{
25
					try {
26
						$sth = $Connection->db->prepare($templine);
27
						$sth->execute();
28
					} catch(PDOException $e) {
29
						return "error (import ".$filename.") : ".$e->getMessage()."\n";
30
					}
31
					$templine = '';
32
				}
33
			}
34
			fclose($handle);
35
		}
36
		//Connection::$db->commit();
37
		$Connection->db = null;
38
		return '';
39
	}
40
41
	public static function import_all_db($directory) {
42
		$error = '';
43
		$dh = opendir($directory);
44
		//foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $filename)
45
		while(false !== ($filename = readdir($dh)))
46
		{
47
			if (preg_match('/\.sql$/',$filename)) $error .= create_db::import_file($directory.$filename);
48
		}
49
		return $error;
50
	}
51
52
	public static function create_database($root,$root_pass,$user,$pass,$db,$db_type,$host,$port = '') {
53
		$root = filter_var($root,FILTER_SANITIZE_STRING);
54
		$root_pass = filter_var($root_pass,FILTER_SANITIZE_STRING);
55
		$user = filter_var($user,FILTER_SANITIZE_STRING);
56
		$password = filter_var($pass,FILTER_SANITIZE_STRING);
57
		$db = filter_var($db,FILTER_SANITIZE_STRING);
58
		$db_type = filter_var($db_type,FILTER_SANITIZE_STRING);
59
		$host = filter_var($host,FILTER_SANITIZE_STRING);
60
		if ($db_type == 'mysql' && $port == '') $port = 3306;
61
		elseif ($port == '') $port = 5432;
62
		// Dirty hack
63
		if ($host != 'localhost' && $host != '127.0.0.1') {
64
			$grantright = $_SERVER['SERVER_ADDR'];
65
		} else $grantright = 'localhost';
66
		try {
67
			if ($host == 'localhost') $dbh = new PDO($db_type.':host=127.0.0.1',$root,$root_pass);
68
			else $dbh = new PDO($db_type.':host='.$host.';port='.$port,$root,$root_pass);
69
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
70
			if ($db_type == 'mysql') {
71
				$dbh->exec('CREATE DATABASE IF NOT EXISTS `'.$db.'`;GRANT ALL ON `'.$db."`.* TO '".$user."'@'".$grantright."' IDENTIFIED BY '".$password."';FLUSH PRIVILEGES;");
72
				if ($grantright == 'localhost') $dbh->exec('GRANT ALL ON `'.$db."`.* TO '".$user."'@'127.0.0.1' IDENTIFIED BY '".$password."';FLUSH PRIVILEGES;");
73
			} else if ($db_type == 'pgsql') {
74
				$dbh->exec("CREATE DATABASE ".$db.";");
75
				$dbh->exec("CREATE USER ".$user." WITH PASSWORD '".$password."';
76
					GRANT ALL PRIVILEGES ON DATABASE ".$db." TO ".$user.";");
77
			}
78
		} catch(PDOException $e) {
79
			$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...
80
			return "error : ".$e->getMessage();
81
		}
82
		$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...
83
	}
84
	
85
}
86
?>