php8
[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     
14     var $object;
15     var $ident;
16     var $filename;
17     var $repo;
18  
19     function getAuth() 
20     {
21         parent::getAuth();
22          
23         return true;
24   
25     }
26  
27     
28   
29  
30     function get($pi = '')
31     {
32         if (!isset($_REQUEST['ajax_body'])) {
33             return;
34         }
35         $this->masterTemplate = 'file.html';
36          
37         $this->pi =  $pi . (strlen($pi) ? $this->bootLoader->ext : '');
38     
39         //var_dump($pi);
40         
41          
42         $this->repo = DB_DataObject::factory('mtrack_repos');
43         $this->filename = $this->repo->loadFromPath($this->pi);
44         if (!$this->repo->id) {
45             throw new Exception("invalid path {$this->pi}");
46         }
47          
48         if (!$this->projectPerm($this->repo->project_id, 'MTrack.Repos', 'S')) {
49             return HTML_FlexyFramework::run('Noperm');  // noperm = loggedin -> need more perms / not.. try loggin in..
50         }
51         
52           
53         if (empty($this->filename) || !$this->repo->id) {
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         $this->file =  isset($_GET['rev']) ? 
62             $this->repo->file($this->filename, 'rev', $_GET['rev']) :  
63             $this->repo->file($this->filename);
64
65         //echo '<PRE>'; print_r($this->file);    exit;
66             
67         $this->ent = $this->file->getChangeEvent();
68
69         
70         if (!empty($_GET['raw'])) {
71              //should it add an image header..
72             require_once 'File/MimeType.php';
73             $y = new File_MimeType();
74             $this->mimetype = $y->fromFilename($this->file->name);
75  
76             $filename = basename($this->filename);
77             header("Content-Type: {$this->mimetype}; name=\"{$filename}\"");
78             if ($_GET['raw'] == 'download') {
79                 header("Content-Disposition: attachment; filename=\"{$filename}\"");
80             }
81             
82             fpassthru($this->file->cat());
83             exit;
84         }
85         
86         $this->jump = empty($_GET['jump']) ? '' : $_GET['jump'];
87         
88         
89         
90        
91         $this->crumbs = array();
92         $this->basename  = basename($this->filename);
93         $location = '';
94         $crumbs = dirname($this->filename) == '.' ? array() :  explode('/', dirname($this->filename));
95          
96         
97         
98         
99         foreach($crumbs as $path) 
100         {
101             if (count($this->crumbs) && !strlen($path)) {
102                 continue;
103             }
104             $c = new StdClass;
105             $c->name =   $path  ;
106             $location .=  strlen($location) ? '/' : '';
107             $location .=  strlen($path)  ?  urlencode($path)  : '';
108             $c->location = $location;
109             $this->crumbs[] = $c;
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         $ext = strtolower(array_pop(explode('.', $this->file->name)));
134         
135         //$this->mimetype = mtrack_mime_detect($location, $this->file->name);
136         // special cases..
137         
138         list($major) = explode('/', $this->mimetype, 2);
139
140         switch ($ext) {
141             case 'sql':
142                 $major = 'text';
143                 break;
144             
145             
146         }
147
148         // Obscure-ish special cases for mime types;
149         // some .y files look like old image format data
150         switch ($this->mimetype) {
151             case 'image/x-3ds':
152             case 'application/xml':
153             case 'application/javascript':
154             case 'application/x-httpd-php':
155                 $major = 'text';
156                 break;
157             
158         }
159         // some special cases:
160         // we would be better to do if octect/stream to use the file tool.
161         
162         switch(basename($this->file->name)) {
163             case '.gitignore':
164             case 'README':
165             case 'LICENSE':
166             case '.hgignore':
167                 $major = 'text';
168                 break;
169            }
170         
171
172         // simple cases first..
173         switch ($major) {
174             case 'image':
175                 $this->majorImage = true;
176                 break;
177                 
178             case 'text':
179                 fseek($t, 0);
180                 
181                 // this should be a json query..
182                 //$this->ann = array_values($this->file->annotate());
183                // echo '<PRE>';print_R($this->ann);exit;
184                 if (!empty($_REQUEST['annotate'])) {
185                     
186                     $ann = array_values($this->file->annotate());
187                     $this->jdata($ann);
188                 }
189                 
190                 $this->ann = array();
191                 if ($this->ann === 'DELETED') {
192                     $this->deleted = true;
193                     $this->ann = false;
194                     break;
195                 }
196                 $this->tmpfile = $t; 
197                 
198                
199                 require_once 'MTrack/SyntaxHighlight.php';
200                 $contents = stream_get_contents($this->tmpfile);
201                 $this->nlines = count(explode("\n", $contents));
202                 
203                 if (!empty($_REQUEST['markup'])) {
204                     // we do a late version call for markup.. 
205                     echo MTrack_SyntaxHighlight::highlightSource(
206                         $contents , null, $this->file->name
207                     );
208                     exit;
209                                        
210                 }
211                 
212                 
213                 $this->data =  htmlspecialchars($contents); /* MTrack_SyntaxHighlight::highlightSource(
214                         $contents , null, $this->file->name
215                    );
216                                           */
217                     //var_dump($this->data);
218                 for ($i =0; $i < $this->nlines; $i++ ) {
219                     $this->ann[] = (object) array(
220                         'rev' => '',
221                         'lineno' => ($i + 1)
222                     );
223                 }
224                 
225                    
226
227     
228             default:
229                 break; // download only..
230         }
231         if (!empty($_REQUEST['markup'])) {
232            $this->jerr("invalid request");
233         }
234        
235       // 
236         //$this->renderEvents();
237     }
238     
239     function schemeSelect() // FIXME... we should use JS highligher.. not PHP one...
240     {
241         if (!$this->data) {
242            return;
243         }
244         require_once 'MTrack/SyntaxHighlight.php';
245         return MTrack_SyntaxHighlight::getSchemeSelect();
246     }
247    
248    
249     
250     
251 }
252     
253
254  
255     
256
257