Disable debugging output when redirecting after login
Disable debugging output when redirecting after login

<?php <?php
/** /**
* Controller for login * Controller for login
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2012 Christopher Han * @copyright Copyright (c) 2012 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
class GitPHP_Controller_Login extends GitPHP_ControllerBase class GitPHP_Controller_Login extends GitPHP_ControllerBase
{ {
/** /**
* Flag whether login was successful * Flag whether login was successful
* *
* @var boolean|null * @var boolean|null
*/ */
protected $loginSuccess = null; protected $loginSuccess = null;
   
/** /**
* Initialize controller * Initialize controller
*/ */
public function Initialize() public function Initialize()
{ {
$this->InitializeConfig(); $this->InitializeConfig();
   
$this->InitializeResource(); $this->InitializeResource();
   
$this->InitializeUserList(); $this->InitializeUserList();
   
$this->EnableLogging(); $this->EnableLogging();
   
$this->InitializeSmarty(); $this->InitializeSmarty();
} }
   
/** /**
* Gets the template for this controller * Gets the template for this controller
* *
* @return string template filename * @return string template filename
*/ */
protected function GetTemplate() protected function GetTemplate()
{ {
return 'login.tpl'; return 'login.tpl';
} }
   
/** /**
* Gets the cache key for this controller * Gets the cache key for this controller
* *
* @return string cache key * @return string cache key
*/ */
protected function GetCacheKey() protected function GetCacheKey()
{ {
$key = (isset($this->params['username']) ? $this->params['username'] : '') . '|' . (isset($this->params['password']) ? $this->params['password'] : ''); $key = (isset($this->params['username']) ? $this->params['username'] : '') . '|' . (isset($this->params['password']) ? $this->params['password'] : '');
if (!empty($key)) if (!empty($key))
$key = sha1($key); $key = sha1($key);
return $key; return $key;
} }
   
/** /**
* Gets the name of this controller's action * Gets the name of this controller's action
* *
* @param boolean $local true if caller wants the localized action name * @param boolean $local true if caller wants the localized action name
* @return string action name * @return string action name
*/ */
public function GetName($local = false) public function GetName($local = false)
{ {
if ($local && $this->resource) { if ($local && $this->resource) {
return $this->resource->translate('login'); return $this->resource->translate('login');
} }
return 'login'; return 'login';
} }
   
/** /**
* Loads headers for this template * Loads headers for this template
*/ */
protected function LoadHeaders() protected function LoadHeaders()
{ {
if (isset($this->params['output']) && ($this->params['output'] == 'js')) { if (isset($this->params['output']) && ($this->params['output'] == 'js')) {
$this->headers[] = 'Content-Type: application/json'; $this->headers[] = 'Content-Type: application/json';
$this->DisableLogging(); $this->DisableLogging();
} }
   
if (!empty($_SESSION['gitphpuser'])) { if (!empty($_SESSION['gitphpuser'])) {
$user = $this->userList->GetUser($_SESSION['gitphpuser']); $user = $this->userList->GetUser($_SESSION['gitphpuser']);
if ($user) { if ($user) {
if (!(isset($this->params['output']) && ($this->params['output'] == 'js'))) { if (!(isset($this->params['output']) && ($this->params['output'] == 'js'))) {
if (!empty($this->params['redirect'])) if (!empty($this->params['redirect']))
$this->headers[] = 'Location: ' . $this->params['redirect']; $this->headers[] = 'Location: ' . $this->params['redirect'];
else else
$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true); $this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
  $this->DisableLogging();
} }
$this->loginSuccess = true; $this->loginSuccess = true;
} else { } else {
unset($_SESSION['gitphpuser']); unset($_SESSION['gitphpuser']);
} }
} }
   
if (!(empty($this->params['username']) || empty($this->params['password']))) { if (!(empty($this->params['username']) || empty($this->params['password']))) {
$user = $this->userList->GetUser($this->params['username']); $user = $this->userList->GetUser($this->params['username']);
if ($user && ($this->params['password'] === $user->GetPassword())) { if ($user && ($this->params['password'] === $user->GetPassword())) {
$_SESSION['gitphpuser'] = $user->GetUsername(); $_SESSION['gitphpuser'] = $user->GetUsername();
if (!(isset($this->params['output']) && ($this->params['output'] == 'js'))) { if (!(isset($this->params['output']) && ($this->params['output'] == 'js'))) {
if (!empty($this->params['redirect'])) if (!empty($this->params['redirect']))
$this->headers[] = 'Location: ' . $this->params['redirect']; $this->headers[] = 'Location: ' . $this->params['redirect'];
else else
$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true); $this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
  $this->DisableLogging();
} }
$this->loginSuccess = true; $this->loginSuccess = true;
} else { } else {
$this->loginSuccess = false; $this->loginSuccess = false;
} }
} }
} }
   
/** /**
* Loads data for this template * Loads data for this template
*/ */
protected function LoadData() protected function LoadData()
{ {
if (!(isset($this->params['output']) && ($this->params['output'] == 'js'))) { if (!(isset($this->params['output']) && ($this->params['output'] == 'js'))) {
if ($this->loginSuccess === false) { if ($this->loginSuccess === false) {
if ($this->resource) { if ($this->resource) {
$this->tpl->assign('loginerror', $this->resource->translate('Invalid username or password')); $this->tpl->assign('loginerror', $this->resource->translate('Invalid username or password'));
} else { } else {
$this->tpl->assign('loginerror', 'Invalid username or password'); $this->tpl->assign('loginerror', 'Invalid username or password');
} }
} }
if (!empty($this->params['redirect'])) { if (!empty($this->params['redirect'])) {
$this->tpl->assign('redirect', $this->params['redirect']); $this->tpl->assign('redirect', $this->params['redirect']);
} else if (!empty($_SERVER['HTTP_REFERER'])) { } else if (!empty($_SERVER['HTTP_REFERER'])) {
$this->tpl->assign('redirect', $_SERVER['HTTP_REFERER']); $this->tpl->assign('redirect', $_SERVER['HTTP_REFERER']);
} }
} }
} }
   
/** /**
* Renders the output * Renders the output
*/ */
public function Render() public function Render()
{ {
if (isset($this->params['output']) && ($this->params['output'] == 'js')) { if (isset($this->params['output']) && ($this->params['output'] == 'js')) {
$result = array(); $result = array();
if ($this->loginSuccess === true) if ($this->loginSuccess === true)
$result['success'] = true; $result['success'] = true;
else { else {
$result['success'] = false; $result['success'] = false;
if ($this->loginSuccess === false) { if ($this->loginSuccess === false) {
if ($this->resource) { if ($this->resource) {
$result['message'] = $this->resource->translate('Invalid username or password'); $result['message'] = $this->resource->translate('Invalid username or password');
} else { } else {
$result['message'] = 'Invalid username or password'; $result['message'] = 'Invalid username or password';
} }
} }
} }
echo json_encode($result); echo json_encode($result);
return; return;
} }
   
if ($this->loginSuccess === true) if ($this->loginSuccess === true)
return; // logged in and redirected, don't render return; // logged in and redirected, don't render
   
return parent::Render(); return parent::Render();
} }
   
} }
   
comments