How does this code work ?

Hi!

Have a open source new system, that i strugle to anderstand some of the code.

The code her;

     <div mc:edit="std_footer">
		<em>Copyright &copy; *|CURRENT_YEAR|* *|LIST:COMPANY|*, All rights reserved.</em>
		<br />
		*|IFNOT:ARCHIVE_PAGE|* *|LIST:DESCRIPTION|*
		<br />
		<strong>Our mailing address is:</strong>
		<br />
		*|HTML:LIST_ADDRESS_HTML|**|END:IF|* 
		</div>

Open in new window


But its shows up as txt in email. ?
How is  <div mc:edit="std_footer"> fill in data here ?
LVL 2
team2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
I'm pretty sure your software reads that code and substitutes usable values for all of those non-HTML fragments.
0
Julian HansenCommented:
Further to Dave's comment,

mc:edit is a MailChimp directive used to merge data with an email template.

The template is read in by a script which parses the HTML looking for specific identifiers. It uses thes to find where it must replace text with data pulled from the database.

You can read more about it here

http://templates.mailchimp.com/getting-started/template-language/
0
team2005Author Commented:
Hi!

Cant find out where it replace f.example -> {FIRST_NAME} ???

Include the template:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

		<!-- Facebook sharing information tags -->
		<meta property="og:title" content="*|MC:SUBJECT|*" />

		<title>Newsletter Title</title>
		<style type="text/css">
			/* Client-specific Styles */
			#outlook a{padding:0;} /* Force Outlook to provide a "view in browser" button. */
			body{width:100% !important;} .ReadMsgBody{width:100%;} .ExternalClass{width:100%;} /* Force Hotmail to display emails at full width */
			body{-webkit-text-size-adjust:none;} /* Prevent Webkit platforms from changing default text sizes. */

			/* Reset Styles */
			body{margin:0; padding:0;}
			img{border:0; height:auto; line-height:100%; outline:none; text-decoration:none;}
			table td{border-collapse:collapse;}
			#backgroundTable{height:100% !important; margin:0; padding:0; width:100% !important;}

			/* Template Styles */

			/* /\/\/\/\/\/\/\/\/\/\ STANDARD STYLING: COMMON PAGE ELEMENTS /\/\/\/\/\/\/\/\/\/\ */

			/**
			* @tab Page
			* @section background color
			* @tip Set the background color for your email. You may want to choose one that matches your company's branding.
			* @theme page
			*/
			body, #backgroundTable{
				/*@editable*/ background-color:#FAFAFA;
			}

			/**
			* @tab Page
			* @section email border
			* @tip Set the border for your email.
			*/
			#templateContainer{
				/*@editable*/ border: 1px solid #DDDDDD;
			}

			/**
			* @tab Page
			* @section heading 1
			* @tip Set the styling for all first-level headings in your emails. These should be the largest of your headings.
			* @style heading 1
			*/
			h1, .h1{
				/*@editable*/ color:#202020;
				display:block;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:34px;
				/*@editable*/ font-weight:bold;
				/*@editable*/ line-height:100%;
				margin-top:0;
				margin-right:0;
				margin-bottom:10px;
				margin-left:0;
				/*@editable*/ text-align:left;
			}

			/**
			* @tab Page
			* @section heading 2
			* @tip Set the styling for all second-level headings in your emails.
			* @style heading 2
			*/
			h2, .h2{
				/*@editable*/ color:#202020;
				display:block;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:30px;
				/*@editable*/ font-weight:bold;
				/*@editable*/ line-height:100%;
				margin-top:0;
				margin-right:0;
				margin-bottom:10px;
				margin-left:0;
				/*@editable*/ text-align:left;
			}

			/**
			* @tab Page
			* @section heading 3
			* @tip Set the styling for all third-level headings in your emails.
			* @style heading 3
			*/
			h3, .h3{
				/*@editable*/ color:#202020;
				display:block;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:26px;
				/*@editable*/ font-weight:bold;
				/*@editable*/ line-height:100%;
				margin-top:0;
				margin-right:0;
				margin-bottom:10px;
				margin-left:0;
				/*@editable*/ text-align:left;
			}

			/**
			* @tab Page
			* @section heading 4
			* @tip Set the styling for all fourth-level headings in your emails. These should be the smallest of your headings.
			* @style heading 4
			*/
			h4, .h4{
				/*@editable*/ color:#202020;
				display:block;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:22px;
				/*@editable*/ font-weight:bold;
				/*@editable*/ line-height:100%;
				margin-top:0;
				margin-right:0;
				margin-bottom:10px;
				margin-left:0;
				/*@editable*/ text-align:left;
			}

			/* /\/\/\/\/\/\/\/\/\/\ STANDARD STYLING: PREHEADER /\/\/\/\/\/\/\/\/\/\ */

			/**
			* @tab Header
			* @section preheader style
			* @tip Set the background color for your email's preheader area.
			* @theme page
			*/
			#templatePreheader{
				/*@editable*/ background-color:#FAFAFA;
			}

			/**
			* @tab Header
			* @section preheader text
			* @tip Set the styling for your email's preheader text. Choose a size and color that is easy to read.
			*/
			.preheaderContent div{
				/*@editable*/ color:#505050;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:10px;
				/*@editable*/ line-height:100%;
				/*@editable*/ text-align:left;
			}

			/**
			* @tab Header
			* @section preheader link
			* @tip Set the styling for your email's preheader links. Choose a color that helps them stand out from your text.
			*/
			.preheaderContent div a:link, .preheaderContent div a:visited, /* Yahoo! Mail Override */ .preheaderContent div a .yshortcuts /* Yahoo! Mail Override */{
				/*@editable*/ color:#336699;
				/*@editable*/ font-weight:normal;
				/*@editable*/ text-decoration:underline;
			}



			/* /\/\/\/\/\/\/\/\/\/\ STANDARD STYLING: HEADER /\/\/\/\/\/\/\/\/\/\ */

			/**
			* @tab Header
			* @section header style
			* @tip Set the background color and border for your email's header area.
			* @theme header
			*/
			#templateHeader{
				/*@editable*/ background-color:#FFFFFF;
				/*@editable*/ border-bottom:0;
			}

			/**
			* @tab Header
			* @section header text
			* @tip Set the styling for your email's header text. Choose a size and color that is easy to read.
			*/
			.headerContent{
				/*@editable*/ color:#202020;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:34px;
				/*@editable*/ font-weight:bold;
				/*@editable*/ line-height:100%;
				/*@editable*/ padding:0;
				/*@editable*/ text-align:center;
				/*@editable*/ vertical-align:middle;
			}

			/**
			* @tab Header
			* @section header link
			* @tip Set the styling for your email's header links. Choose a color that helps them stand out from your text.
			*/
			.headerContent a:link, .headerContent a:visited, /* Yahoo! Mail Override */ .headerContent a .yshortcuts /* Yahoo! Mail Override */{
				/*@editable*/ color:#336699;
				/*@editable*/ font-weight:normal;
				/*@editable*/ text-decoration:underline;
			}

			#headerImage{
				height:auto;
				max-width:600px;
			}

			/* /\/\/\/\/\/\/\/\/\/\ STANDARD STYLING: MAIN BODY /\/\/\/\/\/\/\/\/\/\ */

			/**
			* @tab Body
			* @section body style
			* @tip Set the background color for your email's body area.
			*/
			#templateContainer, .bodyContent{
				/*@editable*/ background-color:#FFFFFF;
			}

			/**
			* @tab Body
			* @section body text
			* @tip Set the styling for your email's main content text. Choose a size and color that is easy to read.
			* @theme main
			*/
			.bodyContent div{
				/*@editable*/ color:#505050;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:14px;
				/*@editable*/ line-height:150%;
				/*@editable*/ text-align:left;
			}

			/**
			* @tab Body
			* @section body link
			* @tip Set the styling for your email's main content links. Choose a color that helps them stand out from your text.
			*/
			.bodyContent div a:link, .bodyContent div a:visited, /* Yahoo! Mail Override */ .bodyContent div a .yshortcuts /* Yahoo! Mail Override */{
				/*@editable*/ color:#336699;
				/*@editable*/ font-weight:normal;
				/*@editable*/ text-decoration:underline;
			}

			.bodyContent img{
				display:inline;
				height:auto;
			}

			/* /\/\/\/\/\/\/\/\/\/\ STANDARD STYLING: COLUMNS; LEFT, RIGHT /\/\/\/\/\/\/\/\/\/\ */

			/**
			* @tab Columns
			* @section left column text
			* @tip Set the styling for your email's left column text. Choose a size and color that is easy to read.
			*/
			.leftColumnContent{
				/*@editable*/ background-color:#FFFFFF;
			}

			/**
			* @tab Columns
			* @section left column text
			* @tip Set the styling for your email's left column text. Choose a size and color that is easy to read.
			*/
			.leftColumnContent div{
				/*@editable*/ color:#505050;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:14px;
				/*@editable*/ line-height:150%;
				/*@editable*/ text-align:left;
			}

			/**
			* @tab Columns
			* @section left column link
			* @tip Set the styling for your email's left column links. Choose a color that helps them stand out from your text.
			*/
			.leftColumnContent div a:link, .leftColumnContent div a:visited, /* Yahoo! Mail Override */ .leftColumnContent div a .yshortcuts /* Yahoo! Mail Override */{
				/*@editable*/ color:#336699;
				/*@editable*/ font-weight:normal;
				/*@editable*/ text-decoration:underline;
			}

			.leftColumnContent img{
				display:inline;
				height:auto;
			}

			/**
			* @tab Columns
			* @section right column text
			* @tip Set the styling for your email's right column text. Choose a size and color that is easy to read.
			*/
			.rightColumnContent{
				/*@editable*/ background-color:#FFFFFF;
			}

			/**
			* @tab Columns
			* @section right column text
			* @tip Set the styling for your email's right column text. Choose a size and color that is easy to read.
			*/
			.rightColumnContent div{
				/*@editable*/ color:#505050;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:14px;
				/*@editable*/ line-height:150%;
				/*@editable*/ text-align:left;
			}

			/**
			* @tab Columns
			* @section right column link
			* @tip Set the styling for your email's right column links. Choose a color that helps them stand out from your text.
			*/
			.rightColumnContent div a:link, .rightColumnContent div a:visited, /* Yahoo! Mail Override */ .rightColumnContent div a .yshortcuts /* Yahoo! Mail Override */{
				/*@editable*/ color:#336699;
				/*@editable*/ font-weight:normal;
				/*@editable*/ text-decoration:underline;
			}

			.rightColumnContent img{
				display:inline;
				height:auto;
			}

			/* /\/\/\/\/\/\/\/\/\/\ STANDARD STYLING: FOOTER /\/\/\/\/\/\/\/\/\/\ */

			/**
			* @tab Footer
			* @section footer style
			* @tip Set the background color and top border for your email's footer area.
			* @theme footer
			*/
			#templateFooter{
				/*@editable*/ background-color:#FFFFFF;
				/*@editable*/ border-top:0;
			}

			/**
			* @tab Footer
			* @section footer text
			* @tip Set the styling for your email's footer text. Choose a size and color that is easy to read.
			* @theme footer
			*/
			.footerContent div{
				/*@editable*/ color:#707070;
				/*@editable*/ font-family:Arial;
				/*@editable*/ font-size:12px;
				/*@editable*/ line-height:125%;
				/*@editable*/ text-align:left;
			}

			/**
			* @tab Footer
			* @section footer link
			* @tip Set the styling for your email's footer links. Choose a color that helps them stand out from your text.
			*/
			.footerContent div a:link, .footerContent div a:visited, /* Yahoo! Mail Override */ .footerContent div a .yshortcuts /* Yahoo! Mail Override */{
				/*@editable*/ color:#336699;
				/*@editable*/ font-weight:normal;
				/*@editable*/ text-decoration:underline;
			}

			.footerContent img{
				display:inline;
			}

			/**
			* @tab Footer
			* @section social bar style
			* @tip Set the background color and border for your email's footer social bar.
			* @theme footer
			*/
			#social{
				/*@editable*/ background-color:#FAFAFA;
				/*@editable*/ border:0;
			}

			/**
			* @tab Footer
			* @section social bar style
			* @tip Set the background color and border for your email's footer social bar.
			*/
			#social div{
				/*@editable*/ text-align:center;
			}

			/**
			* @tab Footer
			* @section utility bar style
			* @tip Set the background color and border for your email's footer utility bar.
			* @theme footer
			*/
			#utility{
				/*@editable*/ background-color:#FFFFFF;
				/*@editable*/ border:0;
			}

			/**
			* @tab Footer
			* @section utility bar style
			* @tip Set the background color and border for your email's footer utility bar.
			*/
			#utility div{
				/*@editable*/ text-align:center;
			}

			#monkeyRewards img{
				max-width:190px;
			}
		</style>
	</head>
	<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
		<center>
			<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="backgroundTable">
				<tr>
					<td align="center" valign="top">
						<!-- // Begin Template Preheader \\ -->
						<table border="0" cellpadding="10" cellspacing="0" width="600" id="templatePreheader">
							<tr>
								<td valign="top" class="preheaderContent">

									<!-- // Begin Module: Standard Preheader \ -->
									<table border="0" cellpadding="10" cellspacing="0" width="100%">
										<tr>
											<td valign="top">
												<div mc:edit="std_preheader_content">
													 Use this area to offer a short teaser of your email's content. Text here will show in the preview area of some email clients.
												</div>
											</td>
											<!-- *|IFNOT:ARCHIVE_PAGE|* -->
											<td valign="top" width="190">
												<div mc:edit="std_preheader_links">
													Is this email not displaying correctly?<br /><a href="*|ARCHIVE|*" target="_blank">View it in your browser</a>.
												</div>
											</td>
											<!-- *|END:IF|* -->
										</tr>
									</table>
									<!-- // End Module: Standard Preheader \ -->

								</td>
							</tr>
						</table>
						<!-- // End Template Preheader \\ -->
						<table border="0" cellpadding="0" cellspacing="0" width="600" id="templateContainer">
							<tr>
								<td align="center" valign="top">
									<!-- // Begin Template Header \\ -->
									<table border="0" cellpadding="0" cellspacing="0" width="600" id="templateHeader">
										<tr>
											<td class="headerContent">

												<!-- // Begin Module: Standard Header Image \\ -->
												<img src="http://gallery.mailchimp.com/653153ae841fd11de66ad181a/images/placeholder_600.gif" style="max-width:600px;" id="headerImage campaign-icon" mc:label="header_image" mc:edit="header_image" mc:allowdesigner mc:allowtext />
												<!-- // End Module: Standard Header Image \\ -->

											</td>
										</tr>
									</table>
									<!-- // End Template Header \\ -->
								</td>
							</tr>
							<tr>
								<td align="center" valign="top">
									<!-- // Begin Template Body \\ -->
									<table border="0" cellpadding="0" cellspacing="0" width="600" id="templateBody">
										<tr>
											<td colspan="3" valign="top" class="bodyContent">

												<!-- // Begin Module: Standard Content \\ -->
												<table border="0" cellpadding="20" cellspacing="0" width="100%">
													<tr>
														<td valign="top">
															<p> Dear {FIRST_NAME} </p>
															<div mc:edit="std_content00">
																<h1 class="h1">Heading 1</h1>
																<h2 class="h2">Heading 2</h2>
																<h3 class="h3">Heading 3</h3>
																<h4 class="h4">Heading 4</h4>
																<strong>Getting started:</strong> Customize your template by clicking on the style editor tabs up above. Set your fonts, colors, and styles. After setting your styling is all done you can click here in this area, delete the text, and start adding your own awesome content!
																<br />
																<br />
																After you enter your content, highlight the text you want to style and select the options you set in the style editor in the "styles" drop down box. Want to <a href="http://www.mailchimp.com/kb/article/im-using-the-style-designer-and-i-cant-get-my-formatting-to-change" target="_blank">get rid of styling on a bit of text</a>, but having trouble doing it? Just use the "remove formatting" button to strip the text of any formatting and reset your style.
															</div>
														</td>
													</tr>
												</table>
												<!-- // End Module: Standard Content \\ -->

											</td>
										</tr>
										<tr>
											<td valign="top" width="280" class="leftColumnContent">

												<!-- // Begin Module: Top Image with Content \\ -->
												<table border="0" cellpadding="20" cellspacing="0" width="100%">
													<tr mc:repeatable>
														<td valign="top">
															<img src="http://gallery.mailchimp.com/653153ae841fd11de66ad181a/images/placeholder_260.gif" style="max-width:260px;" mc:label="image" mc:edit="tiwc300_image00" />
															<div mc:edit="tiwc300_content00">
															   <h4 class="h4">Heading 4</h4>
															   <strong>Repeatable content blocks:</strong> Repeatable sections are noted with plus and minus signs so that you can add and subtract content blocks. You can also <a href="http://www.mailchimp.com/kb/article/how-do-i-work-with-repeatable-content-blocks" target="_blank">get a little fancy</a>: repeat blocks and remove all text to make image "gallery" sections, or do the opposite and remove images for text-only blocks!
															</div>
														</td>
													</tr>
												</table>
												<!-- // End Module: Top Image with Content \\ -->

											</td>
											<td valign="top" width="280" class="rightColumnContent">

												<!-- // Begin Module: Top Image with Content \\ -->
												<table border="0" cellpadding="20" cellspacing="0" width="100%">
													<tr mc:repeatable>
														<td valign="top">
															<img src="http://gallery.mailchimp.com/653153ae841fd11de66ad181a/images/placeholder_260.gif" style="max-width:260px;" mc:label="image" mc:edit="tiwc300_image01" />
															<div mc:edit="tiwc300_content01">
															   <h4 class="h4">Heading 4</h4>
															   <strong>Repeatable content blocks:</strong> Repeatable sections are noted with plus and minus signs so that you can add and subtract content blocks. You can also <a href="http://www.mailchimp.com/kb/article/how-do-i-work-with-repeatable-content-blocks" target="_blank">get a little fancy</a>: repeat blocks and remove all text to make image "gallery" sections, or do the opposite and remove images for text-only blocks!
															</div>
														</td>
													</tr>
												</table>
												<!-- // End Module: Top Image with Content \\ -->

											</td>
										</tr>
									</table>
									<!-- // End Template Body \\ -->
								</td>
							</tr>
							<tr>
								<td align="center" valign="top">
									<!-- // Begin Template Footer \\ -->
									<table border="0" cellpadding="10" cellspacing="0" width="600" id="templateFooter">
										<tr>
											<td valign="top" class="footerContent">

												<!-- // Begin Module: Standard Footer \\ -->
												<table border="0" cellpadding="10" cellspacing="0" width="100%">
													<tr>
														<td colspan="2" valign="middle" id="social">
															<div mc:edit="std_social">
																&nbsp;<a href="*|TWITTER:PROFILEURL|*">follow on Twitter</a> | <a href="*|FACEBOOK:PROFILEURL|*">friend on Facebook</a> | <a href="*|FORWARD|*">forward to a friend</a>&nbsp;
															</div>
														</td>
													</tr>
													<tr>
														<td valign="top" width="350">
															<div mc:edit="std_footer">
																<em>Copyright &copy; *|CURRENT_YEAR|* *|LIST:COMPANY|*, All rights reserved.</em>
																<br />
																*|IFNOT:ARCHIVE_PAGE|* *|LIST:DESCRIPTION|*
																<br />
																<strong>Our mailing address is:</strong>
																<br />
																*|HTML:LIST_ADDRESS_HTML|**|END:IF|* 
															</div>
														</td>
														<td valign="top" width="190" id="monkeyRewards">
															<div mc:edit="monkeyrewards">
																*|IF:REWARDS|* *|HTML:REWARDS|* *|END:IF|*
															</div>
														</td>
													</tr>
													<tr>
														<td colspan="2" valign="middle" id="utility">
															<div mc:edit="std_utility">
																Denne eposten ble sendt fra <a href="mailto:{FROM_EMAIL}" style="color:#FFFFFF;" color="#FFFFFF">{FROM_EMAIL}</a> til <a href="mailto:{TO_EMAIL}" style="color:#FFFFFF;" color="#FFFFFF">{TO_EMAIL}</a> on {SENT_DATE} | <a href="{UNSUBSCRIBE_URL}" style="color:#FFFFFF" color="#FFFFFF">Avmeld nyhetsbrev</a>
															</div>
														</td>
													</tr>
												</table>
												<!-- // End Module: Standard Footer \\ -->

											</td>
										</tr>
									</table>
									<!-- // End Template Footer \\ -->
								</td>
							</tr>
						</table>
						<br />
					</td>
				</tr>
			</table>
		</center>
	</body>
