Start adding basic login infrastructure (still needs styling)
[gitphp.git] / include / controller / ControllerBase.class.php
blob:a/include/controller/ControllerBase.class.php -> blob:b/include/controller/ControllerBase.class.php
<?php <?php
/** /**
* Base class that all controllers extend * Base class that all controllers extend
* *
* @author Christopher Han <xiphux@gmail.com * @author Christopher Han <xiphux@gmail.com
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
abstract class GitPHP_ControllerBase abstract class GitPHP_ControllerBase
{ {
   
/** /**
* Config handler instance * Config handler instance
* *
* @var GitPHP_Config * @var GitPHP_Config
*/ */
protected $config; protected $config;
   
/** /**
* User list instance * User list instance
* *
* @var GitPHP_UserList * @var GitPHP_UserList
*/ */
protected $userList; protected $userList;
   
/** /**
* Resource handler instance * Resource handler instance
* *
* @var GitPHP_Resource * @var GitPHP_Resource
*/ */
protected $resource; protected $resource;
   
/** /**
* Smarty instance * Smarty instance
* *
* @var Smarty * @var Smarty
*/ */
protected $tpl; protected $tpl;
   
/** /**
* Project list * Project list
* *
* @var GitPHP_ProjectListBase * @var GitPHP_ProjectListBase
*/ */
protected $projectList; protected $projectList;
   
/** /**
* Current project * Current project
* *
* @var GitPHP_Project * @var GitPHP_Project
*/ */
protected $project; protected $project;
   
/** /**
* Flag if this is a multi project controller * Flag if this is a multi project controller
* *
* @var boolean * @var boolean
*/ */
protected $multiProject; protected $multiProject;
   
/** /**
* Parameters * Parameters
* *
* @var array * @var array
*/ */
protected $params = array(); protected $params = array();
   
/** /**
* HTTP Headers * HTTP Headers
* *
* @var string[] * @var string[]
*/ */
protected $headers = array(); protected $headers = array();
   
/** /**
* Flag to preserve whitespace in output (for non-html output) * Flag to preserve whitespace in output (for non-html output)
* *
* @var boolean * @var boolean
*/ */
protected $preserveWhitespace = false; protected $preserveWhitespace = false;
   
/** /**
* Logger instance * Logger instance
* *
* @var GitPHP_DebugLog * @var GitPHP_DebugLog
*/ */
protected $log; protected $log;
   
/** /**
* Git executable instance * Git executable instance
* *
* @var GitPHP_GitExe * @var GitPHP_GitExe
*/ */
protected $exe; protected $exe;
   
/** /**
* Url router instance * Url router instance
* *
* @var GitPHP_Router * @var GitPHP_Router
*/ */
protected $router; protected $router;
   
/** /**
* 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->InitializeGitExe(); $this->InitializeGitExe();
   
$this->InitializeProjectList(); $this->InitializeProjectList();
   
$this->InitializeSmarty(); $this->InitializeSmarty();
   
if ($this->multiProject) { if ($this->multiProject) {
$this->projectList->LoadProjects(); $this->projectList->LoadProjects();
} }
   
if (!empty($this->params['project'])) { if (!empty($this->params['project'])) {
$project = $this->projectList->GetProject($this->params['project']); $project = $this->projectList->GetProject($this->params['project']);
if (!$project) { if (!$project) {
throw new GitPHP_InvalidProjectParameterException($this->params['project']); throw new GitPHP_InvalidProjectParameterException($this->params['project']);
} }
$this->project = $project->GetProject(); $this->project = $project->GetProject();
} }
   
if (!($this->project || $this->multiProject)) { if (!($this->project || $this->multiProject)) {
throw new GitPHP_MissingProjectParameterException(); throw new GitPHP_MissingProjectParameterException();
} }
} }
   
/** /**
* Initialize config * Initialize config
*/ */
protected function InitializeConfig() protected function InitializeConfig()
{ {
$this->config = new GitPHP_Config(); $this->config = new GitPHP_Config();
$this->config->LoadConfig(GITPHP_CONFIGDIR . 'gitphp.conf.php'); $this->config->LoadConfig(GITPHP_CONFIGDIR . 'gitphp.conf.php');
} }
   
