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 Gerry Weissbach <gweissbach@inetsoftware.de>
6*/
7
8if(!defined('DOKU_INC')) die();
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10require_once(DOKU_PLUGIN.'action.php');
11
12class action_plugin_daftdrafts extends DokuWiki_Action_Plugin {
13
14	/**
15	*  Returns information about the plugin.
16	*/
17	function getInfo(){
18		return array (
19            'author' => 'Jon Magne B�e',
20            'date' => '2011-11-06',
21            'name' => 'DaftDrafts plugin',
22            'desc' => 'Marks pages as drafts, which hides them from unregistered users.',
23            'url' => 'http://www.dokuwiki.org/plugin:daftdrafts',
24        );
25	}
26
27	/**
28	* Register event handlers
29	*
30	* @author Gerry Weissbach <gweissbach@inetsoftware.de>
31	* @author Jon Magne B�e <jonmagneboe@hotmail.com>
32	*/
33	function register(&$controller) {
34		$controller->register_hook('PARSER_METADATA_RENDER','AFTER',$this,'_daftdrafts');
35		$controller->register_hook('IO_WIKIPAGE_WRITE','AFTER',$this,'_daftdrafts_write');
36		$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
37		$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'show_banner', array());
38	}
39
40	/**
41	* When  WRITE is triggered, and the content is empty
42	*
43	* @author Gerry Weissbach <gweissbach@inetsoftware.de>
44	* @author Jon Magne B�e <jonmagneboe@hotmail.com>
45	*/
46	function _daftdrafts_write(&$event, $param) {
47		global $INFO;
48
49		if ( empty($event->data[0][1]) ) {
50			$id = resolve_id($event->data[1], $event->data[2]);
51			$this->_daftdrafts_del_acl($id);
52			$INFO['perm'] = $this->_auth_quickaclcheck($id);
53		}
54	}
55
56	/**
57	* In case of read or write of a page, this function is triggered.
58	*
59	* @author  Gerry Weissbach <gweissbach@inetsoftware.de>
60	* @author Jon Magne B�e <jonmagneboe@hotmail.com>
61	*/
62	function _daftdrafts(&$event, $param) {
63		global $INFO, $auth, $ID;
64
65		$id = cleanID( empty($event->data['page']) ? $ID : $event->data['page'] );
66		$isDraft = false;
67		$value = $event->data['current']['type'];
68		$isDraft = !empty($value) && $value == 'daftdrafts'; //triggered when the text contains this plugin's syntax.
69
70		if ( $isDraft ) {
71			$this->_daftdrafts_add_acl($id, $event->data['current']['last_change']['user']);
72		} else {
73			$this->_daftdrafts_del_acl($id);
74		}
75
76		$INFO['perm'] = $this->_auth_quickaclcheck($id);
77	}
78
79	/**
80	*  Add ACL to @ALL and current User
81	*
82	* @author Gerry Weissbach <gweissbach@inetsoftware.de>
83	* @author Jon Magne B�e <jonmagneboe@hotmail.com>
84	*/
85	function _daftdrafts_add_acl($id, $user) {
86		global $auth;
87		if ( !($daftdrafts =& plugin_load('helper', 'daftdrafts')) ) { return; }
88		$daftdrafts->acl_add($id, '@ALL', AUTH_NONE);
89		$daftdrafts->acl_add($id, '@user', AUTH_EDIT);
90	}
91
92	/**
93	*  Remove ACL to @ALL and current User
94	*
95	* @author Gerry Weissbach <gweissbach@inetsoftware.de
96	* @author Jon Magne B�e <jonmagneboe@hotmail.com>
97	*/
98	function _daftdrafts_del_acl($id) {
99
100		if ( !($daftdrafts =& plugin_load('helper', 'daftdrafts')) ) { return; }
101		$daftdrafts->acl_del($id);
102	}
103
104	/**
105	*  Show a banner when viewing pages that are not published.
106	*
107	*  @author Jon Magne B�e <jonmagneboe@hotmail.com>
108	*/
109	function show_banner(& $event, $param) {
110		global $AUTH_ACL, $ID, $INFO;
111		$autorizationList = $AUTH_ACL;
112		$pageIdentification = $ID;
113		$allUsers = auth_nameencode('@ALL', true); //all users
114		$additional='DAFTDRAFTS';
115		$autorizationList = $AUTH_ACL;
116		if ( empty($authorizationList) ) $authorizationList = file(DOKU_CONF.'acl.auth.php');
117		$daftAuthorizationList = file(dirname(__FILE__).'/daft.auth.php');
118		$additional = '\t#' . $additional;
119		$acl_pattern_nocomment = '^'.preg_quote($pageIdentification,'/').'\s+.*\s+[0-8].*$'; //pattern for searching acl.auth.php
120		$authorizationHits = preg_grep("/$acl_pattern_nocomment/", $authorizationList);
121		$numberOfAuthorizations = count($authorizationHits);
122		$daftHits = preg_grep("/$acl_pattern_nocomment/", $daftAuthorizationList);
123		$numberOfDaftauthorizations = count($daftHits);
124		//If both acl.auth.php and daft.auth.php contains restrictions of authorization, it's safe to assume it's not published.
125		if ($numberOfAuthorizations != 0 && $numberOfDaftauthorizations != 0) {
126			$published = false;
127		} else { //Ideally the acl.auth.php and the daft.auth.php-files should be in sync, meaning we don't need to check anything else.
128			$published = true;
129		}
130		$htmlcode = array();
131		//This code runs if the page is unpublished, i.e. tagged as a draft and hidden from unregistered users.
132		if (!$published && isset($INFO['userinfo'])) {
133			$htmlcode[] = '<div class="unpublished">';
134			$htmlcode[] = '<span class="draft">';
135			$htmlcode[] = $this->getLang('unpublished');
136			$htmlcode[] = '<br>';
137			$htmlcode[] = $this->getLang('howtopublish'). ' ~~' .$this->getLang('code'). '~~';
138			$htmlcode[] = '</span>';
139			$htmlcode[] = '</div>';
140			ptln(implode($htmlcode));
141			return true;
142		} else {
143			return;
144		}
145	}
146
147	/**
148	*  Adds a button to the toolbar for easy adding of the draft-code
149	*
150	* @author  Jon Magne B�e <jonmagneboe@hotmail.com>
151	*/
152	function insert_button(& $event, $param) {
153		$event->data[] = array (
154			'type' => 'format',
155			'title' => $this->getLang('nowiki'), //Doesn't exist, done on purpose to avoid entering a sample (which is automatically set to title if blank)
156			'icon' => '../../plugins/daftdrafts/images/daftdrafts.gif',
157			'open' => '~~' .$this->getLang('code'). '~~',
158			'close' => '',
159		);
160	}
161
162	/**
163	* Quick acl-check
164	*/
165	function _auth_quickaclcheck($id) {
166		if ( function_exists('auth_quickaclcheck') ) {
167			return auth_quickaclcheck($id);
168		}
169		return 0;
170	}
171}