forked from Ysurac/FlightAirMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.create_db.php
75 lines (70 loc) · 2.82 KB
/
class.create_db.php
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
<?php
require_once(dirname(__FILE__).'/../require/settings.php');
require_once(dirname(__FILE__).'/../require/class.Connection.php');
class create_db {
public static function import_file($filename) {
$filename = filter_var($filename,FILTER_SANITIZE_STRING);
$Connection = new Connection();
//Connection::$db->beginTransaction();
$templine = '';
$lines = file($filename);
foreach ($lines as $line)
{
if (substr($line,0,2) == '--' || $line == '') continue;
$templine .= $line;
if (substr(trim($line), -1,1) == ';')
{
try {
$sth = $Connection->db->prepare($templine);
$sth->execute();
} catch(PDOException $e) {
return "error (import ".$filename.") : ".$e->getMessage()."\n";
}
$templine = '';
}
}
//Connection::$db->commit();
$Connection->db = null;
return '';
}
public static function import_all_db($directory) {
$error = '';
$dh = opendir($directory);
//foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $filename)
while(false !== ($filename = readdir($dh)))
{
if (preg_match('/\.sql$/',$filename)) $error .= create_db::import_file($directory.$filename);
}
return $error;
}
public static function create_database($root,$root_pass,$user,$pass,$db,$db_type,$host) {
$root = filter_var($root,FILTER_SANITIZE_STRING);
$root_pass = filter_var($root_pass,FILTER_SANITIZE_STRING);
$user = filter_var($user,FILTER_SANITIZE_STRING);
$password = filter_var($pass,FILTER_SANITIZE_STRING);
$db = filter_var($db,FILTER_SANITIZE_STRING);
$db_type = filter_var($db_type,FILTER_SANITIZE_STRING);
$host = filter_var($host,FILTER_SANITIZE_STRING);
// Dirty hack
if ($host != 'localhost' && $host != '127.0.0.1') {
$grantright = $_SERVER['SERVER_ADDR'];
} else $grantright = 'localhost';
try {
$dbh = new PDO($db_type.':host='.$host,$root,$root_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($db_type == 'mysql') {
$dbh->exec('CREATE DATABASE IF NOT EXISTS `'.$db.'`;GRANT ALL ON `'.$db."`.* TO '".$user."'@'".$grantright."' IDENTIFIED BY '".$password."';FLUSH PRIVILEGES;");
if ($grantright == 'localhost') $dbh->exec('GRANT ALL ON `'.$db."`.* TO '".$user."'@'127.0.0.1' IDENTIFIED BY '".$password."';FLUSH PRIVILEGES;");
} else if ($db_type == 'pgsql') {
$dbh->exec("CREATE DATABASE ".$db.";
CREATE USER ".$user." WITH PASSWORD '".$password."';
GRANT ALL PRIVILEGES ON DATABASE ".$db." TO ".$user.";");
}
} catch(PDOException $e) {
$dbh = null;
return "error : ".$e->getMessage();
}
$dbh = null;
}
}
?>