<?php
//web-lib.php
// Common functions and definitions for web admin programs

// Vital libraries

// Configuration and spool directories
$config_directory = getenv("WEBMIN_CONFIG");
//$webmin_path = getenv("PATH_TRANSLATED");
$webmin_path = getenv("SERVER_ROOT");
$var_directory = getenv("WEBMIN_VAR");
$module_name=getenv("SCRIPT_NAME");
$base_remote_user=getenv("BASE_REMOTE_USER");
$remote_user=getenv("REMOTE_USER");
//__DATA__


/*---------------------------LIB_PrintHeader($charset)----------------------------
Outputs the http header for HTML
------------------------------------------------------------------------------------------*/  
function LIB_PrintHeader($charset){
    $num_args=func_num_args(); //number of arguments passed to the function
    if($pragma_no_cache || $config[pragma_no_cache])
	Header("Pragma: no-cache\n");
    if($num_args>0)
	Header("Content-type: text/html; Charset=$charset\n\n");
    else Header("Content-type: text/html\n\n");
}

/*--LIB_header($title,$image,$help,$config,$nomodule,$nowebmin,$rightside,$header,$body)-----
Output a page header with some title and image. The header may also include a link to help,
and a link to the config page.
The header will also have a link to the webmin index, and a link to the module menu
if there is no config link.
--------------------------------------------------------------------------------------------*/
function LIB_header($title=undef,$image=undef,$help=undef,$config=undef,$nomodule=undef,
		    $nowebmin=undef,$rightside=undef,$header=undef,$body=undef){
    global $gconfig;
    global $text;
    global $tconfig;
    global $remote_user;
    global $module_name;
    global $module_info; 
    
    $num_args=func_num_args(); //number of arguments passed to the function
    
/*    foreach $l (&list_languages()) {
	$lang = $l if ($l->{'lang'} eq $current_lang);
    }*/
       
    $charset=$force_charset ? $force_charset : "iso-8859-1";
    LIB_PrintHeader($charset);
    echo"<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">\n";
    echo"<html>\n";
    if($charset){
	echo"<meta http-equiv=\"Content-Type\" ",
	    "content=\"text/html; Charset=$charset\">\n";	
    }
    
    $os_type = $gconfig[real_os_type] ? $gconfig[real_os_type]
					  : $gconfig[os_type];
    $os_version = $gconfig[real_os_version] ? $gconfig[real_os_version]
					        : $gconfig[os_version];
    $hostname=LIB_get_system_hostname();
    $webmin_version=LIB_get_webmin_version();
//echo"os_type:$os_type,os_version:$os_version,hostname:$hostname,webmin_version:$webmin_version";
    echo"<head>\n";    
    if($num_args > 0){
//echo"gconfig[sysinfo]:$gconfig[sysinfo]<br>";	
	if ($gconfig[sysinfo] == 1)
	    echo"<title>$title: $remote_user on $hostname ($os_type $os_version)</title>\n";
	else echo"<title>$title</title>\n";
//echo"remote_user:$remote_user<br>";     
	if ($gconfig[sysinfo] == 0 && $remote_user) {
	    $ssl_user=getenv("SSL_USER");
	    $local_user=getenv("LOCAL_USER");		
	    if($ssl_user) $_user="(SSL certified)";
	     elseif($local_user) $_user="(Local user)";
	      else $_user="";    
	    echo"<SCRIPT LANGUAGE=\"JavaScript\">\n";
	    echo"defaultStatus=\"$remote_user$_user logged into Webmin $webmin_version on $hostname ($os_type $os_version)\"\n";
	    echo"</SCRIPT>\n";
	}
    }
//echo"tconfig[headhtml]:$tconfig[headhtml]<br>";
    if ($tconfig[headhtml]) echo"$tconfig[headhtml]\n";
//echo"tconfig[headinclude]:$tconfig[headinclude]<br>";
    if ($tconfig[headinclude]) {
	$file=$module_name ? "../$gconfig[theme]/$tconfig[headinclude]"
			       : "$gconfig[theme]/$tconfig[headinclude]";
	$INC=fopen($file,"r");
	while(!feof($INC)){
	    $current_line=fgetss($INC,255);
	    echo"$current_line";
	}
	fclose($INC);
			/*EN perl
			open(INC, $module_name ? "../$gconfig[theme]/$tconfig[headinclude]"
					       : "$gconfig[theme]/$tconfig[headinclude]");
			while(<INC>) {
			print;
			}
			close(INC);*/
    }
    echo"</head>\n";
    if($tconfig[cs_page]) $bgcolor=$tconfig[cs_page];
     elseif($gconfig[cs_page]) $bgcolor=$gconfig[cs_page];
      else $bgcolor="ffffff";		    
    if($tconfig[cs_link]) $link=$tconfig[cs_link];
     elseif($gconfig[cs_link]) $link=$gconfig[cs_link];
      else $link="0000ee";
    if($tconfig[cs_text]) $_text=$tconfig[cs_text];
     elseif($gconfig[cs_text]) $_text=$gconfig[cs_text];
      else $_text="000000";
    if($tconfig[bgimage]) $bgimage="background=$tconfig[bgimage]"; 
     else $bgimage="";
    echo"<body bgcolor=#$bgcolor link=#$link vlink=#$link text=#$_text ",
	"$bgimage $tconfig[inbody] $body>\n";
    
    $hostname = LIB_get_system_hostname();
    $version = LIB_get_webmin_version();
    $prebody = $tconfig[prebody];
//echo"prebody:$prebody<br>";    
/*    if ($prebody) {
	$prebody =~ s/%HOSTNAME%/$hostname/g;
	$prebody =~ s/%VERSION%/$version/g;
	$prebody =~ s/%USER%/$remote_user/g;
	$prebody =~ s/%OS%/$os_type $os_version/g;
	print "$prebody\n";
    }*/
//echo"tconfig[prebodyinclude]:$tconfig[prebodyinclude]<br>";    
    if ($tconfig[prebodyinclude]) {
	$file= $module_name ? "../$gconfig[theme]/$tconfig[prebodyinclude]" 
			    : "$gconfig[theme]/$tconfig[prebodyinclude]";
	$INC=fopen($file,"r");
	while(!feof($INC)){
	    $current_line=fgetss($INC,255);
	    echo"$current_line";
	}
	fclose($INC);
    }
    if ($num_args > 1) {
	echo"<table width=100%><tr>\n";
	if ($gconfig[sysinfo] == 2 && $remote_user) {
	    $ssl_user=getenv("SSL_USER");
	    $local_user=getenv("LOCAL_USER");		
	    if($ssl_user) $_user="(SSL certified)";
	     elseif($local_user) $_user="(Local user)";
	      else $_user="";    	
	    echo"<td colspan=3 align=center>\n";
	    echo"<tt>$remote_user</tt>$_user logged into Webmin $version on \"<tt>$hostname</tt>\" ($os_type $os_version)</td>\n";
	    echo"</tr> <tr>\n";
	}
	echo"<td width=15% valign=top align=left>";
	$http_webmin_server=getenv("HTTP_WEBMIN_SERVERS");
//echo"http_webmin_server:$http_webmin_server<br>";	
	if ($http_webmin_server) {
	    echo"<a href='$http_webmin_server'>",
		      "$text[header_servers]</a><br>\n";
	}
/*	if (!nowebmin && !$tconfig[noindex]) {
	    local %acl;
	    &read_acl(undef, \%acl);
	    local $mc = @{$acl{$base_remote_user}} == 1;
	    if ($gconfig{'gotoone'} && $main::session_id && $mc == 1) {
		print "<a href='/session_login.cgi?logout=1'>",
		    "$text{'main_logout'}</a><br>";
	    }
	    elsif ($gconfig{'gotoone'} && $mc == 1) {
		print "<a href=/switch_user.cgi>",
		    "$text{'main_switch'}</a><br>";
	    }
	    else {
		print "<a href='/?cat=$module_info{'category'}'>",
		    "$text{'header_webmin'}</a><br>\n";
	    }
	}
*/      
        if ($nowebmin!=undef){
	    echo"<a href=\"/?cat=$module_info[category]\">$text[header_webmin]</a><br>\n"; 
	} 
	if ($nomodule!=undef) { 
	     echo"<a href=\"./\">$text[header_module]</a><br>\n"; 	
	}
	if (is_array($help)) {
	    echo LIB_hlink($text[header_help], $help[0], $help[1]),"<br>\n";
	}
	elseif ($help!=undef) {
		echo LIB_hlink($text[header_help], $help),"<br>\n";
	}
	if ($config!=undef) {
	    $access = LIB_get_module_acl();
	    $mod_name = LIB_get_module_name();
	    if (!$access[noconfig]) {
		echo"<a href=\"/config.cgi?$mod_name\">",
		    $text[header_config],"</a><br>\n";
	    }
	}
	echo"</td>\n";
	ereg("s/&auml;/ä/g",$title); //	$title =~ s/&auml;/ä/g;
	ereg("s/&ouml;/ö/g",$title); //$title =~ s/&ouml;/ö/g;
	ereg("s/&uuml;/ü/g",$title); //$title =~ s/&uuml;/ü/g;
	ereg("s/&nbsp;/ /g",$title); //$title =~ s/&nbsp;/ /g;

	if ($image!=undef) {
	    echo"<td align=center width=70%>",
		"<img alt=\"$title\" src=\"$image\"></td>\n";
	}
/*	elsif ($lang->{'titles'} && !$gconfig{'texttitles'} &&
	       !$tconfig{'texttitles'}) {
		print "<td align=center width=70%>";
		foreach $l (split(//, $title)) {
			$ll = ord($l);
			if ($ll > 127 && $lang->{'charset'}) {
				print "<img src=/images/letters/$ll.$lang->{'charset'}.gif alt=\"$l\" align=bottom>";
				}
			elsif ($l eq " ") {
				print "<img src=/images/letters/$ll.gif alt=\"\&nbsp;\" align=bottom>";
				}
			else {
				print "<img src=/images/letters/$ll.gif alt=\"$l\" align=bottom>";
				}
			}
		print "</td>\n";
		}
*/	else {
		echo"<td align=center width=70%><h1>$title</h1></td>\n";
		}
	echo"<td width=15% valign=top align=right>";
	//echo $rightside;
	echo"</td></tr></table>\n";
	}
}

