3 #This program is free software; you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation; either version 2 of the License, or
6 #(at your option) any later version.
9 # authors: Amalu Obinna <amaluobinna@aol.com>
10 # Brady Miller <brady@sparmy.com>
12 # Debian package post installation script steps:
13 # 1) Collect setting from package configuration file
14 # 2) Install or Upgrade
16 # a) Collect the MySQL root password (if possible)
17 # b) Ensure OpenEMR MySQL database and user do not exist. (if applicable)
18 # c) Configure OpenEMR (if applicable)
22 # a) Modify new OpenEMR version configuration files
23 # b) Upgrade MySQL database(s)
24 # c) Upgrade Access Controls
25 # d) Copy over old configuration files
26 # (Copy to files with .OLD extension to allow manual comparisons by user)
27 # 3) Modify permissions for writable directories
28 # 4) Secure the php installation/upgrading scripts (if applicable)
29 # 5) Modify the package configuration file
30 # 6) Display instructions on starting openemr
32 # summary of how this script can be called:
33 # * <postinst> `configure' <most-recently-configured-version>
34 # * <old-postinst> `abort-upgrade' <new version>
35 # * <conflictor's-postinst> `abort-remove' `in-favour' <package>
37 # * <postinst> `abort-remove'
38 # * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
39 # <failed-install-package> <version> `removing'
40 # <conflicting-package> <version>
41 # for details, see http://www.debian.org/doc/debian-policy/ or
42 # the debian-policy package
44 # Source debconf library.
45 .
/usr
/share
/debconf
/confmodule
51 LOGDIR
=/var
/log
/openemr
53 CONFIGDIR
=/etc
/openemr
54 CONFIG
=$CONFIGDIR/openemr.conf
55 TMPDIR
=/tmp
/openemr-tmp
58 SITEDIR
=$OPENEMR/sites
59 #hardcoded mysql user and database for install (not pertinent for upgrading)
60 # upgrading can use whatever is found in openemr/library/sqlconf.php
62 INSTALL_DATABASE
=openemr
64 INST
=$OPENEMR/contrib
/util
/installScripts
/InstallerAuto.php
65 INSTTEMP
=$OPENEMR/contrib
/util
/installScripts
/InstallerAutoTemp.php
67 PHP
=/etc
/php
5/apache
2/php.ini
72 #Standardized echo function to send to only log file
73 # requires one parameter (string)
75 echo "`date`: $1" >> $LOG
78 #Standardized exit functions to be used
79 # requires one parameter (string with reason for exiting)
81 echo "`date`: $1" >> $LOG
82 echo "`date`: EXITING.........." >> $LOG
86 #function to check mysql for selected databases
87 # 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
100 if [ "`mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^
$2$
"`" == "$2" ]; then
107 #function to collect variables from config files
108 # 1st param is variable name, 2nd param is filename
110 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
113 #function to insert variables into config files
114 # 1st param is variable name, 2nd param is variable, 3rd param is filename
116 sed -i 's@^[ ]*'"$1"'[ =].*$@'"$1"' = '"$2"'@' "$3"
119 #function to prompt for input
120 # 1st param is name, 2nd param is priority, 3rd param is where result gets sent back in
124 db_fset
"$1" seen false
125 db_input
"$2" "$1" || true
128 local input_value
="$RET"
130 db_fset
"$1" seen false
132 eval $__result="'$input_value'"
135 #collect scripting information from config file
136 PROCESS
=$
(collect_var process
$CONFIG)
137 PLAN
=$
(collect_var plan
$CONFIG)
139 #Don't allow re-configuration
140 if [ "$PROCESS" == "complete" ] ; then
141 unable_exit
"OpenEMR has already been configured."
142 elif [ "$PROCESS" == "pending" ] ; then
143 #continue with configuration
144 log_only
"Configuring package..."
146 unable_exit
"Error reading process variable in configuration file."
149 if [ "$PLAN" == "upgrade" ] ; then
150 #continue with upgrade
151 OLD_VERSION
=$
(collect_var previous_version
$CONFIG)
152 log_only
"Continuing Upgrade from ($OLD_VERSION)"
154 #go to openemr directory
157 # NEED TO CONFIGURE APACHE BEFORE CONFIGURATION SINCE ZEND SUPPORT NEEDS PROPER CONFIGURATION
158 # Activate the OpenEMR conf file for apache
159 log_only
"Activate OpenEMR config file for Apache"
160 a2ensite openemr.conf
161 # Ensure the apache rewrite module is turned on
163 # Restart the apache server
164 log_only
"Restarting Apache service..."
165 invoke-rc.d apache2 restart
>> $LOG 2>&1
167 #To support the multisite module, go through each site
168 for dir
in $
(find $SITEDIR/* -maxdepth 0 -type d
); do
170 SITENAME
=$
(basename "$dir")
171 log_only
"Configuring Site ($SITENAME)"
173 #collect more information from config file
174 SQLLOCATION
=$
(collect_var
${SITENAME}_sqllocation
$CONFIG)
175 SQLUSER
=$
(collect_var
${SITENAME}_sqluser
$CONFIG)
176 SQLPASSWORD
=$
(collect_var
${SITENAME}_sqlpassword
$CONFIG)
177 SQLDATABASE
=$
(collect_var
${SITENAME}_sqldatabase
$CONFIG)
178 SQLUTFFLAG
=$
(collect_var
${SITENAME}_sqlutfflag
$CONFIG)
180 #configure database configuration file
181 insert_var
"\$host" "\'$SQLLOCATION\';" $SITEDIR/$SITENAME/sqlconf.php
182 insert_var
"\$login" "\'$SQLUSER\';" $SITEDIR/$SITENAME/sqlconf.php
183 insert_var
"\$pass" "\'$SQLPASSWORD\';" $SITEDIR/$SITENAME/sqlconf.php
184 insert_var
"\$dbase" "\'$SQLDATABASE\';" $SITEDIR/$SITENAME/sqlconf.php
185 insert_var
"\$disable_utf8_flag" "$SQLUTFFLAG;" $SITEDIR/$SITENAME/sqlconf.php
186 sed -i "s/^[ ]*\$config[ =].*0/\$config = 1/" $SITEDIR/$SITENAME/sqlconf.php
188 #upgrade the sql database
189 CONC_VERSION
=$
(echo $OLD_VERSION | cut
-d \
- -f 1)
190 echo "<?php \$_GET['site'] = '$SITENAME'; ?>" > $OPENEMR/TEMPsql_upgrade.php
191 cat $OPENEMR/sql_upgrade.php
>> $OPENEMR/TEMPsql_upgrade.php
192 sed -i "/input type='submit'/d" $OPENEMR/TEMPsql_upgrade.php
193 sed -i "s/!empty(\$_POST\['form_submit'\])/empty(\$_POST\['form_submit'\])/" $OPENEMR/TEMPsql_upgrade.php
194 sed -i "s/^[ ]*\$form_old_version[ =].*$/\$form_old_version = \"$CONC_VERSION\";/" $OPENEMR/TEMPsql_upgrade.php
195 php
-f $OPENEMR/TEMPsql_upgrade.php
>> $LOG 2>&1
196 rm -f $OPENEMR/TEMPsql_upgrade.php
198 #copy the old config file into new with the OLD at end to allow manual configuration of old
200 if [ -d "$TMPDIR/openemr_web_$OLD_VERSION/sites/$SITENAME" ]; then
201 cp -f "$TMPDIR/openemr_web_$OLD_VERSION/sites/$SITENAME/config.php" "$SITEDIR/$SITENAME/config.php.OLD"
203 #need to move from old location, so sitename will always be default in this case
204 cp -f $TMPDIR/openemr_web_
$OLD_VERSION/includes
/config.php
$SITEDIR/default
/config.php.OLD
208 log_only
"Upgraded OpenEMR site ($SITENAME) with sql database ($SQLDATABASE) and sql user ($SQLUSER)."
212 # if site-specific directories are in the old locations, move them.
213 if [ -d $OPENEMR/documents
]; then
214 if [ "$(ls $OPENEMR/documents)" ]; then
215 mv -f $OPENEMR/documents
/* $SITEDIR/default
/documents
/
217 rm -rf $OPENEMR/documents
219 if [ -d $OPENEMR/era
]; then
220 if [ "$(ls $OPENEMR/era)" ]; then
221 mv -f $OPENEMR/era
/* $SITEDIR/default
/era
/
225 if [ -d $OPENEMR/edi
]; then
226 if [ "$(ls $OPENEMR/edi)" ]; then
227 mv -f $OPENEMR/edi
/* $SITEDIR/default
/edi
/
231 if [ -d $OPENEMR/custom
/letter_templates
]; then
232 if [ "$(ls $OPENEMR/custom/letter_templates)" ]; then
233 mv -f $OPENEMR/custom
/letter_templates
/* $SITEDIR/default
/letter_templates
/
235 rm -rf $OPENEMR/custom
/letter_templates
239 chown
-Rf root
:root
$OPENEMR
240 chmod 600 $OPENEMR/acl_setup.php
241 chmod 600 $OPENEMR/acl_upgrade.php
242 chmod 600 $OPENEMR/setup.php
243 chmod 600 $OPENEMR/sql_upgrade.php
244 chmod 600 $OPENEMR/ippf_upgrade.php
245 chmod 600 $OPENEMR/gacl
/setup.php
247 #set writable files and directories (that are not within sites directory)
248 chown
$WEB_GROUP.
$WEB_USER $OPENEMR/interface
/modules
/zend_modules
/config
/application.config.php
249 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/library
/freeb
250 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/cache
251 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/compiled
252 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/gacl
/admin
/templates_c
254 #set writable directories (that are within sites directory)
255 # (go through each site)
256 for dir
in $
(find $SITEDIR/* -maxdepth 0 -type d
); do
258 SITENAME
=$
(basename "$dir")
259 #set the writable directories
260 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/documents
261 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/edi
262 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/era
263 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/letter_templates
266 #update config file, change process to complete and remove others
267 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
268 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
269 sed -i "/^[ ]*previous_version[ =].*$/d" $CONFIG
270 sed -i "/^[ ]*.*sqllocation[ =].*$/d" $CONFIG
271 sed -i "/^[ ]*.*sqluser[ =].*$/d" $CONFIG
272 sed -i "/^[ ]*.*sqlpassword[ =].*$/d" $CONFIG
273 sed -i "/^[ ]*.*sqldatabase[ =].*$/d" $CONFIG
274 sed -i "/^[ ]*.*sqlutfflag[ =].*$/d" $CONFIG
277 prompt_input openemr
/success_upgrade critical ret_result
278 log_only
"OpenEMR upgrade is complete."
279 log_only
"Recommend setting optional configuration settings in:"
280 log_only
"$SITEDIR/<sitename>/config.php"
281 log_only
"(We have renamed your old configuration files to *.OLD)"
282 log_only
"(We recommend you delete the *.OLD files when done)"
283 log_only
"We have placed backup of your old OpenEMR in $TMPDIR"
284 log_only
"(We recommend you copy this somewhere protected since it"
285 log_only
"contains confidential patient information)"
292 elif [ "$PLAN" == "install" ] ; then
293 #continue with installation
294 log_only
"Installing OpenEMR"
296 unable_exit
"Error reading plan variable in configuration file."
299 #This Section edits the php.ini file to accomodate the proper functioning of OpenEMR using php
300 log_only
"Configuring PHP for OpenEMR"
302 #check to ensure the php configuration file exists
304 # First, collect php variables
306 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
308 collect_php_commented_out
() {
309 echo `grep -i "^;[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
311 EXEC_TEXT
="max_execution_time"
312 EXEC
=$
(collect_php
"$EXEC_TEXT")
313 INPUT_TEXT
="max_input_time"
314 INPUT
=$
(collect_php
"$INPUT_TEXT")
315 MEM_TEXT
="memory_limit"
316 MEM
=$
(collect_php
"$MEM_TEXT")
317 DISP_TEXT
="display_errors"
318 DISP
=$
(collect_php
"$DISP_TEXT")
319 LOGG_TEXT
="log_errors"
320 LOGG
=$
(collect_php
"$LOGG_TEXT")
321 GLOB_TEXT
="register_globals"
322 GLOB
=$
(collect_php
"$GLOB_TEXT")
323 POST_TEXT
="post_max_size"
324 POST
=$
(collect_php
"$POST_TEXT")
325 UPLOAD_TEXT
="file_uploads"
326 UPLOAD
=$
(collect_php
"$UPLOAD_TEXT")
327 FILESIZE_TEXT
="upload_max_filesize"
328 FILESIZE
=$
(collect_php
"$FILESIZE_TEXT")
329 MAXINPUTVARS_TEXT
="max_input_vars"
330 MAXINPUTVARS
=$
(collect_php
"$MAXINPUTVARS_TEXT")
331 MAXINPUTVARS_IF_COMMENTED
=$
(collect_php_commented_out
"$MAXINPUTVARS_TEXT")
333 # Second, backup the php.ini file before modifying
336 # Third, edit the required entries
337 # Do this in a for loop.
338 # First iteration will discover the recommended changes
339 # Second iteration will make the changes (if user request this)
342 if [ "$3" -eq "1" ]; then
343 # make rec to php.ini
344 if [ "$FLAG_ON" -eq "0" ]; then
345 log_only
"We changed the following setting(s) in your php configuration file at $PHP :"
350 sed -i "s/^[ ]*$1[ =].*$/$1 = $2/" $PHP
351 log_only
"Successfully set $1 = $2"
354 process_php_commented_out
() {
355 if [ "$3" -eq "1" ]; then
356 # make rec to php.ini
357 if [ "$FLAG_ON" -eq "0" ]; then
358 log_only
"We changed the following setting(s) in your php configuration file at $PHP :"
363 sed -i "s/^;[ ]*$1[ =].*$/$1 = $2/" $PHP
364 log_only
"Successfully set $1 = $2"
367 for i
in `seq 1 2`; do
368 if [ ! -z "$EXEC" ] && [ "$EXEC" -lt "60" ]; then
369 process_php
"$EXEC_TEXT" "60" $i
371 if [ ! -z "$INPUT" ] && [ "$INPUT" -lt "90" ]; then
372 process_php
"$INPUT_TEXT" "90" $i
374 if [ ! -z "$MEM" ] && [ "$MEM" -lt "128" ]; then
375 process_php
"$MEM_TEXT" "128M" $i
377 if [ ! -z "$DISP" ] && [ "$DISP" != "Off" ]; then
378 process_php
"$DISP_TEXT" "Off" $i
380 if [ ! -z "$LOGG" ] && [ "$LOGG" != "On" ]; then
381 process_php
"$LOGG_TEXT" "On" $i
383 if [ ! -z "$GLOB" ] && [ "$GLOB" != "Off" ]; then
384 process_php
"$GLOB_TEXT" "Off" $i
386 if [ ! -z "$POST" ] && [ "$POST" -lt "30" ]; then
387 process_php
"$POST_TEXT" "30M" $i
389 if [ ! -z "$UPLOAD" ] && [ "$UPLOAD" != "On" ]; then
390 process_php
"$UPLOAD_TEXT" "On" $i
392 if [ ! -z "$FILESIZE" ] && [ "$FILESIZE" -lt "30" ]; then
393 process_php
"$FILESIZE_TEXT" "30M" $i
395 if [ ! -z "$MAXINPUTVARS" ] && [ "$MAXINPUTVARS" -lt "3000" ]; then
396 process_php
"$MAXINPUTVARS_TEXT" "3000" $i
398 if [ ! -z "$MAXINPUTVARS_IF_COMMENTED" ] && [ "$MAXINPUTVARS_IF_COMMENTED" -lt "3000" ]; then
399 process_php_commented_out
"$MAXINPUTVARS_TEXT" "3000" $i
401 if [ "$FLAG_ON" -eq "0" ]; then
402 log_only
"Your PHP configuration is perfect for OpenEMR."
405 if [ "$i" -eq "1" ]; then
406 prompt_input openemr
/php_configure high ret_result
409 if [ "$i" -eq "1" ]; then
410 log_only
"(We have placed a backup of your php configuration at $PHP.BAK)"
414 #can't find php config file, so just echo instructions
415 log_only
"We recommend ensuring you have below settings in your php configuration file:"
416 log_only
"max_execution_time = 60"
417 log_only
"max_input_time = 90"
418 log_only
"memory_limit = 128M"
419 log_only
"display_errors = Off"
420 log_only
"log_errors = On"
421 log_only
"register_globals = Off"
422 log_only
"post_max_size = 30M"
423 log_only
"file_uploads = On"
424 log_only
"upload_max_filesize = 30M"
425 log_only
"max_input_vars = 3000"
426 log_only
"(note max_input_vars setting only exists since php 5.3.9)"
429 log_only
"Done configuring PHP"
431 # NEED TO CONFIGURE APACHE BEFORE CONFIGURATION SINCE ZEND SUPPORT NEEDS PROPER CONFIGURATION
432 # Activate the OpenEMR conf file for apache
433 log_only
"Activate OpenEMR config file for Apache"
434 a2ensite openemr.conf
435 # Ensure the apache rewrite module is turned on
437 # Restart the apache server
438 log_only
"Restarting Apache service..."
439 invoke-rc.d apache2 restart
>> $LOG 2>&1
441 #collect the mysql root password (if applicable)
443 if check_mysql
"$MPASS" "mysql"; then
444 log_only
"Passed the mysql check loop"
446 #the blank initial mysql password didn't work, so prompt for password
447 # (will give 3 chances to provide correct password)
450 prompt_input openemr
/mysql_p_install_
${COUNTDOWN} critical ret_result
452 if check_mysql
"$MPASS" "mysql"; then
453 #the mysql root password works, so can exit loop
454 log_only
"Passed the mysql check loop"
457 #the mysql root password did not work
458 if [ "$COUNTDOWN" -ge "3" ]; then
459 prompt_input openemr
/no_configure_mysql_root high ret_result
460 log_only
"Will install OpenEMR, however will not configure OpenEMR. (unable to provide root password)"
468 #decide whether to configure OpenEMR after it is installed
470 if check_mysql
"$MPASS" "mysql"; then
471 #before auto configuration, ensure the openemr user and database do not exist
472 # Check for openemr database in mysql, if exist then will not configure
473 if check_mysql
"$MPASS" "$INSTALL_DATABASE"; then
474 prompt_input openemr
/no_configure_mysql_database high ret_result
475 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL database already exists)"
476 configure_flag
=false
;
478 # Check for OpenEMR user in mysql.user, if exist then will not configure
479 USER
=$
(mysql
-s -u root
-h localhost
--password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
480 if [ "$USER" == "$INSTALL_USER" ]; then
481 prompt_input openemr
/no_configure_mysql_user high ret_result
482 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL user already exists)"
483 configure_flag
=false
;
486 #the mysql root password didn't work, so do not configure OpenEMR
487 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (root password did not work)"
488 configure_flag
=false
;
491 #go to openemr directory
495 chown
-Rf root
:root
$OPENEMR
497 #INSTALL/CONFIGURE OPENEMR
499 if $configure_flag; then
500 log_only
"Installing/Configuring OpenEMR..."
502 log_only
"Installing OpenEMR ..."
505 # Set file and directory permissions (note use default site directory for new install)
506 chmod 666 $SITEDIR/default
/sqlconf.php
507 chown
$WEB_GROUP.
$WEB_USER $OPENEMR/interface
/modules
/zend_modules
/config
/application.config.php
508 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/documents
509 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/edi
510 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/era
511 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/library
/freeb
512 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/letter_templates
513 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/cache
514 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/compiled
515 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/gacl
/admin
/templates_c
517 if $configure_flag; then
518 # Create a random password for the openemr mysql user
519 password
=$
(makepasswd
--char=12)
521 # openemr installation VARIABLES
522 if [ "$MPASS" == "" ] ; then
523 rootpass
="rootpass=BLANK" #MySQL server root password
525 rootpass
="rootpass=$MPASS" #MySQL server root password
527 login
="login=$INSTALL_USER" #username to MySQL openemr database
528 pass
="pass=$password" #password to MySQL openemr database
529 dbname
="dbname=$INSTALL_DATABASE" #MySQL openemr database name
534 sed -e 's@^exit;@ @' <$INST >$INSTTEMP
535 php
-f $INSTTEMP $rootpass $login $pass $dbname >> $LOG 2>&1
538 #remove global permission to all setup scripts
539 chmod 600 $OPENEMR/acl_setup.php
540 chmod 600 $OPENEMR/acl_upgrade.php
541 chmod 600 $OPENEMR/setup.php
542 chmod 600 $OPENEMR/sql_upgrade.php
543 chmod 600 $OPENEMR/ippf_upgrade.php
544 chmod 600 $OPENEMR/gacl
/setup.php
546 log_only
"Done configuring OpenEMR"
549 if $configure_flag; then
550 prompt_input openemr
/success_install_config high ret_result
551 log_only
"You can now use OpenEMR by browsing to:"
552 log_only
"http://localhost/openemr"
553 log_only
"user is 'admin' and password is 'pass'"
554 log_only
"See the openemr man page for further instructions:"
555 log_only
"type 'man openemr' at command line"
557 prompt_input openemr
/success_install high ret_result
558 log_only
"You can now configure OpenEMR by browsing to:"
559 log_only
"http://localhost/openemr"
560 log_only
"See the openemr man page for further instructions:"
561 log_only
"type 'man openemr' at command line"
564 #update config file, change process to complete and remove plan and pass
565 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
566 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
573 abort-upgrade|abort-remove|abort-deconfigure
)
575 echo "postinst asked to do $1"
579 echo "postinst called with unknown argument \`$1'" >&2