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