Skip to content

Commit 73ecb8e

Browse files
authored
Merge pull request #4 from likel/unit-tests
Unit tests
2 parents 2f38435 + fd4be22 commit 73ecb8e

File tree

6 files changed

+298
-63
lines changed

6 files changed

+298
-63
lines changed

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This package is designed to store PHP sessions in a MySQL database so that you c
1212

1313
### Installing on your server
1414

15-
Create a session table in your MySQL database by running [install/setup.sql](install/setup.sql)
15+
1. Create a session table in your MySQL database by running [install/setup.sql](install/setup.sql)
1616

1717
```
1818
CREATE TABLE `likel_sessions` (
@@ -25,20 +25,36 @@ CREATE TABLE `likel_sessions` (
2525
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2626
```
2727

28-
Step 2
28+
2. Move the files under /src into a directory on your server such as "session"
2929

3030
```
31-
Example
31+
e.g. session/ini, session/models, session/autoload.php, session/example.php
3232
```
3333

34-
Run example.php
34+
3. Move the [ini/credentials.ini](ini/credentials.ini) file to a location not accessible by the public
35+
36+
```
37+
e.g. $ mv ini/credentials /var/www/html/
38+
```
39+
40+
4. Update the database information in the credentials.ini file
41+
42+
5. Ensure that when you create a new session you specify the new credentials.ini location
43+
44+
```
45+
$session = new Likel\Session\Handler(array(
46+
'credentials_location' => "/path/to/new/credentials.ini"
47+
));
48+
```
49+
50+
6. Run [src/example.php](src/example.php) and check your database for the newly created session
3551

3652
## Running the tests
3753

38-
Run [file] with PHPUnit
54+
Run [test/SessionHandlerTest.php](test/SessionHandlerTest.php) with PHPUnit
3955

4056
```
41-
$ phpunit Test.php
57+
$ phpunit SessionHandlerTest.php
4258
```
4359

4460
## Author

src/autoload.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22
/**
3-
* Load the models
4-
*
5-
* Sadly we can't use an autoloader here in the case that the end-user
6-
* is using one. Multiple autoloaders can cause conflicts
3+
* PSR-4 autoload
74
*
5+
* After registering this autoload function with require_once()
86
* Likel/Session/Handler can be called like this:
97
*
108
* $session = new Likel\Session\Handler();
@@ -17,6 +15,25 @@
1715
* @version 1.0.0
1816
*/
1917

20-
// Require the models
21-
require_once(__DIR__ . '/models/DB.php');
22-
require_once(__DIR__ . '/models/Session/Handler.php');
18+
// Require the models when called
19+
spl_autoload_register(function ($class_name) {
20+
// Change these depending on the project
21+
$project_prefix = 'Likel\\';
22+
$models_dir = __DIR__ . '/models/';
23+
24+
// Helper variables used in the autoloader
25+
$project_prefix_length = strlen($project_prefix);
26+
$relative_class = substr($class_name, $project_prefix_length);
27+
28+
// Return if the requested class does not include the prefix
29+
if (strncmp($project_prefix, $class_name, $project_prefix_length) !== 0) {
30+
return;
31+
}
32+
33+
// Replace the namespace prefix with the base directory, replace namespace
34+
// separators with directory separators in the class name and append with .php
35+
$file = $models_dir . str_replace('\\', '/', $relative_class) . '.php';
36+
if (file_exists($file)) {
37+
require_once($file);
38+
}
39+
});

src/ini/.htaccess

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# It is highly suggested to move credentials.ini to a directory not
2+
# accessible by the public, such as /var/www/html on linux
3+
#
4+
# If you need to keep the credentials.ini accessible, ensure that you
5+
# give the file '600' permissions '$ chmod 600 credentials.ini' and
6+
# that you have this .htaccess file to deny access or redirect if accessed
7+
#
8+
# @package php-simple-sessions
9+
# @author Liam Kelly <https://github.com/likel>
10+
# @copyright 2017 Liam Kelly
11+
# @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
12+
# @link https://github.com/likel/php-simple-sessions
13+
# @version 1.0.0
14+
15+
deny from all

src/models/DB.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @copyright 2017 Liam Kelly
1010
* @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
1111
* @link https://github.com/likel/php-simple-sessions
12-
* @version 1.0.0
12+
* @version 1.0.1
1313
*/
1414
namespace Likel;
1515

@@ -28,11 +28,9 @@ class DB
2828
public function __construct($credentials_location)
2929
{
3030
try {
31-
$db_credentials = parse_ini_file($credentials_location, true);
32-
$this->database_handler = $this->loadDatabase($db_credentials["likel_db"]);
33-
$this->table_prefix = $db_credentials["likel_db"]["table_prefix"];
31+
$this->database_handler = $this->loadDatabase($credentials_location);
3432
} catch (\Exception $ex) {
35-
throw $ex;
33+
echo $ex->getMessage();
3634
}
3735
}
3836

