Menu

[r8]: / tools / import_settings_to_db.php  Maximize  Restore  History

Download this file

264 lines (211 with data), 8.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
// Required Libraries
include(dirname(realpath(__FILE__)) . '/../includes/adodb/adodb.inc.php');
global $ourDBType;
$ourDBType = null; // Default
// Start the output and conversion here
startOutput();
$settingsFile = dirname(realpath(__FILE__)) . "/../includes/settings.php";
showInfoBox('We are attempting to open and reads your settings.php file.', 'Reading Settings');
if (!file_exists($settingsFile)) {
showInfoBox("<span style=\"color: ff0000;font-weight: bold;\">There was an error trying to read the settings.php file...\nPlease make sure that you have created your settings.php file already...\nAttempting to read file: $settingsFile</span>", 'ERROR');
die();
}
$contents = file_get_contents($settingsFile);
showInfoBox('Successfully opened and read the settings.php file, we are attempting to parse now....', 'Parsing');
if (!isConverted()) {
$keyval = parseSettings($contents);
showInfoBox('Populating database.....', 'Database');
//populateDatabase($keyval);
writeToDB($keyval);
if (createFile()) {
showInfoBox('All done.. Please replace settings.php with the settings-new.php file that was created.');
}
} else {
showInfoBox("You appear to already be converted. Congratulations.\nIf your settings are not working, please try to run the DB Fix tool.");
}
endOutput();
//-------------------------------------------------------------------------------------------//
/**
* writeToDb writes the OBJECT to the database
* @param Array $data
*/
function writeToDB($data) {
// echo print_r(array_keys($data), true);
require_once(dirname(realpath(__FILE__)) . '/../includes/classes/settings.class.php');
$settings = new Settings();
$settings->writeObject($data);
}
/**
* createFile creates the new Config file
* @return bool Returns true on good file write
*/
function createFile() {
global $ourDBType;
$inc_dir = dirname(realpath(__FILE__)) . '/../includes/';
$str = '
<?php
/** Settings file created as part of the conversion process **/
define(\'CONVERTED\', TRUE);
define(\'MYSERVER\', \'' . MYSERVER . '\');
define(\'MYLOGIN\', \'' . MYLOGIN . '\');
define(\'MYPASSWORD\', \'' . MYPASSWORD . '\');
define(\'MYDATABASE\', \'' . MYDATABASE . '\');
$databaseType = \'' . $ourDBType . '\';
/** EOF **/
?>';
//Attemp to write the new file..
$fh = fopen($inc_dir . 'settings-new.php', 'a+');
if (!$fh) {
showInfoBox("Could not create file, make sure you have permissions...\nYou can also replace your settings.php file with this:\n<textarea rows=10 cols=80>$str</textarea>", ' :: ERROR ::');
} else {
fwrite($fh, $str);
fclose($fh);
return true;
}
return false;
}
function isConverted() {
require_once(dirname(realpath(__FILE__)) . '/../includes/settings.php');
if (!defined('CONVERTED')) {
return false;
}
return CONVERTED;
}
function populateDatabase($data) {
//First let's empty the table, we don't want anything in here..
//include(dirname(realpath(__FILE__)) . '/../includes/settings.php'); // So we can hit the DB
//attempt to get the DB Vars
$databaseType = '';
$SERVER = $USERNAME = $PASSWORD = $DATABASE = '';
foreach ($data as $key => $val) {
if (substr($key, 0, 2) == "MY") {
$work = substr($key, 2);
eval("\$$work = '$val';");
}
if ($key == "databaseType") {
$databaseType = $val;
}
}
$db = &ADONewConnection($databaseType);
$db->Connect($SERVER, $LOGIN, $PASSWORD, $DATABASE);
$tableName = "new_config"; //This would be dynamic..
$ret = $db->Execute("delete from $tableName where 1=1");
if (!$ret) die( $db->ErrorMsg());
foreach ($data as $key => $val) {
$sql = "insert into $tableName (?, ?, 1);";
if (!$db->Execute($sql, array($key, val))) {
die('DB Error: ' . $db->ErrorMsg());
}
}
}
function parseSettings($contents) {
global $ourDBType;
$settingsArray = array();
foreach (explode("\n", $contents) as $line ) {
$skipAssign = false;
$line = trim($line);
$doit = true;
$key = $val = "";
if ($line == '' || $line[0] == '#' || substr($line, 0, 5) == "<?php" || substr($line, 0, 2) == "?>" ||
substr($line, 0, 2) == "//") $doit = false;
if ($doit) {
//showInfoBox("DEBUG: Parse this line..<br />" . $line, "Info...");
//Clean the line, remove any ;'s or comments and then parse it..
//We work on two style of lines, defines and variable assignments
if ($line[0] == '$') {
// Variable
$work = substr($line, 1);
// Find hte end of line
$end = strpos($work, ';');
$work = substr($work, 0, $end);
$work = preg_replace('/[\'";()]*/','', $work); // Clean
list($key,$val) = split('=', $work, 2);
$key = trim($key);
$val = trim($val);
//Save DB Type
if ($key == "databaseType") $ourDBType = $val;
// ** Special rules ** \\
if (substr($key, 0, 5) == "table" && (strpos($key, "Prefix") === FALSE)) {
//Do something special here if it has table in it..
$work = substr($key, 5);
// Now find wher the key is
$pos = strpos($work, '[');
$epos = strpos($work, ']');
$tableName = substr($work, 0, $pos);
$keyName = substr($work, ($pos+1), (($epos - $pos)-1));
if ($tableName != '') $settingsArray[$tableName][$keyName] = $val;
$skipAssign = true;
}
elseif (substr($key, 0, 6) == "config") {
//Do something special here if it has table in it..
$work = substr($key, 6);
// Now find wher the key is
$pos = strpos($work, '[');
$epos = strpos($work, ']');
$tableName = substr($work, 0, $pos);
$keyName = substr($work, ($pos+1), (($epos - $pos)-1));
if ($tableName != '') $settingsArray[$tableName][$keyName] = $val;
$skipAssign = true;
}
//showInfoBox("Key: $key - Value: $val", "DEBUG");
} elseif (substr($line, 0, 6) == "define") {
//CONSTANTS
$work = substr($line, 6);
$work = preg_replace('/[\'";()]*/','', $work);
list($key, $val) = split(',', $work, 2);
$key = trim($key);
$val = trim($val);
//showInfoBox("KEY: $key - VAL: $val");
}
if (!$skipAssign) $settingsArray[$key] = $val;
}
}
// showInfoBox('Returning.. ' . print_r($settingsArray, true));
return $settingsArray;
}
// Functions to aid in displaying output to the browser
function showInfoBox($message, $title = '.:: DEBUG ::.') {
$message = nl2br($message);
//special for the text area
if (strpos($message, "<textarea") !== false) {
$str = strpos($message,"<textarea");
$end = strpos($message,"</textarea>");
$replStr = substr($message, $str, ($end - $str));
$replStr = ereg_replace('<br />', "", $replStr);
$message = substr_replace($message, $replStr, $str, ($end-$str));
}
echo '
<fieldset><legend>' . $title . '</legend>' . $message . '</fieldset>
';
}
function startOutput() {
echo '
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Import Settings to DB</title>
<style type="text/css">
* { margin: 0; padding: 0 }
#contentWrapper { margin: 15px; }
#contentWrapper fieldset { padding: 10px; background: #f5f5f5;border: 1px solid #999;}
#contentWrapper fieldset legend { padding: 7px; color: #821400; font-weight: bold;}
</style>
</head>
<body>
<div id="contentWrapper"><h2>Attempting to import settings....</h2>
We are starting to import settings from your local file to the databse...<br /><br />
';
}
function endOutput() {
echo '
</div>
</body>
<!-- (c)2008 - 2009 Batez Consulting, $Revision: 1.2 $ - $Author: norman77 $ - $Date: 2009/01/21 21:29:21 $ ($Id) -->
</html>
';
}
// END //
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.