feat(应用管理): 实现应用审核状态和日志系统
- 在app_store.sql中添加应用审核状态字段 - 重构日志系统到单独文件并实现日志分级 - 改进开发者注册和上传应用的错误处理和日志记录 - 为应用详情页添加评价分页加载功能 - 更新SMTP配置并增强邮件发送调试能力
This commit is contained in:
119
app.php
119
app.php
@@ -28,6 +28,35 @@ if (!$app) {
|
||||
die("<h1>错误:应用不存在</h1><p>找不到ID为 $appId 的应用。请检查ID是否正确。</p>");
|
||||
}
|
||||
|
||||
// 处理评价加载请求
|
||||
if (isset($_GET['action']) && $_GET['action'] === 'load_reviews') {
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
while ($review = $resultReviews->fetch_assoc()) {
|
||||
?>
|
||||
<div class="card mb-3 blur-bg">
|
||||
<div class="card-body">
|
||||
<?php
|
||||
$rating = $review['rating'] !== null ? $review['rating'] : 0;
|
||||
echo '<p class="card-text">评分: ';
|
||||
for ($i = 1; $i <= 5; $i++) {
|
||||
if ($i <= floor($rating)) {
|
||||
echo '<span class="fas fa-star text-warning"></span>';
|
||||
} elseif ($i - $rating <= 0.5) {
|
||||
echo '<span class="fas fa-star-half-alt text-warning"></span>';
|
||||
} else {
|
||||
echo '<span class="far fa-star text-warning"></span>';
|
||||
}
|
||||
}
|
||||
echo '</p>';
|
||||
?>
|
||||
<p class="card-text"><small class="text-muted">评价时间: <?php echo $review['created_at']; ?></small></p>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
// 获取App版本信息
|
||||
$sqlVersions = "SELECT * FROM app_versions WHERE app_id = $appId ORDER BY created_at DESC";
|
||||
$resultVersions = $conn->query($sqlVersions);
|
||||
@@ -36,8 +65,19 @@ $resultVersions = $conn->query($sqlVersions);
|
||||
$sqlImages = "SELECT * FROM app_images WHERE app_id = $appId";
|
||||
$resultImages = $conn->query($sqlImages);
|
||||
|
||||
// 获取评价总数
|
||||
$sqlReviewCount = "SELECT COUNT(*) as total FROM reviews WHERE app_id = $appId";
|
||||
$resultReviewCount = $conn->query($sqlReviewCount);
|
||||
$reviewCount = $resultReviewCount->fetch_assoc()['total'];
|
||||
|
||||
// 分页参数
|
||||
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
||||
$limit = 10;
|
||||
$offset = ($page - 1) * $limit;
|
||||
$hasMore = ($page * $limit) < $reviewCount;
|
||||
|
||||
// 获取评价信息
|
||||
$sqlReviews = "SELECT * FROM reviews WHERE app_id = $appId ORDER BY created_at DESC";
|
||||
$sqlReviews = "SELECT * FROM reviews WHERE app_id = $appId ORDER BY created_at DESC LIMIT $limit OFFSET $offset";
|
||||
$resultReviews = $conn->query($sqlReviews);
|
||||
|
||||
// 获取评分分布
|
||||
@@ -188,33 +228,72 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['rating'])) {
|
||||
<div class="row mt-4">
|
||||
<div class="col-md-6">
|
||||
<h2>评价</h2>
|
||||
<?php while ($review = $resultReviews->fetch_assoc()): ?>
|
||||
<div class="card mb-3 blur-bg">
|
||||
<div class="card-body">
|
||||
<?php
|
||||
$rating = $review['rating'] !== null ? $review['rating'] : 0;
|
||||
echo '<p class="card-text">评分: ';
|
||||
for ($i = 1; $i <= 5; $i++) {
|
||||
if ($i <= floor($rating)) {
|
||||
echo '<span class="fas fa-star text-warning"></span>';
|
||||
} elseif ($i - $rating <= 0.5) {
|
||||
echo '<span class="fas fa-star-half-alt text-warning"></span>';
|
||||
} else {
|
||||
echo '<span class="far fa-star text-warning"></span>';
|
||||
<div id="reviews-container">
|
||||
<?php while ($review = $resultReviews->fetch_assoc()): ?>
|
||||
<div class="card mb-3 blur-bg">
|
||||
<div class="card-body">
|
||||
<?php
|
||||
$rating = $review['rating'] !== null ? $review['rating'] : 0;
|
||||
echo '<p class="card-text">评分: ';
|
||||
for ($i = 1; $i <= 5; $i++) {
|
||||
if ($i <= floor($rating)) {
|
||||
echo '<span class="fas fa-star text-warning"></span>';
|
||||
} elseif ($i - $rating <= 0.5) {
|
||||
echo '<span class="fas fa-star-half-alt text-warning"></span>';
|
||||
} else {
|
||||
echo '<span class="far fa-star text-warning"></span>';
|
||||
}
|
||||
}
|
||||
}
|
||||
echo '</p>';
|
||||
?>
|
||||
<p class="card-text"><small class="text-muted">评价时间: <?php echo $review['created_at']; ?></small></p>
|
||||
echo '</p>';
|
||||
?>
|
||||
<p class="card-text"><small class="text-muted">评价时间: <?php echo $review['created_at']; ?></small></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php endwhile; ?>
|
||||
<?php endwhile; ?>
|
||||
</div>
|
||||
<?php if ($hasMore): ?>
|
||||
<button id="load-more" class="btn btn-secondary" data-page="<?php echo $page + 1; ?>">加载更多</button>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<h2>评分分布</h2>
|
||||
<div id="ratingChartSkeleton" class="skeleton-chart"></div>
|
||||
<canvas id="ratingChart" width="400" height="200"></canvas>
|
||||
<script>
|
||||
// 加载更多评价功能
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const loadMoreBtn = document.getElementById('load-more');
|
||||
if (loadMoreBtn) {
|
||||
loadMoreBtn.addEventListener('click', function() {
|
||||
const button = this;
|
||||
const page = button.getAttribute('data-page');
|
||||
const appId = <?php echo $appId; ?>;
|
||||
|
||||
button.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 加载中...';
|
||||
button.disabled = true;
|
||||
|
||||
fetch(`app.php?id=${appId}&page=${page}&action=load_reviews`)
|
||||
.then(response => response.text())
|
||||
.then(html => {
|
||||
if (html.trim() === '') {
|
||||
button.style.display = 'none';
|
||||
return;
|
||||
}
|
||||
document.getElementById('reviews-container').insertAdjacentHTML('beforeend', html);
|
||||
button.setAttribute('data-page', parseInt(page) + 1);
|
||||
button.innerHTML = '加载更多';
|
||||
button.disabled = false;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('加载评价失败:', error);
|
||||
button.innerHTML = '加载更多';
|
||||
button.disabled = false;
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 评分图表
|
||||
const ctx = document.getElementById('ratingChart').getContext('2d');
|
||||
new Chart(ctx).Bar({
|
||||
labels: ['5星', '4星', '3星', '2星', '1星'],
|
||||
|
||||
@@ -14,9 +14,12 @@ CREATE TABLE IF NOT EXISTS apps (
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
version VARCHAR(20) NOT NULL,
|
||||
changelog TEXT NOT NULL,
|
||||
file_path VARCHAR(255) NOT NULL
|
||||
file_path VARCHAR(255) NOT NULL,
|
||||
status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending'
|
||||
);
|
||||
|
||||
-- 确保状态列存在(用于现有数据库)
|
||||
|
||||
-- 创建APP版本表
|
||||
CREATE TABLE IF NOT EXISTS app_versions (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
|
||||
23
config.php
23
config.php
@@ -10,11 +10,12 @@ define('APP_STORE_NAME', 'LeonAPP');
|
||||
|
||||
// SMTP邮件配置
|
||||
define('SMTP_HOST', 'smtp.163.com');
|
||||
define('SMTP_PORT', 25);
|
||||
define('SMTP_USER', 'leonmmcoset@qq.com');
|
||||
define('SMTP_PASSWORD', 'CXaWtRdekFAabUWZ');
|
||||
define('SMTP_FROM', 'leonmmcoset@qq.com');
|
||||
define('SMTP_FROM_NAME', 'LeonAPP 验证系统');
|
||||
define('SMTP_PORT', 25); // 163邮箱推荐使用587端口
|
||||
define('SMTP_ENCRYPTION', 'tls'); // 启用TLS加密
|
||||
define('SMTP_USERNAME', 'leonmmcoset@163.com'); // 使用完整邮箱地址作为用户名
|
||||
define('SMTP_PASSWORD', 'YFgYVFn6ZaVY9qiV');
|
||||
define('SMTP_FROM_EMAIL', 'leonmmcoset@163.com');
|
||||
define('SMTP_FROM_NAME', 'leonmmcoset@163.com');
|
||||
|
||||
// 管理员账号
|
||||
define('ADMIN_USERNAME', 'Admin');
|
||||
@@ -33,15 +34,5 @@ $conn->set_charset('utf8mb4');
|
||||
date_default_timezone_set('Asia/Shanghai');
|
||||
|
||||
// 错误日志记录函数
|
||||
function log_error($message, $file = '', $line = '') {
|
||||
$log_entry = date('[Y-m-d H:i:s]') . ' Error: ' . $message;
|
||||
if (!empty($file)) {
|
||||
$log_entry .= ' in ' . $file;
|
||||
}
|
||||
if (!empty($line)) {
|
||||
$log_entry .= ' on line ' . $line;
|
||||
}
|
||||
$log_entry .= "\n";
|
||||
file_put_contents('d:\\app2\\logs\\error.log', $log_entry, FILE_APPEND);
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,8 +1,25 @@
|
||||
<?php
|
||||
// Define SMTP constants if not already defined
|
||||
if (!defined('SMTP_USERNAME')) define('SMTP_USERNAME', '');
|
||||
if (!defined('SMTP_ENCRYPTION')) define('SMTP_ENCRYPTION', 'tls');
|
||||
if (!defined('SMTP_FROM_EMAIL')) define('SMTP_FROM_EMAIL', 'noreply@example.com');
|
||||
// 引入配置文件
|
||||
// 检查配置文件是否存在并加载
|
||||
$configFile = 'd:\app2\config.php';
|
||||
if (!file_exists($configFile)) {
|
||||
die('配置文件缺失: ' . $configFile . ',无法继续执行');
|
||||
}
|
||||
require_once $configFile;
|
||||
|
||||
// 引入日志工具
|
||||
require_once 'd:\app2\includes\logger.php';
|
||||
|
||||
// 配置文件加载后日志记录和常量检查
|
||||
log_error('配置文件已成功加载: ' . $configFile);
|
||||
// 验证关键常量是否定义
|
||||
log_error('配置加载后常量检查 - SMTP_HOST: ' . (defined('SMTP_HOST') ? SMTP_HOST : '未定义'));
|
||||
log_error('配置加载后常量检查 - SMTP_USERNAME: ' . (defined('SMTP_USERNAME') ? SMTP_USERNAME : '未定义'));
|
||||
log_error('配置加载后常量检查 - SMTP_PASSWORD: ' . (defined('SMTP_PASSWORD') ? '已设置' : '未定义'));
|
||||
log_error('配置加载后常量检查 - SMTP_PORT: ' . (defined('SMTP_PORT') ? SMTP_PORT : '未定义'));
|
||||
log_error('配置文件加载后 - SMTP_USERNAME: ' . (defined('SMTP_USERNAME') ? SMTP_USERNAME : '未定义') . ', SMTP_PORT: ' . (defined('SMTP_PORT') ? SMTP_PORT : '未定义'));
|
||||
|
||||
|
||||
|
||||
|
||||
// 引入PHPMailer命名空间
|
||||
@@ -10,9 +27,6 @@ use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\SMTP;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
// 引入配置文件
|
||||
require_once '../config.php';
|
||||
|
||||
// 引入Composer自动加载器
|
||||
require_once '../vendor/autoload.php';
|
||||
|
||||
@@ -78,10 +92,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// 生成验证令牌
|
||||
$verificationToken = bin2hex(random_bytes(32));
|
||||
$insertStmt = $conn->prepare('INSERT INTO developers (username, email, password, verification_token) VALUES (?, ?, ?, ?)');
|
||||
if (!$insertStmt) {
|
||||
log_error('插入准备失败: ' . $conn->error, __FILE__, __LINE__);
|
||||
$error = '系统错误,请稍后再试';
|
||||
} else {
|
||||
$insertStmt->bind_param('ssss', $username, $email, $hashedPassword, $verificationToken);
|
||||
if (!$insertStmt->execute()) {
|
||||
log_error('插入执行失败: ' . $insertStmt->error, __FILE__, __LINE__);
|
||||
$error = '系统错误,请稍后再试';
|
||||
} else {
|
||||
// 生成验证链接
|
||||
$verificationLink = 'https://' . $_SERVER['HTTP_HOST'] . '/developer/verify_email.php?token=' . urlencode($verificationToken);
|
||||
|
||||
@@ -92,6 +107,18 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$templateContent = str_replace('{username}', htmlspecialchars($username), $templateContent);
|
||||
$templateContent = str_replace('{verification_link}', $verificationLink, $templateContent);
|
||||
|
||||
// 调试日志测试
|
||||
$testLogDir = 'd:\\app2\\logs';
|
||||
$testLogFile = $testLogDir . '\\test.log';
|
||||
if (!is_dir($testLogDir)) {
|
||||
mkdir($testLogDir, 0755, true);
|
||||
}
|
||||
file_put_contents($testLogFile, date('[Y-m-d H:i:s] ') . '邮件发送代码开始执行' . PHP_EOL, FILE_APPEND);
|
||||
|
||||
// 添加SMTP配置调试日志
|
||||
log_error('SMTP配置参数 - HOST: ' . (defined('SMTP_HOST') ? SMTP_HOST : '未定义') . ', PORT: ' . (defined('SMTP_PORT') ? SMTP_PORT : '未定义') . ', USERNAME: ' . (defined('SMTP_USERNAME') ? SMTP_USERNAME : '未定义') . ', ENCRYPTION: ' . (defined('SMTP_ENCRYPTION') ? SMTP_ENCRYPTION : '未定义'), __FILE__, __LINE__);
|
||||
log_error('开始执行邮件发送流程', __FILE__, __LINE__);
|
||||
|
||||
// 配置SMTP邮件发送
|
||||
require_once '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
|
||||
require_once '../vendor/phpmailer/phpmailer/src/SMTP.php';
|
||||
@@ -101,11 +128,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
|
||||
try {
|
||||
$mail->isSMTP();
|
||||
$mail->SMTPDebug = 4;
|
||||
// 输出当前SMTP配置参数用于调试
|
||||
log_error('SMTP配置参数: HOST=' . SMTP_HOST . ', PORT=' . SMTP_PORT . ', USERNAME=' . SMTP_USERNAME . ', ENCRYPTION=' . SMTP_ENCRYPTION);
|
||||
// 检查openssl扩展是否启用
|
||||
log_error('OpenSSL扩展状态: ' . (extension_loaded('openssl') ? '已启用' : '未启用')); // 启用详细调试
|
||||
$mail->Debugoutput = function($str, $level) {
|
||||
$logDir = 'd:\\app2\\logs';
|
||||
if (!is_dir($logDir)) {
|
||||
mkdir($logDir, 0755, true);
|
||||
}
|
||||
file_put_contents($logDir . '\\smtp_debug.log', date('[Y-m-d H:i:s] ') . $str . PHP_EOL, FILE_APPEND);
|
||||
};
|
||||
$mail->Host = defined('SMTP_HOST') ? SMTP_HOST : 'smtp.example.com';
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = defined('SMTP_USERNAME') ? SMTP_USERNAME : ''; // Ensure SMTP_USERNAME is defined in config.php
|
||||
$mail->Password = defined('SMTP_PASSWORD') ? SMTP_PASSWORD : '';
|
||||
$mail->SMTPSecure = defined('SMTP_ENCRYPTION') ? SMTP_ENCRYPTION : 'tls'; // Ensure SMTP_ENCRYPTION is defined in config.php
|
||||
$mail->AuthType = 'PLAIN'; // 尝试使用PLAIN认证方式
|
||||
$mail->Port = defined('SMTP_PORT') ? SMTP_PORT : 587;
|
||||
|
||||
$mail->setFrom(defined('SMTP_FROM_EMAIL') ? SMTP_FROM_EMAIL : 'noreply@example.com', defined('SMTP_FROM_NAME') ? SMTP_FROM_NAME : 'App Store'); // Ensure SMTP_FROM_EMAIL is defined in config.php
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
<?php
|
||||
require_once('../includes/logger.php');
|
||||
|
||||
// 引入配置文件
|
||||
require_once '../config.php';
|
||||
|
||||
session_start();
|
||||
|
||||
// 检查开发者是否已登录
|
||||
if (!isset($_SESSION['developer_id'])) {
|
||||
if (!isset($_SESSION['developer_id']) || !is_numeric($_SESSION['developer_id'])) {
|
||||
log_error('开发者会话ID不存在或无效', __FILE__, __LINE__);
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
$developerId = $_SESSION['developer_id'];
|
||||
$developerId = (int)$_SESSION['developer_id'];
|
||||
log_info("上传应用的开发者ID: $developerId", __FILE__, __LINE__);
|
||||
log_info("上传应用的开发者ID: $developerId", __FILE__, __LINE__);
|
||||
$error = '';
|
||||
$success = '';
|
||||
|
||||
@@ -26,7 +31,10 @@ if (!$stmt) {
|
||||
$developer = $result->fetch_assoc();
|
||||
$stmt->close();
|
||||
|
||||
if (!$developer || !$developer['is_verified']) {
|
||||
log_info("开发者验证状态: " . ($developer ? ($developer['is_verified'] ? "已验证" : "未验证") : "开发者不存在"), __FILE__, __LINE__);
|
||||
if (!$developer) {
|
||||
$error = '开发者账号不存在,请重新登录。';
|
||||
} elseif (!$developer['is_verified']) {
|
||||
$error = '您的邮箱尚未验证,请先验证邮箱后再上传应用。';
|
||||
}
|
||||
}
|
||||
@@ -89,6 +97,29 @@ if (!($conn instanceof mysqli)) {
|
||||
$error = '应用文件上传失败';
|
||||
}
|
||||
} else {
|
||||
// 验证标签ID是否存在
|
||||
if (!empty($tags)) {
|
||||
$tagIds = implode(',', array_map('intval', $tags));
|
||||
$tagCheckStmt = $conn->prepare("SELECT id FROM tags WHERE id IN ($tagIds)");
|
||||
if (!$tagCheckStmt) {
|
||||
log_error('标签验证查询准备失败: ' . $conn->error, __FILE__, __LINE__);
|
||||
$error = '系统错误,请稍后再试';
|
||||
} else {
|
||||
$tagCheckStmt->execute();
|
||||
$tagResult = $tagCheckStmt->get_result();
|
||||
$validTagIds = [];
|
||||
while ($tag = $tagResult->fetch_assoc()) {
|
||||
$validTagIds[] = $tag['id'];
|
||||
}
|
||||
$tagCheckStmt->close();
|
||||
|
||||
$invalidTags = array_diff($tags, $validTagIds);
|
||||
if (!empty($invalidTags)) {
|
||||
log_error('无效的标签ID: ' . implode(',', $invalidTags), __FILE__, __LINE__);
|
||||
$error = '选择了无效的标签,请刷新页面重试';
|
||||
}
|
||||
}
|
||||
}
|
||||
$error = '应用文件上传错误: ' . ($appFile ? $appFile['error'] : '未找到文件');
|
||||
}
|
||||
|
||||
@@ -111,7 +142,7 @@ if (!($conn instanceof mysqli)) {
|
||||
mkdir($target_dir, 0755, true);
|
||||
}
|
||||
if (move_uploaded_file($tmpName, $imagePath)) {
|
||||
$imagePaths[] = $imagePath;
|
||||
$imagePaths[] = $imageRelativePath;
|
||||
} else {
|
||||
log_error('图片文件移动失败: ' . $images['name'][$key], __FILE__, __LINE__);
|
||||
}
|
||||
@@ -147,13 +178,15 @@ if (!($conn instanceof mysqli)) {
|
||||
// 移除多余的$status参数,匹配SQL中9个占位符
|
||||
// 修正age_rating_description类型为字符串,并确保9个参数与占位符匹配
|
||||
// 修复变量名错误:使用已验证的$appFilePath替换未定义的$file_path
|
||||
$stmt->bind_param('ssissssss', $appName, $appDescription, $developerId, $platforms_json, $ageRating, $ageRatingDescription, $version, $changelog, $filePath);
|
||||
$stmt->bind_param('ssissssss', $appName, $appDescription, $developerId, $platforms_json, $ageRating, $ageRatingDescription, $version, $changelog, $appRelativePath);
|
||||
if (!$stmt->execute()) {
|
||||
throw new Exception('应用基本信息查询执行失败: ' . $stmt->error);
|
||||
}
|
||||
$appId = $stmt->insert_id;
|
||||
log_info("应用已插入数据库: ID=$appId, 状态=pending", __FILE__, __LINE__);
|
||||
$stmt->close();
|
||||
|
||||
log_info("开始处理应用关联数据: ID=$appId", __FILE__, __LINE__);
|
||||
// 插入应用标签关联
|
||||
foreach ($tags as $tagId) {
|
||||
$tagStmt = $conn->prepare('INSERT INTO app_tags (app_id, tag_id) VALUES (?, ?)');
|
||||
@@ -168,7 +201,7 @@ if (!($conn instanceof mysqli)) {
|
||||
}
|
||||
|
||||
// 插入应用图片
|
||||
foreach ($imagePaths as $imagePath) {
|
||||
foreach ($imagePaths as $imageRelativePath) {
|
||||
$imageStmt = $conn->prepare('INSERT INTO app_images (app_id, image_path) VALUES (?, ?)');
|
||||
if (!$imageStmt) {
|
||||
throw new Exception('图片关联查询准备失败: ' . $conn->error);
|
||||
@@ -191,13 +224,15 @@ if (!($conn instanceof mysqli)) {
|
||||
}
|
||||
$versionStmt->close();
|
||||
|
||||
log_info("所有关联数据处理完成,准备提交事务: ID=$appId", __FILE__, __LINE__);
|
||||
// 提交事务
|
||||
$conn->commit();
|
||||
log_info("应用上传成功: ID=$appId, 状态=pending", __FILE__, __LINE__);
|
||||
$success = '应用上传成功,请等待管理员审核';
|
||||
} catch (Exception $e) {
|
||||
// 回滚事务
|
||||
$conn->rollback();
|
||||
log_error('应用上传事务失败: ' . $e->getMessage(), __FILE__, __LINE__);
|
||||
log_error('应用上传事务失败(ID=$appId): ' . $e->getMessage(), __FILE__, __LINE__);
|
||||
$error = '上传应用时发生错误,请稍后再试';
|
||||
}
|
||||
}
|
||||
|
||||
56
includes/logger.php
Normal file
56
includes/logger.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* Logging utility for the application
|
||||
* Provides log_info() and log_error() functions
|
||||
*/
|
||||
|
||||
if (!function_exists('log_info')) {
|
||||
/**
|
||||
* Log informational message
|
||||
* @param string $message The message to log
|
||||
* @param string $file Optional file name where the log was called
|
||||
* @param int $line Optional line number where the log was called
|
||||
*/
|
||||
function log_info($message, $file = '', $line = 0) {
|
||||
$logFile = __DIR__ . '/../logs/app_' . date('Y-m-d') . '.log';
|
||||
|
||||
// Create logs directory if it doesn't exist
|
||||
if (!file_exists(dirname($logFile))) {
|
||||
mkdir(dirname($logFile), 0755, true);
|
||||
}
|
||||
|
||||
$prefix = '[' . date('Y-m-d H:i:s') . '] [INFO]';
|
||||
if (!empty($file) && $line > 0) {
|
||||
$prefix .= ' [' . basename($file) . ':' . $line . ']';
|
||||
}
|
||||
|
||||
$logMessage = $prefix . ' ' . $message . PHP_EOL;
|
||||
file_put_contents($logFile, $logMessage, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('log_error')) {
|
||||
/**
|
||||
* Log error message
|
||||
* @param string $message The error message to log
|
||||
* @param string $file Optional file name where the error occurred
|
||||
* @param int $line Optional line number where the error occurred
|
||||
*/
|
||||
function log_error($message, $file = '', $line = 0) {
|
||||
$logFile = __DIR__ . '/../logs/error_' . date('Y-m-d') . '.log';
|
||||
|
||||
// Create logs directory if it doesn't exist
|
||||
if (!file_exists(dirname($logFile))) {
|
||||
mkdir(dirname($logFile), 0755, true);
|
||||
}
|
||||
|
||||
$prefix = '[' . date('Y-m-d H:i:s') . '] [ERROR]';
|
||||
if (!empty($file) && $line > 0) {
|
||||
$prefix .= ' [' . basename($file) . ':' . $line . ']';
|
||||
}
|
||||
|
||||
$logMessage = $prefix . ' ' . $message . PHP_EOL;
|
||||
file_put_contents($logFile, $logMessage, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user