File:  [mozdev] / seb / www / functions.php
Revision 1.35: download - view: text, annotated - select for diffs - revision graph
Fri Aug 8 11:38:26 2003 UTC (16 years, 6 months ago) by seb
Branches: MAIN
CVS tags: HEAD
trying something better

<?php

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

	CVSId('$Id: functions.php,v 1.35 2003/08/08 11:38:26 seb 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) { // table (basic) template
		global $defined_title, $downloads, $normal, $type;
		$ret = '';

		if ($dir == $downloads) {
			$path = 'downloads.mozdev.org/seb/';
			$filesize = sizeUnit(getFileSize('downloads/'.substr($filename, 0, -2)));
		} else {
			$path = 'seb.mozdev.org/'.$type.'/';
			$filesize = sizeUnit(@filesize($dir.$filename));
		}

		$filesize = sizeUnit(getFileSize(str_replace('/cvs/seb/', '', $dir).
			substr($filename, 0, -2)));

		if (!isset ($defined_title['table_'.$title])) {
			$ret .= "            <tr>\n".
				'              <td colspan="3" style="text-decoration:underline;'.
				'text-align:center;font-style:italic;width:620px;">'.
				$title.
				"</td>\n".
				"            </tr>\n".
				"            <tr>\n".
				'              <td style="width:270px;font-weight:bold;">Filename'.
				"</td>\n".
				'              <td style="width:270px;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($filename, 0, -2).'">'. //strip the ,v (again)
			substr($filename, 0, -2)."</a></td>\n".'              <td>'.
			date("Y/m/d - H:i:s", filemtime($dir.$filename))."</td>\n".
			'              <td>'.$filesize."</td>\n            </tr>\n";

		echo $ret;
	}

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

		echo $ret;
	}

	Function files($title, $type, $eval, $tmpl, $dir) { // 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($type, substr($filename, 0, -2))) { // strip the ",v"
					eval('tmpl_'.$tmpl."(\$filename,\$title,\$dir);");
					$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($Id, 5, -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);
		}
	}

?>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>