<?php
session_start();
include_once('inc/config.php');
mysql_connect($db_server, $db_username, $db_password);
mysql_select_db($db_database);
include_once('inc/util.inc');
include_once('inc/svn/phpsvnclient.php');
include_once('inc/geshi.php');
// Setup Theme
$modules = getModules();
$pageTitle = 'Source Code';
$curPage = 'code';
$theme = getTheme();
$js = '<link rel="stylesheet" type="text/css" href="'.$theme.'dawn.css"/>';
include_once($theme.'header.php');
$repoUrl = getRepoUrl();
if (empty($repoUrl)) {
echo '
<p>'._('SVN Repository not configured yet.').'</p>';
include($theme.'footer.php');
return;
}
$svn = new phpsvnclient($repoUrl);
$show_dirs = true;
//-----------------------------------------------
// Directory
//-----------------------------------------------
if (isset($_GET['directory'])) {
$files = $svn->getDirectoryFiles($_GET['directory']);
//-----------------------------------------------
// File
//-----------------------------------------------
} elseif (isset($_GET['file'])) {
$show_dirs = false;
$logs = $svn->getFileLogs($_GET['file']);
if (isset($_GET['revision'])) {
$file = $svn->getFile($_GET['file'], $_GET['revision']);
// get the id of the current revision
for ($i=0; $i<count($logs); $i++) {
$found = array_search($_GET['revision'], $logs[$i]);
if ($found !== false) {
$id = $i;
break;
}
}
} else {
$file = $svn->getFile($_GET['file']);
$id = count($logs) - 1;
}
$prevRev = isset($logs[$id-1]) ? $logs[$id-1]['version'] : false;
$nextRev = isset($logs[$id+1]) ? $logs[$id+1]['version'] : false;
$date = daysAgo($logs[$id]['date']);
$language = getSourceCodeLanguage($_GET['file']);
$geshi = new GeSHi($file, $language);
$geshi->set_overall_id('source-code');
$geshi->set_header_type(GESHI_HEADER_DIV);
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
$geshi->enable_keyword_links(false);
$geshi->enable_classes();
// Show the previous revision, if applicable
echo '
<div id="revision-nav">';
if ($prevRev) {
echo '
<a href="code.php?file='.$_GET['file'].'&revision='.$prevRev.'">'._('Previous Change').'</a> ';
}
if ($nextRev) {
echo '
| <a href="code.php?file='.$_GET['file'].'&revision='.$nextRev.'">'._('Next Change').'</a>';
}
echo '
</div>';
// Show the breadcrumbs
echo '
<p class="breadcrumbs">
'.getBreadcrumbs($_GET['file']).'
</p>';
// Show file details
echo '
<div id="revision-info">
<p>'.sprintf(_('Revision %d'), $logs[$id]['version']).' '.sprintf(_('(checked in by %s, %s)'), $logs[$id]['author'], $date).'</p>
<p>'.$logs[$id]['comment'].'</p>
</div>
<div id="source-code-header">
<b>'._('Line').'</b>
</div>';
echo $geshi->parse_code();
//-----------------------------------------------
// Default Directory
//-----------------------------------------------
} else {
$files = $svn->getDirectoryFiles();
}
//-----------------------------------------------
// Show the Directory Listing
//-----------------------------------------------
if ($show_dirs) {
$h2 = _('Source Code');
if (isset($_GET['directory'])) {
$h2 = $_GET['directory'];
}
// Sort by directories first, then files
$files = msort($files, 'path', true);
$files = dirSort($files);
echo '
<h2>'.$h2.'</h2><br/>
<table id="code" class="data">
<thead>
<tr>
<th class="file_link">'._('Directory').'</th>
<th>'._('Last Updated').'</th>
<th>'._('Last Change').'</th>
</tr>
</thead>
<tbody>';
if (isset($_GET['directory'])) {
$pos = strrpos($_GET['directory'], '/');
if ($pos === false) {
$up = 'code.php';
} else {
$up = '?directory='.substr($_GET['directory'], 0, $pos);
}
echo '
<tr>
<td><a class="up" href="'.$up.'">'._('up a level').'</a></td>
<td> </td>
<td> </td>
</tr>';
}
$first = true;
foreach ($files as $file) {
if ($file['status'] == 'HTTP/1.1 200 OK' && !$first) {
$link = strrchr($file['path'], "/");
if ($link) {
$link = substr($link, 1);
} else {
$link = $file['path'];
}
$comment = '';
$size = '';
if ($file['type'] == 'file') {
$logs = $svn->getFileLogs($file['path']);
$id = count($logs) - 1;
if ($id > 0) {
$comment = $logs[$id]['comment'];
}
if (strlen($comment) > 100) {
$comment = substr($comment, 0, 100).'...';
}
}
echo '
<tr>
<td><a class="'.$file['type'].'" href="?'.$file['type'].'='.$file['path'].'">'.$link.'</a></td>
<td>'.date('Y-m-d', strtotime($file['last-mod'])).'</td>
<td class="last_change">'.$comment.'</td>
</tr>';
}
$first = false;
}
echo '
</tbody>
</table>';
}
include($theme.'footer.php');
// TODO
// Move these to the inc/util file
function dirSort($arr)
{
$top = array();
$count = count($arr);
for ($i = 0; $i < $count; $i++) {
if ($arr[$i]['type'] == 'directory') {
$top[$i+$count] = $arr[$i];
unset($arr[$i]);
}
}
return array_merge($top, $arr);
}
function getBreadcrumbs ($path)
{
global $mainUrl;
$crumbs = explode("/",$path);
if (!isset($url)) {
$url = 'code.php';
}
$out = '<a href="'.$url.'">'._('root').'</a> / ';
$url = $mainUrl."?directory=";
for ($i=0; $i < count($crumbs)-1; $i++) {
$link = $crumbs[$i];
if ($i > 0) {
$url .= '/';
}
$url .= $crumbs[$i];
$out .= '<a href="'.$url.'">'.$link.'</a> / ';
}
$out .= '<span>'.$crumbs[$i].'</span>';
return $out;
}
function msort($array, $id="id", $sort_ascending=true)
{
$temp_array = array();
while (count($array) > 0) {
$lowest_id = 0;
$index=0;
foreach ($array as $item) {
if (isset($item[$id])) {
if ($array[$lowest_id][$id]) {
if (strtolower($item[$id]) < strtolower($array[$lowest_id][$id])) {
$lowest_id = $index;
}
}
}
$index++;
}
$temp_array[] = $array[$lowest_id];
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
}
if ($sort_ascending) {
return $temp_array;
} else {
return array_reverse($temp_array);
}
}