summaryrefslogtreecommitdiff
path: root/code/admin/utils.php
blob: 4db2d2d1384a3b4584848f12e3edcc3dcdee974e (plain)
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
<?php
	define('RE_TEXT_LINE_CLEANER', '/["\p{C}\p{Zl}\p{Zp}]+/u');
	define('RE_IDENTIFIER_CLEANER', '/[^a-zA-Z0-9_]+/');
	define('RE_LANG_IDENT_CLEANER', '/[^a-zA-Z\/\_-]+/');
	define('RE_RELPATH_CLEANER', '/[^a-zA-Z0-9_\/-]+/'); // Never put \. in this regex

	function sanitize($arg_array, $arg_key, $replace_chars_re, $default_value) {
		//FIXME :  should check string type and strlen !
		if ( ! array_key_exists($arg_key, $arg_array) ) return $default_value;
		return preg_replace($replace_chars_re, '_', $arg_array[$arg_key]);
	}

	function sanitize_ini($ini_path, $array_entry_props) {
		$array_ini = parse_ini_file($ini_path);
		if ( is_array($array_ini) ) { 
			// Sanitize any existing ini entries. Destroy unwanted ones.
			foreach ( $array_ini as $k => $v ) {
				if ( array_key_exists($k, $array_entry_props)
					&& array_key_exists('replace_chars_re', $array_entry_props[$k])
					&& array_key_exists('default_value', $array_entry_props[$k])
				) {
					$array_ini[$k] = sanitize($array_ini, $k,
					       	$array_entry_props[$k]['replace_chars_re'],
						$array_entry_props[$k]['default_value'] );
				} else {
					unset($array_ini[$k]);
				}
			}
			// Set default value for all missing ini entries (if default value exists)
			foreach ( $array_entry_props as $k => $v ) {
				if ( !array_key_exists($k, $array_ini) && array_key_exists('default_value', $array_entry_props[$k]) ) {
					$array_ini[$k] = $array_entry_props[$k]['default_value'];
				}
			}
		}
		return $array_ini;
	}

	function load_ini_site_conf($ini_path) {
		$sanitize_site_conf = array(
			'site_admin_lang'	=> array( 'replace_chars_re' => RE_LANG_IDENT_CLEANER, 'default_value' => 'C' ),
			'site_default_page'	=> array( 'replace_chars_re' => RE_RELPATH_CLEANER, 'default_value' => 'en/index' ),
		);
		return sanitize_ini($ini_path, $sanitize_site_conf);
	}

	function load_ini_page_props($page) {
		
		$sanitize_page_props = array(
			//FIXME : title regex : all but html special chars ?
			'page_template'		=> array( 'replace_chars_re' => RE_IDENTIFIER_CLEANER, 'default_value' => 'default' ),
			'page_layout'   	=> array( 'replace_chars_re' => RE_IDENTIFIER_CLEANER, 'default_value' => 'article' ),
			'page_title'		=> array( 'replace_chars_re' => RE_TEXT_LINE_CLEANER, 'default_value' => '(missing)' ),
			'page_description'	=> array( 'replace_chars_re' => RE_TEXT_LINE_CLEANER, 'default_value' => '(missing)' ),
			'page_keywords'		=> array( 'replace_chars_re' => RE_TEXT_LINE_CLEANER, 'default_value' => '(missing)' ),
		);
		$ini_path="content/$page/props.ini";
		return sanitize_ini($ini_path, $sanitize_page_props);
	}

	function l10n_init($lang) {
		setlocale(LC_MESSAGES, "$lang.utf8");
		$base = bindtextdomain('editablesite', './locale');
		$domain = textdomain('editablesite');
		bind_textdomain_codeset('editablesite', 'UTF-8');
		//echo "<pre>l10n file is '$base/$lang.utf8/LC_MESSAGES/$domain.mo'</pre>\n";
	}

	function need_auth() {
		session_start();
		if ( ! array_key_exists('auth_user', $_SESSION) || $_SESSION['auth_user'] !== TRUE ) {
			$_SESSION['auth_return'] = $_SERVER['REQUEST_URI'];
			header('Location: auth.php');
			exit();
		}
	}

	function is_ress($kind, $path) {
		switch ($kind) {
			case 'page':	return ( is_dir($path) && is_file($path.'/props.ini') );
			case 'media':	return ( substr($path, -4)=='.jpg' && is_file($path) );
			default :	return FALSE;
		}
	}

	function strcmp_tree($a,$b) {
		if (is_array($a) || is_array($b) ) return 0;
		return strnatcasecmp($a,$b);
	}

	function find_all($path, $kind) {
		$result=array();
		if ( $handle = opendir($path) ) {
			while (FALSE !== ($entry = readdir($handle))) {
				if ( array_search($entry, array('.','..')) !== FALSE ) continue;
				$childpath=$path.'/'.$entry;
				if ( is_ress($kind, $childpath) ) $result[] = $entry;
				else if ( is_dir($childpath) ) $result[$entry]=find_all($childpath,$kind);
			}
			closedir($handle);
		}
		uasort($result, 'strcmp_tree');
		return $result;
	}

	function php_array_to_tree($page_tree, $node_cb, $leaf_cb='', $path='', $itemid="item-0") {
		if ( ! is_array($page_tree) ) return;
		echo "<ul>\n";
		foreach ($page_tree as $k => $v) {
			if ( is_numeric($k) ) {
				// Leaf
				if ( strlen($leaf_cb) > 0 ) {
					echo '<li><a href="javascript:' . $leaf_cb . "('$path$v');" . '">' . $v . '</a></li>' . "\n";
				} else {
					echo "<li>$v</li>\n";
				}
			} else {
				// Node
				// Increment $itemid last component
				// "1-1-2-1" will come "1-1-2-2"
				$tmp = explode('-', $itemid);
				array_push($tmp, array_pop($tmp) + 1);
				$itemid = implode('-', $tmp);
				$tmp=null;
				/// Write the node
				echo "<li>\n";
				echo '<input type="checkbox" checked="checked" id="' . $itemid . '">' . "\n";
				echo '<label for="' . $itemid . '">';
				if ( strlen($node_cb) > 0 ) {
					echo '<a href="javascript:' . $node_cb . "('$path$k');" . '">' . $k . '</a>';
				} else {
					echo $k;
				}
 				echo "</label>\n";
				// Recursively build the tree below the node
				php_array_to_tree($v, $node_cb, $leaf_cb, $path.$k.'/', $itemid."-0");
				echo "</li>\n";
			}
		}
		echo "</ul>\n";
	}

	function safe_put_file($path, $content) {
		//FIXME : if exists, then mktemp, put in it then rm and mv. Right preservation problems ?
		$res=FALSE;
		if ($handle = fopen($path, 'w')) {
			$res = fwrite($handle, $content);
			fclose($handle);
		}
		return $res;
	}

	function _write_ini_file_r(&$content, $assoc_arr, $has_sections)
	{
		foreach ($assoc_arr as $key => $val) {
			if (is_array($val)) {
				if($has_sections) {
					$content .= "[$key]\n";
					_write_ini_file_r($content, $val, false);
				} else {
					foreach($val as $iKey => $iVal) {
						if (is_int($iKey))
							$content .= $key ."[] = $iVal\n";
						else
							$content .= $key ."[$iKey] = $iVal\n";
					}
				}
			} else {
				if ( preg_match(RE_IDENTIFIER_CLEANER,$val)===1 ) {
					// Need to quote the value
					$content .= "$key = \"" . str_replace('"', '', $val) . "\"\n";
				} else {
					// No need to quote
					$content .= "$key = $val\n";
				}
			}
		}
	}

	function write_ini_file($assoc_arr, $path, $has_sections) {
		$res=FALSE;
		$content = '';
		_write_ini_file_r($content, $assoc_arr, $has_sections);

		if (is_string($content) && strlen($content) > 0) {
			//TODO : check if produced ini is readable again !
			$res = safe_put_file($path, $content);
		}

		return $res;
	}