<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// main links manager script
	// ========================================================================================
	
	
	$query = "select * from `".$Settings['DBPrefix']."lm-Settings`";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$lmSettings[$row['Name']] = $row['Value'];
	}
	
	
	
	$query = "select * from `".$Settings['DBPrefix']."lm-Categories`";
	$result = mysql_query($query) or die (mysql_error());
	$CategoriesCount = mysql_num_rows($result);
	
	
	
	
		
	
	if ($lmSettings['PageTitle'] != '') {
		$MsgText[8][30] = $lmSettings['PageTitle'];
	}
		
	if ($lmSettings['TitleImage'] != '') {
		$PageTitle = '<div align="'.$AlignmentCodes[$lmSettings['TitleAlignment']].'"><img src="'.$BaseURL.$lmSettings['TitleImage'].'" alt="'.$MsgText[8][30].'" border="0"></div>';	
	}
	else {
		$PageTitle = '<div align="'.$AlignmentCodes[$lmSettings['TitleAlignment']].'">'.$MsgText[8][30].'</div>';
	}
	
	$BrowserTitle = $MsgText[8][30];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[8][30];
	
	
	
	
	if ($Arguments2 == 'alpha') {
	
		$PageTitle = '<div align="'.$AlignmentCodes[$lmSettings['TitleAlignment']].'">'.$MsgText[8][33].'</div>';
		$BrowserTitle = $MsgText[8][33];
		$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="'.$MainURL.'/links.html">'.$MsgText[8][30].'</a> > '.$MsgText[8][33];
	
		
		if ($lmSettings['TopText'] != '') {
			$lmSettings['TopText'] .= '<br><br>';
		}
		
		
			
		$query = "select * from `".$Settings['DBPrefix']."lm-Categories` order by Title asc";
		$result = mysql_query($query) or die (mysql_error());
		while ($row = mysql_fetch_array($result)) {
			$CategoryNames[$row['RecordNumber']] = $row['Title'];
		}
		
		
				
		
		$query = "select * from `".$Settings['DBPrefix']."lm-Links` order by Title asc";
		$result = mysql_query($query) or die (mysql_error());
		while ($row = mysql_fetch_array($result)) {
		
			if ($TitlePrinted[strtolower(substr($row['Title'],0,1))] != 'y') {
				$PageContents .= '<br><a name="'.strtolower(substr($row['Title'],0,1)).'"></a><font size="3"><b>'.strtoupper(substr($row['Title'],0,1)).'</b></font><br>';
				$TitlePrinted[strtolower(substr($row['Title'],0,1))] = 'y';
			}
			
			
			
			if ($row['Description'] != '') {
				$row['Description'] .= '<br>';
			}
			
			if ($lmSettings['ShowClicks'] == 'y') {
				$ShowClicks = '<font size="1"> ('.$MsgText[8][10].': '.number_format($row['ClicksCount']).')</font>';
				$LinkPrint = $MainURL.'/links/click/'.$row['RecordNumber'].'.html';
			}
			else {
				//if ($lmSettings['MaskURL'] == 'y') {
				//	$LinkPrint = $MainURL.'/links/click/'.$row['RecordNumber'].'.html';
				//}
				//else {
					if (strpos($row['Link'], '://', 0) === false) {
						$LinkPrint = 'http://'.$row['Link'];
					}
					else {
						$LinkPrint = $row['Link'];
					}
				//}
			}
			
			
			if ($CategoriesCount > 0) {
				$CategoryPrint = '<font size="1">(in <a href="'.$MainURL.'/links.html#'.$CategoryNames[$row['Category']].'">'.$CategoryNames[$row['Category']].'</a>)</font><br>';			
			}
			
			
			$PageContents .= '<div align="center"><table width="95%" border="0" cellpadding="0" cellspacing="0"><tr><td><font face="'.$FontFace.'" size="'.$FontSize.'"><a href="'.$LinkPrint.'" target="_blank"><b>'.$row['Title'].'</b></a>'.$ShowClicks.'<br>'.$CategoryPrint.$row['Description'].'</td></tr></table></div><br>';
		
		}
		
		
		for ($i = 0; $i <= 25; $i++) {
	
			if ($LettersString != '') {
				$LettersString .= ' &nbsp;';
			}
			
			if ($TitlePrinted[chr($i+97)] == 'y') {
				$LettersString .= '<a href="'.$MainURL.'/links/alpha.html#'.chr($i+97).'">'.chr($i+65).'</a>';
			}
			else {
				$LettersString .= chr($i+65);
			}
	
		}
		
		$PageContents = '<div align="'.$AlignmentCodes[$lmSettings['TopTextAlignment']].'">'.$lmSettings['TopText'].'</div><div align="right"><font size="1"><a href="'.$MainURL.'/links.html">'.$MsgText[8][34].'</a></font></div><br><b>'.$MsgText[8][35].':</b><br><font size="1"><b>'.$LettersString.'</b></font><br><br>'.$PageContents;
		
	
	}
	else if ($Arguments2 == 'click') {
	// redirecting to clicked link destination
		
		$Arguments3 = intval($Arguments3);
		
		if ($Arguments3 > 0) {
			$query = "select * from `".$Settings['DBPrefix']."lm-Links` where RecordNumber='".$Arguments3."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			$query = "update `".$Settings['DBPrefix']."lm-Links` set ClicksCount=ClicksCount+1 where RecordNumber='".$Arguments3."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			
			
			if (strpos($row['Link'], '://', 0) === false) {
				$row['Link'] = 'http://'.$row['Link'];
			}
			
			Header('Location: '.$row['Link']);
		}
	
	}
	else {
	// displaying links list
	
		if ($lmSettings['TopText'] != '') {
			$lmSettings['TopText'] .= '<br><br>';
		}
		
		if ($Arguments2 == '') {
			// main index list
			
			
			$PageContents = '<div align="'.$AlignmentCodes[$lmSettings['TopTextAlignment']].'">'.$lmSettings['TopText'].'</div>';
			
			$PageContents .= '<br>';
			
						
			$query = "select * from `".$Settings['DBPrefix']."lm-Categories` where Parent='0' order by Title asc";
			$result = mysql_query($query) or die (mysql_error());
			if (mysql_num_rows($result) > 0) {
				while ($row = mysql_fetch_array($result)) {
			
					if ($row['Description'] != '') {
						$row['Description'] = '<br>'.$row['Description'];
					}
				
					$PageContents .= '<b><a href="'.$MainURL.'/links/'.$row['RecordNumber'].'.html">'.$row['Title'].'</a></b>'.$row['Description'].'<br><br>';
				}
				
				$CategoriesFound = true;			
			}
			
			
			$query = "select * from `".$Settings['DBPrefix']."lm-Links` where Category='0' order by Title asc";
			$result = mysql_query($query) or die (mysql_error());
			if (mysql_num_rows($result) > 0) {
			
				if ($CategoriesFound == true) {
					$PageContents .= '<br><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" alt="" border="0"></td></tr></table><br>';
				}
			
				while ($row = mysql_fetch_array($result)) {
			
					if ($row['Description'] != '') {
						$row['Description'] = '<br>'.$row['Description'];
					}
					
					if ($lmSettings['ShowClicks'] == 'y') {
						$ShowClicks = '<br><font size="1"> ('.$MsgText[8][10].': '.number_format($row['ClicksCount']).')</font>';
						$LinkPrint = $MainURL.'/links/click/'.$row['RecordNumber'].'.html';
					}
					else {
						if (strpos($row['Link'], '://', 0) === false) {
							$LinkPrint = 'http://'.$row['Link'];
						}
						else {
							$LinkPrint = $row['Link'];
						}
					}
				
					$PageContents .= '<br><a href="'.$LinkPrint.'" target="_blank"><b>'.$row['Title'].'</b></a>'.$row['Description'].$ShowClicks.'<br>';
				}		
			}
			
		}
		else {
		
			$query = "select * from `".$Settings['DBPrefix']."lm-Categories` where RecordNumber='".intval($Arguments2)."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			$CatDescription = '';
			if ($row['Description'] != '') {
				$CatDescription = $row['Description'].'<br>';
			}
			
			$PageTitle = '<div align="'.$AlignmentCodes[$lmSettings['TitleAlignment']].'">'.$row['Title'].'</div>';
			$BrowserTitle = $MsgText[8][30].' - '.$row['Title'];
			
			if ($row['Parent'] == 0) {
							
				$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="'.$MainURL.'/links.html">'.$MsgText[8][30].'</a> > '.$row['Title'];
			
			}
			else {
			
				function BuildNavigationLinks($StartingCategory, $LinksType) {
	
					Global $MainURL, $Settings;
		
	
					$LoopCounter = 0;
					$NavLinks = '';
		
					$CurrentCategoryID = $StartingCategory;
		
		
					while ($CurrentCategoryID != '0' && $LoopCounter < 10) {
						$LoopCounter++;
			
						$query100 = "select * from `".$Settings['DBPrefix']."lm-Categories` where RecordNumber=".$CurrentCategoryID." limit 1";
						$result100 = mysql_query($query100) or die (mysql_error());
						$row100 = mysql_fetch_array($result100);
									
						if ($LinksType == 'category' && $CurrentCategoryID == $StartingCategory) {
							// don't add anything, we're on this category already
						}
						else {
							$NavLinks = '<a href="'.$MainURL.'/links/'.$row100['RecordNumber'].'.html">'.$row100['Title'].'</a> > '.$NavLinks;
						}
						
						$CurrentCategoryID = $row100['Parent'];
			
					}
					
					return $NavLinks;
	
				}
				
				$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="'.$MainURL.'/links.html">'.$MsgText[8][30].'</a> > '.BuildNavigationLinks($row['RecordNumber'],'category').$row['Title'];
			
			}
			
			$PageContents .= $CatDescription;
			
			
			$query = "select * from `".$Settings['DBPrefix']."lm-Categories` where Parent='".intval($Arguments2)."' order by Title asc";
			$result = mysql_query($query) or die (mysql_error());
			if (mysql_num_rows($result) > 0) {
			
				$PageContents .= '<br>';
			
				while ($row = mysql_fetch_array($result)) {
			
					if ($row['Description'] != '') {
						$row['Description'] = '<br>'.$row['Description'];
					}
				
					$PageContents .= '<b><a href="'.$MainURL.'/links/'.$row['RecordNumber'].'.html">'.$row['Title'].'</a></b>'.$row['Description'].'<br><br>';
				}
				
				$CategoriesFound = true;			
			}
			
			
			$query = "select * from `".$Settings['DBPrefix']."lm-Links` where Category='".intval($Arguments2)."' or Category2='".intval($Arguments2)."' or Category3='".intval($Arguments2)."' or Category4='".intval($Arguments2)."' or Category5='".intval($Arguments2)."' or Category6='".intval($Arguments2)."' or Category7='".intval($Arguments2)."' or Category8='".intval($Arguments2)."' or Category9='".intval($Arguments2)."' or Category10='".intval($Arguments2)."' order by Title asc";
			$result = mysql_query($query) or die (mysql_error());
			if (mysql_num_rows($result) > 0) {
			
				if ($CategoriesFound == true) {
					$PageContents .= '<br><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" alt="" border="0"></td></tr></table><br>';
				}
			
				while ($row = mysql_fetch_array($result)) {
			
					if ($row['Description'] != '') {
						$row['Description'] = '<br>'.$row['Description'];
					}
					
					if ($lmSettings['ShowClicks'] == 'y') {
						$ShowClicks = '<br><font size="1"> ('.$MsgText[8][10].': '.number_format($row['ClicksCount']).')</font>';
						$LinkPrint = $MainURL.'/links/click/'.$row['RecordNumber'].'.html';
					}
					else {
						if (strpos($row['Link'], '://', 0) === false) {
							$LinkPrint = 'http://'.$row['Link'];
						}
						else {
							$LinkPrint = $row['Link'];
						}
					}
				
					$PageContents .= '<br><a href="'.$LinkPrint.'" target="_blank"><b>'.$row['Title'].'</b></a>'.$row['Description'].$ShowClicks.'<br>';
				}		
			}
			else {
			
				if ($CategoriesFound != true) {
				
					$PageContents = '<br>[No links exist in this category]';
				
				}
			
			}
			
		}
	
		
		
		
		
		
		
	}
			
	
	
			
	
	// ========================================================================================




?>
