1
0
mirror of https://github.com/Yubico/yubikey-val.git synced 2024-11-29 00:24:13 +01:00

changed updateDbParams function to only update counters if they are newer than before

This commit is contained in:
Olov Danielson 2009-12-07 17:21:38 +00:00
parent 3aa91fa557
commit 530498ef71
2 changed files with 60 additions and 30 deletions

View File

@ -120,11 +120,11 @@ class Db
*/
public function connect(){
if (! $this->db_conn = mysql_connect($this->host, $this->user, $this->pwd)) {
echo 'Could not connect: ' . mysql_error();
error_log('Could not connect: ' . mysql_error());
return false;
}
if (! mysql_select_db($this->db_name)) {
echo 'Could not select database ' . $this->db_name;
error_log('Could not select database ' . $this->db_name);
$this->disconnect();
return false;
}
@ -135,6 +135,7 @@ class Db
{
mysql_query("TRUNCATE TABLE " . $name);
}
/**
* function to update row in database
*
@ -159,14 +160,44 @@ class Db
// Insert UPDATE statement at beginning
$query = "UPDATE " . $table . " SET " . $query;
if (! mysql_query($query)){
echo 'Query failed: ' . mysql_error();
echo 'Query was: ' . $query;
error_log('Query failed: ' . mysql_error());
error_log('Query was: ' . $query);
return false;
}
return true;
}
/**
* function to update row in database
*
* @param string $table Database table to update row in
* @param int $id Id on row to update
* @param array $values Array with key=>values to update
* @param string $condition conditional statement
* @return boolean True on success, otherwise false.
*
*/
public function conditional_update($table, $id, $values, $condition)
{
foreach ($values as $key=>$value){
if ($value != null) $query = $query . " " . $key . "='" . $value . "',";
}
if (! $query) {
log("no values to set in query. Not updating DB");
return true;
}
$query = rtrim($query, ",") . " WHERE id = " . $id . " and " . $condition;
// Insert UPDATE statement at beginning
$query = "UPDATE " . $table . " SET " . $query;
if (! mysql_query($query)){
error_log('Query failed: ' . mysql_error());
error_log('Query was: ' . $query);
return false;
}
return true;
}
/**
* function to insert new row in database
*
@ -188,8 +219,8 @@ class Db
$query = rtrim($query, ",");
$query = $query . ")";
if (! mysql_query($query)){
echo 'Query failed: ' . mysql_error();
echo 'Query was: ' . $query;
error_log('Query failed: ' . mysql_error());
error_log('Query was: ' . $query);
return false;
}
return true;
@ -227,9 +258,8 @@ or false on failure.
if ($nr!=null) $query.= " LIMIT " . $nr;
$result = mysql_query($query);
if (! $result) {
echo 'Query failed: ' . mysql_error();
echo 'Query was: ' . $query;
error_log('Query failed: ' . mysql_error());
error_log('Query was: ' . $query);
return false;
}
if ($nr==1) {
@ -278,9 +308,8 @@ or false on failure.
if ($nr!=null) $query.= " LIMIT " . $nr;
$result = mysql_query($query);
if (! $result) {
echo 'Query failed: ' . mysql_error();
echo 'Query was: ' . $query;
error_log('Query failed: ' . mysql_error());
error_log('Query was: ' . $query);
return false;
}
if ($nr==1) {
@ -324,9 +353,8 @@ or false on failure.
if ($nr!=null) $query.= " LIMIT " . $nr;
$result = mysql_query($query);
if (! $result) {
echo 'Query failed: ' . mysql_error();
echo 'Query was: ' . $query;
error_log('Query failed: ' . mysql_error());
error_log('Query was: ' . $query);
return false;
}
return $result;

View File

@ -157,27 +157,29 @@ class SyncLib
public function updateDbCounters($params)
{
$res=$this->db->lastBy('yubikeys', 'publicName', modhex2b64($params['yk_identity']));
if (isset($res['id'])) {
if(! $this->db->update('yubikeys',
$res['id'],
array('accessed'=>$this->UnixToDbTime($params['modified']),
'counter'=>$params['yk_counter'],
'sessionUse'=>$params['yk_use'],
'low'=>$params['yk_low'],
'high'=>$params['yk_high'])))
$condition='('.$params['yk_counter'].'>counter or ('.$params['yk_counter'].'=counter and ' .
$params['yk_use'] . '>sessionUse))' ;
if(! $this->db->conditional_update('yubikeys',
$res['id'],
array('accessed'=>$this->UnixToDbTime($params['modified']),
'counter'=>$params['yk_counter'],
'sessionUse'=>$params['yk_use'],
'low'=>$params['yk_low'],
'high'=>$params['yk_high']),
$condition))
{
error_log("ykval-synclib:critical: failed to update internal DB with new counters");
return false;
} else {
$this->log("notice", "updated database ", $params);
if (mysql_affected_rows()>0) $this->log("notice", "updated database ", $params);
else $this->log('notice', 'database not updated', $params);
return true;
}
} else return false;
}
public function countersHigherThan($p1, $p2)
{
if ($p1['yk_counter'] > $p2['yk_counter'] ||
@ -341,10 +343,10 @@ class SyncLib
$this->log("notice", "local db contains ", $localParams);
$this->log("notice", "response contains ", $resParams);
/* Check if internal DB should be updated */
if ($this->countersHigherThan($resParams, $lastLocalParams)) {
$this->updateDbCounters($resParams);
}
/* Update internal DB (conditional) */
$this->updateDbCounters($resParams);
/* Check for warnings