<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

if($_GET['action']=="save") {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");

  if(!isnewsadmin($userID)) die('no access!');
  $title = $_POST['title'];
  $message = $_POST['message'];
  $link1 = $_POST['link1'];
  $url1 = $_POST['url1'];
  $window1 = $_POST['window1'];
  $link2 = $_POST['link2'];
  $url2 = $_POST['url2'];
  $window2 = $_POST['window2'];
  $link3 = $_POST['link3'];
  $url3 = $_POST['url3'];
  $window3 = $_POST['window3'];
  $link4 = $_POST['link4'];
  $url4 = $_POST['url4'];
  $window4 = $_POST['window4'];
  $comments = $_POST['comments'];
  $articlesID = $_POST['articlesID'];

    safe_query("UPDATE ".PREFIX."articles SET 
								 title='".mysql_escape_string($title)."',
								 content='".mysql_escape_string($message)."',
								 link1='".mysql_escape_string($link1)."',
								 url1='".mysql_escape_string($url1)."',
								 window1='".mysql_escape_string($window1)."',
								 link2='".mysql_escape_string($link2)."',
								 url2='".mysql_escape_string($url2)."',
								 window2='".mysql_escape_string($window2)."',
								 link3='".mysql_escape_string($link3)."',
								 url3='".mysql_escape_string($url3)."',
								 window3='".mysql_escape_string($window3)."',
								 link4='".mysql_escape_string($link4)."',
								 url4='".mysql_escape_string($url4)."',
								 window4='".mysql_escape_string($window4)."',
								 saved='1',
								 comments='$comments' WHERE articlesID='$articlesID' ");
							 
	// löschen der einträge die älter als 1 stunde sind und keinen text enthalten
	$ergebnis=safe_query("SELECT * FROM ".PREFIX."articles WHERE saved='0'");
	while($ds=mysql_fetch_array($ergebnis)) {
		if((time()-$ds[date]) > (60*60)) safe_query("DELETE FROM ".PREFIX."articles WHERE articlesID='$ds[articlesID]'");
	}
	
	echo'<body onLoad="window.close()"></body>';
}
elseif($_GET['delete']) {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");

  if(!isnewsadmin($userID)) die('no access!');

    $ds=mysql_fetch_array(safe_query("SELECT screens FROM ".PREFIX."articles WHERE articlesID='".$_GET['articlesID']."'"));
	if($ds[screens]) {
		$screens=explode("|", $ds[screens]);
		if(is_array($screens)) {
			$filepath = "./images/articles-pics/";
			foreach($screens as $screen) {
				if(file_exists($filepath.$screen)) @unlink($filepath.$screen);
			}
		}
	}
	
	safe_query("DELETE FROM ".PREFIX."articles WHERE articlesID='".$_GET['articlesID']."'");
	safe_query("DELETE FROM ".PREFIX."comments WHERE parentID='".$_GET['articlesID']."' AND type='ar'");
	
	if($close) echo'<body onLoad="window.close()"></body>';
	else header("Location: index.php?site=articles");
}

function top5() {
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;
	
	echo'<table width="100%" border="0" cellspacing="0" cellpadding="0">
	       <tr><td width="49%" valign="top">';
	// RATING
	$ergebnis=safe_query("SELECT * FROM ".PREFIX."articles WHERE saved='1' ORDER BY rating DESC LIMIT 0,5");
	$top='TOP 5 ARTICLES (rating)';
	eval ("\$top5_head = \"".gettemplate("top5_head")."\";");
	echo $top5_head;
	$n=1;
	while($ds=mysql_fetch_array($ergebnis)) {
	    if($n%2) {
			$bg1=BG_1;
			$bg2=BG_2;
		}
		else {
			$bg1=BG_3;
			$bg2=BG_4;
		}
		
		$title='<a href="index.php?site=articles&action=show&articlesID='.$ds[articlesID].'">'.clearfromtags($ds[title]).'</a>';
		$poster='<a href="index.php?site=profile&id='.$ds[poster].'">'.getnickname($ds[poster]).'</a>';
		$viewed='('.$ds[viewed].')';
		$ratings=array(0,0,0,0,0,0,0,0,0,0);
		for($i=0; $i<$ds[rating]; $i++) {
		    $ratings[$i]=1;
		}
		$ratingpic='<img src="images/icons/rating_'.$ratings[0].'_start.gif" width="1" height="5">';
		foreach($ratings as $pic) {
		    $ratingpic.='<img src="images/icons/rating_'.$pic.'.gif" width="4" height="5">';
		}
		echo'<tr> 
		       <td bgcolor="'.$bg1.'" align="center"><b>'.$n.'.</b></td>
			   <td bgcolor="'.$bg2.'" align="center">'.$ratingpic.'</td>
			   <td bgcolor="'.$bg1.'">'.$title.'</td>
			 </tr>';
		unset($ratingpic);	
		$n++; 
	}
	echo'</table>';
	echo'</td><td width="2%">&nbsp;</td><td width="49%" valign="top">';
	// POINTS
	$ergebnis=safe_query("SELECT * FROM ".PREFIX."articles WHERE saved='1' ORDER BY points DESC LIMIT 0,5");
	$top='TOP 5 ARTICLES (points)';
	eval ("\$top5_head = \"".gettemplate("top5_head")."\";");
	echo $top5_head;
	$n=1;
	while($ds=mysql_fetch_array($ergebnis)) {
	  $title='<a href="index.php?site=articles&action=show&articlesID='.$ds[articlesID].'">'.clearfromtags($ds[title]).'</a>';
		$viewed='('.$ds[viewed].')';
		echo'<tr> 
		       <td bgcolor="'.$bg1.'" align="center"><b>'.$n.'.</b></td>
			   <td bgcolor="'.$bg2.'" align="center">'.$ds[points].'</td>
			   <td bgcolor="'.$bg1.'">'.$title.'</td>
			 </tr>';
		$n++;
	}
	echo'</table></td></tr></table><br>';
}

if($_GET['action']=="new") {
    include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
	
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;
	
	echo'<script src="js/bbcode.js" language="jscript" type="text/javascript"></script>
		 <link href="_stylesheet.css" rel="stylesheet" type="text/css">';
	
    if(isnewsadmin($userID)) {
		safe_query("INSERT INTO ".PREFIX."articles ( date, poster, saved ) VALUES( '".time()."', '$userID', '0' ) ");
		$articlesID=mysql_insert_id();
				
		$bg1=BG_1;
		eval ("\$addbbcode = \"".gettemplate("addbbcode")."\";");
		eval ("\$addflags = \"".gettemplate("flags")."\";");
		
		eval ("\$articles_post = \"".gettemplate("articles_post")."\";");
	    echo $articles_post;  
	}
	else redirect('index.php?site=articles', 'no access!');
}
elseif($_GET['action']=="edit") {

  include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
	
	$articlesID = $_GET['articlesID'];
	
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;
	
	echo'<script src="js/bbcode.js" language="jscript" type="text/javascript"></script>
		 <link href="_stylesheet.css" rel="stylesheet" type="text/css">';	
	
    if(isnewsadmin($userID)) {
		$ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."articles WHERE articlesID='$articlesID'"));
		
		$title=getinput($ds[title]);
		$message=getinput($ds[content]);

		$link1=getinput($ds[link1]);
		$link2=getinput($ds[link2]);
		$link3=getinput($ds[link3]);
		$link4=getinput($ds[link4]);
		$url1=getinput($ds[url1]);
		$url2=getinput($ds[url2]);
		$url3=getinput($ds[url3]);
		$url4=getinput($ds[url4]);
		if($ds[window1]) $window1='<input class="input" name="window1" type="radio" value="1" checked> new window <input class="input" type="radio" name="window1" value="0"> _blank';
		else $window1='<input class="input" name="window1" type="radio" value="1"> new window <input class="input" type="radio" name="window1" value="0" checked> _self';
		
		if($ds[window2]) $window2='<input class="input" name="window2" type="radio" value="1" checked> new window <input class="input" type="radio" name="window2" value="0"> _blank';
		else $window2='<input class="input" name="window2" type="radio" value="1"> new window <input class="input" type="radio" name="window2" value="0" checked> _self';
		
		if($ds[window3]) $window3='<input class="input" name="window3" type="radio" value="1" checked> new window <input class="input" type="radio" name="window3" value="0"> _blank';
		else $window3='<input class="input" name="window3" type="radio" value="1"> new window <input class="input" type="radio" name="window3" value="0" checked> _self';
		
		if($ds[window4]) $window4='<input class="input" name="window4" type="radio" value="1" checked> new window <input class="input" type="radio" name="window4" value="0"> _blank';
		else $window4='<input class="input" name="window4" type="radio" value="1"> new window <input class="input" type="radio" name="window4" value="0" checked> _self';
	
		$comments='<option value="0">disable comments</option><option value="1">enable user comments</option><option value="2">enable visitor comments</option>';								
		$comments=str_replace('value="'.$ds[comments].'"', 'value="'.$ds[comments].'" selected', $comments);
		
		$bg1=BG_1;
		eval ("\$addbbcode = \"".gettemplate("addbbcode")."\";");
		eval ("\$addflags = \"".gettemplate("flags")."\";");
		
		eval ("\$articles_edit = \"".gettemplate("articles_edit")."\";");
	    echo $articles_edit; 
	}
	else redirect('index.php?site=articles', 'no access!');
}
elseif($_GET['action']=="show") {
	eval ("\$title_articles = \"".gettemplate("title_articles")."\";");
	echo $title_articles;
	
  $articlesID = $_GET['articlesID'];
	
	if(isnewsadmin($userID)) echo'<input type="button" class="button" onClick="MM_openBrWindow(\'articles.php?action=new\',\'Articles\',\'toolbar=no,status=no,scrollbars=yes,width=800,height=600\')" value="new Article"> ';
	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=articles\');return document.MM_returnValue" value="all Articles"><br><br>';
	
	safe_query("UPDATE ".PREFIX."articles SET viewed=viewed+1 WHERE articlesID='$articlesID'");
	$result=safe_query("SELECT * FROM ".PREFIX."articles WHERE articlesID='$articlesID'");
	if(mysql_num_rows($result)) {
        $ds=mysql_fetch_array($result);
		$date = date("d.m.Y", $ds[date]);
		$time = date("H:i", $ds[date]);
		$title = clearfromtags($ds[title]);
		$content = htmloutput($ds[content]);
		$content = toggle($content, $ds[articlesID]);

		$poster='<a href="index.php?site=profile&id='.$ds[poster].'"><b>'.getnickname($ds[poster]).'</b></a>';
		if($ds[link1] && $ds[url1]!="http://" && $ds[window1]) $related.='&#8226; <a href="'.$ds[url1].'" target="_blank">'.$ds[link1].'</a> ';
		if($ds[link1] && $ds[url1]!="http://" && !$ds[window1]) $related.='&#8226; <a href="'.$ds[url1].'">'.$ds[link1].'</a> ';
		
		if($ds[link2] && $ds[url2]!="http://" && $ds[window2]) $related.='&#8226; <a href="'.$ds[url2].'" target="_blank">'.$ds[link2].'</a> ';
		if($ds[link2] && $ds[url2]!="http://" && !$ds[window2]) $related.='&#8226; <a href="'.$ds[url2].'">'.$ds[link2].'</a> ';
				
		if($ds[link3] && $ds[url3]!="http://" && $ds[window3]) $related.='&#8226; <a href="'.$ds[url3].'" target="_blank">'.$ds[link3].'</a> ';
		if($ds[link3] && $ds[url3]!="http://" && !$ds[window3]) $related.='&#8226; <a href="'.$ds[url3].'">'.$ds[link3].'</a> ';
				
		if($ds[link4] && $ds[url4]!="http://" && $ds[window4]) $related.='&#8226; <a href="'.$ds[url4].'" target="_blank">'.$ds[link4].'</a> ';
		if($ds[link4] && $ds[url4]!="http://" && !$ds[window4]) $related.='&#8226; <a href="'.$ds[url4].'">'.$ds[link4].'</a> ';
		if(empty($related)) $related="n/a";
		
		$comments_allowed = $ds[comments];
		
		$ratings=array(0,0,0,0,0,0,0,0,0,0);
		for($i=0; $i<$ds[rating]; $i++) {
		    $ratings[$i]=1;
		}
		$ratingpic='<img src="images/icons/rating_'.$ratings[0].'_start.gif" width="1" height="5">';
		foreach($ratings as $pic) {
		    $ratingpic.='<img src="images/icons/rating_'.$pic.'.gif" width="4" height="5">';
		}
		
		if(isnewsadmin($userID)) $adminaction='<br><br><input type="button" class="button" onClick="MM_openBrWindow(\'articles.php?action=edit&articlesID='.$ds[articlesID].'\',\'News\',\'toolbar=no,status=no,scrollbars=yes,width=800,height=600\')" value="edit"> 
		                                       <input type="button" class="button" onClick="MM_confirm(\'really delete this entry?\', \'articles.php?delete=true&articlesID='.$ds[articlesID].'\')" value="delete">';
		else $adminaction='';
		
		if($loggedin) {
			$getarticles=safe_query("SELECT articles FROM ".PREFIX."user WHERE userID='$userID'");
			$found=false;
			if(mysql_num_rows($getarticles)) {
				$ga=mysql_fetch_array($getarticles);
				if($ga[articles]!="") {
	    			$string=$ga[articles];
					$array=explode(":", $string);
    				$anzarray=count($array);
	    				for($i=0; $i<$anzarray; $i++) {
        				if($array[$i]==$articlesID) $found=true;
    				}
				}
			}
			if($found) $rateform="<b>You already rated this article.</b>";	
			else $rateform='<table cellspacing="0" cellpadding="2" align="right">
						 	<tr><form method="post" action="rating.php">
								<td> 
       							(rate with 0-10 points) <select name="rating">
								  <option>0 - poor</option>
								  <option>1</option>
								  <option>2</option>
								  <option>3</option>
								  <option>4</option>
								  <option>5</option>
								  <option>6</option>
								  <option>7</option>
								  <option>8</option>
								  <option>9</option>
								  <option>10 - perfect</option>
								</select>
								<input type="hidden" name="userID" value="'.$userID.'">
								<input type="hidden" name="type" value="ar">
								<input type="hidden" name="id" value="'.$ds[articlesID].'">
       							<input type="submit" name="Submit" value="rate"> 
							</td></form>
 							</tr>
							</table>';
		}
		else $rateform="<br><b>To rate this article you have to be registered and logged in!</b>";	

        $bg1=BG_1;
		eval ("\$articles = \"".gettemplate("articles")."\";");
        echo $articles;
			
		unset($related);
		unset($comments);
		unset($lang);
		unset($ds);
		unset($ratingpic);
		
		$parentID = $articlesID;
		$type = "ar";
		$referer = "index.php?site=articles&action=show&articlesID=$articlesID";
		
		include("comments.php");
	}
	else echo'no entries!';
}
else {

	$page = $_GET['page'];
	$sort = $_GET['sort'];
	$type = $_GET['type'];
	
	eval ("\$title_articles = \"".gettemplate("title_articles")."\";");
	echo $title_articles;
	if(isnewsadmin($userID)) echo'<input type="button" class="button" onClick="MM_openBrWindow(\'articles.php?action=new\',\'Articles\',\'toolbar=no,status=no,scrollbars=yes,width=800,height=600\')" value="new Article"> <br><br>';

	$alle=safe_query("SELECT articlesID FROM ".PREFIX."articles WHERE saved='1'");
	$gesamt = mysql_num_rows($alle);
	$pages=1;
	if(!isset($page)) $page = 1;
	if(!isset($sort)) $sort = "date";
	if(!isset($type)) $type = "DESC";
	
	$max=$maxarticles;
		
    for ($n=$max; $n<=$gesamt; $n+=$max) {
	    if($gesamt>$n) $pages++;
	}
		 	
	if($pages>1) $page_link = makepagelink("index.php?site=articles&sort=$sort&type=$type", $page, $pages);
		
	if ($page == "1") {
        $ergebnis = safe_query("SELECT * FROM ".PREFIX."articles WHERE saved='1' ORDER BY $sort $type LIMIT 0,$max");
	    if($type=="DESC") $n=$gesamt;
		else $n=1;
	}
	else {
	    $start=$page*$max-$max;
	    $ergebnis = safe_query("SELECT * FROM ".PREFIX."articles WHERE saved='1' ORDER BY $sort $type LIMIT $start,$max");
	    if($type=="DESC") $n = ($gesamt)-$page*$max+$max;
		else $n = ($gesamt+1)-$page*$max+$max;
	}
	if($gesamt) {
	    top5();
	    if($type=="ASC") 
		        echo'<a href="index.php?site=articles&page='.$page.'&sort='.$sort.'&type=DESC">Sort:</a> <img src="images/icons/asc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';
		    else 
		        echo'<a href="index.php?site=articles&page='.$page.'&sort='.$sort.'&type=ASC">Sort:</a> <img src="images/icons/desc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';

		
		if($pages>1) echo $page_link;
		echo'<br><br>';
		eval ("\$articles_head = \"".gettemplate("articles_head")."\";");
	    echo $articles_head;
		$n=1;
		while($ds=mysql_fetch_array($ergebnis)) {
			if($n%2) {
				$bg1=BG_1;
				$bg2=BG_2;
			}
			else {
				$bg1=BG_3;
				$bg2=BG_4;
			}
			$date=date("d.m.Y", $ds[date]);
			$rubric=getrubricname($ds[rubric]);
			
			$title='<a href="index.php?site=articles&action=show&articlesID='.$ds[articlesID].'">'.clearfromtags($ds[title]).'</a>';
			$poster='<a href="index.php?site=profile&id='.$ds[poster].'"><b>'.getnickname($ds[poster]).'</b></a>';
			$viewed='('.$ds[viewed].')';
			
			$ratings=array(0,0,0,0,0,0,0,0,0,0);
			for($i=0; $i<$ds[rating]; $i++) {
		    $ratings[$i]=1;
			}
			$ratingpic='<img src="images/icons/rating_'.$ratings[0].'_start.gif" width="1" height="5">';
			foreach($ratings as $pic) {
		    $ratingpic.='<img src="images/icons/rating_'.$pic.'.gif" width="4" height="5">';
			}
			
			eval ("\$articles_content = \"".gettemplate("articles_content")."\";");
    	echo $articles_content;
			unset($ratingpic);
			$n++;
		}
		eval ("\$articles_foot = \"".gettemplate("articles_foot")."\";");
	    echo $articles_foot;
		unset($ds);
	}
	else echo'no entries';
}

?>
