File: class.themeEditor.php

package info (click to toggle)
dotclear 2.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,420 kB
  • sloc: php: 54,270; sql: 1,290; sh: 213; xml: 173; makefile: 158
file content (283 lines) | stat: -rw-r--r-- 6,692 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }

class dcThemeEditor
{
	protected $core;

	protected $default_theme;
	protected $user_theme;
	protected $parent_theme;

	protected $default_tpl = array();
	public $tpl = array();
	public $css = array();
	public $js  = array();
	public $po  = array();

	public function __construct($core)
	{
		$this->core =& $core;
		$this->default_theme = path::real($this->core->blog->themes_path.'/default');
		$this->user_theme = path::real($this->core->blog->themes_path.'/'.$this->core->blog->settings->system->theme);
		if (null !== $this->core->themes) {
			$parent_theme = $this->core->themes->moduleInfo($this->core->blog->settings->system->theme,'parent');
			if ($parent_theme) {
				$this->parent_theme = path::real($this->core->blog->themes_path.'/'.$parent_theme);
			}
		}
		$this->findTemplates();
		$this->findStyles();
		$this->findScripts();
		$this->findLocales();
	}

	public function filesList($type,$item='%1$s')
	{
		$files = $this->getFilesFromType($type);

		if (empty($files)) {
			return '<p>'.__('No file').'</p>';
		}

		$list = '';
		foreach ($files as $k => $v)
		{
			if (strpos($v,$this->user_theme) === 0) {
				$li = sprintf('<li class="default-file">%s</li>',$item);
			} elseif ($this->parent_theme && strpos($v,$this->parent_theme) === 0) {
				$li = sprintf('<li class="parent-file">%s</li>',$item);
			} else {
				$li = sprintf('<li>%s</li>',$item);
			}
			$list .= sprintf($li,$k,html::escapeHTML($k));
		}

		return sprintf('<ul>%s</ul>',$list);
	}

	public function getFileContent($type,$f)
	{
		$files = $this->getFilesFromType($type);

		if (!isset($files[$f])) {
			throw new Exception(__('File does not exist.'));
		}

		$F = $files[$f];
		if (!is_readable($F)) {
			throw new Exception(sprintf(__('File %s is not readable'),$f));
		}

		return array(
			'c' => file_get_contents($F),
			'w' => $this->getDestinationFile($type,$f) !== false,
			'type' => $type,
			'f' => $f
		);
	}

	public function writeFile($type,$f,$content)
	{
		$files = $this->getFilesFromType($type);

		if (!isset($files[$f])) {
			throw new Exception(__('File does not exist.'));
		}

		try
		{
			$dest = $this->getDestinationFile($type,$f);

			if ($dest == false) {
				throw new Exception();
			}

			if ($type == 'tpl' && !is_dir(dirname($dest))) {
				files::makeDir(dirname($dest));
			}

			if ($type == 'po' && !is_dir(dirname($dest))) {
				files::makeDir(dirname($dest));
			}

			$fp = @fopen($dest,'wb');
			if (!$fp) {
				throw new Exception('tocatch');
			}

			$content = preg_replace('/(\r?\n)/m',"\n",$content);
			$content = preg_replace('/\r/m',"\n",$content);

			fwrite($fp,$content);
			fclose($fp);

			# Updating inner files list
			$this->updateFileInList($type,$f,$dest);
		}
		catch (Exception $e)
		{
			throw new Exception(sprintf(__('Unable to write file %s. Please check your theme files and folders permissions.'),$f));
		}
	}

	protected function getDestinationFile($type,$f)
	{
		if ($type == 'tpl') {
			$dest = $this->user_theme.'/tpl/'.$f;
		} elseif ($type == 'po') {
			$dest = $this->user_theme.'/locales/'.$f;
		} else {
			$dest = $this->user_theme.'/'.$f;
		}

		if (file_exists($dest) && is_writable($dest)) {
			return $dest;
		}

		if ($type == 'tpl' && !is_dir(dirname($dest))) {
			if (is_writable($this->user_theme)) {
				return $dest;
			}
		}

		if ($type == 'po' && !is_dir(dirname($dest))) {
			if (is_writable($this->user_theme)) {
				return $dest;
			}
		}

		if (is_writable(dirname($dest))) {
			return $dest;
		}

		return false;
	}

	protected function getFilesFromType($type)
	{
		switch ($type)
		{
			case 'tpl':
				return $this->tpl;
			case 'css':
				return $this->css;
			case 'js':
				return $this->js;
			case 'po':
				return $this->po;
			default:
				return array();
		}
	}

	protected function updateFileInList($type,$f,$file)
	{
		switch ($type)
		{
			case 'tpl':
				$list =& $this->tpl;
				break;
			case 'css':
				$list =& $this->css;
				break;
			case 'js':
				$list =& $this->js;
				break;
			case 'po':
				$list =& $this->po;
				break;
			default:
				return;
		}

		$list[$f] = $file;
	}

	protected function findTemplates()
	{
		# First, we look in template paths
		$this->default_tpl = $this->getFilesInDir($this->default_theme.'/tpl');

		$this->tpl = array_merge(
			$this->default_tpl,
			$this->getFilesInDir($this->parent_theme.'/tpl'),
			$this->getFilesInDir($this->user_theme.'/tpl')
			);
		$this->tpl = array_merge($this->getFilesInDir(DC_ROOT.'/inc/public/default-templates'),$this->tpl);

		# Then we look in 'default-templates' plugins directory
		$plugins = $this->core->plugins->getModules();
		foreach ($plugins as $p) {
			$this->tpl = array_merge($this->getFilesInDir($p['root'].'/default-templates'),$this->tpl);
		}

		uksort($this->tpl,array($this,'sortFilesHelper'));
	}

	protected function findStyles()
	{
		$this->css = $this->getFilesInDir($this->user_theme,'css');
		$this->css= array_merge($this->css,$this->getFilesInDir($this->user_theme.'/style','css','style/'));
	}

	protected function findScripts()
	{
		$this->js = $this->getFilesInDir($this->user_theme,'js');
		$this->js = array_merge($this->js,$this->getFilesInDir($this->user_theme.'/js','js','js/'));
	}

	protected function findLocales()
	{
		$langs = l10n::getISOcodes(1,1);
		foreach ($langs as $k => $v) {
			if ($this->parent_theme) {
				$this->po = array_merge($this->po,$this->getFilesInDir($this->parent_theme.'/locales/'.$v,'po',$v.'/'));
			}
			$this->po = array_merge($this->po,$this->getFilesInDir($this->user_theme.'/locales/'.$v,'po',$v.'/'));
		}
	}

	protected function getFilesInDir($dir,$ext=null,$prefix='',$model=null)
	{
		$dir = path::real($dir);
		if (!$dir || !is_dir($dir) || !is_readable($dir)) {
			return array();
		}

		$d = dir($dir);
		$res = array();
		while (($f = $d->read()) !== false)
		{
			if (is_file($dir.'/'.$f) && !preg_match('/^\./',$f) && (!$ext || preg_match('/\.'.preg_quote($ext).'$/i',$f))) {
				if (!$model || preg_match('/^'.preg_quote($model).'$/i', $f)) {
					$res[$prefix.$f] = $dir.'/'.$f;
				}
			}
		}

		return $res;
	}

	protected function sortFilesHelper($a,$b)
	{
		if ($a == $b) {
			return 0;
		}

		$ext_a = files::getExtension($a);
		$ext_b = files::getExtension($b);

		return strcmp($ext_a.'.'.$a,$ext_b.'.'.$b);
	}
}