Skip to content

Commit 5e51f52

Browse files
committed
Improve Handler loadSecretHash
Move parse_ini_file to loadSecretHash to throw a more informative error.
1 parent fc46d88 commit 5e51f52

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

src/models/DB.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ private function loadDatabase($credentials_location)
6262
$this->table_prefix = $db_credentials["likel_db"]["table_prefix"];
6363

6464
return $pdo_object;
65-
6665
} catch(\PDOException $e) {
6766
throw new \Exception($e->getMessage());
6867
}

src/models/Session/Handler.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ function __construct($parameters = array())
4747
if($this->db->databaseInitialised()) {
4848
// Attempt to get the secret_hash from the credentials file
4949
try {
50-
$session_credentials = parse_ini_file($parameters["credentials_location"], true);
51-
$this->secret_hash = $this->loadSecretHash($session_credentials["likel_session"]);
50+
$this->secret_hash = $this->loadSecretHash($parameters["credentials_location"]);
5251

5352
// Start session
5453
$this->start_session($parameters["session_name"], $parameters["secure"]);
@@ -58,6 +57,33 @@ function __construct($parameters = array())
5857
}
5958
}
6059

60+
/**
61+
* Attempt to retrieve the secret_hash from the credentials file
62+
*
63+
* @param array $credentials likel_session from the credentials.ini file
64+
* @return string
65+
* @throws \Exception If credentials empty or not found
66+
*/
67+
private function loadSecretHash($credentials_location)
68+
{
69+
if(file_exists($credentials_location)) {
70+
$session_credentials = parse_ini_file($credentials_location, true);
71+
$credentials = $session_credentials["likel_session"];
72+
73+
if(!empty($credentials)){
74+
if(!empty($credentials["secret_hash"])) {
75+
return $credentials["secret_hash"];
76+
} else {
77+
throw new \Exception('The session_hash variable is empty.');
78+
}
79+
} else {
80+
throw new \Exception('The likel_session parameter in the credentials file cannot be found.');
81+
}
82+
} else {
83+
throw new \Exception('The credential file could not be located.');
84+
}
85+
}
86+
6187
/**
6288
* Open the session if the db connection has been made
6389
*
@@ -247,27 +273,6 @@ private function getKeyAndIv($id)
247273
}
248274
}
249275

250-
/**
251-
* Attempt to retrieve the secret_hash from the credentials file
252-
*
253-
* @param array $credentials likel_session from the credentials.ini file
254-
* @return string
255-
* @throws \Exception If credentials empty or not found
256-
*/
257-
private function loadSecretHash($credentials)
258-
{
259-
if(!empty($credentials)){
260-
if(!empty($credentials["secret_hash"])) {
261-
return $credentials["secret_hash"];
262-
} else {
263-
throw new \Exception('The session_hash variable is empty.');
264-
}
265-
266-
} else {
267-
throw new \Exception('The credential file could not be located or is empty.');
268-
}
269-
}
270-
271276
/**
272277
* Setup and start the session
273278
*

0 commit comments

Comments
 (0)