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