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