<?

/*
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 newsletter script
	// ========================================================================================
	
	$query = "select * from `".$Settings['DBPrefix']."nl-Settings`";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$nlSettings[$row['Name']] = $row['Value'];
	}
	
	
	$AlignmentCodes[''] = 'left';
	$AlignmentCodes['L'] = 'left';
	$AlignmentCodes['C'] = 'center';
	$AlignmentCodes['R'] = 'right';
	
	
	
	if ($Arguments2 == 'subscribe') {
		
		$PageTitle = $MsgText[5][48];
		$BrowserTitle = $MsgText[5][48];
		$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[5][48];
		
		
		$PageContents .= '<br>'.$MsgText[5][49].'<br><br><form action="'.$MainURL.'/newsletter/subscribe_submit" method="post">
<input type="text" name="Email" size="40" maxlength="50" style="'.$FormStyle.'">&nbsp;&nbsp;<input type="submit" value="'.$MsgText[5][50].'" style="'.$FormStyle.'">
</form><br>'.$MsgText[5][51].' <a href="'.$MainURL.'/newsletter/unsubscribe.html">'.$MsgText[5][52].'</a> '.$MsgText[5][53];
		
	
	}
	else if ($Arguments2 == 'subscribe_submit') {
		
		$PageTitle = $MsgText[5][48];
		$BrowserTitle = $MsgText[5][48];
		$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[5][48];
		
		
		ReadFormArguments();
		Global $FormArguments;
		
		$FormArguments['Email'] = VerifyCleanString($FormArguments['Email'],1);
		
		//if (count($FormArguments) > 0 && $FormArguments['Email'] != '') {
		if (count($FormArguments) > 0 && $FormArguments['Email'] != '' && strlen($FormArguments['Email']) >= 5) {
		
			$FormArguments['Email'] = addslashes($FormArguments['Email']);
		
			$query = "select * from `".$Settings['DBPrefix']."nl-Addresses` where Email='".$FormArguments['Email']."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			if (mysql_num_rows($result) == 0) {
				
				
				$Chars = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
				for ($i = 1; $i <= 20; $i++) {
					$RandomNum = rand(0,35);
					$ConfirmationCode .= $Chars[$RandomNum];
				}
				
				
				$query = "insert into `".$Settings['DBPrefix']."nl-Addresses` (Email, DateAdded, ConfirmationCode) values ('".$FormArguments['Email']."','".GetTimestamp()."','".$ConfirmationCode."')";
				$result = mysql_query($query) or die (mysql_error());
				
				
				if ($nlSettings['DoubleOptInMode'] == 'y') {
					$Message = "\n".$MsgText[5][54]."\n\n".$MainURL."/newsletter/subscribe_confirm/".$FormArguments['Email']."/".$ConfirmationCode."\n\n\n".$MsgText[5][55]."\n\n\n\n".$Settings['SiteName']."\n".$BaseURL;
					
					$DoubleModeMessagePrint = ' '.$MsgText[5][56];
				}
				else {
					$Message = "\n".$MsgText[5][57]."\n\n\n\n".$Settings['SiteName']."\n".$BaseURL;
				}
				mail($FormArguments['Email'], 'Newsletter Subscription', $Message, 'From: "'.$Settings['SiteName'].'" <'.$Settings['ContactEmail'].'>','-f'.$Settings['ContactEmail']);
				
				
				
				$PageContents = '<br>'.$MsgText[5][58].$DoubleModeMessagePrint.'.<br><br>'.$MsgText[5][51].' <a href="'.$MainURL.'/newsletter/unsubscribe.html">'.$MsgText[5][52].'</a> '.$MsgText[5][53].'<br><br><a href="'.$MainURL.'/index.html"><b>'.$MsgText[0][1].'</b></a>';
				
			}
			else {
			
				$PageContents = '<br>'.$MsgText[5][59];
			
			}
				
		
		}
		else {
			$PageContents = $MsgText[0][98];
		}
	
	}
	else if ($Arguments2 == 'subscribe_confirm') {
		
		$PageTitle = $MsgText[5][48];
		$BrowserTitle = $MsgText[5][48];
		$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[5][48];
		
		
		$Arguments3 = addslashes($Arguments3);
		$Arguments4 = addslashes($Arguments4);
		
		$query = "select * from `".$Settings['DBPrefix']."nl-Addresses` where Email='".$Arguments3."' and ConfirmationCode='".$Arguments4."' limit 1";
		$result = mysql_query($query) or die (mysql_error());
		if (mysql_num_rows($result) > 0) {
			
			$query = "update `".$Settings['DBPrefix']."nl-Addresses` set DoubleConfirmed='y' where Email='".$Arguments3."' and ConfirmationCode='".$Arguments4."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			
			
			$PageContents = $MsgText[5][60].'<br><br><a href="'.$MainURL.'/index.html"><b>'.$MsgText[0][1].'</b></a>';
			
		}
		else {
		
			$PageContents = $MsgText[5][61];
		
		}
		
	
	}
	else if ($Arguments2 == 'unsubscribe') {
		
		$PageTitle = $MsgText[5][62];
		$BrowserTitle = $MsgText[5][62];
		$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[5][62];
		
		
		$PageContents .= '<br>'.$MsgText[5][63].'<br><br><form action="'.$MainURL.'/newsletter/unsubscribe_submit" method="post">
<input type="text" name="Email" size="40" maxlength="50" style="'.$FormStyle.'">&nbsp;&nbsp;<input type="submit" value="Unsubscribe" style="'.$FormStyle.'">
</form><br>'.$MsgText[5][64];
		
	
	}
	else if ($Arguments2 == 'unsubscribe_submit') {
		
		$PageTitle = $MsgText[5][62];
		$BrowserTitle = $MsgText[5][62];
		$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[5][62];
		
		
		ReadFormArguments();
		Global $FormArguments;
			
		if (count($FormArguments) > 0 && $FormArguments['Email'] != '') {
		
			$FormArguments['Email'] = addslashes($FormArguments['Email']);
		
			$query = "select * from `".$Settings['DBPrefix']."nl-Addresses` where Email='".$FormArguments['Email']."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			if (mysql_num_rows($result) > 0) {
			
				$row = mysql_fetch_array($result);
				
				$Message = "\n".$MsgText[5][65]."\n\n".$MainURL."/newsletter/unsubscribe_confirm/".$FormArguments['Email']."/".$row['ConfirmationCode']."\n\n\n".$MsgText[5][66]."\n\n\n\n".$Settings['SiteName']."\n".$BaseURL;
					
				
				mail($FormArguments['Email'], 'Newsletter Unsubscribe Request', $Message, 'From: "'.$Settings['SiteName'].'" <'.$Settings['ContactEmail'].'>','-f'.$Settings['ContactEmail']);
				
				
				
				$PageContents = '<br>'.$MsgText[5][67].'<br><br><a href="'.$MainURL.'/index.html"><b>'.$MsgText[0][1].'</b></a>';
				
			}
			else {
			
				$PageContents = '<br>'.$MsgText[5][68];
			
			}
				
		
		}
		else {
			$PageContents = $MsgText[0][98];
		}
	
	}
	else if ($Arguments2 == 'unsubscribe_confirm') {
		
		$PageTitle = $MsgText[5][62];
		$BrowserTitle = $MsgText[5][62];
		$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[5][62];
		
		
		$Arguments3 = addslashes($Arguments3);
		$Arguments4 = addslashes($Arguments4);
		
		$query = "select * from `".$Settings['DBPrefix']."nl-Addresses` where Email='".$Arguments3."' and ConfirmationCode='".$Arguments4."' limit 1";
		$result = mysql_query($query) or die (mysql_error());
		if (mysql_num_rows($result) > 0) {
			
			$query = "delete from `".$Settings['DBPrefix']."nl-Addresses` where Email='".$Arguments3."' and ConfirmationCode='".$Arguments4."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			
			
			$PageContents = $Arguments3.' '.$MsgText[5][69].'<br><br><a href="'.$MainURL.'/index.html"><b>'.$MsgText[0][1].'</b></a>';
			
		}
		else {
		
			$PageContents = $MsgText[5][61];
		
		}
		
	
	}
	else if ($Arguments2 == 'messages') {
						
		if ($Arguments3 == 'current') {
		// displaying last message sent
		
			$query = "select * from `".$Settings['DBPrefix']."nl-Messages` order by DateCode desc limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
						
			if ($nlSettings['PageTitle'] != '') {
				$MsgText[5][70] = $nlSettings['PageTitle'];
			}
			
			if ($nlSettings['TitleImage'] != '') {
				$PageTitle = '<div align="'.$AlignmentCodes[$nlSettings['TitleAlignment']].'"><img src="'.$BaseURL.$nlSettings['TitleImage'].'" alt="'.$MsgText[5][70].'" border="0"></div>';	
			}
			else {
				$PageTitle = '<div align="'.$AlignmentCodes[$nlSettings['TitleAlignment']].'">'.$MsgText[5][70].'</div>';
			}
			
			$BrowserTitle = $row['Title'].' - '.$MsgText[5][70];
			$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="'.$MainURL.'/newsletter/messages.html">'.$MsgText[5][70].'</a> > '.$row['Title'];
			
			if ($row['RecordNumber'] != '') {
						
				# convert email addresses to mailto links 
				//$row['Message'] = preg_replace("/([\w\.-]+)(@)([\w\.-]+)/e", "'<a href=\"mailto:\\0\">\\0</a>'", $row['Message']); 
	
				# convert http urls to hyperlinks 
				//$row['Message'] = preg_replace("/(http:\/\/)([\w\.-]+)/e", "'<a href=\"\\0\" target=\"_blank\">\\0</a>'", $row['Message']);
			
				if (strpos(strtolower($row['Message']),'<br>') !== false || strpos(strtolower($row['Message']),'<br />') !== false || strpos(strtolower($row['Message']),'</p>') !== false) {
					// html
				}
				else {
					$row['Message'] = str_replace("\n",'<br>',$row['Message']);
					
					# convert http urls to hyperlinks 
					$row['Message'] = preg_replace("/(?<!\")(http:\/\/)([\w\.\-\/]+)/e", "'<a href=\"\\0\" target=\"_blank\">\\0</a>'", $row['Message']);
				}
				
				
				$PageContents = '<br><b>'.$row['Title'].'</b><br><br>'.$MsgText[5][10].': '.date("F j, Y",$row['DateCode']).'<br><br><br>'.$row['Message'];

			}
	
		}
		else if ($Arguments3 != '') {
		// displaying specific message by id
		
			$query = "select * from `".$Settings['DBPrefix']."nl-Messages` where RecordNumber='".intval($Arguments3)."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			
			
			if ($nlSettings['PageTitle'] != '') {
				$MsgText[5][70] = $nlSettings['PageTitle'];
			}
			
			if ($nlSettings['TitleImage'] != '') {
				$PageTitle = '<div align="'.$AlignmentCodes[$nlSettings['TitleAlignment']].'"><img src="'.$BaseURL.$nlSettings['TitleImage'].'" alt="'.$MsgText[5][70].'" border="0"></div>';	
			}
			else {
				$PageTitle = '<div align="'.$AlignmentCodes[$nlSettings['TitleAlignment']].'">'.$MsgText[5][70].'</div>';
			}
			
			
			//$PageTitle = $MsgText[5][70];
			$BrowserTitle = $row['Title'].' - '.$MsgText[5][70];
			$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="'.$MainURL.'/newsletter/messages.html">'.$MsgText[5][70].'</a> > '.$row['Title'];
			
			
			# convert email addresses to mailto links 
			//$row['Message'] = preg_replace("/([\w\.-]+)(@)([\w\.-]+)/e", "'<a href=\"mailto:\\0\">\\0</a>'", $row['Message']); 

			# convert http urls to hyperlinks 
			//$row['Message'] = preg_replace("/(http:\/\/)([\w\.-]+)/e", "'<a href=\"\\0\" target=\"_blank\">\\0</a>'", $row['Message']);
			
			if (strpos(strtolower($row['Message']),'<br>') !== false || strpos(strtolower($row['Message']),'<br />') !== false || strpos(strtolower($row['Message']),'</p>') !== false) {
				// html
			}
			else {
				$row['Message'] = str_replace("\n",'<br>',$row['Message']);
				
				# convert http urls to hyperlinks 
				$row['Message'] = preg_replace("/(?<!\")(http:\/\/)([\w\.\-\/]+)/e", "'<a href=\"\\0\" target=\"_blank\">\\0</a>'", $row['Message']);
				//$row['Message'] = preg_replace("/(http:\/\/)([\w\.\-\/]+)/e", "'<a href=\"\\0\" target=\"_blank\">\\0</a>'", $row['Message']);
			}
			
			
			$PageContents = '<br><b>'.$row['Title'].'</b><br><br>'.$MsgText[5][10].': '.date("F j, Y",$row['DateCode']).'<br><br><br>'.$row['Message'];
	
		}
		else {
		// displaying list of messages
		
			
			if ($nlSettings['PageTitle'] != '') {
				$MsgText[5][70] = $nlSettings['PageTitle'];
			}
			
			if ($nlSettings['TitleImage'] != '') {
				$PageTitle = '<div align="'.$AlignmentCodes[$nlSettings['TitleAlignment']].'"><img src="'.$BaseURL.$nlSettings['TitleImage'].'" alt="'.$MsgText[5][70].'" border="0"></div>';	
			}
			else {
				$PageTitle = '<div align="'.$AlignmentCodes[$nlSettings['TitleAlignment']].'">'.$MsgText[5][70].'</div>';
			}
			
			
			//$PageTitle = $MsgText[5][70];
			$BrowserTitle = $MsgText[5][70];
			$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[5][70];
			
			
			if ($nlSettings['TopText'] != '') {
				$PageContents .= '<div align="'.$AlignmentCodes[$nlSettings['TopTextAlignment']].'">'.$nlSettings['TopText'].'</div><br>';
			}
			
			
		
			$query = "select * from `".$Settings['DBPrefix']."nl-Messages` order by DateCode desc";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {		
			
				$PageContents .= '<br><a href="'.$MainURL.'/newsletter/messages/'.$row['RecordNumber'].'.html"><b>'.$row['Title'].'</b></a><br><font size="1">'.$MsgText[5][10].': '.date("F j, Y",$row['DateCode']).'<br></font>';
			
			}
		
		}
		
	
	}
	
	
	$PageTitle = '<div align="'.$AlignmentCodes[$nlSettings['TitleAlignment']].'">'.$PageTitle.'</div>'; 
	
	
	// ========================================================================================




?>
