MyBB Community Forums

Full Version: custom mycodes for admins and mods
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I made a plugin to check if the user is a mod or admin, and replace the [mod] tags with the proper html code, and do not do it if else.

Currently after install, it doesnt do anything to these tags. 
 <?php
// 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.");
}


$plugins->add_hook("parse_message", "team_mycode_run");

function customteamcodetags_info()
{
    return array(
        "name"            => "Custom Team MyCodes",
        "description"    => "Custom MyCodes for team members only",
        "website"        => "",
        "author"        => "metulburr",
        "authorsite"    => "",
        "version"        => "1.0",
        "guid"             =>  "",
        "compatibility" =>  "18*"
    );
}



function team_mycode_run($message)
{
global $mybb, $post;

$uid = intval($post['uid']);
 if ($uid == 4 || $uid == 3)
 {
  $message = preg_replace("#\[mod\](.*?)\[/mod\]#", '<code style="font-family: Monaco, \'Andale Mono\',  \'Courier New\', Courier, monospace; font-size: 1.0em; font-style: normal; line-height: 1.3em;           background: #FFF; padding: 0 3px; color: black; display: inline; background:lightgrey;">Moderator: $1</  code>', $message);
 }
 else
 {
  $message = preg_replace("#\[mod\](.*?)\[/mod\]#", '$1', $message);
 }
  return $message;
}
?>


The problem is that you are checking if the user's uid is 3 or 4. You should be checking the user's usergroup and additionalgroups.

Here is a function to handle permission checks that I use:

function my_user_permissions_check($good_groups)
{
	// array-ify the list if necessary
	if (!is_array($good_groups)) {
		$good_groups = explode(',', $good_groups);
	}

	// no groups = all groups
	if (empty($good_groups)) {
		return true;
	}

	// get all the user's groups in one array
	global $mybb;
	$users_groups = array($mybb->user['usergroup']);
	$adtl_groups = explode(',', $mybb->user['additionalgroups']);
	array_merge($users_groups, $adtl_groups);

	// !empty = true for allow and false for disallow
	return !empty(array_intersect($users_groups, $good_groups));
}

Usage:

if (my_user_permissions_check('3,4')) {
// yes
} else {
//no
}
Thanks for the response.

It works now...but it also works for everyone..all usergroups
<?php
// 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.");
}


$plugins->add_hook("parse_message", "team_mycode_run");

function customteamcodetags_info()
{
    return array(
        "name"            => "Custom Team MyCodes",
        "description"    => "Custom MyCodes for team members only",
        "website"        => "",
        "author"        => "metulburr",
        "authorsite"    => "",
        "version"        => "1.0",
        "guid"             =>  "",
        "compatibility" =>  "18*"
    );
}




function my_user_permissions_check($good_groups)
{
    // array-ify the list if necessary
    if (!is_array($good_groups)) {
        $good_groups = explode(',', $good_groups);
    }

    // no groups = all groups
    if (empty($good_groups)) {
        return true;
    }

    // get all the user's groups in one array
    global $mybb;
    $users_groups = array($mybb->user['usergroup']);
    $adtl_groups = explode(',', $mybb->user['additionalgroups']);
    array_merge($users_groups, $adtl_groups);

    // !empty = true for allow and false for disallow
    return !empty(array_intersect($users_groups, $good_groups));
} 



function team_mycode_run($message)
{
global $mybb, $post;

if (my_user_permissions_check('3,4')) {
// yes

  $message = preg_replace("#\[mod\](.*?)\[/mod\]#", '<code style="font-size: 1.0em; font-style: normal; line-height: 1.3em; background: #FFF; padding: 0 3px; color: black; display: inline; background:lightgrey;">Moderator: $1</code>', $message);
} else {
//no

  $message = preg_replace("#\[mod\](.*?)\[/mod\]#", '$1', $message);
} 

  return $message;
}
?>
I just tested on localhost and it works properly for me.
Thats weird. I made a regular user account, and that account is able to use those code tags as well as admins and global mods.
You have to call the groups of post author, not of logged in user.

Try this:
<?php
// 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.");
}


$plugins->add_hook("parse_message", "team_mycode_run");

function customteamcodetags_info()
{
 return array(
 "name" => "Custom Team MyCodes",
 "description" => "Custom MyCodes for team members only",
 "website" => "",
 "author" => "metulburr",
 "authorsite" => "",
 "version" => "1.0",
 "guid" => "",
 "compatibility" => "18*"
 );
}




function my_user_permissions_check($good_groups)
{
    // array-ify the list if necessary
 if (!is_array($good_groups)) {
 $good_groups = explode(',', $good_groups);
 }

    // no groups = all groups
 if (empty($good_groups)) {
 return true;
 }

    // get all the user's groups in one array
 global $post;
 $users_groups = array($post['usergroup']);
 $adtl_groups = explode(',', $post['additionalgroups']);
 array_merge($users_groups, $adtl_groups);

    // !empty = true for allow and false for disallow
 return !empty(array_intersect($users_groups, $good_groups));
} 



function team_mycode_run($message)
{
 global $mybb;

 if (my_user_permissions_check('3,4'))
 {
 // yes
 $message = preg_replace("#\[mod\](.*?)\[/mod\]#", '<code style="font-size: 1.0em; font-style: normal; line-height: 1.3em; background: #FFF; padding: 0 3px; color: black; display: inline; background:lightgrey;">Moderator: $1</code>', $message);
 }
 else
 {
 //no
 $message = preg_replace("#\[mod\](.*?)\[/mod\]#", '$1', $message);
 } 

 return $message;
}

Thank you that worked out very well. Smile

It also fixed the attachment issue i had by copy and pasting your version.  Link for more explanation
https://community.mybb.com/thread-208390.html
Not sure why? What is the difference between yours and mine that caused the attachment issue to be fixed?

Just trying to get more info on making plugins as i am interested in it.

EDIT:
another question regarding my replacement string in this plugin

if i do

[mod]test[/mod]

it works fine, but if i give it a newline it doesnt work at all

[mod]
test
[/mod]



I would of thought that

\[mod\](.*?)\[/mod\]

would of caught the newline too?

EDIT:
I just realized i dont think that is going to work. The point of the plugin was so we can modify a regular users post and input mod tags. And that only the mods could add them. But with this method it only allows on our (admins/mods) posts, not regular users posts.
Is there a way to modify this to make is so...
if user is admin or mod:
    allow to edit the post adding [mod] tag
else:
    allow to view the post with [mod] tag only

In that way only a mod/admin can edit a post to allow the insert of this tag, while at the same time regular users/mods/admins can see the output of that given mod tag inserted by a team member previously

So i am thinking regardless of the user, always change [mod] tags to their given HTML. Only check the user when editing the post and deny if not an admin/mod if they have a [mod] tag in it. I would assume this would also block users from editing their own posts after a mod added it into the post, but that is OK. 
Im not sure how to do this though?
I'm not trying to push my plugin on you, but with YourCode (link in my sig), you can create the same BB Code and select which groups can use it. Then the groups that can use the code can edit existing user's code and insert the BB Code.
(2017-03-23, 10:21 AM)Wildcard Wrote: [ -> ]I'm not trying to push my plugin on you, but with YourCode (link in my sig), you can create the same BB Code and select which groups can use it. Then the groups that can use the code can edit existing user's code and insert the BB Code.

Wow, i believe this might of fixed my issue completely. The only thing that would be a nice addition is if you already have custom MyCodes to convert them over to YourCode as well. But that is not a big deal as its a one time thing. 

Thanks!!!!!
Pages: 1 2