/** /**
* Initialize resource manager * Initialize resource manager
*/ */
protected function InitializeResource() protected function InitializeResource()
{ {
$locale = null; $locale = null;
   
$baseurl = GitPHP_Util::BaseUrl(); $baseurl = GitPHP_Util::BaseUrl();
   
if (!empty($this->params['lang'])) { if (!empty($this->params['lang'])) {
/* /*
* User picked something * User picked something
*/ */
setcookie(GitPHP_Resource::LocaleCookie, $this->params['lang'], time()+GitPHP_Resource::LocaleCookieLifetime, $baseurl); setcookie(GitPHP_Resource::LocaleCookie, $this->params['lang'], time()+GitPHP_Resource::LocaleCookieLifetime, $baseurl);
$locale = $this->params['lang']; $locale = $this->params['lang'];
} else if (!empty($_COOKIE[GitPHP_Resource::LocaleCookie])) { } else if (!empty($_COOKIE[GitPHP_Resource::LocaleCookie])) {
/** /**
* Returning user with a preference * Returning user with a preference
*/ */
$locale = $_COOKIE[GITPHP_Resource::LocaleCookie]; $locale = $_COOKIE[GITPHP_Resource::LocaleCookie];
} else { } else {
/* /*
* User's first time here, try by HTTP_ACCEPT_LANGUAGE * User's first time here, try by HTTP_ACCEPT_LANGUAGE
*/ */
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$httpAcceptLang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); $httpAcceptLang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$locale = GitPHP_Resource::FindPreferredLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']); $locale = GitPHP_Resource::FindPreferredLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (!empty($locale)) { if (!empty($locale)) {
setcookie(GitPHP_Resource::LocaleCookie, $locale, time()+GitPHP_Resource::LocaleCookieLifetime, $baseurl); setcookie(GitPHP_Resource::LocaleCookie, $locale, time()+GitPHP_Resource::LocaleCookieLifetime, $baseurl);
} }
} }
} }
   
if (empty($locale) && $this->config) { if (empty($locale) && $this->config) {
/* /*
* No preference, fall back on setting * No preference, fall back on setting
*/ */
$locale = $this->config->GetValue('locale'); $locale = $this->config->GetValue('locale');
} }
   
if (!empty($locale) && ($locale != 'en_US')) { if (!empty($locale) && ($locale != 'en_US')) {
try { try {
$this->resource = new GitPHP_Resource($locale); $this->resource = new GitPHP_Resource($locale);
} catch (Exception $e) { } catch (Exception $e) {
} }
} }
} }
   
/** /**
* Initialize user list * Initialize user list
*/ */
public function InitializeUserList() public function InitializeUserList()
{ {
$this->userList = new GitPHP_UserList(); $this->userList = new GitPHP_UserList();
$this->userList->LoadUsers(GITPHP_CONFIGDIR . 'users.conf.php'); $this->userList->LoadUsers(GITPHP_CONFIGDIR . 'users.conf.php');
  if ($this->userList->GetCount() > 0) {
  if (!isset($_SESSION))
  session_start();
  }
} }
   
/** /**
* Initialize executable * Initialize executable
* *
* @param boolean $validate whether the exe should be validated * @param boolean $validate whether the exe should be validated
*/ */
protected function InitializeGitExe($validate = true) protected function InitializeGitExe($validate = true)
{ {
$this->exe = new GitPHP_GitExe($this->config->GetValue('gitbin')); $this->exe = new GitPHP_GitExe($this->config->GetValue('gitbin'));
if ($this->log) if ($this->log)
$this->exe->AddObserver($this->log); $this->exe->AddObserver($this->log);
if ($validate && !$this->exe->Valid()) { if ($validate && !$this->exe->Valid()) {
throw new GitPHP_InvalidGitExecutableException($this->exe->GetBinary()); throw new GitPHP_InvalidGitExecutableException($this->exe->GetBinary());
} }
} }
   