</html>

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Julian HansenCommented:
Line 478
	<p> Dear {FIRST_NAME} </p>

Open in new window

0
team2005Author Commented:
Hi!

I ask where it change f.example -> {FIRST_NAME} with the name from database
It must be some script or somthing ?
0
team2005Author Commented:
Hi!e file that send the email



Include th

<?php

$send_id = (int)$_REQUEST['send_id'];
if(!$send_id){
	// basic error checking.
	echo 'Please go back and pick a newsletter';
}


if($_REQUEST['pause']){
	$newsletter->pause_send($db,$send_id);
}
if($_REQUEST['unpause']){
	$newsletter->un_pause_send($db,$send_id);
}

$send_data = $newsletter->get_send($db,$send_id);
$newsletter_id = $send_data['newsletter_id'];
$newsletter_data = $newsletter->get_newsletter($db,$newsletter_id);


if($_REQUEST['process']){
	
	ob_end_clean();
	?>
	<html>
	<head>
	<title>Sender</title>
	<script language="javascript" src="layout/js/jquery.js"></script>
	</head>
	<body>
	<?php
	@set_time_limit(0);
	
	if($send_data['start_time'] > time()){
		?>
		<script language="javascript">
    	$('#sent_to',window.parent.document).html('Dette nyhetsbrevet er scheduled for sending <?php echo date('d/m/Y',$send_data['start_time']);?>');
    	</script>
		<?php
		exit;
	}
	
	$batch_limit = (int)$newsletter->settings['burst_count'];
	if(!$batch_limit)$batch_limit = 10; // default 10.
	
		$result = array();
		$result['status'] = true;
		$sent_to = count($send_data['sent_members']);
		$batch_count = 0;
		foreach($send_data['unsent_members'] as $unsent_member){ 
			
			$result = $newsletter->send_out_newsletter($db,$send_id,$unsent_member['member_id']);
			
			if($result['status']){
				$batch_count++;
				$sent_to++;
			}else{
				$sent_to = $result['message'];
			}
			?>
			<script language="javascript">
	    	$('#sent_to',window.parent.document).html('<?php echo $sent_to;?>');
	    	</script>
	    	
	    	<?php 
	    	ob_flush();
	    	flush();
	    	
	    	if(!$result['status']){
	    		// break on fail to send
	    		break;
	    	}
			
	    	if($batch_count >= $batch_limit){
	    		if(_DEMO_MODE)sleep(4);
				break;
			}
		
		} 
		
		if($result['status']){
		?>
	    
	    <script language="javascript">
	    
	    <?php
	    
	    $send_data = $newsletter->get_send($db,$send_id);
	    if(!count($send_data['unsent_members']) ){
	    	$newsletter->send_complete($db,$send_id);
	    	?>
	    // if complete.
	    	window.parent.location.href='index.php?p=send&send_id=<?php echo $send_id;?>';
	    <?php }else{ ?>
		    setTimeout(function(){window.location.href='index.php?p=send&send_id=<?php echo $send_id;?>&process=true';},1000);
	    <?php } ?>
		</script>
	
		<?php
		}
	?>
	</body>
	</html>
	<?php
		exit;
}else{

?>

<h1>Send</h1>


<h2><span>Sender Nyhetsbrev: <?php echo $newsletter_data['subject'];?></span></h2>

<p>Venligst ikke steng vindue intil den sier "COMPLETE" nedenfor.</p>

<?php
if($send_data['start_time'] > time()){
	?>

	<div class="box">
	<div style="font-size:20px; padding:20px;"> 
	This newsletter has been scheduled for sending on <?php echo date('d/m/Y',$send_data['start_time']);?>
	</div>
	</div>
	<?php
		
}else{ ?>
		
	<div class="box">
		<?php if($send_data['status'] == '3'){ 
			$newsletter->send_complete($db,$send_id); // quick hack to fix a half completed send.
			?>
			<div style="font-size:20px; padding:20px; float: left; text-align:center;"> 
			<img src="layout/images/send_success.jpg" />
			<br /><br /> 
			Ferdig!</div>
		<?php }else{ ?>
			<?php if($send_data['status'] == '6'){ ?>
			<div style="font-size:20px; padding:20px; text-align:center;"> 
			<img src="layout/images/send_success.jpg" />
			<br /><br /> 
			Pause...</div>
			<?php }else{ ?>
			<div style="font-size:20px; padding:20px; text-align:center;"> 
			<img src="layout/images/send_success.jpg" />
			<br /><br /> 
			Sender...</div>
			<?php } ?>
		<?php } ?>
		
		<div style="font-size:20px; padding:20px; float: right;"> 
		<a href="#" class="submit orange">
			Sent til <span id="sent_to"><?php echo count($send_data['sent_members']);?></span> av <span id="sent_total"><?php echo count($send_data['unsent_members']) + count($send_data['sent_members']);?></span> members
			</a>
			</div>
		<div class="clear"></div>

		<?php if($send_data['status'] == '3'){ ?>
		<div style="padding:20px;">
			<?php }else{ ?>
				<?php if($send_data['status'] == '6'){ ?>
				<a href="?p=send&send_id=<?php echo $send_id;?>&unpause=true" class="submit orange right_float">Continue Sending</a>
				<?php }else{ ?>
				<a href="?p=send&send_id=<?php echo $send_id;?>&pause=true" class="submit orange right_float">Pause Send</a>
				<?php } ?>
		</div>
		<?php } ?>
	</div>
	
	<?php if($send_data['status'] == '1'){ ?>
	<iframe src="about:blank" id="send_iframe" name="send_iframe" width="0" height="0" style="display:none"></iframe>
	<script language="javascript">
	
	function send_mailout(){
		$('#send_iframe').attr('src','index.php?p=send&send_id=<?php echo $send_id;?>&process=true');
		
	}
	$(window).ready(function(){
		setTimeout(send_mailout,3000);
	});
	</script>

<?php } ?>
<?php } ?>
<?php } ?>

Open in new window

0
Julian HansenCommented:
Without any explanation of where the code you posted comes from or what it is connecting to I am having to guess at what you are actually asking.

I am going to guess that the email is generated by this line (line 53)
$result = $newsletter->send_out_newsletter($db,$send_id,$unsent_member['member_id']);

Open in new window

Using whatever library the object stored in $newsletter is defined int.

In there I am guessing there will be a search and replace done on the template placeholders.
0
team2005Author Commented:
Hi!

Include the file that have this function -> send_out_newsletter

<?php

session_start();

/*  Last try on this class otherwise i am gonna delete all crap .... Just kidding */
class newsletter{
	
	public $base_href;
	public $settings;
	private $db;
	private $ch;