/*------------------------------------LIB_hlink($text,$page,$module)-------------------------
Returns HTML for alink to help page. The first parameter is the test of the link and the second
the name of the help page.
--------------------------------------------------------------------------------------------*/
function LIB_hlink($text,$page,$module=undef){
    $mod_name=LIB_get_module_name();
    $mod=($module!=undef) ? $module : $mod_name;
    return "<a onClick='window.open(\"/help.cgi/$mod/$page\", \"help\", \"toolbar=no,menubar=no,scrollbars=yes,width=400,height=300,resizable=yes\"); return false' href=\"/help.cgi/$mod/$page\">$text</a>";
}

/*------------------------------------LIB_get_system_hostname()---------------------------------
Returns the hostname of this system
--------------------------------------------------------------------------------------------*/
function LIB_get_system_hostname(){
    $hostname=`hostname`; //run a shell command
    return $hostname ;
}

/*------------------------------------LIB_get_webmin_version()---------------------------------
Returns the version of Webmin currently being run
--------------------------------------------------------------------------------------------*/
function LIB_get_webmin_version(){
    //$VERSION=fopen("../version","r") || VERSION=fopen("../version","r");
    
    //flose($VERSION);
    return 0.91;
}

/*------------------------------------LIB_get_module_acl($user,$module)---------------------------------
Return an array containing acces control options for the given user 
--------------------------------------------------------------------------------------------*/
function LIB_get_module_acl($user=undef,$module=undef){
    global $base_remote_user;
    global $remote_user;
    global $module_name;
    global $config_directory;
    global $gconfig; //associative array
    
//    $u=($user!=undef) ? $user : $base_remote_user;
//    $u=($user!=undef) ? $user : $remote_user;
    $u=($user!=undef) ? $user : "admin"; //admin par defaut		
//echo"user:$u<br>";
//echo"base_remote_user:$base_remote_user<br>";
$mod_name=LIB_get_module_name();    
    $m=($module!=undef) ? $module : $mod_name;
//echo"module:$m:$module<br>";    
    $rv=LIB_read_file($module_name ? "../$m/"."defaultacl" : ".$m"."defaultacl");
    if($gconfig["risk_$u"] && $m){
	$rf=$gconfig["risk_u"].'.risk';
	$rv=LIB_read_file($module_name ? "../$m/$rf" : "./$m/$rf");
	$sf=$gconfig["skill_u"].'.skill';
        $rv=LIB_read_file($module_name ? "../$m/$sf" : "./$m/$sf");
    }
    else $rv=LIB_read_file("$config_directory/$m/$u.acl");
    
    return $rv;
}
/*------------------------------------LIB_get_module_name()---------------------------------
Returns the name of the current module.This fonction is useful in php because if the script
exemple.cgi is run module_name=/module_name/example.cgi.

--------------------------------------------------------------------------------------------*/
function LIB_get_module_name(){
    global $module_name;
    list($nul,$mod_name,$_nul)=split("/",$module_name);
    return $mod_name;
}

