<?php
/**
 * $Source: /home/cvs/nukescripts/admin/modules/max_admin_functions.php,v $
 * $Revision: 1.0.0-1 $
 * $Author: cyril $
 * $Date: 2007/01/08 11:38:14 $
 * Fix Bug 192
 */
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
if ( !defined('ADMIN_MAX') && !defined('MAXIMUS') && !defined('ADMIN_MAX_USER'))
{die('Illegal File Access Admin');}

if (!function_exists('title')) {
	function title($text) {
		Max_OpenTable();
		br('1');
		echo "<center><font class='title'>$text</font></center>";
		br('1');
		Max_CloseTable();
		br('1');
	}
}
function get_adminlang($module){
	global $currentlang, $language;
	if(@file_exists("admin/language/$currentlang/".$module.".php")){
		include_once("admin/language/$currentlang/".$module.".php");
	}
	else{
		if(@file_exists("admin/language/$language/".$module.".php")){
			include_once("admin/language/$language/".$module.".php");
		}
	}
}

function NavBarAdmin($img1, $img2, $img3, $img4, $img5, $img6, $img7, $img8, $img9) {
global $op, $admin_file, $admintheme;
if (is_file('kernel/addons/image_dyn.php')) {
include_once('kernel/addons/image_dyn.php');
}
echo "<table width='98%'><tr><td width='80%' valign='top' align='left'>";
image_dyn( "admin/themes/".$admintheme."/images/dyn/".$img1."_a.png", "admin/themes/".$admintheme."/images/dyn/".$img1.".png", "index.php", ""._HOMESITE."", "home");
echo "&nbsp;";
image_dyn( "admin/themes/".$admintheme."/images/dyn/".$img2."_a.png", "admin/themes/".$admintheme."/images/dyn/".$img2.".png", "".$admin_file.".php", ""._ADMINISTRATION."", "manager" ) ;
echo "&nbsp;";
image_dyn( "admin/themes/".$admintheme."/images/dyn/".$img3."_a.png", "admin/themes/".$admintheme."/images/dyn/".$img3.".png", "".$admin_file.".php?op=AdminPanel", ""._PANELADMINISTRATION."", "paneladmin" ) ;
echo "&nbsp;";
image_dyn( "admin/themes/".$admintheme."/images/dyn/".$img4."_a.png", "admin/themes/".$admintheme."/images/dyn/".$img4.".png", "javascript:history.go(-1)", ""._RETURN."", "back" ) ;
image_dyn( "admin/themes/".$admintheme."/images/dyn/".$img5."_a.png", "admin/themes/".$admintheme."/images/dyn/".$img5.".png", "javascript:location.reload()", ""._RELOAD."", "reload" ) ;
image_dyn( "admin/themes/".$admintheme."/images/dyn/".$img6."_a.png", "admin/themes/".$admintheme."/images/dyn/".$img6.".png", "javascript:history.go(+1)", ""._NEXT."", "next" ) ;
echo "&nbsp;";
image_dyn( "admin/themes/".$admintheme."/images/dyn/".$img7."_a.png", "admin/themes/".$admintheme."/images/dyn/".$img7.".png", "".$admin_file.".php?op=logout", ""._LOGOUT."", "cancel" ) ;
$navig = $_SERVER['HTTP_USER_AGENT'];
if (stristr( $navig ,'Firefox')) {
echo "&nbsp;&nbsp;&nbsp;<input value=\" $op \" size=\"35\" style=\"font-size: 18 pt; color: black\" disabled />";
} else {
echo "&nbsp;&nbsp;&nbsp;<input value=\" $op \" size=\"25\" style=\"font-size: 12 pt; color: black\" disabled />";
}
echo  "</td><td width=\"20%\" valign=\"top\" align=\"right\">";
image_dyn( "admin/themes/".$admintheme."/images/dyn/".$img8."_a.png", "admin/themes/".$admintheme."/images/dyn/".$img8.".png", "http://www.php-maximus.org/modules.php?name=Forums", ""._SUPPORT."", "support" ) ;
image_dyn( "admin/themes/".$admintheme."/images/dyn/".$img9."_a.png", "admin/themes/".$admintheme."/images/dyn/".$img9.".png", "http://www.php-maximus.org", ""._OFFICIALWEBSITE."", "official" ) ;
echo "</td></tr></table>";
}

function adminmenu($url, $title, $image, $x_module=false) {
    global $counter, $admingraphic;
    if (!empty($x_module)) {
    $image = "modules/$x_module/images/admin/$image";
	} else {
    $image = "images/admin/$image";
    }
    if ($admingraphic == 1) {
		$img = "<img src=\"$image\" border=\"0\" alt=\"$title\" title=\"$title\" /></a><br />";
		$close = "";
    } else {
		$img = "";
		$close = "</a>";
    }
    echo "<td align='center' valign='top' width='16%'><font class='content'><a href=\"$url\">$img<b>$title</b>$close<br /><br /></font></td>";
    if ($counter == 5) {
		echo "</tr><tr>";
		$counter = 0;
    } else {
		$counter++;
    }
}

