Fixed hook & action calls.
[viewgit.git] / index.php
blobf4f781f1513b4b473d3bcfd4e712b5f9cc58a966
1 <?php
2 /** @file
3 * The main "controller" file of ViewGit.
5 * All requests come to this file. You can think of it as the controller in the
6 * Model-View-Controller pattern. It reads config, processes user input,
7 * fetches required data using git commandline, and finally passes the data to
8 * templates to be shown to the user.
9 */
10 error_reporting(E_ALL | E_STRICT);
12 require_once('inc/config.php');
13 require_once('inc/functions.php');
14 require_once('inc/plugins.php');
16 // Include all plugins
17 foreach (glob('plugins/*/main.php') as $plugin) {
18 require_once($plugin);
20 $parts = explode('/', $plugin);
21 $name = $parts[1];
23 $classname = "${name}plugin";
24 $inst = new $classname;
27 $old_error_handler = set_error_handler('vg_error_handler');
29 // Adjust error_reporting based on config.
30 if (!$conf['debug']) {
31 error_reporting(E_ALL ^ E_NOTICE);
34 if (isset($conf['auth_lib'])){
35 require_once("inc/auth_{$conf['auth_lib']}.php");
36 auth_check();
39 if (isset($conf['projects_glob'])) {
40 foreach ($conf['projects_glob'] as $glob) {
41 foreach (glob($glob) as $path) {
42 // Get the last part of the path before .git
43 $name = preg_replace(array('#/?\.git$#', '#^.*/#'), array('', ''), $path);
45 // Workaround against name collisions; proj, proj1, proj2, ...
46 $i = '';
47 while (in_array($name . $i, array_keys($conf['projects']))) {
48 @$i++;
50 $name = $name . $i;
51 $conf['projects'][$name] = array('repo' => $path);
56 $action = 'index';
57 $template = 'index';
58 $page['title'] = 'ViewGit';
60 if (isset($_REQUEST['a'])) {
61 $action = strtolower($_REQUEST['a']);
63 $page['action'] = $action;
66 * index - list of projects
68 if ($action === 'index') {
69 $template = 'index';
70 $page['title'] = 'List of projects - ViewGit';
72 foreach (array_keys($conf['projects']) as $p) {
73 $page['projects'][] = get_project_info($p);
78 * archive - send a tree as an archive to client
79 * @param p project
80 * @param h tree hash
81 * @param t type, "targz" or "zip"
82 * @param n OPTIONAL name suggestion
84 elseif ($action === 'archive') {
85 $project = validate_project($_REQUEST['p']);
86 $tree = validate_hash($_REQUEST['h']);
87 $type = $_REQUEST['t'];
89 $basename = "$project-tree-". substr($tree, 0, 7);
90 if (isset($_REQUEST['n'])) {
91 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
94 if ($type === 'targz') {
95 header("Content-Type: application/x-tar-gz");
96 header("Content-Transfer-Encoding: binary");
97 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
98 run_git_passthru($project, "archive --format=tar $tree |gzip");
100 elseif ($type === 'zip') {
101 header("Content-Type: application/x-zip");
102 header("Content-Transfer-Encoding: binary");
103 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
104 run_git_passthru($project, "archive --format=zip $tree");
106 else {
107 die('Invalid archive type requested');
110 die();
114 * blob - send a blob to browser with filename suggestion
115 * @param p project
116 * @param h blob hash
117 * @param n filename
119 elseif ($action === 'blob') {
120 $project = validate_project($_REQUEST['p']);
121 $hash = validate_hash($_REQUEST['h']);
122 $name = $_REQUEST['n'];
124 header('Content-type: application/octet-stream');
125 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
127 run_git_passthru($project, "cat-file blob $hash");
128 die();
132 * co - git checkout. These requests come from mod_rewrite, see the .htaccess file.
133 * @param p project
134 * @param r path
136 elseif ($action === 'co') {
137 if (!$conf['allow_checkout']) { die('Checkout not allowed'); }
139 // For debugging
140 debug("Project: $_REQUEST[p] Request: $_REQUEST[r]");
142 // eg. info/refs, HEAD
143 $p = validate_project($_REQUEST['p']); // project
144 $r = $_REQUEST['r']; // path
146 $gitdir = $conf['projects'][$p]['repo'];
147 $filename = $gitdir .'/'. $r;
149 // make sure the request is legit (no reading of other files besides those under git projects)
150 if ($r === 'HEAD' || $r === 'info/refs' || preg_match('!^objects/info/(packs|http-alternates|alternates)$!', $r) > 0 || preg_match('!^objects/[0-9a-f]{2}/[0-9a-f]{38}$!', $r) > 0) {
151 if (file_exists($filename)) {
152 debug('OK, sending');
153 readfile($filename);
154 } else {
155 debug('Not found');
156 header('HTTP/1.0 404 Not Found');
158 } else {
159 debug("Denied");
162 die();
166 * commit - view commit information
167 * @param p project
168 * @param h commit hash
170 elseif ($action === 'commit') {
171 $template = 'commit';
172 $page['project'] = validate_project($_REQUEST['p']);
173 $page['title'] = "$page[project] - Commit - ViewGit";
174 $page['commit_id'] = validate_hash($_REQUEST['h']);
176 $info = git_get_commit_info($page['project'], $page['commit_id']);
178 $page['author_name'] = $info['author_name'];
179 $page['author_mail'] = $info['author_mail'];
180 $page['author_datetime'] = gmstrftime($conf['datetime_full'], $info['author_utcstamp']);
181 $page['author_datetime_local'] = gmstrftime($conf['datetime_full'], $info['author_stamp']) .' '. $info['author_timezone'];
182 $page['committer_name'] = $info['committer_name'];
183 $page['committer_mail'] = $info['committer_mail'];
184 $page['committer_datetime'] = gmstrftime($conf['datetime_full'], $info['committer_utcstamp']);
185 $page['committer_datetime_local'] = gmstrftime($conf['datetime_full'], $info['committer_stamp']) .' '. $info['committer_timezone'];
186 $page['tree_id'] = $info['tree'];
187 $page['parents'] = $info['parents'];
188 $page['message'] = $info['message'];
189 $page['message_firstline'] = $info['message_firstline'];
190 $page['message_full'] = $info['message_full'];
195 * commitdiff - view diff of a commit
196 * @param p project
197 * @param h commit hash
199 elseif ($action === 'commitdiff') {
200 $template = 'commitdiff';
201 $page['project'] = validate_project($_REQUEST['p']);
202 $page['title'] = "$page[project] - Commitdiff - ViewGit";
203 $hash = validate_hash($_REQUEST['h']);
204 $page['commit_id'] = $hash;
206 $info = git_get_commit_info($page['project'], $hash);
208 $page['tree_id'] = $info['tree'];
210 $page['message'] = $info['message'];
211 $page['message_firstline'] = $info['message_firstline'];
212 $page['message_full'] = $info['message_full'];
213 $page['author_name'] = $info['author_name'];
214 $page['author_mail'] = $info['author_mail'];
215 $page['author_datetime'] = gmstrftime($conf['datetime'], $info['author_utcstamp']);
217 $text = git_diff($page['project'], "$hash^", $hash);
218 list($page['files'], $page['diffdata']) = format_diff($text);
219 //$page['diffdata'] = format_diff($text);
222 elseif ($action === 'patch') {
223 $project = validate_project($_REQUEST['p']);
224 $hash = validate_hash($_REQUEST['h']);
225 $filename = "$project-". substr($hash, 0, 7) .".patch";
227 //header("Content-Type: text/x-diff");
228 header("Content-Type: application/octet-stream");
229 header("Content-Transfer-Encoding: binary");
230 // TODO git-style filename
231 header("Content-Disposition: attachment; filename=\"$filename\";");
233 run_git_passthru($project, "format-patch --stdout $hash^..$hash");
234 die();
238 * rss-log - RSS feed of project changes
239 * @param p project
241 elseif ($action === 'rss-log') {
242 $page['project'] = validate_project($_REQUEST['p']);
244 $ext_url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) .'/';
246 $page['rss_title'] = "Log for $page[project]";
247 $page['rss_link'] = $ext_url . makelink(array('a' => 'summary', 'p' => $page['project']));
248 $page['rss_description'] = "Git log for project $page[project], generated by ViewGit.";
249 $page['rss_pubDate'] = rss_pubdate(time());
250 $page['rss_ttl'] = $conf['rss_ttl'];
252 $page['rss_items'] = array();
254 $diffstat = strstr($conf['rss_item_description'], '{DIFFSTAT}');
256 $revs = git_get_rev_list($page['project'], $conf['rss_max_items']);
257 foreach ($revs as $rev) {
258 $info = git_get_commit_info($page['project'], $rev);
259 $link = $ext_url . makelink(array('a' => 'commit', 'p' => $page['project'], 'h' => $rev));
260 if ($diffstat) {
261 $info['diffstat'] = git_diffstat($page['project'], $rev);
264 $page['rss_items'][] = array(
265 'title' => rss_item_format($conf['rss_item_title'], $info),
266 'guid' => $link,
267 'link' => $link,
268 'description' => rss_item_format($conf['rss_item_description'], $info),
269 'pubdate' => rss_pubdate($info['author_stamp']),
273 require('templates/rss.php');
274 die();
278 * search - search project history
279 * @param p project
280 * @param st search type: commit,grep,author,committer,pickaxe
281 * @param s string to search for
283 elseif ($action === 'search') {
284 $template = 'shortlog';
286 $page['project'] = validate_project($_REQUEST['p']);
288 $info = git_get_commit_info($page['project']);
289 $page['commit_id'] = $info['h'];
290 $page['tree_id'] = $info['tree'];
292 $type = $_REQUEST['st'];
293 $string = $_REQUEST['s'];
295 $page['search_t'] = $type;
296 $page['search_s'] = $string;
298 $commits = git_search_commits($page['project'], $type, $string);
299 $shortlog = array();
300 foreach ($commits as $c) {
301 $info = git_get_commit_info($page['project'], $c);
302 $shortlog[] = array(
303 'author' => $info['author_name'],
304 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
305 'message' => $info['message'],
306 'commit_id' => $info['h'],
307 'tree' => $info['tree'],
308 'refs' => array(),
311 $page['shortlog'] = $shortlog;
315 * shortlog - project shortlog entries
316 * @param p project
317 * @param h OPTIONAL commit id to start showing log from
319 elseif ($action === 'shortlog') {
320 $template = 'shortlog';
321 $page['project'] = validate_project($_REQUEST['p']);
322 $page['title'] = "$page[project] - Shortlog - ViewGit";
323 if (isset($_REQUEST['h'])) {
324 $page['ref'] = validate_hash($_REQUEST['h']);
325 } else {
326 $page['ref'] = 'HEAD';
329 $info = git_get_commit_info($page['project'], $page['ref']);
330 $page['commit_id'] = $info['h'];
331 $page['tree_id'] = $info['tree'];
333 $page['shortlog'] = handle_shortlog($page['project'], $page['ref']);
335 elseif ($action === 'summary') {
336 $template = 'summary';
337 $page['project'] = validate_project($_REQUEST['p']);
338 $page['title'] = "$page[project] - Summary - ViewGit";
340 $info = git_get_commit_info($page['project']);
341 $page['commit_id'] = $info['h'];
342 $page['tree_id'] = $info['tree'];
344 $page['shortlog'] = handle_shortlog($page['project']);
346 $page['tags'] = handle_tags($page['project'], $conf['summary_tags']);
348 $heads = git_get_heads($page['project']);
349 $page['heads'] = array();
350 foreach ($heads as $h) {
351 $info = git_get_commit_info($page['project'], $h['h']);
352 $page['heads'][] = array(
353 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
354 'h' => $h['h'],
355 'fullname' => $h['fullname'],
356 'name' => $h['name'],
360 elseif ($action === 'tags') {
361 $template = 'tags';
362 $page['project'] = validate_project($_REQUEST['p']);
363 $page['title'] = "$page[project] - Tags - ViewGit";
365 $info = git_get_commit_info($page['project']);
366 $page['commit_id'] = $info['h'];
367 $page['tree_id'] = $info['tree'];
369 $page['tags'] = handle_tags($page['project']);
372 * Shows a tree, with list of directories/files, links to them and download
373 * links to archives.
375 * @param p project
376 * @param h tree hash
377 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
378 * one denotes which commit the user navigated from)
379 * @param f OPTIONAL path the user has followed to view this tree
381 elseif ($action === 'tree') {
382 $template = 'tree';
383 $page['project'] = validate_project($_REQUEST['p']);
384 if (isset($_REQUEST['h'])) {
385 $page['tree_id'] = validate_hash($_REQUEST['h']);
388 else {
389 // TODO walk the tree
390 $page['tree_id'] = 'HEAD';
393 $page['title'] = "$page[project] - Tree - ViewGit";
395 // 'hb' optionally contains the commit_id this tree is related to
396 if (isset($_REQUEST['hb'])) {
397 $page['commit_id'] = validate_hash($_REQUEST['hb']);
399 else {
400 // for the header
401 $info = git_get_commit_info($page['project']);
402 $page['commit_id'] = $info['h'];
405 $page['path'] = '';
406 if (isset($_REQUEST['f'])) {
407 $page['path'] = $_REQUEST['f']; // TODO validate?
410 // get path info for the header
411 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
412 if (!isset($page['tree_id'])) {
413 // Take the last hash from the tree
414 if (count($page['pathinfo']) > 0) {
415 $page['tree_id'] = $page['pathinfo'][count($page['pathinfo']) - 1]['hash'];
416 } else {
417 $page['tree_id'] = 'HEAD';
421 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
424 * View a blob as inline, embedded on the page.
425 * @param p project
426 * @param h blob hash
427 * @param hb OPTIONAL base commit
429 elseif ($action === 'viewblob') {
430 $template = 'blob';
431 $page['project'] = validate_project($_REQUEST['p']);
432 $page['hash'] = validate_hash($_REQUEST['h']);
433 $page['title'] = "$page[project] - Blob - ViewGit";
434 if (isset($_REQUEST['hb'])) {
435 $page['commit_id'] = validate_hash($_REQUEST['hb']);
437 else {
438 $page['commit_id'] = 'HEAD';
441 $page['path'] = '';
442 if (isset($_REQUEST['f'])) {
443 $page['path'] = $_REQUEST['f']; // TODO validate?
446 // For the header's pagenav
447 $info = git_get_commit_info($page['project'], $page['commit_id']);
448 $page['commit_id'] = $info['h'];
449 $page['tree_id'] = $info['tree'];
451 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
453 $page['data'] = join("\n", run_git($page['project'], "cat-file blob $page[hash]"));
455 // GeSHi support
456 if ($conf['geshi'] && strpos($page['path'], '.')) {
457 $old_mask = error_reporting(E_ALL ^ E_NOTICE);
458 require_once($conf['geshi_path']);
459 $ext = array_pop(explode('.', $page['path']));
460 $lang = Geshi::get_language_name_from_extension($ext);
461 if (strlen($lang) > 0) {
462 $geshi =& new Geshi($page['data'], $lang);
463 if (is_int($conf['geshi_line_numbers'])) {
464 if ($conf['geshi_line_numbers'] == 0) {
465 $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
467 else {
468 $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, $conf['geshi_line_numbers']);
471 $page['html_data'] = $geshi->parse_code();
473 error_reporting($old_mask);
476 elseif (in_array($action, array_keys(VGPlugin::$plugin_actions))) {
477 VGPlugin::$plugin_actions[$action]->action($action);
478 die();
480 else {
481 die('Invalid action');
484 require 'templates/header.php';
485 require "templates/$template.php";
486 require 'templates/footer.php';