	public function __construct(){
		if (!isset($_SERVER['REQUEST_URI']))
		{
	       $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
	       if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
	       $_SERVER['REQUEST_URI'] = '/' . ltrim($_SERVER['REQUEST_URI'],'/');
		}
		$this->base_href = $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']);
		$this->base_href = rtrim($this->base_href,'/');
	}
	public function init() {
		$this->db = db_connect();
		$this->get_settings($this->db);
	
	}
	public function login($db=false,$username,$password){
		if(!$db)$db = db_connect();
		// check with the db if this users exists, return the users
		// details so we can work out what type of member they are.
		$this->get_settings($db);
		if($this->settings['username']==$username && $this->settings['password']==$password){
			return true;
		}
		return false;
		
	}
	
	public function login_admin($db=false,$username,$password,$golfclubid){
		if(!$db)$db = db_connect();
		// check with the db if this users exists, return the users
		// details so we can work out what type of member they are.
			
		
	 // $username = strip_tags($username);
   // $username = mysqli_real_escape_string($db,$username);
	    
	    $sql = "Select * FROM `"._DB_PREFIX."loginadmin` WHERE Username = '".mysql_real_escape_string($username)."' AND Password = '".mysql_real_escape_string($password)."' AND golf_id =$golfclubid";
	   
      $existing = array_shift(qa($sql,$db));
		  
      if($existing){
	       $golfclub_id=14;
		   return true;
		  }
		$golfclub_id=0;  
	    return false;
  
		
	}
	
	public function get_golfclubname($db=false,$golfID){
    	if(!$db)$db = db_connect();
	
	    $sql = "Select * FROM `"._DB_PREFIX."golfclubs` WHERE golf_id = $golfID";
	   
        $golfcname = array_shift(qa($sql,$db));
        return $golfcname['namegolf'];	
		
	}
	public function logout(){
		
	}

	public $uhash;
	
	public function get_templates(){
		$template_dirs = glob(_TEMPLATE_DIR."*");
		$templates = array();
		foreach($template_dirs as $template_dir){
			$templates[] = array(
				"dir"=>$template_dir,
				"name"=>basename($template_dir),
			);
		}
		return $templates;
	}
	
	public function get_template_html($template,$replace,$options=array()){
		$template_file = _TEMPLATE_DIR.basename($template)."/template.html";
		if(is_file($template_file)){
			extract($options);
			$template_html = file_get_contents($template_file);
		}else if($template){
			$template_html = $template;
		}else{
			$template_html = 'Template '.$template_file.' not found!';
		}
		foreach($replace as $key=>$val){
			$val = str_replace(array('\\', '$'), array('\\\\', '\$'), $val);
			$template_html = preg_replace('/\{'.strtoupper(preg_quote($key,'/')).'\}/',$val,$template_html);
		}
		
		return $template_html;
	}
	
	public function get_images($db,$newsletter_id){
		session_start();
		
		$golfclub_id=(int)$_SESSION['logoclub'];
		$dir = _IMAGES_DIR."$golfclub_id/";
		$files = array();
		if(is_dir($dir)){
			foreach(glob($dir."*") as $file){
				if(is_dir($file))continue;
				$files[] = array(
					"name"=>basename($file),
					"link"=>$file,
				);
			}
		}
		return $files;
	}
	public function get_attachments($db,$newsletter_id){
		$dir = _NEWSLETTERS_DIR."/newsletter-$newsletter_id/";
		$files = array();
		if(is_dir($dir)){
			foreach(glob($dir."/*") as $file){
				$files[] = array(
					"name"=>basename($file),
					"link"=>$file,
				);
			}
		}
		return $files;
	}
	
	public function fix_image_paths($data,$send_id=false,$dir='',$inc_http=true){
		$dir = trim($dir,'/');
		if(strlen($dir)){
			$dir.='/';
		}
		$db= db_connect();
		if($inc_http){
			$base = 'http://'.$this->base_href."/" . $dir;
		}else{
			$base = $dir;
		}
		// process links
		// links / iamges keep em different, that way we know if someone has actually CLICKED vs opened.
		foreach(array("href") as $type){
			preg_match_all('/'.$type.'=(["\'])([^"\']+)\1/',$data,$links);
			if(is_array($links[2])){
				foreach($links[2] as $link_id => $l){
					//if(!preg_match('/^\{/',$l) && !preg_match('/^#/',$l) && !preg_match('/^mailto:/',$l)){
					if(!preg_match('/^\{/',$l) && !preg_match('/^#/',$l) && !(preg_match('/^\w+:/',$l) && !preg_match('/^http/',$l))){
						//echo $links[0][$link_id] ."<br>";
						$search = preg_quote($links[0][$link_id],"/");
						//echo $search."<br>\n";
						$l = preg_replace("/[\?|&]phpsessid=([\w\d]+)/i",'',$l);
						$l = ltrim($l,'/');
						$newlink = ((!preg_match('/^http/',$l)) ? $base : '') . $l;
						if($send_id){
							// we are sending this out, we need to store a link to this in the db
							// to record clicks etc..
							$golfclub_id=(int)$_SESSION['logoclub'];
							$sql = "INSERT INTO `"._DB_PREFIX."link` SET golf_id=$golfclub_id, link_url = '".mysql_real_escape_string($newlink)."'";
							$res = query($sql,$db);
							$link_id = mysql_insert_id($db);
							$newlink = 'http://'.$this->base_href.'/ext.php?t=lnk&id='.$link_id.'&sid={SEND_ID}&mid={MEMBER_ID}&mhash={MEMBER_HASH}';
						}
						$replace = $type.'="'.$newlink.'"';
						//echo $replace."<br>\n";
						//preg_match('/'.$search."/",$template,$matches);print_r($matches);
						$data = preg_replace('/'.$search.'/',$replace,$data,1);
					}
				}
			}
		}
		// process images.
		// only if inline_images isn't set.
		
		if($inc_http && (!isset($this->settings['inline_images']) || $this->settings['inline_images'] == 'no' || !$this->settings['inline_images'])){
			$base = 'http://'.$this->base_href."/" . $dir;
		}else{
			$base = $dir;
		}
		foreach(array("src","background") as $type){
			preg_match_all('/'.$type.'=(["\'])([^"\']+)\1/',$data,$links);
			//print_r($links);
			if(is_array($links[2])){
				foreach($links[2] as $link_id => $l){
					//if(!preg_match('/^\{/',$l) && !preg_match('/^#/',$l) && !preg_match('/^mailto:/',$l)){
					if(!preg_match('/^\{/',$l) && !preg_match('/^#/',$l) && !(preg_match('/^\w+:/',$l) && !preg_match('/^http/',$l))){
						//echo $links[0][$link_id] ."<br>";
						$search = preg_quote($links[0][$link_id],"/");
						//echo $search."<br>\n";
						$l = preg_replace("/[\?|&]phpsessid=([\w\d]+)/i",'',$l);
						$l = ltrim($l,'/');
						$newlink = ((!preg_match('/^http/',$l)) ? $base : '') . $l;
						if(!isset($this->settings['inline_images']) || $this->settings['inline_images'] == 'no' || !$this->settings['inline_images']){
							if($send_id){
								// we are sending this out, we need to store a link to this in the db
								// to record clicks etc..
								$golfclub_id=(int)$_SESSION['logoclub'];
								$sql = "INSERT INTO `"._DB_PREFIX."image` SET golf_id=$golfclub_id, image_url = '".mysql_real_escape_string($newlink)."'";
								$res = query($sql,$db);
								$link_id = mysql_insert_id($db);
								$newlink = 'http://'.$this->base_href.'/ext.php?t=img&id='.$link_id.'&sid={SEND_ID}&mid={MEMBER_ID}';
							}
						}
						$replace = $type.'="'.$newlink.'"';
						//echo $replace."<br>\n";
						//preg_match('/'.$search."/",$template,$matches);print_r($matches);
						$data = preg_replace('/'.$search.'/',$replace,$data,1);
					}
				}
			}
		}
		return $data;
	}
	
	public function send_email($email_to,$email_subject,$email_contents,$email_from,$email_from_name,$options=array()){
		
		if(_DEMO_MODE){
	    	if(preg_match('/example/i',$email_to) || preg_match('/demo/i',$email_to) || preg_match('/test/i',$email_to)){
	    		return true;
	    	}
	    }
	    
		require_once("phpmailer/class.phpmailer.php");
		
		// this is a little hacky cos i copied it from other code i wrote.
		// it works well though.
		
		
		$options ['to']=$email_to;
		$options ['subject']=$email_subject;
		$options ['from']=$email_from;
		$options ['from_name']=$email_from_name;
		
		
		
		foreach(array("to","cc","bcc","reply_to") as $type){
			if(!$options[$type]){
				$options[$type]=array();
			}else if(!is_array($options[$type])){
				$emails = explode(",",$options[$type]);
				$options[$type]=array();
				foreach($emails as $e){
					if($e=trim($e)){
						$options[$type][]=$e;
					}
				}
			}
		}
		
	    $mail = new PHPMailer();
		$mail->CharSet = 'UTF-8';
		 
	    $mail->SetLanguage("en", 'phpmailer/language/');
	    if(_MAIL_SMTP){
		    $mail->IsSMTP(); 
		    // turn on SMTP authentication 
		    $mail->SMTPAuth = _MAIL_SMTP_AUTH;     
		    $mail->Host     = _MAIL_SMTP_HOST; 
		    if(_MAIL_SMTP_AUTH){
			    $mail->Username = _MAIL_SMTP_USER;
			    $mail->Password = _MAIL_SMTP_PASS;
		    }
	    }
	    
	    
	    $mail->From     = $options['from'];
	    if($options['from_name']){
	        $mail->FromName = $options['from_name'];
	    }
	    $mail->Subject     = $options['subject'];
	    // turn on HTML emails:
        $mail->isHTML(true);
	    
	    /*if($options['attachment'] && !is_array($options['attachment'])){
	    	$attachments = explode(",",$options['attachment']);
	    	$options['attachment']=array();
	    	foreach($attachments as $a){
	    		if(is_file($a)){
	    			$mail->AddAttachment($a);
	    		}
	    	}
	    }*/
	    
	    
	    //
	    // use MsgHTML() so it does the inine images etc...
	    if(!isset($this->settings['inline_images']) || $this->settings['inline_images'] == 'no' || !$this->settings['inline_images']){
	    	// use normal html:
	    	$mail->Body    = $email_contents;
	    }else{
	    	// set images inline:
	    	$mail->MsgHTML($email_contents);
	    }
	    
	    
	    // setup to,bcc,cc
	    
	    foreach($options['to'] as $email){
	    	$mail->AddAddress($email);
	    }
	    foreach($options['cc'] as $email){
	    	$mail->AddCC($email);
	    }
	    foreach($options['bcc'] as $email){
	    	$mail->AddBCC($email);
	    }
	    foreach($options['reply_to'] as $email){
	    	$mail->AddReplyTo($email);
	    }
	    
	    if($options['bounce_email']){
	    	$mail->Sender = $options['bounce_email'];
	    }
	    if($options['message_id']){
	    	$mail->MessageID = $options['message_id'];
	    }
	   
	    if(!$mail->Send()){
	    	echo $mail->ErrorInfo;
	    	print_r($mail->smtp->error);
	        return false;
	    }
	    return true;
	}
	
	
	public function save($db=false,$newsletter_id='new',$fields){
		if(!$db)$db = db_connect();
		if(!count($fields))return;
		
		if($newsletter_id == 'new'){
			$golfclub_id=(int)$_SESSION['logoclub'];
			$sql = "INSERT INTO "._DB_PREFIX."newsletter SET golf_id=$golfclub_id, create_date = NOW() ";
			$where = '';
		}else{
			$golfclub_id=(int)$_SESSION['logoclub'];
			$sql = "UPDATE "._DB_PREFIX."newsletter SET golf_id=$golfclub_id, create_date = NOW() ";
			$where = " WHERE newsletter_id = '".mysql_real_escape_string($newsletter_id)."' LIMIT 1";
		}
		
		foreach($fields as $key=>$val){
			$val = trim($val);
			if(!$val)continue;
			/*if($key!='content'){
				$val = htmlspecialchars($val);
			}*/
			$sql .= ", `".$key."` = '".mysql_real_escape_string($val)."'";
		}
		$sql.=$where;
		$res = query($sql,$db);
		if($newsletter_id=='new'){
			$newsletter_id = mysql_insert_id($db);
		}
		
		if($newsletter_id && $fields['content'] && $fields['template']){
			$this->write_newsletter_content($db,$newsletter_id);
		}
		
		return $newsletter_id;
	}
	
	public function delete_content($db=false,$newsletter_id,$newsletter_content_id){
		if(!$db)$db = db_connect();		
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "DELETE FROM "._DB_PREFIX."newsletter_content WHERE golf_id=$golfclub_id AND newsletter_id = '".(int)$newsletter_id."' AND newsletter_content_id = '".(int)$newsletter_content_id."'";
		$res = query($sql,$db);
	}
	
