1<?php
2/**
3 * DokuWiki Plugin ajaxedit (Action Component)
4 *
5 * adds lastmod and sectok variable to JSINFO
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  lisps
9 */
10
11class action_plugin_ajaxedit extends DokuWiki_Action_Plugin {
12
13	/**
14	 * Register the eventhandlers
15	 */
16	function register(Doku_Event_Handler $controller) {
17		$controller->register_hook('DOKUWIKI_STARTED', 'AFTER',  $this, '_addlastmod');
18		$controller->register_hook('DOKUWIKI_STARTED', 'AFTER',  $this, 'fixsecedit');
19	}
20	function _addlastmod(&$event, $param) {
21		global $ID;
22		global $JSINFO;
23
24		$info = pageinfo();
25		$JSINFO['lastmod'] = $info["lastmod"];
26		$JSINFO['sectok'] = getSecurityToken();
27        $perm = auth_quickaclcheck($ID);
28		if ($perm > AUTH_READ)
29			$JSINFO['acl_write'] = '1';
30	}
31
32	/**
33	 * try to fix sectioninfo for section edit buttons.
34	 *
35	 * @param Doku_Event $event
36	 * @param unknown $param
37	 */
38	function fixsecedit(Doku_Event $event, $param) {
39		global $INPUT;
40		global $RANGE;
41		global $REV;
42		global $INFO;
43		global $ID;
44		global $ACT;
45
46		if($ACT !== 'edit') return;
47		if(!$this->getConf('fix_section_edit')) return;
48		if($INPUT->str('target') !== 'section') return;
49
50		$range = $INPUT->str('range');
51		$rev = $INPUT->str('rev');
52
53		if($rev && $range && (!$REV && !$RANGE)) { //$_POST has range and rev but pageinfo() cleared it -> action
54			list($r_start,$r_end) = explode('-',$range);
55			$instructions = p_cached_instructions($INFO['filepath']); //get instructions
56			$new_section_open = null;
57			$new_section_close = '';
58			$found = null;
59
60			foreach($instructions as $key => $instruction) {
61
62				if($new_section_open && $instruction[0] === 'section_close') { //moved section found, now find closing section
63					$new_section_close = $instruction[2];
64					break; //end
65				} elseif ($new_section_open) {
66					continue;
67				}
68
69				if($instruction[0] === 'section_open') {
70					if($r_start == $instruction[2]) {
71						$new_section_open = $instruction[2];
72						msg(sprintf($this->getLang('section_found'),wl($ID,array('do'=>'edit'))));
73					} else if( abs($r_start-$instruction[2]) < $this->getConf('section_edit_range') ) {
74						$new_section_open = $instruction[2];
75						msg(sprintf($this->getLang('moved_section_found'),wl($ID,array('do'=>'edit'))));
76					}
77
78				}
79			}
80
81			if($new_section_open !== null) {
82				$RANGE = implode('-', array($new_section_open,$new_section_close));
83			}
84		}
85
86
87
88
89	}
90}
91