1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | |
---|
13 | // TODO: Fix loose of relative path in CSS |
---|
14 | |
---|
15 | # Check if config file exists |
---|
16 | if (isset($_SERVER['DC_RC_PATH'])) { |
---|
17 | $zc_conf_file = $_SERVER['DC_RC_PATH']; |
---|
18 | } elseif (isset($_SERVER['REDIRECT_DC_RC_PATH'])) { |
---|
19 | $zc_conf_file = $_SERVER['REDIRECT_DC_RC_PATH']; |
---|
20 | } else { |
---|
21 | $zc_conf_file = dirname(__FILE__).'/config.php'; |
---|
22 | } |
---|
23 | |
---|
24 | if (!is_file($zc_conf_file)) { |
---|
25 | trigger_error('Unable to open config file',E_USER_ERROR); |
---|
26 | exit; |
---|
27 | } |
---|
28 | unset($zc_conf_file); |
---|
29 | |
---|
30 | # Start Dotclear core (required to load template engine and find paths) |
---|
31 | require_once dirname(__FILE__).'/prepend.php'; |
---|
32 | |
---|
33 | # No request |
---|
34 | if (empty($_GET['tf'])) { |
---|
35 | header('Content-Type: text/plain'); |
---|
36 | http::head(404,'Not Found'); |
---|
37 | exit; |
---|
38 | } |
---|
39 | |
---|
40 | # Add default templates path |
---|
41 | if (defined('DC_CONTEXT_ADMIN')) { |
---|
42 | |
---|
43 | # Set admin context |
---|
44 | $_ctx = new dcAdminContext($core); |
---|
45 | $core->tpl->addExtension($_ctx); |
---|
46 | $core->tpl->getLoader()->addPath(dirname(__FILE__).'/admin/default-templates'); |
---|
47 | |
---|
48 | // TODO: Find a better way to add plugins templates paths |
---|
49 | # --BEHAVIOR-- adminPrepend |
---|
50 | $core->callBehavior('adminPrepend',$core,$_ctx); |
---|
51 | } |
---|
52 | else { |
---|
53 | // TODO: dcPublicContext ... |
---|
54 | //$core->tpl->getLoader()->addPath(dirname(__FILE__).'/public/default-templates'); |
---|
55 | } |
---|
56 | |
---|
57 | # Clean up requested filename |
---|
58 | $f = path::clean($_GET['tf']); |
---|
59 | |
---|
60 | # Find templates paths then plugins paths |
---|
61 | $paths = $core->tpl->getLoader()->getPaths(); |
---|
62 | rsort($paths); // read default-templates last |
---|
63 | /* |
---|
64 | // TODO: Find a better way to add plugins templates paths |
---|
65 | $plugins_paths = array_reverse(explode(PATH_SEPARATOR,DC_PLUGINS_ROOT)); |
---|
66 | $paths = array_merge($paths,$plugins_paths); |
---|
67 | //*/ |
---|
68 | |
---|
69 | # Check all paths to see if file exists |
---|
70 | foreach ($paths as $path) { |
---|
71 | $file = path::real($path.'/'.$f); |
---|
72 | |
---|
73 | if ($file !== false) { |
---|
74 | break; |
---|
75 | } |
---|
76 | } |
---|
77 | unset($paths); |
---|
78 | |
---|
79 | # Can't find requested file |
---|
80 | if ($file === false || !is_file($file) || !is_readable($file)) { |
---|
81 | header('Content-Type: text/plain'); |
---|
82 | http::head(404,'Not Found'); |
---|
83 | exit; |
---|
84 | } |
---|
85 | |
---|
86 | # Limit type of files to serve |
---|
87 | $allow_types = array('png','jpg','jpeg','gif','css','js','swf'); |
---|
88 | |
---|
89 | # File extension is not allowed |
---|
90 | if (!in_array(files::getExtension($file),$allow_types)) { |
---|
91 | header('Content-Type: text/plain'); |
---|
92 | http::head(404,'Not Found'); |
---|
93 | exit; |
---|
94 | } |
---|
95 | |
---|
96 | # Display file |
---|
97 | http::$cache_max_age = 7200; |
---|
98 | http::cache(array_merge(array($file),get_included_files())); |
---|
99 | header('Content-Type: '.files::getMimeType($file)); |
---|
100 | |
---|
101 | # Temporary hack css files to regain relative path |
---|
102 | if (files::getExtension($file) == 'css') { |
---|
103 | $content = preg_replace( |
---|
104 | '/url\((\'|)([^:].*?)(\'|)\)/msi', |
---|
105 | 'url($1?tf='.dirname($f).'/$2$3)', |
---|
106 | file_get_contents($file) |
---|
107 | ); |
---|
108 | header('Content-Length: '.strlen($content)); |
---|
109 | echo $content; |
---|
110 | } |
---|
111 | else { |
---|
112 | header('Content-Length: '.filesize($file)); |
---|
113 | readfile($file); |
---|
114 | } |
---|
115 | exit; |
---|
116 | ?> |
---|