Questions from a beginner: porting custom coding into extension

Discussion forum for Extension Writers regarding Extension Development.
User avatar
[Dimetrodon]
Registered User
Posts: 486
Joined: Tue Aug 30, 2022 3:29 am
Location: Paleozoic Era

Questions from a beginner: porting custom coding into extension

Post by [Dimetrodon] »

Hello.

I am someone who hasn't really written an Extension before. Some custom coding in the php and html files, but never a full fledged extension.


Basically, I am trying to alter how search.php behaves when someone does not have permission to search the board and is not logged in. ie. presenting a login box to guests instead of the error massage.

The default code in search.php is this:

Code: Select all

// Is user able to search? Has search been disabled?
if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search'])
{
	$template->assign_var('S_NO_SEARCH', true);
	trigger_error('NO_SEARCH');
}
The custom code that does what I want is this:

Code: Select all

// Is user able to search? Has search been disabled?
if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search'])
{
	// Is the user logged in but unable to search? If so, they will get an error message.	 
	if ($user->data['user_id'] != ANONYMOUS)
	{			
	$template->assign_var('S_NO_SEARCH', true);
	 trigger_error('NO_SEARCH');
	}

	// If the user is a guest and cannot search, they will recieve a login page.                      
	login_box('', ((isset($user->lang['LOGIN_EXPLAIN_' . strtoupper($mode)])) ? $user->lang['LOGIN_EXPLAIN_' . strtoupper($mode)] : $user->lang['LOGIN_EXPLAIN_SEARCH']));
}
In addition to adding this to common.php:

Code: Select all

'LOGIN_EXPLAIN_SEARCH'			=> 'In order to search the board you have to be registered and logged in.',

This code does work, but basically, I am trying to convert this MOD to an Extension, as most people are certainly not going to want to edit core files and for good reason.

I am a beginner when it comes to making Extensions and I haven't had much luck making them in the past, especially since I do not think the needed php event exists for this and I do not have the skills required to make one.


---------

I'm making an attempt to create an event. Am I even close to doing what I need to do? I'm trying to bypass the default "not authorized" behavior as shown in the uppermost code box, but I have no experience and am probably doing a bunch of things wrong.

Code: Select all

$vars = array(
		'Auth',
		'config',
		'template',
		
);
extract($phpbb_dispatcher->trigger_event('core.search_modify_permissions_before', compact($vars)));
Avatar by Phoenix-of-Starlight.
rxu
Extensions Development Team
Posts: 3919
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation

Re: Questions from a beginner: porting custom coding into extension

Post by rxu »

You don't need any of those 3 variables in an event as you just inject those service dependencies into event listener.
What you actually would need is a new event before the // Is user able to search? Has search been disabled? code block with a new variable like $auth_check_override set to false by default, and then it's being added to the if line, so you can change it to true in the extension to circumvent the whole core code. So it'd look like f.e.

Code: Select all

$auth_check_override = false;
/**
* This event allows you to override serach auth checks
*
* @event core.search_auth_checks_override
* @var	bool	auth_check_override		Whether or not the search auth check overridden
* @since 3.3.14-RC1
*/
$vars = [
	'auth_check_override',
];
extract($phpbb_dispatcher->trigger_event('core.search_auth_checks_override', compact($vars)));

// Is user able to search? Has search been disabled?
if (!$auth_check_override && (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search']))
{
	$template->assign_var('S_NO_SEARCH', true);
	trigger_error('NO_SEARCH');
}

User avatar
[Dimetrodon]
Registered User
Posts: 486
Joined: Tue Aug 30, 2022 3:29 am
Location: Paleozoic Era

Re: Questions from a beginner: porting custom coding into extension

Post by [Dimetrodon] »

rxu wrote: Thu Oct 17, 2024 3:08 am You don't need any of those 3 variables in an event as you just inject those service dependencies into event listener.
Thank you for both explaining that and creating the Event. Will you do a pull request to make sure it is in phpBB 3.3.14-RC1?

I do not want to do it myself as it is your Event/code, not mine.
Avatar by Phoenix-of-Starlight.
rxu
Extensions Development Team
Posts: 3919
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation

Re: Questions from a beginner: porting custom coding into extension

Post by rxu »

Return to “Extension Writers Discussion”