forked from ggouv/elgg-markdown_wiki
/
start.php
434 lines (381 loc) · 15.8 KB
/
start.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* Elgg-markdown_wiki plugin
* @package elgg-markdown_wiki
* @author Emmanuel Salomon @ManUtopiK
* @license GNU Affero General Public License, version 3 or late
* @link https://github.com/ManUtopiK/elgg-markdown_wiki
*
* Elgg-markdown_wiki start file
**/
elgg_register_event_handler('init', 'system', 'markdown_wiki_init');
/**
* Initialize elgg-markdown_wiki plugin.
*/
function markdown_wiki_init() {
// register libraries of helper functions
$root = dirname(__FILE__);
elgg_register_library('markdown_wiki:utilities', "$root/lib/utilities.php");
elgg_register_library('markdown_wiki:fineDiff', "$root/vendors/PHP-FineDiff/finediff.php");
// js and css
elgg_register_js('showdown', "/mod/elgg-markdown_wiki/vendors/showdown/compressed/showdown.js");
elgg_register_js('showdownggouv', "/mod/elgg-markdown_wiki/vendors/showdown/compressed/extensions/showdownggouv.js");
elgg_load_js('showdown');
elgg_load_js('showdownggouv');
elgg_register_js('highlight', "/mod/elgg-markdown_wiki/vendors/highlight/highlight.pack.js", 'footer', 100);
elgg_load_js('highlight');
elgg_extend_view('js/elgg', 'markdown_wiki/js');
elgg_extend_view('js/elgg', 'markdown_wiki/editor_js');
elgg_extend_view('css/elgg', 'markdown_wiki/css');
elgg_extend_view('css/elgg', 'markdown_wiki/markdown_css');
elgg_extend_view('css/elgg', 'markdown_wiki/highlight_css');
elgg_extend_view('page/elements/foot', 'markdown_wiki/syntax/language_selector');
// Add a menu item to the main site menu
$item = new ElggMenuItem('markdown_wiki_all', elgg_echo('markdown_wiki'), 'wiki/all');
elgg_register_menu_item('site', $item);
// entity menu
elgg_register_plugin_hook_handler('register', 'menu:entity', 'markdown_wiki_object_menu');
// write permission plugin hooks
elgg_register_plugin_hook_handler('permissions_check', 'object', 'markdown_wiki_write_permission_check');
// Register a page handler, so we can have nice URLs
elgg_register_page_handler('wiki', 'markdown_wiki_page_handler');
// Register entity type for search
elgg_register_entity_type('object', 'markdown_wiki');
// Register a url handler
elgg_register_entity_url_handler('object', 'markdown_wiki', 'markdown_wiki_url');
// Register a script to handle (usually) a POST request (an action)
$base_dir = "$root/actions/markdown_wiki";
elgg_register_action('markdown_wiki/edit', "$base_dir/edit.php");
elgg_register_action('markdown_wiki/compare', "$root/pages/markdown_wiki/compare.php");
elgg_register_action('markdown_wiki/settings', "$base_dir/settings.php");
// add to groups
add_group_tool_option('markdown_wiki', elgg_echo('groups:enable_markdown_wiki'), true);
elgg_extend_view('groups/tool_latest', 'markdown_wiki/group_module');
elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'markdown_wiki_owner_block_menu');
//add a widget
elgg_register_widget_type('markdown_wiki', elgg_echo('markdown_wiki'), elgg_echo('markdown_wiki:widget:description'));
// Language short codes must be of the form "markdown_wiki:key"
// where key is the array key below
elgg_set_config('markdown_wiki', array(
'description' => 'markdown',
'summary' => 'text',
'tags' => 'tags',
'write_access_id' => 'access',
'title' => 'hidden',
'guid' => 'hidden',
'container_guid' => 'hidden',
));
// Parse link
elgg_register_plugin_hook_handler('format_markdown', 'format', 'markdown_wiki_parse_link_plugin_hook', 600);
}
/**
* Dispatcher for elgg-markdown_wiki plugin.
* URLs take the form of :
* All: wiki/all or wiki/world
* User's: wiki/owner/<username>
* Friend's: wiki/friends/<username>
* All Group's pages: wiki/group/<guid>/all
* Group page: wiki/group/<guid>/page/<guid>/<title> (title is ignored)
* or: wiki/group/<guid>/page/<title> (title important !)
*
* Edit/New page: wiki/edit/<guid>/<title> (title is ignored)
* Discussion page: wiki/discussion/<guid>/<title> (title is ignored)
* History page: wiki/history/<guid>/<title>?granularity=<granularity> (granularity default = character) (title is ignored)
* Compare page: wiki/compare/<guid>/<title> (title is ignored)
* result: wiki/compare/<guid>/<title>?from=<annotation guid>&to=<annotation guid>&granularity=<granularity> (title is ignored)
*
* @param array $page
*/
function markdown_wiki_page_handler($page) {
elgg_load_library('markdown_wiki:utilities');
if (!isset($page[0])) {
$page[0] = 'all';
}
elgg_push_breadcrumb(elgg_echo('markdown_wiki'), 'wiki/all');
$base_dir = dirname(__FILE__) . '/pages/markdown_wiki';
switch ($page[0]) {
default:
case 'all':
case 'world':
include "$base_dir/world.php";
break;
case 'owner':
set_input('guid', $page[1]);
include "$base_dir/owner.php";
break;
case 'friends':
include "$base_dir/friends.php";
break;
case 'group':
if ($page[2] == 'all' ) {
include "$base_dir/owner.php";
} else if ($page[2] == 'settings' ) {
include "$base_dir/settings.php";
} else if ($page[2] == 'page' ) {
if (is_numeric($page[3])) {
set_input('guid', $page[3]);
} else {
$query = search_markdown_wiki_by_title($page[3], elgg_get_page_owner_guid()); // @todo security ?
if ($query) {
set_input('guid', $query);
} else {
forward("/wiki/search?container_guid=$page[1]&q=$page[3]");
}
}
include "$base_dir/view.php";
} else {
$group = get_entity((int)$page[1]);
if ($group->markdown_wiki_all_enabled == 'on') {
forward("/wiki/group/$page[1]/all");
} else {
forward('/wiki/group/' . $page[1] . '/page/' . elgg_echo('markdown_wiki:home')); // go to the wiki page home of the group
}
}
break;
case 'edit':
set_input('guid', $page[1]);
include "$base_dir/edit.php";
break;
case 'discussion':
set_input('guid', $page[1]);
include "$base_dir/discussion.php";
break;
case 'history':
set_input('guid', $page[1]);
include "$base_dir/history.php";
break;
case 'compare':
set_input('guid', $page[1]);
include "$base_dir/compare.php";
break;
case 'search':
include "$base_dir/search.php";
break;
}
elgg_pop_context();
return true;
}
/**
* Override the markdown_wiki url
*
* @param ElggObject $entity markdown_wiki object
* @return string
*/
function markdown_wiki_url($entity) {
$title = elgg_get_friendly_title($entity->title);
return "wiki/group/$entity->container_guid/page/$entity->guid/$title";
}
/**
* Add a menu item to the user ownerblock
*/
function markdown_wiki_owner_block_menu($hook, $type, $return, $params) {
if (elgg_instanceof($params['entity'], 'user')) {
$url = "wiki/owner/{$params['entity']->username}";
$item = new ElggMenuItem('markdown_wiki', elgg_echo('markdown_wiki'), $url);
$return[] = $item;
} else {
if ($params['entity']->markdown_wiki_enable != "no") {
if ($params['entity']->markdown_wiki_all_enabled == 'on') {
$url = "/wiki/group/{$params['entity']->guid}/all";
} else {
$url = "wiki/group/{$params['entity']->guid}/page/" . elgg_echo('markdown_wiki:home');
}
$item = new ElggMenuItem('markdown_wiki', elgg_echo('markdown_wiki:group'), $url);
if (elgg_in_context('wiki')) $item->setSelected();
$return[] = $item;
}
}
return $return;
}
/**
* Delete menu item 'delete' to the object menu, parse entity menu and add edit if user can + add history
*/
function markdown_wiki_object_menu($hook, $type, $return, $params) {
if (elgg_in_context('widgets')) {
return $return;
}
$entity = $params['entity'];
$handler = elgg_extract('handler', $params, false);
if ($handler != 'wiki') {
return $return;
}
// history link
$options = array(
'name' => 'history',
'text' => elgg_echo('markdown_wiki:page:history'),
'title' => elgg_echo('markdown_wiki:page:history'),
'class' => 'gwfb tooltip s t',
'href' => "wiki/history/{$params['entity']->guid}/{$params['entity']->title}",
'priority' => 110,
);
$return[] = ElggMenuItem::factory($options);
// discussion link
$options = array(
'name' => 'discussion',
'text' => elgg_echo('markdown_wiki:page:discussion'),
'title' => elgg_echo('markdown_wiki:page:discussion'),
'class' => 'gwfb tooltip s t',
'href' => "wiki/discussion/{$params['entity']->guid}/{$params['entity']->title}",
'priority' => 130,
);
$return[] = ElggMenuItem::factory($options);
foreach ($return as $index => $item) {
if ($item->getName() == 'delete') {
unset($return[$index]);
}
}
return $return;
}
/**
* Extend permissions checking to extend can-edit for write users.
*
* @param unknown_type $hook
* @param unknown_type $entity_type
* @param unknown_type $returnvalue
* @param unknown_type $params
*/
function markdown_wiki_write_permission_check($hook, $entity_type, $returnvalue, $params) {
if ($params['entity']->getSubtype() == 'markdown_wiki') {
$write_permission = $params['entity']->write_access_id;
$user = $params['user'];
if (($write_permission) && ($user)) {
$list_access = get_access_array($user->guid);
if ( can_write_to_container($user, $params['entity']->container_guid, 'object', 'markdown_wiki') || in_array($write_permission, $list_access)) {
return true;
}
}
}
return $returnvalue;
}
/**
* Plugin hook hander that parse for link and return intern link of non exist wiki page,
* exist page or external link
*
* @return string
*/
function markdown_wiki_parse_link_plugin_hook($hook, $entity_type, $returnvalue, $params) {
// escape all markdown link in code block
if (!function_exists('_escape_link_in_block')) {
function _escape_link_in_block($matches) {
if (substr($matches[0], -1) == '`') { // do this because 4 spaces regex eat last char, and it could be `
$last = true;
$matches[0] = substr_replace($matches[0], "", -1);
}
if ($matches[0][0] == '`') { // escape if first char is `
$first = true;
$matches[0] = substr($matches[0], 1);
}
$return = preg_replace('/`/', '„„', $matches[0]); // used with 4 spaces code block to escape ` inside block
if ($last) $return .= '`';
if ($first) $return = '`' . $return;
$return = preg_replace('/:\/\//U', '%:%', $return); // skip plain link
return preg_replace('/\[/U', '%%%', $return);
}
}
$returnvalue = preg_replace('{\r\n?}', "\n", $returnvalue); // # Standardize line endings: # DOS to Unix and Mac to Unix
$returnvalue = preg_replace_callback('/(?:^|\n)```.*\n```/Us', '_escape_link_in_block', $returnvalue); // github style code block (seems we don't need it because span code block do same thing
$returnvalue = preg_replace_callback('/\n\n(?:(?:[ ]{4}|\t).*\n+)+\n*[ ]{0,3}[^ \t\n]|(?=~0)/Um', '_escape_link_in_block', $returnvalue); // 4 spaces code block
$returnvalue = preg_replace_callback('/`+.*`/Um', '_escape_link_in_block', $returnvalue); // span style code block
$returnvalue = preg_replace('/\!\[(.*)\]\((.*)\)/U', '!%%%$1]($2)', $returnvalue); // escape image in link
$returnvalue = preg_replace('/„„/', '`', $returnvalue);
// search wiki page link
if (!function_exists('_parse_link_callback')) {
function _parse_link_callback($matches) {
// link
$array_match = explode('#', $matches[2]);
$link = rtrim($array_match[0], '/');
$html_link = urlencode($link);
$hash = $array_match[1] ? '\#' . $array_match[1] : ''; // check if link is like (apage#aparagraph)
// title
$word = strip_tags(rtrim($matches[1], '/'));
$html_word = strpos($word, '%%%') ? $word : urlencode($word); // check if title contain link (eg: wrap link on image)
$group = elgg_get_page_owner_guid();
//if ($group == 0) $group = elgg_get_logged_in_user_guid(); no wiki for user ?
$site_url = elgg_get_site_url();
$info = elgg_echo('markdown_wiki:create');
if (!$link) { // markdown syntax like [a link]() or [a link](#paragraph)
$link = rtrim($matches[1], '/');
}
if ( strpos($link, '://') !== false ){
if ( strpos($link, $site_url) === false ) { // external link
return "<a rel='nofollow' target='_blank' href='" . $link . "' class='external'>$matches[1]</a><span class='elgg-icon external'></span>";
} else { // internal link with http://
return '<a href="' . $link .'">' . $matches[1] . '</a>';
}
} else {
if (preg_match('/^groupe?\/(\\w+)\/page\/(.*)/', $link, $relative) || preg_match('/^(\\w+):(.*)/', $link, $relative)) {
if ( is_numeric($relative[1]) ) {
if ( is_numeric($relative[2]) ) {
$page = get_entity($relative[2]);
return "<a href='{$page->getUrl()}{$hash}'>{$matches[1]}</a>";
} elseif ( $page_guid = search_markdown_wiki_by_title($relative[2], $relative[1]) ) { // page exists
$page = get_entity($page_guid);
return "<a href='{$page->getUrl()}{$hash}'>{$matches[1]}</a>";
} else { // page doesn't exists
$relative[2] = urlencode($relative[2]);
return "<a href='{$site_url}wiki/search?container_guid={$relative[1]}&q={$relative[2]}' class='tooltip s new' title=\"{$info}\">{$matches[1]}</a>";
}
} else if ($gtitle = search_group_by_title($relative[1])) {
if ( $page_guid = search_markdown_wiki_by_title($relative[2], $gtitle) ) { // page exists
$page = get_entity($page_guid);
return "<a href='{$page->getUrl()}{$hash}'>{$matches[1]}</a>";
} else { // page doesn't exists
$relative[2] = urlencode($relative[2]);
return "<a href='{$site_url}wiki/search?container_guid={$gtitle}&q={$relative[2]}' class='tooltip s new' title=\"{$info}\">{$matches[1]}</a>";
}
} else {
return "<a href='{$site_url}wiki/search?container_guid={$group}&q={$relative[2]}' class='tooltip s new' title=\"{$info}\">{$matches[1]}</a>";
}
} elseif ( $page_guid = search_markdown_wiki_by_title($link, $group) ) { // page exists
$page = get_entity($page_guid);
return "<a href='{$page->getUrl()}{$hash}'>{$matches[1]}</a>";
} else { // page doesn't exists
return "<a href='{$site_url}wiki/search?container_guid={$group}&q={$html_link}' class='tooltip s new' title=\"{$info}\">{$matches[1]}</a>";
}
}
}
}
$return = preg_replace_callback("/(?<!\!)\[(.*)\]\((.*)\)/U", '_parse_link_callback', $returnvalue);
// parse link with reference like [a link][1]
$return = preg_replace_callback("/\[(.*)\]\[(.*)\]/U", function (array $matchesref) use ($returnvalue) {
if ($matchesref[2] == '') $matchesref[2] = $matchesref[1];
preg_match('/\['.$matchesref[2] . '\]:(.*)"?\n/Um', $returnvalue, $ref);
$matchesref[2] = trim($ref[1]);
return _parse_link_callback($matchesref);
}, $return);
if (!function_exists('_parse_plainlink_callback')) {
function _parse_plainlink_callback($matches) {
if ( strpos($matches[2], elgg_get_site_url()) === false ) { // external link
return "$matches[1]<a rel='nofollow' target='_blank' href='" . $matches[2] . "' class='external'>$matches[2]</a><span class='elgg-icon external'></span>$matches[3]";
} else { // internal link.
return $matches[1] . '<a href="' . $matches[2] .'">' . $matches[2] . '</a>' . $matches[3];
}
}
}
$return = preg_replace_callback('/(?<!\]:)(^| +|\n)((?:ht|f)tps?:\/\/[^\s\r\n\t<>"\'\(\)]+)($| |\n)/U', '_parse_plainlink_callback', $return);
$return = preg_replace('/%:%/U', '://', $return);
// unescape link %%%
return preg_replace('/%%%/U', '[', $return);
}
/**
* Wrapper for markdown_wiki_parse_link_plugin_hook
* @param [type] $text The markdown text to parse
* @return text Return markdown text with link formated for wiki (ie: red link = page doesn't exist, blue link = page exist)
*/
function markdown_wiki_parse_link($text) {
elgg_load_library('markdown_wiki:utilities');
return markdown_wiki_parse_link_plugin_hook('', '', $text, '');
}
/**
* Function to replace get_input. Skip htmlawed, so we use strips_tags.
* Markdown use link like <http://example.com>
*
* @return string
*/
function get_markdown_input($text) {
$text = preg_replace('/<(https?|ftp|dict):/U', '%%%$1:', $text);
$text = strip_tags($text, '<iframe><img><div><span><br><table><th><tr><td>');
$text = str_replace(array('\r\n', '\n'), chr(13), $text);
$text = preg_replace('/%%%/U', '<', $text);
return $text;
}