Zend Routing -- Grabbing Variables from URL with Ampersands and not Slashes - Tapjoy - php

I'm currently creating attempting to integrate with the Tapjoy API with their callback URL request which comes in this form:
<callback_url>?snuid=<user_id>&currency=&mac_address=<mac_address>&display_multiplier=<display_multiplier>
However, using ZF1 --
the Zend_Controller_Router_Route seems to be dependent on the variable separation being delimited by slashes and not ampersands.
Here is my current Route code
> $router->addRoute( 'api-tapjoy', new Zend_Controller_Router_Route('api/tapjoy?snuid=:snuid&mac_address=:&mac_address&display_multiplier....etc.etc,
array('controller'=>'api', 'action' =>'tapjoy')));
Whenever I remove the ampersands and initial question mark and replace them with slashes it works. How can I properly receive the HTTP Request whilst using ampersands?

Looks like I figured it out. It has nothing to do with ampersands and slashes, etc. but rather that my Route wasn't created properly.
I was thrown off by this documentation on ZF1's website about routers:
The first parameter in the Zend_Controller_Router_Route constructor is
a route definition that will be matched to a URL. Route definitions
consist of static and dynamic parts separated by the slash ('/')
character. Static parts are just simple text: author. Dynamic parts,
called variables, are marked by prepending a colon to the variable
name: :username.

Related

Laravel - Proper way to support a URL piece that might have a slash in it?

I've defined a route in web.php that looks like this:
Route::delete('/app/charges-for-order/{orderId}', 'AppController#deleteOrderCharges');
It seems to work well apart from when orderId has a forward slash in it. Eg 11/11.
Naturally my first port of call was to generate the url in the frontend using encodeURIComponent:
/app/charges-for-order/${encodeURIComponent(orderId)} (in javascript)
Which produces the url /app/charges-for-order/11%2F11, replacing the forward slash with %2F -- however, this is giving me a 404, as if Laravel is still seeing %2F as a forward slash for routing purposes.
How can I allow orderId to have (encoded) forward slashes and still be part of the URL like that? Is there some alternate encoding scheme that Laravel will respect in this context?
Use where() clause in route file. Which allows you to use RegEx in the file.
->where('orderId', '^[0-9/]+$'); # or
->where('orderId', '[0-9/]+'); # this
Read - Regular Expression Constraints
IMPORTANT: Do not use ->where('orderId', '.*') or ->where('orderId', '.') any cost.
Side Note: I'm not debugging the DELETE route works or not, just testing whether you can pass params. As well, if you found extensive RegEx, use it. I used this for testing purposes, but still, it does the job
I tested with http://127.0.0.1:8090/app/charges-for-order/11/11 in my local, and the result was
I assume you are using Apache as HTTP server? At least Apache is by default decoding %2F behind your back: https://httpd.apache.org/docs/2.4/mod/core.html#allowencodedslashes
Changing this might be a bad idea, so I would suggest you would change your route to match all:
Route::delete('/app/charges-for-order/{orderId}', 'AppController#deleteOrderCharges')
->where('orderId', '.+');
I ended up just changing the methods in question to accept query parameters or post data instead, I was having trouble with the other answers.

Setting a route depending on a String in the URL with Symfony

I am pretty new to Symfony.
I need to check if an URL contains a certain word, which can be in any order, and if it has, it will be redirected to a certain page.
For example www.example.com/mystring/, www.example.com/content/mystring or www.example.com/001/content/mystring/ should redirect you to www.example.com/mystring because it contains mystring in the URL
This could be easy:
$routes->add('mystring', '/mystring/')
->controller([MyStringController::class, 'show'])
;
$routes->add('mystring', '{number}/{content}/mystring')
->controller([MyStringController::class, 'show'])
;
Etc etc the problem is that mystring could be anywhere in the URL.
I already have a workaround in the controller that will redirect you if the string, however I would like a clean solution in the Routing file.
So the question is:
Is there any way to set a route depending if a URL contains certain string that can be anywhere and in any order?
Theoretically, yes. A route placeholder usually isn't allowed to contain a /, because it makes things more difficult. However, there are ways to allow it. BUT, there are other problems arising, unless that additional magic string always takes priority over anything else that happens.
To allow a slash inside a placeholder, this is possible with this: https://symfony.com/doc/current/routing/slash_in_parameter.html
so essentially to add a slash, you'd have a route with
'/{url}', and requirement 'url' => '.+'
now, just a '.+' is not enough for your purpose though. I'm not absolutely certain about escaping in this case, but it would probably be something like
'url' => '.*\bmystring\b.*'
if \b is allowed, this means it's a word boundary (which is probably what you want).
otherwise '(.+/)*mystring(.+/|$)+' should do the trick
Also, you shouldn't name multiple routes the same ... also, this kind of route definition won't give you the other placeholders you have ...
If your special route should extend only existing routes, though, you should probably find a way to cycle through existing routes and add your magic string. But that's a different question ;o)

