Request var in listener?

Discussion forum for Extension Writers regarding Extension Development.
User avatar
Toxyy
Registered User
Posts: 944
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Request var in listener?

Post by Toxyy »

Trying to get a variable I added in a url, posting.php?c=1&.......

using the posting_modify_submit_post_before event in my listener, accessing it somewhere else with

Code: Select all

$event['data'] = array_merge($event['data'], array(
	'custom_mode' => (isset($_POST['c'])) ? true : false,
));
Now, I've tried (isset($_POST['c'])) ? true : false, request_var('c', 0), $this->request->variable('c', 0), and nothing is working. I must be missing something simple here.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
Ger
Registered User
Posts: 2108
Joined: Wed Jan 02, 2008 7:35 pm
Location: 192.168.1.100
Contact:

Re: Request var in listener?

Post by Ger »

Have you injected the $request class in your listener?

Look here for example: https://github.com/GerB/modernquote/blo ... hp#L39-L41
and
https://github.com/GerB/modernquote/blo ... ces.yml#L5
My extensions:
Simple CMS, Feed post bot, Avatar Resize, Modbreak, Magic OGP, Live topic update, Modern Quote, Quoted Where (GDPR) and Autoresponder.
Newest: FAQ manager for 3.2

Like my work? Buy me a coffee to keep it coming. :ugeek:

-Don't PM me for support-
User avatar
Toxyy
Registered User
Posts: 944
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Request var in listener?

Post by Toxyy »

Ger wrote: Fri Jun 08, 2018 10:17 am Have you injected the $request class in your listener?

Look here for example: https://github.com/GerB/modernquote/blo ... hp#L39-L41
and
https://github.com/GerB/modernquote/blo ... ces.yml#L5
Yeah I have, unless I forgot to add it it to my services.yml file. I'll update you in a bit.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: Request var in listener?

Post by david63 »

If you missed it out of your services.yml file you would know about if from the error messages
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here
User avatar
Toxyy
Registered User
Posts: 944
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Request var in listener?

Post by Toxyy »

Yeah, it's in my listener just fine. Declared a public $request above my constructor, I have \phpbb\request\request $request as a parameter, and $this->request = $request; within the constructor itself. I also have it in my services.yml.

Edit: just tried disabling the extension and reenabling it after deleting data, that didn't work either.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: Request var in listener?

Post by david63 »

Have you tried using the correct variable function? $this->request->is_set_post('c')
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Request var in listener?

Post by 3Di »

Toxyy wrote: Fri Jun 08, 2018 9:49 am Trying to get a variable I added in a url, posting.php?c=1&.......

using the posting_modify_submit_post_before event in my listener, accessing it somewhere else with

Code: Select all

$event['data'] = array_merge($event['data'], array(
	'custom_mode' => (isset($_POST['c'])) ? true : false,
));
Now, I've tried (isset($_POST['c'])) ? true : false, request_var('c', 0), $this->request->variable('c', 0), and nothing is working. I must be missing something simple here.

Code: Select all

$event['post_data'] = array_merge($event['post_data'], array(
	'custom_mode' => $this->request->variable('c', ( (!empty($event['post_data']['custom_mode'])) ? $event['post_data']['custom_mode'] : 0 ), 0),
));
should do, on the fly.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Toxyy
Registered User
Posts: 944
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Request var in listener?

Post by Toxyy »

david63 wrote: Fri Jun 08, 2018 9:30 pm Have you tried using the correct variable function? $this->request->is_set_post('c')
That didn't work either.

3Di, I'm just passing this through the url, such as posting.php?c=1&. I don't have anything called custom_mode in post data just the url.

Basically I'm messing with these two:

Code: Select all

       public function posting_modify_submit_post_before(event $event)
        {
                $event['data'] = array_merge($event['data'], array(
			'parent_id' => $event['data']['post_id'],
			'custom_mode' => 1,
		));
        }
        
        // add parent id to database when commenting
        public function submit_post_modify_sql_data($event)
        {
                $sql_data = $event['sql_data'];
		$sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
                        'parent_id' => $event['data']['custom_mode'],
                ));
                
		$event['sql_data'] = $sql_data;
        }
I've changed submit_post_modify_sql_data temporarily to reflect the custom_mode variable directly. Changing 'custom_mode' => 1 with all of these solutions and none seem to work :/

I set it to 1 just now to check that it is indeed working and it is. Just can't get request to work.

EDIT: just saw you updated the code 3Di, let me test that.
EDIT2: nope, still didn't work.

Also tried just adding that entire block you wrote above the $event['data'] = array_merge mess, and that didn't work either.
Last edited by Toxyy on Fri Jun 08, 2018 9:46 pm, edited 1 time in total.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Request var in listener?

Post by 3Di »

I guess you are using the wrong event for the purpose.
Here you are modifying the post's data before the post action is performed.

Specifically the request variable contains something you are putting is some input field above this event.

In this case within core.posting_modify_template_vars -> array $page_data.

This listener uses almost all of the events: https://github.com/phpBB-Studio/DateTop ... stener.php
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Toxyy
Registered User
Posts: 944
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Request var in listener?

Post by Toxyy »

