/
edit.php
480 lines (429 loc) · 17.7 KB
/
edit.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/**
* Copyright( c ) 2004 bitweaver.org
* Copyright( c ) 2003 tikwiki.org
* Copyright( c ) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
* All Rights Reserved. See below for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
*
* @package wiki
* @subpackage functions
*/
/**
* required setup
*/
require_once( '../kernel/includes/setup_inc.php' );
include_once( WIKI_PKG_CLASS_PATH.'BitBook.php' );
$gBitSystem->verifyPackage( 'wiki' );
// bypass lookup_content_inc.php as we can't prevent it parsing faulty pages
unset($_REQUEST['content_id']);
// Disable parsing data if not asking to preview page
$_REQUEST["parse"] = false;
include( WIKI_PKG_INCLUDE_PATH.'lookup_page_inc.php' );
if( $gContent->isValid() ) {
$gContent->verifyUpdatePermission();
} else {
$gContent->verifyCreatePermission();
}
//make comment count for this page available for templates
if( $gBitSystem->isFeatureActive( 'wiki_comments' ) && !empty( $_REQUEST['page_id'] ) ) {
require_once( LIBERTY_PKG_CLASS_PATH.'LibertyComment.php' );
$gComment = new LibertyComment();
$numComments = $gComment->getNumComments($gContent->mContentId);
$gBitSmarty->assign('comments_count', $numComments);
}
#edit preview needs this
if( !isset( $_REQUEST['title'] ) && isset( $gContent->mInfo['title'] ) ) {
$_REQUEST['title'] = $gContent->mInfo['title'];
}
if( $gContent->isLocked() ) {
$gBitSystem->fatalError( 'Cannot edit page because it is locked' );
}
$gContent->invokeServices( 'content_edit_function' );
if( !empty( $gContent->mInfo ) ) {
$formInfo = $gContent->mInfo;
$data_to_edit = !empty( $gContent->mInfo['data'] ) ? $gContent->mInfo['data'] : '';
if (!empty($_REQUEST['section'])) {
$section = $_REQUEST['section'];
$data_to_edit = extract_section($data_to_edit,$section);
$formInfo['data'] = $data_to_edit;
$formInfo['edit_section'] = 1;
$formInfo['section'] = $_REQUEST['section'];
}
$formInfo['edit'] = $data_to_edit;
$formInfo['edit_comment'] = '';
}
$gBitSmarty->assign( 'footnote', '' );
$gBitSmarty->assign( 'has_footnote', 'n' );
if( $gBitSystem->isFeatureActive( 'wiki_footnotes' ) ) {
if( $gBitUser->mUserId ) {
$footnote = $gContent->getFootnote( $gBitUser->mUserId );
$gBitSmarty->assign( 'footnote', $footnote );
if( $footnote )
$gBitSmarty->assign( 'has_footnote', 'y' );
$gBitSmarty->assign( 'parsed_footnote', LibertyContent::parseDataHash( $footnote ) );
if( isset( $_REQUEST['footnote'] ) ) {
$gBitSmarty->assign( 'parsed_footnote', LibertyContent::parseDataHash( $_REQUEST['footnote'] ) );
$gBitSmarty->assign( 'footnote', $_REQUEST['footnote'] );
$gBitSmarty->assign( 'has_footnote', 'y' );
if( empty( $_REQUEST['footnote'] ) ) {
$gContent->expungeFootnote( $gBitUser->mUserId );
} else {
$gContent->storeFootnote( $gBitUser->mUserId, $_REQUEST['footnote'] );
}
}
}
}
if( isset( $_REQUEST["edit"] ) ) {
$formInfo['edit'] = $_REQUEST["edit"];
}
if(isset($_REQUEST["section"])) {
$formInfo['section'] = $_REQUEST["section"];
$formInfo['edit_section'] = 1;
}
if( isset( $_REQUEST['title'] ) ) {
$formInfo['title'] = $_REQUEST['title'];
} elseif( isset( $_REQUEST['page'] ) ) {
$formInfo['title'] = $_REQUEST['page'];
}
if( isset( $_REQUEST["description"] ) ) {
$formInfo['description'] = $_REQUEST["description"];
}
if( isset( $_REQUEST["edit_comment"] ) ) {
$formInfo['edit_comment'] = $_REQUEST["edit_comment"];
} else {
$formInfo['edit_comment'] = '';
}
$cat_obj_type = BITPAGE_CONTENT_TYPE_GUID;
if( $gBitSystem->isFeatureActive( 'wiki_copyrights' ) ) {
if( isset( $_REQUEST['copyrightTitle'] ) ) {
$gBitSmarty->assign( 'copyrightTitle', $_REQUEST["copyrightTitle"] );
}
if( isset( $_REQUEST['copyrightYear'] ) ) {
$gBitSmarty->assign( 'copyrightYear', $_REQUEST["copyrightYear"] );
}
if( isset( $_REQUEST['copyrightAuthors'] ) ) {
$gBitSmarty->assign( 'copyrightAuthors', $_REQUEST["copyrightAuthors"] );
}
}
// Pro
// Check if the page has changed
if( isset( $_REQUEST["fCancel"] ) ) {
if( @BitBase::verifyId( $gContent->mContentId ) ) {
header( "Location: ".$gContent->getDisplayUrl() );
} else {
header( "Location: ".WIKI_PKG_URL );
}
die;
} elseif( isset( $_REQUEST["fSavePage"] ) ) {
// Check if all Request values are delivered, and if not, set them
// to avoid error messages. This can happen if some features are
// disabled
// add permisions here otherwise return error!
if( $gBitSystem->isFeatureActive( 'wiki_copyrights' )
&& isset( $_REQUEST['copyrightAuthors'] )
&& !empty( $_REQUEST['copyrightYear'] )
&& !empty( $_REQUEST['copyrightTitle'] )
) {
require_once( WIKI_PKG_INCLUDE_PATH.'copyrights_lib.php' );
$copyrightYear = $_REQUEST['copyrightYear'];
$copyrightTitle = $_REQUEST['copyrightTitle'];
$copyrightAuthors = $_REQUEST['copyrightAuthors'];
$copyrightslib->add_copyright( $gContent->mPageId, $copyrightTitle, $copyrightYear, $copyrightAuthors, $gBitUser->mUserId );
}
if( $gContent->mPageId )
{ if( isset( $_REQUEST['isminor'] ) && $_REQUEST['isminor']=='on' ) {
$_REQUEST['minor']=true;
} else {
$_REQUEST['minor']=false;
// $links = $gContent->get_links( $edit );
// $wikilib->cache_links( $links );
// $gContent->storeLinks( $links );
}
} else {
// $links = $gContent->get_links( $_REQUEST["edit"] );
// $notcachedlinks = $gContent->get_links_nocache( $_REQUEST["edit"] );
// $cachedlinks = array_diff( $links, $notcachedlinks );
// $gContent->cache_links( $cachedlinks );
// $gContent->storeLinks( $cachedlinks );
}
$data_to_parse = $formInfo['edit'];
if (!empty($formInfo['section']) && !empty($gContent->mInfo['data']) ) {
$full_page_data = $gContent->mInfo['data'];
$data_to_parse = replace_section($full_page_data,$formInfo['section'],$formInfo['edit']);
$_REQUEST["edit"] = $data_to_parse;
}
if( $gContent->store( $_REQUEST ) ) {
if( $gBitSystem->isFeatureActive( 'wiki_watch_author' ) ) {
$gBitUser->storeWatch( "wiki_page_changed", $gContent->mPageId, $gContent->mContentTypeGuid, $_REQUEST['title'], $gContent->getDisplayUrl() );
}
header( "Location: ".$gContent->getDisplayUrl( $gContent->mPageName ) );
die;
} else {
$formInfo = $_REQUEST;
$formInfo['data'] = &$_REQUEST['edit'];
}
} elseif( !empty( $_REQUEST['edit'] ) ) {
// perhaps we have a javascript non-saving form submit
$formInfo = $_REQUEST;
$formInfo['data'] = &$_REQUEST['edit'];
}
if( isset( $_REQUEST['format_guid'] ) && !isset( $gContent->mInfo['format_guid'] ) ) {
$formInfo['format_guid'] = $gContent->mInfo['format_guid'] = $_REQUEST['format_guid'];
}
if( isset( $_REQUEST["preview"] ) ) {
// Chrome does not like seeing the page source in the page itself.
header("X-XSS-Protection: 0");
$gBitSmarty->assign( 'preview',1 );
$gBitSmarty->assign( 'title',!empty( $_REQUEST["title"] ) ? $_REQUEST["title"]:$gContent->mPageName );
if (!empty($formInfo['section'])) {
$formInfo['edit_section'] = 1;
}
$data_to_parse['data'] = $formInfo['edit'];
if( !empty( $formInfo['section'] ) && !empty( $gContent->mInfo['data'] )) {
$full_page_data = $gContent->mInfo['data'];
}
$formInfo['parsed_data'] = LibertyContent::parseDataHash( $data_to_parse );
$gContent->invokeServices( 'content_preview_function' );
}
if( $gContent->isInStructure() ) {
$gBitSmarty->assign( 'showstructs', $gContent->getStructures() );
}
// Flag for 'page bar' that currently 'Edit' mode active
// so no need to show comments & attachments, but need
// to show 'wiki quick help'
$gBitSmarty->assign( 'edit_page', 'y' );
// formInfo might be set due to a error on submit
if( empty( $formInfo ) ) {
$formInfo = &$gContent->mInfo;
}
// make original page title available for template
$formInfo['original_title'] =( !empty( $gContent->mInfo['title'] ) ) ? $gContent->mInfo['title'] : "" ;
$gBitSmarty->assignByRef( 'pageInfo', $formInfo );
$gBitSmarty->assignByRef( 'errors', $gContent->mErrors );
if( $gBitSystem->isPackageActive( 'ckeditor' ) ) {
// loadCkEditor();
}
$gBitSystem->display( 'bitpackage:wiki/edit_page.tpl', 'Edit: '.$gContent->getTitle() , array( 'display_mode' => 'edit' ));
//******************* WIKI Edit Functions
function htmldecode( $string ) {
$string = strtr( $string, array_flip( get_html_translation_table( HTML_ENTITIES ) ) );
$string = preg_replace( "/&#([0-9]+);/me", "chr('\\1')", $string );
return $string;
}
function parse_output( &$obj, &$parts,$i ) {
if( !empty( $obj->parts ) ) {
for( $i=0; $i<count( $obj->parts ); $i++ ) {
parse_output( $obj->parts[$i], $parts,$i );
}
} else {
$ctype = $obj->ctype_primary.'/'.$obj->ctype_secondary;
switch( $ctype ) {
case 'application/x-tikiwiki':
$aux["body"] = $obj->body;
$ccc=$obj->headers["content-type"];
$items = split( ';',$ccc );
foreach( $items as $item ) {
$portions = split( '=',$item );
if( isset( $portions[0] ) &&isset( $portions[1] ) ) {
$aux[trim( $portions[0] )]=trim( $portions[1] );
}
}
$parts[]=$aux;
}
}
}
function extract_section($data,$section) {
global $gContent, $gBitSystem;
if( $gContent->mInfo['format_guid'] == PLUGIN_GUID_TIKIWIKI ) {
$section_data = preg_split( "/\n(".( str_repeat( "!", $gBitSystem->getConfig( "wiki_section_edit" ) ) )."[^!])/", "\n$data", -1, PREG_SPLIT_DELIM_CAPTURE );
$a = 1 + ($section - 1) * 2;
$b = $a + 1;
return $section_data[$a] . $section_data[$b];
}
}
function replace_section($data,$section,$new_section_data) {
global $gContent, $gBitSystem;
if( $gContent->mInfo['format_guid'] == PLUGIN_GUID_TIKIWIKI ) {
$section_data = preg_split("/(\n".( str_repeat( "!", $gBitSystem->getConfig( "wiki_section_edit" ) ) )."[^!])/", "\n$data" ,-1,PREG_SPLIT_DELIM_CAPTURE);
$a = 1 + ($section - 1) * 2;
$b = $a + 1;
$section_data[$a] = "\n";
$section_data[$b] = $new_section_data;
return substr(implode('',$section_data),1);
}
}
function compare_import_versions( $a1, $a2 ) {
return $a1["version"] - $a2["version"];
}
/**
* \brief Parsed HTML tree walker( used by HTML sucker )
*
* This is initial implementation( stupid... w/o any intellegence( almost : ) )
* It is rapidly designed version... just for test: 'can this feature be useful'.
* Later it should be replaced by well designed one :) don't bash me now : )
*
* \param &$c array -- parsed HTML
* \param &$src string -- output string
* \param &$p array -- ['stack'] = closing strings stack,
['listack'] = stack of list types currently opened
['first_td'] = flag: 'is <tr> was just before this <td>'
*/
function walk_and_parse( &$c, &$src, &$p ) {
for( $i=0; $i <= $c["contentpos"]; $i++ ) {
// If content type 'text' output it to destination...
if( $c[$i]["type"] == "text" ) {
$src .= $c[$i]["data"];
} elseif( $c[$i]["type"] == "tag" ) {
if( $c[$i]["data"]["type"] == "open" ) {
// Open tag type
switch( $c[$i]["data"]["name"] ) {
case "br": $src .= "\n"; break;
case "title": $src .= "\n!"; $p['stack'][] = array( 'tag' => 'title', 'string' => "\n" ); break;
case "p": $src .= "\n"; $p['stack'][] = array( 'tag' => 'p', 'string' => "\n" ); break;
case "b": $src .= '__'; $p['stack'][] = array( 'tag' => 'b', 'string' => '__' ); break;
case "i": $src .= "''"; $p['stack'][] = array( 'tag' => 'i', 'string' => "''" ); break;
case "u": $src .= "=="; $p['stack'][] = array( 'tag' => 'u', 'string' => "==" ); break;
case "center": $src .= '::'; $p['stack'][] = array( 'tag' => 'center', 'string' => '::' ); break;
case "code": $src .= '-+'; $p['stack'][] = array( 'tag' => 'code', 'string' => '+-' ); break;
// headers detection looks like real suxx code...
// but possible it run faster :) I don't know where is profiler in PHP...
case "h1": $src .= "\n!"; $p['stack'][] = array( 'tag' => 'h1', 'string' => "\n" ); break;
case "h2": $src .= "\n!!"; $p['stack'][] = array( 'tag' => 'h2', 'string' => "\n" ); break;
case "h3": $src .= "\n!!!"; $p['stack'][] = array( 'tag' => 'h3', 'string' => "\n" ); break;
case "h4": $src .= "\n!!!!"; $p['stack'][] = array( 'tag' => 'h4', 'string' => "\n" ); break;
case "h5": $src .= "\n!!!!!"; $p['stack'][] = array( 'tag' => 'h5', 'string' => "\n" ); break;
case "h6": $src .= "\n!!!!!!"; $p['stack'][] = array( 'tag' => 'h6', 'string' => "\n" ); break;
case "pre": $src .= '~pp~'; $p['stack'][] = array( 'tag' => 'pre', 'string' => '~/pp~' ); break;
// Table parser
case "table": $src .= '||'; $p['stack'][] = array( 'tag' => 'table', 'string' => '||' ); break;
case "tr": $p['first_td'] = true; break;
case "td": $src .= $p['first_td'] ? '' : '|'; $p['first_td'] = false; break;
// Lists parser
case "ul": $p['listack'][] = '*'; break;
case "ol": $p['listack'][] = '#'; break;
case "li":
// Generate wiki list item according to current list depth.
//( ensure '*/#' starts from begining of line )
for( $l = ''; strlen( $l ) < count( $p['listack'] ); $l .= end( $p['listack'] ) );
$src .= "\n$l ";
break;
case "font":
// If color attribute present in <font> tag
if( isset( $c[$i]["pars"]["color"]["value"] ) ) {
$src .= '~~'.$c[$i]["pars"]["color"]["value"].':';
$p['stack'][] = array( 'tag' => 'font', 'string' => '~~' );
}
break;
case "img":
// If src attribute present in <img> tag
if( isset( $c[$i]["pars"]["src"]["value"] ) ) {
// Note what it produce( img ) not {img}! Will fix this below...
$src .= '( img src='.$c[$i]["pars"]["src"]["value"].' )';
}
break;
case "a":
// If href attribute present in <a> tag
if( isset( $c[$i]["pars"]["href"]["value"] ) ) {
$src .= '['.$c[$i]["pars"]["href"]["value"].'|';
$p['stack'][] = array( 'tag' => 'a', 'string' => ']' );
}
break;
}
} else {
// This is close tag type. Is that smth we r waiting for?
switch( $c[$i]["data"]["name"] ) {
case "ul":
if( end( $p['listack'] ) == '*' ) array_pop( $p['listack'] );
break;
case "ol":
if( end( $p['listack'] ) == '#' ) array_pop( $p['listack'] );
break;
default:
$e = end( $p['stack'] );
if( $c[$i]["data"]["name"] == $e['tag'] ) {
$src .= $e['string'];
array_pop( $p['stack'] );
}
break;
}
}
}
// Recursive call on tags with content...
if( isset( $c[$i]["content"] ) ) {
// if( substr( $src, -1 )!= " " )$src .= " ";
walk_and_parse( $c[$i]["content"], $src, $p );
}
}
}
if( isset( $_REQUEST["suck_url"] ) ) {
if( !$gBitSystem->isFeatureActive( 'wiki_url_import' ) ) {
$gBitSystem->fatalError( tra( "Importing remote URLs is disabled" ));
}
// Suck another page and append to the end of current
require_once( UTIL_PKG_INCLUDE_PATH.'htmlparser/html_parser_inc.php' );
$suck_url = isset( $_REQUEST["suck_url"] ) ? $_REQUEST["suck_url"] : '';
$parsehtml = isset( $_REQUEST["parsehtml"] ) ? ( $_REQUEST["parsehtml"] == 'on' ? 'y' : 'n' ): 'n';
if( isset( $_REQUEST['do_suck'] ) && strlen( $suck_url ) > 0 ) {
// \note by zaufi
// This is ugly implementation of wiki HTML import.
// I think it should be plugable import/export converters with ability
// to choose from edit form what converter to use for operation.
// In case of import converter, it can try to guess what source
// file is( using mime type from remote server response ).
// Of couse converters may have itsown configuration panel what should be
// pluged into wiki page edit form too...( like HTML importer may have
// flags 'strip HTML tags' and 'try to convert HTML to wiki' : )
// At least one export filter for wiki already coded : ) -- PDF exporter...
$parsed_url = parse_url($suck_url);
//Disallow urls without schema (usually relative urls), or http(s)
if(!isset($parsed_url['scheme']) || ($parsed_url['scheme']!='http' && $parsed_url['scheme']!='https')){
$gBitSystem->fatalError( tra( "Invalid URL; not absolute or not HTTP" ));
}
//Make sure the passed host isn't local
if(!isset($parsed_url['host']) || ($parsed_url['host']=='localhost') || strncmp($parsed_url['host'],"127.",4)==0){
$gBitSystem->fatalError( tra( "The host specified is either empty or local." ));
}
$sdta = @file_get_contents( $suck_url );
if( isset( $php_errormsg ) && strlen( $php_errormsg ) ) {
$gBitSystem->fatalError( tra( "Can't import remote HTML page" ));
}
// Need to parse HTML?
if( $parsehtml == 'y' ) {
// Read compiled( serialized ) grammar
$grammarfile = UTIL_PKG_INCLUDE_PATH.'htmlparser/htmlgrammar.cmp';
if( !$fp = @fopen( $grammarfile,'r' ) ) {
$gBitSystem->fatalError( tra( "Can't parse remote HTML page" ));
}
$grammar = unserialize( fread( $fp, filesize( $grammarfile ) ) );
fclose( $fp );
// create parser object, insert html code and parse it
$htmlparser = new HtmlParser( $sdta, $grammar, '', 0 );
$htmlparser->Parse();
// Should I try to convert HTML to wiki?
$parseddata = '';
$p = array( 'stack' => array(), 'listack' => array(), 'first_td' => false );
walk_and_parse( $htmlparser->content, $parseddata, $p );
// Is some tags still opened?( It can be if HTML not valid, but this is not reason
// to produce invalid wiki : )
while( count( $p['stack'] ) ) {
$e = end( $p['stack'] );
$sdta .= $e['string'];
array_pop( $p['stack'] );
}
// Unclosed lists r ignored... wiki have no special start/end lists syntax....
// OK. Things remains to do:
// 1 ) fix linked images
$parseddata = preg_replace( ',\[(.*)\|\( img src=(.*)\)\],mU','{img src=$2 link=$1}', $parseddata );
// 2 ) fix remains images( not in links )
$parseddata = preg_replace( ',\( img src=(.*)\),mU','{img src=$1}', $parseddata );
// 3 ) remove empty lines
$parseddata = preg_replace( ",[\n]+,mU","\n", $parseddata );
// Reassign previous data
$sdta = $parseddata;
}
$_REQUEST['edit'] .= $sdta;
}
}
//***************************************
?>