|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Trapdirector; |
|
4
|
|
|
|
|
5
|
|
|
use Trapdirector\Logging; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use PDO; |
|
9
|
|
|
use PDOException; |
|
10
|
|
|
|
|
11
|
|
|
class Database |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
// Databases |
|
15
|
|
|
protected $trapDB=null; //< trap database |
|
16
|
|
|
protected $idoDB=null; //< ido database |
|
17
|
|
|
public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
18
|
|
|
public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
19
|
|
|
|
|
20
|
|
|
protected $trapDSN; //< trap database connection params |
|
21
|
|
|
protected $trapUsername; //< trap database connection params |
|
22
|
|
|
protected $trapPass; //< trap database connection params |
|
23
|
|
|
|
|
24
|
|
|
protected $idoSet; //< bool true is ido database set |
|
25
|
|
|
protected $idoDSN; //< trap database connection params |
|
26
|
|
|
protected $idoUsername; //< trap database connection params |
|
27
|
|
|
protected $idoPass; //< trap database connection params |
|
28
|
|
|
|
|
29
|
|
|
// Logging function |
|
30
|
|
|
|
|
31
|
|
|
protected $logging; //< logging class |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Logging $logClass : where to log |
|
35
|
|
|
* @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
36
|
|
|
*/ |
|
37
|
|
|
function __construct($logClass,$dbParam) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->logging=$logClass; |
|
40
|
|
|
|
|
41
|
|
|
$this->trapDSN=$this->setupDSN($dbParam); |
|
42
|
|
|
$this->trapUsername = $dbParam['username']; |
|
43
|
|
|
$this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
44
|
|
|
$this->trapDBType=$dbParam['db']; |
|
45
|
|
|
$this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername,INFO); |
|
46
|
|
|
$this->db_connect_trap(); |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Setup and connect to IDO database |
|
52
|
|
|
* @param array $dbParam : array of named params |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setupIDO($dbParam) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->idoDSN=$this->setupDSN($dbParam); |
|
57
|
|
|
$this->idoUsername = $dbParam['username']; |
|
58
|
|
|
$this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
59
|
|
|
$this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
60
|
|
|
$this->idoDBType=$dbParam['db']; |
|
61
|
|
|
$this->db_connect_ido(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Connect to IDO database |
|
66
|
|
|
* @return \PDO |
|
67
|
|
|
*/ |
|
68
|
|
|
public function db_connect_ido() |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->idoDB != null) { |
|
71
|
|
|
// Check if connection is still alive |
|
72
|
|
|
try { |
|
73
|
|
|
$this->idoDB->query('select 1')->fetchColumn(); |
|
74
|
|
|
return $this->idoDB; |
|
75
|
|
|
} catch (Exception $e) { |
|
76
|
|
|
// select 1 failed, try to reconnect. |
|
77
|
|
|
$this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
try { |
|
81
|
|
|
$this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
82
|
|
|
} catch (PDOException $e) { |
|
83
|
|
|
$this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
84
|
|
|
} |
|
85
|
|
|
return $this->idoDB; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Connect to Trap database |
|
90
|
|
|
* @return \PDO |
|
91
|
|
|
*/ |
|
92
|
|
|
public function db_connect_trap() |
|
93
|
|
|
{ |
|
94
|
|
|
|
|
95
|
|
|
if ($this->trapDB != null) { |
|
96
|
|
|
// Check if connection is still alive |
|
97
|
|
|
try { |
|
98
|
|
|
$this->trapDB->query('select 1')->fetchColumn(); |
|
99
|
|
|
return $this->trapDB; |
|
|
|
|
|
|
100
|
|
|
} catch (Exception $e) { |
|
101
|
|
|
// select 1 failed, try to reconnect. |
|
102
|
|
|
$this->logging->log('Database connection lost, reconnecting',WARN); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
try { |
|
106
|
|
|
$this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
107
|
|
|
} catch (PDOException $e) { |
|
108
|
|
|
$this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
109
|
|
|
} |
|
110
|
|
|
return $this->trapDB; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Setup dsn and check parameters |
|
115
|
|
|
* @param array $configElmt |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function setupDSN($configElmt) |
|
119
|
|
|
{ |
|
120
|
|
|
if (!array_key_exists('db',$configElmt) || |
|
121
|
|
|
!array_key_exists('host',$configElmt) || |
|
122
|
|
|
!array_key_exists('dbname',$configElmt) || |
|
123
|
|
|
!array_key_exists('username',$configElmt)) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->logging->log('Missing DB params',ERROR); |
|
126
|
|
|
return ''; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
130
|
|
|
$dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
131
|
|
|
|
|
132
|
|
|
if (array_key_exists('port', $configElmt)) |
|
133
|
|
|
{ |
|
134
|
|
|
$dsn .= ';port='.$configElmt['port']; |
|
135
|
|
|
} |
|
136
|
|
|
return $dsn; |
|
137
|
|
|
} |
|
138
|
|
|
} |