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