<?php
/* wowitem.php -- MyBB plugin for querying World of Warcraft item databases
version 1.0.0, February 13th, 2009
http://www.ravenguard.ca/wowitem/
Copyright (C) 2009 Daniel Major
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Daniel Major dmajor@gmail.com
*/
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
// the MyCode tag(s) to parse for, separated by |
define(WOWITEM_MYCODE, 'item|wowitem');
define(WOWITEM_HEADER, '<script type="text/javascript" src="http://www.wowhead.com/widgets/power.js"></script>');
$plugins->add_hook("parse_message", "wowitem_parse");
function wowitem_info()
{
return array(
"name" => "WoW Item Links",
"description" => "Queries online databases to display World of Warcraft item data in posts.",
"website" => "http://www.ravenguard.ca/wowitem/",
"author" => "Bakdu",
"authorsite" => "mailto:bakdu@ravenguard.ca",
"version" => "1.0.0",
"guid" => "15fdaaa7e4e5d0f12defedf033f7c4b7",
"compatibility" => "14*"
);
}
function wowitem_install()
{
global $mybb, $db;
// create cache table
$db->write_query("CREATE TABLE ".TABLE_PREFIX."wowitem (
cache_key varchar(255) NOT NULL,
fetched int unsigned NOT NULL,
id int NOT NULL,
name varchar(255) NOT NULL,
quality int NOT NULL,
icon varchar(255) NOT NULL,
link varchar(255) NOT NULL,
PRIMARY KEY (cache_key)
);");
// delete settings that were not uninstalled cleanly
$db->delete_query('settings', "name LIKE 'wowitem_%'");
$db->delete_query('settinggroups', "name = 'wowitem'");
// add settings group
$query = $db->simple_select('settinggroups', "MAX(disporder) AS max_disporder");
$disporder = $db->fetch_field($query, 'max_disporder') + 1;
$gid = $db->insert_query('settinggroups', array(
'name' => 'wowitem',
'title' => 'WoW Item Links',
'description' => $db->escape_string("Options to configure the WoW Item Links plugin."),
'disporder' => $disporder,
'isdefault' => 0
));
// add settings
$db->insert_query('settings', array(
'name' => 'wowitem_db',
'title' => 'Item Database',
'description' => $db->escape_string("Choose the provider to query for item data.<br />\n<br />\nIf set to disabled, no more requests will be sent to external sites but the locally cached items will still show."),
'optionscode' => "radio\nwowhead=Wowhead\ndisabled=Disabled",
'value' => 'wowhead',
'disporder' => 1,
'gid' => $gid
));
$db->insert_query('settings', array(
'name' => 'wowitem_cached',
'title' => 'Enable Cache',
'description' => $db->escape_string("If the cache is enabled, the item data will be stored in the database. This reduces the number of external requests and allows links to work during provider downtime but adds to the database size."),
'optionscode' => "yesno",
'value' => '1',
'disporder' => 2,
'gid' => $gid
));
$db->insert_query('settings', array(
'name' => 'wowitem_cache_refresh',
'title' => 'Cache Refresh',
'description' => $db->escape_string("The number of minutes before the cached data is considered stale and an external request is made to refresh the data. Set to 0 to disable refreshing."),
'optionscode' => "text",
'value' => '1440',
'disporder' => 3,
'gid' => $gid
));
rebuild_settings();
}
function wowitem_is_installed()
{
global $mybb, $db;
return $db->table_exists('wowitem');
}
function wowitem_uninstall()
{
global $mybb, $db;
// remove settings
$db->delete_query('settings', "name LIKE 'wowitem_%'");
$db->delete_query('settinggroups', "name = 'wowitem'");
rebuild_settings();
// drop cache
$db->drop_table('wowitem');
}
function wowitem_activate()
{
require_once MYBB_ROOT.'/inc/adminfunctions_templates.php';
find_replace_templatesets('headerinclude', '#{\$newpmmsg}#', WOWITEM_HEADER . "\n{\$newpmmsg}");
}
function wowitem_deactivate()
{
require_once MYBB_ROOT.'/inc/adminfunctions_templates.php';
find_replace_templatesets('headerinclude', '#' . preg_quote(WOWITEM_HEADER) . '\n#', '', 0);
}
function wowitem_parse($message)
{
$mycodes = explode('|', WOWITEM_MYCODE);
foreach ($mycodes as $mycode)
{
$message = preg_replace('#\['.$mycode.'\](.*?)\[/'.$mycode.'\]#ie', "wowitem_item('\$1')", $message);
}
}
function wowitem_item($data)
{
global $mybb, $db;
// fetch from cache or provider
$stale = 0;
$cache = wowitem_cache_query('item', $data);
if ($cache)
{
$cache_refresh = intval($mybb->settings['wowitem_cache_refresh']) * 60;
$stale = ($cache_refresh > 0) && ($cache['fetched'] < TIME_NOW - $cache_refresh);
}
if (!$cache || $stale)
{
eval("\$item = wowitem_" . $mybb->settings['wowitem_db'] . "_query('item', \$data);");
if ($item && $mybb->settings['wowitem_cached'] == 1)
{
if ($stale)
{
$db->update_query('wowitem', array(
'fetched' => $item['fetched'],
'id' => $item['id'],
'name' => $db->escape_string($item['name']),
'quality' => $item['quality'],
'icon' => $db->escape_string($item['icon']),
'link' => $db->escape_string($item['link']),
), "cache_key = '" . $db->escape_string($data) . "'");
}
else
{
$db->insert_query('wowitem', array(
'cache_key' => $db->escape_string($data),
'fetched' => $item['fetched'],
'id' => $item['id'],
'name' => $db->escape_string($item['name']),
'quality' => $item['quality'],
'icon' => $db->escape_string($item['icon']),
'link' => $db->escape_string($item['link']),
));
}
}
}
if (!$item)
{
$item = $cache;
}
// no cached data and error with provider, make up a null item
if (!$item)
{
$item = array(
'id' => 0,
'name' => $data,
'quality' => 0,
'icon' => '',
'link' => 'javascript:void(0);',
'fetched' => TIME_NOW
);
}
// HTML is generated, customize here if desired
return '<a class="q' . $item['quality'] . '" href="' . $item['link'] . '"><b>[' . $item['name'] . ']</b></a>';
}
function wowitem_cache_query($type, $data)
{
global $mybb, $db;
if ($mybb->settings['wowitem_cached'] == 1)
{
$query = $db->simple_select('wowitem', 'id, name, quality, icon, link, fetched', "cache_key = '" . $db->escape_string($data) . "'");
$item = $db->fetch_array($query);
if (isset($item['id']))
{
return $item;
}
}
return false;
}
function wowitem_disabled_query($type, $data)
{
return false;
}
function wowitem_wowhead_query($type, $data)
{
$url = 'http://www.wowhead.com/?' . $type . '=' . urlencode($data) . '&xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if ($response)
{
$xml = simplexml_load_string($response);
if (!$xml->error) {
return array(
'id' => intval($xml->item['id']),
'name' => $xml->item->name,
'quality' => $xml->item->quality['id'],
'icon' => $xml->item->icon,
'link' => $xml->item->link,
'fetched' => TIME_NOW
);
}
}
return false;
}
?>
|