<?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(!ispageadmin($userID) || !isnewsadmin($userID)) die('no access!');

	safe_query("INSERT INTO ".PREFIX."links ( linkcatID, name, url, info )
                values( '".$_POST['cat']."', '".$_POST['name']."', '".$_POST['url']."', '".$_POST['info']."' ) ");
	$id=mysql_insert_id();
	
  $banner = $_FILES[banner];

	$filepath = "./images/links/";
	if ($banner[name] != "") {
        move_uploaded_file($banner[tmp_name], $filepath.$banner[name]);
		@chmod($filepath.$banner[name], 0755);
		$file_ext=strtolower(substr($banner[name], strrpos($banner[name], ".")));
		$file=$id.$file_ext;
		if(file_exists($filepath.$file)) @unlink($filepath.$file);
		rename($filepath.$banner[name], $filepath.$file);
		safe_query("UPDATE ".PREFIX."links SET banner='$file' WHERE linkID='$id' ");
	}
	header("Location: index.php?site=links");
}
elseif($_GET['action']=="saveedit") {
	include("_mysql.php");
	include("_settings.php");
  include("_functions.php");
  if(!ispageadmin($userID) || !isnewsadmin($userID)) die('no access!');

	safe_query("UPDATE ".PREFIX."links SET linkcatID='".$_POST['cat']."', name='".$_POST['name']."', url='".$_POST['url']."', info='".$_POST['info']."' WHERE linkID='".$_POST['linkID']."'");
		
	$filepath = "./images/links/";
	$id=$_POST['linkID'];
	$banner = $_FILES[banner];
	if ($banner[name] != "") {
		move_uploaded_file($banner[tmp_name], $filepath.$banner[name]);
		@chmod($filepath.$banner[name], 0755);
		$file_ext=strtolower(substr($banner[name], strrpos($banner[name], ".")));
		$file=$id.$file_ext;
		if(file_exists($filepath.$file)) @unlink($filepath.$file);
		rename($filepath.$banner[name], $filepath.$file);
		safe_query("UPDATE ".PREFIX."links SET banner='$file' WHERE linkID='$id' ");
	}
	header("Location: index.php?site=links");
}
elseif($_GET['action']=="delete") {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
  if(!ispageadmin($userID) || !isnewsadmin($userID)) die('no access!');
	$linkID=$_GET['linkID'];
	safe_query("DELETE FROM ".PREFIX."links WHERE linkID='$linkID'");
	$filepath = "./images/links/";
	if(file_exists($filepath.$linkID.'.jpg')) @unlink($filepath.$linkID.'.jpg');
	if(file_exists($filepath.$linkID.'.gif')) @unlink($filepath.$linkID.'.gif');
	
	header("Location: index.php?site=links");
}

eval ("\$title_links = \"".gettemplate("title_links")."\";");
echo $title_links;

if($_GET['action']=="new") {
    if(ispageadmin($userID) || isnewsadmin($userID)) {
		$rubrics=safe_query("SELECT * FROM ".PREFIX."links_categorys ORDER BY name");
	    while($dr=mysql_fetch_array($rubrics)) {
		    $linkcats.='<option value="'.$dr[linkcatID].'">'.$dr[name].'</option>';
		}
		
		$bg1=BG_1;
		eval ("\$links_new = \"".gettemplate("links_new")."\";");
	    echo $links_new;  
	}
	else redirect('index.php?site=links', 'no access!');
}

