Setting a different/custom page for NOT_AUTHORISED error handle

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
Rogerjwilkinson
Registered User
Posts: 33
Joined: Wed Jan 17, 2018 2:42 pm

Setting a different/custom page for NOT_AUTHORISED error handle

Post by Rogerjwilkinson »

I've been at this for a while now. I've recently converted page to ajax/jquery for dynamic page loading, and as a side-effect, I now need the NOT_AUTHORISED error to EITHER, redirect to a blank page that ONLY says "You must be registered to view this area. Please press back and create an account" (ideal) or, alternatively, at least parse the "Please log in" page with no header and footer. Following code is to protect external pages integrated with sessions, found all over the place here

Code: Select all

<?php if(!$user->data['is_registered'])
{
    if ($user->data['user_id'] != ANONYMOUS)
    {
        trigger_error('NOT_AUTHORISED');
    }

    login_box('', $user->lang['NOT_AUTHORISED']);
}
?>

Now I tried all sorts of variations of

Code: Select all

<?php if(!$user->data['is_registered'])
{
    if ($user->data['user_id'] != ANONYMOUS)
    {
        trigger_error("You must be registered to view this area. Please press back and create an account", E_ALL);
    }
 ?>


But as I understand it these do not work because all of the error_handlers are already defined by phpbb itself (either in functions.php or common.php?). I found this page, that discusses very close my exact issue.

https://wiki.phpbb.com/Talk:Function.trigger_error

" If you want to just halt the script and output something, you would use E_USER_ERROR, but you can also specify your own error handler, or define('HEADER_INC'); which will avoid calling page_header() within trigger error. If you seek additional assistance, please post a topic in the MOD Writers Discussion Forum."

Could somebody please help me make a bit more sense of that so I can try to sort this out. Thank you very much.


edit:

So I found this in includes/functions.php

Code: Select all

/**
* Generate page header
*/
function page_header($page_title = '', $display_online_list = false, $item_id = 0, $item = 'forum', $send_headers = true)
{
	global $db, $config, $template, $SID, $_SID, $_EXTRA_URL, $user, $auth, $phpEx, $phpbb_root_path;
	global $phpbb_dispatcher, $request, $phpbb_container, $phpbb_admin_path;

	if (defined('HEADER_INC'))
	{
		return;
	}

	define('HEADER_INC', true);

but I don't understand, how can I use any of that to make NOT_AUTHORISED page return without header? ty
Rogerjwilkinson
Registered User
Posts: 33
Joined: Wed Jan 17, 2018 2:42 pm

Re: Setting a different/custom page for NOT_AUTHORISED error handle

Post by Rogerjwilkinson »

I tried setting all true to false but no luck

Code: Select all

   set_error_handler("myErrorHandler");
 
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if ($errstr == "PLEASE_REGISTER") {
    header("Location: website.com/noaccess.php");
    die();
    }
}
   
if(!$user->data['is_registered'])
{
    if ($user->data['user_id'] != ANONYMOUS)
    {
        trigger_error("PLEASE_REGISTER")
    }
   
}

I've been working with this, but I think it has to be applied to phpbb (functions.php maybe?) Can anybody guide me in doing this please.
Last edited by Rogerjwilkinson on Mon Jan 22, 2018 1:10 pm, edited 1 time in total.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Setting a different/custom page for NOT_AUTHORISED error handle

Post by mrgoldy »

As far as I can tell there are two ways.

You can use the simple_header.html, which is often used in modals.

Code: Select all

if ($user->data['user_id'] == ANONYMOUS)
{
	$template->assign_var('S_SIMPLE_MESSAGE', true);
	trigger_error('My awesome message.');
}
or use the define:

Code: Select all

if ($user->data['user_id'] == ANONYMOUS)
{
	define('HEADER_INC', true));
	trigger_error('My awesome message.');
}
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Setting a different/custom page for NOT_AUTHORISED error handle

Post by Kailey »

The msg_handler() function has a line where it sets the template file to load (link). I don't think trigger_error() allows you to set a defined template file, so you may have to edit the code a bit.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
Rogerjwilkinson
Registered User
Posts: 33
Joined: Wed Jan 17, 2018 2:42 pm

Re: Setting a different/custom page for NOT_AUTHORISED error handle

Post by Rogerjwilkinson »

You are so awesome man. The second method didn't work at all (my fault I'm sure), but the first method is "almost" perfect. This is how it displays the output on the page

"{"MESSAGE_TITLE":"Information","MESSAGE_TEXT":"You must create an account to see this area.","S_USER_WARNING":false,"S_USER_NOTICE":true,"REFRESH_DATA":null}"

With the following code

Code: Select all

if(!$user->data['is_registered'])
{
if ($user->data['user_id'] == ANONYMOUS)
{
	$template->assign_var('S_SIMPLE_MESSAGE', true);
	trigger_error('You must create an account to see this area.');
}

}
I had a peeksie around in simple_header. and simple_footer.html but didn't see anything that stood out in either.
Rogerjwilkinson
Registered User
Posts: 33
Joined: Wed Jan 17, 2018 2:42 pm

Re: Setting a different/custom page for NOT_AUTHORISED error handle

Post by Rogerjwilkinson »

You legends!!

Changing

Code: Select all

				$json_response->send(array(
					'MESSAGE_TITLE'		=> $msg_title,
					'MESSAGE_TEXT'		=> $msg_text,
					'S_USER_WARNING'	=> ($errno == E_USER_WARNING) ? true : false,
					'S_USER_NOTICE'		=> ($errno == E_USER_NOTICE) ? true : false,
					'REFRESH_DATA'		=> (!empty($refresh_data)) ? $refresh_data : null
				));
to

Code: Select all

				$json_response->send;
in functions.php, with an ever so slight modification to simple_header and simple_footer, and it's now perfect! :D Thank you so much both of you, could NOT have done this without you. ^^
Post Reply

Return to “phpBB Custom Coding”