1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
5 // Browse.php - only for rendering the body..
6 // Tree.php - the actually tree..
8 require_once 'MTrackWeb.php';
10 class MTrackWeb_File extends MTrackWeb
30 function get($pi = '')
32 if (!isset($_REQUEST['ajax_body'])) {
35 $this->masterTemplate = 'file.html';
37 $this->pi = $pi . (strlen($pi) ? $this->bootLoader->ext : '');
42 $this->repo = DB_DataObject::factory('mtrack_repos');
43 $this->filename = $this->repo->loadFromPath($this->pi);
44 if (!$this->repo->id) {
45 throw new Exception("invalid path {$this->pi}");
48 if (!$this->projectPerm($this->repo->project_id, 'MTrack.Repos', 'S')) {
49 return HTML_FlexyFramework::run('Noperm'); // noperm = loggedin -> need more perms / not.. try loggin in..
53 if (empty($this->filename) || !$this->repo->id) {
54 throw new Exception("invalid path " . htmlspecialchars($this->pi) );
58 throw new Exception("invalid path {$this->pi}");
61 $this->file = isset($_GET['rev']) ?
62 $this->repo->file($this->filename, 'rev', $_GET['rev']) :
63 $this->repo->file($this->filename);
65 //echo '<PRE>'; print_r($this->file); exit;
67 $this->ent = $this->file->getChangeEvent();
70 if (!empty($_GET['raw'])) {
71 //should it add an image header..
72 require_once 'File/MimeType.php';
73 $y = new File_MimeType();
74 $this->mimetype = $y->fromFilename($this->file->name);
76 $filename = basename($this->filename);
77 header("Content-Type: {$this->mimetype}; name=\"{$filename}\"");
78 if ($_GET['raw'] == 'download') {
79 header("Content-Disposition: attachment; filename=\"{$filename}\"");
82 fpassthru($this->file->cat());
86 $this->jump = empty($_GET['jump']) ? '' : $_GET['jump'];
91 $this->crumbs = array();
92 $this->basename = basename($this->filename);
94 $crumbs = dirname($this->filename) == '.' ? array() : explode('/', dirname($this->filename));
99 foreach($crumbs as $path)
101 if (count($this->crumbs) && !strlen($path)) {
106 $location .= strlen($location) ? '/' : '';
107 $location .= strlen($path) ? urlencode($path) : '';
108 $c->location = $location;
109 $this->crumbs[] = $c;
113 $finfo = pathinfo($this->file->name);
116 $data = $this->file->cat();
117 // echo '<PRE>'. htmlspecialchars(stream_get_contents($data));exit;
119 stream_copy_to_stream($data, $t);
122 // we have copied the data to a file.. $t...
125 $location = stream_get_meta_data($t);
127 $location = $location['uri'];
128 //print_r($location);
130 require_once 'File/MimeType.php';
131 $y = new File_MimeType();
132 $this->mimetype = $y->fromFilename($this->file->name);
133 $ext = strtolower(array_pop(explode('.', $this->file->name)));
135 //$this->mimetype = mtrack_mime_detect($location, $this->file->name);
138 list($major) = explode('/', $this->mimetype, 2);
148 // Obscure-ish special cases for mime types;
149 // some .y files look like old image format data
150 switch ($this->mimetype) {
152 case 'application/xml':
153 case 'application/javascript':
154 case 'application/x-httpd-php':
159 // some special cases:
160 // we would be better to do if octect/stream to use the file tool.
162 switch(basename($this->file->name)) {
172 // simple cases first..
175 $this->majorImage = true;
181 // this should be a json query..
182 //$this->ann = array_values($this->file->annotate());
183 // echo '<PRE>';print_R($this->ann);exit;
184 if (!empty($_REQUEST['annotate'])) {
186 $ann = array_values($this->file->annotate());
190 $this->ann = array();
191 if ($this->ann === 'DELETED') {
192 $this->deleted = true;
199 require_once 'MTrack/SyntaxHighlight.php';
200 $contents = stream_get_contents($this->tmpfile);
201 $this->nlines = count(explode("\n", $contents));
203 if (!empty($_REQUEST['markup'])) {
204 // we do a late version call for markup..
205 echo MTrack_SyntaxHighlight::highlightSource(
206 $contents , null, $this->file->name
213 $this->data = htmlspecialchars($contents); /* MTrack_SyntaxHighlight::highlightSource(
214 $contents , null, $this->file->name
217 //var_dump($this->data);
218 for ($i =0; $i < $this->nlines; $i++ ) {
219 $this->ann[] = (object) array(
229 break; // download only..
231 if (!empty($_REQUEST['markup'])) {
232 $this->jerr("invalid request");
236 //$this->renderEvents();
239 function schemeSelect() // FIXME... we should use JS highligher.. not PHP one...
244 require_once 'MTrack/SyntaxHighlight.php';
245 return MTrack_SyntaxHighlight::getSchemeSelect();