Начинаем добавлять поддержку генерации atom-лент.
This commit is contained in:
Exile 2014-08-12 14:07:38 +04:00
commit 683b86bf5c
6 changed files with 296 additions and 5 deletions

View file

@ -645,6 +645,7 @@ INSERT INTO `bb_cron` VALUES (19, 1, 'Captcha', 'captcha_gen_gc.php', 'daily', N
INSERT INTO `bb_cron` VALUES (20, 1, 'Tracker dl-complete count', 'tr_complete_count.php', 'interval', NULL, NULL, 255, '', '', '06:00:00', 0, '', 0, 0, 0);
INSERT INTO `bb_cron` VALUES (21, 1, 'Cache garbage collector', 'cache_gc.php', 'interval', NULL, NULL, 255, '', '', '00:05:00', 0, '', 0, 0, 0);
INSERT INTO `bb_cron` VALUES (22, 1, 'Sitemap update', 'sitemap.php', 'daily', NULL, '06:00:00', 30, '', '', NULL, 0, '', 0, 0, 0);
INSERT INTO `bb_cron` VALUES (23, 1, 'Update forums atom', 'update_forums_atom.php', 'interval', NULL, NULL, 255, '', '', '00:20:00', 0, '', 0, 0, 0);
-- --------------------------------------------------------

0
upload/atom/.keep Normal file
View file

View file

@ -56,6 +56,7 @@
* Avatars
* Misc
* Captcha
* Atom feed
**/
if (!defined('BB_ROOT')) die(basename(__FILE__));
@ -68,8 +69,8 @@ $domain_name = (!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : $do
// Version info
$bb_cfg['tp_version'] = '2.0.9 (RC)';
$bb_cfg['tp_release_date'] = '07-08-2014';
$bb_cfg['tp_release_state'] = 'R594b';
$bb_cfg['tp_release_date'] = '12-08-2014';
$bb_cfg['tp_release_state'] = 'R595';
// Database
$charset = 'utf8';
@ -590,10 +591,16 @@ $bb_cfg['advert_url'] = 'misc.php?do=info&show=advert';
// Captcha
$bb_cfg['captcha'] = array(
'disabled' => false,
'disabled' => false,
'secret_key' => 'secret_key',
'img_url' => './images/captcha/', # without '/'
'img_path' => BB_PATH .'/images/captcha/', # without '/'
'img_url' => './images/captcha/',
'img_path' => BB_PATH .'/images/captcha/',
);
// Atom feed
$bb_cfg['atom'] = array(
'path' => BB_PATH .'/atom/',
'url' => "http://{$domain_name}/atom",
);
define('BB_CFG_LOADED', true);

68
upload/feed.php Normal file
View file

@ -0,0 +1,68 @@
<?php
define('IN_FORUM', true);
define('BB_SCRIPT', 'feed');
define('BB_ROOT', './');
require(BB_ROOT .'common.php');
$user->session_start(array('req_login' => true));
$mode = (string) @$_REQUEST['mode'];
$type = (string) @$_POST['type'];
$id = (int) @$_POST['id'];
$timecheck = TIMENOW - 600;
if (!$mode) bb_simple_die('Not specified mode');
if ($mode == 'get_feed_url' && ($type == 'f' || $type == 'u') && $id >= 0)
{
if ($type == 'f')
{
// Check if the user has actually sent a forum ID
$sql = "SELECT allow_reg_tracker, forum_name FROM ". BB_FORUMS ." WHERE forum_id = $id LIMIT 1";
if (!$forum_data = DB()->fetch_row($sql))
{
if ($id == 0)
{
$forum_data = array();
}
else bb_simple_die('Invalid type of query #1');
}
if (file_exists($bb_cfg['atom']['path'] .'/f/'. $id .'.atom') && filemtime($bb_cfg['atom']['path'] .'/f/'. $id .'.atom') > $timecheck)
{
redirect($bb_cfg['atom']['url'] .'/f/'. $id .'.atom');
}
else
{
require_once(INC_DIR .'functions_atom.php');
if (update_forum_feed($id, $forum_data)) redirect($bb_cfg['atom']['url'] .'/f/'. $id .'.atom');
else bb_simple_die('No feed for this forum (no topics)');
}
}
if ($type == 'u')
{
// Check if the user has actually sent a user ID
if ($id < 1)
{
bb_simple_die('Invalid type of query #2');
}
if (!$username = get_username($id))
{
bb_simple_die('Invalid type of query #3');
}
if (file_exists($bb_cfg['atom']['path'] .'/u/'. floor($id/5000000) .'/'. ($id % 100) .'/'. $id .'.atom') && filemtime($bb_cfg['atom']['path'] .'/u/'. floor($id/5000000) .'/'. ($id % 100) .'/'. $id .'.atom') > $timecheck)
{
redirect($bb_cfg['atom']['url'] .'/u/'. floor($id/5000000) .'/'. ($id % 100) .'/'. $id .'.atom');
}
else
{
require_once(INC_DIR .'functions_atom.php');
if (update_user_feed($id, $username)) redirect($bb_cfg['atom']['url'] .'/u/'. floor($id/5000000) .'/'. ($id % 100) .'/'. $id .'.atom');
else bb_simple_die('No feed for this user (no topics)');
}
}
}
else
{
bb_simple_die('Invalid type of query #4');
}

View file

@ -0,0 +1,29 @@
<?php
if (!defined('BB_ROOT')) die(basename(__FILE__));
global $bb_cfg;
require_once(INC_DIR .'functions_atom.php');
$timecheck = TIMENOW - 600;
$forums_data = DB()->fetch_rowset("SELECT forum_id, allow_reg_tracker, forum_name FROM ". BB_FORUMS);
if (file_exists($bb_cfg['atom']['path'] .'/f/0.atom') && filemtime($bb_cfg['atom']['path'] .'/f/0.atom') > $timecheck)
{
}
else
{
update_forum_feed(0, $forums_data);
}
foreach ($forums_data as $forum_data)
{
if (file_exists($bb_cfg['atom']['path'] .'/f/'. $forum_data['forum_id'] .'.atom') && filemtime($bb_cfg['atom']['path'] .'/f/'. $forum_data['forum_id'] .'.atom') > $timecheck)
{
}
else
{
update_forum_feed($forum_data['forum_id'], $forum_data);
}
}

View file

@ -0,0 +1,186 @@
<?php
if (!defined('BB_ROOT')) die(basename(__FILE__));
// Максимум записей = 50
// [Обновлено] - если дата изменения первого поста топика не старее недели (?) или в топике новые сообщения не старее недели (?)
function update_forum_feed ($forum_id, $forum_data)
{
global $bb_cfg;
$file_path = $bb_cfg['atom']['path'] .'/f/'. $forum_id .'.atom';
$select_tor_sql = $join_tor_sql = '';
if ($forum_id == 0) $forum_data['forum_name'] = 'Общая по всем разделам';
if ($forum_id > 0 && $forum_data['allow_reg_tracker'])
{
$select_tor_sql = ', tor.size AS tor_size, tor.tor_status';
$join_tor_sql = "LEFT JOIN ". BB_BT_TORRENTS ." tor ON(t.topic_id = tor.topic_id)";
}
if ($forum_id == 0)
{
$sql = "
SELECT
t.topic_id, t.topic_title, t.topic_status,
u1.username AS first_username,
p1.post_time AS topic_first_post_time, p1.post_edit_time AS topic_first_post_edit_time,
p2.post_time AS topic_last_post_time, p2.post_edit_time AS topic_last_post_edit_time,
tor.size AS tor_size, tor.tor_status
FROM ". BB_BT_TORRENTS ." tor
LEFT JOIN ". BB_TOPICS ." t ON(tor.topic_id = t.topic_id)
LEFT JOIN ". BB_USERS ." u1 ON(t.topic_poster = u1.user_id)
LEFT JOIN ". BB_POSTS ." p1 ON(t.topic_first_post_id = p1.post_id)
LEFT JOIN ". BB_POSTS ." p2 ON(t.topic_last_post_id = p2.post_id)
ORDER BY t.topic_last_post_time DESC
LIMIT 100
";
}
else if ($forum_id > 0)
{
$sql = "
SELECT
t.topic_id, t.topic_title, t.topic_status,
u1.username AS first_username,
p1.post_time AS topic_first_post_time, p1.post_edit_time AS topic_first_post_edit_time,
p2.post_time AS topic_last_post_time, p2.post_edit_time AS topic_last_post_edit_time
$select_tor_sql
FROM ". BB_TOPICS ." t
LEFT JOIN ". BB_USERS ." u1 ON(t.topic_poster = u1.user_id)
LEFT JOIN ". BB_POSTS ." p1 ON(t.topic_first_post_id = p1.post_id)
LEFT JOIN ". BB_POSTS ." p2 ON(t.topic_last_post_id = p2.post_id)
$join_tor_sql
WHERE t.forum_id = $forum_id
ORDER BY t.topic_last_post_time DESC
LIMIT 50
";
}
$topics_tmp = DB()->fetch_rowset($sql);
$topics = array();
foreach ($topics_tmp as $topic)
{
if (isset($topic['topic_status']))
{
if ($topic['topic_status'] == TOPIC_MOVED) continue;
}
if (isset($topic['tor_status']))
{
if (isset($bb_cfg['tor_frozen'][$topic['tor_status']])) continue;
}
$topics[] = $topic;
}
if (!count($topics))
{
@unlink($file_path);
return false;
}
if (create_atom($file_path, 'f', $forum_id, htmlCHR($forum_data['forum_name']), $topics)) return true;
else return false;
}
function update_user_feed ($user_id, $username)
{
global $bb_cfg;
$file_path = $bb_cfg['atom']['path'] .'/u/'. floor($user_id/5000000) .'/'. ($user_id % 100) .'/'. $user_id .'.atom';
$sql = "
SELECT
t.topic_id, t.topic_title, t.topic_status,
u1.username AS first_username,
p1.post_time AS topic_first_post_time, p1.post_edit_time AS topic_first_post_edit_time,
p2.post_time AS topic_last_post_time, p2.post_edit_time AS topic_last_post_edit_time,
tor.size AS tor_size, tor.tor_status
FROM ". BB_TOPICS ." t
LEFT JOIN ". BB_USERS ." u1 ON(t.topic_poster = u1.user_id)
LEFT JOIN ". BB_POSTS ." p1 ON(t.topic_first_post_id = p1.post_id)
LEFT JOIN ". BB_POSTS ." p2 ON(t.topic_last_post_id = p2.post_id)
LEFT JOIN ". BB_BT_TORRENTS ." tor ON(t.topic_id = tor.topic_id)
WHERE t.topic_poster = $user_id
ORDER BY t.topic_last_post_time DESC
LIMIT 50
";
$topics_tmp = DB()->fetch_rowset($sql);
$topics = array();
foreach ($topics_tmp as $topic)
{
if (isset($topic['topic_status']))
{
if ($topic['topic_status'] == TOPIC_MOVED) continue;
}
if (isset($topic['tor_status']))
{
if (isset($bb_cfg['tor_frozen'][$topic['tor_status']])) continue;
}
$topics[] = $topic;
}
if (!count($topics))
{
@unlink($file_path);
return false;
}
if (create_atom($file_path, 'u', $user_id, wbr($username), $topics)) return true;
else return false;
}
function create_atom ($file_path, $mode, $id, $title, $topics)
{
global $bb_cfg;
$dir = dirname($file_path);
if (!file_exists($dir))
{
if (!bb_mkdir($dir)) return false;
}
foreach ($topics as $topic)
{
$last_time = $topic['topic_last_post_time'];
if ($topic['topic_last_post_edit_time']) $last_time = $topic['topic_last_post_edit_time'];
$date = bb_date($last_time, 'Y-m-d', 0);
$time = bb_date($last_time, 'H:i:s', 0);
break;
}
$atom = "";
$atom .= "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
$atom .= "<feed xmlns=\"http://www.w3.org/2005/Atom\" xml:base=\"http://". $bb_cfg['server_name'] . $bb_cfg['script_path'] ."\">\n";
$atom .= "<title>$title</title>\n";
$atom .= "<updated>". $date ."T$time+00:00</updated>\n";
$atom .= "<id>tag:rto.feed,2000:/$mode/$id</id>\n";
$atom .= "<link href=\"http://". $bb_cfg['server_name'] . $bb_cfg['script_path'] ."\" />\n";
foreach ($topics as $topic)
{
$topic_id = $topic['topic_id'];
$tor_size = '';
if (isset($topic['tor_size']))
{
$tor_size = str_replace('&nbsp;', ' ', ' ['. humn_size($topic['tor_size']) .']');
}
$topic_title = $topic['topic_title'];
$orig_word = array();
$replacement_word = array();
obtain_word_list($orig_word, $replacement_word);
if ( count($orig_word) )
{
$topic_title = preg_replace($orig_word, $replacement_word, $topic_title);
}
$topic_title = wbr($topic_title);
$author_name = ($topic['first_username']) ? wbr($topic['first_username']) : 'Гость';
$last_time = $topic['topic_last_post_time'];
if ($topic['topic_last_post_edit_time']) $last_time = $topic['topic_last_post_edit_time'];
$date = bb_date($last_time, 'Y-m-d', 0);
$time = bb_date($last_time, 'H:i:s', 0);
$updated = '';
$checktime = TIMENOW - 604800; // неделя
if ($topic['topic_first_post_edit_time'] && $topic['topic_first_post_edit_time'] > $checktime) $updated = '[Обновлено] ';
$atom .= "<entry>\n";
$atom .= " <title type=\"html\"><![CDATA[$updated$topic_title$tor_size]]></title>\n";
$atom .= " <author>\n";
$atom .= " <name>$author_name</name>\n";
$atom .= " </author>\n";
$atom .= " <updated>". $date ."T$time+00:00</updated>\n";
$atom .= " <id>tag:rto.feed,". $date .":/t/$topic_id</id>\n";
$atom .= " <link href=\"viewtopic.php?t=$topic_id\" />\n";
$atom .= "</entry>\n";
}
$atom .= "</feed>";
@unlink($file_path);
$fp = fopen($file_path, "w");
fwrite($fp, $atom);
fclose ($fp);
return true;
}