Commit 162e9384 authored by Jason Frisvold's avatar Jason Frisvold
Browse files

- Change the database object into a slim singleton

parent 9901fd59
...@@ -16,14 +16,14 @@ ...@@ -16,14 +16,14 @@
#* along with this program; if not, write to the Free Software *# #* along with this program; if not, write to the Free Software *#
#* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
$isauthenticated = function( $role = 'user' ) { $isauthenticated = function( $role = 'user' ) use ($app) {
return function() use ( $role ) { return function() use ( $app, $role ) {
// Globalize the phptodo variables needed // Globalize the phptodo variables needed
global $skynet_sessTime, $skynet_serveruri, $smarty_data, $sqlhdlr; global $skynet_sessTime, $skynet_serveruri, $smarty_data;
// Try and get the id, last time, and user id from the sessions // Try and get the id, last time, and user id from the sessions
// database // database
$result = $sqlhdlr->table('sessions') $result = $app->db->table('sessions')
->select('id', 'last', 'user_id') ->select('id', 'last', 'user_id')
->where('phpsessid', '=', session_id()) ->where('phpsessid', '=', session_id())
->first(); ->first();
...@@ -36,14 +36,14 @@ ...@@ -36,14 +36,14 @@
// Check to see if an id was set, and if the time is valid // Check to see if an id was set, and if the time is valid
if ((isset($id)) && (($last + $skynet_sessTime) >= time())) { if ((isset($id)) && (($last + $skynet_sessTime) >= time())) {
// Good session, update the timestamp // Good session, update the timestamp
$sqlhdlr->table('sessions') $app->db->table('sessions')
->where('id', '=', $id) ->where('id', '=', $id)
->update(array('last' => time())); ->update(array('last' => time()));
// Create the user object // Create the user object
$user_obj = new skynetUser($sqlhdlr, $user_id); $user_obj = new skynetUser($app->db, $user_id);
// Assign the appropriate data to the smarty $smarty_data array // Assign the appropriate data to the smarty array
$smarty_data['username'] = $user_obj->username(); $smarty_data['username'] = $user_obj->username();
$smarty_data['adminflag'] = $user_obj->adminflag(); $smarty_data['adminflag'] = $user_obj->adminflag();
...@@ -51,8 +51,8 @@ ...@@ -51,8 +51,8 @@
$smarty_data['fullname'] = $smarty_data['fullname'] =
stripslashes(htmlentities($user_obj->fullname(), ENT_QUOTES)); stripslashes(htmlentities($user_obj->fullname(), ENT_QUOTES));
} else { } else {
$smarty_data['fullname'] = htmlentities($user_obj->fullname(), $smarty_data['fullname'] =
ENT_QUOTES); htmlentities($user_obj->fullname(),ENT_QUOTES);
} }
return($user_obj); return($user_obj);
......
...@@ -24,10 +24,10 @@ session_start(); ...@@ -24,10 +24,10 @@ session_start();
function login($username, $password) { function login($username, $password) {
// Globalize the phptodo variables needed // Globalize the phptodo variables needed
global $skynet_sessTime, $sqlhdlr; global $skynet_sessTime, $app;
// Create user class // Create user class
$user_obj = new skynetUser($sqlhdlr, -1, $username, $password, true); $user_obj = new skynetUser($app->db, -1, $username, $password, true);
if ($user_obj->logged_in()) { if ($user_obj->logged_in()) {
// Regenerate the session ID (security enhancement) // Regenerate the session ID (security enhancement)
...@@ -35,7 +35,7 @@ function login($username, $password) { ...@@ -35,7 +35,7 @@ function login($username, $password) {
// Insert the user_id into the sessions database along with the // Insert the user_id into the sessions database along with the
// session ID and the current time // session ID and the current time
$sqlhdlr->table('sessions') $app->db->table('sessions')
->insert(array( ->insert(array(
'phpsessid' => session_id(), 'phpsessid' => session_id(),
'user_id' => $user_obj->user_id(), 'user_id' => $user_obj->user_id(),
...@@ -44,7 +44,7 @@ function login($username, $password) { ...@@ -44,7 +44,7 @@ function login($username, $password) {
// Clean up any old sessions that have timed out // Clean up any old sessions that have timed out
$sqlhdlr->table('sessions') $app->db->table('sessions')
->where('last', '<', time() - $skynet_sessTime) ->where('last', '<', time() - $skynet_sessTime)
->delete(); ->delete();
...@@ -62,10 +62,10 @@ function login($username, $password) { ...@@ -62,10 +62,10 @@ function login($username, $password) {
function authenticate() { function authenticate() {
// Globalize the phptodo variables needed // Globalize the phptodo variables needed
global $skynet_sessTime, $smarty_data, $sqlhdlr; global $skynet_sessTime, $app, $smarty_data;
// Try and get the id, last time, and user if from the sessions database // Try and get the id, last time, and user if from the sessions database
$results = $sqlhdlr->table('sessions') $results = $app->db->table('sessions')
->select('id', 'last', 'user_id') ->select('id', 'last', 'user_id')
->where('phpsessid', session_id()) ->where('phpsessid', session_id())
->first(); ->first();
...@@ -77,14 +77,14 @@ function authenticate() { ...@@ -77,14 +77,14 @@ function authenticate() {
// Check to see if an id was set, and if the time is good // Check to see if an id was set, and if the time is good
if ((isset($id)) && (($last + $skynet_sessTime) >= time())) { if ((isset($id)) && (($last + $skynet_sessTime) >= time())) {
// Good session, update the timestamp // Good session, update the timestamp
$sqlhdlr->table('sessions') $app->db->table('sessions')
->where('id', $id) ->where('id', $id)
->update(array( ->update(array(
'last', time() 'last', time()
)); ));
// Create the user object // Create the user object
$user_obj = new skynetUser($sqlhdlr, $user_id); $user_obj = new skynetUser($app->db, $user_id);
// Assign the username to the smarty template // Assign the username to the smarty template
$smarty_data['username'] = $user_obj->username(); $smarty_data['username'] = $user_obj->username();
...@@ -93,8 +93,8 @@ function authenticate() { ...@@ -93,8 +93,8 @@ function authenticate() {
$smarty_data['fullname'] = $smarty_data['fullname'] =
stripslashes(htmlentities($user_obj->fullname(), ENT_QUOTES)); stripslashes(htmlentities($user_obj->fullname(), ENT_QUOTES));
} else { } else {
$smarty_data['fullname'] = htmlentities($user_obj->fullname(), $smarty_data['fullname'] =
ENT_QUOTES); htmlentities($user_obj->fullname(), ENT_QUOTES);
} }
return($user_obj); return($user_obj);
...@@ -110,7 +110,7 @@ function authenticate() { ...@@ -110,7 +110,7 @@ function authenticate() {
function feed_authenticate($user_id, $feed_id, $secret) { function feed_authenticate($user_id, $feed_id, $secret) {
// Globalize the phptodo variables needed // Globalize the phptodo variables needed
global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName, global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName,
$skynet_sessTime, $sqlhdlr; $skynet_sessTime, $app;
// Create the skynetFeed object // Create the skynetFeed object
$feed = new skynetFeed($skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $feed = new skynetFeed($skynet_dbHost, $skynet_dbUser, $skynet_dbPass,
...@@ -118,7 +118,7 @@ function feed_authenticate($user_id, $feed_id, $secret) { ...@@ -118,7 +118,7 @@ function feed_authenticate($user_id, $feed_id, $secret) {
if ($feed->secret() == $secret) { if ($feed->secret() == $secret) {
// Create the user object // Create the user object
$user_obj = new skynetUser($sqlhdlr, $user_id); $user_obj = new skynetUser($app->db, $user_id);
return Array($feed, $user_obj); return Array($feed, $user_obj);
} else { } else {
...@@ -131,10 +131,10 @@ function feed_authenticate($user_id, $feed_id, $secret) { ...@@ -131,10 +131,10 @@ function feed_authenticate($user_id, $feed_id, $secret) {
} }
function logout() { function logout() {
global $sqlhdlr; global $app;
// Try and get the id, last time, and user if from the sessions database // Try and get the id, last time, and user if from the sessions database
$sqlhdlr->table('sessions') $app->db->table('sessions')
->where('phpsessid', session_id()) ->where('phpsessid', session_id())
->delete(); ->delete();
......
...@@ -65,7 +65,7 @@ class skynetUser { ...@@ -65,7 +65,7 @@ class skynetUser {
// Store the database handler // Store the database handler
$this->sqlhdlr = $sqlhdlr; $this->sqlhdlr = $sqlhdlr;
// Everyone starts with a clean slate // Everyone starts with a clean slate
$this->dirty_flag = false; $this->dirty_flag = false;
$this->pwd_dirty_flag = false; $this->pwd_dirty_flag = false;
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
// Reports page // Reports page
$app->get('/reports', $isauthenticated(), function () use ($app) { $app->get('/reports', $isauthenticated(), function () use ($app) {
global $smarty_data; global $smarty_data;
prep_smarty($app); prep_smarty($app);
$app->render('reports.tpl', $smarty_data); $app->render('reports.tpl', $smarty_data);
......
...@@ -27,31 +27,8 @@ ...@@ -27,31 +27,8 @@
// Load ORM // Load ORM
use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Capsule\Manager as Capsule;
// Initiate ORM instance
$sqlhdlr = new Capsule;
$sqlhdlr->addConnection([
'driver' => 'mysql',
'host' => $skynet_dbHost,
'database' => $skynet_dbName,
'username' => $skynet_dbUser,
'password' => $skynet_dbPass,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
// Set the event dispatcher used by Eloquent models...
use Illuminate\Events\Dispatcher; use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container; use Illuminate\Container\Container;
$sqlhdlr->setEventDispatcher(new Dispatcher(new Container));
// Make this Capsule instance available globally via static methods...
$sqlhdlr->setAsGlobal();
// Setup the Eloquent ORM...
$sqlhdlr->bootEloquent();
// Global array for Smarty data // Global array for Smarty data
$smarty_data = array( $smarty_data = array(
...@@ -87,6 +64,36 @@ ...@@ -87,6 +64,36 @@
$app->view()->appendData(array('baseUrl' => '/index.php/')); $app->view()->appendData(array('baseUrl' => '/index.php/'));
}); });
// Register the DB handler as a framework singleton
$app->container->singleton('db', function () use ($app) {
global $skynet_dbHost, $skynet_dbName, $skynet_dbUser, $skynet_dbPass;
// Initiate ORM instance
$sqlhdlr = new Capsule;
$sqlhdlr->addConnection([
'driver' => 'mysql',
'host' => $skynet_dbHost,
'database' => $skynet_dbName,
'username' => $skynet_dbUser,
'password' => $skynet_dbPass,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
// Set the event dispatcher used by Eloquent models...
$sqlhdlr->setEventDispatcher(new Dispatcher(new Container));
// Make this Capsule instance available globally via static methods...
$sqlhdlr->setAsGlobal();
// Setup the Eloquent ORM...
$sqlhdlr->bootEloquent();
return $sqlhdlr;
});
// Load the authentication routes // Load the authentication routes
include('../app/authentication.php'); include('../app/authentication.php');
...@@ -95,21 +102,21 @@ ...@@ -95,21 +102,21 @@
// Home Page Route // Home Page Route
$app->get('/', $isauthenticated(), function () use ($app) { $app->get('/', $isauthenticated(), function () use ($app) {
global $smarty_data, $sqlhdlr; global $smarty_data;
$smarty_data['total_servers'] = $sqlhdlr->table('cloud') $smarty_data['total_servers'] = $app->db->table('cloud')
->where('disabled', 0) ->where('disabled', 0)
->count(); ->count();
$smarty_data['total_scanners'] = $sqlhdlr->table('spawn') $smarty_data['total_scanners'] = $app->db->table('spawn')
->where('disabled', 0) ->where('disabled', 0)
->count(); ->count();
$smarty_data['total_targets'] = $sqlhdlr->table('target') $smarty_data['total_targets'] = $app->db->table('target')
->where('disabled', 0) ->where('disabled', 0)
->count(); ->count();
$smarty_data['total_timers'] = $sqlhdlr->table('timers') $smarty_data['total_timers'] = $app->db->table('timers')
->where('disabled', 0) ->where('disabled', 0)
->count(); ->count();
$smarty_data['total_results'] = $sqlhdlr->table('results') $smarty_data['total_results'] = $app->db->table('results')
->count(); ->count();
prep_smarty($app); prep_smarty($app);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment