Dotclear

source: plugins/themeEditor/class.themeEditor.php @ 2981:801959e1f6e9

Revision 2981:801959e1f6e9, 9.6 KB checked in by franck <carnet.franck.paul@…>, 10 years ago (diff)

Cope with css theme sub-folder, fix #2082

Line 
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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class dcThemeEditor
15{
16     protected $core;
17
18     protected $user_theme;
19     protected $parent_theme;
20     protected $tplset_theme;
21
22     protected $parent_name;
23     protected $tplset_name;
24
25     public $tpl = array();
26     public $css = array();
27     public $js  = array();
28     public $po  = array();
29
30     public function __construct($core)
31     {
32          $this->core =& $core;
33          $this->default_theme = path::real($this->core->blog->themes_path.'/default');
34          $this->user_theme = path::real($this->core->blog->themes_path.'/'.$this->core->blog->settings->system->theme);
35          $this->tplset_theme = DC_ROOT.'/inc/public/default-templates/'.DC_DEFAULT_TPLSET;
36          $this->tplset_name = DC_DEFAULT_TPLSET;
37          if (null !== $this->core->themes) {
38               $parent_theme = $this->core->themes->moduleInfo($this->core->blog->settings->system->theme,'parent');
39               if ($parent_theme) {
40                    $this->parent_theme = path::real($this->core->blog->themes_path.'/'.$parent_theme);
41                    $this->parent_name = $parent_theme;
42               }
43               $tplset = $this->core->themes->moduleInfo($this->core->blog->settings->system->theme,'tplset');
44               if ($tplset) {
45                    $this->tplset_theme = DC_ROOT.'/inc/public/default-templates/'.$tplset;
46                    $this->tplset_name = $tplset;
47               }
48          }
49          $this->findTemplates();
50          $this->findStyles();
51          $this->findScripts();
52          $this->findLocales();
53     }
54
55     public function filesList($type,$item='%1$s',$split=true)
56     {
57          $files = $this->getFilesFromType($type);
58
59          if (empty($files)) {
60               return '<p>'.__('No file').'</p>';
61          }
62
63          $list = '';
64          if ($split) {
65               $list_theme = '';   // Files from current theme
66               $list_parent = '';  // Files from parent of current theme
67               $list_tpl = '';          // Files from template set used by current theme
68               foreach ($files as $k => $v)
69               {
70                    if (strpos($v,$this->user_theme) === 0) {
71                         $li = sprintf('<li class="default-file">%s</li>',$item);
72                         $list_theme .= sprintf($li,$k,html::escapeHTML($k));
73                    } elseif ($this->parent_theme && strpos($v,$this->parent_theme) === 0) {
74                         $li = sprintf('<li class="parent-file">%s</li>',$item);
75                         $list_parent .= sprintf($li,$k,html::escapeHTML($k));
76                    } else {
77                         $li = sprintf('<li>%s</li>',$item);
78                         $list_tpl .= sprintf($li,$k,html::escapeHTML($k));
79                    }
80               }
81               $list .= ($list_theme != '' ? sprintf('<li class="group-file">'.__('From theme:').'<ul>%s</ul></li>',$list_theme) : '');
82               $list .= ($list_parent != '' ? sprintf('<li class="group-file">'.__('From parent:').' %s<ul>%s</ul></li>',
83                    $this->parent_name,$list_parent) : '');
84               $list .= ($list_tpl != '' ? sprintf('<li class="group-file">'.__('From template set:').' %s<ul>%s</ul></li>',
85                    $this->tplset_name,$list_tpl) : '');
86          } else {
87               foreach ($files as $k => $v)
88               {
89                    if (strpos($v,$this->user_theme) === 0) {
90                         $li = sprintf('<li class="default-file">%s</li>',$item);
91                    } elseif ($this->parent_theme && strpos($v,$this->parent_theme) === 0) {
92                         $li = sprintf('<li class="parent-file">%s</li>',$item);
93                    } else {
94                         $li = sprintf('<li>%s</li>',$item);
95                    }
96                    $list .= sprintf($li,$k,html::escapeHTML($k));
97               }
98          }
99
100          return sprintf('<ul>%s</ul>',$list);
101     }
102
103     public function getFileContent($type,$f)
104     {
105          $files = $this->getFilesFromType($type);
106
107          if (!isset($files[$f])) {
108               throw new Exception(__('File does not exist.'));
109          }
110
111          $F = $files[$f];
112          if (!is_readable($F)) {
113               throw new Exception(sprintf(__('File %s is not readable'),$f));
114          }
115
116          return array(
117               'c' => file_get_contents($F),
118               'w' => $this->getDestinationFile($type,$f) !== false,
119               'type' => $type,
120               'f' => $f
121          );
122     }
123
124     public function writeFile($type,$f,$content)
125     {
126          $files = $this->getFilesFromType($type);
127
128          if (!isset($files[$f])) {
129               throw new Exception(__('File does not exist.'));
130          }
131
132          try
133          {
134               $dest = $this->getDestinationFile($type,$f);
135
136               if ($dest == false) {
137                    throw new Exception();
138               }
139
140               if ($type == 'tpl' && !is_dir(dirname($dest))) {
141                    files::makeDir(dirname($dest));
142               }
143
144               if ($type == 'po' && !is_dir(dirname($dest))) {
145                    files::makeDir(dirname($dest));
146               }
147
148               $fp = @fopen($dest,'wb');
149               if (!$fp) {
150                    throw new Exception('tocatch');
151               }
152
153               $content = preg_replace('/(\r?\n)/m',"\n",$content);
154               $content = preg_replace('/\r/m',"\n",$content);
155
156               fwrite($fp,$content);
157               fclose($fp);
158
159               # Updating inner files list
160               $this->updateFileInList($type,$f,$dest);
161          }
162          catch (Exception $e)
163          {
164               throw new Exception(sprintf(__('Unable to write file %s. Please check your theme files and folders permissions.'),$f));
165          }
166     }
167
168     public function deletableFile($type,$f)
169     {
170          if ($type != 'tpl') {
171               // Only tpl files may be deleted
172               return false;
173          }
174
175          $files = $this->getFilesFromType($type);
176          if (isset($files[$f])) {
177               $dest = $this->getDestinationFile($type,$f);
178               if ($dest) {
179                    if (file_exists($dest) && is_writable($dest)) {
180                         // Is there a model (parent theme or template set) ?
181                         if (isset($this->tpl_model[$f])) {
182                              return true;
183                         }
184                    }
185               }
186          }
187          return false;
188     }
189
190     public function deleteFile($type,$f)
191     {
192          if ($type != 'tpl') {
193               // Only tpl files may be deleted
194               return;
195          }
196
197          $files = $this->getFilesFromType($type);
198          if (!isset($files[$f])) {
199               throw new Exception(__('File does not exist.'));
200          }
201
202          try
203          {
204               $dest = $this->getDestinationFile($type,$f);
205               if ($dest) {
206                    // File exists and may be deleted
207                    unlink($dest);
208
209                    // Updating template files list
210                    $this->findTemplates();
211               }
212          }
213          catch (Exception $e)
214          {
215               throw new Exception(sprintf(__('Unable to delete file %s. Please check your theme files and folders permissions.'),$f));
216          }
217     }
218
219     protected function getDestinationFile($type,$f)
220     {
221          if ($type == 'tpl') {
222               $dest = $this->user_theme.'/tpl/'.$f;
223          } elseif ($type == 'po') {
224               $dest = $this->user_theme.'/locales/'.$f;
225          } else {
226               $dest = $this->user_theme.'/'.$f;
227          }
228
229          if (file_exists($dest) && is_writable($dest)) {
230               return $dest;
231          }
232
233          if ($type == 'tpl' && !is_dir(dirname($dest))) {
234               if (is_writable($this->user_theme)) {
235                    return $dest;
236               }
237          }
238
239          if ($type == 'po' && !is_dir(dirname($dest))) {
240               if (is_writable($this->user_theme)) {
241                    return $dest;
242               }
243          }
244
245          if (is_writable(dirname($dest))) {
246               return $dest;
247          }
248
249          return false;
250     }
251
252     protected function getFilesFromType($type)
253     {
254          switch ($type)
255          {
256               case 'tpl':
257                    return $this->tpl;
258               case 'css':
259                    return $this->css;
260               case 'js':
261                    return $this->js;
262               case 'po':
263                    return $this->po;
264               default:
265                    return array();
266          }
267     }
268
269     protected function updateFileInList($type,$f,$file)
270     {
271          switch ($type)
272          {
273               case 'tpl':
274                    $list =& $this->tpl;
275                    break;
276               case 'css':
277                    $list =& $this->css;
278                    break;
279               case 'js':
280                    $list =& $this->js;
281                    break;
282               case 'po':
283                    $list =& $this->po;
284                    break;
285               default:
286                    return;
287          }
288
289          $list[$f] = $file;
290     }
291
292     protected function findTemplates()
293     {
294          $this->tpl = array_merge(
295               $this->getFilesInDir($this->tplset_theme),
296               $this->getFilesInDir($this->parent_theme.'/tpl')
297               );
298          $this->tpl_model = $this->tpl;
299
300          $this->tpl = array_merge($this->tpl,$this->getFilesInDir($this->user_theme.'/tpl'));
301
302          # Then we look in 'default-templates' plugins directory
303          $plugins = $this->core->plugins->getModules();
304          foreach ($plugins as $p) {
305               // Looking in default-templates directory
306               $this->tpl = array_merge($this->getFilesInDir($p['root'].'/default-templates'),$this->tpl);
307               $this->tpl_model = array_merge($this->getFilesInDir($p['root'].'/default-templates'),$this->tpl_model);
308               // Looking in default-templates/tplset directory
309               $this->tpl = array_merge($this->getFilesInDir($p['root'].'/default-templates/'.$this->tplset_name),$this->tpl);
310               $this->tpl_model = array_merge($this->getFilesInDir($p['root'].'/default-templates/'.$this->tplset_name),$this->tpl_model);
311          }
312
313          uksort($this->tpl,array($this,'sortFilesHelper'));
314     }
315
316     protected function findStyles()
317     {
318          $this->css = $this->getFilesInDir($this->user_theme,'css');
319          $this->css= array_merge($this->css,$this->getFilesInDir($this->user_theme.'/style','css','style/'));
320          $this->css= array_merge($this->css,$this->getFilesInDir($this->user_theme.'/css','css','css/'));
321     }
322
323     protected function findScripts()
324     {
325          $this->js = $this->getFilesInDir($this->user_theme,'js');
326          $this->js = array_merge($this->js,$this->getFilesInDir($this->user_theme.'/js','js','js/'));
327     }
328
329     protected function findLocales()
330     {
331          $langs = l10n::getISOcodes(1,1);
332          foreach ($langs as $k => $v) {
333               if ($this->parent_theme) {
334                    $this->po = array_merge($this->po,$this->getFilesInDir($this->parent_theme.'/locales/'.$v,'po',$v.'/'));
335               }
336               $this->po = array_merge($this->po,$this->getFilesInDir($this->user_theme.'/locales/'.$v,'po',$v.'/'));
337          }
338     }
339
340     protected function getFilesInDir($dir,$ext=null,$prefix='',$model=null)
341     {
342          $dir = path::real($dir);
343          if (!$dir || !is_dir($dir) || !is_readable($dir)) {
344               return array();
345          }
346
347          $d = dir($dir);
348          $res = array();
349          while (($f = $d->read()) !== false)
350          {
351               if (is_file($dir.'/'.$f) && !preg_match('/^\./',$f) && (!$ext || preg_match('/\.'.preg_quote($ext).'$/i',$f))) {
352                    if (!$model || preg_match('/^'.preg_quote($model).'$/i', $f)) {
353                         $res[$prefix.$f] = $dir.'/'.$f;
354                    }
355               }
356          }
357
358          return $res;
359     }
360
361     protected function sortFilesHelper($a,$b)
362     {
363          if ($a == $b) {
364               return 0;
365          }
366
367          $ext_a = files::getExtension($a);
368          $ext_b = files::getExtension($b);
369
370          return strcmp($ext_a.'.'.$a,$ext_b.'.'.$b);
371     }
372}
Note: See TracBrowser for help on using the repository browser.

Sites map