do not allow the log backup to go to php temp dir
[openemr.git] / contrib / util / express.php
blobf478c4e16cce1e4e9f990eea32f73cf3b998d3de
1 <html>
2 <head>
3 <title>
4 </title>
5 </head>
6 <body>
7 <?php
8 if (!$_POST['submit']) {
9 ?>
10 <form method=post>
11 <p>
12 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.
13 </p>
14 <p>
15 Make sure that you close Text Editors, Windows Explorer, etc... as these things may stop directory rename that occurs in this script.
16 </p>
17 <p>
18 Enter the name you wish to use for this OpenEMR installation.
19 </p>
20 <input type=text name=newname>
21 <input type=submit name=submit value=submit>
22 </form>
23 <?php
24 exit(0);
26 if ($_POST['submit']) {
27 $newname = $_POST['newname'];
28 //handle the database stuff
29 $dumpfile = "sql/database.sql";
30 $icd9 = "sql/icd9.sql";
31 $conffile = "library/sqlconf.php";
32 $server = "localhost";
33 $port = "3306";
34 $dbname = $newname;
35 $root = "root";
36 $rootpass = "";
37 $login = $newname;
38 $pass = $newname;
39 $loginhost = "localhost";
41 //setup of database
43 echo "Connecting to MySQL Server...\n";
44 flush();
45 if ($server == "localhost")
46 $dbh = mysql_connect("$server","$root","$rootpass");
47 else
48 $dbh = mysql_connect("$server:$port","$root","$rootpass");
49 if ($dbh == FALSE) {
50 echo "ERROR. Check your login credentials.\n";
51 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
52 break;
54 else
55 echo "OK.<br>\n";
56 echo "Creating database...\n";
57 flush();
58 if (mysql_query("create database $dbname",$dbh) == FALSE) {
59 echo "ERROR. Check your login credentials.\n";
60 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
61 break;
63 else
64 echo "OK.<br>\n";
65 echo "Creating user with permissions for database...\n";
66 flush();
67 if (mysql_query("GRANT ALL PRIVILEGES ON $dbname.* TO '$login'@'$loginhost' IDENTIFIED BY '$pass'",$dbh) == FALSE) {
68 echo "ERROR when granting privileges to the specified user.\n";
69 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
70 echo "ERROR.\n";
71 break;
73 else
74 echo "OK.<br>\n";
75 echo "Reconnecting as new user...\n";
76 mysql_close($dbh);
78 else
79 echo "Connecting to MySQL Server...\n";
81 if ($server == "localhost")
82 $dbh = mysql_connect("$server","$login","$pass");
83 else
84 $dbh = mysql_connect("$server:$port","$login","$pass");
86 if ($dbh == FALSE) {
87 echo "ERROR. Check your login credentials.\n";
88 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
89 break;
91 else
92 echo "OK.<br>\n";
93 echo "Opening database...";
94 flush();
95 if (mysql_select_db("$dbname",$dbh) == FALSE) {
96 echo "ERROR. Check your login credentials.\n";
97 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
98 break;
100 else
101 echo "OK.<br>\n";
102 flush();
103 if ($upgrade != 1) {
104 echo "Creating initial tables...\n";
105 mysql_query("USE $dbname",$dbh);
106 flush();
107 $fd = fopen($dumpfile, 'r');
108 if ($fd == FALSE) {
109 echo "ERROR. Could not open dumpfile '$dumpfile'.\n";
110 flush();
111 break;
113 $query = "";
114 $line = "";
115 while (!feof ($fd)){
116 $line = fgets($fd,1024);
117 $line = rtrim($line);
118 if (substr($line,0,2) == "--") // Kill comments
119 continue;
120 if (substr($line,0,1) == "#") // Kill comments
121 continue;
122 if ($line == "")
123 continue;
124 $query = $query.$line; // Check for full query
125 $chr = substr($query,strlen($query)-1,1);
126 if ($chr == ";") { // valid query, execute
127 $query = rtrim($query,";");
128 mysql_query("$query",$dbh);
129 $query = "";
132 echo "OK<br>\n";
133 fclose($fd);
134 flush();
135 echo "Adding Initial User...\n";
136 flush();
137 $iuser = "admin";
138 $iuname = "admin";
139 $igroup = $newname;
140 //echo "INSERT INTO groups VALUES (1,'$igroup','$iuser')<br>\n";
141 if (mysql_query("INSERT INTO groups (id, name, user) VALUES (1,'$igroup','$iuser')") == FALSE) {
142 echo "ERROR. Could not run queries.\n";
143 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
144 flush();
145 break;
147 if (mysql_query("INSERT INTO users (id, username, password, authorized, lname,fname) VALUES (1,'$iuser','1a1dc91c907325c69271ddf0c944bc72',1,'$iuname','')") == FALSE) {
148 echo "ERROR. Could not run queries.\n";
149 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
150 flush();
151 break;
153 echo "OK<br>\n";
154 flush();
156 //Now write sqlconf file
157 echo "Writing SQL Configuration to disk...\n";
158 @touch($conffile); // php bug
159 $fd = @fopen($conffile, 'w');
160 if ($fd == FALSE) {
161 echo "ERROR. Could not open config file '$conffile' for writing.\n";
162 flush();
163 break;
165 $string = "<?
166 // OpenEMR
167 // MySQL Config
168 // Referenced from sql.inc
172 $it_died = 0; //fmg: variable keeps running track of any errors
174 fwrite($fd,$string) or $it_died++;
175 fwrite($fd,"\$host\t= '$host';\n") or $it_died++;
176 fwrite($fd,"\$port\t= '$port';\n") or $it_died++;
177 fwrite($fd,"\$login\t= '$login';\n") or $it_died++;
178 fwrite($fd,"\$pass\t= '$pass';\n") or $it_died++;
179 fwrite($fd,"\$dbase\t= '$dbname';\n") or $it_died++;
182 $string = '
184 $sqlconf = array();
185 $sqlconf["host"]= $host;
186 $sqlconf["port"] = $port;
187 $sqlconf["login"] = $login;
188 $sqlconf["pass"] = $pass;
189 $sqlconf["dbase"] = $dbase;
190 //////////////////////////
191 //////////////////////////
192 //////////////////////////
193 //////DO NOT TOUCH THIS///
194 $config = 1; /////////////
195 //////////////////////////
196 //////////////////////////
197 //////////////////////////
200 ?><? // done just for coloring
202 fwrite($fd,$string) or $it_died++;
204 //it's rather irresponsible to not report errors when writing this file.
205 if ($it_died != 0) {
206 echo "ERROR. Couldn't write $it_died lines to config file '$conffile'.\n";
207 flush();
208 break;
210 fclose($fd);
212 //Now, use new name and fix globals.php and rename directory!!!
213 $d = getcwd();
214 $dn = dirname($d);
215 $contents = file_get_contents($d.'/interface/globals.php');
216 $contents = preg_replace('/\$webserver_root\s+=\s+[\"\'].*?[\"\'];/',
217 "\$webserver_root = '".$dn."/".$newname."';",$contents);
218 $contents = preg_replace('/\$web_root\s+=\s+[\"\'].*?[\"\'];/',
219 "\$web_root = '/".$newname."';",$contents);
220 file_put_contents($d.'/interface/globals.php',$contents);
221 if (rename($d,$dn.'/'.$newname)) {
222 echo "<br/><a href='http://localhost/".$newname."'>click here</a>";
226 </body>
227 </html>