/** /**
* Initialize project list * Initialize project list
*/ */
protected function InitializeProjectList() protected function InitializeProjectList()
{ {
if (file_exists(GITPHP_CONFIGDIR . 'projects.conf.php')) { if (file_exists(GITPHP_CONFIGDIR . 'projects.conf.php')) {
$this->projectList = GitPHP_ProjectList::Instantiate($this->config, GITPHP_CONFIGDIR . 'projects.conf.php', false); $this->projectList = GitPHP_ProjectList::Instantiate($this->config, GITPHP_CONFIGDIR . 'projects.conf.php', false);
} else { } else {
$this->projectList = GitPHP_ProjectList::Instantiate($this->config, GITPHP_CONFIGDIR . 'gitphp.conf.php', true); $this->projectList = GitPHP_ProjectList::Instantiate($this->config, GITPHP_CONFIGDIR . 'gitphp.conf.php', true);
} }
   
$this->projectList->SetMemoryCache(new GitPHP_MemoryCache($this->config->GetValue('objectmemory'))); $this->projectList->SetMemoryCache(new GitPHP_MemoryCache($this->config->GetValue('objectmemory')));
if ($this->config->GetValue('objectcache')) { if ($this->config->GetValue('objectcache')) {
$strategy = null; $strategy = null;
$servers = $this->config->GetValue('memcache'); $servers = $this->config->GetValue('memcache');
if ($servers) { if ($servers) {
if (class_exists('Memcached')) { if (class_exists('Memcached')) {
$strategy = new GitPHP_Cache_Memcached($servers); $strategy = new GitPHP_Cache_Memcached($servers);
} else if (class_exists('Memcache')) { } else if (class_exists('Memcache')) {
$strategy = new GitPHP_Cache_Memcache($servers); $strategy = new GitPHP_Cache_Memcache($servers);
} else { } else {
throw new GitPHP_MissingMemcacheException(); throw new GitPHP_MissingMemcacheException();
} }
} else { } else {
$strategy = new GitPHP_Cache_File(GITPHP_CACHEDIR . 'objects', $this->config->GetValue('objectcachecompress')); $strategy = new GitPHP_Cache_File(GITPHP_CACHEDIR . 'objects', $this->config->GetValue('objectcachecompress'));
} }
$cache = new GitPHP_Cache($strategy); $cache = new GitPHP_Cache($strategy);
$cache->SetLifetime($this->config->GetValue('objectcachelifetime')); $cache->SetLifetime($this->config->GetValue('objectcachelifetime'));
$this->projectList->SetCache($cache); $this->projectList->SetCache($cache);
} }
   
$this->projectList->SetExe($this->exe); $this->projectList->SetExe($this->exe);
   
if ($this->log) if ($this->log)
$this->projectList->AddObserver($this->log); $this->projectList->AddObserver($this->log);
   
} }
   
