<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
//v1.1 www.exoops.info 2006/03 
#####################################################
#  Based on PHP-NUKE: ecalendar System
#  by Don Grabowski Don@ecomjunk.com -  http://ecomjunk.com
#
#  Calendrier version 2.2 Beta for Xoops 1.0 RC3
#  Copyright © 2002, Pascal Le Boustouller
#  pascal@xoopsien.net - http://www.xoopsien.net
#  Licence: GPL
#
#  Merci de laisser ce copyright en place...
#####################################################
function ecal_rand() {
        //
        // Today's Events block
        //
        global $db, $bcoosConfig;
        $block = array();
        $block['title'] = _MB_ECAL_RAND;
        include($bcoosConfig['root_path']."/modules/ecal/cache/config.php");
        $usertimevent = (userTimeOffset()*3600) +time();
        $start = date("Y-m-j", addDays(0, $usertimevent))." 00:00:00";
        $end = date("Y-m-j", addDays(60, $usertimevent))." 23:59:59";
        $query = "SELECT subject, stamp, privat, username FROM ".$db->prefix("ecal")
        ." WHERE valid='yes' AND (stamp >= '$start' and stamp <= '$end') AND valid='yes' order by stamp";
        $numrows = $db->getRowsNum($db->query($query));
        if ($numrows > 1) {
                $numrows = $numrows-1;
                mt_srand((double)microtime()*1000000);
                $srow = mt_rand(0, $numrows);
                $rowsleft = $numrows - $srow +1;
                $rows = min ($rowsleft, $blcklmt);
                $query = "SELECT subject, stamp, privat, username FROM ".$db->prefix("ecal")
                ." WHERE valid='yes' AND (stamp >= '$start' and stamp <= '$end')"
                ." order by stamp limit $srow, $rows";
                $block['content'] = content( $query,"" );
                }
        else $block['content'] .= "no events scheduled";
return $block;
}
function ecal_today() {
        //
        // Today's Events block
        //
        global $db, $bcoosConfig;
        $block = array();
        $block['title'] = _MB_ECAL_TODAY;
        include($bcoosConfig['root_path']."/modules/ecal/cache/config.php");
        $usertimevent = (userTimeOffset()*3600) +time();
        $start = date("Y-m-j", addDays(0, $usertimevent))." 00:00:00";
        $end = date("Y-m-j", addDays(0, $usertimevent))." 23:59:59";
        $query = "SELECT subject, stamp, privat, username FROM ".$db->prefix("ecal")
        ." WHERE valid='yes' AND (stamp >= '$start' and stamp <= '$end')  AND valid='yes'"
        ." order by stamp limit 0, $blcklmt";
        $m = date("m", $usertimevent);
        $d = date("j", $usertimevent);
        $y = date("Y", $usertimevent);
       // setlocale ("LC_TIME",_MB_LOCALTIME);
        $aa = strftime("%d.%b.%Y", mktime(0, 0, 0, $m, $d, $y));
        $block['content'] = content( $query, "time" );
        if ($block['content'] == "") $block['content'] .= "no events scheduled";
        $block['content'] = "<div align=\"center\">".$aa."</div><br/>"
        . $block['content'];
        return $block;
}
function ecal_week() {
        //
        // this weekblock
        //
        global $db, $bcoosConfig;
        $block = array();
        $block['title'] = _MB_ECAL_WEEK;
        include($bcoosConfig['root_path']."/modules/ecal/cache/config.php");
        $usertimevent = (userTimeOffset()*3600) +time();
        $start = date("Y-m-j", addDays(0, $usertimevent))." 00:00:00";
        $end = date("Y-m-j", addDays(7, $usertimevent))." 23:59:59";
        $query = "SELECT subject, stamp, privat, username FROM ".$db->prefix("ecal")
        ." WHERE valid='yes' AND (stamp >= '$start' and stamp <= '$end') AND valid='yes'"
        ." order by stamp limit 0, $blcklmt";
        $block['content'] = content( $query,"" );
        if ($block['content'] == "") $block['content'] .= "no events scheduled";
        return $block;
}
function ecal_nextn() {
        //
        // Next N Events block
        //
        global $db, $bcoosConfig;
        $block = array();
        include($bcoosConfig['root_path']."/modules/ecal/cache/config.php");
        $block['title'] = str_replace('%N', $nextncount, _MB_ECAL_NEXTN) ;
        // how many events to show?
        $numEvents = $nextncount;
        $usertimevent = (userTimeOffset()*3600) +time();
        $start = date("Y-m-j", addDays(0, $usertimevent))." 00:00:00";
        $query = "SELECT subject, stamp, privat, username FROM ".$db->prefix("ecal")
                ." WHERE valid='yes' AND (stamp >= '$start') AND valid='yes' order by stamp limit 0, $numEvents";
        $block['content'] = content( $query,"" );
        if ($block['content'] == "") $block['content'] .= "no events scheduled";
        return $block;
}
function ecal_month() {
        //
        // this Month block
        //
        global $db, $bcoosConfig;
        $block = array();
        $block['title'] = _MB_ECAL_MONTH;
        include($bcoosConfig['root_path']."/modules/ecal/cache/config.php");
        $usertimevent = (userTimeOffset()*3600) +time();
        $starttime = addMonths(0, 1 , $usertimevent); // first day of this
        $endtime = addMonths(1, 0, $usertimevent); // last day of this
        $start = date("Y-m-j", addMonths(0, 1, $usertimevent))." 00:00:00";
        $end = date("Y-m-j", addMonths(1, 0, $usertimevent))." 23:59:59";
        $query = "SELECT subject, stamp, privat, username FROM ".$db->prefix("ecal")
                ." WHERE valid='yes' AND (stamp >= '$start' and stamp <= '$end')  AND valid='yes'"
                ." order by stamp limit 0, $blcklmt ";
        $block['content'] = content( $query,"" );
        if ($block['content'] == "") $block['content'] .= "no events scheduled";
        return $block;
}
function addDays($days, $ts) {
                return mktime(0, 0, 0, date("m", $ts), date("d", $ts)+$days, date("Y", $ts));
}
function addMonths($months, $day, $ts) {
                // $day = 0 for last day of prev,
                ///$day = 1 for first day.
                return mktime(0, 0, 0, date("m", $ts)+$months, $day, date("Y", $ts));
}
function content($query, $lead) {
        global $db, $bcoosConfig, $xoopsUser;
//        $text = "<B>".$head."</B>";
        $result = $db->query($query);
        while (list($subject, $stamp, $privat, $username)=$db->fetchRow($result)) {
		
		if ($xoopsUser ) {
		    if ($privat == 1 and $username == $xoopsUser->uname()) {
				$Cann = strftime("%Y",strtotime($stamp));
                $Cmoi = strftime("%m",strtotime($stamp));
                $Cjour = strftime("%d",strtotime($stamp));
                $Cdt = strftime("%m/%d",strtotime($stamp));
                $Ctm = date("H:i", strtotime($stamp));
                if ($lead == "time") {
                        $Clead = $Ctm;
                } else {
                        $Clead = $Cdt;
                }
			$text .= "<div style='text-align: left;'>- $Clead <A HREF=\""
                        .$bcoosConfig['xoops_url']
                        ."/modules/ecal/display.php?day=$Cjour&month=$Cmoi&year=$Cann\">"
                        ."$subject</A><br /></div>";
			}
		}
		if ($privat == 0) {
		    $Cann = strftime("%Y",strtotime($stamp));
            $Cmoi = strftime("%m",strtotime($stamp));
            $Cjour = strftime("%d",strtotime($stamp));
            $Cdt = strftime("%m/%d",strtotime($stamp));
			$Cdt = strftime(_MB_EVEDATE,strtotime($stamp));
            $Ctm = date("H:i", strtotime($stamp));
            if ($lead == "time") {
	            $Clead = $Ctm;
            } else {
                $Clead = $Cdt;
            }
			$text .= "<div style='text-align: left;'>- $Clead <A HREF=\""
                        .$bcoosConfig['xoops_url']
                        ."/modules/ecal/display.php?day=$Cjour&month=$Cmoi&year=$Cann\">"
                        ."$subject</A><br /></div>";			
		}		
        }
        $text .= "<DIV ALIGN=\"right\"><A HREF=\""
                .$bcoosConfig['xoops_url']
                ."/modules/ecal/\">"._MB_ECAL_EV."...</A></DIV>";
        return $text;
}
function userTimeOffset() {
        global $xoopsUser, $bcoosConfig, $timeoffset;
        $useroffset = "";
        if($xoopsUser) {
                $timezone = $xoopsUser->timezone();
                if(isset($timezone)){
                        $useroffset = $xoopsUser->timezone();
                }else{
                        $useroffset = $bcoosConfig['default_TZ'];
                }
        }
        return $useroffset;
}
//**************************************************************************
//Kalenderblock von Nobse http://www.e-xoops.de
//Bloc calendrier corrigé par EsseBe le 28.11.2003  www.essebe.ch
//**************************************************************************
?>
<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
<script language="JavaScript" src="<?php echo XOOPS_URL;?>/modules/ecal/blocks/overlib.js"><!-- overLIB (c) Erik Bosrup --></script> 
<?php
function ecal_showm(){
        global $db, $bcoosConfig, $timeoffset, $xoopsUser, $time, $mm, $pm1, $my, $py;
                $blockm = array();
        $blockm['title'] = ""._MB_ECAL_TITLE."";
        include($bcoosConfig['root_path']."/modules/ecal/cache/config.php");
        $imagepath = $bcoosConfig['xoops_url']."/modules/ecal/images";
//zugriffsrechte
if ($xoopsUser) {
		$group = $xoopsUser->groups();
    }else{
		$group[] = 3;
	}
	$anno = explode("|", $anoevent);
	$is = array_intersect ($group, $anno);
//*********************************
 $useroffset = "";
    if($xoopsUser) 
          $useroffset = $xoopsUser->getVar("timezone_offset");
    else
          $useroffset = $bcoosConfig['default_TZ'];
  $offset = $useroffset - ($bcoosConfig['server_TZ']-date('I'));
  $usertimevent = time() +($offset * 3600);
	$heute = date("j", $usertimevent);
	$m = date("m", $usertimevent);
	$d = date("j", $usertimevent);
	$y = date("Y", $usertimevent);
if ($mm != "") {
if ($mm != $m) {
	    $heute = 31;
	}
	$m = $mm;
	$d = date("j", $usertimevent);
	$y = date("Y", $usertimevent);
	$y = $my;	
		if ($mm == "0"){
			$m = 12;
			$y = $y-1;
		}
}
if ($pm1 != "") {
	if ($pm1 != $m) {
	    $heute = 31;
	}
	$m = $pm1;
	$d = date("j", $usertimevent);
	$y = date("Y", $usertimevent);
	$y = $py;	
		if ($pm1 == "13"){
			$m = 1;
			$y = $y+1;
		}
}
//*******************
$month = $m;
$year = $y;
if ($month == "01") {
                $monthname = _CALJAN;
        } elseif ($month == "02") {
                $monthname = _CALFEB;
        } elseif ($month == "03") {
                $monthname = _CALMAR;
        } elseif ($month == "04") {
                $monthname = _CALAPR;
        } elseif ($month == "05") {
                $monthname = _CALMAY;
        } elseif ($month == "06") {
                $monthname = _CALJUN;
        } elseif ($month == "07") {
                $monthname = _CALJUL;
        } elseif ($month == "08") {
                $monthname = _CALAUG;
        } elseif ($month == "09") {
                $monthname = _CALSEP;
        } elseif ($month == "10") {
                $monthname = _CALOCT;
        } elseif ($month == "11") {
                $monthname = _CALNOV;
        } elseif ($month == "12") {
                $monthname = _CALDEC;
        }
//**** Get the Day (Integer) for the first day in the month */
$First_Day_of_Month_Date = mktime(0,0,0, $m, 1, $y);
$Date = $First_Day_of_Month_Date;
//$Day_of_First_Week = Date("w",$First_Day_of_Month_Date)-_MB_CALWEEKBEGINN;
if ($beginn == 0) {
	$Day_of_First_Week = Date("w",$First_Day_of_Month_Date);
}else{
	$Day_of_First_Week = (Date("w",$First_Day_of_Month_Date)+6)%7;
}
if ($Day_of_First_Week<0) $Day_of_First_Week=6;
/**** Find the last day of the month */
$Month = $m;
$day = 27;
do {
        $End_of_Month_Date = mktime(0,0,0, $m, $day, $y);
        $Test_Month = Date("m",$End_of_Month_Date);
        $day += 1;
        } while ( $Month == $Test_Month );
$Last_Day = $day - 2;
//****************************
$blockm['content'] .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" valign=\"top\" width=\"100%\" class=\"bg2\"><tr><td><table border=\"0\" cellspacing=\"1\" cellpadding=\"1\"   width=\"100%\">";
$blockm['content'] .= "\n<TR class=\"bg3\">\n\t<TD colspan=\"7\" align=center >";
$blockm['content'] .= "<A HREF=\"".$bcoosConfig['xoops_url']."/modules/ecal/\">";
$blockm['content'] .= "<b>$monthname $year</b></a></TD>\n</TR>";
if ($beginn == 0) {
	$blockm['content'] .= "\n<TR class=\"bg1\" align=\"center\"><TD>"._MB_7DAY."</TD><TD>"._MB_1DAY."</TD><TD>"._MB_2DAY."</TD><TD>"._MB_3DAY."</TD><TD>"._MB_4DAY."</TD><TD>"._MB_5DAY."</TD><TD>"._MB_6DAY."</TD></TR>";
}else{
	$blockm['content'] .= "\n<TR class=\"bg1\" align=\"center\"><TD>"._MB_1DAY."</TD><TD>"._MB_2DAY."</TD><TD>"._MB_3DAY."</TD><TD>"._MB_4DAY."</TD><TD>"._MB_5DAY."</TD><TD>"._MB_6DAY."</TD><TD>"._MB_7DAY."</TD></TR>";
}
$blockm['content'] .= "\n<TR class=\"bg1\">";
if ($Day_of_First_Week != 0){
        $blockm['content'] .= "\n\t<TD class=\"bg2\" colspan=\"$Day_of_First_Week\"  >&nbsp;</TD>";
        }
$day_of_week = $Day_of_First_Week + 1;
/**** Build Current Month */
// Array News ********
if ($shownews ==1) {
	$result=$db->query("SELECT storyid, published, title FROM ".$db->prefix("stories")." WHERE published >= ".strtotime("$y/$m/1")." and published <= ".strtotime("$y/$m/$heute 23:59:59")." and published <= ". time()."");
		while (list( $storyid, $published, $title) = $db->fetchRow($result)){
			$ist = date("j",$published);
			$title=addCSlashes($title,"'");
			//$newsid[$ist] .= $storyid;
			$news[$ist] .= 	"<a href=".$bcoosConfig['xoops_url']."/modules/news/article.php?storyid=$storyid><img src=".$imagepath."/mini_news.gif alt="._CAL_NEWS."> $title</a><br>";
		}
}
//*********************	
// Array Termine ***********
$result1=$db->query("SELECT subject,stamp,privat,username FROM ".$db->prefix("ecal")." WHERE stamp >= \"$y-$m-01 00:00:00\" AND stamp <=\"$y-$m-$Last_Day 23:59:00\" AND valid='yes' order by stamp DESC");
$i = 0 ;
 while   ( list($text,$stamp,$privat,$username) = $db->fetchRow($result1) ){
 
if ($xoopsUser) {
	if ($privat == 1 and $username == $xoopsUser->uname()) {
		$alttext[$i]=addCSlashes($text,"'");
		$zeit[$i] = substr($stamp,11,5);
		$tag[$i] = substr($stamp,8,2);
	}
}
if($privat == 0) {
	$alttext[$i]=addCSlashes($text,"'");
	$zeit[$i] = substr($stamp,11,5);
 	$tag[$i] = substr($stamp,8,2);
}
 $i= $i +1;
 }
//End Array *****************
//Array Termine
$result=$db->query("SELECT stamp,id FROM ".$db->prefix("ecal")." WHERE  stamp >= \"$y-$m-01 00:00:00\" AND stamp <=\"$y-$m-$Last_Day 23:59:00\" AND valid='yes' order by stamp DESC");
$x =0;
while (list($stamp, $id) = $db->fetchRow($result)){
if ($xoopsUser) {
	if ($privat == 1 and $username == $xoopsUser->uname()) {
		$tag[$x] = substr($stamp,8,2);
		$termin[$x] = $id;
	}
}
$x= $x+1;
}
//End Array
for ($day = 1 ; $day <= $Last_Day ; $day++){
        if ($day_of_week == 1){
                $blockm['content'] .= "\n<TR class=\"bg1\">";
                }
$link = $bcoosConfig['xoops_url']."/modules/ecal/display.php?day=$day&month=$m&year=$y";
if (count($is) > 0){
$link2 = $bcoosConfig['xoops_url']."/modules/ecal/addevent.php?month=$m&year=$y";
}else{
$link2 = $bcoosConfig['xoops_url']."/modules/ecal/index.php";
}
//********************************************
$xx =$x;
$dayrows="";
while ($xx > -1) {
if ($tag[$xx] == $day)
      $dayrows++;
$xx=$xx -1;
}
//*********************************************
$ii=$i;
$test = 0;
while ($ii > -1){
      if ($day == $tag[$ii]){
        //   $alt[$day] .= $link." <li> $zeit[$ii] $alttext[$ii]";
     	 $alt[$day] .= "<a href=".$bcoosConfig['xoops_url']."/modules/ecal/display.php?day=$day&month=$m&year=$y><img src=".$imagepath."/mini_calendar.gif alt="._CAL_TERMIN."> $zeit[$ii] $alttext[$ii]</a><br>";
     	$test = 1;
	  }
$ii=$ii -1;
}
if ($test == 0) {
    if ($shownews == 1) {
		if ($news[$day] != "") {
    	$alt[$day] .= $news[$day];
		}
	}
} else {
	if ($shownews == 1) {
		if ($news[$day] != "") {
    	$alt[$day] .= "<hr>".$news[$day];
		}
	}
}
//NEWS *******
//$linknews = $bcoosConfig['xoops_url']."/modules/news/article.php?storyid=$newsid[$day]";
//$overnews = 'onclick=\'window.location.href="'.$linknews.'";\' onmouseover=\'this.style.cursor="hand"; return overlib("'.$news[$day].'", CAPTION , "<center><b>'._CAL_NEWS.'</B>", CSSCLASS, BGCLASS, "bg2", FGCLASS, "bg1", WIDTH, "200", HAUTO, "on", VAUTO, "on");\' onmouseout=\'return nd();\'';
//*************
//$over = 'onclick=\'window.location.href="'.$link.'";\' onmouseover=\'this.style.cursor="hand"; return overlib("'.$alt[$day].'", STICKY,MOUSEOFF, NOCLOSE, 1, FOLLOWMOUSE,1, OFFSETX, 0,OFFSETY, 0,CAPTION , "<center><b>'._CAL_MB_ENDEVENT.'</B>", CSSCLASS, BGCLASS, "bg2", FGCLASS, "bg1", WIDTH, "200" );\' onmouseout=\'return nd();\'';
$over = "onclick=\"window.location.href='$link';\" onmouseover=\"this.style.cursor='hand'; return overlib('$alt[$day]', STICKY,MOUSEOFF, NOCLOSE, 1, FOLLOWMOUSE,1, OFFSETX, 0,OFFSETY, 0,CAPTION , '<center><b>"._CAL_MB_ENDEVENT."</B>', CSSCLASS, BGCLASS, 'bg2', FGCLASS, 'bg1', WIDTH, '200' );\" onmouseout=\"return nd();\"";
if (count($is) > 0){
$over2= 'onclick=\'window.location.href="'.$link2.'";\' onmouseover=\'this.style.cursor="hand"; return overlib("'._ECAL_ADDEV.'", CSSCLASS, BGCLASS, "bg2", FGCLASS, "bg1", WIDTH, "100", HAUTO, "on", VAUTO, "on");\' onmouseout=\'return nd();\'';
}else{
$over2= 'onclick=\'window.location.href="'.$link2.'";\' onmouseover=\'this.style.cursor="hand";\'';
}
//*********************************************
        $imgprops= "width=\"9\" height=\"9\" border=\"0\"";
        
        if ($dayrows == 0 and $news[$day] == "") {
        	$blockm['content'] .= "\n\t<TD ";
        	if ($anoevent ==1 || $xoopsUser)$blockm['content'] .= "$over2";
			//if ($anoevent ==1 || $xoopsUser)$blockm['content'] .= "";
        		$blockm['content'] .= "align=\"center\" ";
        	if ($day == date("j", $usertimevent)and $m == date("m", $usertimevent) )
        		$blockm['content'] .="bgcolor=$daycolor";
        $blockm['content'] .=">$day<br><img src=\"$imagepath/events0.gif\" $imgprops>";
        } else {
		
		//if ($dayrows > 0 and $news[$day] != "") {
		    $blockm['content'] .= "<TD $over align='center' ";
        	if ($day == date("j", $usertimevent)and $m == date("m", $usertimevent) )
        		$blockm['content'] .="bgcolor=$daycolor";
        	$blockm['content'] .= ">$day<br><img src='$imagepath/events1.gif' $imgprops ><br>";
			//$blockm['content'] .= "<img src=\"$imagepath/events1.gif\" $imgprops $overnews><br>";
		}
		/*if ($dayrows > 0 and $news[$day] == "") {
		    $blockm['content'] .= "<TD  align=\"center\" ";
        	if ($day == date("j", $usertimevent)and $m == date("m", $usertimevent) )
        		$blockm['content'] .="bgcolor=$daycolor";
        	$blockm['content'] .= ">$day<br><img src=\"$imagepath/events1.gif\" $imgprops $over><br><img src=\"$imagepath/events0.gif\" $imgprops>";
		} 
		
		if ($dayrows == 0 and $news[$day] != "") {
		    $blockm['content'] .= "<TD  align=\"center\" ";
        	if ($day == date("j", $usertimevent)and $m == date("m", $usertimevent) )
        		$blockm['content'] .="bgcolor=$daycolor";
        	$blockm['content'] .= ">$day<br><img src=\"$imagepath/events0.gif\" $imgprops><br><img src=\"$imagepath/events1.gif\" $imgprops $overnews>";
		
		
		}*/
        $blockm['content'] .= "</TD>";
        if ($day_of_week == 7){
                $day_of_week = 0;
                $blockm['content'] .= "\n</TR>";
                }
        $day_of_week += 1;
        }
        
	
$mm = $m-1;
$pm1 = $m+1;
if ( !empty($bcoosConfig['startpage']) && $bcoosConfig['startpage'] != '--' ) {
	$url = "modules/".$bcoosConfig['startpage']."/index.php?mm=$mm&my=$y";
	$url2 = "modules/".$bcoosConfig['startpage']."/index.php?pm1=$pm1&py=$y";
	}
	else {
	$url = "index.php?mm=$mm&my=$y";
	$url2 = "index.php?pm1=$pm1&py=$y";} 
	
$blockm['content'] .= "<tr><TH colspan=\"1\"><A HREF=\"".$bcoosConfig['xoops_url']."/$url\">";
$blockm['content'] .= "<img src=\"".$bcoosConfig['xoops_url']."/modules/ecal/images/left.gif\"></a></th>";
if ($m <> date("m", $usertimevent )){
$blockm['content'] .= "<TH colspan=\"5\"><A HREF=\"".$bcoosConfig['xoops_url']."\">";
$blockm['content'] .= "<img src=\"".$bcoosConfig['xoops_url']."/modules/ecal/images/right.gif\"><img src=\"".$bcoosConfig['xoops_url']."/modules/ecal/images/left.gif\"></a></th>";
}
else $blockm['content'] .= "<TH colspan=\"5\"></th>";
$blockm['content'] .= "<TH colspan=\"1\"> <A HREF=\"".$bcoosConfig['xoops_url']."/$url2\">";
$blockm['content'] .= "<img src=\"".$bcoosConfig['xoops_url']."/modules/ecal/images/right.gif\"></a></th></tr>";
$blockm['content'] .= "</table>";
	   
       $blockm['content'] .= "</td></tr></table>";
              return $blockm ;
        }
?>