This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/core/auth.inc.php

305 lines
7.7 KiB
PHP
Raw Normal View History

<?php
/**
* AgileBill - Open Billing Software
*
* This body of work is free software; you can redistribute it and/or
* modify it under the terms of the Open AgileBill License
* License as published at http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
*
* Originally authored by Tony Landis, AgileBill LLC
*
* Recent modifications by Deon George
*
* @author Deon George <deonATleenooksDOTnet>
* @copyright 2009 Deon George
* @link http://osb.leenooks.net
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
2009-08-03 04:10:16 +00:00
* @subpackage Core
*/
2009-08-03 04:10:16 +00:00
/**
* The main AgileBill CORE Auth Class
*
* @package AgileBill
* @subpackage Core
*/
class CORE_auth {
private $account = 0;
private $logged = false;
private $group = array();
2009-08-03 04:10:16 +00:00
public function __construct($force) {
global $VAR;
if (defined('SESS_LOGGED')) {
if (SESS_LOGGED == '1') {
$this->logged = true;
$this->account = SESS_ACCOUNT;
}
2009-08-03 04:10:16 +00:00
} else {
if (! defined('SESS_LOGGED'))
define('SESS_LOGGED',false);
if (! defined('SESS'))
define('SESS',false);
}
2009-08-03 04:10:16 +00:00
if ($force && defined('FORCE_SESS_ACCOUNT')) {
$this->account = FORCE_SESS_ACCOUNT;
2009-08-03 04:10:16 +00:00
$this->logged = true;
}
2009-08-03 04:10:16 +00:00
$this->auth_update();
if (isset($VAR['_logout']) || isset($VAR['_login']) || isset($VAR['lid']) || $force == true || CACHE_SESSIONS != '1') {
return;
} else {
if ($this->session_auth_cache_retrieve()) {
$this->module_count = count($this->module);
return;
}
}
}
2009-08-03 04:10:16 +00:00
public function auth_update() {
$this->group = array('0');
2009-02-22 13:11:12 +00:00
$this->module = array('0');
2009-08-03 04:10:16 +00:00
if ($this->account) {
2009-02-22 13:11:12 +00:00
$this->group_list($this->account);
2009-08-03 04:10:16 +00:00
if (! $this->group)
return;
2009-08-03 04:10:16 +00:00
$db = &DB();
$p = AGILE_DB_PREFIX;
$sql = "
SELECT DISTINCT MM.module_id, GM.method_id, GM.group_id, M.name AS module_name, M.parent_id AS module_parent_id, M.menu_display AS module_display, MM.name AS method_name, MM.page AS method_page, MM.menu_display AS method_display
FROM {$p}group_method as GM
LEFT JOIN {$p}module as M on (GM.module_id=M.id and M.site_id=".DEFAULT_SITE.")
LEFT JOIN {$p}module_method as MM on (GM.method_id=MM.id and MM.site_id=".DEFAULT_SITE.") ";
for ($i=0; $i<count($this->group); $i++)
if ($i==0)
$sql .= sprintf('WHERE (GM.group_id=%s ',$this->group[$i]);
else
$sql .= sprintf('OR GM.group_id=%s ',$this->group[$i]);
$sql .= sprintf(') AND GM.site_id=%s ORDER BY M.name,MM.name',DEFAULT_SITE);
$result=$db->Execute($sql);
if ($result === false) {
global $C_debug;
2009-08-03 04:10:16 +00:00
$C_debug->error(__FILE__,__METHOD__,$db->ErrorMsg().'<br/>'.$q);
2009-02-22 13:11:12 +00:00
return;
}
2009-08-03 04:10:16 +00:00
while (!$result->EOF) {
2009-08-03 04:10:16 +00:00
$module_name = $result->fields['module_name'];
$method_name = $result->fields['method_name'];
2009-08-03 04:10:16 +00:00
if (empty($this->module[$module_name]))
$this->module[$module_name] = array($result->fields['module_id'],$result->fields['module_parent_id'],$result->fields['module_display']);
if (empty($this->module[$module_name][$method_name]))
$this->module[$module_name][$method_name] = array($result->fields['method_id'],$result->fields['method_display'],$result->fields['method_page']);
$result->MoveNext();
}
}
2009-08-03 04:10:16 +00:00
$this->session_auth_cache_update();
}
2009-08-03 04:10:16 +00:00
private function session_auth_cache_update() {
$db = &DB();
2009-08-03 04:10:16 +00:00
$expire = time()+7200; // 1 hour
if (isset($this->group) && is_array($this->group))
$group = serialize($this->group);
else
2009-08-03 04:10:16 +00:00
$group = 0;
2009-08-03 04:10:16 +00:00
if (isset($this->module) && is_array($this->module))
$module = serialize($this->module);
else
2009-08-03 04:10:16 +00:00
$module = 0;
2009-08-03 04:10:16 +00:00
$db->Execute(sqlDelete($db,'session_auth_cache',array('session_id'=>SESS)));
$db->Execute(sqlInsert($db,'session_auth_cache',array('session_id'=>SESS,'date_expire'=>$expire,'group_arr'=>$group,'module_arr'=>$module)));
}
2009-08-03 04:10:16 +00:00
private function session_auth_cache_retrieve() {
global $C_sess;
2009-08-03 04:10:16 +00:00
if (! empty($C_sess->auth_cache)) {
if ($C_sess->auth_cache['date_expire'] > time()) {
$group = $C_sess->auth_cache['group_arr'];
$module = $C_sess->auth_cache['module_arr'];
if ($group != '0' && $group != '')
$this->group = unserialize($group);
if ($module != '0' && $module != '')
$this->module = unserialize($module);
return true;
}
2009-08-03 04:10:16 +00:00
}
$db = &DB();
2009-08-03 04:10:16 +00:00
$result = $db->Execute(sqlSelect($db,'session_auth_cache','*',sprintf('session_id=::%s:: AND date_expire >= %s',SESS,time())));
if ($result->RecordCount() > 0) {
$group = $result->fields['group_arr'];
$module = $result->fields['module_arr'];
if ($group != '0' && $group != '')
$this->group = unserialize($group);
if ($module != '0' && $module != '')
$this->module = unserialize($module);
return true;
}
2009-08-03 04:10:16 +00:00
return false;
}
2009-08-03 04:10:16 +00:00
private function group_list($account) {
$this->group[0] = '0';
$time = time();
$db = &DB();
$p = AGILE_DB_PREFIX;
2009-08-03 04:10:16 +00:00
$q = "
SELECT DISTINCT ag.group_id AS group_id,g.parent_id AS parent_id
FROM {$p}account_group as ag
INNER JOIN {$p}group as g ON (ag.group_id=g.id AND g.status=1 AND g.site_id=".DEFAULT_SITE.")
WHERE ag.account_id = '$account'
AND (ag.date_start IS NULL OR ag.date_start < $time)
AND (ag.date_expire IS NULL OR ag.date_expire = 0 OR ag.date_expire > $time)
AND (g.date_start IS NULL OR g.date_start <= $time)
AND (g.date_expire IS NULL OR g.date_expire = 0 OR g.date_expire > $time)
AND ag.active=1 AND g.status=1
AND ag.site_id=".DEFAULT_SITE;
$result = $db->Execute($q);
if ($result === false) {
global $C_debug;
2009-08-03 04:10:16 +00:00
echo $db->ErrorMsg();
2009-08-03 04:10:16 +00:00
$C_debug->error(__FILE__,__METHOD__,$db->ErrorMsg());
exit;
2009-08-03 04:10:16 +00:00
} elseif ($result->RecordCount() == 0) {
return;
2009-08-03 04:10:16 +00:00
} else {
$arr = array();
while (!$result->EOF) {
2009-08-03 04:10:16 +00:00
array_push($arr,$result->fields);
$result->MoveNext();
}
}
2009-08-03 04:10:16 +00:00
for ($i=0; $i<count($arr); $i++) {
$do = true;
2009-08-03 04:10:16 +00:00
for ($ii=0; $ii<count($this->group); $ii++)
if ($this->group[$ii] == $arr[$i]['group_id'])
$do = false;
if ($do) {
array_push($this->group,$arr[$i]['group_id']);
2009-08-03 04:10:16 +00:00
if (! empty($arr[$i]['parent_id']) && $arr[$i]['parent_id'] != $arr[$i]['group_id']) {
$do = true;
2009-08-03 04:10:16 +00:00
for ($ii=0; $ii<count($this->group); $ii++)
if ($this->group[$ii] == $arr[$i]['parent_id'])
$do = false;
if ($do)
array_push($this->group,$arr[$i]['parent_id']);
}
}
2009-02-22 13:11:12 +00:00
}
2009-08-03 04:10:16 +00:00
if ($account != SESS_ACCOUNT)
return $this->group;
}
2009-08-03 04:10:16 +00:00
public function auth_method_by_name($module,$method) {
if (isset($this->module[$module][$method]))
return true;
2009-08-03 04:10:16 +00:00
if ($module == 'core')
if ($method == 'cleanup')
return true;
else
return false;
2009-08-03 04:10:16 +00:00
if (is_file(PATH_MODULES.$module.'/auth.inc.php')) {
include(PATH_MODULES.$module.'/auth.inc.php');
$this->auth_methods = $auth_methods;
for ($i=0; $i<count($this->auth_methods); $i++)
if ($module == @$this->auth_methods[$i]['module'])
2009-08-03 04:10:16 +00:00
if ($method == false || $method == @$this->auth_methods[$i]['method'])
return true;
2009-08-03 04:10:16 +00:00
}
return false;
}
2009-08-03 04:10:16 +00:00
public function auth_group_by_id($id) {
$ids = array();
if (! is_array($id))
array_push($ids,$id);
else
$ids = $id;
foreach ($ids as $group_id)
if (isset($this->group))
foreach ($this->group as $this_group_id)
2009-08-03 04:10:16 +00:00
if ($this_group_id == $group_id)
return true;
return false;
}
2009-08-03 04:10:16 +00:00
public function auth_group_by_account_id($account, $id) {
if (SESS_LOGGED == true && $account == SESS_ACCOUNT)
return $this->auth_group_by_id($id);
unset($this->group);
$this->group_list($account);
for ($i=0; $i<count($this->group); $i++)
if ($this->group[$i] == $id)
return true;
return false;
}
2009-08-03 04:10:16 +00:00
/**
* Generate the admin menu
*/
public function generate_admin_menu() {
include_once(PATH_CORE.'auth_generate_admin_menu.inc.php');
2009-08-03 04:10:16 +00:00
return auth_generate_admin_menu($this);
}
}
2009-08-03 04:10:16 +00:00
?>