function GraphicModules() {
    global $aid, $admingraphic, $language, $admin, $prefix, $counter, $admin_file, $cache_admin;
    $sql = "SELECT `radminsuper` FROM `".TABLE_AUTHORS."` WHERE `aid`='$aid'";
    if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin authors GraphicAdmin', '', __LINE__, __FILE__, $sql);}
    $row         = $GLOBALS['db']->sql_fetchrow($result);
    $radminsuper = $GLOBALS['db']->XMFbin($row['radminsuper']);
if (is_file('kernel/tabs/tabwindow.php')) {
include_once ('kernel/tabs/tabwindow.php');
}
	//opencache ( "CLA-AdminHome", "".$cookie[1]."", "php",  "".$cache_admin."");
	$currentTabs=new Tabs("tb2");

	$currentTabs->StartTabWindow();
	$currentTabs->AddTab(""._ACTIVE."");
    echo "<center><table border='0' width='100%' cellspacing='1'><tr>";
	$sql1 = "SELECT `title`, `admins` FROM `".TABLE_MODULES."` WHERE `install`='1' ORDER BY `title` ASC";
	if ( !($result1 = $GLOBALS['db']->sql_query($sql1)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin modules GraphicAdmin', '', __LINE__, __FILE__, $sql1);}
	$sql2 = "SELECT `name` FROM `".TABLE_AUTHORS."` WHERE `aid`='$aid'";
	if ( !($result2 = $GLOBALS['db']->sql_query($sql2)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin authors GraphicAdmin', '', __LINE__, __FILE__, $sql2);}
	$row2 = $GLOBALS['db']->sql_fetchrow($result2);
	while ($row1 = $GLOBALS['db']->sql_fetchrow($result1)) {
		$xadmins = stripslashes($row1['admins']);
		$admins  = explode(",", $xadmins);
		$auth_user = 0;
		for ($i=0; $i < sizeof($admins); $i++) {
		$xname = stripslashes($row2['name']);
			if ($xname == "$admins[$i]") {
				$auth_user = 1;
			}
		}
		if ($radminsuper == 1 || $auth_user == 1) {
		$xtitle = stripslashes($row1['title']);
if (is_file("modules/$xtitle/admin/links.php")) {
	    		include_once("modules/$xtitle/admin/links.php");
			}
		}
	}
    echo "</tr></table></center>";
    $GLOBALS['db']->sql_freeresult($result1);
    $GLOBALS['db']->sql_freeresult($result2);

$currentTabs->AddTab(""._INACTIVE."");
	echo "<center><table border='0' width='100%' cellspacing='1'><tr>";
	$sql1 = "SELECT `title`, `admins` FROM `".TABLE_MODULES."` WHERE `install`='0' ORDER BY `title` ASC";
	if ( !($result1 = $GLOBALS['db']->sql_query($sql1)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin modules GraphicAdmin', '', __LINE__, __FILE__, $sql1);}
	$sql2 = "SELECT `name` FROM `".TABLE_AUTHORS."` WHERE `aid`='$aid'";
	if ( !($result2 = $GLOBALS['db']->sql_query($sql2)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin authors GraphicAdmin', '', __LINE__, __FILE__, $sql2);}
	$row2 = $GLOBALS['db']->sql_fetchrow($result2);
	while ($row1 = $GLOBALS['db']->sql_fetchrow($result1)) {
		$xadmins = stripslashes($row1['admins']);
		$admins  = explode(",", $xadmins);
		$auth_user = 0;
		for ($i=0; $i < sizeof($admins); $i++) {
		$xname = stripslashes($row2['name']);
			if ($xname == "$admins[$i]") {
				$auth_user = 1;
			}
		}
		if ($radminsuper == 1 || $auth_user == 1) {
		$xtitle = stripslashes($row1['title']);
		if (is_file("modules/$xtitle/admin/links.php")) {
	    		include_once("modules/$xtitle/admin/links.php");
			}
		}
	}
    echo "</tr></table></center>";
    $GLOBALS['db']->sql_freeresult($result1);
    $GLOBALS['db']->sql_freeresult($result2);
//closecache ( "CLA-AdminHome", "".$cookie[1]."", "php" , "".$cache_admin."");
    $currentTabs->EndTabWindow();
}

function GraphicAdmin() {
    global $aid, $admingraphic, $language, $admin, $prefix, $counter, $admin_file, $cache_admin, $db;
    $sql = "SELECT `radminsuper` FROM `".TABLE_AUTHORS."` WHERE `aid`='$aid'";
    if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin authors GraphicAdmin', '', __LINE__, __FILE__, $sql);}
    $row         = $GLOBALS['db']->sql_fetchrow($result);
    $radminsuper = $GLOBALS['db']->XMFbin($row['radminsuper']);
if (is_file('kernel/tabs/tabwindow.php')) {
include_once ('kernel/tabs/tabwindow.php');
}
	opencache ( "CLA-AdminHome", "".$cookie[1]."", "php",  "".$cache_admin."");
$currentTabs=new Tabs("tb2");
$currentTabs->StartTabWindow();
$currentTabs->AddTab(""._SYSTEME."");
	if ($radminsuper == 1) {
	    echo "<table border='0' width='100%' cellspacing='1'><tr>";
	    $linksdir = dir('admin/links');
	    global $menulist;
	    while($func=$linksdir->read()) {
	        if(substr($func, 0, 6) == "links.") {
	    	    $menulist .= "$func ";
			}
	    }
	    closedir($linksdir->handle);
	    $menulist = explode(" ", $menulist);
	    sort($menulist);
	    for ($i=0; $i < sizeof($menulist); $i++) {
			if($menulist[$i]!="") {
		    	$sucounter = 0;
			$test_value = $linksdir->path."/$menulist[$i]";
		    	$test_value = eregi_replace("Array", "", $test_value);
		include($test_value);
			}
	    }
		echo "</tr></table></center>";
		$counter = "";
	}
$currentTabs->AddTab(""._MODULES."");
	echo "<table border='0' width='100%' cellspacing='1'><tr>";
	$sql1 = "SELECT `title`, `admins` FROM `".TABLE_MODULES."` WHERE `install`='1' ORDER BY `title` ASC";
	if ( !($result1 = $GLOBALS['db']->sql_query($sql1)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin modules GraphicAdmin', '', __LINE__, __FILE__, $sql1);}
	$sql2 = "SELECT `name` FROM `".TABLE_AUTHORS."` WHERE `aid`='$aid'";
	if ( !($result2 = $GLOBALS['db']->sql_query($sql2)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin authors GraphicAdmin', '', __LINE__, __FILE__, $sql2);}
	$row2 = $GLOBALS['db']->sql_fetchrow($result2);
	while ($row1 = $GLOBALS['db']->sql_fetchrow($result1)) {
		$xadmins = stripslashes($row1['admins']);
		$admins = explode(",", $xadmins);
		$auth_user = 0;
		for ($i=0; $i < sizeof($admins); $i++) {
		$xname = stripslashes($row2['name']);
			if ($xname == "$admins[$i]") {
				$auth_user = 1;
			}
		}
		if ($radminsuper == 1 || $auth_user == 1) {
		$xtitle = stripslashes($row1['title']);
		if (is_file("modules/$xtitle/admin/links.php")) {
	    		include_once("modules/$xtitle/admin/links.php");
			}
		}
	}
    echo "</tr></table></center>";
    $db->sql_freeresult($result);$db->sql_freeresult($result1);$db->sql_freeresult($result2);

closecache ( "CLA-AdminHome", "".$cookie[1]."", "php" , "".$cache_admin."");
    $currentTabs->EndTabWindow();
}

function AdminLogDeny($req_log) {
   global $cache;
   $logfile = "".$cache."/logs/admindeny.log";
   $fp = fopen($logfile, 'a');
   flock($fp, 2);
   fseek($fp, filesize($logfile));
   if ($mot_log=="") {$mot_log="IP=>".get_ip();}
   $ibid = sprintf("%-10s %-30s %-10s\r\n",date("m/d/Y H:i:s",time()),basename($GLOBALS['PHP_SELF'])."=>".$req_log,$mot_log);
   fwrite($fp, $ibid);
   flock($fp, 3);
   fclose($fp);
}

function AdminLogAllow($req_log) {
global $aid, $cache;
$ip = get_ip();
$sql = "UPDATE `".TABLE_USERS."` SET `last_ip`='$ip',`dernpassage`=now() WHERE `username`='$aid' ";
if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Users', '', __LINE__, __FILE__, $sql);}
   $logfile = "".$cache."/logs/adminallow.log";
   $fp = fopen($logfile, 'a');
   flock($fp, 2);
   fseek($fp, filesize($logfile));
   if ($mot_log=="") {$mot_log="IP=>".get_ip();}
   $ibid = sprintf("%-10s %-30s %-10s\r\n",date("m/d/Y H:i:s",time()),basename($GLOBALS['PHP_SELF'])."=>".$req_log,$mot_log);
   fwrite($fp, $ibid);
   flock($fp, 3);
   fclose($fp);
}

function spaceDisk($adr=".", $plus=0) {
    if ($plus) { static $size;}
    $dp=opendir($adr);
    do {
        $itm=readdir($dp);
        if (is_dir("$adr/$itm") && ($itm!=".") && ($itm!="..") && ($itm!="")) {
            spaceDisk("$adr/$itm", 1);
        }
        elseif (($itm!=".") && ($itm!="..") && ($itm!="")) {
            $size = $size + filesize("$adr/$itm");
        }
    }
    while ($itm != false);
    closedir($dp);
    return round($size);
}