MTrackWeb/File.php
[web.mtrack] / MTrackWeb / File.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4
5 // Browse.php - only for rendering the body..
6 // Tree.php - the actually tree..
7
8 require_once 'MTrackWeb.php';
9  
10 class MTrackWeb_File extends MTrackWeb
11 {
12     
13     var $template = 'file.html';
14  
15     var $object;
16     var $ident;
17     
18     var $repo;
19  
20     function getAuth() 
21     {
22         parent::getAuth();
23         require_once 'MTrack/ACL.php';
24         MTrackACL::requireAllRights('Browser', 'read');
25         return true;
26   
27     }
28  
29     
30   
31  
32     function get($pi = '')
33     {
34         
35         if (!empty($_REQUEST['embed'])) {
36             $this->masterTemplate = $this->template;
37          }
38         
39         $this->pi =  $pi . $this->bootLoader->ext;
40
41         // very svn;y...
42         $data = explode('@', $this->pi);
43         $this->pi = $data[0];
44         if (isset($data[1])) {
45             // do not do this ... ;)
46             $_GET['rev'] = $data[1];
47         }
48         
49         
50         $this->repo = DB_DataObject::factory('repos');
51         $file = $this->repo->loadFromPath($this->pi);
52         
53         if (empty($file) || $this->repo->id) {
54             throw new Exception("invalid path " . htmlspecialchars($this->pi) );
55         }
56  
57         if (!$this->repo) {
58           throw new Exception("invalid path {$this->pi}");
59         }
60         
61         MTrackACL::requireAllRights("repo:{$this->repo->repoid}", 'read');
62
63         $this->file =  isset($_GET['rev']) ? 
64             $this->repo->file($file, 'rev', $_GET['rev']) :  
65             $this->repo->file($$file);
66
67         //echo '<PRE>'; print_r($this->file);    exit;
68             
69         $this->ent = $this->file->getChangeEvent();
70
71         
72         if (!empty($_GET['raw'])) {
73              //should it add an image header..
74             require_once 'File/MimeType.php';
75             $y = new File_MimeType();
76             $this->mimetype = $y->fromFilename($this->file->name);
77  
78             $filename = basename($this->pi);
79             header("Content-Type: {$this->mimetype}; name=\"{$filename}\"");
80             if ($_GET['raw'] == 'download') {
81                 header("Content-Disposition: attachment; filename=\"{$filename}\"");
82             }
83             
84             fpassthru($this->file->cat());
85             exit;
86         }
87         
88         $this->jump = empty($_GET['jump']) ? '' : $_GET['jump'];
89         
90         
91         
92        
93         $this->crumbs = array();
94         $this->basename  = array_pop($crumbs);
95         $location = '';
96         array_unshift($crumbs, '');
97         foreach($crumbs as $path) 
98         {
99             if (count($this->crumbs) && !strlen($path)) {
100                 continue;
101             }
102             $c = new StdClass;
103             $c->name = strlen($path) ? $path : '[root]';
104             $location .=  strlen($location) ? '/' : '';
105             $location .=  strlen($path)  ?  urlencode($path)  : '';
106             $c->location = $location;
107             $this->crumbs[] = $c;
108         }
109
110         
111                 
112         $finfo = pathinfo($this->file->name);
113         $t = tmpfile();
114
115         $data = $this->file->cat();
116 //       echo '<PRE>'. htmlspecialchars(stream_get_contents($data));exit;
117
118         stream_copy_to_stream($data, $t);
119         $data = null;
120         
121         // we have copied the data to a file.. $t...
122         $info = fstat($t);
123
124         $location = stream_get_meta_data($t);
125         
126         $location = $location['uri'];
127         //print_r($location);
128         
129         require_once 'File/MimeType.php';
130         $y = new File_MimeType();
131         $this->mimetype = $y->fromFilename($this->file->name);
132
133         
134         //$this->mimetype = mtrack_mime_detect($location, $this->file->name);
135         
136         
137         list($major) = explode('/', $this->mimetype, 2);
138
139         // Obscure-ish special cases for mime types;
140         // some .y files look like old image format data
141         switch ($this->mimetype) {
142             case 'image/x-3ds':
143             case 'application/xml':
144             case 'application/javascript':
145             case 'application/x-httpd-php':
146                 $major = 'text';
147                 break;
148             
149         }
150         // some special cases:
151         // we would be better to do if octect/stream to use the file tool.
152         
153         switch(basename($this->file->name)) {
154             case '.gitignore':
155             case 'README':
156             case 'LICENSE':
157             case '.hgignore':
158                 $major = 'text';
159                 break;
160            }
161         
162
163         // simple cases first..
164         switch ($major) {
165             case 'image':
166                 $this->majorImage = true;
167                 break;
168                 
169             case 'text':
170                 fseek($t, 0);
171                 
172                 $this->ann = array_values($this->file->annotate());
173                // echo '<PRE>';print_R($this->ann);exit;
174                 if ($this->ann === 'DELETED') {
175                     $this->deleted = true;
176                     $this->ann = false;
177                     break;
178                 }
179                 $this->tmpfile = $t; 
180                 
181                 $this->nlines = count($this->ann);
182                 require_once 'MTrack/SyntaxHighlight.php';
183                 $this->data =  MTrack_SyntaxHighlight::highlightSource(
184                         stream_get_contents($this->tmpfile), null, $this->file->name
185                     );
186                    // var_dump($this->data);
187     
188             default:
189                 break; // download only..
190         }
191       
192        
193       // 
194         //$this->renderEvents();
195     }
196     
197     function schemeSelect() // FIXME... we should use JS highligher.. not PHP one...
198     {
199         if (!$this->data) {
200            return;
201         }
202         require_once 'MTrack/SyntaxHighlight.php';
203         return MTrack_SyntaxHighlight::getSchemeSelect();
204     }
205    
206    
207     
208     
209 }
210     
211
212  
213     
214
215