· 6 years ago · Apr 26, 2019, 09:56 PM
1<?php
2/**
3 * MyBB 1.8
4 *
5 * Top Story
6 * Created by: danielheywood
7 *
8 */
9
10// Make sure we can't access this file directly from the browser.
11if(!defined('IN_MYBB'))
12{
13 die('This file cannot be accessed directly.');
14}
15
16// cache templates - this is important when it comes to performance
17// THIS_SCRIPT is defined by some of the MyBB scripts, including index.php
18if(defined('THIS_SCRIPT'))
19{
20 global $templatelist;
21
22 if(isset($templatelist))
23 {
24 $templatelist .= ',';
25 }
26
27 if(THIS_SCRIPT== 'index.php')
28 {
29 $templatelist .= 'index_topstory, index_topstory_topstory';
30 }
31}
32
33if(defined('IN_ADMINCP'))
34{
35 // Add our topstory_settings() function to the setting management module to load language strings.
36 $plugins->add_hook('admin_config_settings_manage', 'topstory_settings');
37 $plugins->add_hook('admin_config_settings_change', 'topstory_settings');
38 $plugins->add_hook('admin_config_settings_start', 'topstory_settings');
39 // We could hook at 'admin_config_settings_begin' only for simplicity sake.
40}
41else
42{
43 // Add our index_topstory() function to the index_start hook so when that hook is run our function is executed
44 $plugins->add_hook('index_start', 'index_topstory');
45}
46
47function topstory_info()
48{
49 /**
50 * Array of information about the plugin.
51 * name: The name of the plugin
52 * description: Description of what the plugin does
53 * website: The website the plugin is maintained at (Optional)
54 * author: The name of the author of the plugin
55 * authorsite: The URL to the website of the author (Optional)
56 * version: The version number of the plugin
57 * compatibility: A CSV list of MyBB versions supported. Ex, '121,123', '12*'. Wildcards supported.
58 * codename: An unique code name to be used by updated from the official MyBB Mods community.
59 */
60 return array(
61 'name' => 'Top Story',
62 'description' => '',
63 'website' => 'http://danielheywood.co.uk',
64 'author' => 'danielheywood',
65 'authorsite' => 'http://danielheywood.co.uk',
66 'version' => '1.0',
67 'compatibility' => '18*',
68 'codename' => 'topstory'
69 );
70}
71
72/*
73 * _activate():
74 * Called whenever a plugin is activated via the Admin CP. This should essentially make a plugin
75 * 'visible' by adding templates/template changes, language changes etc.
76*/
77function topstory_activate()
78{
79 global $db;
80
81 // Add a new template (topstory) to our global templates (sid = -1)
82 $templatearray = array(
83 'index_topstory' => '<section id="top_story">
84 <table cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}">
85 <thead>
86 <tr>
87 <td width="459">
88 Top Story
89 </td>
90 <td width="284">
91 What\'s New
92 </td>
93 </tr>
94 </thead>
95 <tbody>
96 <tr>
97 <td>
98 <img src="images/top_story.gif" alt="Top Story" title="Top Story" />
99 </td>
100 <td>
101 <div>
102 {$topstories}
103 <a href="{$mybb->settings[\'bburl\']}/forumdisplay.php?fid={$fid}" class="button float_right">Read more<span class="arrow">></span></a>
104 </div>
105 </td>
106 </tr>
107 </tbody>
108 </table>
109</section>',
110 'index_topstory_topstory' => '<strong>[{$dateline}]</strong>
111<a href="{$mybb->settings[\'bburl\']}/showthread.php?tid={$id}">{$subject}</a>
112<br />
113{$message}
114<br />
115<br />'
116 );
117
118 // Create templates.
119 foreach($templatearray as $name => $code)
120 {
121 $template = array(
122 'title' => $db->escape_string($name),
123 'template' => $db->escape_string($code),
124 'version' => '1',
125 'sid' => '-2',
126 'dateline' => TIME_NOW
127 );
128
129 // Create
130 $db->insert_query('templates', $template);
131
132 // Remove this template from the earlier queried list.
133 unset($templates[$name]);
134 }
135
136 // This is required so it updates the settings.php file as well and not only the database - they must be synchronized!
137 rebuild_settings();
138
139 // Include this file because it is where find_replace_templatesets is defined
140 require_once MYBB_ROOT . '/inc/adminfunctions_templates.php';
141
142 // Edit the index template and add our variable to above {$forums}
143 find_replace_templatesets('index', '#'.preg_quote('{$forums}').'#', "{\$topstory}\n{\$forums}");
144}
145
146/*
147 * _deactivate():
148 * Called whenever a plugin is deactivated. This should essentially 'hide' the plugin from view
149 * by removing templates/template changes etc. It should not, however, remove any information
150 * such as tables, fields etc - that should be handled by an _uninstall routine. When a plugin is
151 * uninstalled, this routine will also be called before _uninstall() if the plugin is active.
152*/
153function topstory_deactivate()
154{
155 global $db;
156
157 // Delete templates belonging to template groups.
158 $db->delete_query('templates', 'title=\'index_topstory\' OR title LIKE \'index_topstory_%\'');
159
160 // Include this file because it is where find_replace_templatesets is defined
161 require_once MYBB_ROOT.'inc/adminfunctions_templates.php';
162
163 // Remove template edits
164 find_replace_templatesets('index', '#'.preg_quote('{$topstory}').'#', '');
165}
166
167/*
168 * _install():
169 * Called whenever a plugin is installed by clicking the 'Install' button in the plugin manager.
170 * If no install routine exists, the install button is not shown and it assumed any work will be
171 * performed in the _activate() routine.
172*/
173function topstory_install()
174{
175 global $db;
176
177 $settinggroups = array(
178 'name' => 'topstory',
179 'title' => 'Top Story Settings',
180 'description' => '',
181 'disporder' => 1,
182 'isdefault' => 0
183 );
184
185 $gid = $db->insert_query('settinggroups', $settinggroups);
186
187 $settings[] = array(
188 'name' => 'topstory_display',
189 'title' => 'Do you want to see Top Story?',
190 'description' => '',
191 'optionscode' => 'yesno',
192 'disporder' => 1,
193 'value' => 1,
194 'gid' => $gid
195 );
196
197 $settings[] = array(
198 'name' => 'topstory_forum',
199 'title' => 'Which Forums do you want to see shown on Top Story?',
200 'description' => 'The ID of the forums that you want to show. Separate ID by commas.',
201 'optionscode' => 'text',
202 'disporder' => 2,
203 'value' => '',
204 'gid' => $gid
205 );
206
207 $settings[] = array(
208 'name' => 'topstory_limit',
209 'title' => 'How many Top Stories do you want to see?',
210 'description' => '',
211 'optionscode' => 'text',
212 'disporder' => 3,
213 'value' => 5,
214 'gid' => $gid
215 );
216
217 $db->insert_query_multiple('settings', $settings);
218
219 // This is required so it updates the settings.php file as well and not only the database - they must be synchronized!
220 rebuild_settings();
221}
222
223/*
224 * _is_installed():
225 * Called on the plugin management page to establish if a plugin is already installed or not.
226 * This should return TRUE if the plugin is installed (by checking tables, fields etc) or FALSE
227 * if the plugin is not installed.
228*/
229function topstory_is_installed()
230{
231 global $db;
232 $query = $db->simple_select('settinggroups', '*', 'name=\'topstory\'');
233
234 // If the rows exists then it means the plugin is installed because we only drop it on uninstallation
235 return $db->num_rows($query);
236}
237
238/*
239 * _uninstall():
240 * Called whenever a plugin is to be uninstalled. This should remove ALL traces of the plugin
241 * from the installation (tables etc). If it does not exist, uninstall button is not shown.
242*/
243function topstory_uninstall()
244{
245 global $db;
246
247 // Delete templates belonging to template groups.
248 $db->delete_query('templates', 'title=\'index_topstory\' OR title LIKE \'index_topstory_%\'');
249
250 // Delete settings group
251 $db->delete_query('settinggroups', 'name=\'topstory\'');
252
253 // Remove the settings
254 $db->delete_query('settings', 'name IN (\'topstory_display','topstory_forum','topstory_limit\')');
255
256 // This is required so it updates the settings.php file as well and not only the database - they must be synchronized!
257 rebuild_settings();
258}
259
260/*
261 * Loads the settings language strings.
262*/
263function topstory_settings()
264{
265
266}
267
268/*
269 * Displays the list of stories on index - depending on the setting of course.
270*/
271function index_topstory()
272{
273 global $mybb;
274
275 // Only run this function is the setting is set to yes
276 if($mybb->settings['topstory_display'] == 0)
277 {
278 return;
279 }
280
281 global $db, $templates, $topstories, $topstory;
282
283 $topstory_forum = '';
284
285 if($mybb->settings['topstory_forum'])
286 {
287 $topstory_forum .= ' AND t.fid IN (' . $mybb->settings['topstory_forum'] . ') ';
288 }
289
290 require_once MYBB_ROOT.'inc/functions_search.php';
291
292 $unsearchforums = get_unsearchable_forums();
293
294 if($unsearchforums)
295 {
296 $topstory_forum .= ' AND t.fid IN ($unsearchforums)';
297 }
298
299 $inactiveforums = get_inactive_forums();
300
301 if($inactiveforums)
302 {
303 $topstory_forum .= ' AND t.fid IN ($inactiveforums)';
304 }
305
306 $permissions = forum_permissions();
307
308 for($i = 0; $i <= sizeof($permissions); $i++)
309 {
310 if(isset($permissions[$i]['fid']) && ( $permissions[$i]['canview'] == 0 || $permissions[$i]['canviewthreads'] == 0 ))
311 {
312 $topstory_forum .= ' AND t.fid <> '.$permissions[$i]['fid'];
313 }
314 }
315
316 $topstory_forum .= ' AND p.visible <> -1';
317
318 // Retreive all the stories from the database
319 $story = '';
320 $topstory_limit = (int) $mybb->settings['topstory_limit'];
321 $query = $db->query("
322 SELECT p.*, u.username AS userusername, u.usergroup, u.displaygroup, lp.usergroup AS lastusergroup, lp.displaygroup as lastdisplaygroup, p.visible
323 FROM ".TABLE_PREFIX."posts p
324 LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
325 LEFT JOIN ".TABLE_PREFIX."users lp ON (p.uid=lp.uid)
326 LEFT JOIN ".TABLE_PREFIX."threads t ON (p.tid=t.tid)
327 WHERE 1 = 1 {$topstory_forum}
328 ORDER BY p.dateline DESC
329 LIMIT $topstory_limit
330 ");
331 while($story = $db->fetch_array($query))
332 {
333 // htmlspecialchars_uni is similar to PHP's htmlspecialchars but allows unicode
334 $id = $story['tid'];
335 $fid = $story['fid'];
336 $subject = htmlspecialchars_uni($story['subject']);
337 $message = preg_replace('/((\w+\W*){'.(5-1).'}(\w+))(.*)/', '${1}', htmlspecialchars_uni($story['message'])).'...';
338 $dateline = my_date('d-m-y', $story['lastpost']);
339
340 $topstories .= eval($templates->render('index_topstory_topstory'));
341 }
342
343 // Set $topstory as our template and use eval() to do it so we can have our variables parsed
344 $topstory = eval($templates->render('index_topstory'));
345}
346
347?>