mirror of
https://github.com/Yubico/yubikey-val.git
synced 2025-01-31 16:52:20 +01:00
Changed to using PDO database connection
This commit is contained in:
parent
b9701c16ea
commit
851aa21c66
@ -11,10 +11,10 @@ class DbTest extends PHPUnit_Framework_TestCase
|
||||
public function setup()
|
||||
{
|
||||
global $baseParams;
|
||||
$this->db=new Db($baseParams['__YKVAL_DB_HOST__'],
|
||||
$this->db=new Db($baseParams['__YKVAL_DB_DSN__'],
|
||||
'root',
|
||||
'lab',
|
||||
$baseParams['__YKVAL_DB_NAME__']);
|
||||
$baseParams['__YKVAL_DB_OPTIONS__']);
|
||||
$this->db->connect();
|
||||
$this->db->customQuery("drop table unittest");
|
||||
$this->db->customQuery("create table unittest (id int,value1 int, value2 int)");
|
||||
@ -66,5 +66,14 @@ class DbTest extends PHPUnit_Framework_TestCase
|
||||
$res=$this->db->findBy('unittest', 'id', 1, 1);
|
||||
$this->assertEquals(1000, $res['value2']);
|
||||
}
|
||||
public function testDeleteByMultiple()
|
||||
{
|
||||
$this->assertTrue($this->db->save('unittest', array('value1'=>100,
|
||||
'value2'=>200,
|
||||
'id'=>1)));
|
||||
$this->assertTrue($this->db->deleteByMultiple('unittest', array('value1'=>100,
|
||||
'value2'=>200)));
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
@ -11,10 +11,11 @@ class SyncLibTest extends PHPUnit_Framework_TestCase
|
||||
public function setup()
|
||||
{
|
||||
global $baseParams;
|
||||
$db = new Db($baseParams['__YKVAL_DB_HOST__'],
|
||||
'root',
|
||||
'lab',
|
||||
$baseParams['__YKVAL_DB_NAME__']);
|
||||
$db=new Db($baseParams['__YKVAL_DB_DSN__'],
|
||||
'root',
|
||||
'lab',
|
||||
$baseParams['__YKVAL_DB_OPTIONS__']);
|
||||
|
||||
$db->connect();
|
||||
# $db->truncateTable('queue');
|
||||
$db->disconnect();
|
||||
|
@ -2,13 +2,15 @@
|
||||
|
||||
# For the validation interface.
|
||||
$baseParams = array ();
|
||||
$baseParams['__YKVAL_DB_HOST__'] = 'localhost';
|
||||
$baseParams['__YKVAL_DB_NAME__'] = 'ykval';
|
||||
$baseParams['__YKVAL_DB_DSN__'] = "mysql:dbname=ykval;host=127.0.0.1";
|
||||
$baseParams['__YKVAL_DB_USER__'] = 'ykval_verifier';
|
||||
$baseParams['__YKVAL_DB_PW__'] = 'lab';
|
||||
$baseParams['__YKVAL_DB_OPTIONS__'] = array();
|
||||
|
||||
# For the validation server sync
|
||||
$baseParams['__YKVAL_SYNC_POOL__'] = "http://1.2.3.4/wsapi/2.0/sync;http://2.3.4.5/wsapi/2.0/sync;http://3.4.5.6/wsapi/2.0/sync";
|
||||
$baseParams['__YKVAL_SYNC_POOL__'] = array("http://1.2.3.4/wsapi/2.0/sync",
|
||||
"http://2.3.4.5/wsapi/2.0/sync",
|
||||
"http://3.4.5.6/wsapi/2.0/sync");
|
||||
|
||||
# Specify how often the sync daemon awakens
|
||||
$baseParams['__YKVAL_SYNC_INTERVAL__'] = 60;
|
||||
|
99
ykval-db.php
99
ykval-db.php
@ -53,12 +53,12 @@ class Db
|
||||
* @return void
|
||||
*
|
||||
*/
|
||||
public function __construct($host, $user, $pwd, $db_name)
|
||||
public function __construct($db_dsn, $db_username, $db_password, $dp_options)
|
||||
{
|
||||
$this->host=$host;
|
||||
$this->user=$user;
|
||||
$this->pwd=$pwd;
|
||||
$this->db_name=$db_name;
|
||||
$this->db_dsn=$db_dsn;
|
||||
$this->db_username=$db_username;
|
||||
$this->db_password=$db_password;
|
||||
$this->db_options=$db_options;
|
||||
}
|
||||
/**
|
||||
* function to convert Db timestamps to unixtime(s)
|
||||
@ -95,10 +95,7 @@ class Db
|
||||
*/
|
||||
public function disconnect()
|
||||
{
|
||||
if ($this->db_conn!=NULL) {
|
||||
mysql_close($this->db_conn);
|
||||
$this->db_conn=NULL;
|
||||
}
|
||||
$this->dbh=NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,7 +106,7 @@ class Db
|
||||
*/
|
||||
public function isConnected()
|
||||
{
|
||||
if ($this->db_conn!=NULL) return True;
|
||||
if ($this->dbh!=NULL) return True;
|
||||
else return False;
|
||||
}
|
||||
/**
|
||||
@ -119,22 +116,37 @@ class Db
|
||||
*
|
||||
*/
|
||||
public function connect(){
|
||||
if (! $this->db_conn = mysql_connect($this->host, $this->user, $this->pwd)) {
|
||||
error_log('Could not connect: ' . mysql_error());
|
||||
$this->db_conn=Null;
|
||||
return false;
|
||||
}
|
||||
if (! mysql_select_db($this->db_name)) {
|
||||
error_log('Could not select database ' . $this->db_name);
|
||||
$this->disconnect();
|
||||
|
||||
try {
|
||||
$this->dbh = new PDO($this->db_dsn, $this->db_username, $this->db_password, $this->db_options);
|
||||
} catch (PDOException $e) {
|
||||
error_log("hej hopp");
|
||||
error_log("Database error: " . $e->getMessage());
|
||||
$this->dbh=Null;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function query($query, $returnresult=false) {
|
||||
if($this->dbh) {
|
||||
$result = $this->dbh->query($query);
|
||||
if (! $result){
|
||||
error_log('Database error: ' . print_r($this->dbh->errorInfo(), true));
|
||||
error_log('Query was: ' . $query);
|
||||
return false;
|
||||
}
|
||||
if ($returnresult) return $result;
|
||||
else return true;
|
||||
} else {
|
||||
error_log('No database connection');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function truncateTable($name)
|
||||
{
|
||||
mysql_query("TRUNCATE TABLE " . $name);
|
||||
$this->query("TRUNCATE TABLE " . $name);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -161,12 +173,8 @@ class Db
|
||||
$query = rtrim($query, ",") . " WHERE " . $k . ' = ' . $v;
|
||||
// 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;
|
||||
|
||||
return $this->query($query, false);
|
||||
}
|
||||
|
||||
|
||||
@ -208,13 +216,9 @@ class Db
|
||||
$query = rtrim($query, ",") . " WHERE id = " . $id . " and " . $condition;
|
||||
// Insert UPDATE statement at beginning
|
||||
$query = "UPDATE " . $table . " SET " . $query;
|
||||
|
||||
error_log("query is " . $query);
|
||||
if (! mysql_query($query)){
|
||||
error_log('Query failed: ' . mysql_error());
|
||||
error_log('Query was: ' . $query);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return $this->query($query, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -237,12 +241,7 @@ class Db
|
||||
}
|
||||
$query = rtrim($query, ",");
|
||||
$query = $query . ")";
|
||||
if (! mysql_query($query)){
|
||||
error_log('Query failed: ' . mysql_error());
|
||||
error_log('Query was: ' . $query);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return $this->query($query, false);
|
||||
}
|
||||
/**
|
||||
* helper function to collect last row[s] in database
|
||||
@ -308,19 +307,16 @@ or false on failure.
|
||||
if ($rev==1) $query.= " ORDER BY id DESC";
|
||||
if ($nr!=null) $query.= " LIMIT " . $nr;
|
||||
// error_log('query is ' .$query);
|
||||
$result = mysql_query($query);
|
||||
if (! $result) {
|
||||
error_log('Query failed: ' . mysql_error());
|
||||
error_log('Query was: ' . $query);
|
||||
return false;
|
||||
}
|
||||
$result = $this->query($query, true);
|
||||
if (!$result) return false;
|
||||
|
||||
if ($nr==1) {
|
||||
$row = mysql_fetch_array($result, MYSQL_ASSOC);
|
||||
$row = $result->fetch(PDO::FETCH_ASSOC);
|
||||
return $row;
|
||||
}
|
||||
else {
|
||||
$collection=array();
|
||||
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
|
||||
while($row = $result->fetch(PDO::FETCH_ASSOC)){
|
||||
$collection[]=$row;
|
||||
}
|
||||
return $collection;
|
||||
@ -336,7 +332,7 @@ or false on failure.
|
||||
* @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL.
|
||||
* @param int $rev rev=1 indicates order should be reversed. Default=NULL.
|
||||
* @param string distinct Select rows with distinct columns, Default=NULL
|
||||
* @return mixed Array with values from Db row or 2d-array with multiple rows
|
||||
* @return boolean True on success, otherwise false.
|
||||
*
|
||||
*/
|
||||
public function deleteByMultiple($table, $where, $nr=null, $rev=null)
|
||||
@ -353,20 +349,13 @@ or false on failure.
|
||||
}
|
||||
if ($rev==1) $query.= " ORDER BY id DESC";
|
||||
if ($nr!=null) $query.= " LIMIT " . $nr;
|
||||
$result = mysql_query($query);
|
||||
if (! $result) {
|
||||
error_log('Query failed: ' . mysql_error());
|
||||
error_log('Query was: ' . $query);
|
||||
return false;
|
||||
}
|
||||
return $result;
|
||||
|
||||
return $this->query($query, false);
|
||||
}
|
||||
|
||||
public function customQuery($query)
|
||||
{
|
||||
error_log("custom query: " . $query);
|
||||
return mysql_query($query);
|
||||
return $this->query($query, true);
|
||||
}
|
||||
/**
|
||||
* helper function used to get rows from Db table in reversed order.
|
||||
|
@ -13,11 +13,12 @@ class SyncLib
|
||||
{
|
||||
$this->logname=$logname;
|
||||
global $baseParams;
|
||||
$this->syncServers = explode(";", $baseParams['__YKVAL_SYNC_POOL__']);
|
||||
$this->db=new Db($baseParams['__YKVAL_DB_HOST__'],
|
||||
$this->syncServers = $baseParams['__YKVAL_SYNC_POOL__'];
|
||||
|
||||
$this->db=new Db($baseParams['__YKVAL_DB_DSN__'],
|
||||
$baseParams['__YKVAL_DB_USER__'],
|
||||
$baseParams['__YKVAL_DB_PW__'],
|
||||
$baseParams['__YKVAL_DB_NAME__']);
|
||||
$baseParams['__YKVAL_DB_OPTIONS__']);
|
||||
$this->isConnected=$this->db->connect();
|
||||
$this->random_key=rand(0,1<<16);
|
||||
$this->max_url_chunk=$baseParams['__YKVAL_SYNC_MAX_SIMUL__'];
|
||||
@ -47,12 +48,9 @@ class SyncLib
|
||||
}
|
||||
function getClientData($client)
|
||||
{
|
||||
$res=$this->db->customQuery('SELECT id, secret FROM clients WHERE active AND id='.mysql_quote($client));
|
||||
if(mysql_num_rows($res)>0) {
|
||||
$row = mysql_fetch_assoc($res);
|
||||
mysql_free_result($res);
|
||||
return $row;
|
||||
} else return false;
|
||||
$res=$this->db->customQuery("SELECT id, secret FROM clients WHERE active AND id='" . $client . "'");
|
||||
if($res->rowCount()>0) return $res->fetch(PDO::FETCH_ASSOC);
|
||||
else return false;
|
||||
}
|
||||
function getLast()
|
||||
{
|
||||
@ -489,6 +487,7 @@ class SyncLib
|
||||
|
||||
$ch = array();
|
||||
foreach ($urls as $id => $url) {
|
||||
error_log("url is " . $url);
|
||||
$handle = curl_init();
|
||||
|
||||
curl_setopt($handle, CURLOPT_URL, $url);
|
||||
|
@ -23,6 +23,7 @@ $sync = new SyncLib();
|
||||
if (! $sync->isConnected()) {
|
||||
sendResp(S_BACKEND_ERROR, $apiKey);
|
||||
exit;
|
||||
|
||||
}
|
||||
|
||||
/* Extract values from HTTP request
|
||||
|
Loading…
x
Reference in New Issue
Block a user