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