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->repoid) {
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($file);
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         $crumbs = explode('/', $file);
97         array_unshift($crumbs, ''); // add empty at front..
98         foreach($crumbs as $path) 
99         {
100             if (count($this->crumbs) && !strlen($path)) {
101                 continue;
102             }
103             $c = new StdClass;
104             $c->name = strlen($path) ? $path : '[root]';
105             $location .=  strlen($location) ? '/' : '';
106             $location .=  strlen($path)  ?  urlencode($path)  : '';
107             $c->location = $location;
108             $this->crumbs[] = $c;
109         }
110
111         
112                 
113         $finfo = pathinfo($this->file->name);
114         $t = tmpfile();
115
116         $data = $this->file->cat();
117 //       echo '<PRE>'. htmlspecialchars(stream_get_contents($data));exit;
118
119         stream_copy_to_stream($data, $t);
120         $data = null;
121         
122         // we have copied the data to a file.. $t...
123         $info = fstat($t);
124
125         $location = stream_get_meta_data($t);
126         
127         $location = $location['uri'];
128         //print_r($location);
129         
130         require_once 'File/MimeType.php';
131         $y = new File_MimeType();
132         $this->mimetype = $y->fromFilename($this->file->name);
133
134         
135         //$this->mimetype = mtrack_mime_detect($location, $this->file->name);
136         
137         
138         list($major) = explode('/', $this->mimetype, 2);
139
140         // Obscure-ish special cases for mime types;
141         // some .y files look like old image format data
142         switch ($this->mimetype) {
143             case 'image/x-3ds':
144             case 'application/xml':
145             case 'application/javascript':
146             case 'application/x-httpd-php':
147                 $major = 'text';
148                 break;
149             
150         }
151         // some special cases:
152         // we would be better to do if octect/stream to use the file tool.
153         
154         switch(basename($this->file->name)) {
155             case '.gitignore':
156             case 'README':
157             case 'LICENSE':
158             case '.hgignore':
159                 $major = 'text';
160                 break;
161            }
162         
163
164         // simple cases first..
165         switch ($major) {
166             case 'image':
167                 $this->majorImage = true;
168                 break;
169                 
170             case 'text':
171                 fseek($t, 0);
172                 
173                 $this->ann = array_values($this->file->annotate());
174                // echo '<PRE>';print_R($this->ann);exit;
175                 if ($this->ann === 'DELETED') {
176                     $this->deleted = true;
177                     $this->ann = false;
178                     break;
179                 }
180                 $this->tmpfile = $t; 
181                 
182                 $this->nlines = count($this->ann);
183                 require_once 'MTrack/SyntaxHighlight.php';
184                 $this->data =  MTrack_SyntaxHighlight::highlightSource(
185                         stream_get_contents($this->tmpfile), null, $this->file->name
186                     );
187                    // var_dump($this->data);
188     
189             default:
190                 break; // download only..
191         }
192       
193        
194       // 
195         //$this->renderEvents();
196     }
197     
198     function schemeSelect() // FIXME... we should use JS highligher.. not PHP one...
199     {
200         if (!$this->data) {
201            return;
202         }
203         require_once 'MTrack/SyntaxHighlight.php';
204         return MTrack_SyntaxHighlight::getSchemeSelect();
205     }
206    
207    
208     
209     
210 }
211     
212
213  
214     
215
216