]> git.koha-community.org Git - koha.git/blob - Install.pm
added some new modular code to koha.upgrade
[koha.git] / Install.pm
1 package Install; #assumes Install.pm
2
3 use strict;
4 require Exporter;
5
6 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
7
8 # set the version for version checking
9 $VERSION = 0.01;
10
11 @ISA = qw(Exporter);
12 @EXPORT = qw(   &checkperlmodules
13                 &getmessage
14                 &showmessage
15                 &releasecandidatewarning
16                 &getinstallationdirectories
17                 &getdatabaseinfo
18                 &getapacheinfo
19                 &getapachevhostinfo
20                 &updateapacheconf
21                 &basicauthentication
22                 &installfiles
23                 &databasesetup
24                 &restartapache
25                 &loadconfigfile
26                 );
27
28
29 my $messages;
30 $messages->{'continuing'}->{en}="Great!  Continuing setup.\n\n";
31 $messages->{'WelcomeToKohaInstaller'}->{en}=qq|
32 =================================
33 = Welcome to the Koha Installer =
34 =================================
35
36 Welcome to the Koha install script!  This script will prompt you for some
37 basic information about your desired setup, then install Koha according to
38 your specifications.  To accept the default value for any question, simply hit
39 Enter at the prompt.
40
41 Please be sure to read the documentation, or visit the Koha website at 
42 http://www.koha.org for more information.
43
44 Are you ready to begin the installation? (Y/[N]): |;
45 $messages->{'ReleaseCandidateWarning'}->{en}=qq|
46 =====================
47 = RELEASE CANDIDATE =
48 =====================
49
50 WARNING WARNING WARNING WARNING WARNING
51
52 You are about to install Koha version %s.  This version of Koha is a
53 release candidate.  It is not intended to be installed on production systems.
54 It is being released so that users can test it before we release a final
55 version.
56
57 Are you sure you want to install Koha %s? (Y/[N]): |;
58 $messages->{'WatchForReleaseAnnouncements'}->{en}=qq|
59
60 Watch for announcements of Koha releases on the Koha mailing list or the Koha
61 web site (http://www.koha.org/).
62
63 |;
64
65 $messages->{'NETZ3950Missing'}->{en}=qq|
66
67 The Net::Z3950 module is missing.  This module is necessary if you want to use
68 Koha's Z39.50 client to download bibliographic records from other libraries.
69 To install this module, you will need the yaz client installed from
70 http://www.indexdata.dk/yaz/ and then you can install the perl module with the
71 command:
72
73 perl -MCPAN -e 'install Net::Z3950'
74
75 Press the <ENTER> key to continue: |;
76
77 $messages->{'CheckingPerlModules'}->{en}=qq|
78
79 ==================
80 = PERL & MODULES =
81 ==================
82
83 Checking perl modules ...
84 |;
85
86 $messages->{'PerlVersionFailure'}->{en}="Sorry, you need at least Perl %s\n";
87
88 $messages->{'MissingPerlModules'}->{en}=qq|
89
90 ========================
91 = MISSING PERL MODULES =
92 ========================
93
94 You are missing some Perl modules which are required by Koha.
95 Once these modules have been installed, rerun this installer.
96 They can be installed by running (as root) the following:
97
98 %s
99 |;
100 $messages->{'AllPerlModulesInstalled'}->{en}=qq|
101
102 ==============================
103 = ALL PERL MODULES INSTALLED =
104 ==============================
105
106 All mandatory perl modules are installed.
107
108 Press <ENTER> to continue: |;
109 $messages->{'KohaVersionInstalled'}->{en}="You currently have Koha %s on your system.";
110 $messages->{'KohaUnknownVersionInstalled'}->{en}="I am not able to determine what version of Koha is installed now.";
111 $messages->{'KohaAlreadyInstalled'}->{en}=qq|
112 ==========================
113 = Koha already installed =
114 ==========================
115
116 It looks like Koha is already installed on your system (/etc/koha.conf exists
117 already).  If you would like to upgrade your system to %s, please use
118 the koha.upgrade script in this directory.
119
120 %s
121
122 |;
123 $messages->{'GetOpacDir'}->{en}=qq|
124 ==================
125 = OPAC DIRECTORY =
126 ==================
127
128 Please supply the directory you want Koha to store its OPAC files in.  This
129 directory will be auto-created for you if it doesn't exist.
130
131 OPAC Directory [%s]: |;
132
133 $messages->{'GetIntranetDir'}->{en}=qq|
134 =================================
135 = INTRANET/LIBRARIANS DIRECTORY =
136 =================================
137
138 Please supply the directory you want Koha to store its Intranet/Librarians
139 files in.  This directory will be auto-created for you if it doesn't exist.
140
141 Intranet Directory [%s]: |;
142
143 $messages->{'GetKohaLogDir'}->{en}=qq|
144 ======================
145 = KOHA LOG DIRECTORY =
146 ======================
147
148 Specify a log directory where any Koha daemons can create log files.
149
150 Koha Log Directory [%s]: |;
151
152 $messages->{'AuthenticationWarning'}->{en}=qq|
153 ==================
154 = Authentication =
155 ==================
156
157 This release of Koha has a new authentication module.  If you are not already
158 using basic authentication on your intranet, you will be required to log in to
159 access some of the features of the intranet.  You can log in using the userid
160 and password from the /etc/koha.conf configuration file at any time.  Use the
161 "Members" module to add passwords for other accounts and set their permissions.
162
163 [NOTE PERMISSIONS ARE NOT COMPLETED AS OF THIS RELEASE.  Do not give passwords
164 to any patrons unless you want them to have full access to your intranet.]
165
166 Press the <ENTER> key to continue: |;
167
168 $messages->{'Completed'}->{en}=qq|
169 ==============================
170 = KOHA INSTALLATION COMPLETE =
171 ==============================
172
173 Congratulations ... your Koha installation is complete!
174
175 You will be able to connect to your Librarian interface at:
176
177    http://%s\:%s/
178
179 and the OPAC interface at :
180
181    http://%s\:%s/
182
183
184 Be sure to read the INSTALL, and Hints files. 
185
186 For more information visit http://www.koha.org
187
188 Press <ENTER> to exit the installer: |;
189
190 sub releasecandidatewarning {
191     my $message=getmessage('ReleaseCandidateWarning', [$::kohaversion, $::kohaversion]);
192     my $answer=showmessage($message, 'yn', 'n');
193
194     if ($answer =~ /y/i) {
195         print getmessage('continuing');
196     } else {
197         my $message=getmessage('WatchForReleaseAnnouncements');
198         print $message;
199         exit;
200     };
201 }
202
203
204 #
205 # Test for Perl and Modules
206 #
207 #
208 sub checkperlmodules {
209     my $message = getmessage('CheckingPerlModules');
210     showmessage($message, 'none');
211     
212     unless (eval "require 5.6.0") {
213         die getmessage('PerlVersionFailure', ['5.6.0']);
214     }
215
216     my @missing = ();
217     unless (eval {require DBI})               { push @missing,"DBI" };
218     unless (eval {require Date::Manip})       { push @missing,"Date::Manip" };
219     unless (eval {require DBD::mysql})        { push @missing,"DBD::mysql" };
220     unless (eval {require Set::Scalar})       { push @missing,"Set::Scalar" };
221     unless (eval {require Net::Z3950})        { 
222         my $message = getmessage('NETZ3950Missing');
223         showmessage($message, 'PressEnter', '', 1);
224         if ($#missing>=0) {
225             push @missing, "Net::Z3950";
226         }
227     }
228
229 #
230 # Print out a list of any missing modules
231 #
232
233     if (@missing > 0) {
234         my $missing='';
235         foreach my $module (@missing) {
236             $missing.="   perl -MCPAN -e 'install \"$module\"'\n";
237         }
238         my $message=getmessage('MissingPerlModules', [$missing]);
239         showmessage($message, 'none');
240         exit;
241     } else {
242         showmessage(getmessage('AllPerlModulesInstalled'), 'PressEnter', '', 1);
243     }
244
245
246 }
247
248
249 sub getmessage {
250     my $messagename=shift;
251     my $variables=shift;
252     my $message=$messages->{$messagename}->{$::language} || $messages->{$messagename}->{en} || "Error: No message named $messagename in Install.pm\n";
253     if (defined($variables)) {
254         $message=sprintf $message, @$variables;
255     }
256     return $message;
257 }
258
259
260 sub showmessage {
261     my $message=shift;
262     my $responsetype=shift;
263     my $defaultresponse=shift;
264     my $noclear=shift;
265     ($noclear) || (system('clear'));
266     if ($responsetype =~ /^yn$/) {
267         $responsetype='restrictchar yn';
268     }
269     print $message;
270     SWITCH: {
271         if ($responsetype =~/^restrictchar (.*)/i) {
272             my $response='\0';
273             my $options=$1;
274             until ($options=~/$response/) {
275                 ($defaultresponse) || ($defaultresponse=substr($options,0,1));
276                 $response=<STDIN>;
277                 chomp $response;
278                 (length($response)) || ($response=$defaultresponse);
279                 unless ($options=~/$response/) {
280                     ($noclear) || (system('clear'));
281                     print "Invalid Response.  Choose from [$options].\n\n";
282                     print $message;
283                 }
284             }
285             return $response;
286         }
287         if ($responsetype =~/^free$/i) {
288             (defined($defaultresponse)) || ($defaultresponse='');
289             my $response=<STDIN>;
290             chomp $response;
291             ($response) || ($response=$defaultresponse);
292             return $response;
293         }
294         if ($responsetype =~/^numerical$/i) {
295             (defined($defaultresponse)) || ($defaultresponse='');
296             my $response='';
297             until ($response=~/^\d+$/) {
298                 $response=<STDIN>;
299                 chomp $response;
300                 ($response) || ($response=$defaultresponse);
301                 unless ($response=~/^\d+$/) {
302                     ($noclear) || (system('clear'));
303                     print "Invalid Response ($response).  Response must be a number.\n\n";
304                     print $message;
305                 }
306             }
307             return $response;
308         }
309         if ($responsetype =~/^email$/i) {
310             (defined($defaultresponse)) || ($defaultresponse='');
311             my $response='';
312             until ($response=~/.*\@.*\..*/) {
313                 $response=<STDIN>;
314                 chomp $response;
315                 ($response) || ($response=$defaultresponse);
316                 unless ($response=~/.*\@.*\..*/) {
317                     ($noclear) || (system('clear'));
318                     print "Invalid Response ($response).  Response must be a valid email address.\n\n";
319                     print $message;
320                 }
321             }
322             return $response;
323         }
324         if ($responsetype =~/^PressEnter$/i) {
325             <STDIN>;
326             return;
327         }
328         if ($responsetype =~/^none$/i) {
329             return;
330         }
331     }
332 }
333
334 sub getinstallationdirectories {
335     $::opacdir = '/usr/local/koha/opac';
336     $::intranetdir = '/usr/local/koha/intranet';
337     my $getdirinfo=1;
338     while ($getdirinfo) {
339         # Loop until opac directory and koha directory are different
340         my $message=getmessage('GetOpacDir', [$::opacdir]);
341         $::opacdir=showmessage($message, 'free', $::opacdir);
342
343         $message=getmessage('GetIntranetDir', [$::intranetdir]);
344         $::intranetdir=showmessage($message, 'free', $::intranetdir);
345
346         if ($::intranetdir eq $::opacdir) {
347             print qq|
348
349 You must specify different directories for the OPAC and INTRANET files!
350  :: $::intranetdir :: $::opacdir ::
351 |;
352 <STDIN>
353         } else {
354             $getdirinfo=0;
355         }
356     }
357     $::kohalogdir='/var/log/koha';
358     my $message=getmessage('GetKohaLogDir', [$::kohalogdir]);
359     $::kohalogdir=showmessage($message, 'free', $::kohalogdir);
360
361
362     unless ( -d $::intranetdir ) {
363        my $result=mkdir ($::intranetdir, oct(770));
364        if ($result==0) {
365            my @dirs = split(m#/#, $::intranetdir);
366             my $checkdir='';
367             foreach (@dirs) {
368                 $checkdir.="$_/";
369                 unless (-e "$checkdir") {
370                     mkdir($checkdir, 0775);
371                 }
372             }
373        }
374        chown (oct(0), (getgrnam($::httpduser))[2], "$::intranetdir");
375        chmod (oct(770), "$::intranetdir");
376     }
377     unless ( -d "$::intranetdir/htdocs" ) {
378        mkdir ("$::intranetdir/htdocs", oct(750));
379     }
380     unless ( -d "$::intranetdir/cgi-bin" ) {
381        mkdir ("$::intranetdir/cgi-bin", oct(750));
382     }
383     unless ( -d "$::intranetdir/modules" ) {
384        mkdir ("$::intranetdir/modules", oct(750));
385     }
386     unless ( -d "$::intranetdir/scripts" ) {
387        mkdir ("$::intranetdir/scripts", oct(750));
388     }
389     unless ( -d $::opacdir ) {
390        my $result=mkdir ($::opacdir, oct(770));
391        if ($result==0) {
392            my @dirs = split(m#/#, $::opacdir);
393             my $checkdir='';
394             foreach (@dirs) {
395                 $checkdir.="$_/";
396                 unless (-e "$checkdir") {
397                     mkdir($checkdir, 0775);
398                 }
399             }
400        }
401        chown (oct(0), (getgrnam($::httpduser))[2], "$::opacdir");
402        chmod (oct(770), "$::opacdir");
403     }
404     unless ( -d "$::opacdir/htdocs" ) {
405        mkdir ("$::opacdir/htdocs", oct(750));
406     }
407     unless ( -d "$::opacdir/cgi-bin" ) {
408        mkdir ("$::opacdir/cgi-bin", oct(750));
409     }
410
411
412     unless ( -d $::kohalogdir ) {
413        my $result=mkdir ($::kohalogdir, oct(770));
414        if ($result==0) {
415            my @dirs = split(m#/#, $::kohalogdir);
416             my $checkdir='';
417             foreach (@dirs) {
418                 $checkdir.="$_/";
419                 unless (-e "$checkdir") {
420                     mkdir($checkdir, 0775);
421                 }
422             }
423        }
424
425        chown (oct(0), (getgrnam($::httpduser))[2,3], "$::kohalogdir");
426        chmod (oct(770), "$::kohalogdir");
427     }
428 }
429
430
431
432 $messages->{'DatabaseName'}->{en}=qq|
433 ==========================
434 = Name of MySQL database =
435 ==========================
436
437 Please provide the name of the mysql database for your koha installation.
438
439 Database name [%s]: |;
440
441 $messages->{'DatabaseHost'}->{en}=qq|
442 =================
443 = Database Host =
444 =================
445
446 Please provide the hostname for mysql.  Unless the database is located on
447 another machine this will be "localhost".
448
449 Database host [%s]: |;
450
451 $messages->{'DatabaseUser'}->{en}=qq|
452 =================
453 = Database User =
454 =================
455
456 Please provide the name of the user, who will have full administrative rights
457 to the %s database, when authenticating from %s.
458
459 Database user [%s]: |;
460
461 $messages->{'DatabasePassword'}->{en}=qq|
462 =====================
463 = Database Password =
464 =====================
465
466 Please provide a good password for the user %s.
467
468 Database Password: |;
469
470 $messages->{'BlankPassword'}->{en}=qq|
471 ==================
472 = BLANK PASSWORD =
473 ==================
474
475 You must not use a blank password for your MySQL user!
476
477 Press <ENTER> to try again: 
478 |;
479
480 sub getdatabaseinfo {
481
482     $::dbname = 'Koha';
483     $::hostname = 'localhost';
484     $::user = 'kohaadmin';
485     $::pass = '';
486
487 #Get the database name
488
489     my $message=getmessage('DatabaseName', [$::dbname]);
490     $::dbname=showmessage($message, 'free', $::dbname);
491
492 #Get the hostname for the database
493     
494     $message=getmessage('DatabaseHost', [$::hostname]);
495     $::hostname=showmessage($message, 'free', $::hostname);
496
497 #Get the username for the database
498
499     $message=getmessage('DatabaseUser', [$::dbname, $::hostname, $::user]);
500     $::user=showmessage($message, 'free', $::user);
501
502 #Get the password for the database user
503
504     while ($::pass eq '') {
505         my $message=getmessage('DatabasePassword', [$::user]);
506         $::pass=showmessage($message, 'free', $::pass);
507         if ($::pass eq '') {
508             my $message=getmessage('BlankPassword');
509             showmessage($message,'PressEnter');
510         }
511     }
512 }
513
514
515
516 $messages->{'FoundMultipleApacheConfFiles'}->{en}=qq|
517 ================================
518 = MULTIPLE APACHE CONFIG FILES =
519 ================================
520
521 I found more than one possible Apache configuration file:
522
523 %s
524
525 Choose the correct file [1]: |;
526
527 $messages->{'NoApacheConfFiles'}->{en}=qq|
528 ===============================
529 = NO APACHE CONFIG FILE FOUND =
530 ===============================
531
532 I was not able to find your Apache configuration file.
533
534 The file is usually called httpd.conf or apache.conf.
535
536 Please specify the location of your config file: |;
537
538 $messages->{'NotAFile'}->{en}=qq|
539 =======================
540 = FILE DOES NOT EXIST =
541 =======================
542
543 The file %s does not exist.
544
545 Please press <ENTER> to continue: |;
546
547 $messages->{'EnterApacheUser'}->{en}=qq|
548 ====================
549 = NEED APACHE USER =
550 ====================
551
552 I was not able to determine the user that Apache is running as.  This
553 information is necessary in order to set the access privileges correctly on
554 /etc/koha.conf.  This user should be set in one of the Apache configuration
555 files using the "User" directive.
556
557 Enter the Apache userid: |;
558
559 $messages->{'InvalidUserid'}->{en}=qq|
560 ==================
561 = INVALID USERID =
562 ==================
563
564 The userid %s is not a valid userid on this system.
565
566 Press <ENTER> to continue: |;
567
568 sub getapacheinfo {
569     my @confpossibilities;
570
571     foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
572                           /usr/local/etc/apache/httpd.conf
573                           /usr/local/etc/apache/apache.conf
574                           /var/www/conf/httpd.conf
575                           /etc/apache/conf/httpd.conf
576                           /etc/apache/conf/apache.conf
577                           /etc/apache-ssl/conf/apache.conf
578                           /etc/httpd/conf/httpd.conf
579                           /etc/httpd/httpd.conf)) {
580         if ( -f $httpdconf ) {
581             push @confpossibilities, $httpdconf;
582         }
583     }
584
585     if ($#confpossibilities==-1) {
586         my $message=getmessage('NoApacheConfFiles');
587         my $choice='';
588         until (-f $choice) {
589             $choice=showmessage($message, "free", 1);
590             unless (-f $choice) {
591                 showmessage(getmessage('NotAFile', [$choice]),'PressEnter', '', 1);
592             }
593         }
594     } elsif ($#confpossibilities>0) {
595         my $conffiles='';
596         my $counter=1;
597         my $options='';
598         foreach (@confpossibilities) {
599             $conffiles.="   $counter: $_\n";
600             $options.="$counter";
601             $counter++;
602         }
603         my $message=getmessage('FoundMultipleApacheConfFiles', [$conffiles]);
604         my $choice=showmessage($message, "restrictchar $options", 1);
605         $::realhttpdconf=$confpossibilities[$choice-1];
606     } else {
607         $::realhttpdconf=$confpossibilities[0];
608     }
609     open (HTTPDCONF, $::realhttpdconf) or warn "Insufficient privileges to open $::realhttpdconf for reading.\n";
610     while (<HTTPDCONF>) {
611         if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
612             $::httpduser = $1;
613         }
614     }
615     close(HTTPDCONF);
616
617
618
619
620     unless ($::httpduser) {
621         my $message=getmessage('EnterApacheUser');
622         until (length($::httpduser) && getpwnam($::httpduser)) {
623             $::httpduser=showmessage($message, "free", '');
624             if (length($::httpduser)>0) {
625                 unless (getpwnam($::httpduser)) {
626                     my $message=getmessage('InvalidUserid', [$::httpduser]);
627                     showmessage($message,'PressEnter');
628                 }
629             } else {
630             }
631         }
632         print "AU: $::httpduser\n";
633     }
634 }
635
636
637 $messages->{'ApacheConfigIntroduction'}->{en}=qq|
638 ========================
639 = APACHE CONFIGURATION =
640 ========================
641
642 Koha needs to setup your Apache configuration file for the
643 OPAC and LIBRARIAN virtual hosts.  By default this installer
644 will do this by using one ip address and two different ports
645 for the virtual hosts.  There are other ways to set this up,
646 and the installer will leave comments in httpd.conf detailing
647 what these other options are.
648
649
650 Press <ENTER> to continue: |;
651
652 $messages->{'GetVirtualHostEmail'}->{en}=qq|
653 =============================
654 = WEB SERVER E-MAIL CONTACT =
655 =============================
656
657 Enter the e-mail address to be used as a contact for the virtual hosts (this
658 address is displayed if any errors are encountered).
659
660 E-mail contact [%s]: |;
661
662 $messages->{'GetServerName'}->{en}=qq|
663 ======================================
664 = WEB SERVER HOST NAME OR IP ADDRESS =
665 ======================================
666
667 Please enter the domain name or ip address of your computer.
668
669 Host name or IP Address [%s]: |;
670
671 $messages->{'GetOpacPort'}->{en}=qq|
672 ==========================
673 = OPAC VIRTUAL HOST PORT =
674 ==========================
675
676 Please enter the port for your OPAC interface.  This defaults to port 80, but
677 if you are already serving web content from this server, you should change it
678 to a different port (8000 might be a good choice).
679
680 Enter the OPAC Port [%s]: |;
681
682 $messages->{'GetIntranetPort'}->{en}=qq|
683 ==============================
684 = INTRANET VIRTUAL HOST PORT =
685 ==============================
686
687 Please enter the port for your Intranet interface.  This must be different from
688 the OPAC port (%s).
689
690 Enter the Intranet Port [%s]: |;
691
692
693 sub getapachevhostinfo {
694
695     $::svr_admin = "webmaster\@$::domainname";
696     $::servername=`hostname -f`;
697     chomp $::servername;
698     $::opacport=80;
699     $::intranetport=8080;
700
701     showmessage(getmessage('ApacheConfigIntroduction'), 'PressEnter');
702
703     $::svr_admin=showmessage(getmessage('GetVirtualHostEmail', [$::svr_admin]), 'email', $::svr_admin);
704     $::servername=showmessage(getmessage('GetServerName', [$::servername]), 'free', $::servername);
705
706
707     $::opacport=showmessage(getmessage('GetOpacPort', [$::opacport]), 'numerical', $::opacport);
708     $::intranetport=showmessage(getmessage('GetIntranetPort', [$::opacport, $::intranetport]), 'numerical', $::intranetport);
709
710 }
711
712 $messages->{'StartUpdateApache'}->{en}=qq|
713 =================================
714 = UPDATING APACHE CONFIGURATION =
715 =================================
716
717 Checking for modules that need to be loaded...
718 |;
719
720 $messages->{'LoadingApacheModuleModEnv'}->{en}="Loading SetEnv Apache module.\n";
721
722 $messages->{'LoadingApacheModuleModInc'}->{en}="Loading Includes Apache module.\n";
723
724 $messages->{'ApacheConfigBackupFailed'}->{en}=qq|
725 ======================================
726 = APACHE CONFIGURATION BACKUP FAILED =
727 ======================================
728
729 An error occurred while trying to make a backup copy of %s.
730
731   %s
732
733 No changes will be made to the apache configuration file at this time.
734
735 Press <ENTER> to continue: |;
736
737
738 $messages->{'ApacheAlreadyConfigured'}->{en}=qq|
739 =============================
740 = APACHE ALREADY CONFIGURED =
741 =============================
742
743 %s appears to already have an entry for Koha
744 Virtual Hosts.  You may need to edit %s
745 f anything has changed since it was last set up.  This
746 script will not attempt to modify an existing Koha apache
747 configuration.
748
749 Press <ENTER> to continue: |;
750
751 sub updateapacheconf {
752     my $logfiledir=`grep ^ErrorLog $::realhttpdconf`;
753     chomp $logfiledir;
754
755     if ($logfiledir) {
756         $logfiledir=~m#ErrorLog (.*)/[^/]*$#;
757         $logfiledir=$1;
758     }
759
760     unless ($logfiledir) {
761         $logfiledir='logs';
762     }
763
764     showmessage(getmessage('StartUpdateApache'), 'none');
765
766     my $httpdconf;
767     my $envmodule=0;
768     my $includesmodule=0;
769     open HC, $::realhttpdconf;
770     while (<HC>) {
771         if (/^\s*#\s*LoadModule env_module /) {
772             s/^\s*#\s*//;
773             showmessage(getmessage('LoadingApacheModuleModEnv'));
774             $envmodule=1;
775         }
776         if (/^\s*#\s*LoadModule includes_module /) {
777             s/^\s*#\s*//;
778             showmessage(getmessage('LoadingApacheModuleModInc'));
779         }
780         if (/\s*LoadModule includes_module / ) {
781             $includesmodule=1;
782         }
783         $httpdconf.=$_;
784     }
785
786     my $backupfailed=0;
787     $backupfailed=`cp -f $::realhttpdconf $::realhttpdconf\.prekoha`;
788     if ($backupfailed) {
789         showmessage(getmessage('ApacheConfigBackupFailed', [$::realhttpdconf,$backupfailed ]), 'PressEnter');
790         return;
791     }
792
793     if ($envmodule || $includesmodule) {
794         open HC, ">$::realhttpdconf";
795         print HC $httpdconf;
796         close HC;
797     }
798
799
800     
801     if (`grep 'VirtualHost $::servername' $::realhttpdconf`) {
802         showmessage(getmessage('ApacheAlreadyConfigured', [$::realhttpdconf, $::realhttpdconf]), 'PressEnter');
803         return;
804     } else {
805         my $includesdirectives='';
806         if ($includesmodule) {
807             $includesdirectives.="Options +Includes\n";
808             $includesdirectives.="   AddHandler server-parsed .html\n";
809         }
810         open(SITE,">>$::realhttpdconf") or warn "Insufficient priveleges to open $::realhttpdconf for writing.\n";
811         my $opaclisten = '';
812         if ($::opacport != 80) {
813             $opaclisten="Listen $::opacport";
814         }
815         my $intranetlisten = '';
816         if ($::intranetport != 80) {
817             $intranetlisten="Listen $::intranetport";
818         }
819         print SITE <<EOP
820
821 # Ports to listen to for Koha
822 $opaclisten
823 $intranetlisten
824
825 # NameVirtualHost is used by one of the optional configurations detailed below
826
827 #NameVirtualHost 11.22.33.44
828
829 # KOHA's OPAC Configuration
830 <VirtualHost $::servername\:$::opacport>
831    ServerAdmin $::svr_admin
832    DocumentRoot $::opacdir/htdocs
833    ServerName $::servername
834    ScriptAlias /cgi-bin/koha/ $::opacdir/cgi-bin/
835    ErrorLog $logfiledir/opac-error_log
836    TransferLog $logfiledir/opac-access_log
837    SetEnv PERL5LIB "$::intranetdir/modules"
838    $includesdirectives
839 </VirtualHost>
840
841 # KOHA's INTRANET Configuration
842 <VirtualHost $::servername\:$::intranetport>
843    ServerAdmin $::svr_admin
844    DocumentRoot $::intranetdir/htdocs
845    ServerName $::servername
846    ScriptAlias /cgi-bin/koha/ "$::intranetdir/cgi-bin/"
847    ErrorLog $logfiledir/koha-error_log
848    TransferLog $logfiledir/koha-access_log
849    SetEnv PERL5LIB "$::intranetdir/modules"
850    $includesdirectives
851 </VirtualHost>
852
853 # If you want to use name based Virtual Hosting:
854 #   1. remove the two Listen lines
855 #   2. replace $::servername\:$::opacport wih your.opac.domain.name
856 #   3. replace ServerName $::servername wih ServerName your.opac.domain.name
857 #   4. replace $::servername\:$::intranetport wih your intranet domain name
858 #   5. replace ServerName $::servername wih ServerName your.intranet.domain.name
859 #
860 # If you want to use NameVirtualHost'ing (using two names on one ip address):
861 #   1.  Follow steps 1-5 above
862 #   2.  Uncomment the NameVirtualHost line and set the correct ip address
863
864 EOP
865
866
867     }
868 }
869
870 $messages->{'IntranetAuthenticationQuestion'}->{en}=qq|
871 ===========================
872 = INTRANET AUTHENTICATION =
873 ===========================
874
875 I can set it up so that the Intranet/Librarian site is password protected using
876 Apache's Basic Authorization.
877
878 Would you like to do this ([Y]/N): |;
879
880 $messages->{'BasicAuthUsername'}->{en}="Please enter a userid for intranet access [%s]: ";
881 $messages->{'BasicAuthPassword'}->{en}="Please enter a password for %s: ";
882 $messages->{'BasicAuthPasswordWasBlank'}->{en}="\nYou cannot use a blank password!\n\n";
883
884 sub basicauthentication {
885     my $message=getmessage('IntranetAuthenticationQuestion');
886     my $answer=showmessage($message, 'yn', 'y');
887
888     my $apacheauthusername='librarian';
889     my $apacheauthpassword='';
890     if ($answer=~/^y/i) {
891         ($apacheauthusername) = showmessage(getmessage('BasicAuthUsername', [ $apacheauthusername]), 'free', $apacheauthusername, 1);
892         $apacheauthusername=~s/[^a-zA-Z0-9]//g;
893         while (! $apacheauthpassword) {
894             ($apacheauthpassword) = showmessage(getmessage('BasicAuthPassword', [ $apacheauthusername]), 'free', 1);
895             if (!$apacheauthpassword) {
896                 ($apacheauthpassword) = showmessage(getmessage('BasicAuthPasswordWasBlank'), 'none', '', 1);
897             }
898         }
899         open AUTH, ">/etc/kohaintranet.pass";
900         my $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
901         my $salt=substr($chars, int(rand(length($chars))),1);
902         $salt.=substr($chars, int(rand(length($chars))),1);
903         print AUTH $apacheauthusername.":".crypt($apacheauthpassword, $salt)."\n";
904         close AUTH;
905         open(SITE,">>$::realhttpdconf") or warn "Insufficient priveleges to open $::realhttpdconf for writing.\n";
906         print SITE <<EOP
907
908 <Directory $::intranetdir>
909     AuthUserFile /etc/kohaintranet.pass
910     AuthType Basic
911     AuthName "Koha Intranet (for librarians only)"
912     Require  valid-user
913 </Directory>
914 EOP
915     }
916     close(SITE);
917 }
918
919 $messages->{'InstallFiles'}->{en}=qq|
920 ====================
921 = INSTALLING FILES =
922 ====================
923
924 Copying files to installation directories:
925
926 |;
927
928
929 $messages->{'CopyingFiles'}->{en}="Copying %s to %s.\n";
930
931
932
933 sub installfiles {
934
935
936     showmessage(getmessage('InstallFiles'),'none');
937     print getmessage('CopyingFiles', ['internet-html', "$::intranetdir/htdocs" ]);
938     system("cp -R intranet-html/* $::intranetdir/htdocs/");
939     print getmessage('CopyingFiles', ['internet-cgi', "$::intranetdir/cgi-bin" ]);
940     system("cp -R intranet-cgi/* $::intranetdir/cgi-bin/");
941     print getmessage('CopyingFiles', ['stand-alone scripts', "$::intranetdir/scripts" ]);
942     system("cp -R scripts/* $::intranetdir/scripts/");
943     print getmessage('CopyingFiles', ['perl modules', "$::intranetdir/modules" ]);
944     system("cp -R modules/* $::intranetdir/modules/");
945     print getmessage('CopyingFiles', ['opac-html', "$::opacdir/htdocs" ]);
946     system("cp -R opac-html/* $::opacdir/htdocs/");
947     print getmessage('CopyingFiles', ['opac-cgi', "$::opacdir/cgi-bin" ]);
948     system("cp -R opac-cgi/* $::opacdir/cgi-bin/");
949
950     system("chown -R root.$::httpduser $::opacdir");
951     system("chown -R root.$::httpduser $::intranetdir");
952
953     # Create /etc/koha.conf
954
955     open(KC,">$::etcdir/koha.conf") or warn "Couldn't create file
956     at $::etcdir.  Must have write capability.\n";
957     print KC qq|
958 database=$::dbname
959 hostname=$::hostname
960 user=$::user
961 pass=$::pass
962 includes=$::intranetdir/htdocs/includes
963 intranetdir=$::intranetdir
964 opacdir=$::opacdir
965 kohalogdir=$::kohalogdir
966 kohaversion=$::kohaversion
967 httpduser=$::httpduser
968 |;
969     close(KC);
970
971     chown((getpwnam($::httpduser)) [2,3], "$::etcdir/koha.conf") or warn "can't chown koha.conf: $!";
972     chmod 0440, "$::etcdir/koha.conf";
973
974     chmod 0750, "$::intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
975     chmod 0750, "$::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
976     chmod 0750, "$::intranetdir/scripts/z3950daemon/processz3950queue";
977     chown(0, (getpwnam($::httpduser)) [3], "$::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh") or warn "can't chown $::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh: $!";
978     chown(0, (getpwnam($::httpduser)) [3], "$::intranetdir/scripts/z3950daemon/processz3950queue") or warn "can't chown $::intranetdir/scripts/z3950daemon/processz3950queue: $!";
979
980 }
981
982 $messages->{'MysqlRootPassword'}->{en}=qq|
983 ============================
984 = MYSQL ROOT USER PASSWORD =
985 ============================
986
987 To allow us to create the koha database please supply your
988 mysql server's root user password:
989
990 Enter MySql root user password: |;
991
992 $messages->{'InvalidMysqlRootPassword'}->{en}="Invalid Password.  Please try again.";
993
994 $messages->{'CreatingDatabase'}->{en}=qq|
995 =====================
996 = CREATING DATABASE =
997 =====================
998
999 Creating the MySql database for Koha...
1000
1001 |;
1002
1003 $messages->{'CreatingDatabaseError'}->{en}=qq|
1004 ===========================
1005 = ERROR CREATING DATABASE =
1006 ===========================
1007
1008 Couldn't connect to the MySQL server for the reason given above.
1009 This is a serious problem, the database will not get installed.\a
1010
1011 Press <ENTER> to continue: |;
1012
1013 $messages->{'SampleData'}->{en}=qq|
1014 ===============
1015 = SAMPLE DATA =
1016 ===============
1017
1018 If you are installing Koha for evaluation purposes,  I have a batch of sample
1019 data that you can install now.
1020
1021 If you are installing Koha with the intention of populating it with your own
1022 data, you probably don't want this sample data installed.
1023
1024 Would you like to install the sample data? Y/[N]: |;
1025
1026 $messages->{'SampleDataInstalled'}->{en}=qq|
1027 =========================
1028 = SAMPLE DATA INSTALLED =
1029 =========================
1030
1031 Sample data has been installed.  For some suggestions on testing Koha, please
1032 read the file doc/HOWTO-Testing.  If you find any bugs, please submit them at
1033 http://bugs.koha.org/.  If you need help with testing Koha, you can post a
1034 question through the koha-devel mailing list, or you can check for a developer
1035 online at +irc.katipo.co.nz:6667 channel #koha.
1036
1037 You can find instructions for subscribing to the Koha mailing lists at:
1038
1039     http://www.koha.org
1040
1041
1042 Press <ENTER> to continue: |;
1043
1044 $messages->{'AddBranchPrinter'}->{en}=qq|
1045 ==========================
1046 = Add Branch and Printer =
1047 ==========================
1048
1049 Would you like to install an initial branch and printer? [Y]/N: |;
1050
1051 $messages->{'BranchName'}->{en}="Branch Name [%s]: ";
1052 $messages->{'BranchCode'}->{en}="Branch Code (4 letters or numbers) [%s]: ";
1053 $messages->{'PrinterQueue'}->{en}="Printer Queue [%s]: ";
1054 $messages->{'PrinterName'}->{en}="Printer Name [%s]: ";
1055
1056 sub databasesetup {
1057     my $mysqldir;
1058     $::mysqluser = 'root';
1059     $::mysqlpass = '';
1060
1061     foreach my $mysql (qw(/usr/local/mysql
1062                           /opt/mysql
1063                           )) {
1064        if ( -d $mysql ) {
1065                 $mysqldir=$mysql;
1066        }
1067     }
1068     if (!$mysqldir){
1069         $mysqldir='/usr';
1070     }
1071
1072
1073     my $needpassword=1;
1074     while ($needpassword) {
1075         $::mysqlpass=showmessage(getmessage('MysqlRootPassword'), 'free');
1076         my $result=system("$mysqldir/bin/mysqladmin -u$::mysqluser -p$::mysqlpass proc > /dev/null 2>&1");
1077         if ($result) {
1078             print getmessage('InvalidMysqlRootPassword');
1079         } else {
1080             $needpassword=0;
1081         }
1082     }
1083
1084     showmessage(getmessage('CreatingDatabase'),'none');
1085
1086     my $result=system("$mysqldir/bin/mysqladmin -u$::mysqluser -p$::mysqlpass create $::dbname");
1087     if ($result) {
1088         showmessage(getmessage('CreatingDatabaseError'),'PressEnter', '', 1);
1089     } else {
1090         system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname < koha.mysql");
1091         system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass mysql -e \"insert into user (Host,User,Password) values ('$::hostname','$::user',password('$::pass'))\"\;");
1092         system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass mysql -e \"insert into db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv, index_priv, alter_priv) values ('%','$::dbname','$::user','Y','Y','Y','Y','Y','Y','Y','Y')\"");
1093         system("$mysqldir/bin/mysqladmin -u$::mysqluser -p$::mysqlpass reload");
1094
1095         system ("perl -I $::intranetdir/modules scripts/updater/updatedatabase");
1096
1097
1098         my $response=showmessage(getmessage('SampleData'), 'yn', 'n');
1099         if ($response =~/^y/i) {
1100             system("gunzip sampledata-1.2.gz");
1101             system("cat sampledata-1.2 | $mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname");
1102             system("gzip -9 sampledata-1.2");
1103             system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname -e \"insert into branches (branchcode,branchname,issuing) values ('MAIN', 'Main Library', 1)\"");
1104             system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname -e \"insert into printers (printername,printqueue,printtype) values ('Circulation Desk Printer', 'lp', 'hp')\"");
1105             showmessage(getmessage('SampleDataInstalled'), 'PressEnter');
1106         } else {
1107             my $input;
1108             my $response=showmessage(getmessage('AddBranchPrinter'), 'yn', 'y');
1109
1110             unless ($response =~/^n/i) {
1111                 my $branch='Main Library';
1112                 print "Enter a name for the library branch [$branch]: ";
1113                 $branch=showmessage(getmessage('BranchName', [$branch]), 'free', $branch, 1);
1114                 $branch=~s/[^A-Za-z0-9\s]//g;
1115
1116                 my $branchcode=$branch;
1117                 $branchcode=~s/[^A-Za-z0-9]//g;
1118                 $branchcode=uc($branchcode);
1119                 $branchcode=substr($branchcode,0,4);
1120                 $branchcode=showmessage(getmessage('BranchCode', [$branchcode]), 'free', $branchcode, 1);
1121                 $branchcode=~s/[^A-Z]//g;
1122                 $branchcode=uc($branchcode);
1123                 $branchcode=substr($branchcode,0,4);
1124
1125                 system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname -e \"insert into branches (branchcode,branchname,issuing) values ('$branchcode', '$branch', 1)\"");
1126
1127                 my $printername='Library Printer';
1128                 $printername=showmessage(getmessage('PrinterName', [$printername]), 'free', $printername, 1);
1129                 $printername=~s/[^A-Za-z0-9\s]//g;
1130
1131                 my $printerqueue='lp';
1132                 $printerqueue=showmessage(getmessage('PrinterQueue', [$printerqueue]), 'free', $printerqueue, 1);
1133                 $printerqueue=~s/[^A-Za-z0-9]//g;
1134                 system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname -e \"insert into printers (printername,printqueue,printtype) values ('$printername', '$printerqueue', '')\"");
1135
1136             }
1137         }
1138
1139
1140     }
1141
1142 }
1143
1144 $messages->{'RestartApache'}->{en}=qq|
1145 ==================
1146 = RESTART APACHE =
1147 ==================
1148
1149 Apache needs to be restarted to load the new configuration for Koha.
1150
1151 Would you like to restart Apache now?  [Y]/N: |;
1152
1153 sub restartapache {
1154
1155     my $response=showmessage(getmessage('RestartApache'), 'yn', 'y');
1156
1157
1158
1159     unless ($response=~/^n/i) {
1160         # Need to support other init structures here?
1161         if (-e "/etc/rc.d/init.d/httpd") {
1162             system('/etc/rc.d/init.d/httpd restart');
1163         } elsif (-e "/etc/init.d/apache") {
1164             system('/etc//init.d/apache restart');
1165         } elsif (-e "/etc/init.d/apache-ssl") {
1166             system('/etc/init.d/apache-ssl restart');
1167         }
1168     }
1169
1170 }
1171
1172 sub loadconfigfile {
1173     my %configfile;
1174
1175     open (KC, "/etc/koha.conf");
1176     while (<KC>) {
1177      chomp;
1178      (next) if (/^\s*#/);
1179      if (/(.*)\s*=\s*(.*)/) {
1180        my $variable=$1;
1181        my $value=$2;
1182        # Clean up white space at beginning and end
1183        $variable=~s/^\s*//g;
1184        $variable=~s/\s*$//g;
1185        $value=~s/^\s*//g;
1186        $value=~s/\s*$//g;
1187        $configfile{$variable}=$value;
1188      }
1189     }
1190
1191     $::intranetdir=$configfile{'intranetdir'};
1192     $::opacdir=$configfile{'opacdir'};
1193     $::kohaversion=$configfile{'kohaversion'};
1194     $::kohalogdir=$configfile{'kohalogdir'};
1195     $::database=$configfile{'database'};
1196     $::hostname=$configfile{'hostname'};
1197     $::user=$configfile{'user'};
1198     $::pass=$configfile{'pass'};
1199 }
1200
1201 END { }       # module clean-up code here (global destructor)
1202