1<?php
2/**
3 * DokuWiki Plugin ajaxedit (Helper Component)
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author lisps
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
16require_once(DOKU_INC.'inc/auth.php');
17
18class helper_plugin_ajaxedit extends DokuWiki_Plugin {
19	const ERROR_LOCK	=  1;
20	const ERROR_SECTOC  =  2;
21	const ERROR_MODIFIED=  4;
22	const ERROR_ACL  	=  3;
23	const ERROR_READ	=  5;
24
25
26	const ERROR_OTHER	= 99;
27
28	public $ID;
29	public $index;
30
31	function __construct(){
32	    global $ID;
33	    $ID=isset($_POST["pageid"])?cleanID($_POST["pageid"]):$ID;
34	}
35
36	/**
37	 * error throw an error and leave
38	 * @param string $msg
39	 * @param integer $type error type
40	 * @param boolean $exit leave or not
41	 **/
42	function error($msg,$type=self::ERROR_OTHER,$exit = true){
43		$ret = array(
44			'error' 	=> $type,
45			'msg'		=> $msg,
46			'lastmod'	=> intval($_POST["lastmod"]),
47		);
48
49		echo json_encode($ret);
50		if($exit && !defined('DOKU_UNITTEST')) exit;
51	}
52
53	function _error($type,$exit = true){
54		$ret = array(
55			'error' 	=> $type,
56			'msg'		=> $this->_getErrorMsg($type),
57			'lastmod'	=> intval($_POST["lastmod"]),
58		);
59
60		echo json_encode($ret);
61
62		if($exit && !defined('DOKU_UNITTEST')) exit;
63	}
64
65	/**
66	 * getWikiPage returns the raw wiki data
67	 * @return string
68	 */
69	function getWikiPage($checkLastmod = true, $min_acl = AUTH_EDIT){
70		global $ID;
71		global $INFO;
72		$this->ID=cleanID(trim($_POST["pageid"]));
73
74		$ID = $this->ID;
75		$this->index = intval($_POST["index"]);
76
77		$oldrev = intval($_POST["lastmod"]);
78
79		if(!checkSecurityToken()) $this->_error(self::ERROR_SECTOC);
80
81		if (auth_quickaclcheck($ID) < $min_acl) {
82			$this->_error(self::ERROR_ACL);
83		}
84
85		$INFO = pageinfo();
86		if($checkLastmod && $INFO['lastmod']!=$oldrev ) {
87			$this->_error(self::ERROR_MODIFIED);
88		}
89
90		if(checklock($ID)){
91			$this->_error(self::ERROR_LOCK);
92		}
93
94		if (!($data=rawWiki($ID))){
95			$this->_error(self::ERROR_READ);
96		}
97		return $data;
98
99	}
100	/**
101	 * success sends the success message
102	 * automatically sends error,msg,lastmod,index(id counter)
103	 *
104	 * @param array $data additional data
105	 */
106	function success($data=array()){
107		global $ID;
108		$info = pageinfo();
109
110		$ret = array(
111			'error'  => 0,
112			'msg'    => '',
113			'lastmod'=> $info['lastmod'],
114			'index'  => $this->index,
115			'pageid' => $ID,
116		);
117		$ret = array_merge($ret,$data);
118		echo json_encode($ret);
119		exit;
120	}
121
122	/**
123	 * saveWikiPage saves the wiki page
124	 *
125	 * @param string $data wiki page
126	 * @param string $summary
127	 * @param boolean $minor
128	 * @param array $param will go to @see success
129	 * @param boolean $autosubmit if set will call success
130	 */
131	function saveWikiPage($data,$summary,$minor = false,$param=array(),$autosubmit=true){
132		saveWikiText($this->ID,$data,$summary,$minor);
133
134		if($autosubmit){
135			$this->success($param);
136		}
137		global $INFO;
138		$INFO = pageinfo();
139	}
140
141	function _getErrorMsg($error){
142		global $INFO;
143		$INFO = pageinfo();
144		$msg = '';
145		switch($error){
146			case self::ERROR_LOCK:
147				$msg = 'ERROR_LOCK:tbd';
148				ob_start();
149			    html_locked();
150			    $msg = ob_get_clean();
151				break;
152			case self::ERROR_SECTOC :
153				$msg = 'ERROR_SECTOC:tbd';
154				break;
155			case self::ERROR_ACL:
156				$msg = p_locale_xhtml('denied');
157				break;
158
159			case self::ERROR_READ:
160				$msg = 'ERROR_READ:tbd';
161				break;
162
163			case self::ERROR_MODIFIED:
164				$msg = sprintf($this->getLang('e_modified'),hsc(editorinfo($INFO['user'])));
165				break;
166
167			case self::ERROR_OTHER:
168				$msg = 'tbd';
169				break;
170			default:
171				$msg = 'Undefined Failure';
172				break;
173		}
174
175		return $msg;
176	}
177}