import v1.0.0-RC4 | 2009-05-20
This commit is contained in:
163
libs/Monkeys/Controller/Action.php
Executable file
163
libs/Monkeys/Controller/Action.php
Executable file
@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
|
||||
* @license http://creativecommons.org/licenses/BSD/ BSD License
|
||||
* @author Keyboard Monkey Ltd
|
||||
* @since CommunityID 0.9
|
||||
* @package CommunityID
|
||||
* @packager Keyboard Monkeys
|
||||
*/
|
||||
|
||||
abstract class Monkeys_Controller_Action extends Zend_Controller_Action
|
||||
{
|
||||
/**
|
||||
* not prepended with "_" because their view counterparts can't have "_" prepended
|
||||
*/
|
||||
protected $user;
|
||||
protected $targetUser;
|
||||
|
||||
protected $_config;
|
||||
protected $_numCols = 2;
|
||||
protected $underMaintenance = false;
|
||||
|
||||
public function init()
|
||||
{
|
||||
if (!Zend_Registry::isRegistered('user')) {
|
||||
// guest user
|
||||
$users = new Users();
|
||||
$user = $users->createRow();
|
||||
Zend_Registry::set('user', $user);
|
||||
}
|
||||
|
||||
$this->_config = Zend_Registry::get('config');
|
||||
|
||||
$this->user = Zend_Registry::get('user');
|
||||
$this->view->user = $this->user;
|
||||
|
||||
$this->_validateTargetUser();
|
||||
$this->_checkMaintenanceMode();
|
||||
|
||||
$this->view->controller = $this;
|
||||
|
||||
$this->view->addHelperPath('libs/Monkeys/View/Helper', 'Monkeys_View_Helper');
|
||||
$this->_setScriptPaths();
|
||||
$this->_setBase();
|
||||
$this->view->numCols = $this->_numCols;
|
||||
|
||||
if ($this->getRequest()->isXmlHttpRequest()) {
|
||||
$slowdown = $this->_config->environment->ajax_slowdown;
|
||||
if ($slowdown > 0) {
|
||||
sleep($slowdown);
|
||||
}
|
||||
$this->_helper->layout->disableLayout();
|
||||
} else {
|
||||
$this->view->version = Setup::VERSION;
|
||||
$this->view->messages = $this->_helper->FlashMessenger->getMessages();
|
||||
$this->view->loaderCombine = $this->_config->environment->YDN? 'true' : 'false';
|
||||
$this->view->loaderBase = $this->_config->environment->YDN?
|
||||
'http://yui.yahooapis.com/2.6.0/build/'
|
||||
: $this->view->base . '/javascript/yui/';
|
||||
}
|
||||
}
|
||||
|
||||
private function _setScriptPaths()
|
||||
{
|
||||
if (($template = $this->_config->environment->template) == 'default') {
|
||||
return;
|
||||
}
|
||||
|
||||
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
|
||||
$view = $viewRenderer->view;
|
||||
$scriptPaths = $view->getScriptPaths();
|
||||
$oldPath = $scriptPaths[0];
|
||||
$newPath = substr($oldPath, 0, strrpos($oldPath, DIRECTORY_SEPARATOR, -2) + 1) . "scripts_$template" . DIRECTORY_SEPARATOR;
|
||||
$view->addScriptPath($newPath);
|
||||
}
|
||||
|
||||
private function _setBase()
|
||||
{
|
||||
if ($this->_config->subdomain->enabled) {
|
||||
$protocol = $this->_getProtocol();
|
||||
|
||||
$this->view->base = "$protocol://"
|
||||
. ($this->_config->subdomain->use_www? 'www.' : '')
|
||||
. $this->_config->subdomain->hostname;
|
||||
} else {
|
||||
$this->view->base = $this->view->getBase();
|
||||
}
|
||||
}
|
||||
|
||||
private function _validateTargetUser()
|
||||
{
|
||||
if (Zend_Registry::isRegistered('targetUser')) {
|
||||
// used by unit tests to inject the target user
|
||||
$this->targetUser = Zend_Registry::get('targetUser');
|
||||
} else {
|
||||
$userId = $this->_getParam('userid');
|
||||
|
||||
if (is_null($userId)) {
|
||||
$this->targetUser = $this->user;
|
||||
} elseif ($this->_getParam('userid') == 0) {
|
||||
$users = new Users();
|
||||
$this->targetUser = $users->createRow();
|
||||
} else {
|
||||
if ($userId != $this->user->id && $this->user->role != User::ROLE_ADMIN) {
|
||||
$this->_helper->FlashMessenger->addMessage('Error: Invalid user id');
|
||||
$this->_redirect('profile/edit');
|
||||
}
|
||||
$users = new Users();
|
||||
$this->targetUser = $users->getRowInstance($userId);
|
||||
}
|
||||
}
|
||||
|
||||
$this->view->targetUser = $this->targetUser;
|
||||
}
|
||||
|
||||
protected function _checkMaintenanceMode()
|
||||
{
|
||||
if (!$this->_config->environment->installed) {
|
||||
$this->underMaintenance = true;
|
||||
$this->view->underMaintenance = false;
|
||||
return;
|
||||
}
|
||||
|
||||
$settings = new Settings();
|
||||
$this->underMaintenance = $settings->isMaintenanceMode();
|
||||
$this->view->underMaintenance = $this->underMaintenance;
|
||||
}
|
||||
|
||||
protected function _redirectToNormalConnection()
|
||||
{
|
||||
if ($this->_config->SSL->enable_mixed_mode) {
|
||||
$this->_redirect('http://' . $_SERVER['HTTP_HOST'] . $this->view->base);
|
||||
} else {
|
||||
$this->_redirect('');
|
||||
}
|
||||
}
|
||||
|
||||
protected function _redirectForMaintenance($backToNormalConnection = false)
|
||||
{
|
||||
if ($backToNormalConnection) {
|
||||
$this->_redirectToNormalConnection('');
|
||||
} else {
|
||||
$this->_redirect('');
|
||||
}
|
||||
}
|
||||
|
||||
protected function _redirect($url, $options = array())
|
||||
{
|
||||
Zend_Registry::get('logger')->log("redirected to '$url'", Zend_Log::DEBUG);
|
||||
|
||||
return parent::_redirect($url, $options);
|
||||
}
|
||||
|
||||
protected function _getProtocol()
|
||||
{
|
||||
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
|
||||
return 'https';
|
||||
} else {
|
||||
return 'http';
|
||||
}
|
||||
}
|
||||
}
|
118
libs/Monkeys/Controller/Error.php
Normal file
118
libs/Monkeys/Controller/Error.php
Normal file
@ -0,0 +1,118 @@
|
||||
<?
|
||||
|
||||
/*
|
||||
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
|
||||
* @license http://creativecommons.org/licenses/BSD/ BSD License
|
||||
* @author Keyboard Monkey Ltd
|
||||
* @since CommunityID 0.9
|
||||
* @package CommunityID
|
||||
* @packager Keyboard Monkeys
|
||||
*/
|
||||
|
||||
abstract class Monkeys_Controller_Error extends Monkeys_Controller_Action
|
||||
{
|
||||
protected $_numCols = 1;
|
||||
|
||||
public function errorAction()
|
||||
{
|
||||
$errors = $this->_getParam('error_handler');
|
||||
|
||||
if (!$this->_config->environment->production) {
|
||||
echo "<br /><br />";
|
||||
Zend_Debug::Dump($errors);
|
||||
}
|
||||
|
||||
$exceptionClass = get_class($errors->exception);
|
||||
|
||||
Zend_Registry::get('logger')->log(
|
||||
"Exception $exceptionClass\nMessage: ".$errors->exception->getMessage()."\nStack: \n" . print_r($errors->exception->getTraceAsString(), true),
|
||||
Zend_Log::ERR
|
||||
);
|
||||
|
||||
switch ($exceptionClass) {
|
||||
case 'Monkeys_BadUrlException';
|
||||
$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
|
||||
|
||||
$this->view->message = 'The URL you entered is incorrect. Please correct and try again.';
|
||||
break;
|
||||
case 'Monkeys_AccessDeniedException';
|
||||
$this->getResponse()->setRawHeader('HTTP/1.1 401 Unauthorized');
|
||||
$this->view->message = 'Access Denied - Maybe your session has expired? Try logging-in again.';
|
||||
break;
|
||||
default:
|
||||
$this->view->message = get_class($errors->exception) . '<br />' . $errors->exception->getMessage();
|
||||
if (!$this->_config->environment->production) {
|
||||
$this->view->trace = $errors->exception->getTraceAsString();
|
||||
} else if ($this->_config->email->adminemail) {
|
||||
$mail = self::getMail($errors->exception, $this->user, $errors);
|
||||
$mail->send();
|
||||
$this->view->message .= '<br />The system administrator has been notified.';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$this->getResponse()->clearBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Zend_Mail
|
||||
* @throws Zend_Mail_Protocol_Exception
|
||||
*/
|
||||
public static function getMail(Exception $ex, User $user, $errors)
|
||||
{
|
||||
$exceptionClass = get_class($ex);
|
||||
$stack = $ex->getTraceAsString();
|
||||
$stackDetail = print_r($errors, true);
|
||||
$currentUrl = Zend_OpenId::selfURL();
|
||||
if ($user->role = ROLE_GUEST) {
|
||||
$userLabel = 'Anonymous';
|
||||
} else {
|
||||
$userLabel = $user->getFullName() . '(' . $user->username . ')';
|
||||
}
|
||||
|
||||
$body = <<<EOD
|
||||
Dear Admin,
|
||||
|
||||
An error has occured in your Community-ID installation.
|
||||
|
||||
URL requested: $currentUrl
|
||||
|
||||
By User: $userLabel
|
||||
|
||||
Exception: $exceptionClass
|
||||
|
||||
Call stack:
|
||||
$stack
|
||||
|
||||
Call stack detail:
|
||||
$stackDetail
|
||||
EOD;
|
||||
|
||||
// can't use $this-_config 'cause it's a static function
|
||||
$configEmail = Zend_Registry::get('config')->email;
|
||||
|
||||
switch (strtolower($configEmail->transport)) {
|
||||
case 'smtp':
|
||||
Zend_Mail::setDefaultTransport(
|
||||
new Zend_Mail_Transport_Smtp(
|
||||
$configEmail->host,
|
||||
$configEmail->toArray()
|
||||
)
|
||||
);
|
||||
break;
|
||||
case 'mock':
|
||||
Zend_Mail::setDefaultTransport(new Zend_Mail_Transport_Mock());
|
||||
break;
|
||||
default:
|
||||
Zend_Mail::setDefaultTransport(new Zend_Mail_Transport_Sendmail());
|
||||
}
|
||||
|
||||
$mail = new Zend_Mail();
|
||||
$mail->setBodyText($body);
|
||||
$mail->setFrom('support@community-id.org');
|
||||
$mail->addTo($configEmail->adminemail);
|
||||
$mail->setSubject('Community-ID error report');
|
||||
|
||||
return $mail;
|
||||
}
|
||||
}
|
72
libs/Monkeys/Controller/Plugin/Auth.php
Executable file
72
libs/Monkeys/Controller/Plugin/Auth.php
Executable file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
|
||||
* @license http://creativecommons.org/licenses/BSD/ BSD License
|
||||
* @author Keyboard Monkey Ltd
|
||||
* @since CommunityID 0.9
|
||||
* @package CommunityID
|
||||
* @packager Keyboard Monkeys
|
||||
*/
|
||||
|
||||
class Monkeys_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
|
||||
{
|
||||
private $_acl;
|
||||
|
||||
public function __construct($acl)
|
||||
{
|
||||
$this->_acl = $acl;
|
||||
}
|
||||
|
||||
public function preDispatch($request)
|
||||
{
|
||||
if (!Zend_Registry::get('config')->environment->installed
|
||||
&& $request->getModuleName() != 'install'
|
||||
&& $request->getControllerName() != 'error')
|
||||
{
|
||||
$request->setModuleName('install');
|
||||
$request->setControllerName('index');
|
||||
$request->setActionName('index');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (Zend_Registry::isRegistered('user')) {
|
||||
// used by unit tests to inject the logged-in user
|
||||
$user= Zend_Registry::get('user');
|
||||
} else {
|
||||
$auth = Zend_Auth::getInstance();
|
||||
$users = new Users();
|
||||
if ($auth->hasIdentity()) {
|
||||
$user = $auth->getStorage()->read();
|
||||
$user->init();
|
||||
|
||||
// reactivate row as live data
|
||||
$user->setTable($users);
|
||||
} else {
|
||||
// guest user
|
||||
$user = $users->createRow();
|
||||
}
|
||||
|
||||
Zend_Registry::set('user', $user);
|
||||
}
|
||||
|
||||
$resource = $request->getModuleName() . '_' . $request->getControllerName();
|
||||
|
||||
if (!$this->_acl->has($resource)) {
|
||||
//echo "role: " . $user->role . " - resource: $resource - privilege: " . $request->getActionName() . "<br>\n";
|
||||
throw new Monkeys_BadUrlException($this->getRequest()->getRequestUri());
|
||||
}
|
||||
|
||||
// if an admin is not allowed for this action, then the action doesn't exist
|
||||
if (!$this->_acl->isAllowed(User::ROLE_ADMIN, $resource, $request->getActionName())) {
|
||||
//echo "role: " . $user->role . " - resource: $resource - privilege: " . $request->getActionName() . "<br>\n";
|
||||
throw new Monkeys_BadUrlException($this->getRequest()->getRequestUri());
|
||||
}
|
||||
|
||||
if (!$this->_acl->isAllowed($user->role, $resource, $request->getActionName())) {
|
||||
//echo "role: " . $user->role . " - resource: $resource - privilege: " . $request->getActionName() . "<br>\n";
|
||||
throw new Monkeys_AccessDeniedException();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user