csslib.php 4.61 KB
Newer Older
1
<?php
2
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
3
//
4 5 6
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
7

8
//this script may only be included - so its better to die if called directly.
9 10 11
if (strpos($_SERVER['SCRIPT_NAME'], basename(__FILE__)) !== false) {
	header('location: index.php');
	exit;
12 13
}

14 15
class cssLib extends TikiLib
{
16 17 18
	function list_layouts()
	{
		$available_layouts = array();
19
		foreach (scandir(TIKI_PATH . '/templates/layouts/') as $layoutName) {
20 21 22 23 24 25 26 27
			if ($layoutName[0] != '.' && $layoutName != 'index.php') {
				$available_layouts[$layoutName] = ucfirst($layoutName);
			}   
		}   

		return $available_layouts;
	}

28 29
	function list_css($path, $recursive = false)
	{
30 31 32 33 34 35
		$files = $this->list_files($path, '.css', $recursive);
		foreach ($files as $i=>$file) {
			$files[$i] = preg_replace("|^$path/(.*)\.css$|", '$1', $file);
		}
		return $files;
	}
36

37 38
	function list_files($path, $extension, $recursive)
	{
39
		$back = array();
40

41
		$handle = opendir($path);
42

43 44 45
		while ($file = readdir($handle)) {
			if ((substr($file, -4, 4) == $extension) and (preg_match('/^[-_a-zA-Z0-9\.]*$/', $file))) {
				$back[] = "$path/$file";
46 47 48 49 50 51 52
			} elseif ($recursive 
								&& $file != '.svn' 
								&& $file != '.' 
								&& $file != '..' 
								&& is_dir("$path/$file") 
								&& !file_exists("db/$file/local.php")
			) {
53
				$back = array_merge($back, $this->list_files("$path/$file", $extension, $recursive));
54 55
			}
		}
56
		closedir($handle);
57
		sort($back);
58 59
		return $back;
	}
60

61
	/* nickname = fivealive or Bidi/Bidi */
62
	/* write = in multidomain we always write in subdir domain but we read domain/file if exists or file */
63 64
	function get_nickname_path($nickname, $styledir, $write=false)
	{
65
		global $tikidomain;
66
		if (!strstr($nickname, '\/') && !empty($tikidomain) && (is_file("$styledir/$tikidomain/$nickname.css") || $write)) {
67 68 69 70 71
			$style = "$styledir/$tikidomain/$nickname.css";
		} else {
			$style = "$styledir/$nickname.css";
		}
		return $style;
72
	}
73

74 75
	function browse_css($path)
	{
76
		if (!is_file($path)) {
77
			return array('error' => "No such file : $path");
78
		}
79

80
		$meat = implode('', file($path));
81

82
		$find[0] = '/\}/';
83 84
		$repl[0] = "\n}\n";

85
		$find[1] = '/\{/';
86 87
		$repl[1] = "\n{\n";

88
		$find[2] = '/\/\*/';
89 90
		$repl[2] = "\n/*\n";

91
		$find[3] = '/\*\//';
92 93
		$repl[3] = "\n*/\n";

94
		$find[4] = '/;/';
95 96
		$repl[4] = ";\n";

97 98
		$find[5] = '/(W|w)hite/';
		$repl[5] = '#FFFFFF';
99

100 101
		$find[6] = '/(B|b)lack/';
		$repl[6] = '#000000';
102 103

		$res = preg_replace($find, $repl, $meat);
104
		return array(
105 106
			'error' => '',
			'content' => explode("\n", $res)
107
		);
108 109
	}

110 111
	function parse_css($data)
	{
112
		$back = array();
113

114 115
		$index = 0;
		$type = '';
116

117 118
		foreach ($data as $line) {
			$line = trim($line);
119 120

			if ($line) {
121 122
				if (($type != 'comment') and ($line == '/*')) {
					$type = 'comment';
123

124
					$index++;
125 126 127 128 129 130 131 132 133 134
					$back["$index"]['comment'] = '';
					$back["$index"]['items'] = array();
					$back["$index"]['attributes'] = array();
				} elseif (($type == 'comment') and ($line == '*/')) {
					$type = '';
				} elseif ($type == 'comment') {
					$back["$index"]['comment'] .= "$line\n";
				} elseif (($type == 'items') and ($line == '{')) {
					$type = 'attributes';
				} elseif ($type == 'items') {
135
					$li = explode(',', $line);
136

137 138
					foreach ($li as $l) {
						$l = trim($l);
139 140

						if ($l)
141
							$back["$index"]['items'][] = $l;
142
					}
143 144
				} elseif (($type == 'attributes') and ($line == '}')) {
					$type = '';
145

146
					$index++;
147 148 149 150 151
					$back["$index"]['comment'] = '';
					$back["$index"]['items'] = array();
					$back["$index"]['attributes'] = array();
				} elseif ($type == 'attributes') {
					$parts = explode(':', str_replace(';', '', $line));
152

153 154
					if (isset($parts[0]) && isset($parts[1])) {
						$obj = trim($parts[0]);
155

156
						$back["$index"]['attributes']["$obj"] = trim($parts[1]);
157 158
					}
				} else {
159
					$li = explode(',', $line);
160

161 162
					foreach ($li as $l) {
						$l = trim($l);
163 164

						if ($l)
165
							$back["$index"]['items'][] = $l;
166
					}
167

168
					$type = 'items';
169
				}
170

171
				$back['content'] = $line;
172 173
			}
		}
174

175 176
		return $back;
	}
177 178

	/**
179
	 * Find the version of Tiki that a CSS is compatible with
180
	 *
181 182
	 * @TODO: cache the results
	 * @TODO: only read the first 30 lines or so of the file
183
	 */
184 185
	function version_css($path)
	{
186 187
		if (!file_exists($path))
			return false;
188 189 190 191 192 193 194

		$data = implode('', file($path));
		$pos = strpos($data, '@version');

		if ( $pos === false ) {
			return false;
		}
195
		// get version
196 197 198
		preg_match("/(@[V|v]ersion):?\s?([\d]+)\.([\d]+)/i", $data, $matches);
		$version = $matches[2] . '.' . $matches[3];

199 200
		return $version;
	}
201
}