couple more fixes in prior transactions improvements
[openemr.git] / contrib / util / express.php
blobb3f3319e180daa5fbae9ccde4e43cbddd5d6f756
1 <html>
2 <head>
3 <title>
4 </title>
5 </head>
6 <body>
7 <?php
9 // This exit is to avoid malicious use of this script.
10 // (before running this script, comment out the exit command)
11 // (when you are done running the script, remember to uncomment the exit command)
12 exit;
14 if (!$_POST['submit']) {
16 <form method=post>
17 <p>
18 This script will take the name that you give and create an OpenEMR database with this as the database name, username, password, group name. It will also rename the directory this OpenEMR installation is under to the new name. THIS ONLY WORKS WITH XAMPP AND HAS VERY LIMITED TESTING.
19 </p>
20 <p>
21 Make sure that you close Text Editors, Windows Explorer, etc... as these things may stop directory rename that occurs in this script.
22 </p>
23 <p>
24 Enter the name you wish to use for this OpenEMR installation.
25 </p>
26 <input type=text name=newname>
27 <input type=submit name=submit value=submit>
28 </form>
29 <?php
30 exit(0);
32 if ($_POST['submit']) {
33 $newname = $_POST['newname'];
34 //handle the database stuff
35 $dumpfile = "sql/database.sql";
36 $icd9 = "sql/icd9.sql";
37 $conffile = "library/sqlconf.php";
38 $server = "localhost";
39 $port = "3306";
40 $dbname = $newname;
41 $root = "root";
42 $rootpass = "";
43 $login = $newname;
44 $pass = $newname;
45 $loginhost = "localhost";
47 //setup of database
49 echo "Connecting to MySQL Server...\n";
50 flush();
51 if ($server == "localhost")
52 $dbh = mysql_connect("$server","$root","$rootpass");
53 else
54 $dbh = mysql_connect("$server:$port","$root","$rootpass");
55 if ($dbh == FALSE) {
56 echo "ERROR. Check your login credentials.\n";
57 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
58 break;
60 else
61 echo "OK.<br>\n";
62 echo "Creating database...\n";
63 flush();
64 if (mysql_query("create database $dbname",$dbh) == FALSE) {
65 echo "ERROR. Check your login credentials.\n";
66 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
67 break;
69 else
70 echo "OK.<br>\n";
71 echo "Creating user with permissions for database...\n";
72 flush();
73 if (mysql_query("GRANT ALL PRIVILEGES ON $dbname.* TO '$login'@'$loginhost' IDENTIFIED BY '$pass'",$dbh) == FALSE) {
74 echo "ERROR when granting privileges to the specified user.\n";
75 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
76 echo "ERROR.\n";
77 break;
79 else
80 echo "OK.<br>\n";
81 echo "Reconnecting as new user...\n";
82 mysql_close($dbh);
84 else
85 echo "Connecting to MySQL Server...\n";
87 if ($server == "localhost")
88 $dbh = mysql_connect("$server","$login","$pass");
89 else
90 $dbh = mysql_connect("$server:$port","$login","$pass");
92 if ($dbh == FALSE) {
93 echo "ERROR. Check your login credentials.\n";
94 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
95 break;
97 else
98 echo "OK.<br>\n";
99 echo "Opening database...";
100 flush();
101 if (mysql_select_db("$dbname",$dbh) == FALSE) {
102 echo "ERROR. Check your login credentials.\n";
103 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
104 break;
106 else
107 echo "OK.<br>\n";
108 flush();
109 if ($upgrade != 1) {
110 echo "Creating initial tables...\n";
111 mysql_query("USE $dbname",$dbh);
112 flush();
113 $fd = fopen($dumpfile, 'r');
114 if ($fd == FALSE) {
115 echo "ERROR. Could not open dumpfile '$dumpfile'.\n";
116 flush();
117 break;
119 $query = "";
120 $line = "";
121 while (!feof ($fd)){
122 $line = fgets($fd,1024);
123 $line = rtrim($line);
124 if (substr($line,0,2) == "--") // Kill comments
125 continue;
126 if (substr($line,0,1) == "#") // Kill comments
127 continue;
128 if ($line == "")
129 continue;
130 $query = $query.$line; // Check for full query
131 $chr = substr($query,strlen($query)-1,1);
132 if ($chr == ";") { // valid query, execute
133 $query = rtrim($query,";");
134 mysql_query("$query",$dbh);
135 $query = "";
138 echo "OK<br>\n";
139 fclose($fd);
140 flush();
141 echo "Adding Initial User...\n";
142 flush();
143 $iuser = "admin";
144 $iuname = "admin";
145 $igroup = $newname;
146 //echo "INSERT INTO groups VALUES (1,'$igroup','$iuser')<br>\n";
147 if (mysql_query("INSERT INTO groups (id, name, user) VALUES (1,'$igroup','$iuser')") == FALSE) {
148 echo "ERROR. Could not run queries.\n";
149 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
150 flush();
151 break;
153 //// ViCareplus : As per NIST standard, SHA1 hash/digest of 'pass' is used
154 if (mysql_query("INSERT INTO users (id, username, password, authorized, lname,fname) VALUES (1,'$iuser','9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684',1,'$iuname','')") == FALSE) {
155 echo "ERROR. Could not run queries.\n";
156 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
157 flush();
158 break;
160 echo "OK<br>\n";
161 flush();
163 //Now write sqlconf file
164 echo "Writing SQL Configuration to disk...\n";
165 @touch($conffile); // php bug
166 $fd = @fopen($conffile, 'w');
167 if ($fd == FALSE) {
168 echo "ERROR. Could not open config file '$conffile' for writing.\n";
169 flush();
170 break;
172 $string = "<?php
174 // OpenEMR
175 // MySQL Config
176 // Referenced from sql.inc
180 $it_died = 0; //fmg: variable keeps running track of any errors
182 fwrite($fd,$string) or $it_died++;
183 fwrite($fd,"\$host\t= '$host';\n") or $it_died++;
184 fwrite($fd,"\$port\t= '$port';\n") or $it_died++;
185 fwrite($fd,"\$login\t= '$login';\n") or $it_died++;
186 fwrite($fd,"\$pass\t= '$pass';\n") or $it_died++;
187 fwrite($fd,"\$dbase\t= '$dbname';\n") or $it_died++;
190 $string = '
192 $sqlconf = array();
193 $sqlconf["host"]= $host;
194 $sqlconf["port"] = $port;
195 $sqlconf["login"] = $login;
196 $sqlconf["pass"] = $pass;
197 $sqlconf["dbase"] = $dbase;
198 //////////////////////////
199 //////////////////////////
200 //////////////////////////
201 //////DO NOT TOUCH THIS///
202 $config = 1; /////////////
203 //////////////////////////
204 //////////////////////////
205 //////////////////////////
208 ?><?php // done just for coloring
210 fwrite($fd,$string) or $it_died++;
212 //it's rather irresponsible to not report errors when writing this file.
213 if ($it_died != 0) {
214 echo "ERROR. Couldn't write $it_died lines to config file '$conffile'.\n";
215 flush();
216 break;
218 fclose($fd);
220 //Now, use new name and fix globals.php and rename directory!!!
221 $d = getcwd();
222 $dn = dirname($d);
223 $contents = file_get_contents($d.'/interface/globals.php');
224 $contents = preg_replace('/\$webserver_root\s+=\s+[\"\'].*?[\"\'];/',
225 "\$webserver_root = '".$dn."/".$newname."';",$contents);
226 $contents = preg_replace('/\$web_root\s+=\s+[\"\'].*?[\"\'];/',
227 "\$web_root = '/".$newname."';",$contents);
228 file_put_contents($d.'/interface/globals.php',$contents);
229 if (rename($d,$dn.'/'.$newname)) {
230 echo "<br/><a href='http://localhost/".$newname."'>click here</a>";
234 </body>
235 </html>