]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/CGI.pm
Implemented --underlaydir, and moved files provided by underlay out of doc
[git.ikiwiki.info.git] / IkiWiki / CGI.pm
1 #!/usr/bin/perl
3 use warnings;
4 use strict;
6 package IkiWiki;
8 sub page_locked ($$;$) { #{{{
9         my $page=shift;
10         my $session=shift;
11         my $nonfatal=shift;
12         
13         my $user=$session->param("name");
14         return if length $user && is_admin($user);
16         foreach my $admin (@{$config{adminuser}}) {
17                 my $locked_pages=userinfo_get($admin, "locked_pages");
18                 if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
19                         return 1 if $nonfatal;
20                         error(htmllink("", $page, 1)." is locked by ".
21                               htmllink("", $admin, 1)." and cannot be edited.");
22                 }
23         }
25         return 0;
26 } #}}}
28 sub cgi_recentchanges ($) { #{{{
29         my $q=shift;
30         
31         unlockwiki();
33         my $template=HTML::Template->new(
34                 filename => "$config{templatedir}/recentchanges.tmpl"
35         );
36         $template->param(
37                 title => "RecentChanges",
38                 indexlink => indexlink(),
39                 wikiname => $config{wikiname},
40                 changelog => [rcs_recentchanges(100)],
41                 styleurl => styleurl(),
42         );
43         print $q->header, $template->output;
44 } #}}}
46 sub cgi_signin ($$) { #{{{
47         my $q=shift;
48         my $session=shift;
50         eval q{use CGI::FormBuilder};
51         my $form = CGI::FormBuilder->new(
52                 title => "signin",
53                 fields => [qw(do title page subpage from name password confirm_password email)],
54                 header => 1,
55                 method => 'POST',
56                 validate => {
57                         confirm_password => {
58                                 perl => q{eq $form->field("password")},
59                         },
60                         email => 'EMAIL',
61                 },
62                 required => 'NONE',
63                 javascript => 0,
64                 params => $q,
65                 action => $q->request_uri,
66                 header => 0,
67                 template => (-e "$config{templatedir}/signin.tmpl" ?
68                               "$config{templatedir}/signin.tmpl" : ""),
69                 stylesheet => styleurl(),
70         );
71         
72         $form->field(name => "name", required => 0);
73         $form->field(name => "do", type => "hidden");
74         $form->field(name => "page", type => "hidden");
75         $form->field(name => "title", type => "hidden");
76         $form->field(name => "from", type => "hidden");
77         $form->field(name => "subpage", type => "hidden");
78         $form->field(name => "password", type => "password", required => 0);
79         $form->field(name => "confirm_password", type => "password", required => 0);
80         $form->field(name => "email", required => 0);
81         if ($q->param("do") ne "signin") {
82                 $form->text("You need to log in first.");
83         }
84         
85         if ($form->submitted) {
86                 # Set required fields based on how form was submitted.
87                 my %required=(
88                         "Login" => [qw(name password)],
89                         "Register" => [qw(name password confirm_password email)],
90                         "Mail Password" => [qw(name)],
91                 );
92                 foreach my $opt (@{$required{$form->submitted}}) {
93                         $form->field(name => $opt, required => 1);
94                 }
95         
96                 # Validate password differently depending on how
97                 # form was submitted.
98                 if ($form->submitted eq 'Login') {
99                         $form->field(
100                                 name => "password",
101                                 validate => sub {
102                                         length $form->field("name") &&
103                                         shift eq userinfo_get($form->field("name"), 'password');
104                                 },
105                         );
106                         $form->field(name => "name", validate => '/^\w+$/');
107                 }
108                 else {
109                         $form->field(name => "password", validate => 'VALUE');
110                 }
111                 # And make sure the entered name exists when logging
112                 # in or sending email, and does not when registering.
113                 if ($form->submitted eq 'Register') {
114                         $form->field(
115                                 name => "name",
116                                 validate => sub {
117                                         my $name=shift;
118                                         length $name &&
119                                         ! userinfo_get($name, "regdate");
120                                 },
121                         );
122                 }
123                 else {
124                         $form->field(
125                                 name => "name",
126                                 validate => sub {
127                                         my $name=shift;
128                                         length $name &&
129                                         userinfo_get($name, "regdate");
130                                 },
131                         );
132                 }
133         }
134         else {
135                 # First time settings.
136                 $form->field(name => "name", comment => "use FirstnameLastName");
137                 $form->field(name => "confirm_password", comment => "(only needed");
138                 $form->field(name => "email",            comment => "for registration)");
139                 if ($session->param("name")) {
140                         $form->field(name => "name", value => $session->param("name"));
141                 }
142         }
144         if ($form->submitted && $form->validate) {
145                 if ($form->submitted eq 'Login') {
146                         $session->param("name", $form->field("name"));
147                         if (defined $form->field("do") && 
148                             $form->field("do") ne 'signin') {
149                                 print $q->redirect(cgiurl(
150                                         do => $form->field("do"),
151                                         page => $form->field("page"),
152                                         title => $form->field("title"),
153                                         subpage => $form->field("subpage"),
154                                         from => $form->field("from"),
155                                 ));
156                         }
157                         else {
158                                 print $q->redirect($config{url});
159                         }
160                 }
161                 elsif ($form->submitted eq 'Register') {
162                         my $user_name=$form->field('name');
163                         if (userinfo_setall($user_name, {
164                                            'email' => $form->field('email'),
165                                            'password' => $form->field('password'),
166                                            'regdate' => time
167                                          })) {
168                                 $form->field(name => "confirm_password", type => "hidden");
169                                 $form->field(name => "email", type => "hidden");
170                                 $form->text("Registration successful. Now you can Login.");
171                                 print $session->header();
172                                 print misctemplate($form->title, $form->render(submit => ["Login"]));
173                         }
174                         else {
175                                 error("Error saving registration.");
176                         }
177                 }
178                 elsif ($form->submitted eq 'Mail Password') {
179                         my $user_name=$form->field("name");
180                         my $template=HTML::Template->new(
181                                 filename => "$config{templatedir}/passwordmail.tmpl"
182                         );
183                         $template->param(
184                                 user_name => $user_name,
185                                 user_password => userinfo_get($user_name, "password"),
186                                 wikiurl => $config{url},
187                                 wikiname => $config{wikiname},
188                                 REMOTE_ADDR => $ENV{REMOTE_ADDR},
189                         );
190                         
191                         eval q{use Mail::Sendmail};
192                         my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
193                         sendmail(
194                                 To => userinfo_get($user_name, "email"),
195                                 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
196                                 Subject => "$config{wikiname} information",
197                                 Message => $template->output,
198                         ) or error("Failed to send mail");
199                         
200                         $form->text("Your password has been emailed to you.");
201                         $form->field(name => "name", required => 0);
202                         print $session->header();
203                         print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
204                 }
205         }
206         else {
207                 print $session->header();
208                 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
209         }
210 } #}}}
212 sub cgi_prefs ($$) { #{{{
213         my $q=shift;
214         my $session=shift;
216         eval q{use CGI::FormBuilder};
217         my $form = CGI::FormBuilder->new(
218                 title => "preferences",
219                 fields => [qw(do name password confirm_password email locked_pages)],
220                 header => 0,
221                 method => 'POST',
222                 validate => {
223                         confirm_password => {
224                                 perl => q{eq $form->field("password")},
225                         },
226                         email => 'EMAIL',
227                 },
228                 required => 'NONE',
229                 javascript => 0,
230                 params => $q,
231                 action => $q->request_uri,
232                 template => (-e "$config{templatedir}/prefs.tmpl" ?
233                               "$config{templatedir}/prefs.tmpl" : ""),
234                 stylesheet => styleurl(),
235         );
236         my @buttons=("Save Preferences", "Logout", "Cancel");
237         
238         my $user_name=$session->param("name");
239         $form->field(name => "do", type => "hidden");
240         $form->field(name => "name", disabled => 1,
241                 value => $user_name, force => 1);
242         $form->field(name => "password", type => "password");
243         $form->field(name => "confirm_password", type => "password");
244         $form->field(name => "locked_pages", size => 50,
245                 comment => "(".htmllink("", "GlobList", 1).")");
246         
247         if (! is_admin($user_name)) {
248                 $form->field(name => "locked_pages", type => "hidden");
249         }
250         
251         if (! $form->submitted) {
252                 $form->field(name => "email", force => 1,
253                         value => userinfo_get($user_name, "email"));
254                 $form->field(name => "locked_pages", force => 1,
255                         value => userinfo_get($user_name, "locked_pages"));
256         }
257         
258         if ($form->submitted eq 'Logout') {
259                 $session->delete();
260                 print $q->redirect($config{url});
261                 return;
262         }
263         elsif ($form->submitted eq 'Cancel') {
264                 print $q->redirect($config{url});
265                 return;
266         }
267         elsif ($form->submitted eq "Save Preferences" && $form->validate) {
268                 foreach my $field (qw(password email locked_pages)) {
269                         if (length $form->field($field)) {
270                                 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
271                         }
272                 }
273                 $form->text("Preferences saved.");
274         }
275         
276         print $session->header();
277         print misctemplate($form->title, $form->render(submit => \@buttons));
278 } #}}}
280 sub cgi_editpage ($$) { #{{{
281         my $q=shift;
282         my $session=shift;
284         eval q{use CGI::FormBuilder};
285         my $form = CGI::FormBuilder->new(
286                 fields => [qw(do rcsinfo subpage from page content comments)],
287                 header => 1,
288                 method => 'POST',
289                 validate => {
290                         content => '/.+/',
291                 },
292                 required => [qw{content}],
293                 javascript => 0,
294                 params => $q,
295                 action => $q->request_uri,
296                 table => 0,
297                 template => "$config{templatedir}/editpage.tmpl"
298         );
299         my @buttons=("Save Page", "Preview", "Cancel");
300         
301         # This untaint is safe because titlepage removes any problimatic
302         # characters.
303         my ($page)=titlepage(possibly_foolish_untaint(lc($form->param('page'))));
304         if (! defined $page || ! length $page ||
305             $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
306                 error("bad page name");
307         }
308         $page=lc($page);
309         
310         my $file=$page.$config{default_pageext};
311         my $newfile=1;
312         if (exists $pagesources{lc($page)}) {
313                 $file=$pagesources{lc($page)};
314                 $newfile=0;
315         }
317         $form->field(name => "do", type => 'hidden');
318         $form->field(name => "from", type => 'hidden');
319         $form->field(name => "rcsinfo", type => 'hidden');
320         $form->field(name => "subpage", type => 'hidden');
321         $form->field(name => "page", value => "$page", force => 1);
322         $form->field(name => "comments", type => "text", size => 80);
323         $form->field(name => "content", type => "textarea", rows => 20,
324                 cols => 80);
325         $form->tmpl_param("can_commit", $config{rcs});
326         $form->tmpl_param("indexlink", indexlink());
327         $form->tmpl_param("helponformattinglink",
328                 htmllink("", "HelpOnFormatting", 1));
329         $form->tmpl_param("styleurl", styleurl());
330         if (! $form->submitted) {
331                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
332                         force => 1);
333         }
334         
335         if ($form->submitted eq "Cancel") {
336                 print $q->redirect("$config{url}/".htmlpage($page));
337                 return;
338         }
339         elsif ($form->submitted eq "Preview") {
340                 require IkiWiki::Render;
341                 $form->tmpl_param("page_preview",
342                         htmlize($config{default_pageext},
343                                 linkify($form->field('content'), $page)));
344         }
345         else {
346                 $form->tmpl_param("page_preview", "");
347         }
348         $form->tmpl_param("page_conflict", "");
349         
350         if (! $form->submitted || $form->submitted eq "Preview" || 
351             ! $form->validate) {
352                 if ($form->field("do") eq "create") {
353                         if (exists $pagesources{lc($page)}) {
354                                 # hmm, someone else made the page in the
355                                 # meantime?
356                                 print $q->redirect("$config{url}/".htmlpage($page));
357                                 return;
358                         }
359                         
360                         my @page_locs;
361                         my $best_loc;
362                         my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
363                         if (! defined $from || ! length $from ||
364                             $from ne $form->param('from') ||
365                             $from=~/$config{wiki_file_prune_regexp}/ ||
366                             $from=~/^\// ||
367                             $form->submitted eq "Preview") {
368                                 @page_locs=$best_loc=$page;
369                         }
370                         else {
371                                 my $dir=$from."/";
372                                 $dir=~s![^/]+/$!!;
373                                 
374                                 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
375                                     $page eq 'discussion') {
376                                         $best_loc="$from/$page";
377                                 }
378                                 else {
379                                         $best_loc=$dir.$page;
380                                 }
381                                 
382                                 push @page_locs, $dir.$page;
383                                 push @page_locs, "$from/$page";
384                                 while (length $dir) {
385                                         $dir=~s![^/]+/$!!;
386                                         push @page_locs, $dir.$page;
387                                 }
389                                 @page_locs = grep {
390                                         ! exists $pagesources{lc($_)} &&
391                                         ! page_locked($_, $session, 1)
392                                 } @page_locs;
393                         }
395                         $form->tmpl_param("page_select", 1);
396                         $form->field(name => "page", type => 'select',
397                                 options => \@page_locs, value => $best_loc);
398                         $form->title("creating ".pagetitle($page));
399                 }
400                 elsif ($form->field("do") eq "edit") {
401                         page_locked($page, $session);
402                         if (! defined $form->field('content') || 
403                             ! length $form->field('content')) {
404                                 my $content="";
405                                 if (exists $pagesources{lc($page)}) {
406                                         $content=readfile(srcfile($pagesources{lc($page)}));
407                                         $content=~s/\n/\r\n/g;
408                                 }
409                                 $form->field(name => "content", value => $content,
410                                         force => 1);
411                         }
412                         $form->tmpl_param("page_select", 0);
413                         $form->field(name => "page", type => 'hidden');
414                         $form->title("editing ".pagetitle($page));
415                 }
416                 
417                 print $form->render(submit => \@buttons);
418         }
419         else {
420                 # save page
421                 page_locked($page, $session);
422                 
423                 my $content=$form->field('content');
424                 $content=~s/\r\n/\n/g;
425                 $content=~s/\r/\n/g;
426                 writefile("$config{srcdir}/$file", $content);
427                 
428                 my $message="web commit ";
429                 if (length $session->param("name")) {
430                         $message.="by ".$session->param("name");
431                 }
432                 else {
433                         $message.="from $ENV{REMOTE_ADDR}";
434                 }
435                 if (defined $form->field('comments') &&
436                     length $form->field('comments')) {
437                         $message.=": ".$form->field('comments');
438                 }
439                 
440                 if ($config{rcs}) {
441                         if ($newfile) {
442                                 rcs_add($file);
443                         }
444                         # prevent deadlock with post-commit hook
445                         unlockwiki();
446                         # presumably the commit will trigger an update
447                         # of the wiki
448                         my $conflict=rcs_commit($file, $message,
449                                 $form->field("rcsinfo"));
450                 
451                         if (defined $conflict) {
452                                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
453                                         force => 1);
454                                 $form->tmpl_param("page_conflict", 1);
455                                 $form->field("content", value => $conflict, force => 1);
456                                 $form->field("do", "edit)");
457                                 $form->tmpl_param("page_select", 0);
458                                 $form->field(name => "page", type => 'hidden');
459                                 $form->title("editing $page");
460                                 print $form->render(submit => \@buttons);
461                                 return;
462                         }
463                 }
464                 else {
465                         require IkiWiki::Render;
466                         refresh();
467                         saveindex();
468                 }
469                 
470                 # The trailing question mark tries to avoid broken
471                 # caches and get the most recent version of the page.
472                 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
473         }
474 } #}}}
476 sub cgi () { #{{{
477         eval q{use CGI};
478         eval q{use CGI::Session};
479         
480         my $q=CGI->new;
481         
482         my $do=$q->param('do');
483         if (! defined $do || ! length $do) {
484                 error("\"do\" parameter missing");
485         }
486         
487         # Things that do not need a session.
488         if ($do eq 'recentchanges') {
489                 cgi_recentchanges($q);
490                 return;
491         }
492         
493         CGI::Session->name("ikiwiki_session_$config{wikiname}");
494         
495         my $oldmask=umask(077);
496         my $session = CGI::Session->new("driver:db_file", $q,
497                 { FileName => "$config{wikistatedir}/sessions.db" });
498         umask($oldmask);
499         
500         # Everything below this point needs the user to be signed in.
501         if ((! $config{anonok} && ! defined $session->param("name") ||
502              ! defined $session->param("name") ||
503              ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
504                 cgi_signin($q, $session);
505         
506                 # Force session flush with safe umask.
507                 my $oldmask=umask(077);
508                 $session->flush;
509                 umask($oldmask);
510                 
511                 return;
512         }
513         
514         if ($do eq 'create' || $do eq 'edit') {
515                 cgi_editpage($q, $session);
516         }
517         elsif ($do eq 'prefs') {
518                 cgi_prefs($q, $session);
519         }
520         elsif ($do eq 'blog') {
521                 my $page=titlepage(lc($q->param('title')));
522                 # if the page already exists, munge it to be unique
523                 my $from=$q->param('from');
524                 my $add="";
525                 while (exists $oldpagemtime{"$from/$page$add"}) {
526                         $add=1 unless length $add;
527                         $add++;
528                 }
529                 $q->param('page', $page.$add);
530                 # now run same as create
531                 $q->param('do', 'create');
532                 cgi_editpage($q, $session);
533         }
534         else {
535                 error("unknown do parameter");
536         }
537 } #}}}