more organization of autoloaded files (#424)
[openemr.git] / contrib / util / de_identification_upgrade.php
blobe035095b2ecf6c2df9c9f85a592abae48ed23791
1 <?php
2 /********************************************************************************\
3 * Copyright (C) ViCarePlus, Visolve (vicareplus_engg@visolve.com) *
4 * *
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. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 /********************************************************************************/
20 // Disable PHP timeout. This will not work in safe mode.
21 ini_set('max_execution_time', '0');
23 $ignoreAuth = true; // no login required
25 //set de_identification_config to 1 to run the de_identification_upgrade script
26 $de_identification_config = 0;
28 require_once('../../interface/globals.php');
30 function tableExists_de($tblname) {
31 $row = sqlQuery("SHOW TABLES LIKE '$tblname'");
32 if (empty($row)) return false;
33 return true;
36 function upgradeFromSqlFile_de($filename) {
37 global $webserver_root;
39 flush();
40 echo "<font color='green'>"; echo xl('Processing'); echo " ".$filename."...</font><br />\n";
42 $fullname = "$webserver_root/sql/$filename";
44 $fd = fopen($fullname, 'r');
45 if ($fd == FALSE) {
46 echo xl("Error, unable to open file"); echo " ".$fullname."\n";
47 flush();
48 break;
51 $query = "";
52 $line = "";
53 $skipping = false;
54 $proc = 0;
56 while (!feof ($fd)){
57 $line = fgets($fd, 2048);
58 $line = rtrim($line);
60 if (preg_match('/^\s*--/', $line)) continue;
61 if ($line == "") continue;
63 if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
64 $skipping = tableExists_de($matches[1]);
65 if ($skipping) echo "<font color='green'>"; echo xl('Skipping section'); echo " ".$line."</font><br />\n";
67 else if (preg_match('/^#EndIf/', $line)) {
68 $skipping = false;
70 if (preg_match('/^\s*#/', $line)) continue;
71 if ($skipping) continue;
73 if($proc == 1) $query .= "\n";
74 $query = $query . $line;
76 if (substr($query, -1) == '$')
78 $query = rtrim($query, '$');
79 if($proc == 0)
80 $proc = 1;
81 else
83 $proc = 0; //executes procedures and functions
84 if (!sqlStatement($query))
86 echo "<font color='red'>"; echo xl("The above statement failed"); echo ": " .
87 getSqlLastError() . "<br />"; echo xl("Upgrading will continue"); echo ".<br /></font>\n";
89 $query = '';
94 if (substr($query, -1) == ';'and $proc == 0) {
95 $query = rtrim($query, ';');
96 echo "$query<br />\n"; //executes sql statements
97 if (!sqlStatement($query)) {
98 echo "<font color='red'>"; echo xl("The above statement failed"); echo ": " .
99 getSqlLastError() . "<br />"; echo xl("Upgrading will continue"); echo ".<br /></font>\n";
101 $query = '';
104 flush();
105 } // end function
108 $sqldir = "$webserver_root/sql";
109 $dh = opendir($sqldir);
110 if (! $dh) die( xl("Cannot read","e")." ".$sqldir);
111 closedir($dh);
113 <html>
114 <head>
115 <title><?php xl('OpenEMR Database Upgrade','e'); ?></title>
116 <link rel='STYLESHEET' href='../../interface/themes/style_sky_blue.css'>
117 </head>
118 <body> <br>
119 <center>
120 <span class='title'><?php xl('OpenEMR Database Upgrade for De-identification','e'); ?></span>
121 <br>
122 </center>
123 <?php
124 if (!empty($_POST['form_submit'])) {
125 upgradeFromSqlFile_de("database_de_identification.sql");
127 // grant file privilege to user
129 $dbh = $GLOBALS['dbh'];
131 if ($dbh == FALSE) {
132 echo "\n";
133 echo "<p>".getSqlLastError()." (#".getSqlLastErrorNo().")\n";
134 break;
135 } $login=$sqlconf["login"]; $loginhost=$sqlconf["host"];
136 generic_sql_select_db($sqlconf['dbase']) or die(getSqlLastError());
137 if (sqlStatement("GRANT FILE ON *.* TO '$login'@'$loginhost'") == FALSE) {
138 echo xl("Error when granting file privilege to the OpenEMR user."); echo "\n";
139 echo "<p>".getSqlLastError()." (#".getSqlLastErrorNo().")\n";
140 echo xl("Error"); echo "\n";
141 break;
143 else
144 echo "<font color='green'>"; echo xl("File privilege granted to OpenEMR user."); echo "<br></font>\n";
146 echo "<p><font color='green'>"; echo xl("Database upgrade finished."); echo "</font></p>\n";
147 echo "<p><font color='red'>"; echo xl("Please restart the apache server before playing with de-identification"); echo "</font></p>\n";
148 echo "<p><font color='red'>"; echo xl("Please set de_identification_config variable back to zero"); echo "</font></p>\n"; echo "</body></html>\n";
149 sqlClose($dbh);
150 exit();
154 <script language="JavaScript">
155 function form_validate()
157 if(document.forms[0].root_user_name.value == "")
159 alert("<?php echo xl('Enter Database root Username');?>");
160 return false;
162 /*if(document.forms[0].root_user_pass.value == "")
164 alert("<?php echo xl('Enter Database root Password');?>");
165 return false;
167 return true;
169 </script>
171 <center>
172 <form method='post' action='de_identification_upgrade.php' onsubmit="return form_validate();">
173 </br>
174 <p><?php if($de_identification_config != 1)
176 echo "<p><font color='red'>";
177 echo xl("Please set"); echo " 'de_identification_config' "; echo xl("variable to one to run de-identification upgrade script"); echo "</br></br>";
178 echo "([OPENEMR]/contrib/util/de_identification_upgrade.php)";
180 else
182 xl('Upgrades the OpenEMR database to include Procedures, Functions and tables needed for De-identification process','e');?></p></br>
183 <table class="de_id_upgrade_login" align="center">
184 <tr><td>&nbsp;</td><td colspan=3 align=center>&nbsp;</td><td>&nbsp;</td></tr>
185 <tr valign="top">
186 <td>&nbsp;</td>
187 <td><?php xl('Enter Database root Username','e'); ?></td>
188 <td>:</td>
189 <td> <input type='text' size='20' name='root_user_name' id='root_user_name'
190 value= "" title="<?php xl('Enter Database root Username','e'); ?>" /> </td>
191 <td>&nbsp;</td>
192 </tr>
193 <tr valign="top">
194 <td>&nbsp;</td>
195 <td><?php xl('Enter Database root Password','e'); ?></td>
196 <td>:</td>
197 <td><input type='password' size='20' name='root_user_pass' id='root_user_pass'
198 value= "" title="<?php xl('Enter Database root Password','e'); ?>" /> </td>
199 <td>&nbsp;</td>
200 </tr>
201 <tr><td>&nbsp;</td><td colspan=3 align=center>&nbsp;</td><td>&nbsp;</td></tr>
203 </table>
204 <p><input type='submit' name='form_submit' value="<?php xl('Upgrade Database','e');?>" /></p>
205 <?php } ?>
206 </form>
207 </center>
208 </body>
209 </html>