[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[1179] | 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 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; |
---|
[2566] | 17 | |
---|
[0] | 18 | protected $user_theme; |
---|
| 19 | protected $parent_theme; |
---|
[2601] | 20 | protected $tplset_theme; |
---|
[2566] | 21 | |
---|
[2890] | 22 | protected $parent_name; |
---|
[2862] | 23 | protected $tplset_name; |
---|
| 24 | |
---|
[0] | 25 | public $tpl = array(); |
---|
| 26 | public $css = array(); |
---|
| 27 | public $js = array(); |
---|
[1036] | 28 | public $po = array(); |
---|
[2566] | 29 | |
---|
[0] | 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); |
---|
[2607] | 35 | $this->tplset_theme = DC_ROOT.'/inc/public/default-templates/'.DC_DEFAULT_TPLSET; |
---|
[2862] | 36 | $this->tplset_name = DC_DEFAULT_TPLSET; |
---|
[0] | 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); |
---|
[2890] | 41 | $this->parent_name = $parent_theme; |
---|
[0] | 42 | } |
---|
[2601] | 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; |
---|
[2862] | 46 | $this->tplset_name = $tplset; |
---|
[2601] | 47 | } |
---|
[0] | 48 | } |
---|
| 49 | $this->findTemplates(); |
---|
| 50 | $this->findStyles(); |
---|
| 51 | $this->findScripts(); |
---|
[1036] | 52 | $this->findLocales(); |
---|
[0] | 53 | } |
---|
[2566] | 54 | |
---|
[2647] | 55 | public function filesList($type,$item='%1$s',$split=true) |
---|
[0] | 56 | { |
---|
| 57 | $files = $this->getFilesFromType($type); |
---|
[2566] | 58 | |
---|
[0] | 59 | if (empty($files)) { |
---|
| 60 | return '<p>'.__('No file').'</p>'; |
---|
| 61 | } |
---|
[2566] | 62 | |
---|
[0] | 63 | $list = ''; |
---|
[2647] | 64 | if ($split) { |
---|
[2651] | 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 |
---|
[2647] | 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)); |
---|
[2651] | 73 | } elseif ($this->parent_theme && strpos($v,$this->parent_theme) === 0) { |
---|
[2647] | 74 | $li = sprintf('<li class="parent-file">%s</li>',$item); |
---|
| 75 | $list_parent .= sprintf($li,$k,html::escapeHTML($k)); |
---|
[2651] | 76 | } else { |
---|
[2647] | 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) : ''); |
---|
[2890] | 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) : ''); |
---|
[2647] | 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 | } |
---|
[0] | 98 | } |
---|
[2566] | 99 | |
---|
[0] | 100 | return sprintf('<ul>%s</ul>',$list); |
---|
| 101 | } |
---|
[2566] | 102 | |
---|
[0] | 103 | public function getFileContent($type,$f) |
---|
| 104 | { |
---|
| 105 | $files = $this->getFilesFromType($type); |
---|
[2566] | 106 | |
---|
[0] | 107 | if (!isset($files[$f])) { |
---|
| 108 | throw new Exception(__('File does not exist.')); |
---|
| 109 | } |
---|
[2566] | 110 | |
---|
[0] | 111 | $F = $files[$f]; |
---|
| 112 | if (!is_readable($F)) { |
---|
| 113 | throw new Exception(sprintf(__('File %s is not readable'),$f)); |
---|
| 114 | } |
---|
[2566] | 115 | |
---|
[0] | 116 | return array( |
---|
| 117 | 'c' => file_get_contents($F), |
---|
| 118 | 'w' => $this->getDestinationFile($type,$f) !== false, |
---|
| 119 | 'type' => $type, |
---|
| 120 | 'f' => $f |
---|
| 121 | ); |
---|
| 122 | } |
---|
[2566] | 123 | |
---|
[0] | 124 | public function writeFile($type,$f,$content) |
---|
| 125 | { |
---|
| 126 | $files = $this->getFilesFromType($type); |
---|
[2566] | 127 | |
---|
[0] | 128 | if (!isset($files[$f])) { |
---|
| 129 | throw new Exception(__('File does not exist.')); |
---|
| 130 | } |
---|
[2566] | 131 | |
---|
[0] | 132 | try |
---|
| 133 | { |
---|
| 134 | $dest = $this->getDestinationFile($type,$f); |
---|
[2566] | 135 | |
---|
[0] | 136 | if ($dest == false) { |
---|
| 137 | throw new Exception(); |
---|
| 138 | } |
---|
[2566] | 139 | |
---|
[0] | 140 | if ($type == 'tpl' && !is_dir(dirname($dest))) { |
---|
| 141 | files::makeDir(dirname($dest)); |
---|
| 142 | } |
---|
[2566] | 143 | |
---|
[1036] | 144 | if ($type == 'po' && !is_dir(dirname($dest))) { |
---|
| 145 | files::makeDir(dirname($dest)); |
---|
| 146 | } |
---|
[2566] | 147 | |
---|
[0] | 148 | $fp = @fopen($dest,'wb'); |
---|
| 149 | if (!$fp) { |
---|
| 150 | throw new Exception('tocatch'); |
---|
| 151 | } |
---|
[2566] | 152 | |
---|
[0] | 153 | $content = preg_replace('/(\r?\n)/m',"\n",$content); |
---|
| 154 | $content = preg_replace('/\r/m',"\n",$content); |
---|
[2566] | 155 | |
---|
[0] | 156 | fwrite($fp,$content); |
---|
| 157 | fclose($fp); |
---|
[2566] | 158 | |
---|
[0] | 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 | } |
---|
[2566] | 167 | |
---|
[2663] | 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 | |
---|
[0] | 219 | protected function getDestinationFile($type,$f) |
---|
| 220 | { |
---|
| 221 | if ($type == 'tpl') { |
---|
| 222 | $dest = $this->user_theme.'/tpl/'.$f; |
---|
[1036] | 223 | } elseif ($type == 'po') { |
---|
| 224 | $dest = $this->user_theme.'/locales/'.$f; |
---|
[0] | 225 | } else { |
---|
| 226 | $dest = $this->user_theme.'/'.$f; |
---|
| 227 | } |
---|
[2566] | 228 | |
---|
[0] | 229 | if (file_exists($dest) && is_writable($dest)) { |
---|
| 230 | return $dest; |
---|
| 231 | } |
---|
[2566] | 232 | |
---|
[0] | 233 | if ($type == 'tpl' && !is_dir(dirname($dest))) { |
---|
| 234 | if (is_writable($this->user_theme)) { |
---|
| 235 | return $dest; |
---|
| 236 | } |
---|
| 237 | } |
---|
[1036] | 238 | |
---|
| 239 | if ($type == 'po' && !is_dir(dirname($dest))) { |
---|
| 240 | if (is_writable($this->user_theme)) { |
---|
| 241 | return $dest; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
[0] | 245 | if (is_writable(dirname($dest))) { |
---|
| 246 | return $dest; |
---|
| 247 | } |
---|
[2566] | 248 | |
---|
[0] | 249 | return false; |
---|
| 250 | } |
---|
[2566] | 251 | |
---|
[0] | 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; |
---|
[1036] | 262 | case 'po': |
---|
| 263 | return $this->po; |
---|
[0] | 264 | default: |
---|
| 265 | return array(); |
---|
| 266 | } |
---|
| 267 | } |
---|
[2566] | 268 | |
---|
[0] | 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; |
---|
[1036] | 282 | case 'po': |
---|
| 283 | $list =& $this->po; |
---|
| 284 | break; |
---|
[0] | 285 | default: |
---|
| 286 | return; |
---|
| 287 | } |
---|
[2566] | 288 | |
---|
[0] | 289 | $list[$f] = $file; |
---|
| 290 | } |
---|
[2566] | 291 | |
---|
[0] | 292 | protected function findTemplates() |
---|
| 293 | { |
---|
| 294 | $this->tpl = array_merge( |
---|
[2601] | 295 | $this->getFilesInDir($this->tplset_theme), |
---|
[2663] | 296 | $this->getFilesInDir($this->parent_theme.'/tpl') |
---|
[0] | 297 | ); |
---|
[2663] | 298 | $this->tpl_model = $this->tpl; |
---|
| 299 | |
---|
| 300 | $this->tpl = array_merge($this->tpl,$this->getFilesInDir($this->user_theme.'/tpl')); |
---|
[2566] | 301 | |
---|
[0] | 302 | # Then we look in 'default-templates' plugins directory |
---|
| 303 | $plugins = $this->core->plugins->getModules(); |
---|
| 304 | foreach ($plugins as $p) { |
---|
[2862] | 305 | // Looking in default-templates directory |
---|
[0] | 306 | $this->tpl = array_merge($this->getFilesInDir($p['root'].'/default-templates'),$this->tpl); |
---|
[2663] | 307 | $this->tpl_model = array_merge($this->getFilesInDir($p['root'].'/default-templates'),$this->tpl_model); |
---|
[2862] | 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); |
---|
[0] | 311 | } |
---|
[2566] | 312 | |
---|
[0] | 313 | uksort($this->tpl,array($this,'sortFilesHelper')); |
---|
| 314 | } |
---|
[2566] | 315 | |
---|
[0] | 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 | } |
---|
[2566] | 321 | |
---|
[0] | 322 | protected function findScripts() |
---|
| 323 | { |
---|
| 324 | $this->js = $this->getFilesInDir($this->user_theme,'js'); |
---|
| 325 | $this->js = array_merge($this->js,$this->getFilesInDir($this->user_theme.'/js','js','js/')); |
---|
| 326 | } |
---|
[2566] | 327 | |
---|
[1036] | 328 | protected function findLocales() |
---|
| 329 | { |
---|
| 330 | $langs = l10n::getISOcodes(1,1); |
---|
| 331 | foreach ($langs as $k => $v) { |
---|
| 332 | if ($this->parent_theme) { |
---|
[1129] | 333 | $this->po = array_merge($this->po,$this->getFilesInDir($this->parent_theme.'/locales/'.$v,'po',$v.'/')); |
---|
[1036] | 334 | } |
---|
[1129] | 335 | $this->po = array_merge($this->po,$this->getFilesInDir($this->user_theme.'/locales/'.$v,'po',$v.'/')); |
---|
[1036] | 336 | } |
---|
| 337 | } |
---|
[2566] | 338 | |
---|
[1037] | 339 | protected function getFilesInDir($dir,$ext=null,$prefix='',$model=null) |
---|
[0] | 340 | { |
---|
| 341 | $dir = path::real($dir); |
---|
| 342 | if (!$dir || !is_dir($dir) || !is_readable($dir)) { |
---|
| 343 | return array(); |
---|
| 344 | } |
---|
[2566] | 345 | |
---|
[0] | 346 | $d = dir($dir); |
---|
| 347 | $res = array(); |
---|
| 348 | while (($f = $d->read()) !== false) |
---|
| 349 | { |
---|
| 350 | if (is_file($dir.'/'.$f) && !preg_match('/^\./',$f) && (!$ext || preg_match('/\.'.preg_quote($ext).'$/i',$f))) { |
---|
[1037] | 351 | if (!$model || preg_match('/^'.preg_quote($model).'$/i', $f)) { |
---|
| 352 | $res[$prefix.$f] = $dir.'/'.$f; |
---|
| 353 | } |
---|
[0] | 354 | } |
---|
| 355 | } |
---|
[2566] | 356 | |
---|
[0] | 357 | return $res; |
---|
| 358 | } |
---|
[2566] | 359 | |
---|
[0] | 360 | protected function sortFilesHelper($a,$b) |
---|
| 361 | { |
---|
| 362 | if ($a == $b) { |
---|
| 363 | return 0; |
---|
| 364 | } |
---|
[2566] | 365 | |
---|
[0] | 366 | $ext_a = files::getExtension($a); |
---|
| 367 | $ext_b = files::getExtension($b); |
---|
[2566] | 368 | |
---|
[0] | 369 | return strcmp($ext_a.'.'.$a,$ext_b.'.'.$b); |
---|
| 370 | } |
---|
| 371 | } |
---|