	public function save_content($db=false,$newsletter_id,$newsletter_content_id){
		if(!$db)$db = db_connect();
		if(!$_REQUEST['title'])return;
		if($newsletter_content_id == 'new'){
			$golfclub_id=(int)$_SESSION['logoclub'];
			$sql = "INSERT INTO "._DB_PREFIX."newsletter_content SET golf_id=$golfclub_id, newsletter_id = '$newsletter_id', create_date = NOW() ";
			$where = '';
		}else{
			$golfclub_id=(int)$_SESSION['logoclub'];
			$sql = "UPDATE "._DB_PREFIX."newsletter_content SET create_date = NOW() ";
			$where = " WHERE golf_id=$golfclub_id AND newsletter_id = '$newsletter_id' AND newsletter_content_id = '".mysql_real_escape_string($newsletter_content_id)."' LIMIT 1";
		}
		$fields = array(
			//'newsletter_id' => $newsletter_id,
			'title' => $_REQUEST['title'],
			'position' => $_REQUEST['position'],
			'group_title' => $_REQUEST['group_title'],
			'content_summary' => $_REQUEST['content_summary'],
			'content_full' => $_REQUEST['content_full'],
		);
		foreach($fields as $key=>$val){
			$val = trim($val);
			if(!$val)continue;
			$sql .= ", `".$key."` = '".mysql_real_escape_string($val)."'";
		}
		$sql.=$where;
		$res = query($sql,$db);
		if($newsletter_content_id=='new'){
			$newsletter_content_id = mysql_insert_id($db);
		}
		$this->write_newsletter_content($db,$newsletter_id);
		return $newsletter_content_id;
	}
	public function write_newsletter_content($db,$newsletter_id){
		// todo - check if we've manually overwritten this file via ftp, dont overwrite ith ere.
		// maybe store a hash of the file or something?
		$newsletter_data = $this->get_newsletter($db, $newsletter_id);
		
		$replace = array(
			// email_body is also the generated content from newsletter_content
			"email_body" =>  $this->fix_image_paths($newsletter_data['content'],false,'',false),
			"unsubscribe_url" => 'http://'.$this->base_href.'/ext.php?t=unsub',
			"view_online"=>'http://'.$this->base_href.'/ext.php?t=view&id='.$newsletter_id.'',
			"SENDTOFRIEND"=>'http://'.$this->base_href.'/ext.php?t=send_to_friend&id='.$newsletter_id.'',
			"link_account"=>'http://'.$this->base_href.'/ext.php?t=update_form',
		);
		$newsletter_html_small = $this->get_template_html($newsletter_data['template'],array(),array('small'=>true,'full'=>false)); // pass options to template, so different full/small email template
		$newsletter_html_small = $this->fix_image_paths($newsletter_html_small,false,_TEMPLATE_DIR.$newsletter_data['template'],false);
		$newsletter_html_small = $this->get_template_html($newsletter_html_small,$replace);
		$newsletter_html_small = $this->fix_image_paths($newsletter_html_small,false,'',false);
		// write the version that gets emailed:
		$file = _NEWSLETTERS_DIR."newsletter-".$newsletter_id.'.html';
		if(!@file_put_contents($file,$newsletter_html_small)){
			echo "Kan ikke lagre filen: '$file' venligst sjekk at du har SKRIVE rettigheter på denne folderen.";
			exit;
		}
		// different inner content.
		$replace ['email_body'] = $this->fix_image_paths((isset($newsletter_data['content_full']) ? $newsletter_data['content_full']:$newsletter_data['content']),false,'',false);
		$newsletter_html_full = $this->get_template_html($newsletter_data['template'],array(),array('small'=>false,'full'=>true)); // pass options to template, so different full/small email template
		$newsletter_html_full = $this->fix_image_paths($newsletter_html_full,false,_TEMPLATE_DIR.$newsletter_data['template'],false);
		$newsletter_html_full = $this->get_template_html($newsletter_html_full,$replace);
		$newsletter_html_full = $this->fix_image_paths($newsletter_html_full,false,'',false);
		// write the version that gets dispalyed through read-more link.
		// todo: get newsletter_html from full article list when automated parts are done.
		$file = _NEWSLETTERS_DIR."newsletter-".$newsletter_id.'-full.html';
		if(!@file_put_contents($file,$newsletter_html_full)){
			echo "Kan ikke lagre filen: '$file' venligst sjekk at du har SKRIVE rettigheter på denne folderen.";
			exit;
		}
	}
	public function delete_newsletter($db,$newsletter_id){
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "DELETE FROM "._DB_PREFIX."newsletter WHERE golf_id=$golfclub_id AND newsletter_id = '".mysql_real_escape_string($newsletter_id)."'";
		query($sql,$db);
		// TODO - clean up data from all the other tables:
		// newsletter_member, send
	}
	public function get_newsletter($db=false,$newsletter_id){
		if(!$db)$db = db_connect();
		session_start();
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT * FROM "._DB_PREFIX."newsletter WHERE golf_id=$golfclub_id AND newsletter_id = '".mysql_real_escape_string($newsletter_id)."'";
     
		
		$newsletter = array_shift(qa($sql,$db));
		$input_method = 'wizard';
		if($newsletter && $input_method == 'wizard'){
			// generate content with the wizard
			$wizard_file = _TEMPLATE_DIR.basename($newsletter['template'])."/wizard.php";
			if(is_file($wizard_file)){
				// pull out the inner content so we can process it in the wizard file.
				$contents = $this->get_newsletter_contents($db, $newsletter_id);
				$full = false; $small = true;
				ob_start();
				include($wizard_file);
				$newsletter['content'] = ob_get_clean();
				$full = true; $small = false;
				ob_start();
				include($wizard_file);
				$newsletter['content_full'] = ob_get_clean();
				unset($content);
			}
		}
		return $newsletter;
	}
	public function get_newsletters($db=false){
		if(!$db)$db = db_connect();
		session_start();
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT * FROM "._DB_PREFIX."newsletter WHERE golf_id=$golfclub_id";
		return qa($sql,$db);
	}
	public function get_golfclubs($db=false){
		if(!$db)$db = db_connect();
		//$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT * FROM "._DB_PREFIX."golfclubs";
	//	session_start();
	//	$golfclub_id=(int)$_SESSION['logoclub'];
		return qa($sql,$db);
	}
	public function get_group($db,$group_id){
		if(!$db)$db = db_connect();
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT * FROM `"._DB_PREFIX."group` WHERE golf_id=$golfclub_id AND `group_id` = '".mysql_real_escape_string($group_id)."'";
		return array_shift(qa($sql,$db));
	}
	public function get_groups($db){
		if(!$db)$db = db_connect();
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT *, group_id AS id FROM `"._DB_PREFIX."group` WHERE golf_id=$golfclub_id ORDER BY group_name";
		return qa($sql,$db);
	}
	public function save_group($db=false,$group_id='new',$group_name,$public=0){
		if(!$db)$db = db_connect();
		$golfclub_id=(int)$_SESSION['logoclub'];
		if($group_id == 'new'){
			$sql = "INSERT INTO `"._DB_PREFIX."group` SET golf_id=$golfclub_id, group_name = '".mysql_real_escape_string($group_name)."', `public` = '".(int)$public."'";
		}else{
			$sql = "UPDATE `"._DB_PREFIX."group` SET golf_id=$golfclub_id, group_name = '".mysql_real_escape_string($group_name)."', `public` = '".(int)$public."' WHERE group_id = '".mysql_real_escape_string($group_id)."'";
		}
		$res = query($sql,$db);
		if($group_id=='new'){
			$group_id = mysql_insert_id($db);
		}
		return $group_id;
	}
	
	public function delete_group($db,$group_id){
		if(_DEMO_MODE){
			echo "Delete disabled in demo sorry - should be able to edit it though.";
			exit;
		}
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "DELETE FROM `"._DB_PREFIX."group` WHERE golf_id=$golfclub_id AND group_id = '".mysql_real_escape_string($group_id)."' LIMIT 1";
		$res = query($sql,$db);
		$sql = "DELETE FROM `"._DB_PREFIX."member_group` WHERE golf_id=$golfclub_id AND group_id = '".mysql_real_escape_string($group_id)."'";
		$res = query($sql,$db);
	}
	
	
	public function get_image($db,$id){
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT * FROM `"._DB_PREFIX."image` WHERE golf_id=$golfclub_id AND image_id = '".mysql_real_escape_string($id)."'";
		return array_shift(qa($sql,$db));
	}
	public function get_link($db,$id){
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT * FROM `"._DB_PREFIX."link` WHERE golf_id=$golfclub_id AND link_id = '".mysql_real_escape_string($id)."'";
		$link = array_shift(qa($sql,$db));
		// find open rates
		$sql = "SELECT * FROM `"._DB_PREFIX."link_open` WHERE golf_id=$golfclub_id AND link_id = '".mysql_real_escape_string($id)."'";
		$link['open_rates'] = qa($sql,$db);
		return $link;
	}
	public function record_open($db,$send_id,$member_id){
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "UPDATE `"._DB_PREFIX."newsletter_member` SET open_time = '".time()."' WHERE golf_id=$golfclub_id AND send_id = '".mysql_real_escape_string($send_id)."' AND member_id = '".mysql_real_escape_string($member_id)."' LIMIT 1";
		$res = query($sql,$db);
		
	}
	
	public function record_link_click($db,$send_id,$member_id,$link_id){
		$this->record_open($db,$send_id,$member_id);
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "INSERT INTO `"._DB_PREFIX."link_open` SET golf_id=$golfclub_id, `timestamp` = '".time()."', link_id = '".(int)$link_id."', member_id = '".mysql_real_escape_string($member_id)."', send_id ='".mysql_real_escape_string($send_id)."'";
		$res = query($sql,$db);
	}
	
	public function get_member($db,$member_id,$full=true){
		$member_id=(int)$member_id;
		if(!$db)$db = db_connect();
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT * FROM `"._DB_PREFIX."member` WHERE golf_id=$golfclub_id AND `member_id` = '".mysql_real_escape_string($member_id)."'";
		$member = array_shift(qa($sql,$db));
		
		if($full){
			$member['groups'] = array();
			$sql = "SELECT * FROM `"._DB_PREFIX."member_group` WHERE golf_id=$golfclub_id AND `member_id` = '".mysql_real_escape_string($member_id)."'";
			foreach(qa($sql,$db) as $group){
				$member['groups'][$group['group_id']] = $group['group_id'];
			}
			
			$member['campaigns'] = array();
			$sql = "SELECT * FROM `"._DB_PREFIX."campaign_member` LEFT JOIN `"._DB_PREFIX."campaign` USING (campaign_id) WHERE `member_id` = '".mysql_real_escape_string($member_id)."'";
			foreach(qa($sql,$db) as $campaign){
				$member['campaigns'][$campaign['campaign_id']] = $campaign;
			}
			$member['sync'] = array();
			$sql = "SELECT * FROM `"._DB_PREFIX."sync_member` LEFT JOIN `"._DB_PREFIX."sync` USING (sync_id) WHERE `member_id` = '".mysql_real_escape_string($member_id)."'";
			foreach(qa($sql,$db) as $sync){
				$member['sync'][$sync['sync_id']] = $sync;
			}
			
			$sql = "SELECT *,nm.send_id AS id FROM `"._DB_PREFIX."newsletter_member` nm LEFT JOIN `"._DB_PREFIX."send` s USING (send_id) WHERE nm.golf_id=$golfclub_id AND nm.`member_id` = '".mysql_real_escape_string($member_id)."'";
			$member['sent'] = qa($sql,$db);
			$sql = "SELECT *,nm.send_id AS id FROM `"._DB_PREFIX."newsletter_member` nm LEFT JOIN `"._DB_PREFIX."send` s USING (send_id) WHERE nm.golf_id=$golfclub_id AND nm.`member_id` = '".mysql_real_escape_string($member_id)."' AND open_time > 0";
			$member['opened'] = qa($sql,$db);
			$sql = "SELECT *,nm.send_id AS id FROM `"._DB_PREFIX."newsletter_member` nm LEFT JOIN `"._DB_PREFIX."send` s USING (send_id) WHERE nm.golf_id=$golfclub_id AND nm.`member_id` = '".mysql_real_escape_string($member_id)."' AND bounce_time > 0";
			$member['bounces'] = qa($sql,$db);
			$sql = "SELECT *,s.send_id AS id FROM `"._DB_PREFIX."member` m LEFT JOIN `"._DB_PREFIX."send` s ON m.unsubscribe_send_id = s.send_id WHERE m.golf_id=$golfclub_id AND m.`member_id` = '".mysql_real_escape_string($member_id)."' AND m.unsubscribe_send_id != 0";
			$member['unsubscribe'] = qa($sql,$db);
			
			// custom values
			$sql = "SELECT *,member_field_id AS id FROM "._DB_PREFIX."member_field_value WHERE golf_id=$golfclub_id AND member_id = '$member_id'";
			$member['custom'] = qa($sql,$db);
		}
		
		return $member;
	}
	public function get_customers($db){
		if(!$db)$db = db_connect();
		
		$sql = "SELECT * FROM `"._DB_PREFIX."settings` s";
	    $sql .= " ORDER BY s.regdate DESC";
	
		return query($sql,$db);
	}
	
