masterTemplate = 'file.html'; $this->pi = $pi . (strlen($pi) ? $this->bootLoader->ext : ''); //var_dump($pi); $this->repo = DB_DataObject::factory('mtrack_repos'); $this->filename = $this->repo->loadFromPath($this->pi); if (!$this->repo->id) { throw new Exception("invalid path {$this->pi}"); } if (!$this->projectPerm($this->repo->project_id, 'MTrack.Repos', 'S')) { return HTML_FlexyFramework::run('Noperm'); // noperm = loggedin -> need more perms / not.. try loggin in.. } if (empty($this->filename) || !$this->repo->id) { throw new Exception("invalid path " . htmlspecialchars($this->pi) ); } if (!$this->repo) { throw new Exception("invalid path {$this->pi}"); } $this->file = isset($_GET['rev']) ? $this->repo->file($this->filename, 'rev', $_GET['rev']) : $this->repo->file($this->filename); //echo '
'; print_r($this->file);    exit;
            
        $this->ent = $this->file->getChangeEvent();

        
        if (!empty($_GET['raw'])) {
             //should it add an image header..
            require_once 'File/MimeType.php';
            $y = new File_MimeType();
            $this->mimetype = $y->fromFilename($this->file->name);
 
            $filename = basename($this->filename);
            header("Content-Type: {$this->mimetype}; name=\"{$filename}\"");
            if ($_GET['raw'] == 'download') {
                header("Content-Disposition: attachment; filename=\"{$filename}\"");
            }
            
            fpassthru($this->file->cat());
            exit;
        }
        
        $this->jump = empty($_GET['jump']) ? '' : $_GET['jump'];
        
        
        
       
        $this->crumbs = array();
        $this->basename  = basename($this->filename);
        $location = '';
        $crumbs = dirname($this->filename) == '.' ? array() :  explode('/', dirname($this->filename));
         
        
        
        
        foreach($crumbs as $path) 
        {
            if (count($this->crumbs) && !strlen($path)) {
                continue;
            }
            $c = new StdClass;
            $c->name =   $path  ;
            $location .=  strlen($location) ? '/' : '';
            $location .=  strlen($path)  ?  urlencode($path)  : '';
            $c->location = $location;
            $this->crumbs[] = $c;
        }
         
                
        $finfo = pathinfo($this->file->name);
        $t = tmpfile();

        $data = $this->file->cat();
//       echo '
'. htmlspecialchars(stream_get_contents($data));exit;

        stream_copy_to_stream($data, $t);
        $data = null;
        
        // we have copied the data to a file.. $t...
        $info = fstat($t);

        $location = stream_get_meta_data($t);
        
        $location = $location['uri'];
        //print_r($location);
        
        require_once 'File/MimeType.php';
        $y = new File_MimeType();
        $this->mimetype = $y->fromFilename($this->file->name);
        $ext = strtolower(array_pop(explode('.', $this->file->name)));
        
        //$this->mimetype = mtrack_mime_detect($location, $this->file->name);
        // special cases..
        
        list($major) = explode('/', $this->mimetype, 2);

        switch ($ext) {
            case 'sql':
                $major = 'text';
                break;
            
            
        }

        // Obscure-ish special cases for mime types;
        // some .y files look like old image format data
        switch ($this->mimetype) {
            case 'image/x-3ds':
            case 'application/xml':
            case 'application/javascript':
            case 'application/x-httpd-php':
                $major = 'text';
                break;
            
        }
        // some special cases:
        // we would be better to do if octect/stream to use the file tool.
        
        switch(basename($this->file->name)) {
            case '.gitignore':
            case 'README':
            case 'LICENSE':
            case '.hgignore':
                $major = 'text';
                break;
           }
        

        // simple cases first..
        switch ($major) {
            case 'image':
                $this->majorImage = true;
                break;
                
            case 'text':
                fseek($t, 0);
                
                // this should be a json query..
                //$this->ann = array_values($this->file->annotate());
               // echo '
';print_R($this->ann);exit;
                if (!empty($_REQUEST['annotate'])) {
                    
                    $ann = array_values($this->file->annotate());
                    $this->jdata($ann);
                }
                
                $this->ann = array();
                if ($this->ann === 'DELETED') {
                    $this->deleted = true;
                    $this->ann = false;
                    break;
                }
                $this->tmpfile = $t; 
                
               
                require_once 'MTrack/SyntaxHighlight.php';
                $contents = stream_get_contents($this->tmpfile);
                $this->nlines = count(explode("\n", $contents));
                
                if (!empty($_REQUEST['markup'])) {
                    // we do a late version call for markup.. 
                    echo MTrack_SyntaxHighlight::highlightSource(
                        $contents , null, $this->file->name
                    );
                    exit;
                                       
                }
                
                
                $this->data =  htmlspecialchars($contents); /* MTrack_SyntaxHighlight::highlightSource(
                        $contents , null, $this->file->name
                   );
                                          */
                    //var_dump($this->data);
                for ($i =0; $i < $this->nlines; $i++ ) {
                    $this->ann[] = (object) array(
                        'rev' => '',
                        'lineno' => ($i + 1)
                    );
                }
                
                   

    
            default:
                break; // download only..
        }
        if (!empty($_REQUEST['markup'])) {
           $this->jerr("invalid request");
        }
       
      // 
        //$this->renderEvents();
    }
    
    function schemeSelect() // FIXME... we should use JS highligher.. not PHP one...
    {
        if (!$this->data) {
           return;
        }
        require_once 'MTrack/SyntaxHighlight.php';
        return MTrack_SyntaxHighlight::getSchemeSelect();
    }
   
   
    
    
}