CakePHP Routing Slugs, detect Extension? - cakephp

In my 2.X app we're detecting which Tenant is using the system via a Slug in the URL, ie:
app/[tenant_slug]/[controller]/[action]
The issue however is that when the users browser requests for some static file our server doesn't have, such as:
app/apple-touch-icon-76x76.png
Cake takes 'apple-touch-icon-76x76' as the tenant_slug - so then our server has to determine that no, it doesn't have that slug and then throw back a Not Found error.
I have the following matching criteria on the route:
$validTenant = '[a-zA-Z0-9-_]+';
And I also have the following enabled:
CakePlugin::routes();
What is the correct way of handling this so the router knows that if a static resource is requested (ie: file ending in .js, .css, .png etc), it shouldn't use the file name as a slug? Or, should I have my code that looks up the tenant_slug (TenantAuthorizeComponent) handling the case where there is a file extension?

I would handle this at the webserver level, if possible, do not even let php slow down your static files requests - for example for nginx I pass just php files into the interpreter using this statement in mysite.vhost.conf:
location ~ \.php$ {
fastcgi_pass php;
}

Related

How to deal with callbacks having dot in the URL with built-in PHP server?

I am using Drupal site (with .htaccess and clean URLs which are working fine) within built-in PHP Development Server (run by: php -S localhost:8888) where in CMS I generate dynamically the XML file which I would like to expose, however when I am trying to open /foo/bar.xml the following error happens:
The requested resource /foo/bar.xml was not found on this server.
After investigation, it seems PHP built-in server assumes that all files containing a dot they must be the files in the local filesystem.
Is there any workaround for that?
According to PHP Built-in web server docs, to handle custom requests, you need to specify a "router" script (given on the command line) which returns FALSE, then the requested resource is returned as-is, otherwise the script's output is returned to the browser.
Using a Router Script
Here is simple example which handles requests for images, then display them, otherwise if HTML files are requested, it will display "Welcome to PHP":
<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
return false; // serve the requested resource as-is.
} else {
echo "<p>Welcome to PHP</p>";
}
?>
Drupal 7 & 8
For Drupal 7 & 8, you can use .ht.router.php file, then run it as:
php -S localhost:8888 .ht.router.php

Apache2 configuration issue - Avoid file accessing without extension

I'm developing a website and having development environment as Ubuntu 14.04, PHP 5.5.9, Apache 2.4.7. My issue is if some want accessing a file like http://domain.com/robots(without extension) then I should avoid but I've to allow if some tries to access http://domain.com/robots.txt(with extension).
Now in both ways I'm able to access files, I want to avoid this when user haven't given extension of the file using .htaccess or apache configuration file.
My previous environment was Apache 2.2 and PHP 5.3.10 at the time with default Apache configuration it was working as expected but after upgrade its not working as expected and due lack of knowledge on Apache configuration I'm unable to pick those lines of code.
Add following line in .htaccess
Options -Multiviews

css mime type return as text/plain rather than text/css

I am using this code to get the mime type from a given file
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->buffer(file_get_contents($file));
If I feed it a PHP file then I get
text/x-php
but if I feed it a CSS file I get
text/plain
I have been trying to solve this issue to no avail, I have AddType in my Apache to allow for CSS file types.
Anyone have any suggestions?
You need to tell Apache to serve files as text/css whenever their extension is .css. The only sane way for your server to differentiate between a style sheet and any other type of text file, is by its file name.
You can do so by editing Apache's MIME type configuration file, which is named mime.types ( it lives in a different folder depending on your distro, maybe try /etc/apache2 ) to make the following association:
text/css css
Alternately, if you don't have admin access, you can create an .htaccess file in your own web root. More info on both methods here
Hope this helps!
Upon rereading our question, maybe I misunderstood you, and you have already done the steps above?
If so, could you check what your OS is reading the mimetype as? There are appropriiate commands to check mimetype, over here:
Also, it seems like it would be pragmatic to add a subsequent check to see if the file extension ends with .css. Is there a reason why this won't work?

Non-Latin characters in URL (Google App Engine)

In my project I have html file with non-Latin characters: Кондиционер.html. When i make request:
www.myDomain.com/Кондиционер.html
Server sends 404 error:
Error: Not Found
The requested URL /%D0%9A%D0%BE%D0%BD%D0%B4%D0%B8%D1%86%D0%B8%D0%BE%D0%BD%D0%B5%D1%80.html was not found on this server.
But with Latin latters everything works fine.
What can I do to set GAE server to support such non-Latin file names?
Try this (does not work on the web-console, but in real *.py files):
print urllib.unquote("Ober%C3%B6sterreich.txt").decode("utf8")
For static files you need a redirecting request handler and an ascii-named file to which you redirect.

gwt file permission

I have a little GWT/AppEngine Project which uses RPC. Basically I need to get some data from a XML file that resides on the server. But when I use the RPC to read the file in my server-package I am getting a AccessControlException (access denied). Any ideas what the problem is?
//JAXB powered XML Parser
public PoiList readXML() {
try {
unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new XMLValidEventHandler());
db = (PoiList) unmarshaller.unmarshal(new File("src/com/sem/server/source.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
return db;
}
java.security.AccessControlException: access denied (java.io.FilePermission \WEB-INF\classes\com\sem\server read)
cheers hoax
I think the problem is that you're trying to read a file that is not located in your working directory. The guidlines for structuring your code in gwt apps are as follows
Under the main project directory
create the following directories:
src folder - contains production Java source
war folder - your web app; contains static resources as well as compiled output
test folder - (optional) JUnit test code would go here
Try moving the file to the war directory (for example /war/resources/myFile.xml) and then open it by
File myFile = new File(System.getProperty("user.dir") + "/resources/myFile.xml");
Usually, when you load a resource that is located in your classpath, you should't use java.io.File. Why? Because it's very much possible, that there is no real file - the classes are often packaged as a .jar file, or even loaded in a completely different way (very likely in the case of AppEngine, though I don't know the details.)
So, if you want to load it directly from your classpath, you can use:
ClassLoader classLoader =
getClass().getClassLoader(); // Or some other way to
// get the correct ClassLoader
InputStream is = classloader.getResourceAsStream("/com/sem/server/source.xml");
Then you can use the input stream in your unmarshaller.

Resources