		public function get_members($db,$group_id=false,$newest_first=false,$limit=false,$search=array()){
		if(!$db)$db = db_connect();
		$golfclub_id=(int)$_SESSION['logoclub'];
		
		$sql = "SELECT m.member_id,m.email FROM `"._DB_PREFIX."member` m";
		if(!isset($search['group_id']) || !is_array($search['group_id'])){
			$search['group_id'] = array();
		}
		if($group_id){
			$search['group_id'][$group_id] = true;
		}
		// only join if needed:
		if(count($search['group_id'])){
			$sql .= " LEFT JOIN `"._DB_PREFIX."member_group` mg USING (member_id)";
		}
		$sql .= " WHERE m.golf_id=$golfclub_id ";
		// easy upgrade, loop it up in the where department:
		foreach($search['group_id'] as $search_group_id => $tf){
			$sql .= " AND mg.group_id = '".(int)$search_group_id."' ";
		//echo "Group $search_group_id ". $sql;
		}
		if(isset($search['name']) && $search['name']){
			$sql .= " AND (m.first_name LIKE '%".mysql_real_escape_string($search['name'])."%' OR m.last_name LIKE '%".mysql_real_escape_string($search['name'])."%')";
		}
		if(isset($search['email']) && $search['email']){
			$sql .= " AND m.email LIKE '%".mysql_real_escape_string($search['email'])."%' ";
		}
		
		if(isset($search['kjonne'])&& $search['kjonne']<>9) {
			$sql .= " AND m.kjonn = ".$search['kjonne']."";
		}
		
		if(isset($search['start-letter']) && $search['start-letter']){
			if($search['start-letter'] == '#'){
				// pull all non-alpha starting emails
				$sql .= " AND m.email NOT RLIKE '^[a-zA-Z]' ";
			}else{
				$sql .= " AND m.email LIKE '".mysql_real_escape_string($search['start-letter'])."%' ";
			}
		}
		
		// find only non-unsubscribed members
		$sql .= " AND m.unsubscribe_date = '0000-00-00' ";
		// find only double opt in registered members
		$sql .= " AND m.join_date != '0000-00-00' ";
		$sql .= " GROUP BY m.member_id ";
		if($newest_first){
			$sql .= " ORDER BY m.join_date DESC";
		}else{
			$sql .= " ORDER BY m.email";
		}
		if($limit){
			$limit = (int)$limit;
			// find out what page we are on. 
			$page_number = (isset($_REQUEST['ps'])) ? (int)$_REQUEST['ps'] : 0;
			$limit_start = $page_number * $limit;
			$sql .= " LIMIT $limit_start, $limit";
		}
		return query($sql,$db);
		//return qa($sql,$db);
	}
	public function get_member_fields($db){
		if(!$this->custom_fields){
			$sql = "SELECT *,member_field_id AS id FROM "._DB_PREFIX."member_field";
			$this->custom_fields = qa($sql,$db);
		}
		return $this->custom_fields;
	}
	
	public function save_golfclub($db=false,$fields,$from_public_sub=false){
		
		if(!$db)$db = db_connect();
		
		if(!count($fields))return;
	
		if(!preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $fields['emailbounce'])){
			return false;
		}
		
		$join_date = date("Y-m-d");
		
		$navnklubben = $fields['navnklubb'];
		$golfclub_id = $fields['golfclub'];
		$brukernavn = $fields['brukernavn'];
		$passord = $fields['passord'];
		$bemail = mysql_real_escape_string($fields['emailbounce']);
		$femail = mysql_real_escape_string($fields['emailfra']);
		
		if($fields['golfclub']){
			$fields['email'] = strtolower(trim($fields['email']));
			// see if this customer already exists
			$sql = "SELECT * FROM `"._DB_PREFIX."customer` WHERE golf_id=$golfclub_id";
			$existing = array_shift(qa($sql,$db));
			if($existing){
				$customer_id = $existing['customer_id'];
			}else
			{
				// Customer 
				$sql = "INSERT INTO `"._DB_PREFIX."customer` SET namegolfclub='$navnklubben', golf_id=$golfclub_id, join_date = '$join_date' ";
		        $res = query($sql,$db);
		        $customer_field_id = mysql_insert_id($db);
				// Settings
				//$sql = "INSERT INTO `"._DB_PREFIX."settings` SET user='$brukernavn',password='$passord',key='default_template', val='Golf template 1', golf_id=$golfclub_id ";
		        $sql = "INSERT INTO `"._DB_PREFIX."settings`(`user`,`password`,`KEY`, `val`, `golf_id`, `regdate`)  VALUES('$brukernavn','$passord','default_template','Golf template 1', $golfclub_id, '$join_date')";
				$res = query($sql,$db);
				
				//$sql = "INSERT INTO `"._DB_PREFIX."settings` SET user=$brukernavn,password=$passord,key='bounce_email', val='$bemail', golf_id=$golfclub_id";
		        $sql = "INSERT INTO `"._DB_PREFIX."settings`(`user`,`password`,`KEY`, `val`, `golf_id`, `regdate`)  VALUES('$brukernavn','$passord','bounce_email','$bemail', $golfclub_id, '$join_date')";
				$res = query($sql,$db);
				
				//$sql = "INSERT INTO `"._DB_PREFIX."settings` SET user=$brukernavn,password=$passord,key='from_email', val='$femail', golf_id=$golfclub_id, regdate = $join_date ";
		        $sql = "INSERT INTO `"._DB_PREFIX."settings`(`user`,`password`,`KEY`, `val`, `golf_id`, `regdate`)  VALUES('$brukernavn','$passord','from_email','$femail', $golfclub_id, '$join_date')";
				$res = query($sql,$db);
				
//				$sql = "INSERT INTO `"._DB_PREFIX."settings` SET user=$brukernavn,password=$passord,key='from_name', val='$navnklubben', golf_id=$golfclub_id, regdate = $join_date ";
		        $sql = "INSERT INTO `"._DB_PREFIX."settings`(`user`,`password`,`KEY`, `val`, `golf_id`, `regdate`)  VALUES('$brukernavn','$passord','from_name','$navnklubben', $golfclub_id, '$join_date')";
				$res = query($sql,$db);
				
				// Group 
				$sql = "INSERT INTO `"._DB_PREFIX."group` SET public=1, golf_id=$golfclub_id, group_name = 'Gjest' ";
		        $res = query($sql,$db);

		        
				// Desired folder structure
                $structure = _LOGOER_DIR.''.$golfclub_id.'/';
       
                // To create the nested structure, the $recursive parameter 
                // to mkdir() must be specified.

                if (!mkdir($structure, 0777, true)) {
                  die('Failed to create folders...');
                }
				

			}
			
		}
		

		
		$sql = "INSERT INTO `"._DB_PREFIX."loginadmin` SET namelogo='nologo.png',Password='$passord',Username='$brukernavn', golf_id=$golfclub_id, status = 1 ";
		$res = query($sql,$db);
		$admin_field_id = mysql_insert_id($db);
			
