Skip to content

Instantly share code, notes, and snippets.

@henrikbjorn
Created March 6, 2009 15:03
Show Gist options
  • Save henrikbjorn/74933 to your computer and use it in GitHub Desktop.
Save henrikbjorn/74933 to your computer and use it in GitHub Desktop.
<?php
class AppController extends Controller {
/**
* For controller, does magic
*
* @var array
*/
var $components = array('DebugKit.Toolbar', 'Session', 'Auth', 'Cookie', 'RequestHandler');
/**
* For views
*
* @var array
*/
var $helpers = array('Html', 'Rss', 'Text', 'Javascript', 'Time', 'Form', 'Session', 'Menu', 'Gaming', 'Auth');
/**
* Names of actions that a logged in user cant see
*
* @var array
*/
var $unloggedActions = array();
/**
* Sets up standard auth settings and application wide configuration
* such as subdomain and other varius stuff.
*
* @author Henrik
*/
public function beforeFilter()
{
/*
TODO Check for the unauthorized message and the level not high enough
* messages and redirect to the right help pages, workaround for extended
* functionality in the Auth component
*/
/*
TODO Need to be a bit more flexible with bans termination deletion etc
mayb use bitwise codes for this.
also the active stuff should be in the status field aswell.
User::STATUS_DELETED User::STATUS_BANNED
*/
//$this->Auth->userScope = array('User.active' => 1, 'User.status & ? = ?' => array(User::STATUS_BANNED, 0));
$this->Auth->userScope = array('User.active' => 1);
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
$this->Auth->autoRedirect = false;
/*
TODO Create a cookie login thingy
*/
if ($this->Auth->user() && in_array($this->action, $this->unloggedActions)) {
$this->redirect('/');
}
$sections = ClassRegistry::init('Section')->find('all', array('contain' => false));
array_unshift($sections, array('Section' => Section::$gaming));
Configure::write('Section', Section::getFromSections($sections));
Configure::write('Sections', $sections);
if ($this->Auth->user()) {
$auth = $this->Auth->user();
Configure::write('User', $auth['User']);
$settings = ClassRegistry::init('Setting')->findByUserId($this->Auth->user('id'), array('contain' => false));
Configure::write('User.Setting', $settings['Setting']);
}
$this->_OnlineCount();
}
function _OnlineCount() {
$folder = new Folder;
$folder->path = realpath(APP . 'tmp/sessions');
$files = $folder->read(false, true);
$inArray = array();
foreach($files[1] as $file) {
$inArray[] = substr($file, 5);
}
if (count($inArray)) {
$result = ClassRegistry::init('User')->find('all', array(
'contain' => false,
'fields' => 'COUNT(id) as count',
'conditions' => array(
'User.session_id' => $inArray,
),
));
} else {
$result[0][0]['count'] = 0;
}
Configure::write('User.Online.All', count($files[1]));
Configure::write('User.Online.Guests', (count($files[1]) - $result[0][0]['count']));
Configure::write('User.Online.Users', count($files[1]) - Configure::read('User.Online.Guests'));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment