<?PHP
require('config.php');
require('article.php');
require_once('libs.php');


$Directory =& getFiles();

$about=false;

if(isset($_GET['about'])){
  $about=true;
}


$search = $cfg['FilePrefix'];
$ArticleFiles = getCmsArticles($Directory,$search);

$i = 0;
foreach($ArticleFiles As $file){
    $content = file_get_contents($file);
    $title   = getTitle($content);
    $author  = getAuthor($content);
    $date    = getDatum($content,$file);
    $Category= getCategory($content);
    $body    = getBody($file);


    $art_array[$i]=& new article($title,$Category,$file);
    $art_array[$i]-> setAut($author);
    $art_array[$i]-> setDat($date);
    $art_array[$i]-> setBod($body);
    $i++; 
}
/* This function is necessary to register a destructor method for php4 
/* it should not be neccessary in version 5 of PHP */
/* deactivated, causes it throws a warning in php4 */
/* register_shutdown_function('article::destruct'); */

/* Registriere Funktion zum Sortieren anhand des Datums und sortiere Array */
usort($art_array, "cmp_obj");
/* Setze den Array Zeiger auf den Anfang */
reset($art_array);

#$nr_art=$art_array[3]::getInstances(); 
/* $nr_art=count($art_array); */
#print "Art: ".$nr_art;

 $nr_art=$art_array[0]->GetInstances();
/* $nr_art=count($art_array); */

/* Prüfe, ob keine Parameter in der Adresszeile angegeben wurden, wenn ja
 * dann muß die Startseite angezeigt werden */
if (!isset($_GET["article"])&&!isset($_GET["cat"]))  {
    global $cfg;
    /* 0 heißt Startseite: Lese die Startseite ein*/
    $requ_art=0;
    $file = $cfg['Datadir'].'/'.$cfg['Startpage'];
    $startpage=file_get_contents($file);
    $title = getTitle($startpage);
    $author = getAuthor($startpage);
    $date  = getDatum($startpage,$file);
    $category = getCategory($startpage);
    $body = getBody($file);
    
    $art = new article($title,null, $file);
    $art->setAut($author);
    $art->setDat($date);
    /* Category wird für Startartikel nicht benötigt, sie bleibt sowieso leer */
    //$art->setCat();
    $art->setBod($body);
    array_unshift($art_array,$art);
}
/* Falls irgendein Parameter angegeben wurde, initialisiere zunächst 
 * den Artikel Counter, der angibt, welcher Artikel angezeigt werden soll */
else {
    if (!isset($_GET["article"])){
	$requ_art=1;
    }
    else {
      (int) $requ_art=$_GET["article"];
    }
    if (($requ_art > $nr_art) || $requ_art < 1) {
	$requ_art=1;
    }
}
/* Wurde nach etwas gesucht? Falls ja, muß die Suchanfrage bearbeitet werden 
 This case: no search*/
if(!isset($_POST["suche"])) {
    global $cfg;

    /* Ist eine Kategorie angegeben worden */
    if (!isset($_GET["cat"]))  {
	$category='all';
	$navi=printNaviTit($art_array,$requ_art,$category);
    }
    else {
	$category=addslashes(rawurldecode(($_GET["cat"])));
	$navi=printNaviTit($art_array,$requ_art,$category);
	/* Prüfe, ob die angegebene Kategorie existiert. Falls ja, verkleinere das Array nur auf 
	 * die passenden Artikel */

	if (checkCatExistence($art_array,$category)){
	    outputSelectedCategory($art_array,$category);
	    /*$nr_art=article::$instance; */
	    $nr_art=count($art_array);
	    if (($requ_art > $nr_art)) {
		$requ_art=1;
	    }
	}
    }

    /* Alle Parameter in einem Array speichern */
    $params=array('article'=>$requ_art, 'cat'=>$category);
     
    if($requ_art!=0){
	print(createHTMLhead($cfg['PageTitle'],$art_array[$requ_art-1]->getCat(), $art_array[$requ_art-1]->getTit()));
    }
    else{
	print(createHTMLhead($cfg['PageTitle'],null, $art_array[$requ_art+1]->getTit()));
    }

    $prevLink=null;
    $nextLink=null;
    
    /* Hole den Link zum vorherigen Artikel */
    if (($requ_art!=1)&&(!$about)) {
	$params['article']+=-1;
	$prevLink=pLink($_SERVER['SCRIPT_NAME'],$params);
	$params['article']+=1;
    }

    /* Hole den Link zum nächsten Artikel */
    if (($requ_art<$nr_art) && (!$about)) {
	$params['article']+=1;
	$nextLink=pLink($_SERVER['SCRIPT_NAME'],$params);
	$params['article']+=-1;
    }

    /* Soll ein Artikel angezeigt werden, oder die About-Seite? */
    if (!$about)  {
	if($requ_art!=0){
	    print Navibar($nextLink,$prevLink,$art_array[$params['article']-1]->getTit());
	    print($art_array[$params['article']-1]->output());
	    print '<div class="bottom">';
	    print Navibar($nextLink,$prevLink,$art_array[$params['article']-1]->getTit());
	}
	else {
	    print Navibar($nextLink,null,$art_array[$params['article']]->getTit());
	    print($art_array[$params['article']]->output());
	    print '<div class="bottom">';
	    print Navibar($nextLink,null,$art_array[$params['article']]->getTit());
	}

	$about='<p/><div class="centered">This page is proudly presented by a lonely ';
	$about.='<a href="'.$_SERVER['SCRIPT_NAME'].'?about">php-script</a>.</div>'."\n<!-- Ende Bottom-->\n</div>\n";
	print $about;
    }
    else {
	print Navibar($nextLink,$prevLink,'');
	about();
	print Navibar($nextLink,$prevLink,'');
    }
    print '<!--Ende Div Main --></div>';
}

/* Bearbeite Suchanfrage */
else {
    print(createHTMLhead($cfg['PageTitle'],null, null));
    print Navibar(null,null,'Suchanfrage');
    $navi=printNaviTit($art_array,1,null)."\n";
    $resultArt=array();
    $anzahl = count($art_array);
    for ($i=0; $i < $anzahl; $i++){
    /* Suche in jedem Artikel nach dem übergebenen Suchstring (caseinsensitiv) */
        $Body=$art_array[$i]->getBod();
	$Title=$art_array[$i]->getTit();
	$pos1 = stripos($Body, $_POST['suche']);
	$pos2 = stripos($Title, $_POST['suche']);
	if (!$pos1 === false || !$pos2 === false){
	    $resultArt[$i]=$art_array[$i]->getTit();
	}
    }
    $html='';
    $html.= '<div id="content"><p />';
    if (empty($resultArt)){
        /* Nichts gefunden */
	$html.="<p />Keine passenden Artikel gefunden.<p />";
    }
    else{
        /* Output der gefunden Artikel */
	foreach($resultArt as $key=>$value){
	  if (strpos($value,$cfg['Starttitle'])=== false){
	    $html.= "<a href=\"".$_SERVER['SCRIPT_NAME']."?article=".$key."\">";
	    $html.=$value . "</a></br>";
	  }
	  else {
	    $html.='<a href="'.$_SERVER['SCRIPT_NAME'];
	    $html.="\">".$value . "</a></br>";
	  }
	}
    }
    $html .= "<p/></div>\n";
    $html .= "<!-- Bottom Navigation -->\n";
    $html .= '<div class="bottom">'.Navibar(null,null,'Suchanfrage')."<!-- Ende Bottom --></div>\n";
    $html .='<p/><div class="centered">This page is proudly presented by a lonely ';
    $html .='<a href="'.$_SERVER['SCRIPT_NAME'].'?about">php-script</a>.</div>'."\n<!-- Ende Bottom-->\n</div>\n";
    $html .="<!-- Ende Main --></div>\n";
    print($html);
}
print($navi);
createHTMLfoot();

?>
