<?php
require ('authentication_manager.php');
?>
<?php

/* 群发电邮 */
/* 162100源码 - 162100.com */


if (POWER < 4) {
  err('该命令必须以管理员身份登陆！请重登陆。');
}

$_POST['id'] = array_unique((array)$_POST['id']);
//$_POST['id'] = preg_grep('/^\d+$/', $_POST['id']);

if (count($_POST['id']) == 0) {
  err('参数出错！<br />问题分析：<br />1、您可能未点选<br />2、参数传递出错。');
}
if (!@file_exists('inc/function/mail_class.php')) {
  err('邮件发送功能未安装！请联系<a href="http://www.162100.com/" target="_blank">162100.com</a>官方定制。');
}

@ require ('inc/function/filter.php');
$subject = filter1($_POST['subject'], true);
server_sbj($subject); //主题检测
$content = filter2($_POST['content']);
server_chk($content, $web['mess_wordcount']); //内容检测

$now_id = gmdate('YmdHis', time() + (floatval($web['time_pos']) * 3600));

$content = preg_replace('/(<[^>]+)(__temp__\/temp\/\d+-[^\/\"\s]+)/ie', '"".cha_ui("$1")."".cha_up("$2").""', $content);

function cha_ui($src) {
  global $web;
  return preg_replace('/(src="?).*$/i', '$1'.$web['path'].'', $src);
}

function cha_up($file) {
  global $web, $sql, $now_id, $up_arr, $session;
  $filename = urldecode(basename($file));
  $format = strtolower(ltrim(strrchr($filename, '.'), '.'));
  $title = 'mail-'.$now_id.'.'.$format;
  if ($GLOBALS['ofile'] != $filename) {
    if (file_exists('__temp__/temp/'.$filename)) {
      $mime = preg_replace('/^.+--/i', '', $filename);
      $mime = preg_replace('/\.'.$format.'$/i', '', $mime);
      $up_arr['__temp__/temp/'.$filename] = $web['uploadto'] == 1 ? '
      if ($obj_contents = @file_get_contents($k)) {
        @mysql_query(\'REPLACE INTO `'.$sql['pref'].''.$sql['data']['承载上传数据的表名'].'` (`title`,`text`,`type`,`username`,`date`) VALUES ("'.$title.'","\'.addslashes($obj_contents).\'","'.urldecode($mime).'","'.$session[0].'",DATE_ADD(UTC_TIMESTAMP(), INTERVAL '.floatval($web['time_pos']).' HOUR))\', $db);
        unset($obj_contents);
      }
      '.($web['temp'] == 1 ? '@rename($k, \'__temp__/temp/'.$title.'\');' : '@unlink($k);').'' : '@rename($k, \'__temp__/temp/'.$title.'\');';
      $GLOBALS['ofile'] = $filename;

    } else {
      err('转移上传文件出错！上传源文件已经不存在，可能是重复提交造成。');
    }
  } else {
    $GLOBALS['ofile'] = $filename;
  }
  return 'obj.php?title='.$title;

}

$step = 0;
@ require ('inc/function/mail_class.php');
$subject = "=?UTF-8?B?".base64_encode($subject)."?="; //此行解决utf-8编码邮件标题乱码
foreach ($_POST['id'] as $to){
  $send = $smtp -> sendmail($to, $web['sender'], $subject, $content, $web['mailtype']);
  if ($send == 1) {
    $step++;
    echo '<br />邮件['.$to.']发送成功！';
  } else { 
    echo '<br />邮件['.$to.']发送失败！原因：邮箱密码未设置或出错'; 
  }
  @ob_flush();
@flush();
}

if ($step > 0 && count($up_arr) > 0) {
  if (!isset($sql['db_err'])) {
    db_conn();
  }
  if ($sql['db_err'] == '') {
    //print_r($up_arr);die();
    foreach ($up_arr as $k => $s) {
      eval($s);
    }
  } else {
    $err = '由于数据库故障，上传的图片未存入数据库。';
  }
}

del_uploadfiles();
alert('邮件发送成功。'.$err, 'webmaster_central.php?get=bulk_mail');


?>