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