<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/********************************************************/
/* NSN Mailing List                                     */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright © 2000-2005 by NukeScripts Network         */
/********************************************************/
defined('MAXIMUS') or Header("Location: index.php");
$pagetitle = " - "._ML_TITLE." ".$ml_config['version_number'];
incoh();
title(_ML_TITLE." ".$ml_config['version_number']);
Max_OpenTable();
if (!preg_match("/@/", $email)) {
  echo "<center><b>"._ML_ERROR_INVALID."</b></center><br />";
  echo "<center>"._GOBACK."</center>";
} elseif($sub == 'sub') {
  $mid = $db->sql_numrows($db->sql_query("SELECT * FROM `".$prefix."_nsnml_users` WHERE `email`='$email' AND `lid`='$lid'"));
  if($mid > 0) {
    echo "<center><b>"._ML_ERROR_ALREADY."</b></center><br />";
    echo "<center>"._GOBACK."</center>";
  } else {
    srand ((double)microtime()*1000000);
    $mycode = rand();
    $joined = time();
    $query = "INSERT INTO `".$prefix."_nsnml_users` VALUES (NULL, '$lid', '$email', '0', '$type', '$mycode', '$joined')";
    if(!$db->sql_query($query)) {
      $result = $db->sql_error($query);
      echo "<center><b>".$result['code'].": ".$result['message']."</b></center>\n";
    } else {
      $buildlink = "$nukeurl/modules.php?name=$module_name&op=MLActivate&email=$email&lid=$lid&check=$mycode";
      mail($email, _ML_SUBSCRIPTION, _ML_CONFIGTEXT."\n\n$buildlink", "From: $adminmail");
      echo "<center><b>"._ML_CONFIRSENT."</b></center><br />";
      echo "<center>"._GOBACK."</center>";
    }
  }
} elseif($sub == 'unsub') {
  $mid = $db->sql_numrows($db->sql_query("SELECT * FROM `".$prefix."_nsnml_users` WHERE `email`='$email' AND `lid`='$lid'"));
  if($mid < 1) {
    echo "<center><b>"._ML_ERROR_NOTSUB."</b></center><br />";
    echo "<center>"._GOBACK."</center>";
  } else {
    list($mid) = $db->sql_fetchrow($db->sql_query("SELECT `mid` FROM `".$prefix."_nsnml_users` WHERE `email`='$email' AND `lid`='$lid'"));
    $query1 = $db->sql_query("DELETE FROM `".$prefix."_nsnml_users` WHERE `email`='$email' AND `lid`='$lid'");
    $query2 = $db->sql_query("DELETE FROM `".$prefix."_nsnml_tracked` WHERE `mid`='$mid' AND `lid`='$lid'");
    if(!$query1 || !$query2) {
      $result1 = $db->sql_error($query1);
      echo "<center><b>".$result1['code'].": ".$result1['message']."</b></center>\n";
      $result2 = $db->sql_error($query2);
      echo "<center><b>".$result2['code'].": ".$result2['message']."</b></center>\n";
    } else {
      $db->sql_query("OPTIMIZE TABLE `".$prefix."_nsnml_users`");
      $db->sql_query("OPTIMIZE TABLE `".$prefix."_nsnml_tracked`");
      echo "<center><b>"._ML_SUBEND."</b></center><br />";
      echo "<center>"._GOBACK."</center>";
    }
  }
}
Max_CloseTable();
incof();