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
13 var $template = 'file.html';
23 require_once 'MTrack/ACL.php';
24 MTrackACL::requireAllRights('Browser', 'read');
32 function get($pi = '')
35 if (!empty($_REQUEST['embed'])) {
36 $this->masterTemplate = $this->template;
39 $this->pi = '/'. $pi . $this->bootLoader->ext;
42 $data = explode('@', $this->pi);
44 if (isset($data[1])) {
45 // do not do this ... ;)
46 $_GET['rev'] = $data[1];
48 $this->repo = DB_DataObject::factory('repos');
49 $file = $this->repo->loadFromPath($this->pi);
50 if (empty($file) || $this->repo->id) {
51 throw new Exception("invalid path " . htmlspecialchars($this->pi) );
55 $crumbs = MTrackSCM::makeBreadcrumbs($this->pi);
59 if (!strlen($this->pi) || $this->pi == '/') {
60 throw new Exception("invalid path /");
62 $this->repo = DB_DataObject::factory('repos');
63 $this->repo->parent = $crumbs[1] == 'default' ? '' : 'user:' . $crumbs[0];
64 $this->repo->get('shortname', $crumbs[2]);
66 $dir_pi = implode('/', array_slice($crumbs,3));
70 throw new Exception("invalid path {$this->pi}");
73 MTrackACL::requireAllRights("repo:{$this->repo->repoid}", 'read');
75 $this->file = isset($_GET['rev']) ?
76 $this->repo->file($this->pi, 'rev', $_GET['rev']) :
77 $this->repo->file($this->pi);
79 //echo '<PRE>'; print_r($this->file); exit;
81 $this->ent = $this->file->getChangeEvent();
84 if (!empty($_GET['raw'])) {
85 //should it add an image header..
86 require_once 'File/MimeType.php';
87 $y = new File_MimeType();
88 $this->mimetype = $y->fromFilename($this->file->name);
90 $filename = basename($this->pi);
91 header("Content-Type: {$this->mimetype}; name=\"{$filename}\"");
92 if ($_GET['raw'] == 'download') {
93 header("Content-Disposition: attachment; filename=\"{$filename}\"");
96 fpassthru($this->file->cat());
100 $this->jump = empty($_GET['jump']) ? '' : $_GET['jump'];
105 $this->crumbs = array();
106 $this->basename = array_pop($crumbs);
108 array_unshift($crumbs, '');
109 foreach($crumbs as $path)
111 if (count($this->crumbs) && !strlen($path)) {
115 $c->name = strlen($path) ? $path : '[root]';
116 $location .= strlen($location) ? '/' : '';
117 $location .= strlen($path) ? urlencode($path) : '';
118 $c->location = $location;
119 $this->crumbs[] = $c;
124 $finfo = pathinfo($this->file->name);
127 $data = $this->file->cat();
128 // echo '<PRE>'. htmlspecialchars(stream_get_contents($data));exit;
130 stream_copy_to_stream($data, $t);
133 // we have copied the data to a file.. $t...
136 $location = stream_get_meta_data($t);
138 $location = $location['uri'];
139 //print_r($location);
141 require_once 'File/MimeType.php';
142 $y = new File_MimeType();
143 $this->mimetype = $y->fromFilename($this->file->name);
146 //$this->mimetype = mtrack_mime_detect($location, $this->file->name);
149 list($major) = explode('/', $this->mimetype, 2);
151 // Obscure-ish special cases for mime types;
152 // some .y files look like old image format data
153 switch ($this->mimetype) {
155 case 'application/xml':
156 case 'application/javascript':
157 case 'application/x-httpd-php':
162 // some special cases:
163 // we would be better to do if octect/stream to use the file tool.
165 switch(basename($this->file->name)) {
175 // simple cases first..
178 $this->majorImage = true;
184 $this->ann = array_values($this->file->annotate());
185 // echo '<PRE>';print_R($this->ann);exit;
186 if ($this->ann === 'DELETED') {
187 $this->deleted = true;
193 $this->nlines = count($this->ann);
194 require_once 'MTrack/SyntaxHighlight.php';
195 $this->data = MTrack_SyntaxHighlight::highlightSource(
196 stream_get_contents($this->tmpfile), null, $this->file->name
198 // var_dump($this->data);
201 break; // download only..
206 //$this->renderEvents();
209 function schemeSelect() // FIXME... we should use JS highligher.. not PHP one...
214 require_once 'MTrack/SyntaxHighlight.php';
215 return MTrack_SyntaxHighlight::getSchemeSelect();