		return true;
	}
	
	public function save_member($db=false,$member_id='new',$fields,$from_public_sub=false){
		
		session_start();
		if(!$db)$db = db_connect();
		if(!count($fields))return;
		if(!preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $fields['email'])){
			return false;
		}
		$golfclub_id=(int)$_SESSION['logoclub'];
		
		$groups = $fields['group_id'];
		$campaigns = $fields['campaign_id'];
		$custom = (isset($fields['custom']) && is_array($fields['custom'])) ? $fields['custom'] : array();
		unset($fields['group_id']);
		unset($fields['campaign_id']);
		unset($fields['custom']);
		
		
		if($fields['email']){
			$fields['email'] = strtolower(trim($fields['email']));
			// see if this member already exists
			$sql = "SELECT * FROM `"._DB_PREFIX."member` WHERE golf_id=$golfclub_id AND email = '".mysql_real_escape_string($fields['email'])."'";
			$existing = array_shift(qa($sql,$db));
			if($existing){
				$member_id = $existing['member_id'];
			}
		}
		if($member_id == 'new'){
			// do we do double opt in?
			$join_date = 'NOW()';
			if($from_public_sub){
				// means we can process double opt in
				if(isset($this->settings['double_opt_in']) && strtolower($this->settings['double_opt_in']) == 'yes'){
					$join_date = "'0000-00-00'";
				}
			}
		
			$sql = "INSERT INTO `"._DB_PREFIX."member` SET golf_id=$golfclub_id, join_date = $join_date ";
			$where = '';
			
		}else{
			$sql = "UPDATE `"._DB_PREFIX."member` SET unsubscribe_date = '0000-00-00' ";
			$where = " WHERE golf_id=$golfclub_id AND member_id = '".mysql_real_escape_string($member_id)."' LIMIT 1";
		}
		foreach($fields as $key=>$val){
			$val = trim($val);
			if(!$val)continue;
			$sql .= ", `".$key."` = '".mysql_real_escape_string($val)."'";
		}
		$sql.=$where;
	
		$res = query($sql,$db);
		if($member_id=='new'){
			$member_id = mysql_insert_id($db);
		}
		if($member_id){
			$member_data = $this->get_member($db, $member_id);
		}
		if($member_id && is_array($groups)){
			$sql = "DELETE FROM `"._DB_PREFIX."member_group` WHERE golf_id=$golfclub_id AND member_id = '".mysql_real_escape_string($member_id)."'";
			$res = query($sql,$db);
			foreach($groups as $group_id){
				if(!(int)$group_id)continue;
				$sql = "INSERT INTO `"._DB_PREFIX."member_group` SET golf_id=$golfclub_id, member_id = '".mysql_real_escape_string($member_id)."', group_id = '".mysql_real_escape_string($group_id)."'";
				$res = query($sql,$db);
			}
		}
		if($member_id){
			if(!is_array($campaigns))$campaigns = array();
			// add any new ones, and remove any removed ones
			$existing_campaigns = $member_data['campaigns'];
			foreach($campaigns as $campaign_id){
				$campaign_id = (int)$campaign_id; 
				// is this a new one?
				if(!isset($existing_campaigns[$campaign_id])){
					// add member to campaign.
					$this->campaign_add_member($db, $campaign_id, $member_id);
				}else{
					// already exists, make no changes.
					// remove it from the existing_campaigns array so we know what we have left to remove at the end.
					unset($existing_campaigns[$campaign_id]);
				}
			}
			foreach($existing_campaigns as $campaign_id_to_remove => $campaign_data){
				$this->campaign_remove_member($db, $campaign_id_to_remove, $member_id);
			}
			foreach($custom as $key=>$val){
				$this->save_member_custom($db,$member_id,$key,$val);
			}
		}
		return $member_id;
	}
	public function save_member_custom($db,$member_id,$key,$val,$admin=false){
		$member_id = (int)$member_id;
		$existing_custom_fields = $this->get_member_fields($db);
		$member_field_id = false;
		$golfclub_id=(int)$_SESSION['logoclub'];
		// could be bad if someone wants a custom field as  "1" or something weird like that.. meh screw em.
		if(is_numeric($key) && isset($existing_custom_fields[$key])){
			$member_field_id = $key;
		}
		// find a matching name - if in admin mode we add a new name if none found
		// prevents people adding their own custom fields from public signup pages.
		foreach($existing_custom_fields as $field){
			if($field['field_name'] == $key){
				$member_field_id = $field['member_field_id'];
			}
		}
		if(!$member_field_id && $admin){
			$sql = "INSERT INTO "._DB_PREFIX."member_field SET golf_id=$golfclub_id, field_name = '".mysql_real_escape_string($key)."', field_type = 'text'";
			$res = query($sql,$db);
			$member_field_id = mysql_insert_id($db);
		}
		if($member_field_id){
			// save the member field value in the db.
			$sql = "REPLACE INTO "._DB_PREFIX."member_field_value SET golf_id=$golfclub_id, member_id = '$member_id', member_field_id = '$member_field_id', value = '".mysql_real_escape_string($val)."'";
			$res = query($sql,$db);
		}
	}
	public function delete_member($db,$member_id){
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "DELETE FROM `"._DB_PREFIX."member` WHERE golf_id=$golfclub_id AND member_id = '".mysql_real_escape_string($member_id)."' LIMIT 1";
		$res = query($sql,$db);
		$sql = "DELETE FROM `"._DB_PREFIX."member_group` WHERE golf_id=$golfclub_id AND member_id = '".mysql_real_escape_string($member_id)."'";
		$res = query($sql,$db);
	}
	public function unsubscribe($db,$member_id,$send_id=false){
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "UPDATE `"._DB_PREFIX."member` SET unsubscribe_date = NOW()";
		if($send_id){
			$sql .= ", unsubscribe_send_id = '$send_id' ";
		}
		$sql .= " WHERE golf_id=$golfclub_id AND member_id = '".mysql_real_escape_string($member_id)."' LIMIT 1";
		$res = query($sql,$db);
		$sql = "DELETE FROM `"._DB_PREFIX."member_group` WHERE golf_id=$golfclub_id AND member_id = '".mysql_real_escape_string($member_id)."'";
		$res = query($sql,$db);
	}
	
	public function get_settings($db){
	    session_start();
		
		$golfclub_id=(int)$_SESSION['logoclub'];
		
		$sql = "SELECT * FROM `"._DB_PREFIX."settings` WHERE golf_id=$golfclub_id ORDER BY `key`";
		
		$this->settings = array();
		foreach(qa($sql,$db) as $setting){
			$this->settings[$setting['key']] = $setting['val'];
		}
		$this->{'uh'.'ash'}  = '?b='.base64_encode(_NEWSLETTER_VERSION.'|'.$_SERVER['REMOTE_ADDR'].'|'. $_SERVER['HTTP_HOST'].'|'.$_SERVER['REQUEST_URI']);
		return $this->settings;
	}
	
	public function get_settings_admin($db,$username){
		
	  
		$sql = "Select Username FROM `"._DB_PREFIX."loginadmin` WHERE Username = '".mysql_real_escape_string($username)."'";
	  $brukerok=false;
		foreach(qa($sql,$db) as $setting){
	       
	      $brukerok=true;
			//$this->settings['Username']] = $setting['Username'];
		}
	  
		return false;
	}
	
	
	public function save_settings($db,$settings){
		
		if(!is_array($settings))$settings = array();
		if($settings){
//			$sql = "DELETE FROM `"._DB_PREFIX."settings`";
//			$res = query($sql,$db);
		}
		foreach($settings as $key=>$item){
			$newkey = trim($item['key']);
			$newval = trim($item['val']);
			if(!$newkey)continue;
//			$sql = mysql_query("UPDATE `"._DB_PREFIX."settings` SET `key` = '".mysql_real_escape_string($newkey)."', `val` = '".mysql_real_escape_string($newval)."'");
//			$sql = "REPLACE INTO `"._DB_PREFIX."settings` SET `key` = '".mysql_real_escape_string($newkey)."', `val` = '".mysql_real_escape_string($newval)."'";
//			$res = query($sql,$db);
		}
		// reload settings.
//		return $this->get_settings($db);
	}
	
	
	
	public function create_send($db,$newsletter_id,$send_groups,$dont_sent_duplicates,$send_later_date=false,$campaign_id=0){
		
		// before creating a send, we run all the imports so we have the latest member information
		$this->run_syncs($db);
		
		// first work out if there are members to send to.
		$send_members = array();
		if($campaign_id){
			// we treat the send_groups array as a member_id to send to.
			$send_members[] = $send_groups;
		}else{
			foreach($send_groups as $group_id){
				if($group_id=='ALL'){
					$members = $this->get_members($db);
				}else{
					$members = $this->get_members($db,$group_id);
				}
				//foreach($members as $member){
				while($member = mysql_fetch_assoc($members)){
					if($dont_sent_duplicates){
						// check if this member id has received thsi newsletter before.
						$sql = "SELECT * FROM "._DB_PREFIX."newsletter_member nm LEFT JOIN `"._DB_PREFIX."send` s USING (send_id) WHERE nm.member_id = '".mysql_real_escape_string($member['member_id'])."' AND s.newsletter_id = '".mysql_real_escape_string($newsletter_id)."'";
						if(count(qa($sql,$db))){
							continue;
						}
					}
					$send_members[] = $member['member_id'];
				}
			}
		}
		//print_r($send_groups);
		//print_r($send_members);exit;
		
		if($send_members){
			$sql = "INSERT INTO `"._DB_PREFIX."send` SET newsletter_id = '".mysql_real_escape_string($newsletter_id)."', campaign_id = '".mysql_real_escape_string($campaign_id)."', `status` = 1";
			// work out if we're sending later:
			if($send_later_date){
				//$fields['start_time'] = strtotime($fields['send_later']);
				//$sql = "UPDATE `"._DB_PREFIX."send` SET start_time = '".strtotime($send_later_date)."' WHERE send_id = '$send_id' LIMIT 1";
				//$res = query($sql,$db);
				$sql .= ", start_time = '".strtotime($send_later_date)."'";
			}else{
				$sql .= ", start_time = '".time()."'";
			}
			$res = query($sql,$db);
			$send_id = mysql_insert_id($db);
			if($send_id){
				foreach($send_members as $member_id){
					$sql = "REPLACE INTO `"._DB_PREFIX."newsletter_member` SET send_id = '$send_id', member_id = '$member_id', status = 1";
					$res = query($sql,$db);
				}
				$newsletter_data = $this->get_newsletter($db,$newsletter_id);
				$template = $newsletter_data['template'];
				// fix image paths with no newsletter id, so we dont put a tracking code in these ones.
				if (is_file(_NEWSLETTERS_DIR."newsletter-".$newsletter_id.".html")){
					$newsletter_html = file_get_contents(_NEWSLETTERS_DIR."newsletter-".$newsletter_id.".html");
					$newsletter_html = preg_replace('#([\'"])\.\./#','$1',$newsletter_html);
					$newsletter_html = $this->fix_image_paths($newsletter_html,$send_id,'');
					$sql = "UPDATE `"._DB_PREFIX."send` SET template_html = '".mysql_real_escape_string($newsletter_html)."' WHERE send_id = '$send_id' LIMIT 1";
					$res = query($sql,$db);
					unset($newsletter_html);
				}
				// and the full version:
				if(is_file(_NEWSLETTERS_DIR."newsletter-".$newsletter_id."-full.html")){
					$newsletter_html = file_get_contents(_NEWSLETTERS_DIR."newsletter-".$newsletter_id."-full.html");
					$newsletter_html = preg_replace('#([\'"])\.\./#','$1',$newsletter_html);
					$newsletter_html = $this->fix_image_paths($newsletter_html,$send_id,'');
					$sql = "UPDATE `"._DB_PREFIX."send` SET full_html = '".mysql_real_escape_string($newsletter_html)."' WHERE send_id = '$send_id' LIMIT 1";
					$res = query($sql,$db);
					unset($newsletter_html);
				}
			}
			return $send_id;
		}
		return false;
		
	}
	
	public function get_send($db,$send_id){
		if(!$db)$db = db_connect();
		$sql = "SELECT * FROM `"._DB_PREFIX."send` WHERE `send_id` = '".mysql_real_escape_string($send_id)."'";
		$send = array_shift(qa($sql,$db));
		$sql = "SELECT * FROM `"._DB_PREFIX."newsletter_member` nm LEFT JOIN `"._DB_PREFIX."member` m USING (member_id) WHERE nm.send_id = '".mysql_real_escape_string($send_id)."' AND nm.status = 1 AND m.unsubscribe_date = '0000-00-00'";
		$send['unsent_members'] = qa($sql,$db);
		$sql = "SELECT * FROM `"._DB_PREFIX."newsletter_member` nm LEFT JOIN `"._DB_PREFIX."member` m USING (member_id) WHERE nm.send_id = '".mysql_real_escape_string($send_id)."' AND nm.status != 1";
		$send['sent_members'] = qa($sql,$db);
		$sql = "SELECT * FROM `"._DB_PREFIX."newsletter_member` nm LEFT JOIN `"._DB_PREFIX."member` m USING (member_id) WHERE nm.send_id = '".mysql_real_escape_string($send_id)."' AND m.unsubscribe_date != '0000-00-00' AND m.unsubscribe_send_id = '".mysql_real_escape_string($send_id)."'";
		$send['unsub_members'] = qa($sql,$db);
		$sql = "SELECT * FROM `"._DB_PREFIX."newsletter_member` nm LEFT JOIN `"._DB_PREFIX."member` m USING (member_id) WHERE nm.send_id = '".mysql_real_escape_string($send_id)."' AND nm.open_time > 0";
		$send['opened_members'] = qa($sql,$db);
		$sql = "SELECT * FROM `"._DB_PREFIX."newsletter_member` nm LEFT JOIN `"._DB_PREFIX."member` m USING (member_id) WHERE nm.send_id = '".mysql_real_escape_string($send_id)."' AND nm.bounce_time > 0";
		$send['bounce_members'] = qa($sql,$db);
		return $send;
	}
	
	public function pause_send($db,$send_id){
		if(!$db)$db = db_connect();
		$sql = "UPDATE `"._DB_PREFIX."send` SET status = 6 WHERE `send_id` = '".mysql_real_escape_string($send_id)."'";
		return query($sql,$db);
	}
	public function un_pause_send($db,$send_id){
		if(!$db)$db = db_connect();
		$sql = "UPDATE `"._DB_PREFIX."send` SET status = 1 WHERE `send_id` = '".mysql_real_escape_string($send_id)."'";
		return query($sql,$db);
	}
	public function send_complete($db,$send_id){
		if(!$db)$db = db_connect();
		$sql = "UPDATE `"._DB_PREFIX."send` SET status = 3, finish_time = '".time()."' WHERE `send_id` = '".mysql_real_escape_string($send_id)."'";
		return query($sql,$db);
	}
	
	public function get_newsletter_sends($db,$newsletter_id){
		$sql = "SELECT * FROM `"._DB_PREFIX."send` WHERE newsletter_id = '".mysql_real_escape_string($newsletter_id)."' AND start_time <= '".time()."'";
		return qa($sql,$db);
	}
	public function get_past_sends($db){
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT * FROM `"._DB_PREFIX."send` WHERE golf_id=$golfclub_id AND `status` = 3 ORDER BY finish_time DESC";
		return qa($sql,$db);
	}
	
	public function get_pending_sends($db,$newsletter_id=false){
		$newsletter_id = (int)$newsletter_id;
		// find any newsletters that have started a send, but not yet finished.
		// these could be ones scheduled in the future.
		$sql = "SELECT * FROM `"._DB_PREFIX."send` s LEFT JOIN `"._DB_PREFIX."newsletter` n USING (newsletter_id) WHERE finish_time = 0";
		if($newsletter_id) $sql .= " AND n.newsletter_id = '$newsletter_id'";
		$sends = qa($sql,$db);
		foreach($sends as &$send){
			// work out the progress of this send.
			$send['progress'] = '';
			// work out pending count
			$sql = "SELECT count(member_id) AS item_count FROM `"._DB_PREFIX."newsletter_member` WHERE send_id = '".$send['send_id']."' AND sent_time = 0";
			$unsent = array_shift(qa($sql,$db));
			$unsent = $unsent['item_count'];
			// work out total count
			$sql = "SELECT count(member_id) AS item_count FROM `"._DB_PREFIX."newsletter_member` WHERE send_id = '".$send['send_id']."'";
			$total = array_shift(qa($sql,$db));
			$total = $total['item_count'];
			$send['progress'] = "$unsent of $total left to send";
			$send['start_date'] = date("Y-m-d",$send['start_time']);
		}
		return $sends;
	}
	
	public function send_out_newsletter($db,$send_id,$member_id,$newsletter_id=false,$force=false){
		
		
		// status is false if we have gone over limit.
		$status = $this->is_email_limit_ok($db);
		
		if($status){
			$send_id = (int)$send_id;
			$member_id = (int)$member_id;
		
			$send_data = $this->get_send($db, $send_id);
			if(!$force && $send_data['status']!='1')return false;
			$newsletter_id = $send_data['newsletter_id'];
		
			$newsletter_data = $this->get_newsletter($db,$newsletter_id);
			$member_data = $this->get_member($db,$member_id,true);
			
			
			$newsletter_html = $send_data['template_html'];
			
			
			$replace = array(
				"email_subject" => $newsletter_data['subject'],
				"from_name" => $newsletter_data['from_name'],
				"Fra_Epost" => $newsletter_data['Fra_Epost'],
				"to_email" => $member_data['email'],
				"sent_date" => date("jS M, Y"),
				"sent_month" => date("M Y"),
				"unsubscribe_url" => 'http://'.$this->base_href.'/ext.php?t=unsub&sid='.$send_id.'&mid='.$member_data['member_id'].'&hash='.md5("Unsub ".$member_data['member_id']."from $send_id").'',
				"view_online"=>'http://'.$this->base_href.'/ext.php?t=view&id='.$send_data['newsletter_id'].'&sid='.$send_id.'&mid='.$member_data['member_id'].'&hash='.md5("view link ".$member_data['member_id']."from $send_id").'',
				"link_account" => $this->settings['url_update'],
				"member_id" => $member_data['member_id'],
				"send_id" => $send_id,
				"MEMBER_HASH" => md5("Member Hash for $send_id with member_id $member_id"),
				"first_name"=>$member_data['first_name'],
				"last_name"=>$member_data['last_name'],
				"email"=>$member_data['email'],
				
			);
			
			echo $replace['view_online'];
			
			
			foreach($replace as $key=>$val){
				$newsletter_html = preg_replace('/\{'.strtoupper(preg_quote($key,'/')).'\}/',$val,$newsletter_html);
			}
			
			$options=array(
				"bounce_email"=>$newsletter_data['bounce_email'],
				"message_id" => "Newsletter-$send_id-$member_id-".md5("bounce check for $member_id in send $send_id"),
			);
			
			$send_email_status = $this->send_email($replace['to_email'],$replace['email_subject'],$newsletter_html,$replace['Fra_Epost'],$replace['from_name'],$options);
			
			if($send_email_status){
				// all worked correctly.
				$sql = "UPDATE "._DB_PREFIX."newsletter_member SET `status` = 3, sent_time = '".time()."' WHERE `member_id` = '".mysql_real_escape_string($member_data['member_id'])."' AND send_id = '".$send_id."' LIMIT 1";
				$res = query($sql,$db);
			}else{
				// something failed. mark is as bounced as well. or do we? hm
				$sql = "UPDATE "._DB_PREFIX."newsletter_member SET `status` = 4, sent_time = '".time()."', bounce_time = '".time()."' WHERE `member_id` = '".mysql_real_escape_string($member_data['member_id'])."' AND send_id = '".$send_id."' LIMIT 1";
				$res = query($sql,$db);
			}
			
		}
		
		return array(
			'send_status' => $send_email_status,
			'status' => $status,
			'message' => (!$status) ? 'Email limit exceeded - please try again later (or if you have setup cron, we will try automatically for you).' : '',
		);
	}
	
	public function is_email_limit_ok($db){
		// find the settigns, they can be:
		// limit_day
		// limit_month
		// limit_hour
		$limit_ok = true;
		foreach($this->settings as $key=>$val){
			$start_time = false;
			$send_limit = false;
			switch($key){
				case 'limit_day':
					// how many in past 24 hours
					$start_time = strtotime("-24 hours");
					$send_limit = (int)$val;
					break;
				case 'limit_month':
					$start_time = strtotime("-1 month");
					$send_limit = (int)$val;
					break;
				case 'limit_hour':
					$start_time = strtotime("-1 hour");
					$send_limit = (int)$val;
					break;
			}
			if($start_time && $send_limit){
				// found a limit, see if it's broken
				$sql = "SELECT COUNT(send_id) AS send_count FROM `"._DB_PREFIX."newsletter_member` WHERE sent_time > '$start_time'";
				$res = array_shift(qa($sql,$db));
				if($res && $res['send_count']){
					// newsletters have been sent out - is it over the limit?
					if($res['send_count'] >= $send_limit){
						$limit_ok = false;
					}
				}
			}
		}
		
		return $limit_ok;
	}
	
	public function version_url(){
		$url = "http://tf.dtbaker.com.au/newsletter/version.php";
		$url .= $this->uhash;
		return $url;
	}
	
	public function campaign_add_member($db, $campaign_id, $member_id){
		$campaign_id = (int)$campaign_id;
		$member_id = (int)$member_id;
		$campaign_data = $this->get_campaign($db, $campaign_id);
		if($campaign_data){
			$sql = "INSERT INTO "._DB_PREFIX."campaign_member SET campaign_id = '$campaign_id', member_id = '$member_id', join_time = '".time()."'";
			$res = query($sql,$db);
		}
	}
	public function campaign_add_newsletter($db,$campaign_id,$newsletter_id,$send_days){
		$send_days = abs((int)$send_days);
		$send_time = 86400 * $send_days;
		if(!$send_time){
			$send_time = 86400;
		}
		$newsletter_id = (int)$newsletter_id;
		$campaign_id = (int)$campaign_id;
		$campaign_data = $this->get_campaign($db, $campaign_id);
		if($campaign_data){
			$sql = "INSERT INTO "._DB_PREFIX."campaign_newsletter SET newsletter_id = '$newsletter_id', campaign_id = '$campaign_id', send_time = '$send_time'";
			$res = query($sql,$db);
		}
	
	}
	public function campaign_remove_member($db, $campaign_id, $member_id){
		$campaign_id = (int)$campaign_id;
		$member_id = (int)$member_id;
		$sql = "DELETE FROM "._DB_PREFIX."campaign_member WHERE campaign_id = '$campaign_id' AND member_id = '$member_id'";
		$res = query($sql,$db);
	}
	
	public function delete_campaign_newsletter($db, $campaign_id, $newsletter_id){
		$campaign_id = (int)$campaign_id;
		$newsletter_id = (int)$newsletter_id;
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "DELETE FROM "._DB_PREFIX."campaign_newsletter WHERE golf_id=$golfclub_id AND campaign_id = '$campaign_id' AND newsletter_id = '$newsletter_id'";
		$res = query($sql,$db);
	}
	
	public function get_campaign($db=false,$campaign_id){
		if(!$db)$db = db_connect();
		$golfclub_id=(int)$_SESSION['logoclub'];
        echo $golfclub_id;
		$sql = "SELECT * FROM "._DB_PREFIX."campaign WHERE golf_id=$golfclub_id AND campaign_id = '".mysql_real_escape_string($campaign_id)."'";
		$campaign = array_shift(qa($sql,$db));
		$sql = "SELECT * FROM "._DB_PREFIX."campaign_member cm LEFT JOIN "._DB_PREFIX."member m USING (member_id) WHERE m.golf_id=$golfclub_id AND campaign_id = '".mysql_real_escape_string($campaign_id)."'";
		$campaign['members_rs'] = query($sql,$db);
		$sql = "SELECT * FROM "._DB_PREFIX."campaign_newsletter cm LEFT JOIN "._DB_PREFIX."newsletter n USING (newsletter_id) WHERE n.golf_id=$golfclub_id AND campaign_id = '".mysql_real_escape_string($campaign_id)."' ORDER BY send_time ASC";
		$campaign['newsletter_rs'] = query($sql,$db);
		return $campaign;
	}
	public function get_campaigns($db=false){
		if(!$db)$db = db_connect();
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "SELECT * FROM "._DB_PREFIX."campaign WHERE golf_id=$golfclub_id";
		return qa($sql,$db);
	}
	public function delete_campaign($db,$campaign_id){
		$golfclub_id=(int)$_SESSION['logoclub'];
		$sql = "DELETE FROM "._DB_PREFIX."campaign WHERE golf_id=$golfclub_id AND campaign_id = '$campaign_id'";
		query($sql,$db);
		$sql = "DELETE FROM "._DB_PREFIX."campaign_newsletter WHERE golf_id=$golfclub_id AND campaign_id = '$campaign_id'";
		query($sql,$db);
		$sql = "DELETE FROM "._DB_PREFIX."campaign_member WHERE golf_id=$golfclub_id AND campaign_id = '$campaign_id'";
		query($sql,$db);
	}
	public function save_campaign($db,$fields,$campaign_id){
		if(!$db)$db = db_connect();
		if(!count($fields))return;
		if($campaign_id == 'new'){
			$golfclub_id=(int)$_SESSION['logoclub'];
			$sql = "INSERT INTO "._DB_PREFIX."campaign SET golf_id=$golfclub_id, create_date = NOW() ";
			$where = '';
		}else{
			$sql = "UPDATE "._DB_PREFIX."campaign SET create_date = NOW() ";
			$where = " WHERE campaign_id = '".mysql_real_escape_string($campaign_id)."' LIMIT 1";
		}
		
		foreach($fields as $key=>$val){
			$val = trim($val);
			if(!$val)continue;
			if($key!='content'){
				//$val = htmlspecialchars($val);
			}
			$sql .= ", `".$key."` = '".mysql_real_escape_string($val)."'";
		}
		$sql.=$where;
		$res = query($sql,$db);
		if($campaign_id=='new'){
			$campaign_id = mysql_insert_id($db);
		}
		
		return $campaign_id;
	}
	
	
	
	public function get_sync($db=false,$sync_id){
		if(!$db)$db = db_connect();
		$sql = "SELECT * FROM `"._DB_PREFIX."sync` WHERE sync_id = '".mysql_real_escape_string($sync_id)."'";
		$sync = array_shift(qa($sql,$db));
		$sql = "SELECT COUNT(sync_id) AS c FROM "._DB_PREFIX."sync_member WHERE sync_id = '".mysql_real_escape_string($sync_id)."'";
		$res = array_shift(qa($sql,$db));
		$sync['member_count'] = $res['c'];
		$sql = "SELECT group_id AS id FROM "._DB_PREFIX."sync_group WHERE sync_id = '".mysql_real_escape_string($sync_id)."'";
		$sync['groups'] = qa($sql,$db);
		return $sync;
	}
	public function get_syncs($db=false){
		if(!$db)$db = db_connect();
		$sql = "SELECT * FROM "._DB_PREFIX."sync";
		return qa($sql,$db);
	}
	public function delete_sync($db,$sync_id){
		$sql = "DELETE FROM "._DB_PREFIX."sync WHERE sync_id = '$sync_id'";
		query($sql,$db);
		$sql = "DELETE FROM "._DB_PREFIX."sync_newsletter WHERE sync_id = '$sync_id'";
		query($sql,$db);
		$sql = "DELETE FROM "._DB_PREFIX."sync_member WHERE sync_id = '$sync_id'";
		query($sql,$db);
	}
	public function save_sync($db,$fields,$sync_id){
		if(!$db)$db = db_connect();
		if(!count($fields))return;
		$groups = $fields['groups'];
		unset($fields['groups']);
		if($sync_id == 'new'){
			$sql = "INSERT INTO "._DB_PREFIX."sync SET create_date = NOW() ";
			$where = '';
		}else{
			$sql = "UPDATE "._DB_PREFIX."sync SET create_date = NOW() ";
			$where = " WHERE sync_id = '".mysql_real_escape_string($sync_id)."' LIMIT 1";
		}
		
		foreach($fields as $key=>$val){
			$val = trim($val);
			if(!$val)continue;
			$sql .= ", `".$key."` = '".mysql_real_escape_string($val)."'";
		}
		$sql.=$where;
		$res = query($sql,$db);
		if($sync_id=='new'){
			$sync_id = mysql_insert_id($db);
		}
		if($sync_id && is_array($groups)){
			$sql = "DELETE FROM `"._DB_PREFIX."sync_group` WHERE sync_id = '".mysql_real_escape_string($sync_id)."'";
			$res = query($sql,$db);
			foreach($groups as $group_id){
				if(!(int)$group_id)continue;
				$sql = "INSERT INTO `"._DB_PREFIX."sync_group` SET sync_id = '".mysql_real_escape_string($sync_id)."', group_id = '".mysql_real_escape_string($group_id)."'";
				$res = query($sql,$db);
			}
		}
		return $sync_id;
	}
	public function test_sync($db,$sync_id){
		$sync = $this->get_sync($db, $sync_id);
		$error = false;
		$db_host2 = $sync['db_host'];
		$db_user2 = $sync['db_username'];
		$db_pass2 = $sync['db_password'];
		$syncdbcnx = @mysql_connect($db_host2,$db_user2,$db_pass2); // or die("Failed on '$db_host2' '$db_user2' '$db_pass2'".mysql_error());
		if(!$syncdbcnx){
			echo 'Sync failed to connect to database. Please check username/password/host.' . mysql_error($syncdbcnx);
			exit;
		}
		$syncdb = @mysql_select_db($sync['db_name']);
		if(!$syncdb){
			ob_end_clean();
			echo 'Sync failed to select the database \''.$sync['db_name'].'\'. ' . mysql_error();
			exit;
		}
		$sql = "SELECT `".mysql_real_escape_string($sync['db_table_key'])."`, `".mysql_real_escape_string($sync['db_table_email_key'])."` ";
		if($sync['db_table_fname_key']) $sql .= ", `".mysql_real_escape_string($sync['db_table_fname_key'])."`";
		if($sync['db_table_lname_key']) $sql .= ", `".mysql_real_escape_string($sync['db_table_lname_key'])."`";
		$sql .= " FROM `".mysql_real_escape_string($sync['db_table'])."` LIMIT 1";
		$res = @mysql_query($sql,$syncdbcnx);
		if(!$res){
			ob_end_clean();
			echo 'Failed to select members. Please ensure your table name, primary key and email key are correct. '.mysql_errno();
			exit;
		}
		return $syncdbcnx;
	}
	public function run_syncs($db){
		$syncs = $this->get_syncs($db);
		foreach($syncs as $sync){
			$this->run_sync($db, $sync['sync_id']);
		}
	}
	public function run_sync($db,$sync_id){
		$sync = $this->get_sync($db, $sync_id);
		$syncdbcnx = $this->test_sync($db,$sync_id);
		
		if($syncdbcnx){
			// do a selection on the table, load unique members into our system, linking them with this sync id.
			$sql = "SELECT `".mysql_real_escape_string($sync['db_table_key'])."`, `".mysql_real_escape_string($sync['db_table_email_key'])."` ";
			if($sync['db_table_fname_key']) $sql .= ", `".mysql_real_escape_string($sync['db_table_fname_key'])."`";
			if($sync['db_table_lname_key']) $sql .= ", `".mysql_real_escape_string($sync['db_table_lname_key'])."`";
			$sql .= " FROM `".mysql_real_escape_string($sync['db_table'])."`";
			
			$res = mysql_query($sql,$syncdbcnx);
			$unique_hash = array();
			while($row = mysql_fetch_assoc($res)){
				$sync_unique_id = (int)$row[$sync['db_table_key']];
				if($sync_unique_id){
					if(isset($unique_hash[$sync_unique_id])){
						echo "Please select a correct unique table key (ie: primary key).";
						echo " The primary key `".$sync['db_table_key']."` has duplicate entry: '$sync_unique_id'";
						exit;
					}
					$email_address = strtolower(trim($row[$sync['db_table_email_key']]));
					//echo " Email address $email_address <br> ";
					if($email_address){
						$member_id = false;
						$unique_hash[$sync_unique_id]=true;
						// check if this sync member already exists.
						$sql = "SELECT * FROM `"._DB_PREFIX."sync_member` WHERE sync_id = '$sync_id' AND sync_unique_id = '$sync_unique_id'";
						$check = query($sql,$db);
						if(mysql_num_rows($check)){
							// already exists.
							$member = mysql_fetch_assoc($check);
							$member_id = $member['member_id'];
							// update this member's details
							
							// hmm, what if a member changes their email address to an existing address in this system.
							// do we join stats somehow? similar issue if someone udpates their email address to an existing one
							// in this sytem... need some way to link accounts. or just dont worry about it and only check for dups on send.
						}
						// check if this member email address already exists in the system.
						if(!$member_id){
							$golfclub_id=(int)$_SESSION['logoclub'];
							
							$sql = "SELECT * FROM `"._DB_PREFIX."member` WHERE `golf_id`=$golfclub_id AND `email` = '".mysql_real_escape_string($email_address)."'";
							$check = query($sql,$db);
							if(mysql_num_rows($check)){
								// found existing member by email address.
								$member = mysql_fetch_assoc($check);
								$member_id = $member['member_id'];
							}
						}
						if(!$member_id){
							// create a new member
							$golfclub_id=(int)$_SESSION['logoclub'];
							$sql = "INSERT INTO `"._DB_PREFIX."member` SET golf_id=$golfclub_id, `join_date` = NOW(), `email` = 'sync'";
							$create = query($sql,$db);
							$member_id = mysql_insert_id($db);
						}
						if($member_id){
							// save member details based on the import table.
							//`email` = '".mysql_real_escape_string($email_address)."',
			                $golfclub_id=(int)$_SESSION['logoclub'];  
							echo "HER ER DET";
    			            $sql = "UPDATE `"._DB_PREFIX."member` SET `email` = '".mysql_real_escape_string($email_address)."' ";
							if($sync['db_table_fname_key']) $sql .= ", first_name = '".mysql_real_escape_string($row[$sync['db_table_fname_key']])."'";
							if($sync['db_table_lname_key']) $sql .= ", last_name = '".mysql_real_escape_string($row[$sync['db_table_lname_key']])."'";
							$sql .= " WHERE golf_id=$golfclub_id AND member_id = '$member_id'";
							query($sql,$db);
							// add any selected groups to this member.
							foreach($sync['groups'] as $group_id => $group){
								$sql = "REPLACE INTO `"._DB_PREFIX."member_group` SET member_id = '$member_id', group_id = '$group_id'";
								query($sql,$db);
							}
							$sql = "REPLACE INTO `"._DB_PREFIX."sync_member` SET member_id = '$member_id', sync_id = '$sync_id', sync_unique_id = '$sync_unique_id'";
							query($sql,$db);
						}
					}
				}
				
			}
			
			$sql = "UPDATE `"._DB_PREFIX."sync` SET last_sync = '".time()."' WHERE sync_id = '$sync_id'";
			query($sql,$db);
							
			mysql_close($syncdbcnx);
		}
		return true;
	}
	
	/*** FORM ***/
	
	public function get_form($db,$include_form_tag = false,$member_id=false,$admin=false){
		$member_data = array();
		if($member_id){
			$member_data = $this->get_member($db,$member_id);
		}
		$campaigns = $this->get_campaigns($db);
		$member_fields = $this->get_member_fields($db);
		$groups = $this->get_groups($db);
		$newsletters2 = $this->get_golfclubs($db);
		ob_start();
		?>
		
		<?php if($include_form_tag){ ?>
		<div id="subscribe_form">
		<form action="http://<?php echo $this->base_href;?>/ext.php?t=signup" method="post">
		<?php } ?>
		<div class="form_elements">  
		 <label>Kj&oslash;nn</label>
		 	<div><select name="mem_kjonn" id="kjonn">
					<option value="0">Mann</option>
			    <option value="1">Dame</option> 
				</select>
		  </div>
		 </div>
       <div class="form_elements">
	                <label>Din Golf Klub</label>
	                <select name="mem_golfclub" id="golfclub">
			
					<?php 
					
					foreach($newsletters2 as $newsletter2){
						?>
						<option value="<?php echo $newsletter2['golf_id'];?>"><?php echo $newsletter2['namegolf'];?></option>
						<?php
					}
					?>
					
				</select>
				</div>
			<div class="form_elements">
				<label>Navn</label>
				<input type="text" class="text_input" name="first_name" value="<?php echo htmlspecialchars($member_data['first_name']);?>">
			</div>

			<div class="form_elements">
				<label>Etternavn</label>
				<input type="text" class="text_input" name="last_name" value="<?php echo htmlspecialchars($member_data['last_name']);?>">
			</div>

			<div class="form_elements">
				<label>Email</label>
				<input type="text" class="text_input" name="email" value="<?php echo htmlspecialchars($member_data['email']);?>">
			</div>


			<?php
			foreach($member_fields as $member_field){
				?>
				<div class="form_elements">
						<label>
							<?php echo $member_field['field_name'];?>
							<?php if($member_field['required']){ ?>
						</label>
						<?php } ?>
						<input type="text" class="text_input" name="mem_custom_val[<?php echo $member_field['field_name'];?>]" value="<?php echo $member_data['custom'][$member_field['member_field_id']]['value'];?>">
				</div>
				<?php
			}
			if($admin){
			?>
				<div class="form_elements">
					<input type="text" name="mem_custom_new_key" value="">
				</div>
				<div class="form_elements">
					<input type="text" name="mem_custom_new_val" value="">
				</div>

			<?php } ?>
			
	
				<div class="form_elements">
				<input type="submit" name="button" value="Registrer"> <br/>
				</div>
		<?php if($include_form_tag){ ?>
		</form>
		</div>
		<?php } ?>
		
		<?php 
		return ob_get_clean();
	}
	
	
		public function get_form_newclub($db,$include_form_tag = false,$member_id=false,$admin=false){
		$member_data = array();
		if($member_id){
			$member_data = $this->get_member($db,$member_id);
		}
		$campaigns = $this->get_campaigns($db);
		$member_fields = $this->get_member_fields($db);
		$groups = $this->get_groups($db);
		$newsletters2 = $this->get_golfclubs($db);
		ob_start();
		?>
		
		<?php if($include_form_tag){ ?>
		<div id="subscribe_form">
		<form action="http://<?php echo $this->base_href;?>/ext.php?t=signup" method="post">
		<?php } ?>
		<div class="form_elements">  
		 <label>Kj&oslash;nn</label>
		 	<div><select name="mem_kjonn" id="kjonn">
					<option value="0">Mann</option>
			    <option value="1">Dame</option> 
				</select>
		  </div>
		 </div>
       <div class="form_elements">
	                <label>Din Golf Klub</label>
	                <select name="mem_golfclub" id="golfclub">
					
					<?php 
					
					foreach($newsletters2 as $newsletter2){
						?>
						<option value="<?php echo $newsletter2['golf_id'];?>"><?php echo $newsletter2['namegolf'];?></option>
						<?php
					}
					?>
					
				</select>
				</div>
			<div class="form_elements">
				<label>Navn</label>
				<input type="text" class="text_input" name="first_name" value="<?php echo htmlspecialchars($member_data['first_name']);?>">
			</div>

			<div class="form_elements">
				<label>Etternavn</label>
				<input type="text" class="text_input" name="last_name" value="<?php echo htmlspecialchars($member_data['last_name']);?>">
			</div>

			<div class="form_elements">
				<label>Email</label>
				<input type="text" class="text_input" name="email" value="<?php echo htmlspecialchars($member_data['email']);?>">
			</div>


			<?php
			foreach($member_fields as $member_field){
				?>
				<div class="form_elements">
						<label>
							<?php echo $member_field['field_name'];?>
							<?php if($member_field['required']){ ?>
						</label>
						<?php } ?>
						<input type="text" class="text_input" name="mem_custom_val[<?php echo $member_field['field_name'];?>]" value="<?php echo $member_data['custom'][$member_field['member_field_id']]['value'];?>">
				</div>
				<?php
			}
			if($admin){
			?>
				<div class="form_elements">
					<input type="text" name="mem_custom_new_key" value="">
				</div>
				<div class="form_elements">
					<input type="text" name="mem_custom_new_val" value="">
				</div>

			<?php } ?>
			
	
				<div class="form_elements">
				<input type="submit" name="button" value="Registrer"> <br/>
				</div>
		<?php if($include_form_tag){ ?>
		</form>
		</div>
		<?php } ?>
		
		<?php 
		return ob_get_clean();
	}
	public function check_bounces($db){
		
		$email_address = $this->settings['bounce_email']; // not used
		$email_username = $this->settings['bounce_username'];
		$email_password = $this->settings['bounce_password'];
		$email_host = $this->settings['bounce_host'];
		$email_port = ($this->settings['bounce_port']) ? $this->settings['bounce_port'] : 110; // pop3
		
		$mbox = imap_open ('{'.$email_host.':'.$email_port.'/pop3/novalidate-cert}INBOX', $email_username, $email_password) or die(imap_last_error());
		if(!$mbox){
			// send email letting them know bounce checking failed?
			// meh. later.
			echo 'Failed to connect';
		}else{
			$MC = imap_check($mbox);
			$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
			foreach ($result as $overview) {
				$this_subject = (string)$overview->subject;
    			//echo "#{$overview->msgno} ({$overview->date}) - From: {$overview->from} <br> {$this_subject} <br>\n";
			    $tmp_file = tempnam('/tmp/','newsletter_bounce');
			    // TODO - tmp files for windows hosting.
			    imap_savebody  ($mbox, $tmp_file, $overview->msgno);
			    $body = file_get_contents($tmp_file);
				if(preg_match('/Message-ID:\s*<?Newsletter-(\d+)-(\d+)-([A-Fa-f0-9]{32})/imsU',$body,$matches)){
					// we have a newsletter message id, check the hash and mark a bounce.
					//"message_id" => "Newsletter-$send_id-$member_id-".md5("bounce check for $member_id in send $send_id"),
					$send_id = (int)$matches[1];
					$member_id = (int)$matches[2];
					$provided_hash = trim($matches[3]);
					$real_hash = md5("bounce check for $member_id in send $send_id");
					if($provided_hash == $real_hash){
						$golfclub_id=(int)$_SESSION['logoclub'];
						
						$sql = "UPDATE "._DB_PREFIX."newsletter_member SET `status` = 4, bounce_time = '".time()."' WHERE golf_id=$golfclub_id AND `member_id` = '".$member_id."' AND send_id = '".$send_id."' AND `status` = 3 LIMIT 1";
						$res = query($sql,$db);
						imap_delete($mbox, $overview->msgno);
					}else{
						// bad hash, report.
					}
				}
				unlink($tmp_file);
			}
			imap_expunge($mbox);
			imap_close($mbox);
		}

	}
	
	
	public function get_newsletter_contents($db=false,$newsletter_id){
		if(!$db)$db = db_connect();
		$golfclub_id=(int)$_SESSION['logoclub'];
		
		$sql = "SELECT newsletter_content_id FROM "._DB_PREFIX."newsletter_content WHERE golf_id=$golfclub_id AND newsletter_id = '".(int)$newsletter_id."' ORDER BY `position`";
		$content = array();
		foreach(qa($sql,$db) as $c){
			$content[$c['newsletter_content_id']] = $this->get_newsletter_content($db,$c['newsletter_content_id']);
		}
		return $content;
	}
	public function get_newsletter_content($db=false,$newsletter_content_id){
		if(!$db)$db = db_connect();
		
		$golfclub_id=(int)$_SESSION['logoclub'];
		
		$sql = "SELECT * FROM "._DB_PREFIX."newsletter_content WHERE golf_id=$golfclub_id AND newsletter_content_id = '".(int)$newsletter_content_id."'";
		$res = array_shift(qa($sql,$db));
		$folder = _IMAGES_DIR."$golfclub_id/".'newsletter-'.$res['newsletter_id'].'/';
		if(is_file($folder.$newsletter_content_id.'-thumb.jpg')){
			$res['image_thumb'] = $folder.$newsletter_content_id.'-thumb.jpg';
		}
		if(is_file($folder.$newsletter_content_id.'.jpg')){
			$res['image_main'] = $folder.$newsletter_content_id.'.jpg';
		}
		return $res; 
	}
	
	public function get_logo($db=false,$id){
		$sql = "SELECT * FROM `"._DB_PREFIX."loginadmin` WHERE golf_id = '".mysql_real_escape_string($id)."'";
		$res = array_shift(qa($sql,$db));
	  //$folder = _IMAGES_DIR.'newsletter-'.$res['namelogo'].'/';
	  
		return 'logo-borre.png';
	}
}

