Skip to content

Commit 6db4657

Browse files
authored
Merge pull request #1 from likel/initial-setup
Initial setup
2 parents fdb6a23 + d134168 commit 6db4657

File tree

7 files changed

+750
-0
lines changed

7 files changed

+750
-0
lines changed

CODE_OF_CONDUCT.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

install/setup.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE `likel_sessions` (
2+
`id` char(128) NOT NULL DEFAULT '',
3+
`set_time` char(10) NOT NULL,
4+
`data` text NOT NULL,
5+
`session_key` char(128) NOT NULL,
6+
`iv` varchar(16) NOT NULL,
7+
PRIMARY KEY (`id`)
8+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

src/autoload.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
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
7+
*
8+
* Likel/Session/Handler can be called like this:
9+
*
10+
* $session = new Likel\Session\Handler();
11+
*
12+
* @package php-simple-sessions
13+
* @author Liam Kelly <https://github.com/likel>
14+
* @copyright 2017 Liam Kelly
15+
* @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE>
16+
* @link https://github.com/likel/php-simple-sessions
17+
* @version 1.0.0
18+
*/
19+
20+
// Require the models
21+
require_once(__DIR__ . '/models/DB.php');
22+
require_once(__DIR__ . '/models/Session/Handler.php');

src/example.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Example usage of the php-simple-sessions package
4+
*
5+
* @package php-simple-sessions
6+
* @author Liam Kelly <https://github.com/likel>
7+
* @copyright 2017 Liam Kelly
8+
* @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE>
9+
* @link https://github.com/likel/php-simple-sessions
10+
* @version 1.0.0
11+
*/
12+
13+
header('Content-Type: text/plain');
14+
15+
// Load the scripts, alternatively use a custom PSR-4 autoloader
16+
include('autoload.php');
17+
18+
// This looks nicer and makes the session Handler easier to call
19+
use Likel\Session\Handler as LikelSession;
20+
21+
// Create a new session. Example parameters include:
22+
// $session = new LikelSession(array(
23+
// 'session_name' => "YourCustomSessionName",
24+
// 'credentials_location' => "/path/to/new/credentials.ini",
25+
// 'secure' => true
26+
// ));
27+
$session = new LikelSession();
28+
29+
// Set some session variables
30+
if(!isset($session["user_id"])) {
31+
$session["user_id"] = 1;
32+
$session["name"] = "Liam";
33+
$session["preferences"] = array(
34+
'language' => 'en-AU'
35+
);
36+
$_SESSION["test"] = "works";
37+
}
38+
39+
// Unset our name
40+
unset($session["name"]);
41+
42+
// Dump some variables and the session
43+
echo "The session_id is: " . session_id() . PHP_EOL . PHP_EOL;
44+
echo "The name is (should be blank): " . $session["name"] . PHP_EOL . PHP_EOL;
45+
echo "The user_id is: " . $session["user_id"] . PHP_EOL . PHP_EOL;
46+
echo "The preferences are: " . print_r($session["preferences"], true) . PHP_EOL;
47+
echo "The \$_SESSION variable still: " . $_SESSION["test"] . PHP_EOL . PHP_EOL;
48+
var_dump($session);
49+
50+
// You should no check the database table to see if a row has been added

src/ini/credentials.ini

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; This .ini file stores the database credentials
2+
;
3+
; Make sure that you set these variables when you install the package on your
4+
; environment. It is highly suggested that you move this .ini file to a location
5+
; not accessible to the public
6+
;
7+
; You can set the .ini path when creating a new LikelSession object like so:
8+
;
9+
; $session = new LikelSession(array(
10+
; "credentials_location" => "/path/to/new/credentials.ini"
11+
; ));
12+
;
13+
; @package php-simple-sessions
14+
; @author Liam Kelly <https://github.com/likel>
15+
; @copyright 2017 Liam Kelly
16+
; @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE>
17+
; @link https://github.com/likel/php-simple-sessions
18+
; @version 1.0.0
19+
20+
[likel_db]
21+
host = "localhost"
22+
username = "root"
23+
password = "root"
24+
db_name = "likel_sessions"
25+
table_prefix = "likel_"
26+
27+
[likel_session]
28+
secret_hash = "BnFEC}}[CvP_djqM~[sbcXZp`/N:Y3Vu28m9zX~@:[gM,BNTYA]nFwd}>(nJ*hse"

src/models/DB.php

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<?php
2+
/**
3+
* The database object which helps to abstract database functions
4+
*
5+
* Uses and requires PDO, generally available after PHP 5.1
6+
*
7+
* @package php-simple-sessions
8+
* @author Liam Kelly <https://github.com/likel>
9+
* @copyright 2017 Liam Kelly
10+
* @license MIT License <https://github.com/likel/fizz-buzz/blob/master/LICENSE>
11+
* @link https://github.com/likel/php-simple-sessions
12+
* @version 1.0.0
13+
*/
14+
namespace Likel;
15+
16+
class DB
17+
{
18+
private $database_handler; // Stores the database connection
19+
private $statement; // The MySQL query with prepared values
20+
private $table_prefix; // The table prefix from the credentials.ini file
21+
22+
/**
23+
* Construct the database object
24+
*
25+
* @param string $credentials_location The location of the credential file
26+
* @return void
27+
*/
28+
public function __construct($credentials_location)
29+
{
30+
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"];
34+
} catch (\Exception $ex) {
35+
throw $ex;
36+
}
37+
}
38+
39+
/**
40+
* Attempt to retrieve the likel_db ini array and connect to the database
41+
*
42+
* @param array $credentials likel_db from the credentials.ini file
43+
* @return mixed
44+
* @throws \Exception If credentials empty or not found
45+
* @throws \PDOException If PDO connection is unsuccessful
46+
*/
47+
private function loadDatabase($credentials)
48+
{
49+
if(!empty($credentials)){
50+
try {
51+
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['db_name'];
52+
53+
$options = array(
54+
\PDO::ATTR_PERSISTENT => true,
55+
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
56+
);
57+
58+
$pdo_object = new \PDO($dsn, $credentials['username'], $credentials['password'], $options);
59+
60+
return $pdo_object;
61+
62+
} catch(\PDOException $e) {
63+
throw new \Exception($e->getMessage());
64+
}
65+
66+
} else {
67+
throw new \Exception('The credential file could not be located or is empty.');
68+
}
69+
}
70+
71+
/**
72+
* Prepare the query from a supplied query string
73+
*
74+
* @param string $query The prepared query
75+
* @return void
76+
*/
77+
public function query($query)
78+
{
79+
$this->statement = $this->database_handler->prepare($query);
80+
}
81+
82+
/**
83+
* Bind properties to the statement
84+
* E.G. $DB->bind(':fname', 'Liam');
85+
*
86+
* @param string $param The parameter to replace
87+
* @param mixed $value The value replacement
88+
* @param mixed $type Force the PDO::PARAM type
89+
* @return void
90+
*/
91+
public function bind($param, $value, $type = null)
92+
{
93+
if (is_null($type)) {
94+
switch (true) {
95+
case is_int($value):
96+
$type = \PDO::PARAM_INT;
97+
break;
98+
case is_bool($value):
99+
$type = \PDO::PARAM_BOOL;
100+
break;
101+
case is_null($value):
102+
$type = \PDO::PARAM_NULL;
103+
break;
104+
default:
105+
$type = \PDO::PARAM_STR;
106+
}
107+
}
108+
109+
$this->statement->bindValue($param, $value, $type);
110+
}
111+
112+
/**
113+
* Execute the statement
114+
* Use result()/results() for insert queries
115+
*
116+
* @return bool
117+
*/
118+
public function execute()
119+
{
120+
return $this->statement->execute();
121+
}
122+
123+
/**
124+
* Return multiple rows
125+
*
126+
* @return array
127+
*/
128+
public function results()
129+
{
130+
$this->execute();
131+
return $this->statement->fetchAll(\PDO::FETCH_ASSOC);
132+
}
133+
134+
/**
135+
* Return a single row
136+
*
137+
* @return array
138+
*/
139+
public function result()
140+
{
141+
$this->execute();
142+
return $this->statement->fetch(\PDO::FETCH_ASSOC);
143+
}
144+
145+
/**
146+
* Return the row count
147+
*
148+
* @return int
149+
*/
150+
public function rowCount()
151+
{
152+
return $this->statement->rowCount();
153+
}
154+
155+
/**
156+
* Return if rows exists
157+
*
158+
* @return bool
159+
*/
160+
public function rowsExist()
161+
{
162+
return $this->rowCount() != 0;
163+
}
164+
165+
/**
166+
* Return the id of the last inserted row
167+
*
168+
* @return mixed
169+
*/
170+
public function lastInsertId()
171+
{
172+
return $this->database_handler->lastInsertId();
173+
}
174+
175+
/**
176+
* Begin a transaction for multiple statements
177+
*
178+
* @return bool
179+
*/
180+
public function beginTransaction()
181+
{
182+
return $this->database_handler->beginTransaction();
183+
}
184+
185+
/**
186+
* Commit the transaction for multiple statements
187+
*
188+
* @return bool
189+
*/
190+
public function endTransaction()
191+
{
192+
return $this->database_handler->commit();
193+
}
194+
195+
/**
196+
* Roll back the transaction
197+
*
198+
* @return bool
199+
*/
200+
public function cancelTransaction()
201+
{
202+
return $this->database_handler->rollBack();
203+
}
204+
205+
/**
206+
* Return the table name with prefix
207+
*
208+
* @param string $table_name The table name that's accessed
209+
* @return string
210+
*/
211+
public function getTableName($table_name)
212+
{
213+
return $this->table_prefix . $table_name;
214+
}
215+
216+
/**
217+
* Dump the statement's current parameters
218+
*
219+
* @return void
220+
*/
221+
public function dumpStatement()
222+
{
223+
$this->statement->debugDumpParams();
224+
}
225+
}

0 commit comments

Comments
 (0)