Dotclear

source: plugins/themeEditor/class.themeEditor.php @ 2651:f691be17b0a9

Revision 2651:f691be17b0a9, 7.7 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

One loop is better than three, addresses #1704

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

Sites map