<?php

    
if (isset ($_SERVER['QUERY_STRING'])
            && 
$_SERVER['QUERY_STRING'] == 'source') {
        
show_source(basename(__FILE__));
        exit;
    }

    
CVSId('$Id: functions.php,v 1.47 2004/02/14 17:20:25 tsn Exp $');

    Function 
sizeUnit($fileSize) {
        if (
$fileSize >= 1048576)
            
$fileSize = (round($fileSize 1048576 100) / 100).' MB'
        elseif (
$fileSize >= 1024)
            
$fileSize = (round($fileSize 1024 100) / 100).' KB'
        else 
            
$fileSize $fileSize.' B'
        return 
$fileSize;
    }
    
    Function 
getFileSize($fileName) { // Gets the exact file size. Thanks to Pete Collins <pete@mozdev.org> for this one!
        
global $project;
        
$f_current LXR_PATH.$project.'/'.$fileName;

        if (@
file_exists($f_current))
            return @
filesize($f_current);

        return;
    }

    
$defined_title = array(); // used in files templates

    
Function tmpl_table($filename,$title,$dir$subdir='') { // table (basic) template
        
global $defined_title$downloads$normal$type;
        
$ret '';

        if (
$dir == $downloads) {
            
$path 'downloads.us-east3.mozdev.org/seb/'$subdir;
        } else {
            
$path 'seb.mozdev.org/'.$type.'/';
        }

        if (!isset (
$defined_title['table_'.$title])) {
            
$ret .= "            <tr>\n".
                
'              <td colspan="2" style="text-decoration:underline;'.
                
'text-align:center;font-style:italic;width:620px;">'.
                
$title.
                
"</td>\n".
                
"            </tr>\n".
                
"            <tr>\n".
                
'              <td style="width:310px;font-weight:bold;">Filename'.
                
"</td>\n".
                
'              <td style="width:310px;font-weight:bold;">Date'.
                
"</td>\n".
                
//'              <td style="width:80px;font-weight:bold;">Size'.
                //"</td>\n".
                
"            </tr>\n";
            
$defined_title['table_'.$title] = $title;
        }

        
$ret .= "            <tr>\n".'              <td><a href="'.
            
'http://'.$path.substr($filename0, -2).'">'//strip the ,v
            
substr($filename0, -2)."</a></td>\n".'              <td>'.
            
date("Y/m/d - H:i:s"filemtime($dir.$filename)).//"</td>\n".
            //'              <td>'.
            //sizeUnit(getFileSize(str_replace('/cvs/seb/', '', $dir).
            //substr($filename, 0, -2))).
            
"</td>\n            </tr>\n";

        echo 
$ret;
    }

    Function 
tmpl_table_error() { // table (basic) error template
        
$ret "            <tr>\n".
            
'              <td colspan="2" style="width:620px;">'.
            
"Sorry, couldn't list files</td>\n".
            
"            </tr>\n";

        echo 
$ret;
    }

    Function 
files($title$type$eval$tmpl$dir$subdir='') { // list files from a given pattern
        
global $file;
        
$tmp '';
        if (
is_array($file)) {
            eval (
"\$eval(\$file);");
            
reset($file);
            
$filecount 0;
            while (list (,
$filename) = each($file)) {
                if (
preg_match($typesubstr($filename0, -2))) { // strip the ",v"
                    
eval('tmpl_'.$tmpl."(\$filename,\$title,\$dir,\$subdir);");
                    
$filecount++;
                }
            }
            if (
$filecount == 0) {
#                eval("tmpl_".$tmpl."_error();");
            
}
        } else {
#            eval("tmpl_".$tmpl."_error();");
        
}
    }

    Function 
listFiles($dir) { // generate the file array used in listings
        
global $file;
        if (
$dir = @opendir($dir)) {
            if (!isset (
$file)) 
                
$file = array();
            while (
$file[] = @readdir($dir)) ;
            @
closedir($dir);
        } else
            
$file FALSE;
    }

    Function 
CVSId($Id) { // Grabs the CVS Id from each PHP file.
        
global $cvs;

        if (!isset (
$cvs))
            
$cvs = array();

        
$tmp explode(' 'substr($Id5, -6));
        
$tmp[0] = substr($tmp[0], 0, -2);
        
$cvs[] = $tmp;
    }

    Function 
getRevFromFile($file) { // gets the cvs version number of $file
        
global $cvs;
        
        for (
$i=0$i count($cvs); $i++) {
            if (
$cvs[$i][0] == $file)
                return 
$cvs[$i][1];
        }
    }

    Function 
setMimeType($http='HTTP_ACCEPT',$type='application/xhtml+xml',
        
$default='text/html') { // some browsers support xhtml mime type. Use it!

        
if (isset ($_SERVER[$http])) {
            if (
stristr($_SERVER[$http], $type))
                
header('Content-type: '.$type);
            else
                
header('Content-type: '.$default);
        }
    }

?>