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

@set_time_limit( 600 );
$perpage = 100;
$totalselected = $db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_nsnml_users WHERE active='1' AND lid='$lid'"));
if (!isset($min)) {
  $min=0;
  $pagesint = ($totalselected / $perpage);
  $pages = ceil($pagesint);
  $pagetitle = "- "._ML_ADMIN." ".$ml_config['version_number'];
  incoh();
  title(_ML_ADMIN." ".$ml_config['version_number']);
  ML_Admin();
  br('1');
  Max_OpenTable();
  $query = "INSERT ".$prefix."_nsnml_issues VALUES (NULL, '$lid', '".addslashes($sub)."', '".addslashes($text)."', '".addslashes($htmltext)."', '".time()."')";
  if(!$db->sql_query($query)) {
    $result = $db->sql_error($query);
    echo "<center><b>".$result['code'].": ".$result['message']."</b></center>\n";
    Max_CloseTable();
    incof();
    return;
  }
  $nid = $db->sql_nextid();
  echo _ML_SENDISSUE01."<br />\n";
  echo _ML_SENDISSUE02."<br />\n";
  echo _ML_SENDISSUE03."<br />\n";
  echo _ML_SENDISSUE05."$pages"._ML_SENDISSUE06."<br />\n";
  echo "<br />\n";
  echo "<form method='post' action='".$admin_file.".php'>\n";
  echo "<input type='hidden' name='op' value='MLSend'>\n";
  echo "<input type='hidden' name='min' value='$min'>\n";
  echo "<input type='hidden' name='nid' value='$nid'>\n";
  echo "<input type='hidden' name='lid' value='$lid'>\n";
  echo "<input type='submit' value=\""._ML_LETSGETSTART."\">\n";
  echo "</form>\n";
  Max_CloseTable();
  incof();
} else if($min < $totalselected) {
  $max=$min+$perpage;
  $pagesint = ($totalselected / $perpage);
  $pages = ceil($pagesint);
  $currentpage = ($max / $perpage);
  $pagetitle = " - "._ML_ADMIN." ".$ml_config['version_number'];
  incoh();
  title(_ML_ADMIN." ".$ml_config['version_number']);
  ML_Admin();
  br('1');
  Max_OpenTable();
  $issue = $db->sql_fetchrow($db->sql_query("SELECT * FROM ".$prefix."_nsnml_issues WHERE nid='$nid'"));
  $result = $db->sql_query("SELECT * FROM ".$prefix."_nsnml_users WHERE active='1' AND lid='$lid' LIMIT $min, $perpage");
  while ($user_info = $db->sql_fetchrow($result)) {
    if ($user_info['html'] == '0') {
      mail($user_info['email'], stripslashes($issue['subject']), stripslashes(strip_tags($issue['text_plain'])), "From: $adminmail");
    } else {
      if(strlen($issue['text_html']) > 1) {
        mail($user_info['email'], stripslashes($issue['subject']), stripslashes($issue['text_html']), "From: $adminmail\r\nContent-Type: text/html; charset=iso-8859-1");
      } else {
        mail($user_info['email'], stripslashes($issue['subject']), stripslashes(strip_tags($issue['text_plain'])), "From: $adminmail");
      }
    }
    $count++;
    $query = "INSERT ".$prefix."_nsnml_tracked SET mid='".$user_info['mid']."', nid='$nid', lid='$lid', sent='".time()."'";
    if(!$db->sql_query($query)) {
       $result = $db->sql_error($query);
       echo "<center><b>".$result['code'].": ".$result['message']."</b></center>\n";
       Max_CloseTable();
       incof();
       return;
    }
  }
  echo "<META HTTP-EQUIV=\"refresh\" content=\"1;URL=".$admin_file.".php?op=MLSend&min=$max&nid=$nid&lid=$lid\">";
  echo "<b>"._ML_SECTION." $currentpage "._ML_OF." $pages "._ML_COMPLETED."</b><br />\n";
  if($currentpage==$pages) {
  } else {
    echo "<b>"._ML_SECTION." ".($currentpage+1)." "._ML_WILLSTART."</b><br />\n";
  }
  Max_CloseTable();
  incof();
} else {
  $pagetitle = "- "._ML_ADMIN." ".$ml_config['version_number'];
  incoh();
  title(_ML_ADMIN." ".$ml_config['version_number']);
  ML_Admin();
  echo "<br />\n";
  Max_OpenTable();
  echo "<center><b>"._ML_SENT."</b><br />\n";
  Max_CloseTable();
  incof();
}

?>