MyBB Community Forums

Full Version: Delete plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I guys,
In my last plugins I added the ability to delete the plugin from the server. It removes all the files and folders inserted by the plugin. I think that it's a good thing to add to MyBB and that developers should start to use it.

This is the code:
/** 
 *
 * Plugin info
 * 
 * @return array Info
 */
function MyWhiteboard_info()
{
    global $mybb;
    $destroy = <<<EOT
<p>
    <a style="color: red; font-weight: bold" href="index.php?module=config-plugins&amp;action=deactivate&amp;uninstall=1&amp;destroy=1&amp;plugin=MyWhiteboard&amp;my_post_key={$mybb->post_code}">
        Delete Plugin
    </a>
</p>
EOT;
    
	return [
		'name'			=> "Plugin Title",
		'description'	=> "Plugin Desc".$destroy,
		'website'		=> "website_url",
		'author'		=> 'chack1172',
		'authorsite'	=> "author_url",
		'version'		=> 'version',
		'compatibility'	=> '18*',
		'codename'		=> 'MyWhiteboard'
	];
}

/** 
 *
 * Delete the plugin
 * 
 */
function MyWhiteboard_destroy()
{
    global $mybb, $message;
    
    if($mybb->input['destroy'] == 1)
    {
        // extra files and dirs to remove
        $extra_files = [
            "inc/languages/english/admin/MyWhiteboard.lang.php",
            "inc/languages/english/MyWhiteboard.lang.php",
            "inc/languages/italiano/admin/MyWhiteboard.lang.php",
            "inc/languages/italiano/MyWhiteboard.lang.php",
            "jscripts/spectrum/",
            "jscripts/excanvas.min.js",
            "jscripts/mywhiteboard.min.js",
            "jscripts/mywhiteboard.js",
        ];


        if(!empty($extra_files))
        {
            // remove extra files and dirs
            foreach($extra_files as $file)
            {
                if(!file_exists(MYBB_ROOT.$file))
                {
                    continue;
                }

                if(is_dir(MYBB_ROOT.$file))
                {
                    MyWhiteboard_rmdir(MYBB_ROOT.$file);
                } else {
                    unlink(MYBB_ROOT.$file);
                }
            }
        }
        // remove plugin file
        unlink(__FILE__);
        
        $message = "The selected plugin has been uninstalled and deleted successfully.";
    }
}

function MyWhiteboard_rmdir($dir)
{
    if(file_exists($dir) && is_dir($dir))
    {
        $files = array_diff(scandir($dir), ['.','..']);
        foreach($files as $file)
        {
          (is_dir($dir."/".$file)) ? MyWhiteboard_rmdir($dir."/".$file) : unlink($dir."/".$file);
        }
        return rmdir($dir); 
    } else {
        return true;
    }
}
unlink() and rmdir() both require appropriate user permissions; although your script may work most of the times, the operation's success relies on the actual file/folder permission.

Btw, I support this although it should be shifted to 2.0.
Why on deactivation though? Should be accessible if not installed/activated imo.
It is accessible if not installed/activated
The issue is that on most servers, the files are owned by the FTP user and so the apache user cannot manipulate the file, so it wouldn't be possible to delete them. I built this functionality into my Plugin Uploader plugin but to make it reliable it had to be done by entering FTP details.