1<?php
2/**
3 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author Jon Magne B�e <jonmagneboe@hotmail.com>
5 * @author i-net software <tools@inetsoftware.de>
6 * @author Gerry Weissbach <gweissbach@inetsoftware.de>
7 */
8
9if (!defined('DOKU_INC')) die();
10if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
11
12class helper_plugin_daftdrafts extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin
13
14	var $functions = null;
15
16	/**
17	* Returns information about this plugin.
18	*/
19	function getInfo(){
20		return array (
21            'author' => 'Jon Magne B�e',
22            'date' => '2011-11-06',
23            'name' => 'DaftDrafts (Helper Component)',
24            'desc' => 'Marks pages as drafts, which hides them from unregistered users.',
25            'url' => 'http://www.dokuwiki.org/plugin:daftdrafts',
26        );
27	}
28
29	function getMethods(){
30		$result = array();
31		$result[] = array(
32		  'name'   => 'acl_add',
33		  'desc'   => 'Add an ACL Line into the acl.auth.php',
34		  'params' => array('acl_scope' => 'string', 'acl_user' => 'string', 'acl_level' => 'string', 'additional' => 'string'),
35		  'return' => array('success' => 'boolean'),
36		);
37		$result[] = array(
38		  'name'   => 'acl_del',
39		  'desc'   => 'Remove an ACL Line into the acl.auth.php',
40		  'params' => array('acl_scope' => 'string', 'acl_user' => 'string', 'additional' => 'string'),
41		  'return' => array('success' => 'boolean'),
42		);
43		return $result;
44	}
45
46	/**
47	 * Adds a new acl-entry to conf/acl.auth.php, as well as lib/plugins/daftdrafts/daft.auth.php.
48	 *
49	 * @author Jon Magne B�e <jonmagneboe@hotmail.com>
50	 * @author Frank Schubert <frank@schokilade.de>
51	 * @author Gerry Weissbach <gweissbach@inetsoftware.de>
52	 */
53	function acl_add($acl_scope, $acl_user, $acl_level, $additional='DAFTDRAFTS'){
54		global $AUTH_ACL;
55		$acl_config = $AUTH_ACL;
56		if ( empty($acl_config) ) $acl_config = file(DOKU_CONF.'acl.auth.php');
57		$daftAcl = file(dirname(__FILE__).'/daft.auth.php');
58		$acl_user = auth_nameencode($acl_user,true);
59		if ( empty($acl_user) ) { return false; }
60		if ( !empty($additional) ) {
61			$additionalCheck = '\t#' . $additional;
62			$additional = "\t#" . $additional;
63		}
64		//Checks that the acl_level is not higher than the permitted maximum for pages:
65		if(strpos($acl_scope,'*') === false) {
66			if($acl_level > AUTH_EDIT) $acl_level = AUTH_EDIT;
67		}
68		$existInDaft = false;
69		$existInAcl = false;
70		$acl_pattern = '^'.preg_quote($acl_scope,'/').'\s+'.$acl_user.'\s+[0-8].*' . $additionalCheck . '$';
71		$acl_pattern_nocomment = '^'.preg_quote($acl_scope,'/').'\s+'.$acl_user.'\s+[0-8].*$';
72		//Checks if this exists in daftAcl:
73		if (preg_grep("/$acl_pattern/", $daftAcl)) {
74			if (preg_grep("/$acl_pattern_nocomment/", $acl_config)) {
75				return true;
76			} else {
77				$existInDaft = true;
78			}
79		} elseif (preg_grep("/$acl_pattern_nocomment/", $acl_config)) {
80			$existInAcl = true;
81		}
82		if (!$existInDaft) {
83			$daftAcl[] = "$acl_scope\t$acl_user\t$acl_level$additional\n"; //Adds acl-info to daftAcl
84			io_saveFile(dirname(__FILE__).'/daft.auth.php', join('', $daftAcl));
85		}
86		if (!$existInAcl) {
87			$acl_config[] = "$acl_scope\t$acl_user\t$acl_level$additional\n"; //Adds acl-info to acl_config
88			$AUTH_ACL = $acl_config;
89			io_saveFile(DOKU_CONF.'acl.auth.php', join('', $acl_config));
90		}
91		return true; //The function does not check if the files were actually saved.
92	}
93
94	/**
95	 * Removes an acl-entry from conf/acl.auth.php
96	 *
97	 * @author Jon Magne B�e <jonmagneboe@hotmail.com>
98	 * @author Frank Schubert <frank@schokilade.de>
99	 * @author Gerry Weissbach <gweissbach@inetsoftware.de>
100	 */
101	function acl_del($acl_scope, $additional='DAFTDRAFTS'){
102		global $AUTH_ACL;
103		$acl_config = $AUTH_ACL;
104		if ( empty($acl_config) ) $acl_config = file(DOKU_CONF.'acl.auth.php');
105		$daftAcl = file(dirname(__FILE__).'/daft.auth.php');
106		$additional = '\t#' . $additional;
107		$acl_pattern = '^'.preg_quote($acl_scope,'/').'\s+.*\s+[0-8].*' . $additional . '$';
108		$acl_pattern_nocomment = '^'.preg_quote($acl_scope,'/').'\s+.*\s+[0-8].*$';
109		$new_config = preg_grep("/$acl_pattern_nocomment/", $acl_config, PREG_GREP_INVERT);
110		$newDaftAcl = preg_grep("/$acl_pattern/", $daftAcl, PREG_GREP_INVERT);
111		$AUTH_ACL = $new_config;
112		if ($new_config != $acl_config) {
113			io_saveFile(DOKU_CONF.'acl.auth.php', join('',$new_config));
114		}
115		if ($newDaftAcl != $daftAcl) {
116			io_saveFile(dirname(__FILE__).'/daft.auth.php', join('',$newDaftAcl));
117		}
118		return;
119	}
120}