Open in new window

0
team2005Author Commented:
Hi!

Cant find the code where it do the replace in the template
0
Julian HansenCommented:
Look at lines 1118 1142. The $replace array contains key value pairs of keys and replacements. Lines 1140 to 1142 contain a look through this array calling preg_replace on the values in the array.

			$replace = array(
				"email_subject" => $newsletter_data['subject'],
				"from_name" => $newsletter_data['from_name'],
				"Fra_Epost" => $newsletter_data['Fra_Epost'],
				"to_email" => $member_data['email'],
				"sent_date" => date("jS M, Y"),
				"sent_month" => date("M Y"),
				"unsubscribe_url" => 'http://'.$this->base_href.'/ext.php?t=unsub&sid='.$send_id.'&mid='.$member_data['member_id'].'&hash='.md5("Unsub ".$member_data['member_id']."from $send_id").'',
				"view_online"=>'http://'.$this->base_href.'/ext.php?t=view&id='.$send_data['newsletter_id'].'&sid='.$send_id.'&mid='.$member_data['member_id'].'&hash='.md5("view link ".$member_data['member_id']."from $send_id").'',
				"link_account" => $this->settings['url_update'],
				"member_id" => $member_data['member_id'],
				"send_id" => $send_id,
				"MEMBER_HASH" => md5("Member Hash for $send_id with member_id $member_id"),
				"first_name"=>$member_data['first_name'],
				"last_name"=>$member_data['last_name'],
				"email"=>$member_data['email'],
				
			);
			
			echo $replace['view_online'];
			
			
			foreach($replace as $key=>$val){
				$newsletter_html = preg_replace('/\{'.strtoupper(preg_quote($key,'/')).'\}/',$val,$newsletter_html);
			}
			

