OSDN Git Service

(no commit message)
[fswiki/fswiki.git] / wiki.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 #
4 # FreeStyleWiki ¥Õ¥í¥ó¥È¥¨¥ó¥ÉCGI¥¹¥¯¥ê¥×¥È
5 #
6 ###############################################################################
7 BEGIN {
8         if(exists $ENV{MOD_PERL}){
9                 # ¥«¥ì¥ó¥È¥Ç¥£¥ì¥¯¥È¥ê¤ÎÊѹ¹
10                 chdir($ENV{FSWIKI_HOME});
11         }
12 }
13 # ModPerl::Registry(Prefork)¤Ç¤Ï¼Â¹Ô»þ¤ËÊѹ¹¤µ¤ì¤Æ¤¤¤ë²ÄǽÀ­¤¬¤¢¤ë
14 if(exists $ENV{MOD_PERL}){
15         chdir($ENV{FSWIKI_HOME});
16 }
17
18 #==============================================================================
19 # ¥â¥¸¥å¡¼¥ë¤Î¥¤¥ó¥¯¥ë¡¼¥É
20 #==============================================================================
21 use Cwd;
22 use lib './lib';
23 # ModPerl::Registry(Prefork)¤Ç¤Ï@INC¤¬½é´ü²½¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤¬¤¢¤ë
24 unshift @INC, './lib' if(exists $ENV{MOD_PERL});
25 use strict;
26 #use CGI::Carp qw(fatalsToBrowser);
27 #use CGI2;
28 use Wiki;
29 use Util;
30 use Jcode;
31 use HTML::Template;
32
33 # ¤³¤ì¤ò¤ä¤é¤Ê¤¤¤ÈApache::Registory¤ÇÆ°¤«¤Ê¤¤
34 if(exists $ENV{MOD_PERL}){
35         eval("use Digest::Perl::MD5;");
36         eval("use plugin::core::Diff;");
37         eval("use plugin::pdf::PDFMaker;");
38         &Jcode::load_module("Jcode::Unicode") unless $Jcode::USE_ENCODE;
39 }
40
41 #==============================================================================
42 # CGI¤ÈWiki¤Î¥¤¥ó¥¹¥¿¥ó¥¹²½
43 #==============================================================================
44 my $wiki = Wiki->new('setup.dat');
45 my $cgi = $wiki->get_CGI();
46
47 Util::override_die();
48 eval {
49         # SessionÍѥǥ£¥ì¥¯¥È¥ê¤ÏFarm¤Ç¤â¶¦Ä̤˻ÈÍѤ¹¤ë
50         $wiki->config('session_dir',$wiki->config('log_dir'));
51         
52         #==============================================================================
53         # Farm¤È¤·¤ÆÆ°ºî¤¹¤ë¾ì¹ç
54         #==============================================================================
55         my $path_info  = $cgi->path_info();
56         my $path_count = 0;
57         if(length($path_info) > 0){
58                 # Farm¤¬¤¢¤ë¤«³Îǧ¤¹¤ë
59                 unless($path_info =~ m<^(/[A-Za-z0-9]+)*/?$> and -d $wiki->config('data_dir').$path_info){
60                         CORE::die("Wiki¤¬Â¸ºß¤·¤Þ¤»¤ó¡£");
61                 }
62                 
63                 # PATH_INFO¤ÎºÇ¸å¤¬/¤À¤Ã¤¿¤é/¤Ê¤·¤ÎURL¤ËžÁ÷¤¹¤ë
64                 if($path_info =~ m|/$|) {
65                         $path_info =~ s|/$||;
66                         $wiki->redirectURL($cgi->url().$path_info);
67                 }
68                 $path_info =~ m</([^/]+)$>;
69                 $wiki->config('script_name', $1);
70                 $wiki->config('data_dir'   , $wiki->config('data_dir'  ).$path_info);
71                 $wiki->config('config_dir' , $wiki->config('config_dir').$path_info);
72                 $wiki->config('backup_dir' , $wiki->config('backup_dir').$path_info);
73                 $wiki->config('log_dir'    , $wiki->config('log_dir'   ).$path_info);
74
75                 if(!($wiki->config('theme_uri') =~ /^(\/|http:|https:|ftp:)/)){
76                         my @paths = split(/\//,$path_info);
77                         $path_count = $#paths;
78                         for(my $i=0;$i<$path_count;$i++){
79                                 $wiki->config('theme_uri','../'.$wiki->config('theme_uri'));
80                         }
81                 }
82         }
83
84         #==============================================================================
85         # ÀßÄê¤òÈ¿±Ç¡Ê¤â¤¦¤Á¤ç¤Ã¤È¥¹¥Þ¡¼¥È¤Ë¤ä¤ê¤¿¤¤¤Í¡Ë
86         #==============================================================================
87         my $config = &Util::load_config_hash($wiki,$wiki->config('config_file'));
88         foreach my $key (keys(%$config)){
89                 $wiki->config($key,$config->{$key});
90         }
91         # ¸ÄÊ̤ËÀßÄ꤬ɬÍפʤâ¤Î¤À¤±¾å½ñ¤­
92         $wiki->config('css',
93                 $wiki->config('theme_uri')."/".$config->{theme}."/".$config->{theme}.".css");
94         $wiki->config('site_tmpl',
95                 $wiki->config('tmpl_dir')."/site/".$config->{site_tmpl_theme}."/".$config->{site_tmpl_theme}.".tmpl");
96         $wiki->config('site_handyphone_tmpl',
97                 $wiki->config('tmpl_dir')."/site/".$config->{site_tmpl_theme}."/".$config->{site_tmpl_theme}."_handyphone.tmpl");
98
99         #==============================================================================
100         # ¥¿¥¤¥à¥¢¥¦¥È¤·¤Æ¤¤¤ë¥»¥Ã¥·¥ç¥ó¤òÇË´þ
101         #==============================================================================
102         $cgi->remove_session($wiki);
103
104         #==============================================================================
105         # ¥æ¡¼¥¶¾ðÊó¤ÎÆɤ߹þ¤ß
106         #==============================================================================
107         my $users = &Util::load_config_hash($wiki,$wiki->config('userdat_file'));
108         foreach my $id (keys(%$users)){
109                 my ($pass,$type) = split(/\t/,$users->{$id});
110                 $wiki->add_user($id,$pass,$type);
111         }
112
113         #==============================================================================
114         # ¥×¥é¥°¥¤¥ó¤Î¥¤¥ó¥¹¥È¡¼¥ë¤È½é´ü²½
115         #==============================================================================
116         my @plugins = split(/\n/,&Util::load_config_text($wiki,$wiki->config('plugin_file')));
117         my $plugin_error = '';
118         foreach(sort(@plugins)){
119                 $plugin_error .= $wiki->install_plugin($_);
120         }
121         # ¥×¥é¥°¥¤¥ó¤´¤È¤Î½é´ü²½½èÍý¤òµ¯Æ°
122         $wiki->do_hook("initialize");
123
124         #==============================================================================
125         # ¥¢¥¯¥·¥ç¥ó¥Ï¥ó¥É¥é¤Î¸Æ¤Ó½Ð¤·
126         #==============================================================================
127         my $action  = $cgi->param("action");
128         my $content = $wiki->call_handler($action);
129
130         # ¥×¥é¥°¥¤¥ó¤Î¥¤¥ó¥¹¥È¡¼¥ë¤Ë¼ºÇÔ¤·¤¿¾ì¹ç
131         $content = $plugin_error . $content if $plugin_error ne '';
132
133         #==============================================================================
134         # ¥ì¥¹¥Ý¥ó¥¹
135         #==============================================================================
136         my $output        = "";
137         my $is_handyphone = &Util::handyphone();
138         my $is_smartphone = &Util::smartphone();
139         my $template_name = "";
140
141         if ($is_handyphone || $is_smartphone) {
142                 $template_name = 'site_handyphone_tmpl';
143         } else {
144                 $template_name = 'site_tmpl';
145         }
146
147         # ¥È¥Ã¥×¥Ú¡¼¥¸¤«¤É¤¦¤«¤òȽÄê
148         my $top  = 0;
149         if($cgi->param("page") eq $wiki->config("frontpage")){
150                 $top = 1;
151         }
152
153         # ¥Ú¡¼¥¸¤Î¥¿¥¤¥È¥ë¤ò·èÄê
154         my $title = "";
155         if($cgi->param('action') eq "" && $wiki->page_exists($cgi->param('page')) && $wiki->is_installed('search')){
156                 $title = "<a href=\"".$wiki->create_url({action=>"SEARCH",word=>$wiki->get_title()})."\">".
157                        &Util::escapeHTML($wiki->get_title())."</a>";
158         } else {
159                 $title = &Util::escapeHTML($wiki->get_title());
160         }
161
162         #------------------------------------------------------------------------------
163         # ¥Ø¥Ã¥À¤ÎÀ¸À®
164         #------------------------------------------------------------------------------
165         my $header_tmpl = HTML::Template->new(filename => $wiki->config('tmpl_dir')."/header.tmpl",
166                                               die_on_bad_params => 0,
167                                               case_sensitive    => 1);
168         # ¥á¥Ë¥å¡¼¤ò¼èÆÀ
169         my @menu = ();
170         foreach(sort {$b->{weight}<=>$a->{weight}} @{$wiki->{menu}}){
171                 if($_->{href} ne ""){
172                         push(@menu,$_);
173                 }
174         }
175         $header_tmpl->param(MENU       => \@menu,
176                             FRONT_PAGE => $top);
177         my $header = $header_tmpl->output();
178
179         #------------------------------------------------------------------------------
180         # ¥Õ¥Ã¥¿¤ÎÀ¸À®
181         #------------------------------------------------------------------------------
182         my $footer_tmpl = HTML::Template->new(filename => $wiki->config('tmpl_dir')."/footer.tmpl",
183                                               die_on_bad_params => 0,
184                                               case_sensitive    => 1);
185
186         # ¥³¥Ô¡¼¥é¥¤¥È¤òɽ¼¨¤¹¤ë¤«¤É¤¦¤«
187         my $admin_name = $wiki->config('admin_name');
188         my $admin_mail = $wiki->config('admin_mail_pub');
189         my $out_copyright  = 1;
190         if($admin_name eq ""){ $admin_name = $admin_mail; }
191         if($admin_name eq "" && $admin_mail eq ""){ $out_copyright = 0; }
192
193         $footer_tmpl->param(ADMIN_NAME    => $admin_name,
194                             ADMIN_MAIL    => $admin_mail,
195                             OUT_COPYRIGHT => $out_copyright,
196                             FRONT_PAGE    => $top,
197                             VERSION       => Wiki->VERSION,
198                             PERL_VERSION  => $]);
199
200         if(exists $ENV{MOD_PERL}){
201                 $footer_tmpl->param(MOD_PERL=>$ENV{MOD_PERL});
202         }
203
204         my $footer = $footer_tmpl->output();
205
206         #------------------------------------------------------------------------------
207         # ¥µ¥¤¥È¥Æ¥ó¥×¥ì¡¼¥È¤Î½èÍý
208         #------------------------------------------------------------------------------
209         # ¥Æ¥ó¥×¥ì¡¼¥È¤ÎÆɤ߹þ¤ß
210         my $template = HTML::Template->new(filename => $wiki->config($template_name),
211                                            die_on_bad_params => 0,
212                                            case_sensitive    => 1);
213
214         # »²¾È¸¢¸Â¤¬¤¢¤ë¤«¤É¤¦¤«
215         my $can_show = 0;
216         if($action ne '' || ($action eq '' && $wiki->can_show($cgi->param('page')))){
217                 $can_show = 1;
218         }
219
220         # head¥¿¥°Æâ¤Ëɽ¼¨¤¹¤ë¾ðÊó¤òºîÀ®
221         my $head_info = "";
222         foreach (@{$wiki->{'head_info'}}){
223                 $head_info .= $_."\n";
224         }
225
226         # ¥Æ¥ó¥×¥ì¡¼¥È¤Ë¥Ñ¥é¥á¡¼¥¿¤ò¥»¥Ã¥È
227         $template->param(SITE_TITLE  => &Util::escapeHTML($wiki->get_title()." - ".$wiki->config('site_title')),
228                          MENU        => $header,
229                          TITLE       => $title,
230                          CONTENT     => $content,
231                          FRONT_PAGE  => $top,
232                          FOOTER      => $footer,
233                          EDIT_MODE   => $action,
234                          CAN_SHOW    => $can_show,
235                          HEAD_INFO   => $head_info,
236                          SITE_NAME   => $wiki->config('site_title'));
237
238         my $login = $wiki->get_login_info();
239         $template->param(
240                 IS_ADMIN => defined($login) && $login->{type}==0,
241                 IS_LOGIN => defined($login)
242         );
243
244         if ($is_handyphone || $is_smartphone) {
245                 # ·ÈÂÓÅÅÏÃÍѽèÍý
246                 $output = $template->output;
247                 &Jcode::convert(\$output,"sjis");
248         } else {
249                 # ¥Ñ¥½¥³¥óÍѽèÍý
250                 my $usercss = &Util::load_config_text($wiki,$wiki->config('usercss_file'));
251                 
252                 if($config->{'theme'} eq ''){
253                         # ¥Æ¡¼¥Þ¤¬»ÈÍѤµ¤ì¤Æ¤ª¤é¤º¡¢³°ÉôCSS¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¤½¤ì¤ò»ÈÍÑ
254                         if($config->{'outer_css'} ne ''){
255                                 $wiki->config('css',$config->{'outer_css'});
256                         # ¥Æ¡¼¥Þ¤â³°ÉôCSS¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¥¹¥¿¥¤¥ë¥·¡¼¥È¤ò»ÈÍѤ·¤Ê¤¤
257                         } else {
258                                 $wiki->config('css','');
259                         }
260                 }
261                 # ¥Ñ¥é¥á¡¼¥¿¤ò¥»¥Ã¥È
262                 $template->param(HAVE_USER_CSS => $usercss ne "",
263                                  THEME_CSS     => $wiki->config('css'),
264                                  USER_CSS      => &Util::escapeHTML($usercss),
265                                  THEME_URI     => $wiki->config('theme_uri'));
266                 
267                 # ¥Ú¡¼¥¸Ì¾¤òEXIST_PAGE_¥Ú¡¼¥¸Ì¾¤È¤¤¤¦¥Ñ¥é¥á¡¼¥¿¤Ë¥»¥Ã¥È
268                 # ¤¿¤À¤·¡¢¥¹¥é¥Ã¥·¥å¤ò´Þ¤à¥Ú¡¼¥¸Ì¾¤Ï¥»¥Ã¥È¤·¤Ê¤¤
269                 my @pagelist = $wiki->get_page_list();
270                 foreach my $page (@pagelist){
271                         if(index($page,"/")==-1 && $wiki->can_show($page)){
272                                 $template->param("EXIST_PAGE_".$page=>1);
273                         }
274                 }
275                 
276                 $output = $template->output;
277                 
278                 # ¥¤¥ó¥¯¥ë¡¼¥ÉÌ¿Îá
279                 # <!--FSWIKI_INCLUDE PAGE="¥Ú¡¼¥¸Ì¾"-->
280                 # ¥Ú¡¼¥¸Ì¾¤ÇWikiName¤ò»ØÄꤹ¤ë¡£
281                 my $fswiki_include_tag = '<!--\s*FSWIKI_INCLUDE\s+PAGE\s*=\s*"([^"]*)"\s*-->';
282                 while($output =~ /$fswiki_include_tag/o){
283                         if($wiki->page_exists($1) && $wiki->can_show($1)){
284                                 $output =~ s/$fswiki_include_tag/$wiki->process_wiki($wiki->get_page($1))/oe;
285                         } else {
286                                 $output =~ s/$fswiki_include_tag//o;
287                         }
288                 }
289         }
290         
291         #------------------------------------------------------------------------------
292         # ½ÐÎϽèÍý
293         #------------------------------------------------------------------------------
294         # ¥Ø¥Ã¥À¤Î½ÐÎÏ
295         if($is_handyphone || $is_smartphone){
296                 print "Content-Type: text/html;charset=Shift_JIS\n";
297         } else {
298                 print "Content-Type: text/html;charset=EUC-JP\n";
299         }
300         print "Pragma: no-cache\n";
301         print "Cache-Control: no-cache\n\n";
302          
303         # HTML¤Î½ÐÎÏ
304         print $output;
305 };
306
307 my $msg = $@;
308 $ENV{'PATH_INFO'} = undef;
309 $wiki->_process_before_exit();
310
311 if($msg && index($msg, 'safe_die')<0){
312         $msg = Util::escapeHTML($msg);
313         print "Content-Type: text/html\n\n";
314         print "<html><head><title>Software Error</title></head>";
315         print "<body><h1>Software Error:</h1><p>$msg</p></body></html>";
316 }
317 Util::restore_die();