1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 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 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | class dcThemeEditor |
---|
15 | { |
---|
16 | protected $core; |
---|
17 | |
---|
18 | protected $default_theme; |
---|
19 | protected $user_theme; |
---|
20 | protected $parent_theme; |
---|
21 | |
---|
22 | protected $default_tpl = array(); |
---|
23 | public $tpl = array(); |
---|
24 | public $css = array(); |
---|
25 | public $js = array(); |
---|
26 | public $po = array(); |
---|
27 | |
---|
28 | public function __construct($core) |
---|
29 | { |
---|
30 | $this->core =& $core; |
---|
31 | $this->default_theme = path::real($this->core->blog->themes_path.'/default'); |
---|
32 | $this->user_theme = path::real($this->core->blog->themes_path.'/'.$this->core->blog->settings->system->theme); |
---|
33 | if (null !== $this->core->themes) { |
---|
34 | $parent_theme = $this->core->themes->moduleInfo($this->core->blog->settings->system->theme,'parent'); |
---|
35 | if ($parent_theme) { |
---|
36 | $this->parent_theme = path::real($this->core->blog->themes_path.'/'.$parent_theme); |
---|
37 | } |
---|
38 | } |
---|
39 | $this->findTemplates(); |
---|
40 | $this->findStyles(); |
---|
41 | $this->findScripts(); |
---|
42 | $this->findLocales(); |
---|
43 | } |
---|
44 | |
---|
45 | public function filesList($type,$item='%1$s') |
---|
46 | { |
---|
47 | $files = $this->getFilesFromType($type); |
---|
48 | |
---|
49 | if (empty($files)) { |
---|
50 | return '<p>'.__('No file').'</p>'; |
---|
51 | } |
---|
52 | |
---|
53 | $list = ''; |
---|
54 | foreach ($files as $k => $v) |
---|
55 | { |
---|
56 | if (strpos($v,$this->user_theme) === 0) { |
---|
57 | $li = sprintf('<li class="default-file">%s</li>',$item); |
---|
58 | } elseif ($this->parent_theme && strpos($v,$this->parent_theme) === 0) { |
---|
59 | $li = sprintf('<li class="parent-file">%s</li>',$item); |
---|
60 | } else { |
---|
61 | $li = sprintf('<li>%s</li>',$item); |
---|
62 | } |
---|
63 | $list .= sprintf($li,$k,html::escapeHTML($k)); |
---|
64 | } |
---|
65 | |
---|
66 | return sprintf('<ul>%s</ul>',$list); |
---|
67 | } |
---|
68 | |
---|
69 | public function getFileContent($type,$f) |
---|
70 | { |
---|
71 | $files = $this->getFilesFromType($type); |
---|
72 | |
---|
73 | if (!isset($files[$f])) { |
---|
74 | throw new Exception(__('File does not exist.')); |
---|
75 | } |
---|
76 | |
---|
77 | $F = $files[$f]; |
---|
78 | if (!is_readable($F)) { |
---|
79 | throw new Exception(sprintf(__('File %s is not readable'),$f)); |
---|
80 | } |
---|
81 | |
---|
82 | return array( |
---|
83 | 'c' => file_get_contents($F), |
---|
84 | 'w' => $this->getDestinationFile($type,$f) !== false, |
---|
85 | 'type' => $type, |
---|
86 | 'f' => $f |
---|
87 | ); |
---|
88 | } |
---|
89 | |
---|
90 | public function writeFile($type,$f,$content) |
---|
91 | { |
---|
92 | $files = $this->getFilesFromType($type); |
---|
93 | |
---|
94 | if (!isset($files[$f])) { |
---|
95 | throw new Exception(__('File does not exist.')); |
---|
96 | } |
---|
97 | |
---|
98 | try |
---|
99 | { |
---|
100 | $dest = $this->getDestinationFile($type,$f); |
---|
101 | |
---|
102 | if ($dest == false) { |
---|
103 | throw new Exception(); |
---|
104 | } |
---|
105 | |
---|
106 | if ($type == 'tpl' && !is_dir(dirname($dest))) { |
---|
107 | files::makeDir(dirname($dest)); |
---|
108 | } |
---|
109 | |
---|
110 | if ($type == 'po' && !is_dir(dirname($dest))) { |
---|
111 | files::makeDir(dirname($dest)); |
---|
112 | } |
---|
113 | |
---|
114 | $fp = @fopen($dest,'wb'); |
---|
115 | if (!$fp) { |
---|
116 | throw new Exception('tocatch'); |
---|
117 | } |
---|
118 | |
---|
119 | $content = preg_replace('/(\r?\n)/m',"\n",$content); |
---|
120 | $content = preg_replace('/\r/m',"\n",$content); |
---|
121 | |
---|
122 | fwrite($fp,$content); |
---|
123 | fclose($fp); |
---|
124 | |
---|
125 | # Updating inner files list |
---|
126 | $this->updateFileInList($type,$f,$dest); |
---|
127 | } |
---|
128 | catch (Exception $e) |
---|
129 | { |
---|
130 | throw new Exception(sprintf(__('Unable to write file %s. Please check your theme files and folders permissions.'),$f)); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | protected function getDestinationFile($type,$f) |
---|
135 | { |
---|
136 | if ($type == 'tpl') { |
---|
137 | $dest = $this->user_theme.'/tpl/'.$f; |
---|
138 | } elseif ($type == 'po') { |
---|
139 | $dest = $this->user_theme.'/locales/'.$f; |
---|
140 | } else { |
---|
141 | $dest = $this->user_theme.'/'.$f; |
---|
142 | } |
---|
143 | |
---|
144 | if (file_exists($dest) && is_writable($dest)) { |
---|
145 | return $dest; |
---|
146 | } |
---|
147 | |
---|
148 | if ($type == 'tpl' && !is_dir(dirname($dest))) { |
---|
149 | if (is_writable($this->user_theme)) { |
---|
150 | return $dest; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | if ($type == 'po' && !is_dir(dirname($dest))) { |
---|
155 | if (is_writable($this->user_theme)) { |
---|
156 | return $dest; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | if (is_writable(dirname($dest))) { |
---|
161 | return $dest; |
---|
162 | } |
---|
163 | |
---|
164 | return false; |
---|
165 | } |
---|
166 | |
---|
167 | protected function getFilesFromType($type) |
---|
168 | { |
---|
169 | switch ($type) |
---|
170 | { |
---|
171 | case 'tpl': |
---|
172 | return $this->tpl; |
---|
173 | case 'css': |
---|
174 | return $this->css; |
---|
175 | case 'js': |
---|
176 | return $this->js; |
---|
177 | case 'po': |
---|
178 | return $this->po; |
---|
179 | default: |
---|
180 | return array(); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | protected function updateFileInList($type,$f,$file) |
---|
185 | { |
---|
186 | switch ($type) |
---|
187 | { |
---|
188 | case 'tpl': |
---|
189 | $list =& $this->tpl; |
---|
190 | break; |
---|
191 | case 'css': |
---|
192 | $list =& $this->css; |
---|
193 | break; |
---|
194 | case 'js': |
---|
195 | $list =& $this->js; |
---|
196 | break; |
---|
197 | case 'po': |
---|
198 | $list =& $this->po; |
---|
199 | break; |
---|
200 | default: |
---|
201 | return; |
---|
202 | } |
---|
203 | |
---|
204 | $list[$f] = $file; |
---|
205 | } |
---|
206 | |
---|
207 | protected function findTemplates() |
---|
208 | { |
---|
209 | # First, we look in template paths |
---|
210 | $this->default_tpl = $this->getFilesInDir($this->default_theme.'/tpl'); |
---|
211 | |
---|
212 | $this->tpl = array_merge( |
---|
213 | $this->default_tpl, |
---|
214 | $this->getFilesInDir($this->parent_theme.'/tpl'), |
---|
215 | $this->getFilesInDir($this->user_theme.'/tpl') |
---|
216 | ); |
---|
217 | $this->tpl = array_merge($this->getFilesInDir(DC_ROOT.'/inc/public/default-templates'),$this->tpl); |
---|
218 | |
---|
219 | # Then we look in 'default-templates' plugins directory |
---|
220 | $plugins = $this->core->plugins->getModules(); |
---|
221 | foreach ($plugins as $p) { |
---|
222 | $this->tpl = array_merge($this->getFilesInDir($p['root'].'/default-templates'),$this->tpl); |
---|
223 | } |
---|
224 | |
---|
225 | uksort($this->tpl,array($this,'sortFilesHelper')); |
---|
226 | } |
---|
227 | |
---|
228 | protected function findStyles() |
---|
229 | { |
---|
230 | $this->css = $this->getFilesInDir($this->user_theme,'css'); |
---|
231 | $this->css= array_merge($this->css,$this->getFilesInDir($this->user_theme.'/style','css','style/')); |
---|
232 | } |
---|
233 | |
---|
234 | protected function findScripts() |
---|
235 | { |
---|
236 | $this->js = $this->getFilesInDir($this->user_theme,'js'); |
---|
237 | $this->js = array_merge($this->js,$this->getFilesInDir($this->user_theme.'/js','js','js/')); |
---|
238 | } |
---|
239 | |
---|
240 | protected function findLocales() |
---|
241 | { |
---|
242 | $langs = l10n::getISOcodes(1,1); |
---|
243 | foreach ($langs as $k => $v) { |
---|
244 | if ($this->parent_theme) { |
---|
245 | $this->po = array_merge($this->po,$this->getFilesInDir($this->parent_theme.'/locales/'.$v,'po',$v.'/')); |
---|
246 | } |
---|
247 | $this->po = array_merge($this->po,$this->getFilesInDir($this->user_theme.'/locales/'.$v,'po',$v.'/')); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | protected function getFilesInDir($dir,$ext=null,$prefix='',$model=null) |
---|
252 | { |
---|
253 | $dir = path::real($dir); |
---|
254 | if (!$dir || !is_dir($dir) || !is_readable($dir)) { |
---|
255 | return array(); |
---|
256 | } |
---|
257 | |
---|
258 | $d = dir($dir); |
---|
259 | $res = array(); |
---|
260 | while (($f = $d->read()) !== false) |
---|
261 | { |
---|
262 | if (is_file($dir.'/'.$f) && !preg_match('/^\./',$f) && (!$ext || preg_match('/\.'.preg_quote($ext).'$/i',$f))) { |
---|
263 | if (!$model || preg_match('/^'.preg_quote($model).'$/i', $f)) { |
---|
264 | $res[$prefix.$f] = $dir.'/'.$f; |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | return $res; |
---|
270 | } |
---|
271 | |
---|
272 | protected function sortFilesHelper($a,$b) |
---|
273 | { |
---|
274 | if ($a == $b) { |
---|
275 | return 0; |
---|
276 | } |
---|
277 | |
---|
278 | $ext_a = files::getExtension($a); |
---|
279 | $ext_b = files::getExtension($b); |
---|
280 | |
---|
281 | return strcmp($ext_a.'.'.$a,$ext_b.'.'.$b); |
---|
282 | } |
---|
283 | } |
---|
284 | ?> |
---|