-
Notifications
You must be signed in to change notification settings - Fork 20
/
flash_news.php
158 lines (142 loc) · 3.86 KB
/
flash_news.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
*
* @package Icy Phoenix
* @version $Id$
* @copyright (c) 2008 Icy Phoenix
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
define('IN_ICYPHOENIX', true);
if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './');
if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
include(IP_ROOT_PATH . 'common.' . PHP_EXT);
// Options - BEGIN
// Number of items
$news_items = 10;
// Items type: 'news' or 'topics'
$news_type = 'news';
// Recent: true or false (if set to false then random items will be selected)
$news_recent = true;
// Cache: select true only if you want to use cached version of selected items
$news_cache = false;
// Cache file
$news_cache_file = MAIN_CACHE_FOLDER . 'flash_news_data.xml';
// Cache frequency
$news_cache_freq = 86400;
// Base address
$news_base_address = create_server_url();
// Viewtopic address
$news_base_url = CMS_PAGE_VIEWTOPIC;
// Options - END
/*
* Build_allowed_forums_list: needed to build a list of forum with read access
*/
function flash_build_allowed_forums_list()
{
$allowed_forums = '';
$forum_types = array(FORUM_POST);
$forums_array = get_forums_ids($forum_types, true, false, true, true);
foreach ($forums_array as $forum)
{
$allowed_forums .= (empty($allowed_forums) ? '' : ',') . $forum['forum_id'];
}
return $allowed_forums;
}
/*
* Output xml header
*/
function xml_header($time)
{
if(!empty($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache/2'))
{
header('Cache-Control: no-cache, pre-check=0, post-check=0, max-age=0');
}
else
{
header('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
}
header('Last-Modified: ' . $time);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
header('Content-Type: text/xml; charset=UTF-8');
}
/*
* Output xml header
*/
function xml_file_content($file_content)
{
$xml_content = '';
$xml_content .= '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml_content .= '<news>' . "\n";
$xml_content .= $file_content . "\n";
$xml_content .= '</news>' . "\n";
return $xml_content;
}
// Cache - BEGIN
$use_cache = false;
if($news_cache && @file_exists($news_cache_file))
{
$cache_file_time = @filemtime($news_cache_file);
$cache_refresh_time = ($cache_file_time + $news_cache_freq);
if($cache_refresh_time > time())
{
$use_cache = true;
}
}
// Cache - END
if($use_cache)
{
$time = gmdate('D, d M Y H:i:s', $cache_file_time) . ' GMT';
xml_header($time);
readfile($news_cache_file);
}
else
{
$allowed_forums = flash_build_allowed_forums_list();
if ($news_recent)
{
$sql_sort = "p.post_time DESC";
}
else
{
$sql_sort = "rand()";
}
$sql_news = '';
if ($news_type == 'news')
{
$sql_news = "AND t.news_id > 0";
}
$sql = "SELECT t.topic_id, t.topic_title, t.topic_first_post_id, u.user_id, u.username, p.post_time
FROM " . TOPICS_TABLE . " AS t, " . USERS_TABLE . " AS u, " . POSTS_TABLE . " AS p
WHERE t.forum_id IN (" . $allowed_forums . ")
AND u.user_id = t.topic_poster
AND p.post_id = t.topic_first_post_id
" . $sql_news . "
ORDER BY " . $sql_sort . "
LIMIT " . $news_items;
$result = ($news_recent ? $db->sql_query($sql, 0, 'posts_flash_', POSTS_CACHE_FOLDER) : $db->sql_query($sql));
$xml_content = '';
while($row = $db->sql_fetchrow($result))
{
$topic_title = strip_tags($row['topic_title']);
$topic_author = $row['username'];
$topic_user_id = $row['user_id'];
$news_url = $news_base_address . $news_base_url . '?' . POST_TOPIC_URL . '=' . $row['topic_id'];
$xml_content .= '<item url="' . $news_url . '">' . $topic_title . '</item>' . "\n";
}
$time = gmdate('D, d M Y H:i:s', time()) . ' GMT';
$xml_content = xml_file_content($xml_content);
if($news_cache)
{
if($f = @fopen($news_cache_file, 'w'))
{
@fwrite($f, $xml_content, strlen($xml_content));
@fclose($f);
}
}
xml_header($time);
echo($xml_content);
}
$db->sql_close();
exit;
?>