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
68 PHP_ALTERNATE
=/etc
/php
/7.0/apache
2/php.ini
73 #Standardized echo function to send to only log file
74 # requires one parameter (string)
76 echo "`date`: $1" >> $LOG
79 #Standardized exit functions to be used
80 # requires one parameter (string with reason for exiting)
82 echo "`date`: $1" >> $LOG
83 echo "`date`: EXITING.........." >> $LOG
87 #function to check mysql for selected databases
88 # 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
101 if [ "`mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^
$2$
"`" == "$2" ]; then
108 #function to collect variables from config files
109 # 1st param is variable name, 2nd param is filename
111 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
114 #function to insert variables into config files
115 # 1st param is variable name, 2nd param is variable, 3rd param is filename
117 sed -i 's@^[ ]*'"$1"'[ =].*$@'"$1"' = '"$2"'@' "$3"
120 #function to prompt for input
121 # 1st param is name, 2nd param is priority, 3rd param is where result gets sent back in
125 db_fset
"$1" seen false
126 db_input
"$2" "$1" || true
129 local input_value
="$RET"
131 db_fset
"$1" seen false
133 eval $__result="'$input_value'"
136 #collect scripting information from config file
137 PROCESS
=$
(collect_var process
$CONFIG)
138 PLAN
=$
(collect_var plan
$CONFIG)
140 #Don't allow re-configuration
141 if [ "$PROCESS" == "complete" ] ; then
142 unable_exit
"OpenEMR has already been configured."
143 elif [ "$PROCESS" == "pending" ] ; then
144 #continue with configuration
145 log_only
"Configuring package..."
147 unable_exit
"Error reading process variable in configuration file."
150 if [ "$PLAN" == "upgrade" ] ; then
151 #continue with upgrade
152 OLD_VERSION
=$
(collect_var previous_version
$CONFIG)
153 log_only
"Continuing Upgrade from ($OLD_VERSION)"
155 #go to openemr directory
158 # NEED TO CONFIGURE APACHE BEFORE CONFIGURATION SINCE ZEND SUPPORT NEEDS PROPER CONFIGURATION
159 # Activate the OpenEMR conf file for apache
160 log_only
"Activate OpenEMR config file for Apache"
161 a2ensite openemr.conf
162 # Ensure the apache rewrite module is turned on
164 # Restart the apache server
165 log_only
"Restarting Apache service..."
166 invoke-rc.d apache2 restart
>> $LOG 2>&1
168 #To support the multisite module, go through each site
169 for dir
in $
(find $SITEDIR/* -maxdepth 0 -type d
); do
171 SITENAME
=$
(basename "$dir")
172 log_only
"Configuring Site ($SITENAME)"
174 #collect more information from config file
175 SQLLOCATION
=$
(collect_var
${SITENAME}_sqllocation
$CONFIG)
176 SQLUSER
=$
(collect_var
${SITENAME}_sqluser
$CONFIG)
177 SQLPASSWORD
=$
(collect_var
${SITENAME}_sqlpassword
$CONFIG)
178 SQLDATABASE
=$
(collect_var
${SITENAME}_sqldatabase
$CONFIG)
179 SQLUTFFLAG
=$
(collect_var
${SITENAME}_sqlutfflag
$CONFIG)
181 #configure database configuration file
182 insert_var
"\$host" "\'$SQLLOCATION\';" $SITEDIR/$SITENAME/sqlconf.php
183 insert_var
"\$login" "\'$SQLUSER\';" $SITEDIR/$SITENAME/sqlconf.php
184 insert_var
"\$pass" "\'$SQLPASSWORD\';" $SITEDIR/$SITENAME/sqlconf.php
185 insert_var
"\$dbase" "\'$SQLDATABASE\';" $SITEDIR/$SITENAME/sqlconf.php
186 insert_var
"\$disable_utf8_flag" "$SQLUTFFLAG;" $SITEDIR/$SITENAME/sqlconf.php
187 sed -i "s/^[ ]*\$config[ =].*0/\$config = 1/" $SITEDIR/$SITENAME/sqlconf.php
189 #upgrade the sql database
190 CONC_VERSION
=$
(echo $OLD_VERSION | cut
-d \
- -f 1)
191 echo "<?php \$_GET['site'] = '$SITENAME'; ?>" > $OPENEMR/TEMPsql_upgrade.php
192 cat $OPENEMR/sql_upgrade.php
>> $OPENEMR/TEMPsql_upgrade.php
193 sed -i "/input type='submit'/d" $OPENEMR/TEMPsql_upgrade.php
194 sed -i "s/!empty(\$_POST\['form_submit'\])/empty(\$_POST\['form_submit'\])/" $OPENEMR/TEMPsql_upgrade.php
195 sed -i "s/^[ ]*\$form_old_version[ =].*$/\$form_old_version = \"$CONC_VERSION\";/" $OPENEMR/TEMPsql_upgrade.php
196 php
-f $OPENEMR/TEMPsql_upgrade.php
>> $LOG 2>&1
197 rm -f $OPENEMR/TEMPsql_upgrade.php
199 #copy the old config file into new with the OLD at end to allow manual configuration of old
201 if [ -d "$TMPDIR/openemr_web_$OLD_VERSION/sites/$SITENAME" ]; then
202 cp -f "$TMPDIR/openemr_web_$OLD_VERSION/sites/$SITENAME/config.php" "$SITEDIR/$SITENAME/config.php.OLD"
204 #need to move from old location, so sitename will always be default in this case
205 cp -f $TMPDIR/openemr_web_
$OLD_VERSION/includes
/config.php
$SITEDIR/default
/config.php.OLD
209 log_only
"Upgraded OpenEMR site ($SITENAME) with sql database ($SQLDATABASE) and sql user ($SQLUSER)."
213 # if site-specific directories are in the old locations, move them.
214 if [ -d $OPENEMR/documents
]; then
215 if [ "$(ls $OPENEMR/documents)" ]; then
216 mv -f $OPENEMR/documents
/* $SITEDIR/default
/documents
/
218 rm -rf $OPENEMR/documents
220 if [ -d $OPENEMR/era
]; then
221 if [ "$(ls $OPENEMR/era)" ]; then
222 mv -f $OPENEMR/era
/* $SITEDIR/default
/era
/
226 if [ -d $OPENEMR/edi
]; then
227 if [ "$(ls $OPENEMR/edi)" ]; then
228 mv -f $OPENEMR/edi
/* $SITEDIR/default
/edi
/
232 if [ -d $OPENEMR/custom
/letter_templates
]; then
233 if [ "$(ls $OPENEMR/custom/letter_templates)" ]; then
234 mv -f $OPENEMR/custom
/letter_templates
/* $SITEDIR/default
/letter_templates
/
236 rm -rf $OPENEMR/custom
/letter_templates
240 chown
-Rf root
:root
$OPENEMR
241 chmod 600 $OPENEMR/acl_setup.php
242 chmod 600 $OPENEMR/acl_upgrade.php
243 chmod 600 $OPENEMR/setup.php
244 chmod 600 $OPENEMR/sql_upgrade.php
245 chmod 600 $OPENEMR/ippf_upgrade.php
246 chmod 600 $OPENEMR/gacl
/setup.php
248 #set writable files and directories (that are not within sites directory)
249 chown
$WEB_GROUP.
$WEB_USER $OPENEMR/interface
/modules
/zend_modules
/config
/application.config.php
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 if PHP7 config file exists; if it does then use it.
303 if [ -f $PHP_ALTERNATE ]; then
307 #check to ensure the php configuration file exists
309 # First, collect php variables
311 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
313 collect_php_commented_out
() {
314 echo `grep -i "^;[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
316 EXEC_TEXT
="max_execution_time"
317 EXEC
=$
(collect_php
"$EXEC_TEXT")
318 INPUT_TEXT
="max_input_time"
319 INPUT
=$
(collect_php
"$INPUT_TEXT")
320 MEM_TEXT
="memory_limit"
321 MEM
=$
(collect_php
"$MEM_TEXT")
322 DISP_TEXT
="display_errors"
323 DISP
=$
(collect_php
"$DISP_TEXT")
324 LOGG_TEXT
="log_errors"
325 LOGG
=$
(collect_php
"$LOGG_TEXT")
326 GLOB_TEXT
="register_globals"
327 GLOB
=$
(collect_php
"$GLOB_TEXT")
328 POST_TEXT
="post_max_size"
329 POST
=$
(collect_php
"$POST_TEXT")
330 UPLOAD_TEXT
="file_uploads"
331 UPLOAD
=$
(collect_php
"$UPLOAD_TEXT")
332 FILESIZE_TEXT
="upload_max_filesize"
333 FILESIZE
=$
(collect_php
"$FILESIZE_TEXT")
334 MAXINPUTVARS_TEXT
="max_input_vars"
335 MAXINPUTVARS
=$
(collect_php
"$MAXINPUTVARS_TEXT")
336 MAXINPUTVARS_IF_COMMENTED
=$
(collect_php_commented_out
"$MAXINPUTVARS_TEXT")
338 # Second, backup the php.ini file before modifying
341 # Third, edit the required entries
342 # Do this in a for loop.
343 # First iteration will discover the recommended changes
344 # Second iteration will make the changes (if user request this)
347 if [ "$3" -eq "1" ]; then
348 # make rec to php.ini
349 if [ "$FLAG_ON" -eq "0" ]; then
350 log_only
"We changed the following setting(s) in your php configuration file at $PHP :"
355 sed -i "s/^[ ]*$1[ =].*$/$1 = $2/" $PHP
356 log_only
"Successfully set $1 = $2"
359 process_php_commented_out
() {
360 if [ "$3" -eq "1" ]; then
361 # make rec to php.ini
362 if [ "$FLAG_ON" -eq "0" ]; then
363 log_only
"We changed the following setting(s) in your php configuration file at $PHP :"
368 sed -i "s/^;[ ]*$1[ =].*$/$1 = $2/" $PHP
369 log_only
"Successfully set $1 = $2"
372 for i
in `seq 1 2`; do
373 if [ ! -z "$EXEC" ] && [ "$EXEC" -lt "60" ]; then
374 process_php
"$EXEC_TEXT" "60" $i
376 if [ ! -z "$INPUT" ] && [ "$INPUT" -lt "90" ]; then
377 process_php
"$INPUT_TEXT" "90" $i
379 if [ ! -z "$MEM" ] && [ "$MEM" -lt "128" ]; then
380 process_php
"$MEM_TEXT" "128M" $i
382 if [ ! -z "$DISP" ] && [ "$DISP" != "Off" ]; then
383 process_php
"$DISP_TEXT" "Off" $i
385 if [ ! -z "$LOGG" ] && [ "$LOGG" != "On" ]; then
386 process_php
"$LOGG_TEXT" "On" $i
388 if [ ! -z "$GLOB" ] && [ "$GLOB" != "Off" ]; then
389 process_php
"$GLOB_TEXT" "Off" $i
391 if [ ! -z "$POST" ] && [ "$POST" -lt "30" ]; then
392 process_php
"$POST_TEXT" "30M" $i
394 if [ ! -z "$UPLOAD" ] && [ "$UPLOAD" != "On" ]; then
395 process_php
"$UPLOAD_TEXT" "On" $i
397 if [ ! -z "$FILESIZE" ] && [ "$FILESIZE" -lt "30" ]; then
398 process_php
"$FILESIZE_TEXT" "30M" $i
400 if [ ! -z "$MAXINPUTVARS" ] && [ "$MAXINPUTVARS" -lt "3000" ]; then
401 process_php
"$MAXINPUTVARS_TEXT" "3000" $i
403 if [ ! -z "$MAXINPUTVARS_IF_COMMENTED" ] && [ "$MAXINPUTVARS_IF_COMMENTED" -lt "3000" ]; then
404 process_php_commented_out
"$MAXINPUTVARS_TEXT" "3000" $i
406 if [ "$FLAG_ON" -eq "0" ]; then
407 log_only
"Your PHP configuration is perfect for OpenEMR."
410 if [ "$i" -eq "1" ]; then
411 prompt_input openemr
/php_configure high ret_result
414 if [ "$i" -eq "1" ]; then
415 log_only
"(We have placed a backup of your php configuration at $PHP.BAK)"
419 #can't find php config file, so just echo instructions
420 log_only
"We recommend ensuring you have below settings in your php configuration file:"
421 log_only
"max_execution_time = 60"
422 log_only
"max_input_time = 90"
423 log_only
"memory_limit = 128M"
424 log_only
"display_errors = Off"
425 log_only
"log_errors = On"
426 log_only
"register_globals = Off"
427 log_only
"post_max_size = 30M"
428 log_only
"file_uploads = On"
429 log_only
"upload_max_filesize = 30M"
430 log_only
"max_input_vars = 3000"
431 log_only
"(note max_input_vars setting only exists since php 5.3.9)"
434 log_only
"Done configuring PHP"
436 # NEED TO CONFIGURE APACHE BEFORE CONFIGURATION SINCE ZEND SUPPORT NEEDS PROPER CONFIGURATION
437 # Activate the OpenEMR conf file for apache
438 log_only
"Activate OpenEMR config file for Apache"
439 a2ensite openemr.conf
440 # Ensure the apache rewrite module is turned on
442 # Restart the apache server
443 log_only
"Restarting Apache service..."
444 invoke-rc.d apache2 restart
>> $LOG 2>&1
446 #collect the mysql root password (if applicable)
448 if check_mysql
"$MPASS" "mysql"; then
449 log_only
"Passed the mysql check loop"
451 #the blank initial mysql password didn't work, so prompt for password
452 # (will give 3 chances to provide correct password)
455 prompt_input openemr
/mysql_p_install_
${COUNTDOWN} critical ret_result
457 if check_mysql
"$MPASS" "mysql"; then
458 #the mysql root password works, so can exit loop
459 log_only
"Passed the mysql check loop"
462 #the mysql root password did not work
463 if [ "$COUNTDOWN" -ge "3" ]; then
464 prompt_input openemr
/no_configure_mysql_root high ret_result
465 log_only
"Will install OpenEMR, however will not configure OpenEMR. (unable to provide root password)"
473 #decide whether to configure OpenEMR after it is installed
475 if check_mysql
"$MPASS" "mysql"; then
476 #before auto configuration, ensure the openemr user and database do not exist
477 # Check for openemr database in mysql, if exist then will not configure
478 if check_mysql
"$MPASS" "$INSTALL_DATABASE"; then
479 prompt_input openemr
/no_configure_mysql_database high ret_result
480 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL database already exists)"
481 configure_flag
=false
;
483 # Check for OpenEMR user in mysql.user, if exist then will not configure
484 USER
=$
(mysql
-s -u root
-h localhost
--password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
485 if [ "$USER" == "$INSTALL_USER" ]; then
486 prompt_input openemr
/no_configure_mysql_user high ret_result
487 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL user already exists)"
488 configure_flag
=false
;
491 #the mysql root password didn't work, so do not configure OpenEMR
492 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (root password did not work)"
493 configure_flag
=false
;
496 #go to openemr directory
500 chown
-Rf root
:root
$OPENEMR
502 #INSTALL/CONFIGURE OPENEMR
504 if $configure_flag; then
505 log_only
"Installing/Configuring OpenEMR..."
507 log_only
"Installing OpenEMR ..."
510 # Set file and directory permissions (note use default site directory for new install)
511 chmod 666 $SITEDIR/default
/sqlconf.php
512 chown
$WEB_GROUP.
$WEB_USER $OPENEMR/interface
/modules
/zend_modules
/config
/application.config.php
513 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/documents
514 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/edi
515 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/era
516 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/letter_templates
517 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/cache
518 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/compiled
519 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/gacl
/admin
/templates_c
521 if $configure_flag; then
522 # Create a random password for the openemr mysql user
523 password
=$
(makepasswd
--char=12)
525 # openemr installation VARIABLES
526 if [ "$MPASS" == "" ] ; then
527 rootpass
="rootpass=BLANK" #MySQL server root password
529 rootpass
="rootpass=$MPASS" #MySQL server root password
531 login
="login=$INSTALL_USER" #username to MySQL openemr database
532 pass
="pass=$password" #password to MySQL openemr database
533 dbname
="dbname=$INSTALL_DATABASE" #MySQL openemr database name
538 sed -e 's@^exit;@ @' <$INST >$INSTTEMP
539 php
-f $INSTTEMP $rootpass $login $pass $dbname >> $LOG 2>&1
542 #remove global permission to all setup scripts
543 chmod 600 $OPENEMR/acl_setup.php
544 chmod 600 $OPENEMR/acl_upgrade.php
545 chmod 600 $OPENEMR/setup.php
546 chmod 600 $OPENEMR/sql_upgrade.php
547 chmod 600 $OPENEMR/ippf_upgrade.php
548 chmod 600 $OPENEMR/gacl
/setup.php
550 log_only
"Done configuring OpenEMR"
553 if $configure_flag; then
554 prompt_input openemr
/success_install_config high ret_result
555 log_only
"You can now use OpenEMR by browsing to:"
556 log_only
"http://localhost/openemr"
557 log_only
"user is 'admin' and password is 'pass'"
558 log_only
"See the openemr man page for further instructions:"
559 log_only
"type 'man openemr' at command line"
561 prompt_input openemr
/success_install high ret_result
562 log_only
"You can now configure OpenEMR by browsing to:"
563 log_only
"http://localhost/openemr"
564 log_only
"See the openemr man page for further instructions:"
565 log_only
"type 'man openemr' at command line"
568 #update config file, change process to complete and remove plan and pass
569 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
570 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
577 abort-upgrade|abort-remove|abort-deconfigure
)
579 echo "postinst asked to do $1"
583 echo "postinst called with unknown argument \`$1'" >&2