X-Git-Url: https://git.cweiske.de/auerswald-callnotifier.git/blobdiff_plain/67eb8026d25dd261358ef5de1c8a5a6111a8e282..b0d0370b7269a8e5fc4254b3f47f266551957fc4:/src/callnotifier/Logger/CallDb.php diff --git a/src/callnotifier/Logger/CallDb.php b/src/callnotifier/Logger/CallDb.php index 110e963..0135fba 100644 --- a/src/callnotifier/Logger/CallDb.php +++ b/src/callnotifier/Logger/CallDb.php @@ -1,8 +1,19 @@ dsn = $dsn; + $this->username = $username; + $this->password = $password; + //check if the credentials are correct + $this->connect(); + } + + /** + * Connect to the SQL server. + * SQL servers close the connection automatically after some hours, + * and since calls often don't come in every minute, we will have + * disconnects in between. + * Thus, we will reconnect on every location load. + * + * @return void + */ + protected function connect() { $this->db = new \PDO( - $dsn, $username, $password, + $this->dsn, $this->username, $this->password, array( \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', - \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + \PDO::ATTR_PERSISTENT => true ) ); - $this->stmt = $this->db->prepare( - 'INSERT INTO finished (' - . ' call_start' - . ', call_end' - . ', call_type' - . ', call_from' - . ', call_from_name' - . ', call_from_location' - . ', call_to' - . ', call_to_name' - . ', call_to_location' - . ', call_length' - . ') VALUES (' - . ' :call_start' - . ', :call_end' - . ', :call_type' - . ', :call_from' - . ', :call_from_name' - . ', :call_from_location' - . ', :call_to' - . ', :call_to_name' - . ', :call_to_location' - . ', :call_length' - . ')' - ); } public function log($type, $arData) @@ -54,9 +62,18 @@ class Logger_CallDb extends Logger_CallBase } $call = $arData['call']; + if (!$this->hasValidType($call)) { + return; + } + if (!$this->hasValidMsn($call)) { + return; + } + $this->addUnsetVars($call); - $ret = $this->stmt->execute( + $this->connect(); + $stmt = $this->prepareDbStatement(); + $ret = $stmt->execute( array( 'call_start' => date('Y-m-d H:i:s', $call->start), 'call_end' => date('Y-m-d H:i:s', $call->end), @@ -73,11 +90,40 @@ class Logger_CallDb extends Logger_CallBase if ($ret === false) { throw new \Exception( 'Error logging call to database: ' - . implode(' / ', $this->stmt->errorInfo()) + . implode(' / ', $stmt->errorInfo()) ); } } + protected function prepareDbStatement() + { + return $this->db->prepare( + 'INSERT INTO finished (' + . ' call_start' + . ', call_end' + . ', call_type' + . ', call_from' + . ', call_from_name' + . ', call_from_location' + . ', call_to' + . ', call_to_name' + . ', call_to_location' + . ', call_length' + . ') VALUES (' + . ' :call_start' + . ', :call_end' + . ', :call_type' + . ', :call_from' + . ', :call_from_name' + . ', :call_from_location' + . ', :call_to' + . ', :call_to_name' + . ', :call_to_location' + . ', :call_length' + . ')' + ); + } + } ?>