Commit a0cf8bf9 authored by Jason Frisvold's avatar Jason Frisvold
Browse files

- Started to convert index to slim

parent 8cf21970
......@@ -19,12 +19,157 @@
// Define skynet (to allow access to files)
define('skynet', 1);
// Define usesmarty (to indicate that config needs to load smarty)
define('usesmarty', 1);
// Load framework
require 'vendor/autoload.php';
// Load the configuration file
require_once("config.php");
// Globals
$data = array(
'skynet_favicon' => $skynet_favicon,
'skynet_stylesheet' => $skynet_stylesheet,
'skynet_homepage' => $skynet_homepage,
'skynet_version' => $skynet_version,
);
// Connect to the database server
$sqlhdlr = new mysqli($skynet_dbHost, $skynet_dbUser, $skynet_dbPass,
$skynet_dbName);
// Make sure we have a connection
if (mysqli_connect_errno()) {
die( '<p>Unable to connect to the database server at this time: ' .
mysqli_connect_error() . ' </p>' );
}
function prep_smarty($app) {
$smarty = $app->view()->getInstance();
$smarty->loadFilter("pre", 'whitespace_control');
$smarty->debugging = true;
}
$app = new \Slim\Slim(array(
'view' => new \Slim\Views\Smarty(),
'debug' => true,
'log.enable' => true,
'log.path' => 'logs/',
'log.level' => 4,
'mode' => 'development'
));
$view = $app->view();
$view->parserDirectory = $skynet_smarty_dir;
$view->parserCompileDirectory = "$skynet_work_dir/templates_c";
$view->parserCacheDirectory = "$skynet_work_dir/cache";
$view->parserExtensions = array(
dirname(__FILE__) . '/libs',
);
$authenticateForRole = function ( $role = 'member' ) {
return function () use ( $role ) {
$user = User::fetchFromDatabaseSomehow();
if ( $user->belongsToRole($role) === false ) {
$app = \Slim\Slim::getInstance();
$app->flash('error', 'Login required');
$app->redirect('/login');
}
};
};
$isauthenticated = function() {
return function() {
// Globalize the phptodo variables needed
global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName,
$skynet_sessTime, $sqlhdlr, $data;
// Globalize the user variables
//global $smarty;
// Try and get the id, last time, and user if from the sessions database
$query = sprintf('SELECT id, last, user_id FROM sessions WHERE ' .
'phpsessid = "%s"', session_id());
if ($result = $sqlhdlr->query($query)) {
list($id, $last, $user_id) = $result->fetch_array();
$result->close();
} else {
die( 'Error: ' . $sqlhdlr->error );
}
// Check to see if an id was set, and if the time is good
if ((isset($id)) && (($last + $skynet_sessTime) >= time())) {
// Good session, update the timestamp
$query = sprintf("UPDATE sessions SET last = %d WHERE id = %d", time(),
$id);
$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
// Create the user object
$user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
$skynet_dbPass, $skynet_dbName, $user_id);
$data['username'] = $user_obj->username();
$data['adminflag'] = $user_obj->adminflag();
// Assign the username to the smarty template
//$smarty->assign('username', $user_obj->username());
//$smarty->assign('adminflag', $user_obj->adminflag());
if (get_magic_quotes_gpc()) {
$data['fullname'] = stripslashes(htmlentities($user_obj->fullname(),
ENT_QUOTES));
// $smarty->assign('fullname',
// stripslashes(htmlentities($user_obj->fullname(),
// ENT_QUOTES)));
} else {
$data['fullname'] = htmlentities($user_obj->fullname(),
ENT_QUOTES);
// $smarty->assign('fullname', htmlentities($user_obj->fullname(),
// ENT_QUOTES));
}
return($user_obj);
} else {
// Return a 0 to indicate that authentication was not successful
$app = \Slim\Slim::getInstance();
$app->flash('error', 'Login required');
//$app->redirect('/login');
}
// Return a 0 to indicate that authentication was not successful
$app = \Slim\Slim::getInstance();
$app->flash('error', 'Login required');
//$app->redirect('/login');
};
};
// GET route
$app->get('/', $isauthenticated(), function () use ($app) {
global $data;
prep_smarty($app);
print_r($data);
$app->render('index.tpl', $data);
});
// GET route
$app->post('/', function () use ($app) {
global $data;
prep_smarty($app);
$app->render('index.tpl', $data);
});
$app->run();
// Define usesmarty (to indicate that config needs to load smarty)
define('usesmarty', 1);
// Connect to the database server
$sqlhdlr = new mysqli($skynet_dbHost, $skynet_dbUser, $skynet_dbPass,
$skynet_dbName);
......@@ -70,7 +215,7 @@
}
// Display the template
$smarty->display('index.tpl');
//$smarty->display('index.tpl');
}
$sqlhdlr->close();
......
......@@ -20,7 +20,7 @@ $view->parserExtensions = array(
);
// GET route
$app->get('/', function () use ($app) {
$app->map('/', function () use ($app) {
$smarty = $app->view()->getInstance();
$smarty->loadFilter("pre", 'whitespace_control');
......@@ -34,10 +34,10 @@ $app->get('/', function () use ($app) {
$app->render('index.tpl', $data);
});
})->via('GET', 'POST');
// GET route
$app->get('/about', function () use ($app) {
$app->map('/about', function () use ($app) {
$smarty = $app->view()->getInstance();
$smarty->loadFilter("pre", 'whitespace_control');
$data = array(
......@@ -48,7 +48,7 @@ $app->get('/about', function () use ($app) {
'username' => 'guest',
);
$app->render('about.tpl', $data);
});
})->via('GET', 'POST');
// GET route
$app->get('/another', function () use ($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