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