📄 ifunctions.php
字号:
}
### If we are still going, btype = 'PHPQBACKUP' or 'SURVEYBACKUP'...
# We only mess with phpQAdmin if they have requested an erase and restore
# from PHPQBACKUP formatted files...
if ($table == 'phpQAdmin') {
global $phpQAdmin;
if ($flag == 'erase' && $btype == 'PHPQBACKUP' && !$tcreated[$table]) {
mysql_query("DROP TABLE phpQAdmin");
# Attempt to catch and handle any error when creating phpQAdmin...
if (!mysql_query($phpQC['phpQAdmin'])) {
$GLOBALS["message"] = $action.sprintf($phpQlang['ImportCreateTable'], 'phpQAdmin');
return 0;
}
if (!mysql_query("INSERT INTO phpQAdmin (Passwd,".join(',',$phpQF[$table]).") VALUES (".PHPQ2SQL($phpQAdmin['Passwd']).','.join(',',$sqlvalues).")")) {
$GLOBALS["message"] = $action.sprintf($phpQlang['ImportInsertTable'], 'phpQAdmin');
return 0;
}
$tcreated[$table] = 1;
}
}
# Handle any surveys that need to be inserted...
elseif (isset($phpQF[$table])) {
if ($flag == 'erase' && !$tcreated[$table]) {
# We do not throw an error here in case their tables just don't exist.
mysql_query("DROP TABLE $table");
if (!mysql_query($phpQC[$table])) {
$GLOBALS["message"] = $action.sprintf($phpQlang['ImportCreateTable'], $table);
return 0;
}
$tcreated[$table] = 1;
}
# Attempt to insert the data as-is. If there is a collision with unique
# ids of SID, QID, UID an error will be thrown and we can attempt to
# correct.
for ($i = 0; $i < count($phpQR[$table]); $i += 2) {
$rid = $phpQR[$table][$i];
$rname = $phpQR[$table][$i+1];
if (isset($idmap[$rname][$sqlvalues[$rid]])) {
$sqlvalues[$rid] = '"'.ProtectMySQL($idmap[$rname][$sqlvalues[$rid]]).'"';
}
}
$mqinsert = mysql_query("INSERT INTO $table (".join(',',$phpQF[$table]).") VALUES (".join(',',$sqlvalues).")");
# If our insert failed and we weren't supposed to erase data and there
# is a field defined to be unique, we need to attempt to insert again,
# this time with a NULL value in the place of the unique field.
if (!$mqinsert && $flag != 'erase' && isset($phpQU[$table])) {
$tnames = $phpQF[$table];
$tvalues = $sqlvalues;
array_splice($tnames, $phpQU[$table][0], 1);
array_splice($tvalues, $phpQU[$table][0], 1);
$ufield = $phpQU[$table][1];
$uvalue = ($ufield == 'UID') ? GenerateUID() : 'NULL';
if (mysql_query("INSERT INTO $table ($ufield,".join(',',$tnames).") VALUES ($uvalue,".join(',',$tvalues).")")) {
$uvalue = ($ufield == 'UID') ? $uvalue : mysql_insert_id();
$idmap[$ufield][$sqlvalues[$phpQU[$table][0]]] = $uvalue;
}
else {
$GLOBALS["message"] = $action.sprintf($phpQlang['ImportInsertTable'], $table);
return 0;
}
}
elseif (!$mqinsert) {
$GLOBALS["message"] = $action.sprintf($phpQlang['ImportInsertTable'], $table);
return 0;
}
}
}
}
# Here we make sure that any tables that have not been dropped and recreated
# are erased if the restore calls for it.
if ($flag == 'erase') {
$tablea = array('phpQSurvey','phpQQuestion','phpQAnswer','phpQUser');
for ($i = 0; $i < count($tablea); $i++) {
$table = $tablea[$i];
if (!$tcreated[$table]) {
# We do not throw an error here in case their tables just don't exist.
mysql_query("DROP TABLE $table");
if (!mysql_query($phpQC[$table])) {
$GLOBALS["message"] = $action.sprintf($phpQlang['ImportCreateTable'], $table);
return 0;
}
$tcreated[$table] = 1;
}
}
}
return $sid;
}
function PHPQ2SQL($mystring) {
# Starting with 2.0, no database fields are NULL any longer...
if ($mystring == '=NULL=') { $mystring = ''; }
return '"'.ProtectMySQL(urldecode($mystring)).'"';
}
function FindITemplate ($template) {
$templates = GetTemplates();
if (!count($templates)) { return ''; }
if (isset($templates[$template])) { return $template; }
# Could not find requested template, try to locate phpQ default template...
global $phpQAdmin;
if (isset($templates[$phpQAdmin["Template"]])) {
return $phpQAdmin["Template"];
}
if (isset($templates['default'])) { return 'default'; }
foreach ($templates as $temp => $name) { return $temp; }
return '';
}
function FindILanguage ($language) {
$languages = GetLanguages();
if (!count($languages)) { return ''; }
if (isset($languages[$language])) { return $language; }
# Could not find requested template, try to locate phpQ default template...
global $phpQAdmin;
if (isset($languages[$phpQAdmin['Language']])) {
return $phpQAdmin['Language'];
}
if (isset($languages['english'])) { return 'english'; }
foreach ($languages as $lang => $name) { return $lang; }
return '';
}
function GenerateUID () {
global $HTTP_SERVER_VARS, $REMOTE_ADDR;
# Here we use several weak sources of entropy to generate the best random
# seed we can. This includes the time in seconds and microseconds, the user's
# remote ip address and also the process id (which is 0 if unsupported -- ok)
list($usec, $sec) = explode(' ', microtime());
$usec *= 1000000;
$ip = $REMOTE_ADDR ? $REMOTE_ADDR : $HTTP_SERVER_VARS['REMOTE_ADDR'];
mt_srand(time() ^ (int)$usec ^ ip2long($ip) ^ (int)@getmypid());
$loop = true;
while ($loop) {
$randval = mt_rand(1000000000,1999999999); # max int is usually 2147483647
$mquser = mysql_query("select * from phpQUser where UID = \"$randval\"");
if (!($mauser = mysql_fetch_array($mquser))) {
$loop = false;
}
}
return $randval;
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -