You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
268 lines
7.7 KiB
268 lines
7.7 KiB
<?php
|
|
/**
|
|
* 邮件统计API
|
|
*/
|
|
|
|
// 设置错误报告
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 0);
|
|
|
|
// 设置JSON响应头
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
// 包含必要的类
|
|
require_once '../../utils/Config.php';
|
|
require_once '../../utils/Logger.php';
|
|
require_once '../../utils/Helper.php';
|
|
require_once '../../utils/Database.php';
|
|
|
|
// 处理请求
|
|
try {
|
|
// 获取请求方法
|
|
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
|
|
|
// 连接数据库
|
|
$db = Database::getInstance();
|
|
|
|
// 只支持GET请求
|
|
if ($method === 'GET') {
|
|
// 获取统计类型
|
|
$type = isset($_GET['type']) ? $_GET['type'] : 'mail';
|
|
|
|
switch ($type) {
|
|
case 'mail':
|
|
// 获取邮件统计
|
|
getMailStats($db);
|
|
break;
|
|
case 'traffic':
|
|
// 获取流量统计
|
|
getTrafficStats($db);
|
|
break;
|
|
case 'send':
|
|
// 获取发送统计
|
|
getSendStats($db);
|
|
break;
|
|
default:
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '不支持的统计类型' . $type
|
|
]);
|
|
break;
|
|
}
|
|
} else {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '不支持的请求方法' . $method
|
|
]);
|
|
}
|
|
} catch (Exception $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '服务器内部错误: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取邮件统计
|
|
* @param Database $db 数据库实例
|
|
*/
|
|
function getMailStats($db) {
|
|
// 获取请求参数
|
|
$username = isset($_GET['username']) ? $_GET['username'] : '';
|
|
$startDate = isset($_GET['startDate']) ? $_GET['startDate'] : '';
|
|
$endDate = isset($_GET['endDate']) ? $_GET['endDate'] : '';
|
|
|
|
// 验证必要参数
|
|
if (empty($username)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '缺少用户名参数'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// 获取用户邮箱
|
|
$user = $db->fetchOne("SELECT email FROM user WHERE username = ?", [$username]);
|
|
if (!$user) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '用户不存在'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// 构建查询条件
|
|
$where = "WHERE (rcpt_to = ? OR `from` = ?)";
|
|
$params = [$user['email'], $user['email']];
|
|
|
|
if (!empty($startDate)) {
|
|
$where .= " AND date >= ?";
|
|
$params[] = $startDate . " 00:00:00";
|
|
}
|
|
|
|
if (!empty($endDate)) {
|
|
$where .= " AND date <= ?";
|
|
$params[] = $endDate . " 23:59:59";
|
|
}
|
|
|
|
// 获取邮件类型统计
|
|
$typeStats = [
|
|
'inbox' => $db->fetchOne("SELECT COUNT(*) as count FROM email $where AND folder = 'inbox' AND is_deleted = 0", $params)['count'],
|
|
'sent' => $db->fetchOne("SELECT COUNT(*) as count FROM email $where AND folder = 'sent' AND is_deleted = 0", $params)['count'],
|
|
'draft' => $db->fetchOne("SELECT COUNT(*) as count FROM email $where AND folder = 'draft' AND is_deleted = 0", $params)['count'],
|
|
'trash' => $db->fetchOne("SELECT COUNT(*) as count FROM email $where AND folder = 'trash' AND is_deleted = 1", $params)['count'],
|
|
'total' => $db->fetchOne("SELECT COUNT(*) as count FROM email $where", $params)['count']
|
|
];
|
|
|
|
// 获取阅读状态统计
|
|
$readStats = [
|
|
'read' => $db->fetchOne("SELECT COUNT(*) as count FROM email WHERE rcpt_to = ? AND is_read = 1 AND is_deleted = 0", [$user['email']])['count'],
|
|
'unread' => $db->fetchOne("SELECT COUNT(*) as count FROM email WHERE rcpt_to = ? AND is_read = 0 AND is_deleted = 0", [$user['email']])['count']
|
|
];
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'data' => [
|
|
'typeStats' => $typeStats,
|
|
'readStats' => $readStats
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取流量统计
|
|
* @param Database $db 数据库实例
|
|
*/
|
|
function getTrafficStats($db) {
|
|
// 获取请求参数
|
|
$username = isset($_GET['username']) ? $_GET['username'] : '';
|
|
$startDate = isset($_GET['startDate']) ? $_GET['startDate'] : '';
|
|
$endDate = isset($_GET['endDate']) ? $_GET['endDate'] : '';
|
|
|
|
// 验证必要参数
|
|
if (empty($username)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '缺少用户名参数'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// 获取用户邮箱
|
|
$user = $db->fetchOne("SELECT email FROM user WHERE username = ?", [$username]);
|
|
if (!$user) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '用户不存在'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// 构建查询条件
|
|
$where = "WHERE (rcpt_to = ? OR `from` = ?)";
|
|
$params = [$user['email'], $user['email']];
|
|
|
|
if (!empty($startDate)) {
|
|
$where .= " AND date >= ?";
|
|
$params[] = $startDate . " 00:00:00";
|
|
}
|
|
|
|
if (!empty($endDate)) {
|
|
$where .= " AND date <= ?";
|
|
$params[] = $endDate . " 23:59:59";
|
|
}
|
|
|
|
// 获取邮件大小统计
|
|
$sizeStats = $db->fetchOne("SELECT SUM(length) as total_size FROM email $where", $params);
|
|
|
|
// 获取邮箱配额
|
|
$quota = $db->fetchOne("SELECT quota_bytes, used_bytes FROM mailboxes WHERE username = ?", [$username]);
|
|
if (!$quota) {
|
|
$quota = [
|
|
'quota_bytes' => 1073741824, // 1GB默认配额
|
|
'used_bytes' => 0
|
|
];
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'data' => [
|
|
'totalSize' => $sizeStats['total_size'] ?: 0,
|
|
'quota' => $quota['quota_bytes'],
|
|
'used' => $quota['used_bytes'],
|
|
'remaining' => $quota['quota_bytes'] - $quota['used_bytes']
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取发送统计
|
|
* @param Database $db 数据库实例
|
|
*/
|
|
function getSendStats($db) {
|
|
// 获取请求参数
|
|
$username = isset($_GET['username']) ? $_GET['username'] : '';
|
|
$startDate = isset($_GET['startDate']) ? $_GET['startDate'] : '';
|
|
$endDate = isset($_GET['endDate']) ? $_GET['endDate'] : '';
|
|
|
|
// 验证必要参数
|
|
if (empty($username)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '缺少用户名参数'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// 获取用户邮箱
|
|
$user = $db->fetchOne("SELECT email FROM user WHERE username = ?", [$username]);
|
|
if (!$user) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '用户不存在'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// 构建查询条件
|
|
$where = "WHERE `from` = ? AND folder = 'sent' AND is_deleted = 0";
|
|
$params = [$user['email']];
|
|
|
|
if (!empty($startDate)) {
|
|
$where .= " AND date >= ?";
|
|
$params[] = $startDate . " 00:00:00";
|
|
}
|
|
|
|
if (!empty($endDate)) {
|
|
$where .= " AND date <= ?";
|
|
$params[] = $endDate . " 23:59:59";
|
|
}
|
|
|
|
// 获取发送总量
|
|
$totalSent = $db->fetchOne("SELECT COUNT(*) as count FROM email $where", $params)['count'];
|
|
|
|
// 获取每日发送统计
|
|
$dailyStats = [];
|
|
|
|
// 构建每日统计查询
|
|
$dailyQuery = "SELECT DATE(date) as date, COUNT(*) as count FROM email $where GROUP BY DATE(date) ORDER BY date ASC";
|
|
$dailyResult = $db->fetchAll($dailyQuery, $params);
|
|
|
|
if ($dailyResult) {
|
|
foreach ($dailyResult as $row) {
|
|
$dailyStats[] = [
|
|
'date' => $row['date'],
|
|
'count' => $row['count']
|
|
];
|
|
}
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'data' => [
|
|
'totalSent' => $totalSent,
|
|
'dailyStats' => $dailyStats
|
|
]
|
|
]);
|
|
}
|