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/library
/freeb
251 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/cache
252 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/compiled
253 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/gacl
/admin
/templates_c
255 #set writable directories (that are within sites directory)
256 # (go through each site)
257 for dir
in $
(find $SITEDIR/* -maxdepth 0 -type d
); do
259 SITENAME
=$
(basename "$dir")
260 #set the writable directories
261 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/documents
262 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/edi
263 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/era
264 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/letter_templates
267 #update config file, change process to complete and remove others
268 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
269 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
270 sed -i "/^[ ]*previous_version[ =].*$/d" $CONFIG
271 sed -i "/^[ ]*.*sqllocation[ =].*$/d" $CONFIG
272 sed -i "/^[ ]*.*sqluser[ =].*$/d" $CONFIG
273 sed -i "/^[ ]*.*sqlpassword[ =].*$/d" $CONFIG
274 sed -i "/^[ ]*.*sqldatabase[ =].*$/d" $CONFIG
275 sed -i "/^[ ]*.*sqlutfflag[ =].*$/d" $CONFIG
278 prompt_input openemr
/success_upgrade critical ret_result
279 log_only
"OpenEMR upgrade is complete."
280 log_only
"Recommend setting optional configuration settings in:"
281 log_only
"$SITEDIR/<sitename>/config.php"
282 log_only
"(We have renamed your old configuration files to *.OLD)"
283 log_only
"(We recommend you delete the *.OLD files when done)"
284 log_only
"We have placed backup of your old OpenEMR in $TMPDIR"
285 log_only
"(We recommend you copy this somewhere protected since it"
286 log_only
"contains confidential patient information)"
293 elif [ "$PLAN" == "install" ] ; then
294 #continue with installation
295 log_only
"Installing OpenEMR"
297 unable_exit
"Error reading plan variable in configuration file."
300 #This Section edits the php.ini file to accomodate the proper functioning of OpenEMR using php
301 log_only
"Configuring PHP for OpenEMR"
303 #check if PHP7 config file exists; if it does then use it.
304 if [ -f $PHP_ALTERNATE ]; then
308 #check to ensure the php configuration file exists
310 # First, collect php variables
312 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
314 collect_php_commented_out
() {
315 echo `grep -i "^;[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
317 EXEC_TEXT
="max_execution_time"
318 EXEC
=$
(collect_php
"$EXEC_TEXT")
319 INPUT_TEXT
="max_input_time"
320 INPUT
=$
(collect_php
"$INPUT_TEXT")
321 MEM_TEXT
="memory_limit"
322 MEM
=$
(collect_php
"$MEM_TEXT")
323 DISP_TEXT
="display_errors"
324 DISP
=$
(collect_php
"$DISP_TEXT")
325 LOGG_TEXT
="log_errors"
326 LOGG
=$
(collect_php
"$LOGG_TEXT")
327 GLOB_TEXT
="register_globals"
328 GLOB
=$
(collect_php
"$GLOB_TEXT")
329 POST_TEXT
="post_max_size"
330 POST
=$
(collect_php
"$POST_TEXT")
331 UPLOAD_TEXT
="file_uploads"
332 UPLOAD
=$
(collect_php
"$UPLOAD_TEXT")
333 FILESIZE_TEXT
="upload_max_filesize"
334 FILESIZE
=$
(collect_php
"$FILESIZE_TEXT")
335 MAXINPUTVARS_TEXT
="max_input_vars"
336 MAXINPUTVARS
=$
(collect_php
"$MAXINPUTVARS_TEXT")
337 MAXINPUTVARS_IF_COMMENTED
=$
(collect_php_commented_out
"$MAXINPUTVARS_TEXT")
339 # Second, backup the php.ini file before modifying
342 # Third, edit the required entries
343 # Do this in a for loop.
344 # First iteration will discover the recommended changes
345 # Second iteration will make the changes (if user request this)
348 if [ "$3" -eq "1" ]; then
349 # make rec to php.ini
350 if [ "$FLAG_ON" -eq "0" ]; then
351 log_only
"We changed the following setting(s) in your php configuration file at $PHP :"
356 sed -i "s/^[ ]*$1[ =].*$/$1 = $2/" $PHP
357 log_only
"Successfully set $1 = $2"
360 process_php_commented_out
() {
361 if [ "$3" -eq "1" ]; then
362 # make rec to php.ini
363 if [ "$FLAG_ON" -eq "0" ]; then
364 log_only
"We changed the following setting(s) in your php configuration file at $PHP :"
369 sed -i "s/^;[ ]*$1[ =].*$/$1 = $2/" $PHP
370 log_only
"Successfully set $1 = $2"
373 for i
in `seq 1 2`; do
374 if [ ! -z "$EXEC" ] && [ "$EXEC" -lt "60" ]; then
375 process_php
"$EXEC_TEXT" "60" $i
377 if [ ! -z "$INPUT" ] && [ "$INPUT" -lt "90" ]; then
378 process_php
"$INPUT_TEXT" "90" $i
380 if [ ! -z "$MEM" ] && [ "$MEM" -lt "128" ]; then
381 process_php
"$MEM_TEXT" "128M" $i
383 if [ ! -z "$DISP" ] && [ "$DISP" != "Off" ]; then
384 process_php
"$DISP_TEXT" "Off" $i
386 if [ ! -z "$LOGG" ] && [ "$LOGG" != "On" ]; then
387 process_php
"$LOGG_TEXT" "On" $i
389 if [ ! -z "$GLOB" ] && [ "$GLOB" != "Off" ]; then
390 process_php
"$GLOB_TEXT" "Off" $i
392 if [ ! -z "$POST" ] && [ "$POST" -lt "30" ]; then
393 process_php
"$POST_TEXT" "30M" $i
395 if [ ! -z "$UPLOAD" ] && [ "$UPLOAD" != "On" ]; then
396 process_php
"$UPLOAD_TEXT" "On" $i
398 if [ ! -z "$FILESIZE" ] && [ "$FILESIZE" -lt "30" ]; then
399 process_php
"$FILESIZE_TEXT" "30M" $i
401 if [ ! -z "$MAXINPUTVARS" ] && [ "$MAXINPUTVARS" -lt "3000" ]; then
402 process_php
"$MAXINPUTVARS_TEXT" "3000" $i
404 if [ ! -z "$MAXINPUTVARS_IF_COMMENTED" ] && [ "$MAXINPUTVARS_IF_COMMENTED" -lt "3000" ]; then
405 process_php_commented_out
"$MAXINPUTVARS_TEXT" "3000" $i
407 if [ "$FLAG_ON" -eq "0" ]; then
408 log_only
"Your PHP configuration is perfect for OpenEMR."
411 if [ "$i" -eq "1" ]; then
412 prompt_input openemr
/php_configure high ret_result
415 if [ "$i" -eq "1" ]; then
416 log_only
"(We have placed a backup of your php configuration at $PHP.BAK)"
420 #can't find php config file, so just echo instructions
421 log_only
"We recommend ensuring you have below settings in your php configuration file:"
422 log_only
"max_execution_time = 60"
423 log_only
"max_input_time = 90"
424 log_only
"memory_limit = 128M"
425 log_only
"display_errors = Off"
426 log_only
"log_errors = On"
427 log_only
"register_globals = Off"
428 log_only
"post_max_size = 30M"
429 log_only
"file_uploads = On"
430 log_only
"upload_max_filesize = 30M"
431 log_only
"max_input_vars = 3000"
432 log_only
"(note max_input_vars setting only exists since php 5.3.9)"
435 log_only
"Done configuring PHP"
437 # NEED TO CONFIGURE APACHE BEFORE CONFIGURATION SINCE ZEND SUPPORT NEEDS PROPER CONFIGURATION
438 # Activate the OpenEMR conf file for apache
439 log_only
"Activate OpenEMR config file for Apache"
440 a2ensite openemr.conf
441 # Ensure the apache rewrite module is turned on
443 # Restart the apache server
444 log_only
"Restarting Apache service..."
445 invoke-rc.d apache2 restart
>> $LOG 2>&1
447 #collect the mysql root password (if applicable)
449 if check_mysql
"$MPASS" "mysql"; then
450 log_only
"Passed the mysql check loop"
452 #the blank initial mysql password didn't work, so prompt for password
453 # (will give 3 chances to provide correct password)
456 prompt_input openemr
/mysql_p_install_
${COUNTDOWN} critical ret_result
458 if check_mysql
"$MPASS" "mysql"; then
459 #the mysql root password works, so can exit loop
460 log_only
"Passed the mysql check loop"
463 #the mysql root password did not work
464 if [ "$COUNTDOWN" -ge "3" ]; then
465 prompt_input openemr
/no_configure_mysql_root high ret_result
466 log_only
"Will install OpenEMR, however will not configure OpenEMR. (unable to provide root password)"
474 #decide whether to configure OpenEMR after it is installed
476 if check_mysql
"$MPASS" "mysql"; then
477 #before auto configuration, ensure the openemr user and database do not exist
478 # Check for openemr database in mysql, if exist then will not configure
479 if check_mysql
"$MPASS" "$INSTALL_DATABASE"; then
480 prompt_input openemr
/no_configure_mysql_database high ret_result
481 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL database already exists)"
482 configure_flag
=false
;
484 # Check for OpenEMR user in mysql.user, if exist then will not configure
485 USER
=$
(mysql
-s -u root
-h localhost
--password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
486 if [ "$USER" == "$INSTALL_USER" ]; then
487 prompt_input openemr
/no_configure_mysql_user high ret_result
488 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL user already exists)"
489 configure_flag
=false
;
492 #the mysql root password didn't work, so do not configure OpenEMR
493 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (root password did not work)"
494 configure_flag
=false
;
497 #go to openemr directory
501 chown
-Rf root
:root
$OPENEMR
503 #INSTALL/CONFIGURE OPENEMR
505 if $configure_flag; then
506 log_only
"Installing/Configuring OpenEMR..."
508 log_only
"Installing OpenEMR ..."
511 # Set file and directory permissions (note use default site directory for new install)
512 chmod 666 $SITEDIR/default
/sqlconf.php
513 chown
$WEB_GROUP.
$WEB_USER $OPENEMR/interface
/modules
/zend_modules
/config
/application.config.php
514 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/documents
515 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/edi
516 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/era
517 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/library
/freeb
518 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/letter_templates
519 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/cache
520 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/compiled
521 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/gacl
/admin
/templates_c
523 if $configure_flag; then
524 # Create a random password for the openemr mysql user
525 password
=$
(makepasswd
--char=12)
527 # openemr installation VARIABLES
528 if [ "$MPASS" == "" ] ; then
529 rootpass
="rootpass=BLANK" #MySQL server root password
531 rootpass
="rootpass=$MPASS" #MySQL server root password
533 login
="login=$INSTALL_USER" #username to MySQL openemr database
534 pass
="pass=$password" #password to MySQL openemr database
535 dbname
="dbname=$INSTALL_DATABASE" #MySQL openemr database name
540 sed -e 's@^exit;@ @' <$INST >$INSTTEMP
541 php
-f $INSTTEMP $rootpass $login $pass $dbname >> $LOG 2>&1
544 #remove global permission to all setup scripts
545 chmod 600 $OPENEMR/acl_setup.php
546 chmod 600 $OPENEMR/acl_upgrade.php
547 chmod 600 $OPENEMR/setup.php
548 chmod 600 $OPENEMR/sql_upgrade.php
549 chmod 600 $OPENEMR/ippf_upgrade.php
550 chmod 600 $OPENEMR/gacl
/setup.php
552 log_only
"Done configuring OpenEMR"
555 if $configure_flag; then
556 prompt_input openemr
/success_install_config high ret_result
557 log_only
"You can now use OpenEMR by browsing to:"
558 log_only
"http://localhost/openemr"
559 log_only
"user is 'admin' and password is 'pass'"
560 log_only
"See the openemr man page for further instructions:"
561 log_only
"type 'man openemr' at command line"
563 prompt_input openemr
/success_install high ret_result
564 log_only
"You can now configure OpenEMR by browsing to:"
565 log_only
"http://localhost/openemr"
566 log_only
"See the openemr man page for further instructions:"
567 log_only
"type 'man openemr' at command line"
570 #update config file, change process to complete and remove plan and pass
571 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
572 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
579 abort-upgrade|abort-remove|abort-deconfigure
)
581 echo "postinst asked to do $1"
585 echo "postinst called with unknown argument \`$1'" >&2