/** /**
* Initialize smarty * Initialize smarty
*/ */
protected function InitializeSmarty() protected function InitializeSmarty()
{ {
require_once(GITPHP_SMARTYDIR . 'Smarty.class.php'); require_once(GITPHP_SMARTYDIR . 'Smarty.class.php');
$this->tpl = new Smarty; $this->tpl = new Smarty;
$this->tpl->error_reporting = E_ALL & ~E_NOTICE; $this->tpl->error_reporting = E_ALL & ~E_NOTICE;
$this->tpl->merge_compiled_includes = true; $this->tpl->merge_compiled_includes = true;
$this->tpl->addPluginsDir(GITPHP_INCLUDEDIR . 'smartyplugins'); $this->tpl->addPluginsDir(GITPHP_INCLUDEDIR . 'smartyplugins');
   
if ($this->config->GetValue('cache')) { if ($this->config->GetValue('cache')) {
$cacheDir = GITPHP_CACHEDIR . 'templates'; $cacheDir = GITPHP_CACHEDIR . 'templates';
   
if (file_exists($cacheDir)) { if (file_exists($cacheDir)) {
if (!is_dir($cacheDir)) { if (!is_dir($cacheDir)) {
throw new Exception($cacheDir . ' exists but is not a directory'); throw new Exception($cacheDir . ' exists but is not a directory');
} else if (!is_writable($cacheDir)) { } else if (!is_writable($cacheDir)) {
throw new Exception($cacheDir . ' is not writable'); throw new Exception($cacheDir . ' is not writable');
} }
} else { } else {
if (!mkdir($cacheDir, 0777)) if (!mkdir($cacheDir, 0777))
throw new Exception($cacheDir . ' could not be created'); throw new Exception($cacheDir . ' could not be created');
chmod($cacheDir, 0777); chmod($cacheDir, 0777);
} }
$this->tpl->setCacheDir($cacheDir); $this->tpl->setCacheDir($cacheDir);
   
$this->tpl->caching = Smarty::CACHING_LIFETIME_SAVED; $this->tpl->caching = Smarty::CACHING_LIFETIME_SAVED;
if ($this->config->HasKey('cachelifetime')) { if ($this->config->HasKey('cachelifetime')) {
$this->tpl->cache_lifetime = $this->config->GetValue('cachelifetime'); $this->tpl->cache_lifetime = $this->config->GetValue('cachelifetime');
} }
   
$servers = $this->config->GetValue('memcache'); $servers = $this->config->GetValue('memcache');
if (isset($servers) && is_array($servers) && (count($servers) > 0)) { if (isset($servers) && is_array($servers) && (count($servers) > 0)) {
$this->tpl->registerCacheResource('memcache', new GitPHP_CacheResource_Memcache($servers)); $this->tpl->registerCacheResource('memcache', new GitPHP_CacheResource_Memcache($servers));
$this->tpl->caching_type = 'memcache'; $this->tpl->caching_type = 'memcache';
} }
   
} }
   
} }
   
/** /**
* Set router instance * Set router instance
* *
* @param GitPHP_Router $router router * @param GitPHP_Router $router router
*/ */
public function SetRouter($router) public function SetRouter($router)
{ {
$this->router = $router; $this->router = $router;
} }
   
/** /**
* Get config instance * Get config instance
* *
* @return GitPHP_Config * @return GitPHP_Config
*/ */
public function GetConfig() public function GetConfig()
{ {
return $this->config; return $this->config;
} }
   
/** /**
* Get log instance * Get log instance
* *
* @return GitPHP_DebugLog * @return GitPHP_DebugLog
*/ */
public function GetLog() public function GetLog()
{ {
return $this->log; return $this->log;
} }
   
/** /**
* Enable logging * Enable logging
*/ */
public function EnableLogging() public function EnableLogging()
{ {
if ($this->log) if ($this->log)
return; return;
   
$debug = $this->config->GetValue('debug'); $debug = $this->config->GetValue('debug');
if ($debug) { if ($debug) {
$this->log = new GitPHP_DebugLog($debug, $this->config->GetValue('benchmark')); $this->log = new GitPHP_DebugLog($debug, $this->config->GetValue('benchmark'));
$this->log->SetStartTime(GITPHP_START_TIME); $this->log->SetStartTime(GITPHP_START_TIME);
$this->log->SetStartMemory(GITPHP_START_MEM); $this->log->SetStartMemory(GITPHP_START_MEM);
if ($this->exe) if ($this->exe)
$this->exe->AddObserver($this->log); $this->exe->AddObserver($this->log);
if ($this->projectList) if ($this->projectList)
$this->projectList->AddObserver($this->log); $this->projectList->AddObserver($this->log);
} }
} }
   
/** /**
* Disable logging * Disable logging
*/ */
protected function DisableLogging() protected function DisableLogging()
{ {
if (!$this->log) if (!$this->log)
return; return;
   
$this->projectList->RemoveObserver($this->log); $this->projectList->RemoveObserver($this->log);
$this->exe->RemoveObserver($this->log); $this->exe->RemoveObserver($this->log);
   
$this->log->SetEnabled(false); $this->log->SetEnabled(false);
   
$this->log = null; $this->log = null;
} }
   
/** /**
* Gets the project for this controller * Gets the project for this controller
* *
* @return GitPHP_Project|null project * @return GitPHP_Project|null project
*/ */
public function GetProject() public function GetProject()
{ {
if ($this->project) if ($this->project)
return $this->projectList->GetProject($this->project); return $this->projectList->GetProject($this->project);
return null; return null;
} }
   
/** /**
* Gets the template for this controller * Gets the template for this controller
* *
* @return string template filename * @return string template filename
*/ */
protected abstract function GetTemplate(); protected abstract function GetTemplate();
   
/** /**
* Gets the cache key for this controller * Gets the cache key for this controller
* *
* @return string cache key * @return string cache key
*/ */
protected abstract function GetCacheKey(); protected abstract function GetCacheKey();
   
/** /**
* Get the prefix for all cache keys * Get the prefix for all cache keys
* *
* @param boolean $projectKeys include project-specific key pieces * @param boolean $projectKeys include project-specific key pieces
* @return string cache key prefix * @return string cache key prefix
*/ */
private function GetCacheKeyPrefix($projectKeys = true) private function GetCacheKeyPrefix($projectKeys = true)
{ {
if ($this->resource) if ($this->resource)
$cacheKeyPrefix = $this->resource->GetLocale(); $cacheKeyPrefix = $this->resource->GetLocale();
else else
$cacheKeyPrefix = 'en_US'; $cacheKeyPrefix = 'en_US';
   
if ($this->projectList) { if ($this->projectList) {
$cacheKeyPrefix .= '|' . sha1(serialize($this->projectList->GetProjectListConfig())) . '|' . sha1(serialize($this->projectList->GetProjectSettings())); $cacheKeyPrefix .= '|' . sha1(serialize($this->projectList->GetProjectListConfig())) . '|' . sha1(serialize($this->projectList->GetProjectSettings()));
} }
if ($this->project && $projectKeys) { if ($this->project && $projectKeys) {
$cacheKeyPrefix .= '|' . sha1($this->project); $cacheKeyPrefix .= '|' . sha1($this->project);
} }
return $cacheKeyPrefix; return $cacheKeyPrefix;
} }
   
/** /**
* Get the full cache key * Get the full cache key
* *
* @return string full cache key * @return string full cache key
*/ */
protected function GetFullCacheKey() protected function GetFullCacheKey()
{ {
$cacheKey = $this->GetCacheKeyPrefix(); $cacheKey = $this->GetCacheKeyPrefix();
   
$subCacheKey = $this->GetCacheKey(); $subCacheKey = $this->GetCacheKey();
   
if (!empty($subCacheKey)) if (!empty($subCacheKey))
$cacheKey .= '|' . $subCacheKey; $cacheKey .= '|' . $subCacheKey;
   
if (strlen($cacheKey) > 100) { if (strlen($cacheKey) > 100) {
$cacheKey = sha1($cacheKey); $cacheKey = sha1($cacheKey);
} }
   
return $cacheKey; return $cacheKey;
} }
   
/** /**
* 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 abstract function GetName($local = false); public abstract function GetName($local = false);
   
/** /**
* Set a parameter * Set a parameter
* *
* @param string $key key to set * @param string $key key to set
* @param mixed $value value to set * @param mixed $value value to set
*/ */
public function SetParam($key, $value) public function SetParam($key, $value)
{ {
if (empty($key)) if (empty($key))
return; return;
   
if (empty($value)) if (empty($value))
unset($this->params[$key]); unset($this->params[$key]);
   
if (is_string($value)) if (is_string($value))
$value = str_replace(chr(0), '', $value); $value = str_replace(chr(0), '', $value);
   
$this->params[$key] = $value; $this->params[$key] = $value;
} }
   
/** /**
* Loads headers for this template * Loads headers for this template
*/ */
protected function LoadHeaders() protected function LoadHeaders()
{ {
$this->headers[] = 'Content-Type: text/html; charset=UTF-8'; $this->headers[] = 'Content-Type: text/html; charset=UTF-8';
} }
   
/** /**
* Loads data for this template * Loads data for this template
*/ */
protected abstract function LoadData(); protected abstract function LoadData();
   
/** /**
* Loads common data used by all templates * Loads common data used by all templates
*/ */
private function LoadCommonData() private function LoadCommonData()
{ {
global $gitphp_version, $gitphp_appstring; global $gitphp_version, $gitphp_appstring;
   
$this->tpl->assign('version', $gitphp_version); $this->tpl->assign('version', $gitphp_version);
   
$stylesheet = $this->config->GetValue('stylesheet'); $stylesheet = $this->config->GetValue('stylesheet');
if ($stylesheet == 'gitphp.css') { if ($stylesheet == 'gitphp.css') {
// backwards compatibility // backwards compatibility
$stylesheet = 'gitphpskin.css'; $stylesheet = 'gitphpskin.css';
} }
$this->tpl->assign('stylesheet', preg_replace('/\.css$/', '', $stylesheet)); $this->tpl->assign('stylesheet', preg_replace('/\.css$/', '', $stylesheet));
   
$this->tpl->assign('javascript', $this->config->GetValue('javascript')); $this->tpl->assign('javascript', $this->config->GetValue('javascript'));
$this->tpl->assign('googlejs', $this->config->GetValue('googlejs')); $this->tpl->assign('googlejs', $this->config->GetValue('googlejs'));
if ($this->config->HasKey('title')) { if ($this->config->HasKey('title')) {
$this->tpl->assign('pagetitle', $this->config->GetValue('title')); $this->tpl->assign('pagetitle', $this->config->GetValue('title'));
} else { } else {
$this->tpl->assign('pagetitle', $gitphp_appstring); $this->tpl->assign('pagetitle', $gitphp_appstring);
} }
if ($this->config->HasKey('homelink')) { if ($this->config->HasKey('homelink')) {
$this->tpl->assign('homelink', $this->config->GetValue('homelink')); $this->tpl->assign('homelink', $this->config->GetValue('homelink'));
} else { } else {
if ($this->resource) if ($this->resource)
$this->tpl->assign('homelink', $this->resource->translate('projects')); $this->tpl->assign('homelink', $this->resource->translate('projects'));
else else
$this->tpl->assign('homelink', 'projects'); $this->tpl->assign('homelink', 'projects');
} }
$this->tpl->assign('action', $this->GetName()); $this->tpl->assign('action', $this->GetName());
$this->tpl->assign('actionlocal', $this->GetName(true)); $this->tpl->assign('actionlocal', $this->GetName(true));
if ($this->project) if ($this->project)
$this->tpl->assign('project', $this->GetProject()); $this->tpl->assign('project', $this->GetProject());
if ($this->config->GetValue('search')) if ($this->config->GetValue('search'))
$this->tpl->assign('enablesearch', true); $this->tpl->assign('enablesearch', true);
if ($this->config->GetValue('filesearch')) if ($this->config->GetValue('filesearch'))
$this->tpl->assign('filesearch', true); $this->tpl->assign('filesearch', true);
if (isset($this->params['search'])) if (isset($this->params['search']))
$this->tpl->assign('search', $this->params['search']); $this->tpl->assign('search', $this->params['search']);
if (isset($this->params['searchtype'])) if (isset($this->params['searchtype']))
$this->tpl->assign('searchtype', $this->params['searchtype']); $this->tpl->assign('searchtype', $this->params['searchtype']);
if ($this->resource) { if ($this->resource) {
$this->tpl->assign('currentlocale', $this->resource->GetLocale()); $this->tpl->assign('currentlocale', $this->resource->GetLocale());
$this->tpl->assign('currentprimarylocale', $this->resource->GetPrimaryLocale()); $this->tpl->assign('currentprimarylocale', $this->resource->GetPrimaryLocale());
$this->tpl->assign('resource', $this->resource); $this->tpl->assign('resource', $this->resource);
} else { } else {
$this->tpl->assign('currentlocale', 'en_US'); $this->tpl->assign('currentlocale', 'en_US');
$this->tpl->assign('currentprimarylocale', 'en'); $this->tpl->assign('currentprimarylocale', 'en');
} }
$this->tpl->assign('supportedlocales', GitPHP_Resource::SupportedLocales(true)); $this->tpl->assign('supportedlocales', GitPHP_Resource::SupportedLocales(true));
if ($this->config->GetValue('graphs')) if ($this->config->GetValue('graphs'))
$this->tpl->assign('enablegraphs', true); $this->tpl->assign('enablegraphs', true);
   
$this->tpl->assign('baseurl', GitPHP_Util::BaseUrl()); $this->tpl->assign('baseurl', GitPHP_Util::BaseUrl());
   
$requesturl = $_SERVER['REQUEST_URI']; $requesturl = $_SERVER['REQUEST_URI'];
$querypos = strpos($requesturl, '?'); $querypos = strpos($requesturl, '?');
if ($querypos !== false) if ($querypos !== false)
$requesturl = substr($requesturl, 0, $querypos); $requesturl = substr($requesturl, 0, $querypos);
$this->tpl->assign('requesturl', $requesturl); $this->tpl->assign('requesturl', $requesturl);
if ($this->router) { if ($this->router) {
$this->router->SetCleanUrl($this->config->GetValue('cleanurl') ? true : false); $this->router->SetCleanUrl($this->config->GetValue('cleanurl') ? true : false);
$this->router->SetAbbreviate($this->config->GetValue('abbreviateurl') ? true : false); $this->router->SetAbbreviate($this->config->GetValue('abbreviateurl') ? true : false);
if ($this->config->HasKey('self')) { if ($this->config->HasKey('self')) {
$this->router->SetBaseUrl($this->config->GetValue('self')); $this->router->SetBaseUrl($this->config->GetValue('self'));
} }
$this->tpl->assign('router', $this->router); $this->tpl->assign('router', $this->router);
} }
   
$getvars = explode('&', $_SERVER['QUERY_STRING']); $getvars = explode('&', $_SERVER['QUERY_STRING']);
$getvarsmapped = array(); $getvarsmapped = array();
foreach ($getvars as $varstr) { foreach ($getvars as $varstr) {
$eqpos = strpos($varstr, '='); $eqpos = strpos($varstr, '=');
if ($eqpos > 0) { if ($eqpos > 0) {
$var = substr($varstr, 0, $eqpos); $var = substr($varstr, 0, $eqpos);
$val = substr($varstr, $eqpos + 1); $val = substr($varstr, $eqpos + 1);
if (!(empty($var) || empty($val) || ($var == 'q'))) { if (!(empty($var) || empty($val) || ($var == 'q'))) {
$getvarsmapped[$var] = urldecode($val); $getvarsmapped[$var] = urldecode($val);
} }
} }
} }
$this->tpl->assign('requestvars', $getvarsmapped); $this->tpl->assign('requestvars', $getvarsmapped);
   
$this->tpl->assign('snapshotformats', GitPHP_Archive::SupportedFormats()); $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());
  }
  }
  }
} }
   
