Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
simplesamlphp
Manage
Activity
Members
Labels
Plan
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Perun
Perun ProxyIdP
v1
simplesamlphp
Commits
e2f0f2b9
Commit
e2f0f2b9
authored
9 years ago
by
Jaime Perez Crespo
Browse files
Options
Downloads
Patches
Plain Diff
Reformat code and fix some more phpdoc comments.
parent
4e6195b1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/SimpleSAML/Database.php
+262
-236
262 additions, 236 deletions
lib/SimpleSAML/Database.php
with
262 additions
and
236 deletions
lib/SimpleSAML/Database.php
+
262
−
236
View file @
e2f0f2b9
...
@@ -7,7 +7,7 @@ namespace SimpleSAML;
...
@@ -7,7 +7,7 @@ namespace SimpleSAML;
*
*
* This database class supports a single database, or a master/slave
* This database class supports a single database, or a master/slave
* configuration with as many defined slaves as a user would like.
* configuration with as many defined slaves as a user would like.
*
*
* The goal of this class is to provide a single mechanism to connect to a database
* The goal of this class is to provide a single mechanism to connect to a database
* that can be reused by any component within SimpleSAMLphp including modules.
* that can be reused by any component within SimpleSAMLphp including modules.
* When using this class, the global configuration should be passed here, but
* When using this class, the global configuration should be passed here, but
...
@@ -21,239 +21,265 @@ namespace SimpleSAML;
...
@@ -21,239 +21,265 @@ namespace SimpleSAML;
class
Database
class
Database
{
{
/**
/**
* This variable holds the instance of the session - Singleton approach.
* This variable holds the instance of the session - Singleton approach.
*/
*/
private
static
$instance
=
array
();
private
static
$instance
=
array
();
/**
/**
* PDO Object for the Master database server
* PDO Object for the Master database server
*/
*/
private
$dbMaster
;
private
$dbMaster
;
/**
/**
* Array of PDO Objects for configured database
* Array of PDO Objects for configured database
* slaves
* slaves
*/
*/
private
$dbSlaves
=
array
();
private
$dbSlaves
=
array
();
/**
/**
* Prefix to apply to the tables
* Prefix to apply to the tables
*/
*/
private
$tablePrefix
;
private
$tablePrefix
;
/**
* Retrieves the current database instance. Will create a new one if there isn't an existing connection.
/**
*
* Retrieves the current database instance. Will create a new one if there isn't an existing connection.
* @param \SimpleSAML_Configuration object $altConfig Optional: Instance of a SimpleSAML_Configuration class
*
* @return \SimpleSAML\Database The shared database connection.
* @param \SimpleSAML_Configuration $altConfig Optional: Instance of a SimpleSAML_Configuration class
*/
*
public
static
function
getInstance
(
$altConfig
=
null
)
* @return \SimpleSAML\Database The shared database connection.
{
*/
$config
=
(
$altConfig
)
?
$altConfig
:
\SimpleSAML_Configuration
::
getInstance
();
public
static
function
getInstance
(
$altConfig
=
null
)
$instanceId
=
self
::
generateInstanceId
(
$config
);
{
$config
=
(
$altConfig
)
?
$altConfig
:
\SimpleSAML_Configuration
::
getInstance
();
/* Check if we already have initialized the session. */
$instanceId
=
self
::
generateInstanceId
(
$config
);
if
(
isset
(
self
::
$instance
[
$instanceId
]))
{
return
self
::
$instance
[
$instanceId
];
/* Check if we already have initialized the session. */
}
if
(
isset
(
self
::
$instance
[
$instanceId
]))
{
return
self
::
$instance
[
$instanceId
];
/* Create a new session. */
}
self
::
$instance
[
$instanceId
]
=
new
Database
(
$config
);
return
self
::
$instance
[
$instanceId
];
/* Create a new session. */
}
self
::
$instance
[
$instanceId
]
=
new
Database
(
$config
);
return
self
::
$instance
[
$instanceId
];
/**
}
* Private constructor that restricts instantiation to getInstance().
*
* @param object $config Instance of the SimpleSAML_Configuration class
/**
*/
* Private constructor that restricts instantiation to getInstance().
private
function
__construct
(
$config
)
*
{
* @param \SimpleSAML_Configuration $config Instance of the SimpleSAML_Configuration class
$driverOptions
=
array
();
*/
if
(
$config
->
getBoolean
(
'database.persistent'
,
TRUE
))
{
private
function
__construct
(
$config
)
$driverOptions
=
array
(
\PDO
::
ATTR_PERSISTENT
=>
TRUE
);
{
}
$driverOptions
=
array
();
if
(
$config
->
getBoolean
(
'database.persistent'
,
true
))
{
// Connect to the master
$driverOptions
=
array
(
\PDO
::
ATTR_PERSISTENT
=>
true
);
$this
->
dbMaster
=
$this
->
connect
(
$config
->
getValue
(
'database.dsn'
),
$config
->
getValue
(
'database.username'
),
$config
->
getValue
(
'database.password'
),
$driverOptions
);
}
// Connect to any configured slaves
// Connect to the master
$slaves
=
$config
->
getValue
(
'database.slaves'
);
$this
->
dbMaster
=
$this
->
connect
(
if
(
count
(
$slaves
>=
1
))
{
$config
->
getString
(
'database.dsn'
),
foreach
(
$slaves
as
$slave
)
{
$config
->
getString
(
'database.username'
),
array_push
(
$this
->
dbSlaves
,
$this
->
connect
(
$slave
[
'dsn'
],
$slave
[
'username'
],
$slave
[
'password'
],
$driverOptions
));
$config
->
getString
(
'database.password'
),
}
$driverOptions
}
);
$this
->
tablePrefix
=
$config
->
getString
(
'database.prefix'
,
''
);
// Connect to any configured slaves
}
$slaves
=
$config
->
getArray
(
'database.slaves'
,
array
());
if
(
count
(
$slaves
>=
1
))
{
/**
foreach
(
$slaves
as
$slave
)
{
* Generate an Instance ID based on the database
array_push
(
* configuration.
$this
->
dbSlaves
,
*
$this
->
connect
(
* @param \SimpleSAML_Configuration $config Configuration class
$slave
[
'dsn'
],
*
$slave
[
'username'
],
* @return string $instanceId
$slave
[
'password'
],
*/
$driverOptions
private
static
function
generateInstanceId
(
$config
)
)
{
);
$assembledConfig
=
array
(
}
'master'
=>
array
(
}
'database.dsn'
=>
$config
->
getValue
(
'database.dsn'
),
'database.username'
=>
$config
->
getValue
(
'database.username'
),
$this
->
tablePrefix
=
$config
->
getString
(
'database.prefix'
,
''
);
'database.password'
=>
$config
->
getValue
(
'database.password'
),
}
'database.prefix'
=>
$config
->
getValue
(
'database.prefix'
),
'database.persistent'
=>
$config
->
getValue
(
'database.persistent'
),
),
/**
'slaves'
=>
$config
->
getValue
(
'database.slaves'
),
* Generate an Instance ID based on the database
);
* configuration.
*
return
sha1
(
serialize
(
$assembledConfig
));
* @param \SimpleSAML_Configuration $config Configuration class
}
*
* @return string $instanceId
/**
*/
* This function connects to a dabase.
private
static
function
generateInstanceId
(
$config
)
*
{
* @param string $dsn Database connection string
$assembledConfig
=
array
(
* @param string $username SQL user
'master'
=>
array
(
* @param string $password SQL password
'database.dsn'
=>
$config
->
getString
(
'database.dsn'
),
* @param array $options PDO options
'database.username'
=>
$config
->
getString
(
'database.username'
),
*
'database.password'
=>
$config
->
getString
(
'database.password'
),
* @return \PDO object
'database.prefix'
=>
$config
->
getString
(
'database.prefix'
,
''
),
*/
'database.persistent'
=>
$config
->
getBoolean
(
'database.persistent'
,
false
),
private
function
connect
(
$dsn
,
$username
,
$password
,
$options
)
),
{
'slaves'
=>
$config
->
getArray
(
'database.slaves'
,
array
()),
try
{
);
$db
=
new
\PDO
(
$dsn
,
$username
,
$password
,
$options
);
$db
->
setAttribute
(
\PDO
::
ATTR_ERRMODE
,
\PDO
::
ERRMODE_EXCEPTION
);
return
sha1
(
serialize
(
$assembledConfig
));
}
return
$db
;
}
catch
(
\PDOException
$e
)
{
throw
new
\Exception
(
"Database error: "
.
$e
->
getMessage
());
/**
}
* This function connects to a database.
}
*
* @param string $dsn Database connection string
/**
* @param string $username SQL user
* This function randomly selects a slave database server
* @param string $password SQL password
* to query. In the event no slaves are configured, it
* @param array $options PDO options
* will return the master.
*
*
* @throws \Exception If an error happens while trying to connect to the database.
* @return \PDO object
* @return \PDO object
*/
*/
private
function
getSlave
()
private
function
connect
(
$dsn
,
$username
,
$password
,
$options
)
{
{
if
(
count
(
$this
->
dbSlaves
)
>
0
)
{
try
{
$slaveId
=
rand
(
0
,
count
(
$this
->
dbSlaves
)
-
1
);
$db
=
new
\PDO
(
$dsn
,
$username
,
$password
,
$options
);
return
$this
->
dbSlaves
[
$slaveId
];
$db
->
setAttribute
(
\PDO
::
ATTR_ERRMODE
,
\PDO
::
ERRMODE_EXCEPTION
);
}
else
{
return
$this
->
dbMaster
;
return
$db
;
}
}
catch
(
\PDOException
$e
)
{
}
throw
new
\Exception
(
"Database error: "
.
$e
->
getMessage
());
}
/**
}
* This function simply applies the table prefix to
* a suppled table name.
*
/**
* @param string $table Table to apply prefix to, if configured
* This function randomly selects a slave database server
* @return string Table with configured prefix
* to query. In the event no slaves are configured, it
*/
* will return the master.
public
function
applyPrefix
(
$table
)
*
{
* @return \PDO object
return
$this
->
tablePrefix
.
$table
;
*/
}
private
function
getSlave
()
{
/**
if
(
count
(
$this
->
dbSlaves
)
>
0
)
{
* This function queries the database
$slaveId
=
rand
(
0
,
count
(
$this
->
dbSlaves
)
-
1
);
*
return
$this
->
dbSlaves
[
$slaveId
];
* @param \PDO $db PDO object to use
}
else
{
* @param string $stmt Prepared SQL statement
return
$this
->
dbMaster
;
* @param array $params Parameters
}
*
}
* @return \PDO statement object
*/
private
function
query
(
$db
,
$stmt
,
$params
)
/**
{
* This function simply applies the table prefix to a supplied table name.
assert
(
'is_object($db)'
);
*
assert
(
'is_string($stmt)'
);
* @param string $table Table to apply prefix to, if configured
assert
(
'is_array($params)'
);
*
* @return string Table with configured prefix
try
{
*/
$query
=
$db
->
prepare
(
$stmt
);
public
function
applyPrefix
(
$table
)
{
foreach
(
$params
as
$param
=>
$value
)
{
return
$this
->
tablePrefix
.
$table
;
if
(
is_array
(
$value
))
{
}
$query
->
bindValue
(
":
$param
"
,
$value
[
0
],
(
$value
[
1
])
?
$value
[
1
]
:
\PDO
::
PARAM_STR
);
}
else
{
$query
->
bindValue
(
":
$param
"
,
$value
,
\PDO
::
PARAM_STR
);
/**
}
* This function queries the database
}
*
* @param \PDO $db PDO object to use
$query
->
execute
();
* @param string $stmt Prepared SQL statement
* @param array $params Parameters
return
$query
;
*
}
catch
(
\PDOException
$e
)
{
* @throws \Exception If an error happens while trying to execute the query.
throw
new
\Exception
(
"Database error: "
.
$e
->
getMessage
());
* @return \PDO statement object
}
*/
}
private
function
query
(
$db
,
$stmt
,
$params
)
{
/**
assert
(
'is_object($db)'
);
* This function queries the database without using a
assert
(
'is_string($stmt)'
);
* prepared statement.
assert
(
'is_array($params)'
);
*
* @param \PDO $db PDO object to use
try
{
* @param string $stmt Prepared SQL statement
$query
=
$db
->
prepare
(
$stmt
);
* @param array $params Parameters
*
foreach
(
$params
as
$param
=>
$value
)
{
* @return \PDO statement object
if
(
is_array
(
$value
))
{
*/
$query
->
bindValue
(
":
$param
"
,
$value
[
0
],
(
$value
[
1
])
?
$value
[
1
]
:
\PDO
::
PARAM_STR
);
private
function
exec
(
$db
,
$stmt
)
}
else
{
{
$query
->
bindValue
(
":
$param
"
,
$value
,
\PDO
::
PARAM_STR
);
assert
(
'is_object($db)'
);
}
assert
(
'is_string($stmt)'
);
}
try
{
$query
->
execute
();
$query
=
$db
->
exec
(
$stmt
);
return
$query
;
return
$query
;
}
catch
(
\PDOException
$e
)
{
}
catch
(
\PDOException
$e
)
{
throw
new
\Exception
(
"Database error: "
.
$e
->
getMessage
());
throw
new
\Exception
(
"Database error: "
.
$e
->
getMessage
());
}
}
}
}
/**
/**
* This executes queries directly on the master.
* This function queries the database without using a
*
* prepared statement.
* @param string $stmt Prepared SQL statement
*
* @param array $params Parameters
* @param \PDO $db PDO object to use
*
* @param string $stmt Prepared SQL statement
* @return \PDO statement object
*
*/
* @throws \Exception If an error happens while trying to execute the query.
public
function
write
(
$stmt
,
$params
=
array
())
* @return \PDO statement object
{
*/
$db
=
$this
->
dbMaster
;
private
function
exec
(
$db
,
$stmt
)
{
if
(
is_array
(
$params
))
{
assert
(
'is_object($db)'
);
return
$this
->
query
(
$db
,
$stmt
,
$params
);
assert
(
'is_string($stmt)'
);
}
else
{
return
$this
->
exec
(
$db
,
$stmt
);
try
{
}
$query
=
$db
->
exec
(
$stmt
);
}
return
$query
;
/**
}
catch
(
\PDOException
$e
)
{
* This executes queries on a database server
throw
new
\Exception
(
"Database error: "
.
$e
->
getMessage
());
* that is determined by this::getSlave()
}
*
}
* @param string $stmt Prepared SQL statement
* @param array $params Parameters
*
/**
* @return \PDO statement object
* This executes queries directly on the master.
*/
*
public
function
read
(
$stmt
,
$params
=
array
())
* @param string $stmt Prepared SQL statement
{
* @param array $params Parameters
$db
=
$this
->
getSlave
();
*
* @return \PDO statement object
return
$this
->
query
(
$db
,
$stmt
,
$params
);
*/
}
public
function
write
(
$stmt
,
$params
=
array
())
{
$db
=
$this
->
dbMaster
;
if
(
is_array
(
$params
))
{
return
$this
->
query
(
$db
,
$stmt
,
$params
);
}
else
{
return
$this
->
exec
(
$db
,
$stmt
);
}
}
/**
* This executes queries on a database server
* that is determined by this::getSlave()
*
* @param string $stmt Prepared SQL statement
* @param array $params Parameters
*
* @return \PDO statement object
*/
public
function
read
(
$stmt
,
$params
=
array
())
{
$db
=
$this
->
getSlave
();
return
$this
->
query
(
$db
,
$stmt
,
$params
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment