Added flat file login capabilities and dynamic loading of local module class files.

git-svn-id: http://svn.cleancode.org/svn/pickles@127 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2009-06-07 04:53:18 +00:00
parent 2c3766df0a
commit 4444455d68
2 changed files with 48 additions and 22 deletions

View file

@ -60,31 +60,48 @@ class Security extends Object {
* landing page in the configuration as well.
*/
public function authenticate() {
$table = array(
'name' => 'users',
'fields' => array(
'id' => 'id',
'username' => 'username',
'password' => 'password'
)
);
$table = $this->config->getTableMapping('users', $table);
if (isset($this->config->admin, $this->config->admin->username, $this->config->admin->password)) {
if (isset($_SERVER['PHP_AUTH_USER'])) {
$from = '
FROM ' . $table['name'] . '
WHERE ' . $table['fields']['username'] . ' = "' . $_SERVER['PHP_AUTH_USER'] . '"
AND ' . $table['fields']['password'] . ' = "' . md5($_SERVER['PHP_AUTH_PW']) . '";
';
$this->db->execute('SELECT COUNT(' . $table['fields']['id'] . ') ' . $from);
if ($this->db->getField() != 0) {
$this->db->execute('SELECT ' . $table['fields']['id'] . ' ' . $from);
$_SESSION['user_id'] = $this->db->getField();
$_SESSION['user_id'] = null;
if (isset($_SERVER['PHP_AUTH_USER'])) {
if (
$_SERVER['PHP_AUTH_USER'] == $this->config->admin->username
&& $this->encrypt($this->config->admin->salt, $_SERVER['PHP_AUTH_PW']) == $this->config->admin->password
) {
$_SESSION['user_id'] = 1;
}
}
else {
$_SESSION['user_id'] = null;
}
else {
$table = array(
'name' => 'users',
'fields' => array(
'id' => 'id',
'username' => 'username',
'password' => 'password'
)
);
$table = $this->config->getTableMapping('users', $table);
if (isset($_SERVER['PHP_AUTH_USER'])) {
$from = '
FROM ' . $table['name'] . '
WHERE ' . $table['fields']['username'] . ' = "' . $_SERVER['PHP_AUTH_USER'] . '"
AND ' . $table['fields']['password'] . ' = "' . md5($_SERVER['PHP_AUTH_PW']) . '";
';
$this->db->execute('SELECT COUNT(' . $table['fields']['id'] . ') ' . $from);
if ($this->db->getField() != 0) {
$this->db->execute('SELECT ' . $table['fields']['id'] . ' ' . $from);
$_SESSION['user_id'] = $this->db->getField();
}
else {
$_SESSION['user_id'] = null;
}
}
}
@ -111,6 +128,10 @@ class Security extends Object {
session_destroy();
header('Location: /');
}
public function encrypt($salt, $string) {
return md5($salt . md5($salt . $string));
}
}
?>

View file

@ -67,6 +67,7 @@ function __autoload($class) {
$class_file = PICKLES_PATH . 'classes/' . $filename;
$module_file = PICKLES_PATH . 'common/modules/' . $filename;
$local_file = $_SERVER['DOCUMENT_ROOT'] . '/../modules/' . $filename;
// Loads the class file
if (file_exists($class_file)) {
@ -76,6 +77,10 @@ function __autoload($class) {
else if (file_exists($module_file)) {
return require_once $module_file;
}
// Loads the local module
else if (file_exists($local_file)) {
return require_once $local_file;
}
// Loads Smarty
else if ($class == 'Smarty') {
return require_once 'contrib/smarty/libs/Smarty.class.php';