<?

/*
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
*/



	// ========================================================================================
	// admin add subscribers
	// ========================================================================================
	
	$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'];
	}
	
	
	$PageTitle = $MsgText[5][32];
	$BrowserTitle = $MsgText[5][32];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > <a href="'.$MainURL.'/admin/newsletter_subscribers.html">'.$MsgText[5][3].'</a>  > '.$MsgText[5][32];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[502] == 'y') {
							
		
			ReadFormArguments();
			Global $FormArguments;
			
			if (count($FormArguments) > 0 && $FormArguments['EmailAddresses'] != '') {
			
				
				$AddressesList = explode("\n",$FormArguments['EmailAddresses']);
				
				if (is_array($AddressesList)) {
				// multiple addresses
					$AddressesCount = count($AddressesList);
				}
				else {
				// single address
					$AddressesCount = 1;
				}
				
				
				$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 = 0; $i < $AddressesCount; $i++) {
				
					if (strpos($AddressesList[$i],'@') !== false && strpos($AddressesList[$i],'.') !== false && strpos($AddressesList[$i],' ') === false && strlen($AddressesList[$i]) > 6) {
					// somewhat eliminates potential fakes and other invalids
					
						$AddressesList[$i] = str_replace("\r",'',$AddressesList[$i]);
												
					
						$query = "select * from `".$Settings['DBPrefix']."nl-Addresses` where Email='".$AddressesList[$i]."' limit 1";
						$result = mysql_query($query) or die (mysql_error());
						if (mysql_num_rows($result) == 0) {
						// address doesn't yet exist in the list
						
							$ConfirmationCode = '';					
							for ($x = 1; $x <= 20; $x++) {
								$RandomNum = rand(0,35);
								$ConfirmationCode .= $Chars[$RandomNum];
							}
						
						
							if ($FormArguments['SubscriberAction'] != 'confirm' && $nlSettings['DoubleOptInMode'] == 'y') {
							// not asking for confirmation, and in double mode, so set them as confirmed to start with
							
								$query5 = "insert into `".$Settings['DBPrefix']."nl-Addresses` (Email,DateAdded,DoubleConfirmed,ConfirmationCode) values ('".$AddressesList[$i]."','".GetTimestamp()."','y','".$ConfirmationCode."')";
								$result5 = mysql_query($query5) or die (mysql_error());
							
							}
							else {
							// don't set them as confirmed, either b/c we're asking them for confirmation, or b/c we're not in double mode
							
								$query5 = "insert into `".$Settings['DBPrefix']."nl-Addresses` (Email,DateAdded,DoubleConfirmed,ConfirmationCode) values ('".$AddressesList[$i]."','".GetTimestamp()."','','".$ConfirmationCode."')";
								$result5 = mysql_query($query5) or die (mysql_error());
							
							}
							
							if ($FormArguments['SubscriberAction'] == 'confirm') {
								
								$Message = "\n".$MsgText[5][43]."\n\n".$MainURL."/newsletter/subscribe_confirm/".$AddressesList[$i]."/".$ConfirmationCode."\n\n\n".$MsgText[5][44]."\n\n\n\n".$Settings['SiteName']."\n".$BaseURL;
								
								mail($AddressesList[$i], 'Newsletter Subscription', $Message, 'From: '.$Settings['SiteName'].' <'.$Settings['ContactEmail'].'>','-f'.$Settings['ContactEmail']);

							}
							else if ($FormArguments['SubscriberAction'] == 'welcome') {
								
								$Message = "\n".$MsgText[5][45].": ".$MainURL."/newsletter/messages.html\n\n\n\n".$Settings['SiteName']."\n".$BaseURL;
								
								mail($AddressesList[$i], 'Newsletter Subscription', $Message, 'From: '.$Settings['SiteName'].' <'.$Settings['ContactEmail'].'>','-f'.$Settings['ContactEmail']);

							}
							
						
						}
					
					}
				
				}
			
			
										
				$PageContents = '<br>'.$MsgText[5][46].'<br><br><a href="'.$MainURL.'/admin/newsletter_subscribers.html"><b>'.$MsgText[5][3].'</b></a>';
				
			}
			else {
				$PageContents = $MsgText[0][98];
			}
			
			
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}
		


?>
