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 #To support the multisite module, go through each site
158 for dir
in $
(find $SITEDIR/* -maxdepth 0 -type d
); do
160 SITENAME
=$
(basename "$dir")
161 log_only
"Configuring Site ($SITENAME)"
163 #collect more information from config file
164 SQLLOCATION
=$
(collect_var
${SITENAME}_sqllocation
$CONFIG)
165 SQLUSER
=$
(collect_var
${SITENAME}_sqluser
$CONFIG)
166 SQLPASSWORD
=$
(collect_var
${SITENAME}_sqlpassword
$CONFIG)
167 SQLDATABASE
=$
(collect_var
${SITENAME}_sqldatabase
$CONFIG)
168 SQLUTFFLAG
=$
(collect_var
${SITENAME}_sqlutfflag
$CONFIG)
170 #configure database configuration file
171 insert_var
"\$host" "\'$SQLLOCATION\';" $SITEDIR/$SITENAME/sqlconf.php
172 insert_var
"\$login" "\'$SQLUSER\';" $SITEDIR/$SITENAME/sqlconf.php
173 insert_var
"\$pass" "\'$SQLPASSWORD\';" $SITEDIR/$SITENAME/sqlconf.php
174 insert_var
"\$dbase" "\'$SQLDATABASE\';" $SITEDIR/$SITENAME/sqlconf.php
175 insert_var
"\$disable_utf8_flag" "$SQLUTFFLAG;" $SITEDIR/$SITENAME/sqlconf.php
176 sed -i "s/^[ ]*\$config[ =].*0/\$config = 1/" $SITEDIR/$SITENAME/sqlconf.php
178 #upgrade the sql database
179 CONC_VERSION
=$
(echo $OLD_VERSION | cut
-d \
- -f 1)
180 echo "<?php \$_GET['site'] = '$SITENAME'; ?>" > $OPENEMR/TEMPsql_upgrade.php
181 cat $OPENEMR/sql_upgrade.php
>> $OPENEMR/TEMPsql_upgrade.php
182 sed -i "/input type='submit'/d" $OPENEMR/TEMPsql_upgrade.php
183 sed -i "s/!empty(\$_POST\['form_submit'\])/empty(\$_POST\['form_submit'\])/" $OPENEMR/TEMPsql_upgrade.php
184 sed -i "s/^[ ]*\$form_old_version[ =].*$/\$form_old_version = \"$CONC_VERSION\";/" $OPENEMR/TEMPsql_upgrade.php
185 php
-f $OPENEMR/TEMPsql_upgrade.php
>> $LOG 2>&1
186 rm -f $OPENEMR/TEMPsql_upgrade.php
188 #copy the old config file into new with the OLD at end to allow manual configuration of old
190 if [ -d "$TMPDIR/openemr_web_$OLD_VERSION/sites/$SITENAME" ]; then
191 cp -f "$TMPDIR/openemr_web_$OLD_VERSION/sites/$SITENAME/config.php" "$SITEDIR/$SITENAME/config.php.OLD"
193 #need to move from old location, so sitename will always be default in this case
194 cp -f $TMPDIR/openemr_web_
$OLD_VERSION/includes
/config.php
$SITEDIR/default
/config.php.OLD
198 log_only
"Upgraded OpenEMR site ($SITENAME) with sql database ($SQLDATABASE) and sql user ($SQLUSER)."
202 # if site-specific directories are in the old locations, move them.
203 if [ -d $OPENEMR/documents
]; then
204 if [ "$(ls $OPENEMR/documents)" ]; then
205 mv -f $OPENEMR/documents
/* $SITEDIR/default
/documents
/
207 rm -rf $OPENEMR/documents
209 if [ -d $OPENEMR/era
]; then
210 if [ "$(ls $OPENEMR/era)" ]; then
211 mv -f $OPENEMR/era
/* $SITEDIR/default
/era
/
215 if [ -d $OPENEMR/edi
]; then
216 if [ "$(ls $OPENEMR/edi)" ]; then
217 mv -f $OPENEMR/edi
/* $SITEDIR/default
/edi
/
221 if [ -d $OPENEMR/custom
/letter_templates
]; then
222 if [ "$(ls $OPENEMR/custom/letter_templates)" ]; then
223 mv -f $OPENEMR/custom
/letter_templates
/* $SITEDIR/default
/letter_templates
/
225 rm -rf $OPENEMR/custom
/letter_templates
229 chown
-Rf root
:root
$OPENEMR
230 chmod 600 $OPENEMR/acl_setup.php
231 chmod 600 $OPENEMR/acl_upgrade.php
232 chmod 600 $OPENEMR/sl_convert.php
233 chmod 600 $OPENEMR/setup.php
234 chmod 600 $OPENEMR/sql_upgrade.php
235 chmod 600 $OPENEMR/ippf_upgrade.php
236 chmod 600 $OPENEMR/gacl
/setup.php
238 #set writable files and directories (that are not within sites directory)
239 chown
$WEB_GROUP.
$WEB_USER $OPENEMR/interface
/modules
/zend_modules
/config
/application.config.php
240 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/library
/freeb
241 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/cache
242 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/compiled
243 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/gacl
/admin
/templates_c
245 #set writable directories (that are within sites directory)
246 # (go through each site)
247 for dir
in $
(find $SITEDIR/* -maxdepth 0 -type d
); do
249 SITENAME
=$
(basename "$dir")
250 #set the writable directories
251 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/documents
252 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/edi
253 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/era
254 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/$SITENAME/letter_templates
257 #update config file, change process to complete and remove others
258 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
259 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
260 sed -i "/^[ ]*previous_version[ =].*$/d" $CONFIG
261 sed -i "/^[ ]*.*sqllocation[ =].*$/d" $CONFIG
262 sed -i "/^[ ]*.*sqluser[ =].*$/d" $CONFIG
263 sed -i "/^[ ]*.*sqlpassword[ =].*$/d" $CONFIG
264 sed -i "/^[ ]*.*sqldatabase[ =].*$/d" $CONFIG
265 sed -i "/^[ ]*.*sqlutfflag[ =].*$/d" $CONFIG
267 # Activate the OpenEMR conf file for apache and restart apache
268 log_only
"Activate OpenEMR config file for Apache"
269 a2ensite openemr.conf
270 log_only
"Restarting Apache service"
271 invoke-rc.d apache2 restart
>> $LOG 2>&1
274 prompt_input openemr
/success_upgrade critical ret_result
275 log_only
"OpenEMR upgrade is complete."
276 log_only
"Recommend setting optional configuration settings in:"
277 log_only
"$SITEDIR/<sitename>/config.php"
278 log_only
"(We have renamed your old configuration files to *.OLD)"
279 log_only
"(We recommend you delete the *.OLD files when done)"
280 log_only
"We have placed backup of your old OpenEMR in $TMPDIR"
281 log_only
"(We recommend you copy this somewhere protected since it"
282 log_only
"contains confidential patient information)"
289 elif [ "$PLAN" == "install" ] ; then
290 #continue with installation
291 log_only
"Installing OpenEMR"
293 unable_exit
"Error reading plan variable in configuration file."
296 #This Section edits the php.ini file to accomodate the proper functioning of OpenEMR using php
297 log_only
"Configuring PHP for OpenEMR"
299 #check to ensure the php configuration file exists
301 # First, collect php variables
303 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
305 collect_php_commented_out
() {
306 echo `grep -i "^;[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
308 TAG_TEXT
="short_open_tag"
309 TAG
=$
(collect_php
"$TAG_TEXT")
310 EXEC_TEXT
="max_execution_time"
311 EXEC
=$
(collect_php
"$EXEC_TEXT")
312 INPUT_TEXT
="max_input_time"
313 INPUT
=$
(collect_php
"$INPUT_TEXT")
314 MEM_TEXT
="memory_limit"
315 MEM
=$
(collect_php
"$MEM_TEXT")
316 DISP_TEXT
="display_errors"
317 DISP
=$
(collect_php
"$DISP_TEXT")
318 LOGG_TEXT
="log_errors"
319 LOGG
=$
(collect_php
"$LOGG_TEXT")
320 GLOB_TEXT
="register_globals"
321 GLOB
=$
(collect_php
"$GLOB_TEXT")
322 POST_TEXT
="post_max_size"
323 POST
=$
(collect_php
"$POST_TEXT")
324 UPLOAD_TEXT
="file_uploads"
325 UPLOAD
=$
(collect_php
"$UPLOAD_TEXT")
326 FILESIZE_TEXT
="upload_max_filesize"
327 FILESIZE
=$
(collect_php
"$FILESIZE_TEXT")
328 MAXINPUTVARS_TEXT
="max_input_vars"
329 MAXINPUTVARS
=$
(collect_php
"$MAXINPUTVARS_TEXT")
330 MAXINPUTVARS_IF_COMMENTED
=$
(collect_php_commented_out
"$MAXINPUTVARS_TEXT")
332 # Second, backup the php.ini file before modifying
335 # Third, edit the required entries
336 # Do this in a for loop.
337 # First iteration will discover the recommended changes
338 # Second iteration will make the changes (if user request this)
341 if [ "$3" -eq "1" ]; then
342 # make rec to php.ini
343 if [ "$FLAG_ON" -eq "0" ]; then
344 log_only
"We changed the following setting(s) in your php configuration file at $PHP :"
349 sed -i "s/^[ ]*$1[ =].*$/$1 = $2/" $PHP
350 log_only
"Successfully set $1 = $2"
353 process_php_commented_out
() {
354 if [ "$3" -eq "1" ]; then
355 # make rec to php.ini
356 if [ "$FLAG_ON" -eq "0" ]; then
357 log_only
"We changed the following setting(s) in your php configuration file at $PHP :"
362 sed -i "s/^;[ ]*$1[ =].*$/$1 = $2/" $PHP
363 log_only
"Successfully set $1 = $2"
366 for i
in `seq 1 2`; do
367 if [ ! -z "$TAG" ] && [ "$TAG" != "On" ]; then
368 process_php
"$TAG_TEXT" "On" $i
370 if [ ! -z "$EXEC" ] && [ "$EXEC" -lt "60" ]; then
371 process_php
"$EXEC_TEXT" "60" $i
373 if [ ! -z "$INPUT" ] && [ "$INPUT" -lt "90" ]; then
374 process_php
"$INPUT_TEXT" "90" $i
376 if [ ! -z "$MEM" ] && [ "$MEM" -lt "128" ]; then
377 process_php
"$MEM_TEXT" "128M" $i
379 if [ ! -z "$DISP" ] && [ "$DISP" != "Off" ]; then
380 process_php
"$DISP_TEXT" "Off" $i
382 if [ ! -z "$LOGG" ] && [ "$LOGG" != "On" ]; then
383 process_php
"$LOGG_TEXT" "On" $i
385 if [ ! -z "$GLOB" ] && [ "$GLOB" != "Off" ]; then
386 process_php
"$GLOB_TEXT" "Off" $i
388 if [ ! -z "$POST" ] && [ "$POST" -lt "30" ]; then
389 process_php
"$POST_TEXT" "30M" $i
391 if [ ! -z "$UPLOAD" ] && [ "$UPLOAD" != "On" ]; then
392 process_php
"$UPLOAD_TEXT" "On" $i
394 if [ ! -z "$FILESIZE" ] && [ "$FILESIZE" -lt "30" ]; then
395 process_php
"$FILESIZE_TEXT" "30M" $i
397 if [ ! -z "$MAXINPUTVARS" ] && [ "$MAXINPUTVARS" -lt "3000" ]; then
398 process_php
"$MAXINPUTVARS_TEXT" "3000" $i
400 if [ ! -z "$MAXINPUTVARS_IF_COMMENTED" ] && [ "$MAXINPUTVARS_IF_COMMENTED" -lt "3000" ]; then
401 process_php_commented_out
"$MAXINPUTVARS_TEXT" "3000" $i
403 if [ "$FLAG_ON" -eq "0" ]; then
404 log_only
"Your PHP configuration is perfect for OpenEMR."
407 if [ "$i" -eq "1" ]; then
408 prompt_input openemr
/php_configure high ret_result
411 if [ "$i" -eq "1" ]; then
412 log_only
"(We have placed a backup of your php configuration at $PHP.BAK)"
416 #can't find php config file, so just echo instructions
417 log_only
"We recommend ensuring you have below settings in your php configuration file:"
418 log_only
"short_open_tag = On"
419 log_only
"max_execution_time = 60"
420 log_only
"max_input_time = 90"
421 log_only
"memory_limit = 128M"
422 log_only
"display_errors = Off"
423 log_only
"log_errors = On"
424 log_only
"register_globals = Off"
425 log_only
"post_max_size = 30M"
426 log_only
"file_uploads = On"
427 log_only
"upload_max_filesize = 30M"
428 log_only
"max_input_vars = 3000"
429 log_only
"(note max_input_vars setting only exists since php 5.3.9)"
432 log_only
"Done configuring PHP"
434 # Activate the OpenEMR conf file for apache
435 log_only
"Activate OpenEMR config file for Apache"
436 a2ensite openemr.conf
439 log_only
"Restarting Apache service"
440 invoke-rc.d apache2 restart
>> $LOG 2>&1
442 #collect the mysql root password (if applicable)
444 if check_mysql
"$MPASS" "mysql"; then
445 log_only
"Passed the mysql check loop"
447 #the blank initial mysql password didn't work, so prompt for password
448 # (will give 3 chances to provide correct password)
451 prompt_input openemr
/mysql_p_install_
${COUNTDOWN} critical ret_result
453 if check_mysql
"$MPASS" "mysql"; then
454 #the mysql root password works, so can exit loop
455 log_only
"Passed the mysql check loop"
458 #the mysql root password did not work
459 if [ "$COUNTDOWN" -ge "3" ]; then
460 prompt_input openemr
/no_configure_mysql_root high ret_result
461 log_only
"Will install OpenEMR, however will not configure OpenEMR. (unable to provide root password)"
469 #decide whether to configure OpenEMR after it is installed
471 if check_mysql
"$MPASS" "mysql"; then
472 #before auto configuration, ensure the openemr user and database do not exist
473 # Check for openemr database in mysql, if exist then will not configure
474 if check_mysql
"$MPASS" "$INSTALL_DATABASE"; then
475 prompt_input openemr
/no_configure_mysql_database high ret_result
476 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL database already exists)"
477 configure_flag
=false
;
479 # Check for OpenEMR user in mysql.user, if exist then will not configure
480 USER
=$
(mysql
-s -u root
-h localhost
--password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
481 if [ "$USER" == "$INSTALL_USER" ]; then
482 prompt_input openemr
/no_configure_mysql_user high ret_result
483 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL user already exists)"
484 configure_flag
=false
;
487 #the mysql root password didn't work, so do not configure OpenEMR
488 log_only
"Will install OpenEMR, however will not automatically configure OpenEMR. (root password did not work)"
489 configure_flag
=false
;
492 #go to openemr directory
496 chown
-Rf root
:root
$OPENEMR
498 #INSTALL/CONFIGURE OPENEMR
500 if $configure_flag; then
501 log_only
"Installing/Configuring OpenEMR..."
503 log_only
"Installing OpenEMR ..."
506 # Set file and directory permissions (note use default site directory for new install)
507 chmod 666 $SITEDIR/default
/sqlconf.php
508 chown
$WEB_GROUP.
$WEB_USER $OPENEMR/interface
/modules
/zend_modules
/config
/application.config.php
509 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/documents
510 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/edi
511 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/era
512 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/library
/freeb
513 chown
-R $WEB_GROUP.
$WEB_USER $SITEDIR/default
/letter_templates
514 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/cache
515 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/interface
/main
/calendar
/modules
/PostCalendar
/pntemplates
/compiled
516 chown
-R $WEB_GROUP.
$WEB_USER $OPENEMR/gacl
/admin
/templates_c
518 if $configure_flag; then
519 # Create a random password for the openemr mysql user
520 password
=$
(makepasswd
--char=12)
522 # openemr installation VARIABLES
523 if [ "$MPASS" == "" ] ; then
524 rootpass
="rootpass=BLANK" #MySQL server root password
526 rootpass
="rootpass=$MPASS" #MySQL server root password
528 login
="login=$INSTALL_USER" #username to MySQL openemr database
529 pass
="pass=$password" #password to MySQL openemr database
530 dbname
="dbname=$INSTALL_DATABASE" #MySQL openemr database name
535 sed -e 's@^exit;@ @' <$INST >$INSTTEMP
536 php
-f $INSTTEMP $rootpass $login $pass $dbname >> $LOG 2>&1
539 #remove global permission to all setup scripts
540 chmod 600 $OPENEMR/acl_setup.php
541 chmod 600 $OPENEMR/acl_upgrade.php
542 chmod 600 $OPENEMR/sl_convert.php
543 chmod 600 $OPENEMR/setup.php
544 chmod 600 $OPENEMR/sql_upgrade.php
545 chmod 600 $OPENEMR/ippf_upgrade.php
546 chmod 600 $OPENEMR/gacl
/setup.php
548 log_only
"Done configuring OpenEMR"
551 if $configure_flag; then
552 prompt_input openemr
/success_install_config high ret_result
553 log_only
"You can now use OpenEMR by browsing to:"
554 log_only
"http://localhost/openemr"
555 log_only
"user is 'admin' and password is 'pass'"
556 log_only
"See the openemr man page for further instructions:"
557 log_only
"type 'man openemr' at command line"
559 prompt_input openemr
/success_install high ret_result
560 log_only
"You can now configure OpenEMR by browsing to:"
561 log_only
"http://localhost/openemr"
562 log_only
"See the openemr man page for further instructions:"
563 log_only
"type 'man openemr' at command line"
566 #update config file, change process to complete and remove plan and pass
567 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
568 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
575 abort-upgrade|abort-remove|abort-deconfigure
)
577 echo "postinst asked to do $1"
581 echo "postinst called with unknown argument \`$1'" >&2