How to pass forward slash (%2F) in laravel 5 url with addition get parrameters

Users can search my site. Sometimes they might use a search term containing a forward slash (search with / slash) which when submitted by the form turns into %2F in the url.
eg. www.mysite.com/search/search+with+%2F+slash
I have used the answer from here which works great to give me the right page and not send me to a 404.
My problem now is I use pagination on the page and custom filters which are both passed as get vars in the url and when accessing the GET var it's empty.
eg. www.mysite.com/search/search+with+%2F+slash?page=2
This is my current route
$this->get('search/{search_term}', ['uses' => 'SearchController#search'])
->where('search_term', '(.*(?:%2F:)?.*)');
Not sure what do from here
Including an encoded slash (%2F) in the path component of a URL is not a good idea. The HTTP RFC says that this sequence should be "equivalent" to a real slash:
Characters other than those in the "reserved" and "unsafe" sets (see
RFC 2396 [42]) are equivalent to their ""%" HEX HEX" encoding.
In practice, the handling of these URLs is inconsistent. Some web servers (and even some browsers!) will treat %2F as equivalent to a real slash, some will treat it differently, and some tools, including some web application firewalls and proxies, will simply reject URLs which contain such a sequence.
If you need to include user input like this in a URL, you should probably put it in a query string (/search/?q=search+with+%2f+slash).

Pass string, containing a slash as get-param

I'm wondering if it's possible to parse a string containing an URL as GET parameter using Mod-Rewrite through CakePHP. Do I have to pass this via $this->request->data (POST)?
Is it common to set Routes for such cases or would you either recommend splitting the URL into separate parameters?
A slash in $_GET? Woudn't that just be a query string containing such a slash?
See http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-querystring-parameters
By default those query strings are properly escaped, so all is well without any hacks on your end.
You can use rawurlencode() to esacpe special characters.

Urlencode forward slash 404 error

http://localhost/foo/profile/%26lt%3Bi%26gt%3Bmarco%26lt%3B%2Fi%26gt%3B
The url above gives me a 404 Error, the url code is this: urlencode(htmlspecialchars($foo));, as for the $foo: <i>badhtml</i>
The url works fine when there's nothing to encode e.g. marco.
Thanks. =D
Update: I'm supposed to capture the segment in the encoded part of the uri, so a 404 isn't supposed to appear.
There isn't any document there, marco is simply the string that I needed to fetch that person's info from db. If the user doesn't exist, it won't throw that ugly error anyways.
Slight idea what's wrong: I found out that if I used <i>badhtml<i>, it works just fine but <i>badhtml</i> won't, what do I do so that I can maintain the / in the <i>?
It probably think of the request as http://localhost/foo/profile/<i>badhtml<**/**i>
Since there is a slash / in the parameter, this is getting interpreted as a path name separator.
The solution, therefore, is to replace all occurrences of a slash with something that doesn't get interpreted as a separator. \u2044 or something. And when reading the parameter back in, change all \u2044s back to normal slashes.
(I chose \u2044 because this character looks remarkably like a normal slash, but you can use anthing that would never occur in the parameter, of course.)
It is most likely that the regex responsible for handling the URL rewrite does not like some of the characters in the URL-encoded string. This is most likely httpd/apache question, rather than PHP. Your best guess is to start by looking at the .htaccess (file containing URL rewrite rules).
This question assumes that your are trying to pass an argument through the URL, rather than access a file named <i>badhtml</i>.
Mr. Lister, you rocked.
"The solution, therefore, is to replace all occurrences of a slash with something that doesn't get interpreted as a separator. \u2044 or something. And when reading the parameter back in, change all \u2044s back to normal slashes."

Categories