trigger_error() acting strange

Discussion forum for Extension Writers regarding Extension Development.
User avatar
MarkDHamill
Registered User
Posts: 3780
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

trigger_error() acting strange

Post by MarkDHamill » Mon May 20, 2019 9:34 pm

This behavior strikes me as bizarre. I have this language variable defined in another file:

Code: Select all

	'FBC_DENY_ACCESS'					=> 'Your access is denied due to the country code assigned to your Internet Protocol (IP) address. Your IP is %s and your country is %s',
I set some local variables, in this case both $user_ip and $country are good because when I echo() them they show up separately.

When I code this:

Code: Select all

				@trigger_error(sprintf($this->language->lang('FBC_DENY_ACCESS'), $user_ip, $country), E_USER_NOTICE);
the sprintf() logic doesn't work and it results in:
Your access is denied due to the country code assigned to your Internet Protocol (IP) address. Your IP is %s and your country is %s
Okay, I realize this is not the ideal way to code this these days. So let's try the more correct way:

Code: Select all

	'FBC_DENY_ACCESS'					=> 'Your access is denied due to the country code assigned to your Internet Protocol (IP) address. Your IP is %1$s and your country is %2$s',
with a similar result:
Your access is denied due to the country code assigned to your Internet Protocol (IP) address. Your IP is %1$s and your country is %2$s
I've tried all sorts of variants, even using str_replace() to see if that would work but it doesn't. I'm guessing this behavior is something weird about trigger_error(), which is built into PHP?

Note: I suppress errors on trigger_error() because otherwise I get a PHP notice I can't get rid of. The result is the same though.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

User avatar
david63
Registered User
Posts: 16062
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: trigger_error() acting strange

Post by david63 » Mon May 20, 2019 11:00 pm

You do not use sprintf whith the language object. You should have @trigger_error($this->language->lang('FBC_DENY_ACCESS', $user_ip, $country), E_USER_NOTICE);
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
MarkDHamill
Registered User
Posts: 3780
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: trigger_error() acting strange

Post by MarkDHamill » Mon May 20, 2019 11:27 pm

I had actually tried this variation.

Code: Select all

@trigger_error($this->language->lang('FBC_DENY_ACCESS', $user_ip, $country), E_USER_NOTICE);
still results in:
Your access is denied due to the country code assigned to your Internet Protocol (IP) address. Your IP is %1$s and your country is %2$s.
Quite puzzling.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1040
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: trigger_error() acting strange

Post by mrgoldy » Mon May 20, 2019 11:56 pm

What's the PHP notice you're getting when you do not suppress the trigger_error?

As the last example should give you the expected result, so there might be something else going on..
You would only have to suppress a trigger_error if you do not want it to show up in the command line interface.
Moreover, the language strings leads to suggest it is a "bad" thing, so perhaps E_USER_WARNING would be more appropiate?
Also, not sure where exactly it is you are using the mentioned code, but as a note, you should only have to use trigger_error while insde the control panels.

Anyways, if it's still not working out we might have to see some more code..
phpBB Studio / ''Proud member of the Studio"

User avatar
MarkDHamill
Registered User
Posts: 3780
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: trigger_error() acting strange

Post by MarkDHamill » Tue May 21, 2019 12:01 am

The notice is:
Notice: FBC_DENY_ACCESS in /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/filterbycountry/event/main_listener.php on line 157
The line of code is the same:

Code: Select all

				trigger_error($this->language->lang('FBC_DENY_ACCESS', $user_ip, $country), E_USER_NOTICE);
The behavior is the same if I use E_USER_WARNING.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 2228
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: trigger_error() acting strange

Post by kinerity » Tue May 21, 2019 1:19 pm

Not that this is ideal, but what happens if you assign the string to a variable and them pass that to trigger_error?

Code: Select all

$my_var = $this->language->lang('FBC_DENY_ACCESS', $user_ip, $country);
trigger_error($my_var);
Kailey Truscott - Community Team

User avatar
MarkDHamill
Registered User
Posts: 3780
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: trigger_error() acting strange

Post by MarkDHamill » Tue May 21, 2019 1:22 pm

Thanks for the suggestion. I did try this and many variations. No change.
Your access is denied due to the country code assigned to your Internet Protocol (IP) address. Your IP is %1$s and your country is %2$s.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

User avatar
david63
Registered User
Posts: 16062
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: trigger_error() acting strange

Post by david63 » Tue May 21, 2019 2:17 pm

Can we have link to your Git repository so that we can see the whole code and try to identify the problem
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
canonknipser
Registered User
Posts: 1960
Joined: Thu Sep 08, 2011 4:16 am
Location: Germany
Name: Frank Jakobs
Contact:

Re: trigger_error() acting strange

Post by canonknipser » Tue May 21, 2019 2:50 pm

language->lang uses vsprintf internally, so maybe you need to pass your parameter as array

Code: Select all

$my_var = $this->language->lang('FBC_DENY_ACCESS', array($user_ip, $country));
trigger_error($my_var);
Just a guess, untested
Greetings, Frank
phpbb.de support team member
English is not my native language - no support via PM or mail
New arrival - Extensions and scripts for phpBB

User avatar
david63
Registered User
Posts: 16062
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: trigger_error() acting strange

Post by david63 » Tue May 21, 2019 3:06 pm

I have just done a quick test with lang var 'TESTER' => 'My name is %1$s %2$s',

and in my controller

Code: Select all

$var1 = 'Donald';
$var2 = 'Duck';

trigger_error($this->language->lang('TESTER', $var1, $var2), E_USER_NOTICE);
And the output was
Information

My name is Donald Duck
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
MarkDHamill
Registered User
Posts: 3780
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: trigger_error() acting strange

Post by MarkDHamill » Tue May 21, 2019 3:15 pm

Thanks David. One thing I am wondering if it's because I'm using the core.user_setup() event. There I first link in the language files for the extension.

Code: Select all

		// Load the language files for the extension
		$lang_set_ext = $event['lang_set_ext'];
		$lang_set_ext[] = array(
			'ext_name' => 'phpbbservices/filterbycountry',
			'lang_set' => array('info_acp_filterbycountry', 'common'),
		);
		$event['lang_set_ext'] = $lang_set_ext;
Maybe they aren't hooked in yet and that's the issue, but I get other language strings from the extension okay. It's something about parsing.

I do want to prohibit forum access ASAP so this seemed a logical place to do it.

I could do sprintf()'s in the language file itself and that would probably work. But past reviews by the extension review team tells me they will disallow it.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

User avatar
david63
Registered User
Posts: 16062
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: trigger_error() acting strange

Post by david63 » Tue May 21, 2019 3:26 pm

As you are using events then you will be using a listener and you do not use trigger_error in a listener file.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
MarkDHamill
Registered User
Posts: 3780
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: trigger_error() acting strange

Post by MarkDHamill » Tue May 21, 2019 4:34 pm

Thanks. I'll have to figure out a different place to do this then.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3697
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: trigger_error() acting strange

Post by kasimi » Tue May 21, 2019 4:54 pm

Use core.user_setup only to load languages with the lang_set_ext event variable. Don't call $language->add_lang() in that handler.
In core.user_setup_after you can load your language with $language->add_lang() and access your language keys.

User avatar
MarkDHamill
Registered User
Posts: 3780
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: trigger_error() acting strange

Post by MarkDHamill » Tue May 21, 2019 7:13 pm

Thanks. So for core events you can only manipulate variables attached to the event object mentioned in the documentation for the event. In this case I added the language object to the main_listener.php and assumed I could use that. I could, but I couldn't expect any interpolation.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

Post Reply

Return to “Extension Writers Discussion”