Alanstormdotcom_Staticroute - Version 0.1.0

Version Notes

More an example of how you might do this than a fully featured extension.

Download this release

Release Info

Developer Magento Core Team
Extension Alanstormdotcom_Staticroute
Version 0.1.0
Comparing to
See all releases


Version 0.1.0

app/code/local/Alanstormdotcom/Staticroute/Controller/Router.php ADDED
@@ -0,0 +1,102 @@
1
+ <?php
2
+ /**
3
+ * Lets us load
4
+ * http://example.com/static-frontend/css/example.css
5
+ * http://example.com/static-frontend/js/example.js
6
+ */
7
+ class Alanstormdotcom_Staticroute_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
8
+ {
9
+ public function initControllerRouters($observer)
10
+ {
11
+ /* @var $front Mage_Core_Controller_Varien_Front */
12
+ $front = $observer->getEvent()->getFront();
13
+ $front->addRouter('static-frontend', $this);
14
+ }
15
+
16
+ /**
17
+ * Validate and Match Cms Page and modify request
18
+ *
19
+ * @param Zend_Controller_Request_Http $request
20
+ * @return bool
21
+ */
22
+ public function match(Zend_Controller_Request_Http $request)
23
+ {
24
+ $identifier = trim($request->getPathInfo(), '/');
25
+ $identifier = $this->_sanatizeIdentifier($identifier);
26
+ $file = $this->_checkForFile($identifier);
27
+ if(!$file)
28
+ {
29
+ return false;
30
+ }
31
+ $this->_serveFile($file);
32
+ exit; //serveFile should exit, but just in case!
33
+
34
+ // Mage::log($identifier);
35
+ // Mage::log($this->getModulePath());
36
+ // return false;
37
+ }
38
+
39
+ /**
40
+ * Take soem paranoid steps to ensure people don't get clever
41
+ * since we're doinga readfile
42
+ */
43
+ protected function _sanatizeIdentifier($identifier)
44
+ {
45
+ $identifier = str_replace('..','',$identifier);
46
+ $identifier = preg_replace('{[^a-zA-Z0-9_./-]}i','',$identifier);
47
+ return substr($identifier,0,1024);
48
+ }
49
+
50
+ protected function _serveFile($file)
51
+ {
52
+ header('Content-Type: ' . $this->_getContentType($file));
53
+ readfile($file);
54
+ exit;
55
+ }
56
+
57
+ const RE_JAVASCIPRT = '{\.js$}i';
58
+ const RE_CSS = '{\.css$}i';
59
+ const RE_XML = '{\.xml$}i';
60
+
61
+ /**
62
+ * Could use finfo_file is our glorious PHP 5.3 future
63
+ * (somewhere, the apache team is snickering)
64
+ */
65
+ protected function _getContentType($file)
66
+ {
67
+ if(preg_match(self::RE_JAVASCIPRT, $file))
68
+ {
69
+ return 'Content-Type: application/x-javascript';
70
+ }
71
+ else if(preg_match(self::RE_CSS, $file))
72
+ {
73
+ return 'Content-Type: text/css';
74
+ }
75
+ else if(preg_match(self::RE_XML, $file))
76
+ {
77
+ return 'Content-Type: text/xml';
78
+ }
79
+ else
80
+ {
81
+ return 'Content-Type: text/html';
82
+ }
83
+ }
84
+
85
+ protected function _checkForFile($identifier)
86
+ {
87
+ $path = $this->getModulePath() . '/' . $identifier;
88
+ if(file_exists($path))
89
+ {
90
+ return $path;
91
+ }
92
+ return false;
93
+ }
94
+
95
+ /**
96
+ * Probably not the most efficient way to do this
97
+ */
98
+ public function getModulePath()
99
+ {
100
+ return realpath(dirname(__FILE__) . '/..');
101
+ }
102
+ }
app/code/local/Alanstormdotcom/Staticroute/etc/config.xml ADDED
@@ -0,0 +1,51 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <Alanstormdotcom_Staticroute>
5
+ <version>0.1.0</version>
6
+ </Alanstormdotcom_Staticroute>
7
+ </modules>
8
+ <frontend>
9
+ <routers>
10
+ <alanstormdotcomstaticroute>
11
+ <use>
12
+ standard
13
+ </use>
14
+ <args>
15
+ <module>Alanstormdotcom_Staticroute</module>
16
+ <frontName>alanstormdotcomstaticroute</frontName>
17
+ </args>
18
+ </alanstormdotcomstaticroute>
19
+ </routers>
20
+ </frontend>
21
+ <global>
22
+ <blocks>
23
+ <alanstormdotcomstaticroute>
24
+ <class>Alanstormdotcom_Staticroute_Block</class>
25
+ </alanstormdotcomstaticroute>
26
+ </blocks>
27
+ <models>
28
+ <alanstormdotcomstaticroute>
29
+ <class>Alanstormdotcom_Staticroute_Model</class>
30
+ </alanstormdotcomstaticroute>
31
+ </models>
32
+ <helpers>
33
+ <alanstormdotcomstaticroute>
34
+ <class>
35
+ Alanstormdotcom_Staticroute_Helper
36
+ </class>
37
+ </alanstormdotcomstaticroute>
38
+ </helpers>
39
+
40
+ <events>
41
+ <controller_front_init_routers>
42
+ <observers>
43
+ <static_files>
44
+ <class>Alanstormdotcom_Staticroute_Controller_Router</class>
45
+ <method>initControllerRouters</method>
46
+ </static_files>
47
+ </observers>
48
+ </controller_front_init_routers>
49
+ </events>
50
+ </global>
51
+ </config>
app/code/local/Alanstormdotcom/Staticroute/static-frontend/css/example.css ADDED
@@ -0,0 +1,4 @@
1
+ body
2
+ {
3
+ font-size:24px;
4
+ }
app/code/local/Alanstormdotcom/Staticroute/static-frontend/js/example.js ADDED
@@ -0,0 +1,4 @@
1
+ var hello = function (message)
2
+ {
3
+ alert(message);
4
+ }
app/etc/modules/Alanstormdotcom_Staticroute.xml ADDED
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><config><modules><Alanstormdotcom_Staticroute><active>true</active><codePool>local</codePool></Alanstormdotcom_Staticroute></modules></config>
package.xml ADDED
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Alanstormdotcom_Staticroute</name>
4
+ <version>0.1.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.opensource.org/licenses/mit-license.html">MIT License</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>A simple example of use a custom Router to serve up static files for your module.</summary>
10
+ <description>A simple example of use a custom Router to serve up static files for your module.</description>
11
+ <notes>More an example of how you might do this than a fully featured extension.</notes>
12
+ <authors><author><name>Alan Storm</name><user>auto-converted</user><email>astorm@alanstorm.com</email></author></authors>
13
+ <date>2010-09-28</date>
14
+ <time>20:18:55</time>
15
+ <contents><target name="magelocal"><dir name="Alanstormdotcom"><dir name="Staticroute"><dir name="Controller"><file name="Router.php" hash="99138f63452b9f021b119092b9b641b0"/></dir><dir name="etc"><file name="config.xml" hash="4c779ca8df230e981647cd60257fe405"/></dir><dir name="static-frontend"><dir name="css"><file name="example.css" hash="974d215a67194d296c20a3ce9cc67502"/></dir><dir name="js"><file name="example.js" hash="8582d02d18607a1d3ea211b70a852866"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Alanstormdotcom_Staticroute.xml" hash="ccfc9222a6211371389173ebd274777b"/></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies/>
18
+ </package>