elseif($_GET['action']=="edit") {
  	$linkID=$_GET['linkID'];
    if(ispageadmin($userID) || isnewsadmin($userID)) {
	    $ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."links WHERE linkID='$linkID'"));

		$name=$ds[name];
		$url=$ds[url];
		$info=$ds[info];
		
		$newsrubrics=safe_query("SELECT * FROM ".PREFIX."links_categorys ORDER BY name");
		if(mysql_num_rows($newsrubrics)) {
		    while($dr=mysql_fetch_array($newsrubrics)) {
			    if($ds[linkcatID]==$dr[linkcat]) $linkcats.='<option value="'.$dr[linkcatID].'" selected>'.$dr[name].'</option>';
				else $linkcats.='<option value="'.$dr[linkcatID].'">'.$dr[name].'</option>';
			}
		}
		else $linkcats='<option>!! no categorys available !!</option>';
		
		$linkcats=str_replace(" selected", "", $linkcats);
		$linkcats=str_replace('value="'.$ds[linkcatID].'"', 'value="'.$ds[linkcatID].'" selected', $linkcats);
		
    	$bg1=BG_1;	
		eval ("\$links_edit = \"".gettemplate("links_edit")."\";");
	    echo $links_edit;      
	}
	else redirect('index.php?site=links', 'no access!');
}

elseif($_GET['action']=="show") {
	if(ispageadmin($userID) || isnewsadmin($userID)) echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=links&action=new\');return document.MM_returnValue" value="new link"><br><br>';

  $linkcatID=$_GET['linkcatID'];
  $linkID=$_GET['linkID'];
  
  $linkcat=safe_query("SELECT * FROM ".PREFIX."links WHERE linkcatID='$linkcatID' ORDER BY name");
  if(mysql_num_rows($linkcat)) {
    eval ("\$links_details_head = \"".gettemplate("links_details_head")."\";");
      echo $links_details_head;
      $i=1;
	  while($ds=mysql_fetch_array($linkcat)){
      if($i%2) {
        $bg1=BG_1;
        $bg2=BG_2;
        }
        else {
        $bg1=BG_3;
        $bg2=BG_4;
        }
        $i++;
        
        $link='<a href="'.$ds[url].'" target="_blank"><b>'.$ds[name].'</b></a>';
        $info=cleartext($ds[info]);
        if($ds[banner]) $banner='<a href="'.$ds[url].'" target="_blank"><img src="images/links/'.$ds[banner].'" border="0"></a>';
        if(ispageadmin($userID) || isnewsadmin($userID))
          $adminaction='<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=links&action=edit&linkID='.$ds[linkID].'\');return document.MM_returnValue" value="edit"> 
          <input type="button" class="button" onClick="MM_confirm(\'really delete this entry?\', \'links.php?action=delete&linkID='.$ds[linkID].'\')" value="delete">';

        
		  eval ("\$links_details = \"".gettemplate("links_details")."\";");
        echo $links_details;
          unset($banner);
		  }
	  eval ("\$links_foot = \"".gettemplate("links_foot")."\";");
      echo $links_foot;
	}
  else echo 'no links available.<br><br>
             [ <a href="index.php?site=links">go back</a> ]';
}

else {
	if(ispageadmin($userID) || isnewsadmin($userID)) echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=links&action=new\');return document.MM_returnValue" value="new link"><br><br>';

  $cats=safe_query("SELECT * FROM ".PREFIX."links_categorys ORDER BY name");
    if(mysql_num_rows($cats)) {
      $anzcats=mysql_num_rows(safe_query("SELECT linkcatID FROM ".PREFIX."links_categorys"));
      eval ("\$links_category = \"".gettemplate("links_category")."\";");
        echo $links_category;
        $i=1;
      while($ds=mysql_fetch_array($cats)) {
        $anzlinks=mysql_num_rows(safe_query("SELECT linkID FROM ".PREFIX."links WHERE linkcatID='".$ds[linkcatID]."'"));
        if($i%2) {
				  $bg1=BG_1;
          $bg2=BG_2;
				}
        else {
          $bg1=BG_3;
          $bg2=BG_4;
        }
        $linkcatname='&#8226; <a href="index.php?site=links&action=show&linkcatID='.$ds[linkcatID].'"><b>'.$ds[name].'</b></a>';
					
      eval ("\$links_content = \"".gettemplate("links_content")."\";");
        echo $links_content;
        $i++;
      }
    eval ("\$links_foot = \"".gettemplate("links_foot")."\";");
      echo $links_foot;
    }
  else echo 'no categories available.';
}
?>
