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

/* 发送短信 */
/* 162100源码 - 162100.com */


if (POWER == 0) {
  err('请<a href="./login.php" target="_blank">登陆</a>。');
}

if (!$_GET['username'] = trim($_GET['username'])) {
  $_GET['username'] = $_POST['username'];
}
if (empty($_GET['username']) || !preg_match('/^([^\x00-\x7f]|\w|\.){3,45}$/', $_GET['username'])) {
  err('提交被拒绝！用户名请用汉字、数字、英文及字符 _ 或 . 组成！长度范围为3-45字符。注：中文占3个字符，其它等占1个。即：纯中文可输入15字，英文或数字或英语符号可输入45字。');
}

@ 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));
$user = $_GET['username'] == 'admin' ? $web['manager'] : $_GET['username'];

$up_arr = array();

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

function cha_up($file) {
  global $web, $sql, $now_id, $up_arr, $user;
  $filename = urldecode(basename($file));
  $format = strtolower(ltrim(strrchr($filename, '.'), '.'));
  $title = 'mess-'.$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).'","'.$user.'",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;

}

//$content = addslashes($content);

//时间|来信人|主题|内容|||已读状态
$line = "".$now_id."|".$session[0]."|".$subject."|".$content."||| \n";

if (!isset($sql['db_err'])) {
  db_conn();
}
if ($sql['db_err'] != '') {
  err($sql['db_err']);
}

$result = @mysql_query('SELECT mess,mess_num,check_reg FROM '.$sql['pref'].''.$sql['data']['承载成员档案的表名'].' WHERE username="'.$user.'"', $db);
if (!$row = @mysql_fetch_assoc($result)) {
  err('数据库无'.$user.'用户或无短信记录。');
} else {
  if ($row['check_reg'] == '1') {
    err('用户'.$user.'尚在注册审核中！');
  }
  if ($row['check_reg'] == '2') {
    err('用户'.$user.'已经被置入黑名单！');
  }
  @mysql_free_result($result);
  $messlist = @array_filter(explode("\n", trim($line.$row['mess'])));
  $messlist = @array_slice($messlist, 0, $web['mess_count']);
  $newline = "".@implode("\n", $messlist)."\n";
  //echo abs($row["mess_num"]); die;
  //mysql_query("ALTER TABLE yzsoumember CHANGE `mess` `mess` TEXT");
  //@mysql_query('ALTER TABLE '.$sql['pref'].''.$sql['data']['承载成员档案的表名'].' MODIFY COLUMN mess TEXT CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL');
  @mysql_query('UPDATE '.$sql['pref'].''.$sql['data']['承载成员档案的表名'].' SET mess="'.addslashes($newline).'",mess_num="'.(abs($row['mess_num'])+1).'/'.count($messlist).'" WHERE username="'.$user.'"', $db);
  if (@mysql_affected_rows() > 0) {
    if (count($up_arr) > 0) {
      //print_r($up_arr);die();
      foreach ($up_arr as $k => $s) {
        eval($s);
      }
    }
    del_uploadfiles();

    if ($_POST['m']==1) {
      err('
<script>
  if (parent.$("submit_safe_in")!=null) parent.$("submit_safe_in").innerHTML="短信发布成功！";
  parent.runReset();
  setTimeout("parent.location.href=\'member.php?get=mess&username='.urlencode($_GET['username']).'\';", 1000);
</script>短信发布成功！', 'ok');
    
    }

    alert('短信发布成功！', 'member.php?get=mess&username='.urlencode($_GET['username']).'');
  }else{
    //echo mysql_errno().':'.mysql_error();
    del_uploadfiles();
    err('短信发布不成功！（错误代码：'.mysql_errno().':'.mysql_error().'）。');
  }
}

@mysql_close();



?>