3 // Copyright (C) 2008-2010 Rod Roark <rod@sunsetsystems.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This may be run after an upgraded OpenEMR has been installed.
11 // Its purpose is to upgrade the MySQL OpenEMR database as needed
12 // for the new release.
14 /* @TODO add language selection. needs RTL testing */
16 $GLOBALS['ongoing_sql_upgrade'] = true;
18 if (php_sapi_name() === 'cli') {
19 // setting for when running as command line script
20 // need this for output to be readable when running as command line
21 $GLOBALS['force_simple_sql_upgrade'] = true;
24 // Checks if the server's PHP version is compatible with OpenEMR:
25 require_once(__DIR__
. "/src/Common/Compatibility/Checker.php");
26 $response = OpenEMR\Common\Compatibility\Checker
::checkPhpVersion();
27 if ($response !== true) {
28 die(htmlspecialchars($response));
31 @ini_set
('zlib.output_compression', 0);
32 @ini_set
('implicit_flush', 1);
34 // Disable PHP timeout. This will not work in safe mode.
35 @ini_set
('max_execution_time', '0');
36 if (ob_get_level() === 0) {
40 $ignoreAuth = true; // no login required
41 $sessionAllowWrite = true;
42 $GLOBALS['connection_pooling_off'] = true; // force off database connection pooling
44 require_once('interface/globals.php');
45 require_once('library/sql_upgrade_fx.php');
47 use OpenEMR\Common\Csrf\CsrfUtils
;
48 use OpenEMR\Core\Header
;
49 use OpenEMR\Services\VersionService
;
52 $GLOBALS["enable_auditlog"] = 0;
55 $sqldir = "$webserver_root/sql";
56 $dh = opendir($sqldir);
58 die("Cannot read $sqldir");
61 while (false !== ($sfname = readdir($dh))) {
62 if ($sfname[0] === '.') {
66 if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
67 $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
68 $versions[$version] = $sfname;
75 $res2 = sqlStatement("select * from lang_languages where lang_description = ?", array($GLOBALS['language_default']));
76 for ($iter = 0; $row = sqlFetchArray($res2); $iter++
) {
77 $result2[$iter] = $row;
80 if (count($result2) == 1) {
81 $defaultLangID = $result2[0]["lang_id"];
82 $defaultLangName = $result2[0]["lang_description"];
83 $direction = (int)$result2[0]["lang_is_rtl"] === 1 ?
'rtl' : 'ltr';
85 //default to english if any problems
87 $defaultLangName = "English";
90 $_SESSION['language_choice'] = $defaultLangID;
91 $_SESSION['language_direction'] = $direction;
92 CsrfUtils
::setupCsrfKey();
93 session_write_close();
95 header('Content-type: text/html; charset=utf-8');
98 <!-- @todo Adding DOCTYPE html breaks BS width
/height percentages
. Why?
-->
101 <title
>OpenEMR Database Upgrade
</title
>
102 <?php Header
::setupHeader(); ?
>
103 <link rel
="shortcut icon" href
="public/images/favicon.ico" />
106 let processProgress
= 0;
108 let serverPaused
= 0;
109 // recursive long polling where ending is based
110 // on global doPoll true or false.
111 // added a forcePollOff parameter to avoid polling from staying on indefinitely when updating from patch.sql
112 async
function serverStatus(version
= '', start
= 0, forcePollOff
= 0) {
114 let endMsg
= "<li class='text-success bg-light'>" +
115 <?php
echo xlj("End watching server processes for upgrade version"); ?
> +
" " + currentVersion +
"</li>";
117 currentVersion
= version
;
118 updateMsg
= "<li class='text-light bg-success'>" +
119 <?php
echo xlj("Start watching server processes for upgrade version"); ?
> +
" " + version +
"</li>";
123 let url
= "library/ajax/sql_server_status.php?poll=" +
encodeURIComponent(currentVersion
);
124 let data
= new FormData
;
125 data
.append("csrf_token_form", <?php
echo js_escape(CsrfUtils
::collectCsrfToken('sqlupgrade')); ?
>);
126 data
.append("poll", currentVersion
);
128 let response
= await
fetch(url
, {
133 if (response
.status
=== 502) {
134 progressStatus("<li class='text-light bg-danger'> ERROR: Restarting. " + response
.statusText
) +
"</li>";
135 // connection timeout, just reconnect
137 await
serverStatus();
139 } else if (response
.status
!== 200) {
141 progressStatus("<li class='text-light bg-danger'> ERROR: " + response
.statusText
) +
"</li>";
142 // reconnect in one second
144 await
serverStatus();
148 let status
= await response
.text();
149 if (status
=== 'Internal Server Error') {
150 let errorMsg
= "<li class='text-light bg-danger'>" +
151 <?php
echo xlj("Stopping activity status checks. Internal Server Error"); ?
> +
"</li>";
152 progressStatus(errorMsg
);
156 if (status
=== 'Authentication Error') {
157 let errorMsg
= "<li class='text-light bg-danger'>" +
158 <?php
echo xlj("Stopping status checks. Csrf Error. No harm to upgrade and will continue."); ?
> +
"</li>";
159 progressStatus(errorMsg
);
164 progressStatus(updateMsg
);
169 if (forcePollOff
=== 1) {
172 // display to screen div
174 progressStatus(status
);
179 await
serverStatus();
181 progressStatus(endMsg
);
186 * Focus scrolls to bottom of view
188 function doScrolls() {
189 let serverStatus
= document
.getElementById("serverStatus");
190 let isServerBottom
= serverStatus
.scrollHeight
- serverStatus
.clientHeight
<= serverStatus
.scrollTop +
1;
191 let processDetails
= document
.getElementById("processDetails");
192 let isDetailsBottom
= processDetails
.scrollHeight
- processDetails
.clientHeight
<= processDetails
.scrollTop +
1;
194 if(!isServerBottom
) {
195 serverStatus
.scrollTop
= serverStatus
.scrollHeight
- serverStatus
.clientHeight
;
196 }if(!isDetailsBottom
) {
197 processDetails
.scrollTop
= processDetails
.scrollHeight
- processDetails
.clientHeight
;
201 function progressStatus(msg
= '') {
202 let eventList
= document
.getElementById('status-message');
203 let progressEl
= document
.getElementById('progress');
205 progressEl
.style
.width
= processProgress +
"%";
206 progressEl
.innerHTML
= processProgress +
"%" +
" v" + currentVersion
;
208 eventList
.innerHTML +
= msg
;
213 function setWarnings(othis
) {
214 if (othis
.value
< '5.0.0') {
215 document
.querySelector('.version-warning').classList
.remove("d-none");
217 document
.querySelector('.version-warning').classList
.add("d-none");
221 function pausePoll(othis
) {
222 if (serverPaused
=== 0 && doPoll
=== 1) {
223 let alertMsg
= "<li class='text-dark bg-warning'>" +
224 <?php
echo xlj("Paused status checks."); ?
> +
"</li>";
225 progressStatus(alertMsg
);
228 document
.querySelector('.pause-server').classList
.remove("btn-success");
229 document
.querySelector('.pause-server').classList
.add("btn-warning");
230 } else if (serverPaused
=== 1) {
231 let alertMsg
= "<li class='text-dark bg-success'>" +
232 <?php
echo xlj("Resuming status checks."); ?
> +
"</li>";
233 progressStatus(alertMsg
);
237 document
.querySelector('.pause-server').classList
.remove("btn-warning");
238 document
.querySelector('.pause-server').classList
.add("btn-success");
244 <div
class="container my-3">
247 <h2
><?php
echo xlt("OpenEMR Database Upgrade"); ?
></h2
>
251 <form
class="form-inline" method
='post' action
='sql_upgrade.php'>
252 <div
class="form-group mb-1">
253 <label
><?php
echo xlt("Please select the prior release you are converting from"); ?
>:</label
>
254 <select
class='mx-3 form-control' name
='form_old_version' onchange
="setWarnings(this)">
256 $cnt_versions = count($versions);
257 foreach ($versions as $version => $filename) {
259 echo " <option value='$version'";
260 // Defaulting to most recent version or last version in list.
261 if ($cnt_versions === 0) {
264 echo ">$version</option>\n";
268 <span
><?php
echo xlt("If you are unsure or were using a development version between two releases, then choose the older of possible releases"); ?
>.</span
>
270 <span
class="alert alert-warning text-danger version-warning d-none">
271 <?php
echo xlt("If you are upgrading from a version below 5.0.0 to version 5.0.0 or greater, do note that this upgrade can take anywhere from several minutes to several hours (you will only see a whitescreen until it is complete; do not stop the script before it is complete or you risk corrupting your data)"); ?
>.
273 <button type
='submit' class='btn btn-primary btn-transmit' name
='form_submit' value
='Upgrade Database'>
274 <?php
echo xlt("Upgrade Database"); ?
>
276 <div
class="btn-group">
279 <!-- server status card
-->
280 <div
class="card card-header">
281 <span
class="btn-group">
282 <a
class="btn btn-success pause-server fa fa-pause float-left" onclick
="pausePoll(this)" title
="<?php echo xla("Click to start
or end sql server activity checks
."); ?>"></a
>
283 <a
class="btn btn-primary w-100" data
-toggle
="collapse" href
="#serverStatus">
284 <?php
echo xlt("Server Status"); ?
><i
class="fa fa-angle-down rotate-icon float-right"></i
>
288 <div id
="serverStatus" class="card card-body pb-2 h-25 overflow-auto collapse show">
289 <div
class="bg-light text-dark">
290 <ul id
="status-message"></ul
>
294 <!-- collapse place holder
for upgrade processing on submit
. -->
295 <div
class="card card-header">
296 <a
class="btn btn-primary" data
-toggle
="collapse" href
="#processDetails">
297 <?php
echo xlt("Processing Details"); ?
><i
class="fas fa-angle-down rotate-icon float-right"></i
>
299 <div id
="progress-div" class="bg-secondary float-left">
300 <div id
="progress" class="mt-1 progress-bar bg-success" style
="height:1.125rem;width:0;"></div
>
303 <div id
='processDetails' class='card card-body pb-2 h-50 overflow-auto collapse show'>
304 <div
class='bg-light text-dark'>
305 <?php
if (!empty($_POST['form_submit'])) {
306 $form_old_version = $_POST['form_old_version'];
308 foreach ($versions as $version => $filename) {
309 if (strcmp($version, $form_old_version) < 0) {
312 // set polling version and start
313 flush_echo("<script>serverStatus(" . js_escape($version) . ", 1);</script>");
314 upgradeFromSqlFile($filename);
316 sleep(2); // fixes odd bug, where if the sql upgrade goes to fast, then the polling does not stop
317 flush_echo("<script>processProgress = 100;doPoll = 0;</script>");
320 if (!empty($GLOBALS['ippf_specific'])) {
321 // Upgrade custom stuff for IPPF.
322 upgradeFromSqlFile('ippf_upgrade.sql');
325 if ((!empty($v_realpatch)) && ($v_realpatch != "") && ($v_realpatch > 0)) {
326 // This release contains a patch file, so process it.
327 echo "<script>serverStatus('Patch', 0, 1);</script>";
328 upgradeFromSqlFile('patch.sql');
332 echo "<p class='text-success'>" . xlt("Updating global configuration defaults") . "..." . "</p><br />\n";
333 $skipGlobalEvent = true; //use in globals.inc.php script to skip event stuff
334 require_once("library/globals.inc.php");
335 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
336 foreach ($grparr as $fldid => $fldarr) {
337 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
338 if (is_array($fldtype) ||
(substr($fldtype, 0, 2) !== 'm_')) {
339 $row = sqlQuery("SELECT count(*) AS count FROM globals WHERE gl_name = '$fldid'");
340 if (empty($row['count'])) {
341 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
342 "VALUES ( '$fldid', '0', '$flddef' )");
348 echo "<p class='text-success'>" . xlt("Updating Access Controls") . "..." . "</p><br />\n";
349 require("acl_upgrade.php");
352 $versionService = new VersionService();
353 $currentVersion = $versionService->fetch();
354 $desiredVersion = $currentVersion;
355 $desiredVersion['v_database'] = $v_database;
356 $desiredVersion['v_tag'] = $v_tag;
357 $desiredVersion['v_realpatch'] = $v_realpatch;
358 $desiredVersion['v_patch'] = $v_patch;
359 $desiredVersion['v_minor'] = $v_minor;
360 $desiredVersion['v_major'] = $v_major;
362 $canRealPatchBeApplied = $versionService->canRealPatchBeApplied($desiredVersion);
363 $line = "Updating version indicators";
365 if ($canRealPatchBeApplied) {
366 $line = $line . ". " . xlt("Patch was also installed, updating version patch indicator");
369 echo "<p class='text-success'>" . $line . "...</p><br />\n";
370 $versionService->update($desiredVersion);
372 echo "<p><p class='text-success'>" . xlt("Database and Access Control upgrade finished.") . "</p></p>\n";
373 echo "</div></body></html>\n";