* @copyright (cc) creative commons - attribution-shareAlike 3.0 unported * @version 1.0 * @package app * @subpackage controllers */ class code extends controller { /** * index function * depending on the url this function can: * - display all the git repositories * - display trees/blobs * - display commit history * - export zip/tarballs of files */ public function index() { $html["title"] = 'code'; $html["meta"] = ''; $html["sidebar"] = $this->view("blog/sidebar_qr", array(), true); $html["selected"] = 'code'; $html["script"] = ''; $html["body"] = ''; $urls = library::catalog()->url; $cm = $this->model("codeModel"); if(!isset($urls[1])) { //---list all repos $repos = $cm->getRepos(); if(!isset($repos[0])) { $html["body"] = 'No git repositories found...'; } else { foreach ($repos as $repo) { $html["body"] .= '
'; } } } else { //---decode url $repo = (isset($urls[1])) ? filter_var($urls[1], FILTER_SANITIZE_URL) : ''; $branch = (isset($urls[2])) ? filter_var($urls[2], FILTER_SANITIZE_URL) : 'master'; $commit = (isset($urls[3])) ? filter_var($urls[3], FILTER_SANITIZE_URL) : ''; $type = (isset($urls[4])) ? filter_var($urls[4], FILTER_SANITIZE_URL) : 'tree'; $objid = (isset($urls[5])) ? filter_var($urls[5], FILTER_SANITIZE_URL) : ''; $export = (isset($urls[6])) ? filter_var($urls[6], FILTER_SANITIZE_URL) : ''; $commitName = ''; if($commit == 'history') { $type = 'history'; } //---get info from db $repoInfo = $cm->getRepo($repo); if(!isset($repoInfo[0])) { throw new Exception("unknown repository", statusCodes::HTTP_NOT_FOUND); } //---load GLiP $this->library(qoob_types::utility, 'git'); $this->git->init(QOOB_ROOT.'/repos/'.$repoInfo[0]["repo"]); //---check branch name if(!in_array($branch, $this->git->branches)) { throw new Exception("unknown branch", statusCodes::HTTP_NOT_FOUND); } //---html branch select $branches = ''; //---list history $gitcommit = $this->git->getTip($branch); $obj = $this->git->getObject($gitcommit); $hist = $obj->getHistory(); $hist = array_reverse($hist); $found = false; $commits = ' view history'; //---check commit id if($commit != '' && $commit != 'history') { if(!$found) { throw new Exception("unknown commit id", statusCodes::HTTP_NOT_FOUND); } } $dl = QOOB_DOMAIN.'code/'.$urls[1].'/'.$branch.'/'.$commitName.'/archive/'; $fileName = ($type == 'tree') ? 'Files' : 'File'; if($type == 'tree') { if($objid != '') { $tree = $this->git->getObject(sha1_bin($objid)); $dl .= $objid; } else { if($commit != '') { $gitcommit = $this->git->getObject(sha1_bin($commit)); $tree = $gitcommit->getTree(); $dl .= sha1_hex($tree->name); } else { $gitcommit = $this->git->getObject(sha1_bin($commitName)); $tree = $gitcommit->getTree(); $dl .= sha1_hex($tree->name); } } $fileName = 'Files'; $url = QOOB_DOMAIN.'code/'.$urls[1].'/'.$branch.'/'.$commitName.'/'; $ord = array(); foreach ($tree->nodes as $file) { if($file->is_dir) { $ord[] = '/'.$file->name.'/'; } } foreach ($tree->nodes as $file) { if(!$file->is_dir) { $ord[] = ' '.$file->name.''; } } $files = ''; foreach ($ord as $file) { $files .= $file.'
'; } $files .= '
'; $gitcommit = $this->git->getObject(sha1_bin($commit)); $tree = $gitcommit->getTree()->listRecursive(); $langs = array('cgi', 'pl', 'cs', 'xml', 'xhtml', 'xslt', 'html', 'htm', 'mxml', 'sql', 'mysql', 'mssql', 'db', 'php', 'php5', 'as3', 'as', 'css', 'js'); foreach ($tree as $name => $sha) { if(sha1_hex($sha) == $objid) { $fileName = 'File: '.$name; $ext = substr(strrchr($name,'.'),1); } } $wrapper = (in_array($ext, $langs)) ? ''; $html["script"] = $this->view("gitJS", array('URL' => QOOB_DOMAIN.'code/'.library::catalog()->url[1].'/'), true); } $post = array( 'mainCat' => '', 'url' => '', 'title' => 'Code', 'subtitle' => 'GIT repositories', 'content' => $html["body"], 'comments' => 0 ); $html["body"] = $this->view("post", $post, true); $this->view("pixelgraff", $html); } } ?>' : ''; $blob = $this->git->getObject(sha1_bin($objid)); if(is_ascii($blob->data) ) { $files = $wrapper.htmlentities($blob->data).''; $html['jsfiles'] = ''.PHP_EOL.''.PHP_EOL.''.PHP_EOL; } else { switch($ext) { case 'jpg': $files = ''; break; case 'png': $files = '
'; break; case 'gif': $files = '
'; break; case 'bmp': $files = '
'; break; default: $files = '
This file is not ascii formatted, so cannot be displayed. sorry :(
'; break; } } } else if($type == 'archive') { if($export == 'zip' || $export == 'tarball') { $archive = $this->git->archive($repoInfo[0]["repo"].'-'.substr($objid, 0, 7), QOOB_ROOT.'/repos/'.$repoInfo[0]["repo"], $objid, $export); die($archive); } else { throw new Exception("unknown compression method", statusCodes::HTTP_NOT_FOUND); } } else { $fileName = 'History'; $files = '
clone the git repo and get a copy.
'; foreach ($hist as $event) { $files .= 'sha1: '.sha1_hex($event->name).'
committer: '.$event->committer->name.' <'.$event->committer->email.'>
date: '.date('m/d/y', $event->committer->time).'
'.wordwrap($event->summary, 90).'
'; } $files .= '
'; } $html["body"] .= '