<?php
  
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('C:/wamp/www/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  // error checking
  if (!@$GLOBALS['NEWSLETTER_BUILDER_PLUGIN']) { die("You must activate the newsletter plugin to see this page."); }

  // load newsletter settings
  $newsletterSettings  = mysql_get('_nlb_settings', 1);

  // dispatch form actions
  list($errorsAndAlerts, $isConfirmSubscribe, $isConfirmUnsubscribe) = nlb_manage_dispatch();

  if(@$_REQUEST['saveUser']){
        // error checking
    if     (!@$_REQUEST['e'])              { $errorsAndAlerts .= "No email address specified!<br/>\n"; }
    elseif (!isValidEmail($_REQUEST['e'])) { $errorsAndAlerts .= "Invalid email specified, email must be in the format: user@example.com<br/>\n"; }
    if(!@$_REQUEST['first_name'])          { $errorsAndAlerts .= "You must enter your full first name<br/>\n";}

    //
    if (!$errorsAndAlerts) {

      // convert "Display Name" <local-part@domain> to local-part@domain
      $_REQUEST['e'] = array_value(isValidEmail($_REQUEST['e']), 0, 0);
      $subscriber    = mysql_get('_nlb_subscribers', null, array('email' => $_REQUEST['e']));

      // create subscriber if they don't already exist
      if (!$subscriber) {
        $colsToValues = array(
          'email'      => $_REQUEST['e'],
          'first_name' => $_REQUEST['first_name'],
          'authkey'    => _nlb_generateAuthKey()
        );
        $recordNum    = mysql_insert('_nlb_subscribers', $colsToValues, true);
        $subscriber   = mysql_get('_nlb_subscribers', $recordNum);
      }

      // send message & show alert
      $errorsAndAlerts = '';
      if ($subscriber['confirmed']) {
        $errorsAndAlerts = "You're already subscribed to this newsletter.<br/>\n";
      }
      else {
        nlb_log('0', $subscriber['num'], '1');

        $to                 = $subscriber['email'];
        $newsletterSettings = mysql_get('_nlb_settings', 1);
        $mailErrors         = nlb_sendMessage($to, $newsletterSettings['confirm_subject'], $newsletterSettings['confirm_html'], $subscriber);
        if ($mailErrors) { die("Mail Error: $mailErrors"); }

        // send alert
        $errorsAndAlerts .= "Thanks, we've emailed you at " .htmlencode($to). ", to confirm your subscription just check your mail and click the confirmation link.<br/>\n";
        $errorsAndAlerts .= "If you don't receive an email from us within a few minutes check your spam filter for messages from " .htmlencode($newsletterSettings['from_email']). "<br/>\n";
        $_REQUEST = array(); // clear form
      }
    }
  }

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <style type="text/css">
    body          { font-family: arial; }
    .instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
  </style>
 </head>
<body>


  <!-- INSTRUCTIONS -->
    <div class="instructions">
      <b>Sample Subscriber Script - Instructions:</b>
      <ol>
        <?php /*><li style="color: red; font-weight: bold">Rename this file to have a .php extension!</li><x */ ?>
        <li>Newsletter subscribers will use this script to subscribe, confirm subscriptions, and unsubscribe</li>
        <li>Modify the HTML and design of this page as needed, then update <a href="http://127.0.0.1/cmsAdmin/admin.php?menu=_nlb_settings">Manage Url</a> under Newsletter Settings.</li>
      </ol>
    </div>
  <!-- /INSTRUCTIONS -->

<?php if ($isConfirmSubscribe): ?>
<?php /*** START: CONFIRM SUBSCRIBE - Shown when a user clicks the "Confirm Subscription" link in their email ***/ ?>

  <h1>Confirm Subscription</h1>
  <div style="color: #C00; font-weight: bold; font-size: 14px;"><?php echo $errorsAndAlerts; ?><br/></div>


<?php /*** END: CONFIRM SUBSCRIBE ***/ ?>
<?php elseif ($isConfirmUnsubscribe): // if this is a "Confirm Unsubscribe" link from an email ?>
<?php /*** START: CONFIRM UNSUBSCRIBE - Shown when a user clicks the "Unsubscribe" link in their email ***/ ?>

  <h1>Confirm Unsubscribe</h1>
  <div style="color: #C00; font-weight: bold; font-size: 14px;"><?php echo $errorsAndAlerts; ?><br/></div>



<?php /*** END: CONFIRM UNSUBSCRIBE ***/ ?>
<?php else: ?>
<?php /*** START: SUBSCRIBE FORM - Shown when a user uses the web form to signup ***/ ?>

  <h1>Subscribe to newsletter</h1>

  <?php if (@$errorsAndAlerts): ?>
    <div style="color: #C00; font-weight: bold; font-size: 14px;"><?php echo $errorsAndAlerts; ?><br/></div>
  <?php endif ?>

  <form method="post" action="#">
    <input type="hidden" name="saveUser" value="1" />
    Email: <input type="text" name="e" value="<?php echo htmlencode(@$_REQUEST['e']) ?>" size="50"/>
    <input type="text" name="first_name" value="<?php echo htmlencode(@$_REQUEST['first_name']) ?>" size="30"/>
    <input type="submit" name="subscribe" value="Subscribe" /><br/>
  </form>

<?php /*** END: SUBSCRIBE FORM ***/ ?>
<?php endif ?>

</body>
</html>