/*------------------------------------LIB_footer($page,$name)---------------------------------
Output a footer for returning to some page
--------------------------------------------------------------------------------------------*/
function LIB_footer($page,$name){
    //Global variables used in this function
    global $text; //$text is an associative array initialised in init_config
    global $module_info; //associative array
    global $module_name;
    
    $url=$page;
    if($url == "/"){
	$url="/?cat=$module_info[category]";
    }
    elseif($url == "" && $module_name){
	    $url=" ";//return to the last page
    }
    elseif( ereg("?",$url) || ereg($module_name,$url) ){
	$url="/$module_name/$url";
    }
    
    $link=LIB_text($text[main_return],$name);
    //$link=LIB_text($text[main_version],"32","33","34");
    echo "<a href=\"$url\"><img alt=\"<-\" align=middle border=0 src=/images/left.gif></a>\n";
    echo "<a href=\"$url\">$link</a><br>\n";
}


/*--------------------------------------LIB_read_file($file)----------------------------------
Fill an associative array with name=value pairs from a file
--------------------------------------------------------------------------------------------*/
function LIB_read_file($file){
    $delimiter="=";
    if(!$fp=fopen($file,"r")) 
	return -1;
    while(!feof($fp)){
	$current_line=fgetss($fp,255);
	$current_line=trim($current_line);
	    //trim delete all space in the string, try chop() and ltrim()
	list($option,$value)=split($delimiter,$current_line);
	$assoc_array[$option]=$value;
    }
    fclose($fp);
    return $assoc_array;
}
/*-------------------------------------LIB_init_config()------------------------------------
set the following variables:
    $gconfig : Global configuration
    $config : module configuration
    $module_info
    $tconfig
------------------------------------------------------------------------------------------*/     
function LIB_init_config(){
    //Global variables used in this function
    global $config_directory;
    global $module_name;
    global $webmin_path;
    
    //Read the webmin global config file and fill the associative array $gconfig
    $GLOBALS["gconfig"]=LIB_read_file($config_directory."/config");
    //Set PATH and LD_LIBRARY_PATH
     //if($gconfig[path]) putenv(PATH=$gconfig[path]);
     //if($gconfig[ld_env]) putenv($gconfig[ld_env]=gconfig[ld_path]);

    list($nul,$m,$_nul)=split("/",$module_name);
    $GLOBALS["config"]=LIB_read_file($config_directory."/".$m."/config");
    $GLOBALS["module_info"]=LIB_read_file($webmin_path."/".$m."/module.info");
    //Load language string into associative array named $text
    $GLOBALS["text"]=LIB_load_language();
    
    $theme=LIB_check_key_array($GLOBALS["gconfig"],theme);
    if($theme){
	$GLOBALS["tconfig"]=LIB_read_file($module_name ? "../$theme/config"
				:"$theme/config");
    }
// $key=LIB_array_to_keystring($GLOBALS["text"]);
// $val=LIB_array_to_valstring($GLOBALS["text"]);
// echo"<br>$key<br>$val<br>";    

//    echo"sans arg<br>";
//    LIB_ReadParse();
//    echo"avec gconfig<br>";
//    LIB_ReadParse($GLOBALS["gconfig"]);
//test_display_array($GLOBALS["gconfig"]); 
}

