93 lines
2.9 KiB
PHP
93 lines
2.9 KiB
PHP
|
|
<?php
|
|||
|
|
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 单篇文章 Markdown 导出
|
|||
|
|
*
|
|||
|
|
* 路由:/action/article_to_obsidian_export?cid=1&_nonce=xxxxxxxx
|
|||
|
|
* 由 ArticleToObsidian_Plugin::renderExportBtn() 生成链接,
|
|||
|
|
* 直接返回 Content-Disposition: attachment,浏览器另存为 .md,不需要 JS。
|
|||
|
|
*/
|
|||
|
|
class ArticleToObsidian_ExportAction extends Typecho_Widget implements Widget_Interface_Do
|
|||
|
|
{
|
|||
|
|
public function action()
|
|||
|
|
{
|
|||
|
|
$this->export();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private function export()
|
|||
|
|
{
|
|||
|
|
$cid = (int) $this->request->get('cid');
|
|||
|
|
$nonce = (string) $this->request->get('_nonce');
|
|||
|
|
|
|||
|
|
if ($cid <= 0 || !$this->nonceValid($cid, $nonce)) {
|
|||
|
|
$this->fail('导出链接已失效,刷新文章页后重新点击即可。', 403);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$db = Typecho_Db::get();
|
|||
|
|
$post = $db->fetchRow($db->select('cid', 'title', 'text', 'created', 'modified', 'slug', 'status')
|
|||
|
|
->from('table.contents')
|
|||
|
|
->where('cid = ?', $cid)
|
|||
|
|
->where('type = ?', 'post')
|
|||
|
|
->limit(1));
|
|||
|
|
|
|||
|
|
if (!$post) {
|
|||
|
|
$this->fail('文章不存在。', 404);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ($post['status'] !== 'publish') {
|
|||
|
|
$this->fail('文章尚未发布,不能导出。', 403);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$markdown = ArticleToObsidian_Plugin::buildMarkdown($post);
|
|||
|
|
$filename = ArticleToObsidian_Plugin::exportFilename($post['title'], $cid);
|
|||
|
|
|
|||
|
|
// 中文文件名:给老浏览器一个纯 ASCII 兜底名,再按 RFC 5987 补上真实文件名
|
|||
|
|
$fallback = 'post-' . $cid . '.md';
|
|||
|
|
|
|||
|
|
$this->response->setStatus(200);
|
|||
|
|
$this->response->setHeader(
|
|||
|
|
'Content-Disposition',
|
|||
|
|
'attachment; filename="' . $fallback . '"; filename*=UTF-8\'\'' . rawurlencode($filename)
|
|||
|
|
);
|
|||
|
|
$this->response->setHeader('X-Content-Type-Options', 'nosniff');
|
|||
|
|
$this->response->setHeader('Cache-Control', 'private, max-age=0, must-revalidate');
|
|||
|
|
$this->response->throwContent($markdown, 'text/markdown');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 校验串:当前小时或上一小时都算有效
|
|||
|
|
*
|
|||
|
|
* @param int $cid
|
|||
|
|
* @param string $nonce
|
|||
|
|
* @return bool
|
|||
|
|
*/
|
|||
|
|
private function nonceValid($cid, $nonce)
|
|||
|
|
{
|
|||
|
|
if ($nonce === '') {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$now = time();
|
|||
|
|
foreach (array($now, $now - 3600) as $time) {
|
|||
|
|
if (hash_equals(ArticleToObsidian_Plugin::exportNonce($cid, $time), $nonce)) {
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @param string $message
|
|||
|
|
* @param int $status
|
|||
|
|
*/
|
|||
|
|
private function fail($message, $status = 400)
|
|||
|
|
{
|
|||
|
|
// 状态码必须走 Response 对象:Typecho 最后会用它自己的 status 覆盖 header(),
|
|||
|
|
// 直接 http_response_code() 是无效的(试过,永远 200)。
|
|||
|
|
$this->response->setStatus($status);
|
|||
|
|
$this->response->throwContent($message, 'text/plain');
|
|||
|
|
}
|
|||
|
|
}
|