Skip to content

Commit d38f58d

Browse files
committed
added DB layer
1 parent ce2a1d2 commit d38f58d

File tree

8 files changed

+71
-19
lines changed

8 files changed

+71
-19
lines changed

bootstrap.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*
1212
*/
1313

14+
if (version_compare(phpversion(), '5.6.0', '<')) { die('PHP5.6+ Required! PHP7 Recommended.'); }
15+
1416
/**
1517
* Display errors (disable it on production)
1618
*/
@@ -49,6 +51,13 @@
4951
$cachedriver = new Stash\Driver\FileSystem(['path'=>$config->cache['stash']['cachedir']]);
5052
$cache = new Stash\Pool($cachedriver);
5153

54+
/**
55+
* Load Database
56+
* Create a new PDO connection to MySQL
57+
* Create a new static DB class object
58+
*/
59+
System\DB::$c = (new System\Database($config))->connect();
60+
5261
/**
5362
* Load System Language
5463
*/
@@ -60,10 +69,9 @@
6069
$response = new Zend\Diactoros\Response();
6170
$response = $response
6271
->withHeader('Content-Type', 'text/html')
63-
->withAddedHeader('X-Phreak-KEY', SITE_KEY)
72+
->withAddedHeader('X-Phreak-KEY', $config->system['site_key'])
6473
->withHeader('Cache-Control', 'private, max-age=3600, must-revalidate');
6574
$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
66-
6775
$requestURI=$request->getUri()->getPath();
6876

6977
/**
@@ -83,13 +91,15 @@
8391
// $session->set('requestURI',$requestURI);
8492

8593
/**
86-
* Load System Modules
94+
* Load System Modules and Routes
8795
*/
8896
$modules = $container->get('\System\Modules');
8997
$modules->loadRoutes($router);
9098

99+
/**
100+
* Cache Routes
101+
*/
91102
if ($config->system['cache_routes']) {
92-
// Cache the routes data
93103
$item = $cache->getItem('routes');
94104
$routesData = $item->get();
95105
if ($item->isMiss()) {

config/database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
'dbname'=>'phreak',
66
'username'=>'root',
77
'password'=>'',
8-
'charset'=>'utf-8'
8+
'charset'=>'utf8'
99
]
1010
];

config/session.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* SESSION CONFIG
4+
*/
5+
return [
6+
'use_only_cookies'=>true,
7+
'use_trans_sid'=>false,
8+
'cookie_httponly'=>true,
9+
'save_handler'=>'files',
10+
'gc_probability'=>true,
11+
'cookie_lifetime'=>'1800',
12+
'gc_maxlifetime'=>'1800',
13+
'save_path'=>sys_get_temp_dir()
14+
];

config/system.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
'max_upload_size'=>'52428800', //filesize in bytes also depends on server settings inside php.ini (1MB=1048576bytes)
88
'session_expire'=>'30', // session TTL (time to live) in minutes
99
'websocket_server'=>'ws://127.0.0.1:12345',
10-
'cache_routes'=>true,
10+
'cache_routes'=>true
1111
];

system/Config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public function __construct () {
99
$this->configfiles = [
1010
'system'=>ROOT_DIR.'/config/system.php',
1111
'cache'=>ROOT_DIR.'/config/cache.php',
12-
'database'=>ROOT_DIR.'/config/database.php'
12+
'database'=>ROOT_DIR.'/config/database.php',
13+
'session'=>ROOT_DIR.'/config/session.php'
1314
];
1415
$this->conf = [
1516
'site_url'=>BASE_URL,

system/Database.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace System;
3+
4+
class Database {
5+
public function __construct (Config $conf) {
6+
$this->conf = $conf;
7+
}
8+
9+
public function connect () {
10+
/** Create a new PDO connection to MySQL **/
11+
try {
12+
$pdo = new \PDO(
13+
'mysql:dbname='.$this->conf->database['mysql']['dbname'].';
14+
host='.$this->conf->database['mysql']['host'],
15+
$this->conf->database['mysql']['username'],
16+
$this->conf->database['mysql']['password'],
17+
[
18+
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES ".$this->conf->database['mysql']['charset'],
19+
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
20+
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
21+
]
22+
);
23+
$pdo->exec("SET CHARACTER SET ".$this->conf->database['mysql']['charset']);
24+
$pdo->exec("SET CHARACTER_SET_CONNECTION=".$this->conf->database['mysql']['charset']);
25+
$pdo->exec("SET SQL_MODE = ''");
26+
} catch (\PDOException $err) {
27+
die('Unable to connect to database: ' . $err->getMessage());
28+
}
29+
30+
return $pdo;
31+
}
32+
}

system/Language.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public function __construct (Config $conf) {
99
$this->conf = $conf;
1010
$this->default = 'en';
1111
$this->current = $this->default;
12+
// $langs=DB::fetch('SELECT * from `languages`');
1213
}
1314

1415
}

system/SessionHandler.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,19 @@ class SessionHandler extends \SessionHandler {
2121
/**
2222
* Constructor
2323
*/
24-
public function __construct() {
24+
public function __construct(Config $conf) {
2525
if (!extension_loaded('openssl')) {
2626
throw new \RuntimeException(sprintf("You need the OpenSSL extension to use %s", __class__));
2727
}
2828
if (!extension_loaded('mbstring')) {
2929
throw new \RuntimeException(sprintf("You need the Multibytes extension to use %s", __class__));
3030
}
3131
if (session_id() === '') {
32-
ini_set('session.use_only_cookies', 'On');
33-
ini_set('session.use_cookies', 'On');
34-
ini_set('session.use_trans_sid', 'Off');
35-
ini_set('session.cookie_httponly', 'On');
36-
ini_set('session.save_handler', 'files');
37-
ini_set('session.gc_probability', 'On');
38-
// ini_set('session.cookie_lifetime', '2592000'); // 1800 seconds = 30mins
39-
// ini_set('session.gc_maxlifetime', '2592000'); // 2592000 = 30 days
40-
$sessionPath = sys_get_temp_dir();
41-
ini_set('session.save_path', $sessionPath);
42-
session_save_path($sessionPath);
32+
// ini_set session config values
33+
foreach ($conf->session as $skey => $sval) {
34+
ini_set('session.'.$skey, $sval);
35+
}
36+
session_save_path($conf->session['save_path']);
4337
}
4438
}
4539

0 commit comments

Comments
 (0)