forked from bentglasstube/blosxom-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit-v0i90
535 lines (400 loc) · 19.4 KB
/
edit-v0i90
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# Blosxom Plugin: edit
# Author(s): Paul Kulchenko <[email protected]>
# Version: 0.90
# Based on wikieditish plug-in by Rael Dornfest <[email protected]>
# Documentation: See the bottom of this file or type: perldoc edit
package edit;
use vars qw/$noname $nopath $filename $resource $newpage/,
qw/$categories $emptycategory $links $origlinks/,
qw/$delete $rename $moveto $linkto/,
qw/$response $password $title $body $date $name/;
# --- Configurable variables -----
# Should editing this blog require a password?
# 0 = no, 1 = yes (default)
$require_password = 0;
# What is the password for editing this blog?
my $blog_password = '';
# Should editing this blog be restricted to a particular set of IPs?
# 0 = no (default), 1 = yes
$restrict_by_ip = 0;
# To what IPs should editing this blog be restricted?
@ips = qw/ 127.0.0.1 /;
# What file extension should I use for edited pages?
# (Not sure why you'd change this, but just in case...)
my $file_extension = $blosxom::file_extension;
# --------------------------------
# Response to edit; use as $edit::response in
# flavour templates
$response = '';
# The raw title and body
($title, $body) = ('', '');
# The password entered into the form (for prepopulating the form upon Save)
$password = '';
# --------------------------------
use vars qw/$require_password/;
use CGI qw/:standard/;
use FileHandle;
use File::Copy;
use File::Temp qw/tempfile/;
# Strip potentially confounding bits from user-configurable variables
$file_extension =~ s!^\.!!;
sub filter {
my($pkg, $files_ref) = @_;
%files = %$files_ref;
1;
}
sub start {
my($path,$fn) = $blosxom::path_info =~ m!^(?:(.*)/)?([^\?\.]+)(?:\.(.+))?$!;
$path = "/$path" if $path and $path !~ m!^/!;
$flavor = $3;
{
$noname = $fn eq 'new'; # 'new' is a special name
$nopath = !$path;
$filename = "$blosxom::datadir$path/$fn";
$resource = "$blosxom::datadir$path/$fn.$file_extension";
# new resource unless file exists (with extension) or dir exists (without extension)
$newpage = "$blosxom::url$path/$fn.edit" if !-f $resource && !-d $filename;
# populate 'name' for the template ($blosxom::fn doesn't work, because the name is used in head)
$name = $noname && $newpage ? 'File name...' : $fn;
$delete = !$newpage;
$rename = !$newpage || $noname;
$moveto = !$newpage || $nopath;
$linkto = $links::links_supported;
}
# Only spring into action if POSTing to the edit plug-in
if ( request_method() eq 'POST' and param('plugin') eq 'edit' ) {
# these three parameters will be preserved on the form
$password = param('password');
$title = param('title');
$body = param('body');
$name = param('name');
$date = param('date');
my $category = param('newCategory') || param('destCategory');
my $action = lc param('action');
my %newlinks = map {$_ => 1} grep m!^/!, param('destLinks');
my %origlinks = map {$_ => 1} split(/;/, param('origLinks'));
if ($action eq 'cancel') {
$redirect = "${blosxom::url}$path/$fn";
return 1;
}
# Something's fishy with the path
$path =~ /[^\/\w\-]/
and warn "blosxom : edit plugin : something's fishy with the path, $path\n"
and $response = "Something didn't go as expected; page not saved."
and return 1;
# password required, but not set
$require_password and !$blog_password
and warn "blosxom : edit plugin : password required but is not yet set; trying to > $blosxom::datadir$path/$fn.file_extension\n"
and $response = "A password is required to edit this page but one has not yet been set; page not saved."
and return 1;
# password required, set, but not correctly supplied
$require_password and (!param('password') or (param('password') and param('password') ne $blog_password))
and warn "blosxom : edit plugin : incorrect password supplied for > $blosxom::datadir$path/$fn.file_extension\n"
and $response = "Incorrect password supplied; page not saved."
and return 1;
# restricted by ip
$restrict_by_ip and !grep(/^\Q$ENV{'REMOTE_ADDR'}\E$/, @ips)
and warn "blosxom : edit plugin : incorrect IP address > $blosxom::datadir$path/$fn.file_extension\n"
and $response = "Incorrect IP address; page not saved."
and return 1;
# blosxom's $datadir is not writeable
!-w $blosxom::datadir
and warn "blosxom : edit plugin > \$blosxom::datadir, $blosxom::datadir, is not writeable.\n"
and $response = "Something didn't go as expected; page not saved."
and return 1;
($action eq 'rename' or ($action eq 'save' and $noname))
and ($name =~ /^[\w-]+$/
or warn "blosxom : edit plugin : incorrect file name ($name).\n"
and $response = "Incorrect file name ($name); page not saved."
and return 1);
$action eq 'save'
and $title =~ /^\s*$/
and $response = "Title should not be empty; page not saved."
and return 1;
if ($action eq 'rename') {
-e "$blosxom::datadir$category/$name.$file_extension"
and $response = "Entry with this name ($category/$name) already exists; page not saved. Choose a different name or a different category."
and return 1;
rename "$blosxom::datadir$path/$fn.$file_extension"
=> "$blosxom::datadir$path/$name.$file_extension";
if ($links::links_supported) {
links::unlink("$blosxom::datadir$path/$fn.$file_extension" => keys %origlinks);
links::link("$blosxom::datadir$path/$name.$file_extension" => keys %origlinks);
}
# redirect to the new entry
$redirect = "${blosxom::url}$path/$name";
return 1;
}
if ($action eq 'delete') {
rename "$blosxom::datadir$path/$fn.$file_extension"
=> "$blosxom::datadir$path/$fn.$file_extension~";
links::unlink("$blosxom::datadir$path/$fn.$file_extension") if $links::links_supported;
# redirect to the category
$redirect = "${blosxom::url}$path/";
return 1;
}
$fn = $name if $noname;
my $oldpath = $path;
if ($action eq 'move' || ($action eq 'save' && $nopath)) {
$category =~ m!^/!
or $response = "Please select a category name or create a new one. The name should be absolute and start with '/'."
and return 1;
$category =~ /[^\/\w\-]/
and warn "blosxom : edit plugin : something's fishy with the path, $path\n"
and $response = "Category name is not valid ($category); page not saved."
and return 1;
-e "$blosxom::datadir$category/$fn.$file_extension"
and $response = "Entry with this name ($category/$fn) already exists. Choose a different category or rename your entry."
and return 1;
$path = $category;
}
# the destination directory for this blog entry does not yet exist
unless ( -d "$blosxom::datadir$path" and -w "$blosxom::datadir$path" ) {
warn "blosxom : edit plugin : mkdir $blosxom::datadir$path\n";
foreach ( ('', split /\//, $path) ) {
$p .= "/$_";
$p =~ s!^/!!;
-d "$blosxom::datadir/$p" or mkdir "$blosxom::datadir/$p", 0755
or ( warn "blosxom : edit plugin : couldn't mkdir $blosxom::datadir/$p." and return 1 );
}
}
if ($action eq 'move') {
rename "$blosxom::datadir$oldpath/$fn.$file_extension"
=> "$blosxom::datadir$path/$fn.$file_extension";
if ($links::links_supported) {
links::unlink("$blosxom::datadir$oldpath/$fn.$file_extension" => keys %origlinks);
links::link("$blosxom::datadir$path/$fn.$file_extension" => keys %origlinks);
}
}
if ($action eq 'save') {
my $mtime = text2dt($date)
or $response = "Incorrect date format ($date); expected yyyy-mm-dd hh:mm:ss. Page not saved."
and return 1;
my $filename = "$blosxom::datadir$path/$fn.$file_extension";
my($fh, $tmpname) = eval {tempfile};
$@
and do {
warn "couldn't open a tempfile to save $filename";
$response = "There was a problem saving this page.";
return 1;
};
print $fh join "\n", $title, $body;
$fh->close();
# we use move instead of rename, because rename doesn't work across different devices
File::Copy::move $tmpname => $filename
or $response = "There was a problem saving this page ($!)."
and return 1;
utime(time, $mtime, $filename)
or do {
warn "blosxom : edit plugin : couldn't set lastmodified time on $blosxom::datadir$path/$fn.$file_extension.";
return 1;
};
}
if ($action eq 'link' || ($action eq 'save' && $newpage)) {
my @unlink = grep {!$newlinks{$_}} keys %origlinks;
my @link = grep {!$origlinks{$_}} keys %newlinks;
grep(/[^\/\w\-]/, @unlink, @link)
and warn "blosxom : edit plugin : something's fishy with the path, $path\n"
and $response = "Category name is not valid; page not saved."
and return 1;
if ($links::links_supported) {
links::unlink("$blosxom::datadir$path/$fn.$file_extension" => @unlink);
links::link("$blosxom::datadir$path/$fn.$file_extension" => @link);
}
}
# redirect to the updated entry
$redirect = "${blosxom::url}$path/$fn";
}
1;
}
sub head {
my($pkg, $path) = @_;
# prepare list of categories to move to except the current one
my %categories = map {$_ => 1} # remove duplicates
# has been replaced by delete below (2004-03-19 --PK)
# grep {"/$path" !~ m!^$_(/[^\/]+)?$!} # skip current
map {my @c = $_; push @c, $_ while s!/[^/]+$!! && $_; @c} # turn /foo/bar into /foo/bar AND /foo
map {s!^$blosxom::datadir!!; s!/[^/]*$!!; $_} keys %files;
(my $category = $path) =~ s!/[^\/]+$!!;
my $current = delete $categories{"/$category"}; # skip current
$categories = $moveto
? join("\n", "<option>@{[$newpage ? 'Save' : 'Move']} to category...</option>",
map(qq!<option value="$_">$_</option>!, sort keys %categories),
'<option value="@NEW">[New Category]</option>'
)
: '';
# prepare list of categories to link to except the current one
# don't want to modify ::linked by s/// in map{} below; took me 10 minutes to find and fix
my @links = @{$links::linked{"$filename.$file_extension"}};
my %links = map {s!^$blosxom::datadir!!; s!/[^/]*$!!; $_ => 1} @links;
$links = $linkto
? join("\n", '<option>Link to category...</option>', # this is not typo ---------------------vvvvvvvvvv
map(qq!<option @{[$links{$_}?'selected':'']} value="$_">$_</option>!, sort keys %categories)
)
: '';
$origlinks = join(";", grep {$links{$_}} keys %categories);
# don't show rename/move/link controls for links
if ($links::link{"$filename.$file_extension"}) {
$rename = $moveto = $linkto = 0;
(my $linkedfrom = $links::link{"$filename.$file_extension"}) =~ s!^$blosxom::datadir|\..+?$!!g;
$response = qq!Warning: This page is a link to <a href="$blosxom::url$linkedfrom">$linkedfrom</a>. Delete will remove only this page.!;
}
# is it empty category with no files?
$emptycategory = "$blosxom::url/$path/new.edit" if $path && !$newpage && !$current;
# add a resource if it's a new one, so that edit form is displayed properly
$blosxom::files{$resource} = time if $newpage && $flavor eq 'edit';
# show date/time
$date = dt2text($blosxom::files{$resource});
}
sub last {
if ($redirect) {
$blosxom::header = {'-Location'=>$redirect};
$blosxom::output = '';
} elsif (-f $resource) { # cache only entry pages for now
# save cached version
# TODO create folder first
# TODO replace by the smartcache plugin
# my $fh = new FileHandle;
# if ( $fh->open("> $blosxom::static_dir/$blosxom::path_info") ) {
# print $fh $blosxom::output;
# $fh->close();
# }
}
}
sub story {
my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
unless ( param('plugin') eq 'edit' ) {
my @body;
($title, @body) = split /\n/, $blosxom::raw;
$body = join "\n", @body;
}
1;
}
sub dt2text { # time => yyyy-mm-dd hh:mm:ss
my($sec,$min,$hour,$mday,$mon,$year) = localtime(shift);
return sprintf "%04d-%02d-%02d %02d:%02d:%02d",
$year + 1900, $mon + 1, $mday, $hour, $min, $sec;
}
sub text2dt { use Time::Local; # yyyy-mm-dd hh:mm:ss => time
my($time) = shift;
return unless $time =~ /^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/;
# month should be in the range 0..11
return timelocal($6, $5, $4, $3, $2-1, $1);
}
1;
__END__
=head1 NAME
Blosxom Plug-in: edit
=head1 SYNOPSIS
Edit a Blosxom blog wiki-style, from right in the browser.
=head2 QUICK START
Drop this edit plug-in file into your plug-ins directory
(whatever you set as $plugin_dir in blosxom.cgi).
edit, being a well-behaved plug-in, won't do anything until you
either set a password or turn off the password requirement
(set $require_password = 0).
Move the contents of the flavours folder included in this distribution
into your Blosxom data directory (whatever you set as $datadir in blosxom.cgi).
Don't move the folder itself, only the files it contains! If you don't
have the the sample flavours handy, you can download them from:
http://notebook.kulchenko.com/themes/edit/page
Point your browser at one of your Blosxom entries, specifying the edit
flavour (e.g. http://localhost/cgi-bin/blosxom.cgi/path/to/a/post.edit)
Edit the entry, supply your password (if required -- the default), and hit the Save button to save your changes.
You can just as easily create a new blog entry by pointing your browser at a non-existent filename, potentially on a non-existent path (e.g. http://localhost/cgi-bin/blosxom.cgi/path/to/a/nonexistent_post.edit). Give the entry a
title and body, supply your password (again, if required), and hit the Save
button. The edit plug-in will create a new blog entry for you on
your specified path, creating the supplied path's directory structure for you
on the fly if necessary.
Enjoy!
=back
=head2 SAMPLE FLAVOUR TEMPLATES
I've made sample flavour templates available to you to help with any
learning curve this plug-in might require.
NOTE: The edit plug-in requires the presence of a "plugin" form
variable with the value set to "edit"; this tells the plug-in
that it should handle the incoming POSTing data rather than leaving it
for another plug-in.
=back
=head2 FLAVOURING EDIT
While there's not much in the way of template variables and the sample
foot.edit provides about everything you'll need, here's a list of
variables and their purposes for your reference:
=item * $edit::title and $edit::body prepopulate the form with the values from the existing blog entry file.
=item * $edit::password is prepopulated with the password you just entered into and submitted in the "edit this blog" form or preferences stored in a 'edit' cookie, if you've the cookie plug-in installed and enabled.
=back
=head2 INVITING CONTRIBUTIONS
The edit plug-in serves dual purposes: as a browser-based editor for
your Blosxom blog and as a wiki-style community blog, allowing contributions
by a particular group of bloggers (using a shared password) or passers-by
(without need of a password -- true Wiki-style).
If you'd like to invite contribution, you can assocate an "edit" button with
each entry like so:
<a href="$url$path/$fn.edit">edit this blog</a>
HERE
=head1 INSTALLATION
Drop edit into your plug-ins directory ($blosxom::plugin_dir).
=head1 CONFIGURATION
=head2 RESTRICTING BY PASSWORD
By default, the edit plug-in requires a password. You'll need to
set one before being able to edit anything. Set the $blog_password
configuration variable to anything you wish
(e.g. my $blog_password = 'abc123';). Just be
sure to use something you'll remember and other's won't guess.
You can disable password-protection if you wish, allowing passers-by to
contribute to your blog, Wiki-style. Be sure this is something you want to
do. It has some possible security implications, anyone being able to write
to your server's hard drive, post to your public-facing blog, and edit
(read: alter, spindle, contort) any blog postings. Those warning's out of
the way, to turn off password-protection, set $require_password to 0.
=head2 RESTRICTING BY IP
You can alternatively decide to restrict editing to a particular IP address
or addresses -- those in your office, for example, or the machine actually
running Blosxom (127.0.0.1).
To do so, set $restrict_by_ip to 1 (it's off, or 0, by default), and
populate the @ips array with a list of approved IP addresses. By default,
this is set to "this machine", the machine running Blosxom; shorthand for
"this machine" in IP-speak is 127.0.0.1. The following example restricts
editing to those coming from three IPs, including 127.0.0.1:
# Should editing this blog be restricted to a particular set of IPs?
# 0 = no (default), 1 = yes
$restrict_by_ip = 1;
# To what IPs should editing this blog be restricted?
@ips = qw/ 127.0.0.1 10.0.0.3 140.101.22.10/;
Of course, you can use a combination of password-protection and IP
restriction if you so wish.
=head1 VERSION
0.90
=head1 AUTHOR
Paul Kulchenko <[email protected]>
Original wikieditish plug-in -- Rael Dornfest <[email protected]>, http://www.raelity.org/
=head1 SEE ALSO
The edit plug-in plays nicely with the wikiwordish plug-in
[http://www.raelity.org/apps/blosxom/plugins/text/wikiwordish.individual]
for wiki-style linking action. And for wiki-style markup, be sure to try the
textile [http://www.raelity.org/apps/blosxom/plugins/text/textile.individual]
or tiki [http://www.raelity.org/apps/blosxom/plugins/text/tiki.individual]
plug-ins.
Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/
Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml
=head1 BUGS
Address bug reports and comments to the Blosxom mailing list
[http://www.yahoogroups.com/group/blosxom].
=head1 LICENSE
Blosxom and original wikieditish Blosxom Plug-in -- Copyright 2003 Rael Dornfest
This Plug-in -- Copyright 2004 Paul Kulchenko
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.