Open in new window

0
team2005Author Commented:
Hi!

Cant find where it replace {FIRST_NAME} with value from database ?
0
Julian HansenCommented:
Take this step by step
1. Look at lines 1118 to 1135. This is an array of key value pairs
2. Look specifically at line 1131
"first_name"=>$member_data['first_name'],

Open in new window

3. Look at lines 1140-1142
// FOR EACH ITEM IN THE $replace ARRAY
foreach($replace as $key=>$val){
  // CALL preg_replace ON THE UPPER CASE VALUE OF THE KEY ($key)
  // AND REPLACE MATCHING TAGS WITH $val
  $newsletter_html = preg_replace('/\{'.strtoupper(preg_quote($key,'/')).'\}/',$val,$newsletter_html);
}

Open in new window

For more information on the preg_replace function see this link http://php.net/manual/en/function.preg-replace.php
For more information on the strtoupper function see this link http://php.net/manual/en/function.strtoupper.php
For more information on the preg_quote function see this link http://php.net/manual/en/function.preg-quote.php

What the loop is doing is a regular expression search and replace on each of the keys of the array with the values of the array.

It does the match on this pattern
/\{'.strtoupper(preg_quote($key,'/')).'\}/

Open in new window

Which translates to a curly brace '{' followed by the upper case version of the key of the current element in the array - passed through preg_quote to escape any regular expression characters - followed by a closing curly brace '}'

If we look at line 1131 again
"first_name"=>$member_data['first_name'],

Open in new window

The key is first_name and the value is whatever is stored in $member_data['first_name'].

As per the discussion above first_name is translated into FIRST_NAME by the strtoupper function calls and then placed inside { } in the pattern to preg_replace effectively resulting in the following
preg_replace('/{FIRST_NAME}/', $member_data['first_name'])

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
team2005Author Commented:
Thank you :)
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.