<?php
/**
 * $Source: /home/cvs/nukescripts/kernel/utility.php,v $
 * $Revision: 1.1.0a-2 $
 * $Author: cyril $
 * $Date: 2006/09/10 14:49:04 $
 * Fix ligne 213 zorteil - Bug 162
 * Fix ligne 150 Helger  - Bug 132
 */
/************************************************************************/
/* 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                                 */
/************************************************************************/
defined('MAXIMUS') or die ('Access denied utility MAXIMUS directly...');

function CronHours($hours1, $hours2, $namecron, $cronfile) {
global $operation, $datecron;
$hours1   = $GLOBALS['db']->XMFnum($hours1);
$hours2   = $GLOBALS['db']->XMFnum($hours2);
$namecron = stripslashes($namecron);
$cronfile = stripslashes($cronfile);
if ($hours1 == $hours2) {return;}
$today = getdate();
     $day = $GLOBALS['db']->XMFnum($today['mday']);
    if ($day < 10) {
	$day = "0$day";
    }
    $month = $GLOBALS['db']->XMFnum($today['mon']);
    if ($month < 10) {
	$month = "0$month";
    }
    $year = $GLOBALS['db']->XMFnum($today['year']);
    $hour = $GLOBALS['db']->XMFnum($today['hours']);
    if ($hour < 10) {
	$hour = "0$hour";
    }
    $min = $GLOBALS['db']->XMFnum($today['minutes']);
    if ($min < 10) {
	$min = "0$min";
    }
    $sec = $GLOBALS['db']->XMFnum($today['seconds']);
    if ($sec < 10) {
	$sec = "0$sec";
    }
    $complete    = "$day:$month:$year : $hour:$min:$sec";
    $hoursactual = "$hour$min";
    $dayactual   = "$day$month";


if (($hours1 >=  $hoursactual ) or ( $hoursactual > $hours2)) {
return;
}
if (file_exists("cache/cron/cron-$namecron.php")) {
include_once ("cache/cron/cron-".$namecron.".php");}

if (($operation == "1") && ($datecron == $dayactual )) {
return;
 } else {
include_once ("cache/cron/config-$cronfile.php");
}

if (($hours1 <=  $hoursactual ) & ( $hoursactual < $hours2)) {
$crondatafile = "cache/cron/cron-$namecron.php";
$file = fopen("$crondatafile", "w");
$content = "<?php\n";
$content .= "\n";
$content .= "\$realised = \"$complete\";\n";
$content .= "\n";
$content .= "\$operation = \"1\";\n";
$content .= "\$datecron = \"$day$month\";";
fwrite($file, $content);
fclose($file);
}
}

function query_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '', $err_file = '', $sql = '')
{ global $adminmail, $sitename, $cookie, $urle;

	$sql_error = $GLOBALS['db']->sql_error();
	$debug_text = '';
	if ( $err_line != '' && $err_file != '' )
	{
	$debug_text .= 'File : ' . basename($err_file).' <br />Line : ' . $err_line ;
	}
	if ( $sql_error['message'] != '' )
	{
	$debug_text .= ' - SQL Error : ' . $sql_error['code'] . ' ' . $sql_error['message'];
	}
	if ( $sql_store != '' )
	{
	$debug_text .= " - $sql_store";
	}
incoh();

//Max_OpenTable();
echo "<center>"._MESSAGE_ERROR1."<br /><br /><br /><img src='modules/Your_Account/images/delete.png' alt=\""._ERROR."\" title=\""._ERROR."\" /></center><br /><br />"._MESSAGE_ERROR2."<br /><br />";

		$subject = "Erreur fatale sur $sitename";
		$msg = "$sitename\n\n";
		$msg .= "$msg_code"
   		 ."\nMessage: $msg_text"
   		 ."\nLine: $err_line"
   		 ."\nFile: $err_file"
   		 ."\nQuery: $sql"
   		 ."\nHard Query:$debug_text";
		$to = $adminmail;
		$mailheaders = "From: $adminmail<$adminmail>\r\n";
		$mailheaders .= "Reply-To: $adminmail\r\n";
		$mailheaders .= "Return-Path: $adminmail\r\n";
		mail($to, $subject, $msg, $mailheaders);

if (defined('USER_ALLOW')) {
$u_id   = $GLOBALS['db']->XMFnum($cookie[0]);
$u_name = addslashes(htmlentities($cookie[1]));
} else {
$u_id   = 1;
$u_name = ""._ANONYMOUS."";
}
$user_ip = get_ip();
$msg     = addslashes(htmlentities($msg, ENT_QUOTES));
$urle    = addslashes(htmlentities($urle, ENT_QUOTES));
$GLOBALS['db']->sql_query("INSERT INTO `".TABLE_ERRORS."` values (NULL, now(), '$msg', '$u_id', '$u_name', '$urle', '$user_ip')");

//Max_CloseTable();
incof();
}

$ser    = 1;
$secmax = 1;
define('MAX_IMP', true);

function is_active($module) {
    if (empty($module)) { return NULL; }
    $module = addslashes(htmlentities($module, ENT_QUOTES));
    $sql = "SELECT `mid` FROM `".TABLE_MODULES."` WHERE `title`='$module' AND `active`='1' AND `install`='1' ";
if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(KERNEL_ERROR, 'Could not SELECT _modules is_active', '', __LINE__, __FILE__, $sql);}
    $numrow    = $GLOBALS['db']->sql_numrows($result);

   if (!empty($numrow)) {
		return 1;
	} else {
	return 0;
     }
}

