1<?php
2/**
3 * AJAX call handler for tagindex admin plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Gina Häußge, Michael Klier <dokuwiki@chimeric.de>
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 * @author     Arthur Lobert <arthur.lobert@gmail.com>
9 */
10
11if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../../../') . '/');
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13if (!defined('DOKU_REG')) define ('DOKU_REG', DOKU_PLUGIN.'autolink3/register/');
14if (!defined('DOKU_PAGE')) define ('DOKU_PAGE', DOKU_INC.'data\pages');
15if (!defined('DOKU_PAGES')) define ('DOKU_PAGES', realpath(DOKU_PAGE));
16if (!defined('NL')) define('NL', "\n");
17
18//fix for Opera XMLHttpRequests
19if(!count($_POST) && $HTTP_RAW_POST_DATA)
20{
21	parse_str($HTTP_RAW_POST_DATA, $_POST);
22}
23
24require_once(DOKU_INC.'inc/init.php');
25require_once(DOKU_INC.'inc/common.php');
26require_once(DOKU_INC.'inc/pageutils.php');
27require_once(DOKU_INC.'inc/auth.php');
28require_once(DOKU_INC.'inc/search.php');
29require_once(DOKU_INC.'inc/indexer.php');
30
31//close session
32session_write_close();
33header('Content-Type: text/plain; charset=utf-8');
34if (!auth_isadmin())
35{
36	die('for admins only');
37}
38
39//clear all index files
40if (@file_exists($conf['indexdir'].'/page.idx'))// new word length based index
41{
42	$tag_idx = $conf['indexdir'].'/topic.idx';
43}
44else
45{                                          // old index
46$tag_idx = $conf['cachedir'].'/topic.idx';
47}
48$tag_helper =& plugin_load('helper', 'tag');
49
50//call the requested function
51$call = 'ajax_'.$_POST['call'];
52if(function_exists($call))
53{
54	$call();
55}
56else
57{
58	print "The called function '".htmlspecialchars($call)."' does not exist!";
59}
60
61/**
62 * Searches for pages
63 *
64 * @author Andreas Gohr <andi@splitbrain.org>
65 */
66function ajax_pagelist()
67{
68	global $conf;
69
70	$pages = array();
71	search($pages, $conf['datadir'], 'search_allpages', array());
72	foreach($pages as $page)
73	{
74		print $page['id']."\n";
75	}
76}
77
78/**
79 * Clear all index files
80 */
81function ajax_clearindex() {
82	global $conf;
83	global $tag_idx;
84
85
86	// keep running
87	@ignore_user_abort(true);
88
89	// try to aquire a lock
90	$lock = $conf['lockdir'].'/_tagindexer.lock';
91	while(!@mkdir($lock))
92	{
93		if(time()-@filemtime($lock) > 60*5)
94		{
95			// looks like a stale lock - remove it
96			@rmdir($lock);
97		}
98		else
99		{
100			print 'tagindexer is locked.';
101			exit;
102		}
103	}
104	io_saveFile($tag_idx,'');
105	// we're finished
106	print 1;
107}
108
109/**
110 * check if the current page is concerned by the link processing (Ajax).
111 * @param $page : name of the current page
112 * @param $text : content of $page
113 * @return $text
114 */
115
116function is_link_application($page, $text)
117{
118
119	$link = sort_tab(read_file(), compare_len);
120	$link = sort_tab_space($link);
121	foreach ($link as $elem):{
122		$locate = trim($elem[2]);
123		$page = realpath($page);
124		$locate = realpath($locate);
125		if (!strncmp($page, $locate, strlen($locate)))
126		{
127			$text = link_replace($text, $elem[0], $elem[1], DOKU_PAGE.str_replace(':', '/', $page));
128		}
129	}
130	endforeach;
131	return ($text);
132}
133
134function ajax_indexpage($page = NULL, $text = NULL) {
135	//check end
136		global $conf;
137
138	if(!$_POST['page'] && $page == NULL)
139	{
140		print 1;
141		exit;
142	}
143	// keep running
144	@ignore_user_abort(true);
145
146	$flag = 0;
147	// choose $page source
148	!$page ? $page = $_REQUEST['page'] : $flag = 1;
149
150	//$page format
151	$page = ':'.trim($page).'.txt';
152	$text = is_link_application($page, $text);
153	if ($text != '')
154	{
155		$rd_page = fopen(DOKU_PAGE.str_replace(':', '/', $page), 'w+');
156		fwrite($rd_page, $text);
157	}
158
159	$lock = $conf['lockdir'].'/_tagindexer.lock';
160	// we're finished
161
162	@rmdir($lock);
163	print 1;
164}
165?>