<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/*                                                                      */
/* NPDS Copyright (c) 2002-2008 by Philippe Brunier                     */
/*                                                                      */
/* 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.       */
/************************************************************************/
// Modification pour IZ-Xinstall - EBH
if (file_exists("IZ-Xinstall.ok")) {
   if (file_exists("install.php") OR is_dir("install")) {
      echo "<div style=\"text-align: center; font-size: 20px; font-weight: bold; color: #FF0000\"><br /><br />\n"
         ."Vous devez supprimer le fichier 'install.php' ainsi que le répertoire 'install/' avant de poursuivre !<br /><br />\n"
         ."You must remove the directory 'install/' as well as the file 'install.php' before continuing!\n"
         ."</div>\n";
      die();
   }
} else {
   if (file_exists("install.php") AND is_dir("install")) {
      header("location: install.php");
   }
}

if (!function_exists("Mysql_Connexion")) {
   include ("mainfile.php");
}

// Redirect for default Start Page of the portal - look at Admin Preferences for choice
function select_start_page($op) {
    global $Start_Page, $index;
    if (!AutoReg()) { global $user; unset($user); }
    if (($Start_Page=="") or ($op=="index.php") or ($op=="edito")or ($op=="edito-nonews")) {
       $index = 1;
       theindex($op, "", "");
       die("");
    } else {
       Header("Location: $Start_Page");
    }
}

function automatednews() {
    global $gmt;
    global $NPDS_Prefix;

    $today = getdate(time()+($gmt*3600));
    $day = $today[mday];
    if ($day < 10)
       $day = "0$day";
    $month = $today[mon];
    if ($month < 10)
       $month = "0$month";
    $year = $today[year];
    $hour = $today[hours];
    $min = $today[minutes];
    $result = sql_query("select anid, date_debval from ".$NPDS_Prefix."autonews where date_debval like '$year-$month%'");
    while(list($anid, $date_debval) = sql_fetch_row($result)) {
       ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date_debval, $date);
       if (($date[1] <= $year) AND ($date[2] <= $month) AND ($date[3] <= $day)) {
          if (($date[4] < $hour) AND ($date[5] >= $min) OR ($date[4] <= $hour) AND ($date[5] <= $min) OR (($day-$date[3])>=1)) {
             $result2 = sql_query("select catid, aid, title, hometext, bodytext, topic, informant, notes, ihome, date_finval, auto_epur from ".$NPDS_Prefix."autonews where anid='$anid'");
             while (list($catid, $aid, $title, $hometext, $bodytext, $topic, $author, $notes, $ihome, $date_finval, $epur) = sql_fetch_row($result2)) {
                $title = stripslashes(FixQuotes($title));
                $hometext = stripslashes(FixQuotes($hometext));
                $bodytext = stripslashes(FixQuotes($bodytext));
                $notes = stripslashes(FixQuotes($notes));
                sql_query("insert into ".$NPDS_Prefix."stories values (NULL, '$catid', '$aid', '$title', now(), '$hometext', '$bodytext', '0', '0', '$topic', '$author', '$notes', '$ihome', '0', '$date_finval', '$epur')");
                sql_query("delete from ".$NPDS_Prefix."autonews where anid='$anid'");
                global $subscribe;
                if ($subscribe) {
                   subscribe_mail("topic",$topic,"",$title,"");
                }
             }
          }
       }
    }
    // Purge automatique
    $result = sql_query("select sid, date_finval, auto_epur from ".$NPDS_Prefix."stories where date_finval like '$year-$month%'");
    while(list($sid, $date_finval, $epur) = sql_fetch_row($result)) {
       ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date_finval, $date);
       if (($date[1] <= $year) AND ($date[2] <= $month) AND ($date[3] <= $day)) {
          if (($date[4] < $hour) AND ($date[5] >= $min) OR ($date[4] <= $hour) AND ($date[5] <= $min)) {
             if ($epur==1)
                sql_query("delete from ".$NPDS_Prefix."stories where sid='$sid'");
             else
                sql_query("update ".$NPDS_Prefix."stories set archive='1' where sid='$sid'");
          }
       }
    }
}

function aff_edito() {
    list($affich,$Xcontents)=fab_edito();
    if (($affich) and ($Xcontents!="")) {
       $notitle=false;
       if (strstr($Xcontents,"!edito-notitle!")) {
          $notitle="notitle";
          $Xcontents=str_replace("!edito-notitle!","",$Xcontents);
       }
       $ret=false;
       if (function_exists("themedito")) {
          $ret=themedito($Xcontents);
       } else {
          if (function_exists("theme_centre_box")) {
             if (!$notitle) {$title=translate("EDITO");} else {$title="";}
             theme_centre_box($title, $Xcontents);
             $ret=true;
          }
       }
       if ($ret==false) {
          if (!$notitle) {
             echo "<span class=\"edito\">".translate("EDITO")."</span>";
          }
          opentable();
          echo $Xcontents;
          closetable();
          echo "<br />";
       }
    }
}

function aff_news($op,$catid,$marqeur) {
   $news_tab=prepa_aff_news($op,$catid,$marqeur);
   $story_limit=0;
   while ($story_limit<sizeof($news_tab)) {
      $story_limit++;
      $aid=unserialize($news_tab[$story_limit][aid]);
      $informant=unserialize($news_tab[$story_limit][informant]);
      $datetime=unserialize($news_tab[$story_limit][datetime]);
      $title=unserialize($news_tab[$story_limit][title]);
      $counter=unserialize($news_tab[$story_limit][counter]);
      $topic=unserialize($news_tab[$story_limit][topic]);
      $hometext=unserialize($news_tab[$story_limit][hometext]);
      $notes=unserialize($news_tab[$story_limit][notes]);
      $morelink=unserialize($news_tab[$story_limit][morelink]);
      $topicname=unserialize($news_tab[$story_limit][topicname]);
      $topicimage=unserialize($news_tab[$story_limit][topicimage]);
      $topictext=unserialize($news_tab[$story_limit][topictext]);
      $s_id=unserialize($news_tab[$story_limit][id]);
      themeindex($aid, $informant, $datetime, $title, $counter, $topic, $hometext, $notes, $morelink, $topicname, $topicimage, $topictext, $s_id);
   }
   if ($op=="categories") {
      global $storyhome, $cookie;
      if (isset($cookie[3])) {
         $storynum = $cookie[3];
      } else {
         $storynum = $storyhome;
      }
      if (sizeof($news_tab)==$storynum) {
         $marqeur=$marqeur+sizeof($news_tab);
         echo "<br /><p align=\"right\"><a href=\"index.php?op=newindex&amp;catid=$catid&amp;marqeur=$marqeur\" class=\"noir\">".translate("Next Page")."</a></p>";
      }
   }
   if ($op=="topics") {
      global $storyhome, $cookie;
      if (isset($cookie[3])) {
         $storynum = $cookie[3];
      } else {
         $storynum = $storyhome;
      }
      if (sizeof($news_tab)==$storynum) {
         $marqeur=$marqeur+sizeof($news_tab);
         echo "<br /><p align=\"right\"><a href=\"index.php?op=newtopic&amp;topic=$catid&amp;marqeur=$marqeur\" class=\"noir\">".translate("Next Page")."</a></p>";
      }
   }
}

function theindex($Xop, $catid, $marqeur) {
    include("header.php");
    if ($op=="") $op=$Xop;
    // Include cache manager
    global $SuperCache;
    if ($SuperCache) {
       $cache_obj = new cacheManager();
       $cache_obj->startCachingPage();
    }
    if (($cache_obj->genereting_output==1) or ($cache_obj->genereting_output==-1) or (!$SuperCache)) {
       // Appel de la publication de News et la purge automatique
       automatednews();

       global $theme;
       if (($op=="categories") or ($op=="topics")) {
          aff_news($Xop,$catid,$marqeur);
       } else {
          if (file_exists("themes/$theme/central.php")) {
             include("themes/$theme/central.php");
          } else {
             if (($op=="edito") or ($op=="edito-nonews")) {aff_edito();}
             if ($op!="edito-nonews") {aff_news($Xop,$catid,$marqeur);}
          }
       }
    }
    if ($SuperCache) {
       $cache_obj->endCachingPage();
    }
    include("footer.php");
}

switch ($op) {
   case "newindex":
        theindex("categories", $catid, $marqeur);
        break;

   case "newtopic":
        theindex("topics", $topic, $marqeur);
        break;

   default:
        select_start_page($op, "");
        break;
}
?>