final move of files
[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
51         $crumbs = MTrackSCM::makeBreadcrumbs($this->pi);
52         
53         print_R($this->pi);
54         
55         if (!strlen($this->pi) || $this->pi == '/') {
56             throw new Exception("invalid path /");
57         } 
58         $this->repo = MTrackSCM::factory($this->pi);
59
60         if (!$this->repo) {
61           throw new Exception("invalid path {$this->pi}");
62         }
63         
64         MTrackACL::requireAllRights("repo:{$this->repo->repoid}", 'read');
65
66         $this->file =  isset($_GET['rev']) ? 
67             $this->repo->file($this->pi, 'rev', $_GET['rev']) :  
68             $this->repo->file($this->pi);
69
70         //echo '<PRE>'; print_r($this->file);    exit;
71             
72         $this->ent = $this->file->getChangeEvent();
73
74         
75         if (!empty($_GET['raw'])) {
76              //should it add an image header..
77             require_once 'File/MimeType.php';
78             $y = new File_MimeType();
79             $this->mimetype = $y->fromFilename($this->file->name);
80  
81             $filename = basename($this->pi);
82             header("Content-Type: {$this->mimetype}; name=\"{$filename}\"");
83             if ($_GET['raw'] == 'download') {
84                 header("Content-Disposition: attachment; filename=\"{$filename}\"");
85             }
86             
87             fpassthru($this->file->cat());
88             exit;
89         }
90         
91         $this->jump = empty($_GET['jump']) ? '' : $_GET['jump'];
92         
93         
94         
95        
96         $this->crumbs = array();
97         $this->basename  = array_pop($crumbs);
98         $location = '';
99         array_unshift($crumbs, '');
100         foreach($crumbs as $path) 
101         {
102             if (count($this->crumbs) && !strlen($path)) {
103                 continue;
104             }
105             $c = new StdClass;
106             $c->name = strlen($path) ? $path : '[root]';
107             $location .=  strlen($location) ? '/' : '';
108             $location .=  strlen($path)  ?  urlencode($path)  : '';
109             $c->location = $location;
110             $this->crumbs[] = $c;
111         }
112
113         
114                 
115         $finfo = pathinfo($this->file->name);
116         $t = tmpfile();
117
118         $data = $this->file->cat();
119 //       echo '<PRE>'. htmlspecialchars(stream_get_contents($data));exit;
120
121         stream_copy_to_stream($data, $t);
122         $data = null;
123         
124         // we have copied the data to a file.. $t...
125         $info = fstat($t);
126
127         $location = stream_get_meta_data($t);
128         
129         $location = $location['uri'];
130         //print_r($location);
131         
132         require_once 'File/MimeType.php';
133         $y = new File_MimeType();
134         $this->mimetype = $y->fromFilename($this->file->name);
135
136         
137         //$this->mimetype = mtrack_mime_detect($location, $this->file->name);
138         
139         
140         list($major) = explode('/', $this->mimetype, 2);
141
142         // Obscure-ish special cases for mime types;
143         // some .y files look like old image format data
144         switch ($this->mimetype) {
145             case 'image/x-3ds':
146             case 'application/xml':
147             case 'application/javascript':
148             case 'application/x-httpd-php':
149                 $major = 'text';
150                 break;
151             
152         }
153         // some special cases:
154         // we would be better to do if octect/stream to use the file tool.
155         
156         switch(basename($this->file->name)) {
157             case '.gitignore':
158             case 'README':
159             case 'LICENSE':
160             case '.hgignore':
161                 $major = 'text';
162                 break;
163            }
164         
165
166         // simple cases first..
167         switch ($major) {
168             case 'image':
169                 $this->majorImage = true;
170                 break;
171                 
172             case 'text':
173                 fseek($t, 0);
174                 
175                 $this->ann = array_values($this->file->annotate());
176                // echo '<PRE>';print_R($this->ann);exit;
177                 if ($this->ann === 'DELETED') {
178                     $this->deleted = true;
179                     $this->ann = false;
180                     break;
181                 }
182                 $this->tmpfile = $t; 
183                 
184                 $this->nlines = count($this->ann);
185                 require_once 'MTrack/SyntaxHighlight.php';
186                 $this->data =  MTrack_SyntaxHighlight::highlightSource(
187                         stream_get_contents($this->tmpfile), null, $this->file->name
188                     );
189                    // var_dump($this->data);
190     
191             default:
192                 break; // download only..
193         }
194       
195        
196       // 
197         //$this->renderEvents();
198     }
199     
200     function schemeSelect() // FIXME... we should use JS highligher.. not PHP one...
201     {
202         if (!$this->data) {
203            return;
204         }
205         require_once 'MTrack/SyntaxHighlight.php';
206         return MTrack_SyntaxHighlight::getSchemeSelect();
207     }
208    
209    
210     
211     
212 }
213     
214
215  
216     
217
218