@@ -44,27 +42,34 @@ public function __construct($credentials_location)
4442
* @throws \Exception If credentials empty or not found
4543
* @throws \PDOException If PDO connection is unsuccessful
4644
*/
47-
private function loadDatabase($credentials)
45+
private function loadDatabase($credentials_location)
4846
{
49-
if(!empty($credentials)){
50-
try {
51-
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['db_name'];
47+
if(file_exists($credentials_location)) {
48+
$db_credentials = parse_ini_file($credentials_location, true);
49+
$credentials = $db_credentials["likel_db"];
5250

53-
$options = array(
54-
\PDO::ATTR_PERSISTENT => true,
55-
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
56-
);
51+
if(!empty($credentials)){
52+
try {
53+
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['db_name'];
5754

58-
$pdo_object = new \PDO($dsn, $credentials['username'], $credentials['password'], $options);
55+
$options = array(
56+
\PDO::ATTR_PERSISTENT => true,
57+
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
58+
);
5959

60-
return $pdo_object;
60+
$pdo_object = new \PDO($dsn, $credentials['username'], $credentials['password'], $options);
6161

62-
} catch(\PDOException $e) {
63-
throw new \Exception($e->getMessage());
64-
}
62+
$this->table_prefix = $db_credentials["likel_db"]["table_prefix"];
6563

64+
return $pdo_object;
65+
} catch(\PDOException $e) {
66+
throw new \Exception($e->getMessage());
67+
}
68+
} else {
69+
throw new \Exception('The likel_db parameter in the credentials file cannot be found.');
70+
}
6671
} else {
67-
throw new \Exception('The credential file could not be located or is empty.');
72+
throw new \Exception('The credential file could not be located.');
6873
}
6974
}
7075

@@ -222,4 +227,9 @@ public function dumpStatement()
222227
{
223228
$this->statement->debugDumpParams();
224229
}
230+
231+
public function databaseInitialised()
232+
{
233+
return !empty($this->database_handler);
234+
}
225235
}

src/models/Session/Handler.php

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @copyright 2017 Liam Kelly
1414
* @license MIT License <https://github.com/likel/php-simple-sessions/blob/master/LICENSE>
1515
* @link https://github.com/likel/php-simple-sessions
16-
* @version 1.0.0
16+
* @version 1.0.1
1717
*/
1818
namespace Likel\Session;
1919

@@ -32,24 +32,56 @@ class Handler implements \ArrayAccess
3232
*/
3333
function __construct($parameters = array())
3434
{
35+
if(!is_array($parameters)) {
36+
$parameters = array();
37+
}
38+
3539
// Defaults
3640
$parameters["session_name"] = empty($parameters["session_name"]) ? "likel_session" : $parameters["session_name"];
37-
$parameters["secure"] = empty($parameters["secure"]) ? false : $parameters["secure"];
41+
$parameters["secure"] = empty($parameters["secure"]) ? false : is_bool($parameters["secure"] === true) ? true : false;
3842
$parameters["credentials_location"] = empty($parameters["credentials_location"]) ? __DIR__ . '/../../ini/credentials.ini' : $parameters["credentials_location"];
3943

4044
// Setup the database class variable
4145
$this->db = new \Likel\DB($parameters["credentials_location"]);
4246

43-
// Attempt to get the secret_hash from the credentials file
44-
try {
45-
$session_credentials = parse_ini_file($parameters["credentials_location"], true);
46-
$this->secret_hash = $this->loadSecretHash($session_credentials["likel_session"]);
47-
} catch (\Exception $ex) {
48-
throw $ex;
47+
if($this->db->databaseInitialised()) {
48+
// Attempt to get the secret_hash from the credentials file
49+
try {
50+
$this->secret_hash = $this->loadSecretHash($parameters["credentials_location"]);
51+
52+
// Start session
53+
$this->start_session($parameters["session_name"], $parameters["secure"]);
54+
} catch (\Exception $ex) {
55+
echo $ex->getMessage();
56+
}
4957
}
58+
}
5059

51-
// Start session
52-
$this->start_session($parameters["session_name"], $parameters["secure"]);
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+
}
5385
}
5486

5587
/**
@@ -241,27 +273,6 @@ private function getKeyAndIv($id)
241273
}
242274
}
243275

244-
/**
245-
* Attempt to retrieve the secret_hash from the credentials file
246-
*
247-
* @param array $credentials likel_session from the credentials.ini file
248-
* @return string
249-
* @throws \Exception If credentials empty or not found
250-
*/
251-
private function loadSecretHash($credentials)
252-
{
253-
if(!empty($credentials)){
254-
if(!empty($credentials["secret_hash"])) {
255-
return $credentials["secret_hash"];
256-
} else {
257-
throw new \Exception('The session_hash variable is empty.');
258-
}
259-
260-
} else {
261-
throw new \Exception('The credential file could not be located or is empty.');
262-
}
263-
}
264-
265276
/**
266277
* Setup and start the session
267278
*
@@ -294,6 +305,9 @@ private function start_session($session_name, $secure)
294305
session_name($session_name);
295306
session_start();
296307

308+
// Put it into the DB so we don't delay
309+
$this->_write(session_id(), '');
310+
297311
// Regenerate ID is recommended to reset the session every reload
298312
// Bug occurs if set to true that causes the current session to
299313
// be removed if loading pages too quickly

0 commit comments

Comments
 (0)