完成一个destoon文章一键下载为doc或者pdf的插件,整整一天,不太好处理各种问题哦
`
<?php
/**
- 文章一键下载插件核心文件(无调试信息版)
- 路径:module/article_download/download.php
*/
define('DT_ADMIN', false);
define('NOFRAME', true);
ob_clean(); // 清除缓冲区,避免干扰文件输出
require '../../common.inc.php';
define('MODULE_PATH', DT_ROOT . '/module/article_download/');
define('MODULE_LIB', MODULE_PATH . 'inc/');
error_reporting(E_ALL);
ini_set('display_errors', 1);
// --------------------------
// 1. 参数验证
// --------------------------
$type = in_array($_GET['type'], ['doc', 'pdf']) ? $_GET['type'] : '';
$itemid = intval($_GET['itemid']);
$moduleid = intval($_GET['moduleid']);
// 允许的模块ID列表(根据你的实际需求添加,如21、22、23等)
$allow_moduleids = [21, 26, 33,41,40]; // 例如:支持模块21、22、23
// 验证参数:类型有效、itemid正整数、moduleid在允许列表中
if (empty($type) || $itemid < 1 || !in_array($moduleid, $allow_moduleids)) {
exit("参数错误:请检查请求参数(支持的模块ID:" . implode(',', $allow_moduleids) . ")");}
// --------------------------
// 2. 数据库配置(固定表前缀)
// --------------------------
$table_prefix = 'xu_';
$main_table = $table_prefix . 'article_' . $moduleid; // xu_article_21
$content_table = $table_prefix . 'article_data_' . $moduleid; // xu_article_data_21
// --------------------------
// 3. 读取文章数据(关联主表和内容表)
// --------------------------
$sql = "SELECT m.title, c.content
FROM {$main_table} m
INNER JOIN {$content_table} c ON m.itemid = c.itemid
WHERE m.itemid = {$itemid} AND m.status = 3 LIMIT 1";
$article = $db->get_one($sql);
if (empty($article) || empty($article['title'])) {
exit("文章不存在或未发布");}
// 处理空内容
$article['title'] = trim($article['title']) ?: "未命名文章_{$itemid}";
$article['content'] = trim($article['content']) ?: "无内容";
// --------------------------
// 4. 检查依赖库
// --------------------------
$lib_path = DT_ROOT . '/module/article_download/inc/';
$error = '';
if ($type == 'doc') {
if (!file_exists($lib_path . 'PHPWord/Autoloader.php')) {
$error = "DOC生成库缺失,请检查PHPWord是否正确安装";
}} else {
if (!file_exists($lib_path . 'tcpdf/tcpdf.php')) {
exit("PDF生成库缺失,请检查TCPDF是否正确安装于目录: " . $lib_path . 'tcpdf/');}
}
if ($error) {
exit($error);}
// --------------------------
// 5. 生成并输出文件
// --------------------------
try {
if ($type == 'doc') {
require $lib_path . 'doc_generator.php';
DocGenerator::output($article['title'], $article['content']);
} else {
require $lib_path . 'pdf_generator.php';
$pdf = new PdfGenerator(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);$pdf->generateOutput($article['title'], $article['content']);
}} catch (Exception $e) {
exit("文件生成失败:" . $e->getMessage());} catch (Error $e) {
exit("系统错误:" . $e->getMessage());}
exit;
`
<!-- 文章下载按钮 --><?php/**
- 强制清理临时图片文件(解决无法删除问题)
*/
defined('IN_DESTOON') or exit('Access Denied');
// 临时目录路径(与生成器中的路径一致)
$tempDir = DT_ROOT . '/module/article_download/temp/';
$expireTime = 3600; // 清理1小时前的文件(可调整)
if (!is_dir($tempDir)) {
exit("临时目录不存在:{$tempDir}");}
// 强制修改目录权限(确保可写)
@chmod($tempDir, 0777);
// 遍历目录中的所有文件
$files = glob($tempDir . '*');
$deleted = 0;
$failed = 0;
foreach ($files as $file) {
// 只处理文件(跳过子目录)
if (is_file($file)) {
// 检查文件是否过期
if (time() - filemtime($file) > $expireTime) {
// 尝试强制删除(处理权限问题)
if (@unlink($file)) {
$deleted++;
} else {
// 若删除失败,先修改权限再尝试
@chmod($file, 0666);
if (@unlink($file)) {
$deleted++;
} else {
$failed++;
error_log("无法删除文件:{$file}");
}
}
}
}}
exit("清理完成:成功删除{$deleted}个文件,失败{$failed}个(失败文件已记录到错误日志)");
评论(0)