Code: Select all

        public function posting_modify_submission_errors(event $event)
        {
                $event['post_data'] = array_merge($event['post_data'], array(
                        'custom_mode2' => $this->request->is_set_post('c') ? 1 : 2,
                ));
        }
        
        public function posting_modify_template_vars(event $event)
        {
                $event['post_data'] = array_merge($event['post_data'], array(
                        'custom_mode' => $this->request->is_set_post('c') ? 3 : 4,
                ));
        }
        
        public function posting_modify_submit_post_before(event $event)
        {
                $event['data'] = array_merge($event['data'], array(
			'custom_mode' => $event['post_data']['custom_mode'],
		));
        }
        
        public function submit_post_modify_sql_data($event)
        {
                $sql_data = $event['sql_data'];
		$sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
                        'parent_id' => $event['data']['custom_mode'],
                ));
                
		$event['sql_data'] = $sql_data;
        }
So, posting_modify_submit_post_before doesn't seem to get $event['post_data']['custom_mode'] from posting_modify_template_vars, only posting_modify_submission_errors, but that doesn't matter much because I still can't check to see if the variable c is set in the url. I see $request->variable('p', 0); and $request->variable('delete_reason', '', true); and things like that used within posting.php...

I don't see anything in $page_data in posting.php that would seem to help much, as none of the variables in that array seem to post the full url or anything that has to do with my c=1 in the url.

Edit:
$this->request->is_set_post('f') seems to work, but $this->request->is_set_post('c') doesn't. Hmm...
plain request_var('f', 0) works as well but not request_var('c', 1)...

Edit again:

So I've been testing changing the mode name, and to get it to work I have to use this:

Code: Select all

        public function modify_posting_parameters(event $event)
        {
            $mode = $event['mode'];
            
            $this->current_mode = 'no';
            
            if($mode == 'custommode')
            {
                $this->current_mode = 'yes';
                $mode = 'quote';
            }
            
            $event['mode'] = $mode;
        }
Now strangely enough, when I call $this->current_mode in other events, they always say that it's 'no', but if I only have it instantiated in the if statement there, then they say it's null. So I AM able to check if I've changed the url, however modify_posting_parameters isn't useful to me at all without being able to modify post_data, and its the latest that i can/have to modify the mode to quote otherwise i get an invalid mode error.

Turns out that $this->request is pulling info from the post url, not the url in my address bar in posting.php, that's why I'm not able to find my 'c' variable. Hmm...

Edit again:
So I did var_dump($_GET); (also did _POST and _SERVER) and the QUERY_STRING and REQUEST_URI show the c= in the url! So phpbb wiki tells me
$this->user->page['query_string'] will give me query string :)

But nope, it gives me the one that is made when you click submit....

:(

not making things easy for me here guys..........

UPDATE:

YES I FIGURED IT OUT

Code: Select all

$this->request->variable('HTTP_REFERER', '', false,\phpbb\request\request_interface::SERVER)
This gives me the initial posting.php url, not the one that request_var uses :) now I can just either change mode= to whatever I want, add some new variables like c=whatever... boy am I happy now.

If there's a better solution here I'm willing to hear it. After taking this long to get somewhere I really don't care about using this in posting_modify_submit_post_before and parsing to get what I need myself. That was a LOT harder to find.

I guess the best question here would be, how do I use request_var in the http_referer?

Funny enough, vardump($this->request->variable('c', '')) actually printed the correct value, even though right under it $this->request->variable('c', '') didn't exist apparently. Very odd.

Thanks for your help everyone! I feel like a big php noob after all of this but I'm getting places!
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28619
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Request var in listener?

Post by Paul »

Your extension will be most definitely denied when using that solution if you submit it for validation
User avatar
martti
Registered User
Posts: 911
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: Request var in listener?

Post by martti »

  1. In the event listener, you can not directly assign the result of a function to $event["post_data'].
    Do:

    Code: Select all

    $post_data = $event['post_data'];
    // process $post_data
    $event['post_data'] = $post_data;
    See the "Note" (blue panel) in the documentation.
  2. You can't depend on the HTTP REFERER, because it is set (or may be not) by the client.
User avatar
Toxyy
Registered User
Posts: 944
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Request var in listener?

Post by Toxyy »

Paul wrote: Sat Jun 09, 2018 12:07 pm Your extension will be most definitely denied when using that solution if you submit it for validation
Well it's the only way to actually get the variable I need, because request_var parses the wrong url. Any ideas?
martti wrote: Sat Jun 09, 2018 1:23 pm
  1. In the event listener, you can not directly assign the result of a function to $event["post_data'].
    Do: See the "Note" (blue panel) in the documentation.
  2. You can't depend on the HTTP REFERER, because it is set (or may be not) by the client.
I copied the format from some extension, I can't remember. Thanks for the tip. I understand that but request_var is getting variables from the wrong url (unless I do vardump), I need to use request_var on the original posting.php link that I made in viewtopic somehow...
Last edited by Toxyy on Sat Jun 09, 2018 5:44 pm, edited 3 times in total.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
Toxyy
Registered User
Posts: 944
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Request var in listener?

Post by Toxyy »

double post delete me
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Request var in listener?

Post by 3Di »

We are going round in circles here.
May I suggest you to provide the following elements in order to get help?
  • Synopsys for the extension
  • A GitHub'ed repository of it
  • The final goal for that, if not already explained in the Sysnopsys
  • How are you going to add a parameter to an URI/L
Moreover, some suggestions
  • Make sure you have read and understood the Documentation as its whole
  • Make sure to get in touch with the core code, its logic and how it works.
Martti wrote: Sat Jun 09, 2018 4:05 pm In the event listener, you can not directly assign the result of a function to $event["post_data']
It depends on the event, are more those where you are allowed to do so than the rest.
In any case a PHP debug error is thrown if an event doesn't allow event variables to be overloaded.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
Post Reply

Return to “Extension Writers Discussion”