function online() {
	global $user, $cookie, $admin_file, $name, $max_time, $max_url;
	$max_url      = addslashes(htmlentities($max_url, ENT_QUOTES));
	$name         = addslashes(htmlentities($name, ENT_QUOTES));
	$ip           = get_ip();
        if (defined('USER_ALLOW')) {
        	cookiedecode($user);
        	$uname = $cookie[1];
        		if (!isset($uname)) {
        			$guest = 0;
        		 }
       		 } else {
       		$uname = $ip;
        	$guest = 1;
        }

if ( !defined('ADMIN_MAX')) {
$past =  $max_time - MAXADV_TIMESESSION_U ;
$past2 = $max_time - MAXADV_TIMESESSION_V ;

$del =  "DELETE FROM `".TABLE_SESSION."` WHERE ( `guest`='0' AND `time` < '$past' ) OR ( `guest`='1' AND `time` < '$past2' )  ";
if ( !($resultat = $GLOBALS['db']->sql_query($del)) ){
	query_die(KERNEL_ERROR, 'Could not DELETE _session online', '', __LINE__, __FILE__, $del);}
}

$sql1 = "SELECT `time` FROM `".TABLE_SESSION."` WHERE `uname`='$uname'";
if ( !($result1 = $GLOBALS['db']->sql_query($sql1)) ){
	query_die(KERNEL_ERROR, 'Could not SELECT _session online', '', __LINE__, __FILE__, $sql1);
}

if (!defined('ADMIN_MAX')) {
	if (strstr($max_url, 'name=')) {
		$max_url = "modules.php?$max_url";
	}
} else {

if ( $max_url != "" ) {
	$max_url = "".$admin_file.".php?$max_url";
	} else {
	$max_url = "".$admin_file.".php";
	}
}
	$ctime = $max_time;
	if (!empty($uname)) {
		$uname = substr($uname, 0,25);
		$row = $GLOBALS['db']->sql_fetchrow($result1);

	       	if ($row) {
		$sql2 = "UPDATE `".TABLE_SESSION."` SET `time`='$ctime', `host_addr`='$ip', `guest`='$guest', `Wurl`='$max_url', `Wname`='$name' WHERE `uname`='$uname'";
			if ( !($result2 = $GLOBALS['db']->sql_query($sql2)) )
{ query_die(KERNEL_ERROR, 'Could not UPDATE _session online', '', __LINE__, __FILE__, $sql2);}
		} else {
			$sql3 = "INSERT INTO `".TABLE_SESSION."` (uname, time, host_addr, guest, Wurl, Wname) VALUES ('$uname', '$ctime', '$ip', '$guest', '$max_url', '$name')";
			if ( !($result3 = $GLOBALS['db']->sql_query($sql3)) )
{ query_die(KERNEL_ERROR, 'Could not INSERT _session online', '', __LINE__, __FILE__, $sql3);}
		}
	}
	$GLOBALS['db']->sql_freeresult($result1);
}

function formatTimestamp($time) {
    global $datetime, $locale, $decalage;
    setlocale(LC_TIME, $locale);
    if ( !empty($decalage) ) { $decalage = ( $decalage * 3600 ); }
    if ( !is_numeric($time) ) {
        preg_match('/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/', $time, $datetime);
        $time = gmmktime($datetime[4],$datetime[5],$datetime[6],$datetime[2],$datetime[3],$datetime[1]) + $decalage ;
    }
    $time -= date("Z");
    $datetime = strftime(_DATESTRING, $time);
    $datetime = ucfirst($datetime);
    return $datetime;
}
// compatibility phpnuke

function paid() {
}

function update_points( $id = FALSE ) {
}

// Additif 24-08-2004 - séparation du calcul et mise en '' du calcul
function serverload() {
	global $max_time ;
	$max_time = $GLOBALS['db']->XMFnum($max_time);
	$duration = '3600';
	$calcul   = $max_time - $duration;
if ( @ empty(  $_SESSION['serverload'] ) ) {
	$sql = "DELETE FROM `".TABLE_SERVERLOAD."` WHERE `hours` < '$calcul' "  ;
	if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(KERNEL_ERROR, 'Could not DELETE _serverload ', '', __LINE__, __FILE__, $sql);}
	@ $_SESSION['serverload'] = '1' ;
}
	$sql = "INSERT INTO `".TABLE_SERVERLOAD."` (`id`, `hours`) VALUES (NULL, '$max_time') ";
	if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(KERNEL_ERROR, 'Could not INSERT _serverload ', '', __LINE__, __FILE__, $sql);}
	$sql = "SELECT `id` FROM `".TABLE_SERVERLOAD."`";
	if ( !($result = $GLOBALS['db']->sql_query($sql)) )
{ query_die(KERNEL_ERROR, 'Could not SELECT _serverload ', '', __LINE__, __FILE__, $sql);}
        $num = $GLOBALS['db']->sql_numrows ($result);
        $GLOBALS['db']->sql_freeresult ($result);
        return $num;
}

function br($num) {
$num = $GLOBALS['db']->XMFnum($num);
$i = 1;
while ( $i <= $num ) {
	echo "<br />\n";
	$i++;
      }
}