PHP Code:
<?php
/**
* MyBB 1.8
*
* Top Story
*
*/// Make sure we can't access this file directly from the browser.
if(!defined('IN_MYBB'))
{
die('This file cannot be accessed directly.');
}// cache templates - this is important when it comes to performance
// THIS_SCRIPT is defined by some of the MyBB scripts, including index.php
if(defined('THIS_SCRIPT'))
{
global $templatelist; if(isset($templatelist))
{
$templatelist .= ',';
} if(THIS_SCRIPT== 'index.php')
{
$templatelist .= 'index_topstory, index_topstory_topstory';
}
}if(defined('IN_ADMINCP'))
{
// Add our topstory_settings() function to the setting management module to load language strings.
$plugins->add_hook('admin_config_settings_manage', 'topstory_settings');
$plugins->add_hook('admin_config_settings_change', 'topstory_settings');
$plugins->add_hook('admin_config_settings_start', 'topstory_settings');
// We could hook at 'admin_config_settings_begin' only for simplicity sake.
}
else
{
// Add our index_topstory() function to the index_start hook so when that hook is run our function is executed
$plugins->add_hook('index_start', 'index_topstory');
}function topstory_info()
{
/**
* Array of information about the plugin.
* name: The name of the plugin
* description: Description of what the plugin does
* website: The website the plugin is maintained at (Optional)
* author: The name of the author of the plugin
* authorsite: The URL to the website of the author (Optional)
* version: The version number of the plugin
* compatibility: A CSV list of MyBB versions supported. Ex, '121,123', '12*'. Wildcards supported.
* codename: An unique code name to be used by updated from the official MyBB Mods community.
*/
return array(
'name' => 'Top Story',
'description' => '',
'website' => '',
'author' => '',
'authorsite' => '',
'version' => '1.0',
'compatibility' => '18*',
'codename' => 'topstory'
);
}/*
* _activate():
* Called whenever a plugin is activated via the Admin CP. This should essentially make a plugin
* 'visible' by adding templates/template changes, language changes etc.
*/
function topstory_activate()
{
global $db;
// Add a new template (topstory) to our global templates (sid = -1)
$templatearray = array(
'index_topstory' => '<section id="top_story">
<table cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}">
<thead>
<tr>
<td width="459">
Top Story
</td>
<td width="284">
What\'s New
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<img src="http://forum.ragezone.com/images/top_story.gif" alt="Top Story" title="Top Story" />
</td>
<td>
<div>
{$topstories}
<a href="{$mybb->settings[\'bburl\']}/forumdisplay.php?fid={$fid}" class="button float_right">Read more<span class="arrow">></span></a>
</div>
</td>
</tr>
</tbody>
</table>
</section>',
'index_topstory_topstory' => '<strong>[{$dateline}]</strong>
<a href="{$mybb->settings[\'bburl\']}/showthread.php?tid={$id}">{$subject}</a>
<br />
{$message}
<br />
<br />'
); // Create templates.
foreach($templatearray as $name => $code)
{
$template = array(
'title' => $db->escape_string($name),
'template' => $db->escape_string($code),
'version' => '1',
'sid' => '-2',
'dateline' => TIME_NOW
);
// Create
$db->insert_query('templates', $template);
// Remove this template from the earlier queried list.
unset($templates[$name]);
} // This is required so it updates the settings.php file as well and not only the database - they must be synchronized!
rebuild_settings(); // Include this file because it is where find_replace_templatesets is defined
require_once MYBB_ROOT . '/inc/adminfunctions_templates.php'; // Edit the index template and add our variable to above {$forums}
find_replace_templatesets('index', '#'.preg_quote('{$forums}').'#', "{\$topstory}\n{\$forums}");
}/*
* _deactivate():
* Called whenever a plugin is deactivated. This should essentially 'hide' the plugin from view
* by removing templates/template changes etc. It should not, however, remove any information
* such as tables, fields etc - that should be handled by an _uninstall routine. When a plugin is
* uninstalled, this routine will also be called before _uninstall() if the plugin is active.
*/
function topstory_deactivate()
{
global $db; // Delete templates belonging to template groups.
$db->delete_query('templates', 'title=\'index_topstory\' OR title LIKE \'index_topstory_%\''); // Include this file because it is where find_replace_templatesets is defined
require_once MYBB_ROOT.'inc/adminfunctions_templates.php'; // Remove template edits
find_replace_templatesets('index', '#'.preg_quote('{$topstory}').'#', '');
}/*
* _install():
* Called whenever a plugin is installed by clicking the 'Install' button in the plugin manager.
* If no install routine exists, the install button is not shown and it assumed any work will be
* performed in the _activate() routine.
*/
function topstory_install()
{
global $db;
$settinggroups = array(
'name' => 'topstory',
'title' => 'Top Story Settings',
'description' => '',
'disporder' => 1,
'isdefault' => 0
); $gid = $db->insert_query('settinggroups', $settinggroups); $settings[] = array(
'name' => 'topstory_display',
'title' => 'Do you want to see Top Story?',
'description' => '',
'optionscode' => 'yesno',
'disporder' => 1,
'value' => 1,
'gid' => $gid
); $settings[] = array(
'name' => 'topstory_forum',
'title' => 'Which Forums do you want to see shown on Top Story?',
'description' => 'The ID of the forums that you want to show. Separate ID by commas.',
'optionscode' => 'text',
'disporder' => 2,
'value' => '',
'gid' => $gid
); $settings[] = array(
'name' => 'topstory_limit',
'title' => 'How many Top Stories do you want to see?',
'description' => '',
'optionscode' => 'text',
'disporder' => 3,
'value' => 5,
'gid' => $gid
); $db->insert_query_multiple('settings', $settings); // This is required so it updates the settings.php file as well and not only the database - they must be synchronized!
rebuild_settings();
}/*
* _is_installed():
* Called on the plugin management page to establish if a plugin is already installed or not.
* This should return TRUE if the plugin is installed (by checking tables, fields etc) or FALSE
* if the plugin is not installed.
*/
function topstory_is_installed()
{
global $db;
$query = $db->simple_select('settinggroups', '*', 'name=\'topstory\''); // If the rows exists then it means the plugin is installed because we only drop it on uninstallation
return $db->num_rows($query);
}/*
* _uninstall():
* Called whenever a plugin is to be uninstalled. This should remove ALL traces of the plugin
* from the installation (tables etc). If it does not exist, uninstall button is not shown.
*/
function topstory_uninstall()
{
global $db; // Delete templates belonging to template groups.
$db->delete_query('templates', 'title=\'index_topstory\' OR title LIKE \'index_topstory_%\''); // Delete settings group
$db->delete_query('settinggroups', 'name=\'topstory\''); // Remove the settings
$db->delete_query('settings', 'name IN (\'topstory_display','topstory_forum','topstory_limit\')'); // This is required so it updates the settings.php file as well and not only the database - they must be synchronized!
rebuild_settings();
}/*
* Loads the settings language strings.
*/
function topstory_settings()
{}/*
* Displays the list of stories on index - depending on the setting of course.
*/
function index_topstory()
{
global $mybb; // Only run this function is the setting is set to yes
if($mybb->settings['topstory_display'] == 0)
{
return;
} global $db, $templates, $topstories, $topstory; $topstory_forum = ''; if($mybb->settings['topstory_forum'])
{
$topstory_forum .= ' AND t.fid IN (' . $mybb->settings['topstory_forum'] . ') ';
} require_once MYBB_ROOT.'inc/functions_search.php'; $unsearchforums = get_unsearchable_forums(); if($unsearchforums)
{
$topstory_forum .= ' AND t.fid IN ($unsearchforums)';
} $inactiveforums = get_inactive_forums(); if($inactiveforums)
{
$topstory_forum .= ' AND t.fid IN ($inactiveforums)';
} $permissions = forum_permissions(); for($i = 0; $i <= sizeof($permissions); $i++)
{
if(isset($permissions[$i]['fid']) && ( $permissions[$i]['canview'] == 0 || $permissions[$i]['canviewthreads'] == 0 ))
{
$topstory_forum .= ' AND t.fid <> '.$permissions[$i]['fid'];
}
} $topstory_forum .= ' AND p.visible <> -1'; // Retreive all the stories from the database
$story = '';
$topstory_limit = (int) $mybb->settings['topstory_limit'];
$query = $db->query("
SELECT p.*, u.username AS userusername, u.usergroup, u.displaygroup, lp.usergroup AS lastusergroup, lp.displaygroup as lastdisplaygroup, p.visible
FROM ".TABLE_PREFIX."posts p
LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
LEFT JOIN ".TABLE_PREFIX."users lp ON (p.uid=lp.uid)
LEFT JOIN ".TABLE_PREFIX."threads t ON (p.tid=t.tid)
WHERE 1 = 1 {$topstory_forum}
ORDER BY p.dateline DESC
LIMIT $topstory_limit
");
while($story = $db->fetch_array($query))
{
// htmlspecialchars_uni is similar to PHP's htmlspecialchars but allows unicode
$id = $story['tid'];
$fid = $story['fid'];
$subject = htmlspecialchars_uni($story['subject']);
$message = preg_replace('/((\w+\W*){'.(5-1).'}(\w+))(.*)/', '${1}', htmlspecialchars_uni($story['message'])).'...';
$dateline = my_date('d-m-y', $story['lastpost']);
$topstories .= eval($templates->render('index_topstory_topstory'));
} // Set $topstory as our template and use eval() to do it so we can have our variables parsed
$topstory = eval($templates->render('index_topstory'));
}?>