<?php
/* by Tomasz 'Devilshakerz' Mlynski [devilshakerz.com]; Copyright (C) 2012
 released under Creative Commons BY-NC-SA 3.0 license: http://creativecommons.org/licenses/by-nc-sa/3.0/ */

$plugins->add_hook('global_end', 'dvz_sb_init');
$plugins->add_hook('xmlhttp', 'dvz_sb_xmlhttp');
$plugins->add_hook('index_end', 'dvz_shoutbox');

function dvz_shoutbox_info ()
{
    return array(
        'name'           => 'DVZ Shoutbox',
        'description'    => 'Lightweight AJAX chat',
        'website'        => 'http://devilshakerz.com/',
        'author'         => 'Tomasz \'Devilshakerz\' Młyński',
        'authorsite'     => 'http://devilshakerz.com/',
        'version'        => '1.0',
        'guid'           => '',
        'compatibility'  => '16*',
    );
}

function dvz_shoutbox_is_installed () {
		global $db;
		$query = $db->simple_select('settinggroups', '*', 'name=\'dvz_shoutbox\'');
        return $db->num_rows($query);
}

function dvz_shoutbox_install () {
    global $db;

    // table
    $db->write_query("CREATE TABLE IF NOT EXISTS `".TABLE_PREFIX."dvz_shoutbox` (
      `id` int(11) NOT NULL auto_increment,
      `uid` int(11) NOT NULL,
      `text` text NOT NULL,
      `date` int(11) NOT NULL,
      `ip` varchar(15) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM");

    $db->write_query("INSERT INTO ".TABLE_PREFIX."dvz_shoutbox VALUES (NULL, 1, 'DVZ Shoutbox!', ".time().", '127.0.0.1')");

    // settings
    $db->write_query("INSERT INTO `".TABLE_PREFIX."settinggroups` VALUES (NULL, 'dvz_shoutbox', 'DVZ Shoutbox', 'Settings for DVZ Shoutbox.', 1, 0)");
    $sgID = $db->insert_id();

    $db->write_query("INSERT INTO `".TABLE_PREFIX."settings` VALUES
        (NULL, 'dvz_sb_num', 'Shouts to display', 'Number of last posted shouts displayed in the Shoutbox window.', 'text', '20', 1, $sgID, 0),
        (NULL, 'dvz_sb_num_archive', 'Shouts on archive view', 'Shouts to display per page on archive view.', 'text', '15', 2, $sgID, 0),
        (NULL, 'dvz_sb_height', 'Shoutbox height', 'Height of the Shoutbox window in pixels.', 'text', '200', 3, $sgID, 0),
        (NULL, 'dvz_sb_interval', 'Refresh interval', 'Maximum number of seconds before new posted shouts are displayed in the window (lower values provide better synchronization, although cause higher server load).', 'text', '5', 4, $sgID, 0),
        (NULL, 'dvz_sb_away', 'Away mode', 'Number of seconds after last user action (e.g. click) after which auto-refreshing will be disabled to prevent unnecessary usage of server resources. Set 0 to disable.', 'text', '600', 5, $sgID, 0),
        (NULL, 'dvz_sb_antiflood', 'Anti-flood protection', 'Minimum number of seconds before user can post next shout (this does not apply to moderators).', 'text', '5', 6, $sgID, 0),
        (NULL, 'dvz_sb_minposts', 'Minimum posts to shout', 'Set 0 to allow everyone.', 'text', '0', 7, $sgID, 0),
        (NULL, 'dvz_sb_blocked_users', 'Banned users', 'Comma-separated list of user IDs that are banned from posting.', 'textarea', '', 8, $sgID, 0),
        (NULL, 'dvz_sb_groups_view', 'Groups access', 'Comma-separated list of user groups that can view Shoutbox. Leave unfilled to let everyone view (including guests).', 'text', '', 9, $sgID, 0),
        (NULL, 'dvz_sb_groups_post', 'Groups post access', 'Comma-separated list of user groups that can post messages in Shoutbox. Leave unfilled to let everyone post (excluding guests).', 'text', '', 10, $sgID, 0),
        (NULL, 'dvz_sb_groups_mod', 'Moderator groups', 'Comma-separated list of users groups that can moderate the Shoutbox (edit and delete messages).', 'text', '', 11, $sgID, 0),
        (NULL, 'dvz_sb_supermods', 'Supermods as Shoutbox moderators', 'Automatically allow board super moderators to moderate Shoutbox either.', 'yesno', '1', 12, $sgID, 0)
    ");

    rebuild_settings();

    // templates
    $template_shoutbox = '
<div class="tablehead">Shoutbox<p class="right"><a href="{$mybb->settings[bburl]}/index.php?action=shoutbox_archive">&laquo; {$lang->dvz_sb_archivelink}</a></p></div>
<div id="shoutbox">
<div class="panel-away" style="display:none"><button class="button" onclick="dvz_shoutbox.setBack()">{$lang->dvz_sb_away_button}</button>{$lang->dvz_sb_away}</div>
{$dvz_shoutbox_panel}
<div id="shoutbox_data" style="height:{$mybb->settings[dvz_sb_height]}px; overflow:auto;"></div>
</div>

<script type="text/javascript" src="jscripts/dvz_shoutbox.js"></script>
<script>
now = new Date;
dvz_shoutbox.userLastAction = now.getTime();
dvz_shoutbox.interval = {$mybb->settings[dvz_sb_interval]};
dvz_shoutbox.antiflood = {$mybb->settings[dvz_sb_antiflood]};
dvz_shoutbox.maxShouts = {$mybb->settings[dvz_sb_num]};
dvz_shoutbox.awayTimeout = {$mybb->settings[dvz_sb_away]}*1000;
dvz_shoutbox.lang = [\'{$lang->dvz_sb_delete_confirm}\', \'{$lang->dvz_sb_antiflood}\'];
{$dvz_shoutbox_js}
dvz_shoutbox.checkForMessages();
</script>';

    $template_archive = '<html>
<head>
<title>{$lang->dvz_sb_archive}</title>
{$headerinclude}
</head>
<body>
{$header}

<script type="text/javascript" src="jscripts/dvz_shoutbox.js"></script>
{$dvz_shoutbox_js}

{$multipage}

<div class="tablehead">{$lang->dvz_sb_archive}</div>
<div id="shoutbox">
{$messagesList}
</div>
<br />

{$multipage}

{$footer}
</body>
</html>';

    $db->write_query("INSERT INTO `".TABLE_PREFIX."templates` VALUES (NULL, 'dvz_shoutbox', '".$db->escape_string($template_shoutbox)."', '-1', '1', '', '".time()."')");
    $db->write_query("INSERT INTO `".TABLE_PREFIX."templates` VALUES (NULL, 'dvz_shoutbox_archive', '".$db->escape_string($template_archive)."', '-1', '1', '', '".time()."')");

}

function dvz_shoutbox_activate () {
}

function dvz_shoutbox_deactivate () {
}

function dvz_shoutbox_uninstall () {
    global $db;

    $sgID = $db->simple_select('settinggroups', 'gid', 'name=\'dvz_shoutbox\'');
    $sgID = $db->fetch_field($sgID, 'gid');

    $db->delete_query('settinggroups', 'name=\'dvz_shoutbox\'');
    $db->delete_query('settings', 'gid='.$sgID);
    $db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title IN('dvz_shoutbox', 'dvz_shoutbox_archive')");
    $db->query("DROP TABLE ".TABLE_PREFIX.'dvz_shoutbox');
}

function dvz_sb_init () {
    global $mybb, $lang;
    $lang->load("dvz_shoutbox");
    if ($mybb->input['action'] == 'shoutbox_archive') {
        return dvz_sb_show_archive();
    }
}

function dvz_shoutbox ($page_data) {
    global $templates, $dvz_shoutbox, $lang, $mybb;

    $dvz_shoutbox = null;
    $dvz_shoutbox_js = null;
    $dvz_shoutbox_panel = null;

    if (dvz_sb_access_view()) {
        if ($mybb->user['uid']) {
            if (dvz_sb_user_blocked()) {
                $dvz_shoutbox_panel = '<div class="panel-blocked">'.$lang->dvz_sb_user_blocked.'</div>';
            }
            elseif (!dvz_sb_access_minposts() && !dvz_sb_access_mod()) {
                $text = str_replace('{MINPOSTS}', $mybb->settings['dvz_sb_minposts'], $lang->dvz_sb_minposts);
                $dvz_shoutbox_panel = '<div class="panel-minposts">'.$text.'</div>';
            }
            else if (dvz_sb_access_post()) {
                $dvz_shoutbox_panel = '<div class="panel2"><form onsubmit="dvz_shoutbox.shout();return false"><input type="text" id="shout_text" placeholder="'.$lang->dvz_sb_default.'" autocomplete="off" /><input type="submit" value="'.$lang->dvz_sb_button.' &raquo;" id="shout_button" /><img src="'.$mybb->settings['bburl'].'/images/loading-mini.gif" style="display:none" id="dvz_sb_loading" /></form></div>';
            }
            if (dvz_sb_access_mod()) {
                $dvz_shoutbox_js = 'dvz_shoutbox.godMode = true;';
            }
        }

        eval('$dvz_shoutbox = "'.$templates->get('dvz_shoutbox').'";');
    }
}

function dvz_sb_xmlhttp () {
    global $mybb, $lang, $charset;

    $lang->load("dvz_shoutbox");

    header('Content-type: text/html; charset='.$charset);

    switch ($mybb->input['action']) {
        case 'dvz_sb_check':
            if (!dvz_sb_access_view()) exit;
            dvz_sb_xmlhttp_get_messages(intval($mybb->input['from']));
            exit;
        break;
        case 'dvz_sb_post':
        	if (!verify_post_check($mybb->input['key'], true)) exit;
            echo dvz_sb_shout($mybb->input['text']);
            exit;
        break;
        case 'dvz_sb_edit_get':
            if (!dvz_sb_access_mod()) exit;
            if (!verify_post_check($mybb->input['key'], true)) exit;
            echo dvz_sb_get(intval($mybb->input['id']));
            exit;
        break;
        case 'dvz_sb_edit_save':
            if (!dvz_sb_access_mod()) exit;
            if (!verify_post_check($mybb->input['key'], true)) exit;
            dvz_sb_update(intval($mybb->input['id']), $mybb->input['text']);
            exit;
        break;
    }
}

function dvz_sb_xmlhttp_get_messages ($from=0) {
    global $mybb;

    $entries = 0;

    $data = dvz_sb_messages("WHERE s.id > $from ORDER by s.id DESC LIMIT {$mybb->settings[dvz_sb_num]}", true);

    if (!empty($data['html'])) echo json_encode($data);
}

function dvz_sb_show_archive () {
    global $db, $mybb, $templates, $lang, $footer, $headerinclude, $header, $charset;

    if (!dvz_sb_access_view()) return false;

    if (dvz_sb_access_mod()) {
        $dvz_shoutbox_js = '<script>dvz_shoutbox.godMode = true;</script>';

        if ($mybb->input['delete']) {
    		if (verify_post_check($mybb->input['key'], true))
    		{
    			dvz_sb_delete($mybb->input['delete']);
    		}
        }
    }

    header('Content-type: text/html; charset='.$charset);

    $messages = $db->simple_select('dvz_shoutbox', 'COUNT(*) as messages');
    $messages = $db->fetch_field($messages, 'messages');

    $pageNum = intval($mybb->input['page']);
    $perPage = $mybb->settings['dvz_sb_num_archive'];
    $pages = ceil($messages / $perPage);

    if (!$pageNum || $pageNum < 1 || $pageNum > $pages) $pageNum = 1;

    $start = ($pageNum - 1) * $perPage;

    if ($messages > $perPage) {
        $multipage = multipage($messages, $perPage, $pageNum, 'index.php?action=shoutbox_archive');
    }
    add_breadcrumb($lang->dvz_sb_shoutbox, "index.php?action=shoutbox_archive");

    $messagesList = dvz_sb_messages("ORDER by s.id DESC LIMIT $start,$perPage", false, 'archive');

    eval('$content = "'.$templates->get("dvz_shoutbox_archive").'";');

    output_page($content);
    exit;
}

function dvz_sb_messages ($queryClauses, $dataArray=false, $mode=false) {
    global $db, $mybb, $lang;

    require_once MYBB_ROOT.'inc/class_parser.php';
    $parser = new postParser;

    $html = '';
    $lastMessageID = 0;

    // , u.avatar
    $query = $db->query("
        SELECT s.*, u.username, u.usergroup, u.displaygroup, u.avatar
        FROM ".TABLE_PREFIX."dvz_shoutbox s LEFT JOIN ".TABLE_PREFIX."users u ON u.uid = s.uid 
        $queryClauses
    ");

    while ($entry = $db->fetch_array($query)) {
        $parser_options = array(
                'allow_mycode' => 1,
                'allow_smilies' => 1,
                'allow_imgcode' => 0,
                'me_username' => $entry['username']
        );

        $messageID = $entry['id'];
        $message = $parser->parse_message($entry['text'], $parser_options);
        $avatarURL = empty($entry['avatar']) ? 'images/default_avatar.gif' : $entry['avatar'];
        $avatarIMG = '<img src="'.$avatarURL.'" class="avatar" alt="avatar" />';
        $usernameFormatted = format_name($entry['username'], $entry['usergroup'], $entry['displaygroup']);
        $username = '<a href="member.php?action=profile&uid='.intval($entry['uid']).'">'.$avatarIMG.' '.$usernameFormatted.'</a>';
        $date_time = my_date('d/m H:i', $entry['date']);

        $insertArrow = '<span title="'.$lang->dvz_sb_insert_name.'" class="insUser" onclick="dvz_shoutbox.insertUsername(\''.$entry['username'].'\')">&raquo;</span>';

        if (dvz_sb_access_mod()) {
            if ($mode == 'archive') {
                $modOptions = '<a href="javascript:dvz_shoutbox.edit('.$messageID.')" title="'.$lang->dvz_sb_edit.'" class="mod">E</a> <a onclick="return confirm(\''.$lang->dvz_sb_delete_confirm.'\')" href="index.php?action=shoutbox_archive&delete='.$messageID.'&key='.$mybb->post_code.'" title="'.$lang->dvz_sb_delete.'" class="mod dvz_sb_del">X</a>';
            } else {
                $modOptions = '<a href="javascript:dvz_shoutbox.edit('.$messageID.')" title="'.$lang->dvz_sb_edit.'" class="mod">E</a> <a onclick="return dvz_shoutbox.delConfirm(this)" href="index.php?action=shoutbox_archive&delete='.$messageID.'" title="'.$lang->dvz_sb_delete.'" class="mod dvz_sb_del">X</a>';
            }
        }

        $class = alt_trow();

        $html .= "<div class='sbentry $class' id='shout-$messageID'><p class='userdata'>$insertArrow $username:</p><p class='info'><span class='date'>$date_time</span>$modOptions</p><p class='text'>$message</p><div class='clear'></div></div>\r\n";

        if ($lastMessageID == 0) $lastMessageID = $messageID;
    }

    return $dataArray ?
        array (
            'html' => $html,
            'last_id' => $lastMessageID
        )
    : $html;

}

// actions
function dvz_sb_shout ($text) {
    global $db, $mybb;

    if (!dvz_sb_access_post()) return false;

    if ($mybb->settings['dvz_sb_antiflood'] && !dvz_sb_access_mod()) {
        $lastShoutTime = $db->fetch_field(
            $db->simple_select('dvz_shoutbox', 'date', 'uid='.$mybb->user['uid'], array(
                'order_by' => 'date',
                'order_dir' => 'desc',
                'limit' => 1
        )), 'date');
        $timeBetween = time() - $lastShoutTime;
        if ($timeBetween <= $mybb->settings['dvz_sb_antiflood']) die('antiflood');
    }

    $shout_data = array(
            'uid' => $mybb->user['uid'],
            'text' => $db->escape_string($text),
            'date' => time(),
            'ip' => get_ip()
    );
    $db->insert_query('dvz_shoutbox', $shout_data);

    exit;
}
function dvz_sb_get ($id) {
    global $db;
    return $db->fetch_field( $db->simple_select('dvz_shoutbox', 'text', 'id='.intval($id)) , 'text');
}
function dvz_sb_update ($id, $text) {
    global $db;
    return $db->update_query('dvz_shoutbox', array('text' => $text), 'id='.intval($id));
}
function dvz_sb_delete ($id) {
    global $db;
    return $db->delete_query('dvz_shoutbox', 'id='.intval($id));
}

// permissions
function dvz_sb_access_view () {
    global $mybb;
    $allowedGroups = dvz_sb_getcsv('groups_view');
    return (
        empty($allowedGroups) ||
        dvz_sb_memberOf($allowedGroups)
    );
}
function dvz_sb_access_post () {
    global $mybb;
    $allowedGroups = dvz_sb_getcsv('groups_post');
    return (
        !($mybb->user['usergroup'] == 1 && $mybb->user['uid'] < 1) &&
        (
            dvz_sb_access_mod() ||
            (
                dvz_sb_access_view() &&
                !dvz_sb_user_blocked() &&
                dvz_sb_access_minposts() &&
                (
                    empty($allowedGroups) ||
                    dvz_sb_memberOf($allowedGroups)
                )
            )
        )
    );
}
function dvz_sb_access_mod () {
    global $mybb;
    $allowedGroups = dvz_sb_getcsv('groups_mod');
    return (
        dvz_sb_memberOf($allowedGroups) ||
        ($mybb->settings['dvz_sb_supermods'] && $mybb->usergroup['issupermod'])
    );
}
function dvz_sb_user_blocked () {
    global $mybb;
    $blockedUsers = dvz_sb_getcsv('blocked_users');
    return in_array($mybb->user['uid'], $blockedUsers);
}
function dvz_sb_access_minposts () {
    global $mybb;
    return $mybb->user['postnum'] >= $mybb->settings['dvz_sb_minposts'];
}

// library
function dvz_sb_memberOf ($groupsArray) {
    global $mybb, $dvz_sb_userGroups;
    if (!isset($dvz_sb_userGroups)) {
        $dvz_sb_userGroups = explode(',', $mybb->user['additionalgroups']);
        $dvz_sb_userGroups[] = $mybb->user['usergroup'];
    }
    return array_intersect($dvz_sb_userGroups, $groupsArray);
}
function csv2array ($input){
    if (function_exists('str_getcsv')) return str_getcsv($input);

    $fields = explode(',', $input);
    if (!$fields) $fields = array($input);

    return($fields);
} 
function dvz_sb_getcsv ($name) {
    global $mybb;
    $groups = csv2array($mybb->settings['dvz_sb_'.$name]);
    if (count($groups) == 1 && $groups[0] == '') return Array();
    return $groups;
}

?>
