1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CILogon\Service; |
4
|
|
|
|
5
|
|
|
require_once 'DB.php'; |
6
|
|
|
|
7
|
|
|
use CILogon\Service\Util; |
8
|
|
|
use PEAR; |
9
|
|
|
use DB; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* DBProps |
13
|
|
|
* |
14
|
|
|
* This class reads the config.ini file for the username, password, |
15
|
|
|
* and database name used for storage. You specify which database type |
16
|
|
|
* to use when calling the constructor, either 'mysql' or 'pgsql'. There |
17
|
|
|
* is also a method getDBConnect to open a PEAR DB database connection |
18
|
|
|
* using the parameters in the config file. |
19
|
|
|
*/ |
20
|
|
|
class DBProps |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string $dbtype Either 'mysqli' or 'pgsql' |
24
|
|
|
*/ |
25
|
|
|
private $dbtype; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* __construct |
29
|
|
|
* |
30
|
|
|
* The constuctor sets the $dbtype class variable. |
31
|
|
|
* |
32
|
|
|
* @param string $db Database type, either 'mysqli' or 'pgsql'. |
33
|
|
|
*/ |
34
|
|
|
public function __construct($db) |
35
|
|
|
{ |
36
|
|
|
$this->dbtype = $db; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* queryAttribute |
41
|
|
|
* |
42
|
|
|
* This is a general method looks in the cilogon.ini file for the |
43
|
|
|
* named database configuration attribute. |
44
|
|
|
* |
45
|
|
|
* @param string $attr Name of the attribute to query, one of |
46
|
|
|
* 'username', 'password', or 'database'. |
47
|
|
|
* @return string The value of the desired attribute, or empty string |
48
|
|
|
* on error. |
49
|
|
|
*/ |
50
|
|
|
public function queryAttribute($attr) |
51
|
|
|
{ |
52
|
|
|
return constant(strtoupper($this->dbtype) . '_' . strtoupper($attr)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* getUsername |
57
|
|
|
* |
58
|
|
|
* This is a convenience method which calls queryAttribute to get |
59
|
|
|
* the database username. |
60
|
|
|
* |
61
|
|
|
* @return string The username for the selected database type. |
62
|
|
|
*/ |
63
|
|
|
public function getUsername() |
64
|
|
|
{ |
65
|
|
|
return $this->queryAttribute('username'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* getPassword |
70
|
|
|
* |
71
|
|
|
* This is a convenience method which calls queryAttribute to get |
72
|
|
|
* the database password. |
73
|
|
|
* |
74
|
|
|
* @return string The password for the selected database type. |
75
|
|
|
*/ |
76
|
|
|
public function getPassword() |
77
|
|
|
{ |
78
|
|
|
return $this->queryAttribute('password'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* getDatabase |
83
|
|
|
* |
84
|
|
|
* This is a convenience method which calls queryAttribute to get |
85
|
|
|
* the database name. |
86
|
|
|
* |
87
|
|
|
* @return string The database name for the selected database type. |
88
|
|
|
*/ |
89
|
|
|
public function getDatabase() |
90
|
|
|
{ |
91
|
|
|
return $this->queryAttribute('database'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* getHostspec |
96
|
|
|
* |
97
|
|
|
* This is a convenience method which calls queryAttribute to get |
98
|
|
|
* the hostspec, i.e., 'host:port' to connect to. If the hostspec |
99
|
|
|
* has not been configured, return 'localhost'. |
100
|
|
|
* |
101
|
|
|
* @return string The hostspec type for the selected database type. |
102
|
|
|
* Defaults to 'localhost'. |
103
|
|
|
*/ |
104
|
|
|
public function getHostspec() |
105
|
|
|
{ |
106
|
|
|
$hostspec = $this->queryAttribute('hostspec'); |
107
|
|
|
if (strlen($hostspec) == 0) { |
108
|
|
|
$hostspec = 'localhost'; |
109
|
|
|
} |
110
|
|
|
return $hostspec; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* getDBConnect |
115
|
|
|
* |
116
|
|
|
* This function uses the PEAR DB module to connect to database |
117
|
|
|
* using the parameters found in config.secrets.php. Upon |
118
|
|
|
* success, it returns a DB connection returned by 'DB::connect' |
119
|
|
|
* suitable for future DB calls. If there is a problem connecting, |
120
|
|
|
* it returns null. |
121
|
|
|
* |
122
|
|
|
* @return DB A PEAR DB object connected to a database, or null |
123
|
|
|
* on error connecting to database. |
124
|
|
|
*/ |
125
|
|
|
public function getDBConnect() |
126
|
|
|
{ |
127
|
|
|
$db = new DB(); // So defined constants get read in |
|
|
|
|
128
|
|
|
$dsn = array( |
129
|
|
|
'phptype' => $this->dbtype, |
130
|
|
|
'username' => $this->getUsername(), |
131
|
|
|
'password' => $this->getPassword(), |
132
|
|
|
'database' => $this->getDatabase(), |
133
|
|
|
'hostspec' => $this->getHostspec() |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$opts = array( |
137
|
|
|
'persistent' => true, |
138
|
|
|
'portability' => DB_PORTABILITY_ALL |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$retval = DB::connect($dsn, $opts); |
142
|
|
|
if (PEAR::isError($retval)) { |
143
|
|
|
$retval = null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $retval; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|