<?
include 'amodules3/loader.php';
class DBForm extends Form {
}
class FormAddUser extends Text{
function init(){
parent::init();
$this->set("<right><input type=Button onclick=\"document.location='".
$this->api->getDestinationURL('AddUser')."'\" value=\"Add new user\"></right>");
}
}
class UserList extends Grid {
function init(){
parent::init();
$this
->addColumn('expander','email','E-mail')->makeSortable()
->addColumn('password','clear','Password')
->addColumn('text','name','Full Name')->makeSortable()
->addColumn('text','relocated_to','Relocated To')
->addColumn('text','forward_to','Forward To')
->addColumn('text','cc_to','Send Copy To')
->setSource('users');
if($this->api->getUserLevel() == 99){
$this->addColumn('access', 'access_level', 'Level')
->addColumn('text', 'domains', 'Trusted domains');
}
if($this->api->getUserLevel() == 99){
$this->add('FormAddUser', null, 'grid_menu');
}
/*
* preventing user from seeing anyone else
*/
if($this->api->getUserLevel() == 0)$this->dq
->where('email', $this->api->auth->auth_data['name']);
$p=$this->add('Paginator',null,'paginator')
->useDQ($this->dq);
if($this->api->getUserLevel() < 99)$this->setDomains();
}
function format_access($field){
switch($this->current_row[$field]){
case "0":
return $this->current_row[$field] = "Self only";
case "9":
return $this->current_row[$field] = "Maintain";
case "99":
return $this->current_row[$field] = "Admin";
default:
return $this->current_row[$field] = "Who is there? Get out!!!";
}
}
function format_password($field){
$this->current_row[$field] = '***';
}
/**
* Sets a filter for user by trusted domains
*/
function setDomains(){
$domains = split(';', $this->api->getUserDomains());
$where = "";
foreach($domains as $domain){
if($where != "")$where .= ' or ';
$where .= "email like '%$domain%'";
}
$this->dq->where($where);
}
}
class UserEditForm extends Form {
function init(){
parent::init();
$this
->addField('line', 'email', 'E-Mail')
->addField('password', 'clear', 'Password')
->addField('line', 'name', 'Full Name')
->addField('line', 'relocated_to', 'Relocate to')
->addField('line', 'forward_to', 'Forward to')
->addField('line', 'cc_to', 'Send copy to')
;
if($this->api->getUserLevel() == 99){
$this
->addField('text', 'domains', 'Trusted domains')
->addField('dropdown', 'access_level', 'Access Level')
->setValueList(array(0=>'Self only', 9=>'Maintain', 99=>'Admin'))
;
}
$this->addSubmit('Save');
if($this->api->getUserLevel() == 99&&$_GET['id']!=''){
$this->addSubmit('Delete');
}
$this
->setSource('users')
->addConditionFromGET('id');
}
function submitted(){
if(!parent::submitted())return false;
if($this->isClicked('Save')){
//setting additional fields
$this->dq->set('relocated', $this->get('relocated_to')?'Y':'N');
$this->dq->set('forward', $this->get('forward_to')||$this->get('cc_to')?'Y':'N');
if(!$this->update())throw new BaseException("Cannot save record");
}elseif($this->isClicked('Delete')){
if(!$this->dq->do_delete())throw new BaseException("Cannot delete record");
}else return false;
$this->api->redirect('UserManagement');
}
}
class MailFilterForm extends Filter {
public $domain;
function init(){
parent::init();
$this
->addField('dropdown','Domain');
$this->domain = $this->last_field;
$this->setValueList(array(''=>'All')+$this->getDomains())
->addField('Line','QuickSearch')
->addSubmit('Show')
->addSubmit('Clear');
}
function getDomains(){
return $this->api->db->getAssoc("select distinct substring(email, locate('@', email)+1)," .
"substring(email, locate('@', email)+1) email from users order by email");
}
function applyDQ($dq){
if($this->get('Domain'))$dq->where("email like",'%@'.$this->get('Domain').'%');
if($this->get('QuickSearch'))$dq->where("email like",'%'.$this->get('QuickSearch').'%');
}
}
class ApiMailSql extends ApiAdmin {
public $auth;
public $logger;
public $apinfo=array(
'version'=>'0.96',
'name'=>'MailSql Admin'
);
function init(){
$this->readConfig('config.php');
parent::init();
$this->logger = $this->add('Logger');
$this->api->debug = defined('DEBUG');
$this->dbConnect();
$this->api->add('VersionControl');
$this->template->trySet('page_title', $this->apinfo['name']);
$this->auth = $this->api->add('Auth')->setNoCrypt();
$this->auth->setSource('users', 'email', 'clear')->dq
->field('id')
->field('access_level')
->field('domains');
/*
* trying to authorize as a user from email list
*/
$this->template->del('Content');
$this->template->del('Locator');
$this->template->del('msgbox');
$this->template->del('RightSidebar');
$this->template->del('InfoWindow');
}
function layout_Menu(){
if(!$this->isAuthenticated())$this->template->del('Menu');
else{
$menu = $this->add('Menu', null, 'Menu');
$menu
->addMenuItem('User Management')
//->addMenuItem('Postfix Configuration')
->addMenuItem('About')
->addMenuItem('Logout')
;
}
}
function page_Index(){
if($this->isAuthenticated())$this->redirect('UserManagement');
}
function page_Logout(){
$this->auth->logout();
}
function page_UserManagement($p){
if($this->isAuthenticated()){
if($this->getUserLevel() > 0){
$filter = $this->frame('Content','Quick Search')
->add('MailFilterForm',null,'content');
$userlist = $this->add('UserList', null, 'Content');
$filter->useDQ($userlist->dq);
}else{
$_GET['id'] = $this->auth->auth_data['id'];
$this->frame('Content', 'Your account data')->add('UserEditForm', null, 'content');
//->addCondition('id', $this->auth->auth_data['id']);
}
}
}
function page_PostfixConfiguration($p){
$p->add('NotImplemented', null, 'Content');
}
function addEditForm($p){
$this->frame('Content', 'User data', $p)->add('UserEditForm', null, 'content');
}
function page_UserManagement_email($p){
$this->addEditForm($p);
}
function page_UserManagement_clear($p){
$this->addEditForm($p);
}
function page_UserManagement_name($p){
$this->addEditForm($p);
}
function page_AddUser($p){
$this->frame('Content', 'New mail user')
->add('UserEditForm', null, 'content');
}
function getUserLevel(){
return $this->auth->auth_data['access_level'];
}
function getUserDomains(){
return $this->auth->auth_data['domains'] == "" ? "none" : $this->auth->auth_data['domains'];
}
function isAuthenticated(){
return $this->auth->auth_data['authenticated'];
}
}
$api = new ApiMailSql('MailSQL');
//$api->info('test');
$api->main();