/*----------------------LIB_check_key_array($assoc_array,$key)-----------------------------
check if the key passed to the function exist,return the corresponding value else return 0  
if(LIB_check_key_array($GLOBALS["gconfig"],theme)){
	echo"existing key<br>"; 
    }
    else echo"this key is not in this array<br>";
}
------------------------------------------------------------------------------------------*/     
function LIB_check_key_array($assoc_array,$key){
    //test_display_array($assoc_array);  
    if($assoc_array[$key]) $rv=$assoc_array[$key];
    else $rv=0;
    return $rv; 
}

/*---------------------------LIB_ReadParse($assoc_array,$method)----------------------------
fills the given associative array with cgi paramaters, or uses the globals $in
if none is given.
------------------------------------------------------------------------------------------*/     
function LIB_ReadParse($assoc_array=undef,$method=undef){
    
    if($assoc_array!=undef) $a=$assoc_array;
     else $a=$in;
    if($method!=undef) $meth=$methode;
     else $meth=getenv("REQUEST_METHOD");  
test_display_array($a);     
echo"<br>meth:$meth<br>";
$z=getenv("CONTENT_LENGTH");
echo"CONTENT_LENGTH:$z<br>";
     
    if($meth=='POST') read(STDIN, $in, getenv("CONTENT_LENGTH"));
    test_display_array($a); 
/*    if(getenv("QUERY_STRING")){
	if($in)
    }*/
}
/*sub ReadParse
{
local $a = $_[0] ? $_[0] : \%in;
local $i;
local $meth = $_[1] ? $_[1] : $ENV{'REQUEST_METHOD'};
undef($in);
if ($meth eq 'POST') {
	read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
	}
if ($ENV{'QUERY_STRING'}) {
	if ($in) { $in .= "&".$ENV{'QUERY_STRING'}; }
	else { $in = $ENV{'QUERY_STRING'}; }
	}
@in = split(/\&/, $in);
foreach $i (@in) {
	local ($k, $v) = split(/=/, $i, 2);
	$k =~ s/\+/ /g; $k =~ s/%(..)/pack("c",hex($1))/ge;
	$v =~ s/\+/ /g; $v =~ s/%(..)/pack("c",hex($1))/ge;
	$a->{$k} = defined($a->{$k}) ? $a->{$k}."\0".$v : $v;
	}
}*/
/*---------------------------LIB_redirect($url)----------------------------
Output headers to redirect the browser to some page
------------------------------------------------------------------------------------------*/     
function LIB_redirect($url){
    global $module_info;
    $server_port=getenv("SERVER_PORT");
    $https=getenv("HTTPS");
    $server_name=getenv("SERVER_NAME");
    $script_name=getenv("SCRIPT_NAME ");
    
    if( ($server_port==443) && ($https=="ON")) $port="";
     elseif( ($server_port==80) && ($https!="ON")) $port="";
      else $port=$server_port;	    
    if($https=="ON") $prot="https";
    else $prot="http";
    
    if(ereg("http",$url) | ereg("https",$url) | ereg("ftp",$url) | ereg("gopher",$url)){
	Header("Location: $url");
    }
    elseif(ereg("/^\//",$url)){
	Header("Location: $prot://$server_name:$port$url");
    }
    elseif(ereg("/^(.*)\/[^\/]*$/",$sript_name)){
	Header("Location: $prot://$server_name:$port$1/$url");
    }
    elseif($url==""){//return to the current module category 
	Header("Location: $prot://$server_name:$port/$url/?cat=$module_info[category]");
    }
    else Header("Location: $prot://$server_name:$port/$url"); 
}

