Start adding basic login infrastructure (still needs styling)
[gitphp.git] / include / router / Router.class.php
blob:a/include/router/Router.class.php -> blob:b/include/router/Router.class.php
<?php <?php
/** /**
* Request router * Request router
* *
* @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 Router * @subpackage Router
*/ */
class GitPHP_Router class GitPHP_Router
{ {
   
/** /**
* Route map * Route map
* *
* @var array * @var array
*/ */
protected $routes = array(); protected $routes = array();
   
/** /**
* Query parameter map * Query parameter map
* *
* @var array * @var array
*/ */
protected $queryParameters = array(); protected $queryParameters = array();
   
/** /**
* Clean url flag * Clean url flag
* *
* @var boolean * @var boolean
*/ */
protected $cleanurl = false; protected $cleanurl = false;
   
/** /**
* Abbreviate hashes flag * Abbreviate hashes flag
* *
* @var boolean * @var boolean
*/ */
protected $abbreviate = false; protected $abbreviate = false;
   
/** /**
* Base url * Base url
* *
* @var string * @var string
*/ */
protected $baseurl; protected $baseurl;
   
/** /**
* Full url * Full url
* *
* @var string * @var string
*/ */
protected $fullurl; protected $fullurl;
   
/** /**
* Constructor * Constructor
* *
* @param boolean $cleanurl true to generate clean urls * @param boolean $cleanurl true to generate clean urls
* @param boolean $abbreviate true to abbreviate hashes * @param boolean $abbreviate true to abbreviate hashes
*/ */
public function __construct($cleanurl = false, $abbreviate = false) public function __construct($cleanurl = false, $abbreviate = false)
{ {
$this->cleanurl = $cleanurl; $this->cleanurl = $cleanurl;
$this->abbreviate = $abbreviate; $this->abbreviate = $abbreviate;
   
$this->baseurl = GitPHP_Util::BaseUrl(); $this->baseurl = GitPHP_Util::BaseUrl();
if (empty($this->baseurl)) if (empty($this->baseurl))
$this->baseurl = '/'; $this->baseurl = '/';
$this->fullurl = GitPHP_Util::BaseUrl(true); $this->fullurl = GitPHP_Util::BaseUrl(true);
   
$this->InitializeRoutes(); $this->InitializeRoutes();
$this->InitializeQueryParameters(); $this->InitializeQueryParameters();
} }
   
/** /**
* Get clean url setting * Get clean url setting
* *
* @return boolean * @return boolean
*/ */
public function GetCleanUrl() public function GetCleanUrl()
{ {
return $this->cleanurl; return $this->cleanurl;
} }
   
/** /**
* Set clean url setting * Set clean url setting
* *
* @param boolean $cleanurl true to generate clean urls * @param boolean $cleanurl true to generate clean urls
*/ */
public function SetCleanUrl($cleanurl) public function SetCleanUrl($cleanurl)
{ {
$this->cleanurl = $cleanurl; $this->cleanurl = $cleanurl;
} }
   
/** /**
* Get abbreviate hash setting * Get abbreviate hash setting
* *
* @return boolean * @return boolean
*/ */
public function GetAbbreviate() public function GetAbbreviate()
{ {
return $this->abbreviate; return $this->abbreviate;
} }
   
/** /**
* Set abbreviate hash setting * Set abbreviate hash setting
* *
* @param boolean $abbreviate abbreviate * @param boolean $abbreviate abbreviate
*/ */
public function SetAbbreviate($abbreviate) public function SetAbbreviate($abbreviate)
{ {
$this->abbreviate = $abbreviate; $this->abbreviate = $abbreviate;
} }
   
/** /**
* Get base url * Get base url
* *
* @param boolean $full true to return full base url (include protocol and hostname) * @param boolean $full true to return full base url (include protocol and hostname)
* @return string base url * @return string base url
*/ */
public function GetBaseUrl($full = false) public function GetBaseUrl($full = false)
{ {
if ($full) if ($full)
return $this->fullurl; return $this->fullurl;
   
return $this->baseurl; return $this->baseurl;
} }
   
/** /**
* Set base url * Set base url
* *
* @param string $baseurl base url * @param string $baseurl base url
*/ */
public function SetBaseUrl($baseurl) public function SetBaseUrl($baseurl)
{ {
$this->baseurl = $baseurl; $this->baseurl = $baseurl;
$this->fullurl = $baseurl; $this->fullurl = $baseurl;
} }
   
/** /**
* Initialize route map * Initialize route map
*/ */
private function InitializeRoutes() private function InitializeRoutes()
{ {
// project view // project view
$projectroute = new GitPHP_Route('projects/:project', array( $projectroute = new GitPHP_Route('projects/:project', array(
'project' => '[^\?]+' 'project' => '[^\?]+'
)); ));
   
// project-specific action with hash and output method // project-specific action with hash and output method
$this->routes[] = new GitPHP_Route(':action/:hash/:output', array( $this->routes[] = new GitPHP_Route(':action/:hash/:output', array(
'action' => 'blobs', 'action' => 'blobs',
'hash' => '[0-9A-Fa-f]{4,40}|HEAD', 'hash' => '[0-9A-Fa-f]{4,40}|HEAD',
'output' => 'plain' 'output' => 'plain'
), array(), $projectroute); ), array(), $projectroute);
   
// project-specific action with hash // project-specific action with hash
$this->routes[] = new GitPHP_Route(':action/:hash', array( $this->routes[] = new GitPHP_Route(':action/:hash', array(
'action' => 'commits|trees|blobs|search|snapshot|commitdiff|blobdiff|blame', 'action' => 'commits|trees|blobs|search|snapshot|commitdiff|blobdiff|blame',
'hash' => '[0-9A-Fa-f]{4,40}|HEAD' 'hash' => '[0-9A-Fa-f]{4,40}|HEAD'
), array(), $projectroute); ), array(), $projectroute);
   
// project-specific action with hash or ref // project-specific action with hash or ref
$this->routes[] = new GitPHP_Route(':action/:hash', array( $this->routes[] = new GitPHP_Route(':action/:hash', array(
'action' => 'shortlog|log', 'action' => 'shortlog|log',
'hash' => '[^\?]+' 'hash' => '[^\?]+'
), array(), $projectroute); ), array(), $projectroute);
   
// map heads to shortlog // map heads to shortlog
$this->routes[] = new GitPHP_Route(':action/:hash', array( $this->routes[] = new GitPHP_Route(':action/:hash', array(
'action' => 'heads', 'action' => 'heads',
'hash' => '[^\?]+' 'hash' => '[^\?]+'
), array( ), array(
'action' => 'shortlog' 'action' => 'shortlog'
), $projectroute); ), $projectroute);
   
// project-specific graphs // project-specific graphs
$this->routes[] = new GitPHP_Route(':action/:graphtype', array( $this->routes[] = new GitPHP_Route(':action/:graphtype', array(
'action' => 'graphs', 'action' => 'graphs',
'graphtype' => '[a-z]+' 'graphtype' => '[a-z]+'
), array(), $projectroute); ), array(), $projectroute);
   
// project-specific tag // project-specific tag
$this->routes[] = new GitPHP_Route(':action/:tag', array( $this->routes[] = new GitPHP_Route(':action/:tag', array(
'action' => 'tags', 'action' => 'tags',
'tag' => '[^\?]+' 'tag' => '[^\?]+'
), array(), $projectroute); ), array(), $projectroute);
   
$formats = GitPHP_Archive::SupportedFormats(); $formats = GitPHP_Archive::SupportedFormats();
if (count($formats) > 0) { if (count($formats) > 0) {
$formatconstraint = implode("|", array_keys($formats)); $formatconstraint = implode("|", array_keys($formats));
// project specific snapshot format with hash // project specific snapshot format with hash
$this->routes[] = new GitPHP_Route(':format/:hash', array( $this->routes[] = new GitPHP_Route(':format/:hash', array(
'format' => $formatconstraint, 'format' => $formatconstraint,
'hash' => '[0-9A-Fa-f]{4,40}|HEAD' 'hash' => '[0-9A-Fa-f]{4,40}|HEAD'
), array( ), array(
'action' => 'snapshot' 'action' => 'snapshot'
), $projectroute); ), $projectroute);
   
// project specific snapshot format // project specific snapshot format
$this->routes[] = new GitPHP_Route(':format', array( $this->routes[] = new GitPHP_Route(':format', array(
'format' => $formatconstraint 'format' => $formatconstraint
), array( ), array(
'action' => 'snapshot' 'action' => 'snapshot'
), $projectroute); ), $projectroute);
} }
   
// project-specific action only // project-specific action only
$this->routes[] = new GitPHP_Route(':action', array( $this->routes[] = new GitPHP_Route(':action', array(
'action' => 'tags|heads|shortlog|log|search|atom|rss|snapshot|commits|graphs|trees|blobs|history|commitdiff|blobdiff' 'action' => 'tags|heads|shortlog|log|search|atom|rss|snapshot|commits|graphs|trees|blobs|history|commitdiff|blobdiff'
), array(), $projectroute); ), array(), $projectroute);
   
$this->routes[] = $projectroute; $this->routes[] = $projectroute;
   
// non-project action // non-project action
$this->routes[] = new GitPHP_Route(':action', array( $this->routes[] = new GitPHP_Route(':action', array(
'action' => 'opml|projectindex' 'action' => 'opml|projectindex|login|logout'
)); ));
   
usort($this->routes, array('GitPHP_Route', 'CompareRoute')); usort($this->routes, array('GitPHP_Route', 'CompareRoute'));
} }
   
/** /**
* Initialize query parameter map * Initialize query parameter map
*/ */
private function InitializeQueryParameters() private function InitializeQueryParameters()
{ {
$this->queryParameters = array( $this->queryParameters = array(
'project' => 'p', 'project' => 'p',
'action' => 'a', 'action' => 'a',
'hash' => 'h', 'hash' => 'h',
'hashbase' => 'hb', 'hashbase' => 'hb',
'hashparent' => 'hp', 'hashparent' => 'hp',
'graphtype' => 'g', 'graphtype' => 'g',
'output' => 'o', 'output' => 'o',
'format' => 'fmt', 'format' => 'fmt',
'tag' => 't', 'tag' => 't',
'page' => 'pg', 'page' => 'pg',
'search' => 's', 'search' => 's',
'searchtype' => 'st', 'searchtype' => 'st',
'diffmode' => 'd', 'diffmode' => 'd',
'file' => 'f', 'file' => 'f',
'mark' => 'm', 'mark' => 'm',
'prefix' => 'prefix', 'prefix' => 'prefix',
'sort' => 'sort', 'sort' => 'sort',
'lang' => 'l' 'lang' => 'l'
); );
} }
   
/** /**
* Convert a parameter array to a query variable array * Convert a parameter array to a query variable array
* *
* @param array $params parameter array * @param array $params parameter array
* @return array query variable array * @return array query variable array
*/ */
private function ParameterArrayToQueryVarArray($params) private function ParameterArrayToQueryVarArray($params)
{ {
$queryvars = array(); $queryvars = array();
   
if (count($params) < 1) if (count($params) < 1)
return $queryvars; return $queryvars;
   
foreach ($params as $param => $val) { foreach ($params as $param => $val) {
   
if (empty($val)) if (empty($val))
continue; continue;
   
if (empty($this->queryParameters[$param])) if (empty($this->queryParameters[$param]))
continue; continue;
   
$queryvar = $this->queryParameters[$param]; $queryvar = $this->queryParameters[$param];
   
if (!empty($queryvar)) if (!empty($queryvar))
$queryvars[$queryvar] = $val; $queryvars[$queryvar] = $val;
} }
   
return $queryvars; return $queryvars;
} }
   
/** /**
* Convert a query variable array to a parameter array * Convert a query variable array to a parameter array
* *
* @param array $queryvars query variable array * @param array $queryvars query variable array
* @return array parameter array * @return array parameter array
*/ */
private function QueryVarArrayToParameterArray($queryvars) private function QueryVarArrayToParameterArray($queryvars)
{ {
$params = array(); $params = array();
   
if (count($queryvars) < 1) if (count($queryvars) < 1)
return $params; return $params;
   
foreach ($queryvars as $var => $val) { foreach ($queryvars as $var => $val) {
   
if (empty($val)) if (empty($val))
continue; continue;
   
$param = array_search($var, $this->queryParameters); $param = array_search($var, $this->queryParameters);
   
if (!empty($param)) if (!empty($param))
$params[$param] = $val; $params[$param] = $val;
} }
   
return $params; return $params;
} }
   
/** /**
* Build route from url parameters * Build route from url parameters
* *
* @param array $urlparams url parameters * @param array $urlparams url parameters
*/ */
private function BuildRoute($urlparams) private function BuildRoute($urlparams)
{ {
foreach ($this->routes as $route) { foreach ($this->routes as $route) {
   
if (!$route->Valid($urlparams)) if (!$route->Valid($urlparams))
continue; continue;
   
$path = $route->Build($urlparams); $path = $route->Build($urlparams);
$usedparams = $route->GetUsedParameters(); $usedparams = $route->GetUsedParameters();
   
return array($path, $usedparams); return array($path, $usedparams);
} }
   
return array(null, array()); return array(null, array());
} }
   
/** /**
* Find route matching query * Find route matching query
* *
* @param string $query query * @param string $query query
* @return array query parameters * @return array query parameters
*/ */
private function FindRoute($query) private function FindRoute($query)
{ {
if (empty($query)) if (empty($query))
return array(); return array();
   
foreach ($this->routes as $route) { foreach ($this->routes as $route) {
   
$params = $route->Match($query); $params = $route->Match($query);
if ($params === false) if ($params === false)
continue; continue;
   
return $params; return $params;
} }
   
return array(); return array();
} }
   
/** /**
* Gets a controller for an action * Gets a controller for an action
* *
* @return mixed controller object * @return mixed controller object
*/ */
public function GetController() public function GetController()
{ {
$params = $this->QueryVarArrayToParameterArray($_GET); $params = $this->QueryVarArrayToParameterArray($_GET);
   
if (!empty($_GET['q'])) { if (!empty($_GET['q'])) {
$restparams = GitPHP_Router::ReadCleanUrl($_SERVER['REQUEST_URI']); $restparams = GitPHP_Router::ReadCleanUrl($_SERVER['REQUEST_URI']);
if (count($restparams) > 0) if (count($restparams) > 0)
$params = array_merge($params, $restparams); $params = array_merge($params, $restparams);
} }
   
$controller = null; $controller = null;
   
$action = null; $action = null;
if (!empty($params['action'])) if (!empty($params['action']))
$action = $params['action']; $action = $params['action'];
   
switch ($action) { switch ($action) {
   
   
case 'search': case 'search':
$controller = new GitPHP_Controller_Search(); $controller = new GitPHP_Controller_Search();
break; break;
   
   
case 'commitdiff': case 'commitdiff':
case 'commitdiff_plain': case 'commitdiff_plain':
$controller = new GitPHP_Controller_Commitdiff(); $controller = new GitPHP_Controller_Commitdiff();
if ($action === 'commitdiff_plain') if ($action === 'commitdiff_plain')
$controller->SetParam('output', 'plain'); $controller->SetParam('output', 'plain');
break; break;
   
   
case 'blobdiff': case 'blobdiff':
case 'blobdiff_plain': case 'blobdiff_plain':
$controller = new GitPHP_Controller_Blobdiff(); $controller = new GitPHP_Controller_Blobdiff();
if ($action === 'blobdiff_plain') if ($action === 'blobdiff_plain')
$controller->SetParam('output', 'plain'); $controller->SetParam('output', 'plain');
break; break;
   
   
case 'history': case 'history':
$controller = new GitPHP_Controller_History(); $controller = new GitPHP_Controller_History();
break; break;
   
   
case 'shortlog': case 'shortlog':
case 'log': case 'log':
$controller = new GitPHP_Controller_Log(); $controller = new GitPHP_Controller_Log();
if ($action === 'shortlog') if ($action === 'shortlog')
$controller->SetParam('short', true); $controller->SetParam('short', true);
break; break;
   
   
case 'snapshot': case 'snapshot':
$controller = new GitPHP_Controller_Snapshot(); $controller = new GitPHP_Controller_Snapshot();
break; break;
   
   
case 'tree': case 'tree':
case 'trees': case 'trees':
$controller = new GitPHP_Controller_Tree(); $controller = new GitPHP_Controller_Tree();
break; break;
   
   
case 'tags': case 'tags':
if (empty($params['tag'])) { if (empty($params['tag'])) {
$controller = new GitPHP_Controller_Tags(); $controller = new GitPHP_Controller_Tags();
break; break;
} }
case 'tag': case 'tag':
$controller = new GitPHP_Controller_Tag(); $controller = new GitPHP_Controller_Tag();
break; break;
   
   
case 'heads': case 'heads':
$controller = new GitPHP_Controller_Heads(); $controller = new GitPHP_Controller_Heads();
break; break;
   
   
case 'blame': case 'blame':
$controller = new GitPHP_Controller_Blame(); $controller = new GitPHP_Controller_Blame();
break; break;
   
   
case 'blob': case 'blob':
case 'blobs': case 'blobs':
case 'blob_plain': case 'blob_plain':
$controller = new GitPHP_Controller_Blob(); $controller = new GitPHP_Controller_Blob();
if ($action === 'blob_plain') if ($action === 'blob_plain')
$controller->SetParam('output', 'plain'); $controller->SetParam('output', 'plain');
break; break;
   
   
case 'atom': case 'atom':
case 'rss': case 'rss':
$controller = new GitPHP_Controller_Feed(); $controller = new GitPHP_Controller_Feed();
if ($action == 'rss') if ($action == 'rss')
$controller->SetParam('format', GitPHP_Controller_Feed::RssFormat); $controller->SetParam('format', GitPHP_Controller_Feed::RssFormat);
else if ($action == 'atom') else if ($action == 'atom')
$controller->SetParam('format', GitPHP_Controller_Feed::AtomFormat); $controller->SetParam('format', GitPHP_Controller_Feed::AtomFormat);
break; break;
   
   
case 'commit': case 'commit':
case 'commits': case 'commits':
$controller = new GitPHP_Controller_Commit(); $controller = new GitPHP_Controller_Commit();
break; break;
   
   
case 'summary': case 'summary':
$controller = new GitPHP_Controller_Project(); $controller = new GitPHP_Controller_Project();
break; break;
   
   
case 'project_index': case 'project_index':
case 'projectindex': case 'projectindex':
$controller = new GitPHP_Controller_ProjectList(); $controller = new GitPHP_Controller_ProjectList();
$controller->SetParam('txt', true); $controller->SetParam('txt', true);
break; break;
   
   
case 'opml': case 'opml':
$controller = new GitPHP_Controller_ProjectList(); $controller = new GitPHP_Controller_ProjectList();
$controller->SetParam('opml', true); $controller->SetParam('opml', true);
break; 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 'graph':
case 'graphs': case 'graphs':
//$controller = new GitPHP_Controller_Graph(); //$controller = new GitPHP_Controller_Graph();
//break; //break;
case 'graphdata': case 'graphdata':
//$controller = new GitPHP_Controller_GraphData(); //$controller = new GitPHP_Controller_GraphData();
//break; //break;
default: default:
if (!empty($params['project'])) { if (!empty($params['project'])) {
$controller = new GitPHP_Controller_Project(); $controller = new GitPHP_Controller_Project();
} else { } else {
$controller = new GitPHP_Controller_ProjectList(); $controller = new GitPHP_Controller_ProjectList();
} }
} }
   
foreach ($params as $paramname => $paramval) { foreach ($params as $paramname => $paramval) {
if ($paramname !== 'action') if ($paramname !== 'action')
$controller->SetParam($paramname, $paramval); $controller->SetParam($paramname, $paramval);
} }
   
$controller->SetRouter($this); $controller->SetRouter($this);
   
return $controller; return $controller;
} }
   
/** /**
* Get message controller * Get message controller
* *
* @return GitPHP_ControllerBase * @return GitPHP_ControllerBase
*/ */
public function GetMessageController() public function GetMessageController()
{ {
$params = $this->QueryVarArrayToParameterArray($_GET); $params = $this->QueryVarArrayToParameterArray($_GET);
   
if (!empty($_GET['q'])) { if (!empty($_GET['q'])) {
$restparams = GitPHP_Router::ReadCleanUrl($_SERVER['REQUEST_URI']); $restparams = GitPHP_Router::ReadCleanUrl($_SERVER['REQUEST_URI']);
if (count($restparams) > 0) if (count($restparams) > 0)
$params = array_merge($params, $restparams); $params = array_merge($params, $restparams);
} }
   
$controller = new GitPHP_Controller_Message(); $controller = new GitPHP_Controller_Message();
   
foreach ($params as $paramname => $paramval) { foreach ($params as $paramname => $paramval) {
if ($paramname !== 'action') if ($paramname !== 'action')
$controller->SetParam($paramname, $paramval); $controller->SetParam($paramname, $paramval);
} }
   
$controller->SetRouter($this); $controller->SetRouter($this);
   
return $controller; return $controller;
} }
   
/** /**
* Read a rest-style clean url * Read a rest-style clean url
* *
* @param string $url url * @param string $url url
* @return array request parameters from url * @return array request parameters from url
*/ */
private function ReadCleanUrl($url) private function ReadCleanUrl($url)
{ {
$querypos = strpos($url, '?'); $querypos = strpos($url, '?');
if ($querypos !== false) if ($querypos !== false)
$url = substr($url, 0, $querypos); $url = substr($url, 0, $querypos);
   
$url = rtrim($url, "/"); $url = rtrim($url, "/");
   
$baseurl = GitPHP_Util::AddSlash(GitPHP_Util::BaseUrl(), false); $baseurl = GitPHP_Util::AddSlash(GitPHP_Util::BaseUrl(), false);
if (empty($baseurl)) if (empty($baseurl))
$baseurl = '/'; $baseurl = '/';
   
if (strncmp($baseurl, $url, strlen($baseurl)) === 0) if (strncmp($baseurl, $url, strlen($baseurl)) === 0)
$url = substr($url, strlen($baseurl)); $url = substr($url, strlen($baseurl));
   
return $this->FindRoute($url); return $this->FindRoute($url);
} }
   
/** /**
* Generate a url * Generate a url
* *
* @param array $params request parameters * @param array $params request parameters
* @param boolean $full true to get full url (include protocol and hostname) * @param boolean $full true to get full url (include protocol and hostname)
*/ */
public function GetUrl($params = array(), $full = false) public function GetUrl($params = array(), $full = false)
{ {
if ($full) if ($full)
$baseurl = $this->fullurl; $baseurl = $this->fullurl;
else else
$baseurl = $this->baseurl; $baseurl = $this->baseurl;
   
if ($this->cleanurl) { if ($this->cleanurl) {
if (substr_compare($baseurl, 'index.php', -9) === 0) { if (substr_compare($baseurl, 'index.php', -9) === 0) {
$baseurl = dirname($baseurl); $baseurl = dirname($baseurl);
} }
$baseurl = GitPHP_Util::AddSlash($baseurl, false); $baseurl = GitPHP_Util::AddSlash($baseurl, false);
} else { } else {
if (substr_compare($baseurl, 'index.php', -9) !== 0) { if (substr_compare($baseurl, 'index.php', -9) !== 0) {
$baseurl = GitPHP_Util::AddSlash($baseurl, false); $baseurl = GitPHP_Util::AddSlash($baseurl, false);
} }
} }
   
if (count($params) < 1) if (count($params) < 1)
return $baseurl; return $baseurl;
   
$abbreviate = $this->abbreviate; $abbreviate = $this->abbreviate;
if ($abbreviate && !empty($params['project']) && ($params['project'] instanceof GitPHP_Project)) { if ($abbreviate && !empty($params['project']) && ($params['project'] instanceof GitPHP_Project)) {
if ($params['project']->GetCompat()) if ($params['project']->GetCompat())
$abbreviate = false; $abbreviate = false;
} }
   
foreach ($params as $paramname => $paramval) { foreach ($params as $paramname => $paramval) {
switch ($paramname) { switch ($paramname) {
case 'hash': case 'hash':
case 'hashbase': case 'hashbase':
case 'hashparent': case 'hashparent':
case 'mark': case 'mark':
$params[$paramname] = GitPHP_Router::GetHash($paramval, $abbreviate); $params[$paramname] = GitPHP_Router::GetHash($paramval, $abbreviate);
break; break;
case 'tag': case 'tag':
$params[$paramname] = GitPHP_Router::GetTag($paramval); $params[$paramname] = GitPHP_Router::GetTag($paramval);
break; break;
case 'project': case 'project':
$params[$paramname] = GitPHP_Router::GetProject($paramval); $params[$paramname] = GitPHP_Router::GetProject($paramval);
break; break;
} }
} }
   
if ($this->cleanurl) { if ($this->cleanurl) {
   
if (!empty($params['action'])) { if (!empty($params['action'])) {
switch ($params['action']) { switch ($params['action']) {
case 'blob': case 'blob':
case 'commit': case 'commit':
case 'tree': case 'tree':
case 'graph': case 'graph':
case 'tag': case 'tag':
// these actions are plural in clean urls // these actions are plural in clean urls
$params['action'] = $params['action'] . 's'; $params['action'] = $params['action'] . 's';
break; break;
} }
} }
   
list($queryurl, $exclude) = $this->BuildRoute($params); list($queryurl, $exclude) = $this->BuildRoute($params);
$baseurl .= $queryurl; $baseurl .= $queryurl;
   
foreach ($exclude as $excludeparam) { foreach ($exclude as $excludeparam) {
unset($params[$excludeparam]); unset($params[$excludeparam]);
} }
} }
   
$querystr = GitPHP_Router::GetQueryString($params); $querystr = GitPHP_Router::GetQueryString($params);
   
if (empty($querystr)) if (empty($querystr))
return $baseurl; return $baseurl;
   
return $baseurl . '?' . $querystr; return $baseurl . '?' . $querystr;
} }
   
/** /**
* Gets query parameters for a url * Gets query parameters for a url
* *
* @param array $params query parameters * @param array $params query parameters
* @return string query string * @return string query string
*/ */
private function GetQueryString($params = array()) private function GetQueryString($params = array())
{ {
if (count($params) < 1) if (count($params) < 1)
return null; return null;
   
$query = $this->ParameterArrayToQueryVarArray($params); $query = $this->ParameterArrayToQueryVarArray($params);
   
if (count($query) < 1) if (count($query) < 1)
return null; return null;
   
$querystr = null; $querystr = null;
   
foreach ($query as $var => $val) { foreach ($query as $var => $val) {
if (empty($val)) if (empty($val))
continue; continue;
if (!empty($querystr)) if (!empty($querystr))
$querystr .= '&'; $querystr .= '&';
$querystr .= $var . '=' . rawurlencode($val); $querystr .= $var . '=' . rawurlencode($val);
} }
   
return $querystr; return $querystr;
} }
   
/** /**
* Gets a hash for a string or hash-identified object * Gets a hash for a string or hash-identified object
* *
* @param string|GitPHP_GitObject $value string or hashed object * @param string|GitPHP_GitObject $value string or hashed object
* @param boolean $abbreviate true to abbreviate hash * @param boolean $abbreviate true to abbreviate hash
* @return string hash * @return string hash
*/ */
private static function GetHash($value, $abbreviate = false) private static function GetHash($value, $abbreviate = false)
{ {
if ($value instanceof GitPHP_GitObject) if ($value instanceof GitPHP_GitObject)
return $value->GetHash($abbreviate); return $value->GetHash($abbreviate);
else if (is_string($value)) else if (is_string($value))
return $value; return $value;
   
return null; return null;
} }
   
/** /**
* Gets an identifier for a tag * Gets an identifier for a tag
* *
* @param string|GitPHP_Tag $value string or tag * @param string|GitPHP_Tag $value string or tag
* @return string hash * @return string hash
*/ */
private static function GetTag($value) private static function GetTag($value)
{ {
if ($value instanceof GitPHP_Tag) if ($value instanceof GitPHP_Tag)
return $value->GetName(); return $value->GetName();
else if (is_string($value)) else if (is_string($value))
return $value; return $value;
return null; return null;
} }
   
/** /**
* Gets a project identifier for a project * Gets a project identifier for a project
* *
* @param string|GitPHP_Project $value string or project * @param string|GitPHP_Project $value string or project
* @return string identifier * @return string identifier
*/ */
private static function GetProject($value) private static function GetProject($value)
{ {
if ($value instanceof GitPHP_Project) { if ($value instanceof GitPHP_Project) {
return $value->GetProject(); return $value->GetProject();
} else if (is_string($project)) { } else if (is_string($project)) {
return $value; return $value;
} }
} }
   
} }
   
comments