Tutorial  Reduce attachments.php file resources usage.

Hi.

I use the attachment system quite often as an media system in forums. Take this site for example, I mainly use the system to manage images. I think it is unnecessary to load the whole global.php to serve its purpose, thus I decided to strip it out and only load what I find to be required for the script to properly work.

You can take a look to the attached images, basically the main visual difference should be the error system, which won't render the whole page, but users will be able to use the login form regardless.

This will save you from 2 queries (error load) up to 5 queries (successful load) by file load. If you take into account the number of loaded attachments by page load as n, you will save from n*2 to n*5 queries by page load. So, for example, if you are rendering 10 attached images in a page, you could save from 20 to 50 queries per page load.

This is enough for me to apply the changes.

Open attachment.php, and find:
require_once "./global.php";

Replace with:
$working_dir = dirname(__FILE__);
if(!$working_dir)
{
	$working_dir = '.';
}

$shutdown_queries = $shutdown_functions = array();

require_once $working_dir.'/inc/init.php';

$groupscache = $cache->read('usergroups');

if(!is_array($groupscache))
{
	$cache->update_usergroups();
	$groupscache = $cache->read('usergroups');
}

$current_page = my_strtolower(basename(THIS_SCRIPT));

if(isset($mybb->input['thumbnail']))
{
	define('NO_ONLINE', 1);
}

require_once MYBB_ROOT.'inc/class_session.php';
$session = new session;
$session->init();
$mybb->session = &$session;

$mybb->user['ismoderator'] = is_moderator(0, '', $mybb->user['uid']);

$mybb->post_code = generate_post_check();

if(isset($mybb->input['language']) && $lang->language_exists($mybb->get_input('language')) && verify_post_check($mybb->get_input('my_post_key'), true))
{
	$mybb->settings['bblanguage'] = $mybb->get_input('language');
	// If user is logged in, update their language selection with the new one
	if($mybb->user['uid'])
	{
		if(isset($mybb->cookies['mybblang']))
		{
			my_unsetcookie('mybblang');
		}

		$db->update_query('users', array('language' => $db->escape_string($mybb->settings['bblanguage'])), "uid = '{$mybb->user['uid']}'");
	}
	// Guest = cookie
	else
	{
		my_setcookie('mybblang', $mybb->settings['bblanguage']);
	}
	$mybb->user['language'] = $mybb->settings['bblanguage'];
}
else if(!$mybb->user['uid'] && !empty($mybb->cookies['mybblang']) && $lang->language_exists($mybb->cookies['mybblang']))
{
	$mybb->settings['bblanguage'] = $mybb->cookies['mybblang'];
}
else if(!isset($mybb->settings['bblanguage']))
{
	$mybb->settings['bblanguage'] = 'english';
}

$lang->set_language($mybb->settings['bblanguage']);

$lang->load('global');
$lang->load('messages');

if($mybb->cookies['lockoutexpiry'] && $mybb->cookies['lockoutexpiry'] < TIME_NOW)
{
	my_unsetcookie('lockoutexpiry');
}

Update: As of 1.8.23 the login form isn't displayed within the no permission page or container.
Edited 22 Jun, 2021, 11:05 pm by Omar G..
Files

attachmentserror.png (Size 5.82 KB / Downloads 609) attachmentserrornopermission.png (Size 22.16 KB / Downloads 612)
There are currently no posts to display. Be the first one to leave a reply.
Stream

« see more

New thread Omar G. 3 Apr, 2025, 6:26 am

Plugin Releases Plugin Contract Thread

New thread Omar G. 3 Apr, 2025, 6:02 am

Plugin Releases Plugin Extra Forum Permissions

New reply Omar G. 3 Apr, 2025, 4:34 am

Plugin Releases Plugin Forum Cleaner

New reply Omar G. 3 Apr, 2025, 3:59 am

Plugin Releases Plugin Lock Content

New thread Omar G. 3 Apr, 2025, 3:57 am

Plugin Releases Plugin Forum Cleaner

New thread Omar G. 3 Apr, 2025, 3:35 am

Plugin Releases Plugin Lock Content

New thread Omar G. 3 Apr, 2025, 2:16 am

Plugin Releases Paid Plugin Moderator Quick Buttons

New thread Omar G. 29 Mar, 2025, 1:44 am

Plugin Releases Paid Plugin Awards Rules

New thread Omar G. 27 Mar, 2025, 5:53 pm

Plugin Releases Plugin My2FA

New reply Omar G. 23 Mar, 2025, 10:57 am

Plugin Releases Plugin Awards



Current time: 17 Apr, 2025 11:57 am