-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
executable file
·306 lines (268 loc) · 6.09 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/*
* This plugin finds phpbb links to a thread or Topic from a speceficboard
* and if there is no title set it adds the thread title
*
* @author Dominik Eckelmann <[email protected]>
*/
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
if(!defined('DOKU_DATA')) define('DOKU_DATA',DOKU_INC.'data/');
require_once(DOKU_PLUGIN.'action.php');
class action_plugin_phpbblinks extends DokuWiki_Action_Plugin
{
var $con;
var $pre;
/**
* return some info
*/
function getInfo()
{
return confToHash(dirname(__FILE__).'/info.txt');
}
function register(&$controller)
{
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'replace', array());
}
function replace(&$event, $param)
{
// if the action is not save or preview -> we are not interested
if (!is_array($event->data)) return false;
if ( !isset($event->data['preview']) && !isset($event->data['save']))
{
return false;
}
// ignore admin interface
global $ACT;
if ($ACT == 'admin')
{
return false;
}
// if not enabled -> return
if (!$this->getConf('enable'))
{
return false;
}
// get the board url
$url = $this->_getUrl();
// no url given and the plugin has nothing to do
if (!$url)
{
$this->admMsg('No BoardURL is set',-1);
return;
}
// no db connection
if (!$this->_connect())
{
return false;
}
global $TEXT;
// we take the requested text and split it into pices
$text = $TEXT;
$textpice = preg_split('/[\s]/',$text);
// prepare some stuff
$urlc = strlen($url);
$urla = parse_url($url);
$search = array();
$replace = array();
// start to check every word
foreach ($textpice as $word)
{
// ignore empty lines
if (empty($word))
{
continue;
}
// prepare some more stuff
$args = array();
$enclose = false;
$org = $word;
// if the word is a link
if (substr($word,0,2) == '[[' && substr($word,-2) == ']]' )
{
$enclose = true;
if (strpos($word,'|'))
{
// link has already a title - ignore
continue;
}
$word = substr($word,2,-2);
if (strlen($word) <= $urlc)
{
// word is to short to be a real interesting url
continue;
}
if (substr($word,0,$urlc) != $url)
{
// not the right url
continue;
}
// get url args
$pices = parse_url($word);
if (!$pices['query'])
{
continue; // no args are given
}
// get the args
parse_str($pices['query'],$args);
} else {
$wu = parse_url($word);
// compare links
$same = true;
foreach (array('scheme','host','port','user','pass','path') as $k)
{
if (!isset($urla[$k]))
{
if (isset($wu[$k]))
{
$same = false;
break;
}
continue;
}
if ($wu[$k] != $urla[$k])
{
// links are not the same
$same = false;
break;
}
}
if (!$same)
{
continue;
}
// get the url args
parse_str($wu['query'],$args);
}
// args make smbting
if (isset($args['p']))
{
// get post title
$p = $args['p'];
$txt = $this->_getTopicByPostId($p);
if (!$txt) continue;
$txt = $this->getConf('linktxt') . $txt;
$search[] = '/(\s)?'.str_replace('/','\\/',preg_quote($org)).'(\s)?/';
$replace[] = "\\1[[$url?p=$p#$p|$txt]]\\2";
}
elseif (isset($args['t']))
{
// get thread title
$t = $args['t'];
$txt = $this->_getTopicByTopicId($t);
if (!$txt) continue;
$txt = $this->getConf('linktxt') . $txt;
$search[] = '/(\s)?'.str_replace('/','\\/',preg_quote($org)).'(\s)?/';
$replace[] = "\\1[[$url?t=$t|$txt]]\\2";
}
else continue;
}
ksort($search);
ksort($replace);
$TEXT = preg_replace($search,$replace,$TEXT);
$this->_disconnect();
}
function _getUrl() {
$url = $this->getConf('boardurl');
if (empty($url)) return false;
// add a final /
if (substr($url,-1) != '/') {
$url .= '/';
}
// add the viewtopic file
$url .= 'viewtopic.php';
return $url;
}
function _getTopicByTopicId($id) {
if (!$this->con) return '';
$q = sprintf(
'SELECT topic_title FROM %1$stopics WHERE topic_id=%2$d
',$this->pre,intval($id));
$r = $this->_query($q);
if (!$r) return false;
$r = $r['topic_title'];
return $r;
}
function _getTopicByPostId($id) {
if (!$this->con) return '';
$q = sprintf('
SELECT
%1$stopics.topic_title
FROM
%1$sposts , %1$stopics
WHERE
%1$sposts.post_id=%2$d AND
%1$sposts .topic_id = %1$stopics.topic_id
',$this->pre,intval($id));
$r = $this->_query($q);
if (!$r) return false;
$r = $r['topic_title'];
return $r;
}
function _connect() {
$host = '';
$user = '';
$pass = '';
$dbu = '';
$pre = '';
if ($this->getConf('integration'))
{
global $conf;
global $table_prefix;
$host = $conf['auth']['mysql']['server'];
$user = $conf['auth']['mysql']['user'];
$pass = $conf['auth']['mysql']['password'];
$dbu = $conf['auth']['mysql']['database'];
$pre = $table_prefix;
}
else
{
$host = $this->getConf('host');
$user = $this->getConf('user');
$pass = $this->getConf('pass');
$dbu = $this->getConf('db');
$pre = $this->getConf('pre');
}
$this->pre = $pre;
$con = @mysql_connect( $host , $user , $pass) or $err = @mysql_error();
if (!$con)
{
$this->admMsg('Database connection failed '.$err,-1);
return false;
}
$this->con = $con;
$db = @mysql_select_db($dbu,$this->con);
if ($db)
{
return true;
}
$this->admMsg('Database selection failed '.@mysql_error($this->con),-1);
return false;
}
function _disconnect() {
mysql_close($this->con);
}
function _query($q) {
$r = @mysql_query($q,$this->con);
if (!$r)
{
$this->admMsg('Database Error '.@mysql_error($this->con),-1);
return array();
}
$r = mysql_fetch_assoc($r);
return $r;
}
function _esc($s) {
return mysql_real_escape_string($s,$this->con);
}
function admMsg($msg,$type=0)
{
global $INFO;
if (!$INFO['isadmin'] && !$INFO['ismanager'])
{
return;
}
msg("PHPBBLinks: $msg",$type);
}
}
?>