1<?php
2/**
3 * DokuWiki Plugin dokutranslate (Misc functions)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Martin Doucha <next_ghost@quick.cz>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16define('DOKUTRANSLATE_MODLIST', DOKU_INC . 'conf/dokutranslate.modlist.conf');
17
18$DOKUTRANSLATE_EDITFORM = '';
19
20# Read cleaned instructions for file and group them by paragraphs
21function getCleanInstructions($file) {
22	$instructions = p_cached_instructions($file);
23	$ret = array();
24	$i = 0;
25
26	foreach ($instructions as $ins) {
27		switch ($ins[0]) {
28		# Filter out sections and document start/end instructions
29		case 'document_start':
30		case 'document_end':
31		case 'section_open':
32		case 'section_close':
33			break;
34
35		# Start new block of instructions on paragraph end
36		case 'p_close':
37			$ret[$i++][] = $ins;
38			break;
39
40		# Add the instruction to current block
41		default:
42			$ret[$i][] = $ins;
43			break;
44		}
45	}
46
47	return $ret;
48}
49
50function dataPath($id) {
51	return dirname(wikiFN($id)) . '/_' . noNS($id);
52}
53
54function getParID() {
55	return isset($_REQUEST['parid']) ? intval($_REQUEST['parid']) : 0;
56}
57
58# Read the modlist file and return array of lines
59function loadModlist() {
60	$ret = @file(DOKUTRANSLATE_MODLIST);
61
62	return $ret === false ? array() : $ret;
63}
64
65# Parse array of modlist lines and return array(ns => modgroup)
66function parseModlist($lines) {
67	$ret = array();
68
69	foreach ($lines as $line) {
70		$line = trim(preg_replace('/#.*$/', '', $line)); //ignore comments
71		if (!$line) {
72			continue;
73		}
74
75		$entry = preg_split('/\s+/', $line);
76		$entry[1] = rawurldecode($entry[1]);
77		$ret[$entry[0]] = $entry[1];
78	}
79
80	return $ret;
81}
82
83# Check if current user has moderator privileges for given page ID
84function isModerator($id) {
85	global $USERINFO;
86	static $modlist = NULL;
87
88	# Not logged in
89	if (empty($_SERVER['REMOTE_USER'])) {
90		return false;
91	}
92
93	if (is_null($modlist)) {
94		$modlist = parseModlist(loadModlist());
95	}
96
97	# Check nearest non-root parent namespace
98	for ($ns = getNS($id); $ns; $ns = getNS($ns)) {
99		$wildcard = $ns . ':*';
100
101		if (!empty($modlist[$wildcard])) {
102			return in_array($modlist[$wildcard], $USERINFO['grps']);
103		}
104	}
105
106	# Check root namespace
107	if (!empty($modlist['*'])) {
108		return in_array($modlist['*'], $USERINFO['grps']);
109	}
110
111	# No moderator group set for any of parent namespaces
112	return false;
113}
114
115function canReview($id, $meta, $parid) {
116	return isModerator($id) && $meta[$parid]['user'] != $_SERVER['REMOTE_USER'] && $meta[$parid]['ip'] != clientIP(true);
117}
118
119function needsReview($id, $meta, $parid) {
120	return canReview($id, $meta, $parid) && !isset($meta[$parid]['reviews'][$_SERVER['REMOTE_USER']]);
121}
122
123// vim:ts=4:sw=4:et:
124