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