/*---------------------------LIB_get_module_info($module)----------------------------

------------------------------------------------------------------------------------------*/     
function LIB_get_module_info($module){

}

/*---------------------------LIB_load_language()----------------------------
Returns a hashtable mapping text codes to strings in the appropriate language
------------------------------------------------------------------------------------------*/     
function LIB_load_language(){
    //Global variables used in this module	
    global $webmin_path;
    global $gconfig;
    global $module_name;
    
    //$a=$webmin_path.$module_name."lang/".$gconfig[lang];
    //echo"$a<br>";
    //fill the associative array $text with the correct language
	//firsty with general text codes: /path/to/webmin-0.91/lang/current_language
	$text=LIB_read_file($webmin_path."/lang/".$gconfig[lang]);
	//secondly with module text codes: /path/to/webmin-0.91/current_module/lang/current_language
	if($module_name){
	    $text_mod=LIB_read_file("lang/".$gconfig[lang]);
	    //fill $text with the content of $text_mod    
	    while(list($key,$value)=each($text_mod)){
		$text[$key]=$value;
	    }
	}
//	test_display_array($text);
    return $text;
}

/*------------------------LIB_text($message,$substitute,$sub2,$sub3)--------------------------
Looks up the given message in the appropriate language translation file, replaces the text
$1, $2 and so on with the rest of the parameters, and returns the result.
I suppose there is only 3 substituted arguments passed to the function($1,$2,$3).
If an other substituted argument is added, you should add one for this function.
To check the number of substituted arguments open read this file: /path/to/webmin-0.91/lang/en     
---------------------------------------------------------------------------------------------*/     
function LIB_text($message,$substitute,$sub2=nul,$sub3=nul){
    global $text; //$text is an associative array
    
    $rv=$message;
    $num_args=func_num_args(); //number of arguments passed to the function
    $args_list=func_get_args(); //$args_list is an associative array
    
    for($i=1;$i<$num_args;$i++){
	$replace=func_get_arg($i);
	$rv=str_replace("\$$i",$replace,$rv);
    }
    return $rv;
}

/*-------------------------LIB_array_to_keystring($assoc_array)--------------------------------
Push all key of an array into a string
Instead of passing an array from a script to an other one(it's not possible with php???),
we could passed a string.So this function built a string with all key of this array 
---------------------------------------------------------------------------------------------*/
function LIB_array_to_keystring($assoc_array){
    $separator=" |#| ";
    while(list($key,$value)=each($assoc_array)){
	if($key!="") $key_string=$key_string.$key.$separator;
    }    
    return $key_string;
}
/*---------------------------LIB_array_to_valstring($assoc_array)--------------------------------
this function built a string with all value of an array 
---------------------------------------------------------------------------------------------*/
function LIB_array_to_valstring($assoc_array){
    $val_string=implode(" |#| ",$assoc_array);
    return $val_string;
}
/*---------------------------LIB_convert_str_to_array($keystr,$valstr)--------------------------------
This function built an associative array from 2 strings.
I didn't used explose because keys become numbers.   
---------------------------------------------------------------------------------------------*/
function LIB_convert_str_to_array($keystr,$valstr){
    while(list($key,$value)=each($assoc_array)){
    
    }
}

function test_display_array($a){
    while(list($k,$v)=each($a)){
	echo"$k:$v<br>";
    }
}
?>