Thread Rating:
  • 4 Vote(s) - 4.75 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Upgrade? And other Hi-Tech Scams
#72
.
.
Apologies for posting during the workweek while Punaweb repairs are ongoing but it relates directly to the upgrade. Please accept this donation of tweak-able code along with step by step instructions to implement the much needed improvements.

This code will limit each user to 3 posts per day from midnight to midnight, max 500 words per post, allows editing for only 15 minutes after posting, and auto refreshes every 10 seconds.

_______________________________________________________________________________

Step 1: Create the database table

Go into phpMyAdmin, select the punaweb database, click the SQL tab, paste the code below, and click Go:CREATE TABLE IF NOT EXISTS mybb_chat_messages (
  mid int(10) unsigned NOT NULL AUTO_INCREMENT,
  uid int(10) unsigned NOT NULL,
  username varchar(255) NOT NULL,
  message text NOT NULL,
  dateline int(10) unsigned NOT NULL,
  ipaddress varchar(45) NOT NULL,
  edited tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (mid),
  KEY uid (uid),
  KEY dateline (dateline)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Step 2: Create the chat.php file

Create a new file named exactly chat.php and paste the entire code below into it. Then upload it to the root folder of the forum (same place as index.php): define('IN_MYBB', 1);
require_once './global.php';

if (!$mybb->user['uid']) {
    error_no_permission();
}

$action = $mybb->get_input('action');

$page_title = "Punaweb Live Chatroom";
add_breadcrumb("Live Chatroom", "chat.php");

switch ($action) {
    case 'post':
        post_message();
        break;
    case 'edit':
        edit_message();
        break;
    case 'delete':
        delete_message();
        break;
    case 'get_messages':
        get_messages_ajax();
        break;
    default:
        show_chatroom();
        break;
}

function show_chatroom() {
    global $mybb, $templates, $header, $headerinclude, $footer, $db;

    $messages_html = get_messages_html();

    $daily_count = get_daily_post_count($mybb->user['uid']);
    $can_post = ($daily_count < 3);
    $remaining = 3 - $daily_count;

    $post_form = $can_post
        ? eval($templates->render('chat_post_form'))
        : "

You have reached your limit of 3 posts today. Limit resets at midnight.

";

    $auto_refresh_js = '
    <script>
        let lastDateline = ' . TIME_NOW . ';
        function refreshChat() {
            fetch("chat.php?action=get_messages&last=" + lastDateline, {cache: "no-store"})
                .then(r => r.text())
                .then(html => {
                    if (html.trim() !== "") {
                        document.getElementById("chat-messages").innerHTML = html;
                        lastDateline = Math.floor(Date.now() / 1000);
                    }
                });
        }
        setInterval(refreshChat, 10000);
    </script>';

    eval('$chatroom = "' . $templates->render('chat_main') . '";');
    output_page($chatroom);
}

function get_messages_html($last = 0) {
    global $mybb, $db;
    $where = $last > 0 ? "WHERE dateline > " . (int)$last : "";
    $query = $db->simple_select("chat_messages", "*", $where, [
        "order_by" => "dateline", "order_dir" => "DESC", "limit" => 100
    ]);

    $html = '';
    while ($msg = $db->fetch_array($query)) {
        $edit = $delete = '';
        $time_since = TIME_NOW - $msg['dateline'];

        if ($msg['uid'] == $mybb->user['uid']) {
            if ($time_since <= 900) $edit = " • Edit";
            $delete = " • Delete";
        }

        $username = htmlspecialchars_uni($msg['username']);
        $message = nl2br(htmlspecialchars_uni($msg['message']));
        $time = my_date('relative', $msg['dateline']);

        $html .= "
            {$username} ({$time})

            {$message}

            {$edit}{$delete}
       

";
    }
    return $html ?: '

No messages yet — be the first!

';
}

function get_messages_ajax() {
    global $mybb;
    $last = $mybb->get_input('last', MyBB::INPUT_INT);
    echo get_messages_html($last);
    exit;
}

function post_message() {
    global $mybb, $db;
    verify_post_check($mybb->get_input('my_post_key'));

    if (get_daily_post_count($mybb->user['uid']) >= 3) {
        error("You can only post 3 times per day.");
    }

    $message = trim($mybb->get_input('message'));
    if (str_word_count($message) > 500) error("Maximum 500 words per post.");
    if (empty($message)) error("Message cannot be empty.");

    $db->insert_query('chat_messages', [
        'uid'      => $mybb->user['uid'],
        'username'  => $db->escape_string($mybb->user['username']),
        'message'  => $db->escape_string($message),
        'dateline'  => TIME_NOW,
        'ipaddress' => $db->escape_string($mybb->user['ipaddress'])
    ]);

    redirect("chat.php", "Message posted.");
}

function edit_message() {
    global $mybb, $db, $templates;
    $mid = $mybb->get_input('mid', MyBB::INPUT_INT);
    $msg = $db->fetch_array($db->simple_select("chat_messages", "*", "mid = {$mid}"));

    if (!$msg || $msg['uid'] != $mybb->user['uid']) error("Not your message.");
    if (TIME_NOW - $msg['dateline'] > 900) error("15-minute edit window has expired.");

    if ($mybb->request_method == "post") {
        verify_post_check($mybb->get_input('my_post_key'));
        $new = trim($mybb->get_input('message'));
        if (str_word_count($new) > 500) error("Maximum 500 words.");

        $db->update_query('chat_messages', [
            'message' => $db->escape_string($new),
            'edited'  => 1
        ], "mid = {$mid}");

        redirect("chat.php", "Message edited.");
    } else {
        $message_text = htmlspecialchars_uni($msg['message']);
        eval('$page = "' . $templates->render('chat_edit') . '";');
        output_page($page);
    }
}

function delete_message() {
    global $mybb, $db;
    $mid = $mybb->get_input('mid', MyBB::INPUT_INT);
    $msg = $db->fetch_array($db->simple_select("chat_messages", "uid", "mid = {$mid}"));
    if (!$msg || $msg['uid'] != $mybb->user['uid']) error("Not your message.");

    $db->delete_query('chat_messages', "mid = {$mid}");
    redirect("chat.php", "Message deleted.");
}

function get_daily_post_count($uid) {
    global $db;
    $today_start = strtotime("today midnight");
    return (int)$db->fetch_field($db->simple_select("chat_messages", "COUNT(*) as cnt", "uid = " . (int)$uid . " AND dateline >= " . $today_start), 'cnt');
}
?>

Step 3: Add the three templates

Go to Admin CP → Templates & Style → Templates → [Your Theme] and create these new templates one by one:Template Name: chat_main
Paste this code: {$mybb->settings['bbname']} - {$page_title}
{$headerinclude}
<style>
.chat-message { margin:12px 0; padding:10px; background:#f9f9f9; border:1px solid #ddd; border-radius:6px; }
.time { color:#666; font-size:0.9em; }
#chat-messages { max-height:650px; overflow-y:auto; }
</style>
{$auto_refresh_js}
{$header}
Punaweb Live Chatroom (auto-refreshes every 10 seconds)
   
{$messages_html}
   

    {$post_form}
{$footer}

Template Name: chat_post_form
Paste this code:
   
   

   
    You have {$remaining} post(s) left today.

Template Name: chat_edit
Paste this code: Edit Message - {$mybb->settings['bbname']}
{$headerinclude}
{$header}
   
   

   
{$footer}

The last Step:
After adding the templates, go to Templates & Style → Templates → [Your Theme], click the options gear icon at the top, and choose Refresh Templates.The chatroom should now be available at:
https://punaweb.org/chat.php

You can add a link to it in the navigation menu or in a forum thread when you're ready.
.
.
Reply


Messages In This Thread
Upgrade? And other Hi-Tech Scams - by HiloJulie - 03-19-2026, 10:38 PM
RE: Upgrade? - by Moderator 2 - 03-19-2026, 10:44 PM
RE: Upgrade? - by MyManao - 03-19-2026, 11:36 PM
RE: Upgrade? - by ironyak - 03-19-2026, 11:15 PM
RE: Upgrade? - by randomq - 03-19-2026, 11:56 PM
RE: Upgrade? - by Wao nahele wahine - 03-20-2026, 02:15 AM
RE: Upgrade? - by HereOnThePrimalEdge - 03-20-2026, 12:13 AM
RE: Upgrade? - by ironyak - 03-20-2026, 12:25 AM
RE: Upgrade? - by randomq - 03-20-2026, 12:25 AM
RE: Upgrade? - by HereOnThePrimalEdge - 03-20-2026, 12:46 AM
RE: Upgrade? - by HiloJulie - 03-20-2026, 01:52 AM
RE: Upgrade? - by ironyak - 03-20-2026, 06:09 AM
RE: Upgrade? - by TomK - 03-20-2026, 07:07 AM
RE: Upgrade? - by ironyak - 03-20-2026, 09:07 AM
RE: Upgrade? - by randomq - 03-20-2026, 05:36 PM
RE: Upgrade? - by HiloJulie - 03-20-2026, 05:52 PM
RE: Upgrade? - by MyManao - 03-20-2026, 05:57 PM
RE: Upgrade? - by randomq - 03-20-2026, 05:52 PM
RE: Upgrade? - by randomq - 03-20-2026, 06:29 PM
RE: Upgrade? - by ironyak - 03-20-2026, 06:53 PM
RE: Upgrade? - by Moderator 2 - 03-22-2026, 02:19 AM
RE: Upgrade? - by TomK - 03-22-2026, 07:12 AM
RE: Upgrade? - by ironyak - 03-22-2026, 06:25 PM
RE: Upgrade? - by MyManao - 03-23-2026, 10:40 PM
RE: Upgrade? - by ironyak - 03-24-2026, 04:22 AM
RE: Upgrade? - by randomq - 03-25-2026, 12:57 AM
RE: Upgrade? - by kalianna - 03-24-2026, 12:15 AM
RE: Upgrade? - by HiloJulie - 03-24-2026, 05:23 AM
RE: Upgrade? - by ironyak - 03-24-2026, 05:07 PM
RE: Upgrade? - by HiloJulie - 03-24-2026, 05:57 PM
RE: Upgrade? - by ironyak - 03-24-2026, 06:36 PM
RE: Upgrade? - by HiloJulie - 03-24-2026, 07:14 PM
RE: Upgrade? - by ironyak - 03-24-2026, 10:13 PM
RE: Upgrade? - by HereOnThePrimalEdge - 03-24-2026, 07:27 PM
RE: Upgrade? - by HiloJulie - 03-24-2026, 08:14 PM
RE: Upgrade? - by HiloJulie - 03-24-2026, 11:03 PM
RE: Upgrade? - by ironyak - 03-25-2026, 02:48 AM
RE: Upgrade? - by MyManao - 03-25-2026, 04:12 AM
RE: Upgrade? - by HereOnThePrimalEdge - 03-24-2026, 11:04 PM
RE: Upgrade? - by HiloJulie - 03-24-2026, 11:21 PM
RE: Upgrade? - by HereOnThePrimalEdge - 03-25-2026, 12:45 AM
RE: Upgrade? - by HiloJulie - 03-25-2026, 03:46 AM
RE: Upgrade? - by ironyak - 03-25-2026, 05:14 PM
RE: Upgrade? - by HiloJulie - 03-25-2026, 05:39 AM
RE: Upgrade? - by MyManao - 03-25-2026, 05:53 AM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 03-25-2026, 07:23 PM
RE: Upgrade? And other Hi-Tech Scams - by randomq - 03-25-2026, 06:52 PM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 03-26-2026, 06:21 AM
RE: Upgrade? And other Hi-Tech Scams - by randomq - 03-26-2026, 01:58 AM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 03-27-2026, 06:00 PM
RE: Upgrade? And other Hi-Tech Scams - by randomq - 03-26-2026, 10:00 PM
RE: Upgrade? And other Hi-Tech Scams - by MyManao - 03-27-2026, 06:46 AM
RE: Upgrade? And other Hi-Tech Scams - by randomq - 03-28-2026, 01:01 AM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 03-28-2026, 05:50 PM
RE: Upgrade? And other Hi-Tech Scams - by MyManao - 03-28-2026, 09:15 PM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 03-29-2026, 05:04 PM
RE: Upgrade? And other Hi-Tech Scams - by kalakoa - 03-29-2026, 03:44 PM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 03-29-2026, 04:00 PM
RE: Upgrade? And other Hi-Tech Scams - by Chas - 03-29-2026, 05:42 PM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 03-29-2026, 10:03 PM
RE: Upgrade? And other Hi-Tech Scams - by Punatang - 03-30-2026, 05:35 PM
RE: Upgrade? And other Hi-Tech Scams - by MyManao - 03-30-2026, 05:50 PM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 03-30-2026, 07:54 PM
RE: Upgrade? And other Hi-Tech Scams - by Chas - 03-30-2026, 08:48 PM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 03-31-2026, 01:58 AM
RE: Upgrade? And other Hi-Tech Scams - by randomq - 03-30-2026, 09:45 PM
RE: Upgrade? And other Hi-Tech Scams - by ironyak - 04-05-2026, 06:27 AM
RE: Upgrade? And other Hi-Tech Scams - by Chas - 04-05-2026, 07:25 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)