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     
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 = array_pop(explode('.', $this->file->name));
134         
135         //$this->mimetype = mtrack_mime_detect($location, $this->file->name);
136         // special cases..
137         switch ($ext) {
138             case 'sql':
139                 $major = 'text';
140                 break;
141             
142             
143         }
144         list($major) = explode('/', $this->mimetype, 2);
145
146         // Obscure-ish special cases for mime types;
147         // some .y files look like old image format data
148         switch ($this->mimetype) {
149             case 'image/x-3ds':
150             case 'application/xml':
151             case 'application/javascript':
152             case 'application/x-httpd-php':
153                 $major = 'text';
154                 break;
155             
156         }
157         // some special cases:
158         // we would be better to do if octect/stream to use the file tool.
159         
160         switch(basename($this->file->name)) {
161             case '.gitignore':
162             case 'README':
163             case 'LICENSE':
164             case '.hgignore':
165                 $major = 'text';
166                 break;
167            }
168         
169
170         // simple cases first..
171         switch ($major) {
172             case 'image':
173                 $this->majorImage = true;
174                 break;
175                 
176             case 'text':
177                 fseek($t, 0);
178                 
179                 // this should be a json query..
180                 //$this->ann = array_values($this->file->annotate());
181                // echo '<PRE>';print_R($this->ann);exit;
182                 if (!empty($_REQUEST['annotate'])) {
183                     
184                     $ann = array_values($this->file->annotate());
185                     $this->jdata($ann);
186                 }
187                 
188                 $this->ann = array();
189                 if ($this->ann === 'DELETED') {
190                     $this->deleted = true;
191                     $this->ann = false;
192                     break;
193                 }
194                 $this->tmpfile = $t; 
195                 
196                
197                 require_once 'MTrack/SyntaxHighlight.php';
198                 $contents = stream_get_contents($this->tmpfile);
199                 $this->nlines = count(explode("\n", $contents));
200                 $this->data =  MTrack_SyntaxHighlight::highlightSource(
201                         $contents , null, $this->file->name
202                    );
203                     //var_dump($this->data);
204                 for ($i =0; $i < $this->nlines; $i++ ) {
205                     $this->ann[] = (object) array(
206                         'rev' => '',
207                         'lineno' => ($i + 1)
208                     );
209                 }
210                 
211                    
212
213     
214             default:
215                 break; // download only..
216         }
217       
218        
219       // 
220         //$this->renderEvents();
221     }
222     
223     function schemeSelect() // FIXME... we should use JS highligher.. not PHP one...
224     {
225         if (!$this->data) {
226            return;
227         }
228         require_once 'MTrack/SyntaxHighlight.php';
229         return MTrack_SyntaxHighlight::getSchemeSelect();
230     }
231    
232    
233     
234     
235 }
236     
237
238  
239     
240
241