Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 196 additions & 26 deletions plugin/rss.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

function plugin_rss_action()
{
global $vars, $rss_max, $page_title, $whatsnew, $trackback;
global $qblog_defaultpage, $qblog_title, $qblog_close;
global $vars, $rss_max, $page_title, $whatsnew, $trackback, $username;
global $qblog_defaultpage, $qblog_title, $qblog_close, $script;
$qm = get_qm();
$qt = get_qt();

Expand All @@ -33,12 +33,14 @@ function plugin_rss_action()

$lang = LANG;
$qblog_mode = FALSE;
$qblog_export = FALSE;
$page_export = FALSE; // 全ページエクスポート

//blogモード
if( isset($vars['blog_rss']) && $vars['blog_rss']!='' ){
$blog_mode = $vars['blog_rss']; //ブログページ名
$qt->setv('blog_rss_mode', true);

$page_title_utf8 = $page_title.' - '.$blog_mode;
}
// qblogモード
Expand All @@ -52,6 +54,16 @@ function plugin_rss_action()
$qblog_mode = TRUE;
$blog_mode = FALSE;
$page_title_utf8 = mb_convert_encoding($qblog_title, 'UTF-8', SOURCE_ENCODING);

//全ページ表示
if (isset($vars['qblog_export']) && $vars['qblog_export'] === $username) {
$qblog_export = TRUE;
}
}
else if (isset($vars['page_export']) && $vars['page_export'] === $username) {
$blog_mode = FALSE;
$page_export = TRUE;
$page_title_utf8 = mb_convert_encoding($page_title, 'UTF-8', SOURCE_ENCODING);
}
else
{
Expand All @@ -64,28 +76,42 @@ function plugin_rss_action()
// Creating <item>
global $ignore_plugin, $strip_plugin, $strip_plugin_inline;
$items = $rdf_li = '';


if ($qblog_mode)
{
$qblog_recent = CACHEQBLOG_DIR . 'qblog_recent.dat';
if (! file_exists($qblog_recent)) die($qm->m['plg_rss']['err_nodata']);

// ページネーション用のヘッダー行を飛ばす
$lines = file_head($qblog_recent, $rss_max+1);
array_shift($lines);
if ($qblog_export) {
$lines = plugin_rss_qblog_posts();
}
else {
// ページネーション用のヘッダー行を飛ばす
$lines = file_head($qblog_recent, $rss_max+1);
array_shift($lines);
}
}
else
{
$recent = CACHE_DIR . 'recent.dat';
if (! file_exists($recent)) die($qm->m['plg_rss']['err_nodata']);
$lines = file_head($recent, $rss_max);
if ($page_export) {
$pages = plugin_rss_get_all_page();
$lines = array();
foreach ($pages as $_page) {
if ( ! preg_match('/\A(:config(\z|\/)|InterWiki|MenuAdmin|QBlog|QHMAdmin|RecentChanges|RecentDeleted)/', $_page)) {
$lines[] = "\t$_page";
}
}
} else {
$recent = CACHE_DIR . 'recent.dat';
if (! file_exists($recent)) die($qm->m['plg_rss']['err_nodata']);
$lines = file_head($recent, $rss_max);
}
}

foreach ($lines as $line)
{
$skip_list = FALSE;

if ($qblog_mode)
{
$page = trim($line);
Expand All @@ -100,19 +126,19 @@ function plugin_rss_action()
if( $blog_mode && !preg_match('/^'.$blog_mode.'\/.*/', $page) ){
continue;
}

$r_page = rawurlencode($page);
$title = get_page_title($page);
$source = get_source($page);

foreach($source as $k => $l)
{
if (preg_match($ignore_plugin, $l))
{ // リストから省く
$skip_list = TRUE;
break;
}

if (preg_match($strip_plugin, $l))
{ // 説明から省く
unset($source[$k]);
Expand All @@ -122,30 +148,69 @@ function plugin_rss_action()
{
continue;
}

//html(noskinを避ける)
if( count($source) > 0){
$source = str_replace('#html(noskin)', '#html()', $source);
$source = preg_replace($strip_plugin_inline, '', $source); // 行内のプラグインを説明から省く
}
$contents = mb_strimwidth( strip_htmltag( convert_html( $source ) ), 0, RSS_DESP_LENGTH , '...' );
$contents = preg_replace_callback('/(&[^;]+;)/', 'plugin_rss_html_entity_decode', $contents);

// 全文出力
if ($qblog_export || $page_export) {
$contents = convert_html($source);
$contents = preg_replace(
'/<img src="(swfu\/.*?)"/',
'<img src="'.h(dirname($script)).'/\1"',
$contents
);
} else {
$contents = mb_strimwidth( strip_htmltag( convert_html( $source ) ), 0, RSS_DESP_LENGTH , '...' );
}
$contents = preg_replace_callback(
'/(&[^;]+;)/',
'plugin_rss_html_entity_decode', $contents);
$contents = plugin_rss_utf8_for_xml($contents);

switch ($version) {
case '0.91': /* FALLTHROUGH */
case '2.0':

$wp_post_type = $page_export ? '<wp:post_type>page</wp:post_type>' : '';
$wp_post_type = $qblog_export ? '<wp:post_type>post</wp:post_type>' : $wp_post_type;

$cat = get_qblog_category($page);
$cat_nicename = strtolower(rawurlencode($cat));
$wp_category = $qblog_export ? '<category domain="category" nicename="'.$cat_nicename.'"><![CDATA['.$cat.']]></category>': '';
$wp_comments = $qblog_export ? plugin_rss_get_wp_comments($page) : '';

$post_date = date('Y-m-d H:i:s', $time);
$post_date_gmt = gmdate('Y-m-d H:i:s', $time);
$wp_post_date = $page_export || $qblog_export ? '<wp:post_date>'.$post_date.'</wp:post_date><wp:post_date_gmt>'.$post_date_gmt.'</wp:post_date_gmt>' : '';

$permalink = "{$self}?{$r_page}";
$wp_qhm_permalink = $qblog_export || $page_export ? '<wp:postmeta><wp:meta_key>qhm_permalink</wp:meta_key><wp:meta_value>'.$permalink.'</wp:meta_value></wp:postmeta>' : '';
$date = get_date('D, d M Y H:i:s T', $time);
$date = ($version == '0.91') ?
' <description>' . $date .' -- '. $contents. '</description>' :
' <pubDate>' . $date . '</pubDate>' .
' <description>' . $contents . '</description>';
if ($version == '0.91') {
$date = '';
$desc = ' <description>' . $date .' -- '. $contents. '</description>';
} else {
$date = ' <pubDate>' . $date . '</pubDate>';
if ($qblog_export || $page_export) {
$desc = '<content:encoded><![CDATA['. $contents . ']]></content:encoded>';
} else {
$desc = ' <description>' . $contents . '</description>';
}
}
$items .= <<<EOD
<item>
<title>$title</title>
<link>$self?$r_page</link>
<link>$permalink</link>
$date
$desc
$wp_post_type
$wp_post_date
$wp_category
$wp_comments
$wp_qhm_permalink
</item>

EOD;
Expand Down Expand Up @@ -190,6 +255,10 @@ function plugin_rss_action()
$page_title_utf8 = h(plugin_rss_utf8_for_xml($page_title_utf8));
$description = h(plugin_rss_utf8_for_xml($description));

$wxr_definitions = $page_export || $qblog_export ? '
<wp:wxr_version>1.2</wp:wxr_version>
' : '';

switch ($version) {
case '0.91':
print '<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"' .
Expand All @@ -198,12 +267,16 @@ function plugin_rss_action()

case '2.0':
print <<<EOD
<rss version="{$version}">
<rss version="{$version}"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wp="http://wordpress.org/export/1.2/"
>
<channel>
<title>{$page_title_utf8}</title>
<link>{$self}?{$pagename}</link>
<description>{$description}</description>
<language>{$lang}</language>
{$wxr_definitions}

{$items}
</channel>
Expand Down Expand Up @@ -253,4 +326,101 @@ function plugin_rss_utf8_for_xml($string)
return preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
}

?>
function plugin_rss_qblog_posts()
{
global $qblog_page_prefix, $qblog_page_re;

$files = glob(DATA_DIR . encode($qblog_page_prefix) . '*');

$pages = array();
foreach ($files as $file)
{
$pagename = decode(basename($file, '.txt'));
if (preg_match($qblog_page_re, $pagename, $mts))
{
$pages[] = $pagename;
}
}
return $pages;
}

function plugin_rss_get_all_page($dir = DATA_DIR, $ext = '.txt')
{
$pages = get_existpages($dir, $ext);
$qm = get_qm();

$symbol = ' ';
$list = array();

//並び替える
foreach ($pages as $file => $page)
{
$list[] = $page;
}

return $list;
}

function plugin_rss_get_wp_comments($page)
{
$comments = array();
if (file_exists(CACHEQBLOG_DIR . encode($page) . '.qbcm.dat')) {
$comments = unserialize(file_get_contents(CACHEQBLOG_DIR . encode($page) . '.qbcm.dat'));
}
else {
return '';
}

$xml = '';
foreach ($comments as $i => $comment)
{
if ( ! $comment['show']) {
unset($comments[$i]);
continue;
}

$comment_body = h($comment['msg']);
$comment_body = nl2br($comment_body);
$ptns = array(
'/(?:https?|ftp)(?::\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/',
);
$rpls = array(
'<a href="$0">$0</a>'
);
$comment_body = preg_replace($ptns, $rpls, $comment_body);
$comment_body = '<b>' . h($comment['title']) . "</b><br>\n<br>\n" .$comment_body;

// datetime: 2016.09.15 13:22:43
//var_dump($comment);
$time = mktime(
(int)substr($comment['datetime'], 11, 2),
(int)substr($comment['datetime'], 14, 2),
(int)substr($comment['datetime'], 17, 2),
(int)substr($comment['datetime'], 5, 2),
(int)substr($comment['datetime'], 8, 2),
(int)substr($comment['datetime'], 0, 4)
);
$date = date('Y-m-d H:i:s', $time);
$gmdate = gmdate('Y-m-d H:i:s', $time);

$xml .= "<wp:comment>\n";

$xml .= "<wp:comment_author><![CDATA[" . $comment['name'] . "]]></wp:comment_author>\n";
$xml .= "<wp:comment_author_email></wp:comment_author_email>\n";
$xml .= "<wp:comment_author_url></wp:comment_author_url>\n";
$xml .= "<wp:comment_date>" . $date . "</wp:comment_date>\n";
$xml .= "<wp:comment_date_gmt>" . $gmdate . "</wp:comment_date_gmt>\n";
$xml .= "<wp:comment_content><![CDATA[" . $comment_body . "]]></wp:comment_content>\n";
if ($comment['accepted'])
$xml .= "<wp:comment_approved>1</wp:comment_approved>\n";
else
$xml .= "<wp:comment_approved>0</wp:comment_approved>\n";
$xml .= "<wp:comment_type></wp:comment_type>\n";
$xml .= "<wp:comment_parent>0</wp:comment_parent>\n";
$xml .= "<wp:comment_user_id>0</wp:comment_user_id>\n";

$xml .= "</wp:comment>\n";
}

return $xml;
}