Start adding basic login infrastructure (still needs styling)
Start adding basic login infrastructure (still needs styling)

--- a/css/gitphp.css
+++ b/css/gitphp.css
@@ -153,6 +153,13 @@
 	display: none;
 }
 
+/*
+ * login link
+ */
+div.login {
+	float: right;
+}
+
 
 /*
  * Geshi styles

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -201,6 +201,10 @@
 	{
 		$this->userList = new GitPHP_UserList();
 		$this->userList->LoadUsers(GITPHP_CONFIGDIR . 'users.conf.php');
+		if ($this->userList->GetCount() > 0) {
+			if (!isset($_SESSION))
+				session_start();
+		}
 	}
 
 	/**
@@ -562,6 +566,16 @@
 		$this->tpl->assign('requestvars', $getvarsmapped);
 
 		$this->tpl->assign('snapshotformats', GitPHP_Archive::SupportedFormats());
+
+		if ($this->userList && ($this->userList->GetCount() > 0)) {
+			$this->tpl->assign('loginenabled', true);
+			if (!empty($_SESSION['gitphpuser'])) {
+				$user = $this->userList->GetUser($_SESSION['gitphpuser']);
+				if ($user) {
+					$this->tpl->assign('loggedinuser', $user->GetUsername());
+				}
+			}
+		}
 	}
 
 	/**
@@ -617,7 +631,7 @@
 
 		$this->tpl->clearAllAssign();
 
-		if ($this->log)
+		if ($this->log && $this->projectList)
 			$this->log->Log('MemoryCache count: ' . $this->projectList->GetMemoryCache()->GetCount());
 	}
 

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

--- /dev/null
+++ b/include/controller/Controller_Logout.class.php
@@ -1,1 +1,75 @@
+<?php
+/**
+ * Controller for logout
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Controller
+ */
+class GitPHP_Controller_Logout extends GitPHP_ControllerBase
+{
+	/**
+	 * Initialize controller
+	 */
+	public function Initialize()
+	{
+		$this->InitializeUserList();
+	}
 
+	/**
+	 * Gets the template for this controller
+	 *
+	 * @return string template filename
+	 */
+	protected function GetTemplate()
+	{
+	}
+
+	/**
+	 * Gets the cache key for this controller
+	 *
+	 * @return string cache key
+	 */
+	protected function GetCacheKey()
+	{
+	}
+
+	/**
+	 * Gets the name of this controller's action
+	 *
+	 * @param boolean $local true if caller wants the localized action name
+	 * @return string action name
+	 */
+	public function GetName($local = false)
+	{
+		return 'logout';
+	}
+
+	/**
+	 * Loads headers for this template
+	 */
+	protected function LoadHeaders()
+	{
+		if (!empty($_SESSION['gitphpuser'])) {
+			unset($_SESSION['gitphpuser']);
+		}
+		$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
+	}
+
+	/**
+	 * Loads data for this template
+	 */
+	protected function LoadData()
+	{
+	}
+
+	/**
+	 * Renders the output
+	 */
+	public function Render()
+	{
+	}
+
+}
+

--- a/include/router/Router.class.php
+++ b/include/router/Router.class.php
@@ -214,7 +214,7 @@
 
 		// non-project action
 		$this->routes[] = new GitPHP_Route(':action', array(
-			'action' => 'opml|projectindex'
+			'action' => 'opml|projectindex|login|logout'
 		));
 
 		usort($this->routes, array('GitPHP_Route', 'CompareRoute'));
@@ -479,6 +479,19 @@
 				$controller->SetParam('opml', true);
 				break;
 
+			
+			case 'login':
+				$controller = new GitPHP_Controller_Login();
+				if (!empty($_POST['username']))
+					$controller->SetParam('username', $_POST['username']);
+				if (!empty($_POST['password']))
+					$controller->SetParam('password', $_POST['password']);
+				break;
+
+			case 'logout':
+				$controller = new GitPHP_Controller_Logout();
+				break;
+
 
 			case 'graph':
 			case 'graphs':

--- /dev/null
+++ b/templates/login.tpl
@@ -1,1 +1,38 @@
+{*
+ * Login template
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Template
+ *}
+{extends file='main.tpl'}
 
+{block name=header}
+  <a href="{geturl}">{if $homelink}{$homelink}{else}{t}projects{/t}{/if}</a> / {if $actionlocal}{$actionlocal}{/if}
+{/block}
+
+{block name=main}
+{if $loginerror}
+<div class="loginerror">
+{$loginerror}
+</div>
+{/if}
+<form method="post" action="{geturl action=login}">
+<table>
+  <tr>
+    <td><label for="username">Username:</label></td>
+    <td><input type="text" name="username" {if $username}value="{$username}"{/if} /></td>
+  </tr>
+  <tr>
+    <td><label for="password">Password:</label></td>
+    <td><input type="password" name="password" /></td>
+  </tr>
+  <tr>
+    <td></td>
+    <td><input type="submit" value="Login" /></td>
+  </tr>
+</table>
+</form>
+{/block}
+

--- a/templates/main.tpl
+++ b/templates/main.tpl
@@ -86,6 +86,15 @@
     {/if}
   </head>
   <body>
+    {if $loginenabled}
+    <div class="login">
+    {if $loggedinuser}
+      <a href="{geturl action=logout}" />logout</a>
+    {else}
+      <a href="{geturl action=login}" />login</a>
+    {/if}
+    </div>
+    {/if}
     <div class="page_header">
       {if $supportedlocales}
       <div class="lang_select">

comments