php8.1 fixes (#5676)
[openemr.git] / contrib / util / de_identification_upgrade.php
blobe94d674c2e83ed3ea2c8943200d7f7318c0f0cc9
1 <?php
3 /********************************************************************************\
4 * Copyright (C) ViCarePlus, Visolve (vicareplus_engg@visolve.com) *
5 * *
6 * This program is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU General Public License *
8 * as published by the Free Software Foundation; either version 2 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 /********************************************************************************/
21 // Disable PHP timeout. This will not work in safe mode.
22 ini_set('max_execution_time', '0');
24 // $ignoreAuth = true; // no login required
26 //set de_identification_config to 1 to run the de_identification_upgrade script
27 $de_identification_config = 0;
29 require_once('../../interface/globals.php');
31 use OpenEMR\Common\Csrf\CsrfUtils;
33 function tableExists_de($tblname)
35 $row = sqlQuery("SHOW TABLES LIKE '" . add_escape_custom($tblname) . "'");
36 if (empty($row)) {
37 return false;
40 return true;
43 function upgradeFromSqlFile_de($filename)
45 global $webserver_root;
47 flush();
48 echo "<font color='green'>";
49 echo xlt('Processing');
50 echo " " . text($filename) . "...</font><br />\n";
52 $fullname = "$webserver_root/sql/" . convert_safe_file_dir_name($filename);
54 $fd = fopen($fullname, 'r');
55 if ($fd == false) {
56 echo xlt("Error, unable to open file");
57 echo " " . text($fullname) . "\n";
58 flush();
59 exit();
62 $query = "";
63 $line = "";
64 $skipping = false;
65 $proc = 0;
67 while (!feof($fd)) {
68 $line = fgets($fd, 2048);
69 $line = rtrim($line);
71 if (preg_match('/^\s*--/', $line)) {
72 continue;
75 if ($line == "") {
76 continue;
79 if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
80 $skipping = tableExists_de($matches[1]);
81 if ($skipping) {
82 echo "<font color='green'>";
85 echo xlt('Skipping section');
86 echo " " . text($line) . "</font><br />\n";
87 } elseif (preg_match('/^#EndIf/', $line)) {
88 $skipping = false;
91 if (preg_match('/^\s*#/', $line)) {
92 continue;
95 if ($skipping) {
96 continue;
99 if ($proc == 1) {
100 $query .= "\n";
103 $query = $query . $line;
105 if (substr($query, -1) == '$') {
106 $query = rtrim($query, '$');
107 if ($proc == 0) {
108 $proc = 1;
109 } else {
110 $proc = 0; //executes procedures and functions
111 if (!sqlStatement($query)) {
112 echo "<font color='red'>";
113 echo xlt("The above statement failed"); echo ": " .
114 text(getSqlLastError()) . "<br />";
115 echo xlt("Upgrading will continue");
116 echo ".<br /></font>\n";
119 $query = '';
123 if (substr($query, -1) == ';' and $proc == 0) {
124 $query = rtrim($query, ';');
125 echo text($query) . "<br />\n"; //executes sql statements
126 if (!sqlStatement($query)) {
127 echo "<font color='red'>";
128 echo xlt("The above statement failed"); echo ": " .
129 text(getSqlLastError()) . "<br />";
130 echo xlt("Upgrading will continue");
131 echo ".<br /></font>\n";
134 $query = '';
138 flush();
139 } // end function
142 $sqldir = "$webserver_root/sql";
143 $dh = opendir($sqldir);
144 if (! $dh) {
145 die(xlt("Cannot read") . " " . text($sqldir));
148 closedir($dh);
150 <html>
151 <head>
152 <title><?php echo xlt('OpenEMR Database Upgrade'); ?></title>
153 <link rel='STYLESHEET' href='../../interface/themes/style_sky_blue.css'>
154 </head>
155 <body> <br />
156 <center>
157 <span class='title'><?php echo xlt('OpenEMR Database Upgrade for De-identification'); ?></span>
158 <br />
159 </center>
160 <?php
161 if (!empty($_POST['form_submit'])) {
162 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
163 CsrfUtils::csrfNotVerified();
166 upgradeFromSqlFile_de("database_de_identification.sql");
168 // grant file privilege to user
170 $dbh = $GLOBALS['dbh'];
172 if ($dbh == false) {
173 echo "\n";
174 echo "<p>" . text(getSqlLastError()) . " (#" . text(getSqlLastErrorNo()) . ")\n";
175 exit();
176 } $login = $sqlconf["login"];
177 $loginhost = $sqlconf["host"];
178 generic_sql_select_db($sqlconf['dbase']) or die(text(getSqlLastError()));
179 if (sqlStatement("GRANT FILE ON *.* TO '$login'@'$loginhost'") == false) {
180 echo xlt("Error when granting file privilege to the OpenEMR user.");
181 echo "\n";
182 echo "<p>" . text(getSqlLastError()) . " (#" . text(getSqlLastErrorNo()) . ")\n";
183 echo xlt("Error");
184 echo "\n";
185 exit();
186 } else {
187 echo "<font color='green'>";
190 echo xlt("File privilege granted to OpenEMR user.");
191 echo "<br /></font>\n";
193 echo "<p><font color='green'>";
194 echo xlt("Database upgrade finished.");
195 echo "</font></p>\n";
196 echo "<p><font color='red'>";
197 echo xlt("Please restart the apache server before playing with de-identification");
198 echo "</font></p>\n";
199 echo "<p><font color='red'>";
200 echo xlt("Please set de_identification_config variable back to zero");
201 echo "</font></p>\n";
202 echo "</body></html>\n";
203 sqlClose($dbh);
204 exit();
208 <script>
209 function form_validate()
211 if(document.forms[0].root_user_name.value == "")
213 alert("<?php echo xls('Enter Database root Username');?>");
214 return false;
216 /*if(document.forms[0].root_user_pass.value == "")
218 alert("<?php echo xls('Enter Database root Password');?>");
219 return false;
221 return true;
223 </script>
225 <center>
226 <form method='post' action='de_identification_upgrade.php' onsubmit="return form_validate();">
227 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
228 <br />
229 <p><?php if ($de_identification_config != 1) {
230 echo "<p><font color='red'>";
231 echo xlt("Please set");
232 echo " 'de_identification_config' ";
233 echo xlt("variable to one to run de-identification upgrade script");
234 echo "<br /><br />";
235 echo "([OPENEMR]/contrib/util/de_identification_upgrade.php)";
236 } else {
237 echo xlt('Upgrades the OpenEMR database to include Procedures, Functions and tables needed for De-identification process');?></p><br />
238 <table class="de_id_upgrade_login" align="center">
239 <tr><td>&nbsp;</td><td colspan=3 align=center>&nbsp;</td><td>&nbsp;</td></tr>
240 <tr valign="top">
241 <td>&nbsp;</td>
242 <td><?php echo xlt('Enter Database root Username'); ?></td>
243 <td>:</td>
244 <td> <input type='text' size='20' name='root_user_name' id='root_user_name'
245 value= "" title="<?php echo xla('Enter Database root Username'); ?>" /> </td>
246 <td>&nbsp;</td>
247 </tr>
248 <tr valign="top">
249 <td>&nbsp;</td>
250 <td><?php echo xlt('Enter Database root Password'); ?></td>
251 <td>:</td>
252 <td><input type='password' size='20' name='root_user_pass' id='root_user_pass'
253 value= "" title="<?php echo xlt('Enter Database root Password'); ?>" /> </td>
254 <td>&nbsp;</td>
255 </tr>
256 <tr><td>&nbsp;</td><td colspan=3 align=center>&nbsp;</td><td>&nbsp;</td></tr>
258 </table>
259 <p><input type='submit' name='form_submit' value="<?php echo xla('Upgrade Database');?>" /></p>
260 <?php } ?>
261 </form>
262 </center>
263 </body>
264 </html>