/** /**
* Renders any special headers * Renders any special headers
*/ */
public function RenderHeaders() public function RenderHeaders()
{ {
$this->LoadHeaders(); $this->LoadHeaders();
   
if (count($this->headers) > 0) { if (count($this->headers) > 0) {
$hascontenttype = false; $hascontenttype = false;
foreach ($this->headers as $hdr) { foreach ($this->headers as $hdr) {
if (empty($hdr)) if (empty($hdr))
continue; continue;
   
if (strncmp($hdr, 'Content-Type:', 13) === 0) { if (strncmp($hdr, 'Content-Type:', 13) === 0) {
if ($hascontenttype) if ($hascontenttype)
throw new Exception('Duplicate Content-Type header'); throw new Exception('Duplicate Content-Type header');
$hascontenttype = true; $hascontenttype = true;
} }
header($hdr); header($hdr);
} }
} }
} }
   
/** /**
* Renders the output * Renders the output
*/ */
public function Render() public function Render()
{ {
if (($this->config->GetValue('cache') == true) && ($this->config->GetValue('cacheexpire') === true)) if (($this->config->GetValue('cache') == true) && ($this->config->GetValue('cacheexpire') === true))
$this->CacheExpire(); $this->CacheExpire();
   
if (!$this->tpl->isCached($this->GetTemplate(), $this->GetFullCacheKey())) { if (!$this->tpl->isCached($this->GetTemplate(), $this->GetFullCacheKey())) {
$this->tpl->clearAllAssign(); $this->tpl->clearAllAssign();
if ($this->log && $this->log->GetBenchmark()) if ($this->log && $this->log->GetBenchmark())
$this->log->Log("Data load begin"); $this->log->Log("Data load begin");
$this->LoadCommonData(); $this->LoadCommonData();
$this->LoadData(); $this->LoadData();
if ($this->log && $this->log->GetBenchmark()) if ($this->log && $this->log->GetBenchmark())
$this->log->Log("Data load end"); $this->log->Log("Data load end");
} }
   
if (!$this->preserveWhitespace) { if (!$this->preserveWhitespace) {
//$this->tpl->loadFilter('output', 'trimwhitespace'); //$this->tpl->loadFilter('output', 'trimwhitespace');
} }
   
if ($this->log && $this->log->GetBenchmark()) if ($this->log && $this->log->GetBenchmark())
$this->log->Log("Smarty render begin"); $this->log->Log("Smarty render begin");
$this->tpl->display($this->GetTemplate(), $this->GetFullCacheKey()); $this->tpl->display($this->GetTemplate(), $this->GetFullCacheKey());
if ($this->log && $this->log->GetBenchmark()) if ($this->log && $this->log->GetBenchmark())
$this->log->Log("Smarty render end"); $this->log->Log("Smarty render end");
   
$this->tpl->clearAllAssign(); $this->tpl->clearAllAssign();
   
if ($this->log) if ($this->log && $this->projectList)
$this->log->Log('MemoryCache count: ' . $this->projectList->GetMemoryCache()->GetCount()); $this->log->Log('MemoryCache count: ' . $this->projectList->GetMemoryCache()->GetCount());
} }
   
/** /**
* Expires the cache * Expires the cache
* *
* @param boolean $expireAll expire the whole cache * @param boolean $expireAll expire the whole cache
*/ */
public function CacheExpire($expireAll = false) public function CacheExpire($expireAll = false)
{ {
if ($expireAll) { if ($expireAll) {
$this->tpl->clearAllCache(); $this->tpl->clearAllCache();
return; return;
} }
   
if (!$this->project) if (!$this->project)
return; return;
   
$epoch = $this->GetProject()->GetEpoch(); $epoch = $this->GetProject()->GetEpoch();
if (empty($epoch)) if (empty($epoch))
return; return;
   
$age = $this->GetProject()->GetAge(); $age = $this->GetProject()->GetAge();
   
$this->tpl->clearCache(null, $this->GetCacheKeyPrefix(), null, $age); $this->tpl->clearCache(null, $this->GetCacheKeyPrefix(), null, $age);
$this->tpl->clearCache('projectlist.tpl', $this->GetCacheKeyPrefix(false), null, $age); $this->tpl->clearCache('projectlist.tpl', $this->GetCacheKeyPrefix(false), null, $age);
} }
   
} }
   
comments