From 378a3adf9811b27980efd03dd8a51d9fe00258ca Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 10 Aug 2012 22:32:08 +0200 Subject: [PATCH] all loggers support call type and MSN filtering now --- src/callnotifier/Logger/CallBase.php | 59 ++++++++++++++++++++++ src/callnotifier/Logger/CallDb.php | 14 ++++- src/callnotifier/Logger/CallDreambox.php | 18 +++++-- src/callnotifier/Logger/CallEcho.php | 15 +++++- src/callnotifier/Logger/CallFile.php | 18 +------ src/callnotifier/Logger/CallNotifySend.php | 15 +++++- 6 files changed, 112 insertions(+), 27 deletions(-) diff --git a/src/callnotifier/Logger/CallBase.php b/src/callnotifier/Logger/CallBase.php index 96fe76f..31ad0bc 100644 --- a/src/callnotifier/Logger/CallBase.php +++ b/src/callnotifier/Logger/CallBase.php @@ -3,6 +3,65 @@ namespace callnotifier; abstract class Logger_CallBase implements Logger { + protected $callTypes; + protected $msns; + + + /** + * Create a new call logger. + * + * @param string $callTypes Which types of call to log: + * - "i" - incoming calls only + * - "o" - outgoing calls only + * - "io" - both incoming and outgoing calls + * @param array $msns Array of MSN (Multi Subscriber Number) that + * calls to shall get logged. + * If the array is empty, calls to all MSNs get + * logged. + */ + public function __construct($callTypes = 'io', $msns = array()) + { + $this->callTypes = $callTypes; + $this->msns = (array)$msns; + } + + /** + * Check if the call type (incoming or outgoing) shall be logged. + * + * @return boolean True if it should be logged, false if not + */ + protected function hasValidType($call) + { + if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') { + return false; + } + if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') { + return false; + } + + return true; + } + + /** + * Check if the MSN shall be logged + * + * @return boolean True if it should be logged, false if not + */ + protected function hasValidMsn($call) + { + if ($call->type == CallMonitor_Call::INCOMING) { + $msn = $call->to; + } else { + $msn = $call->from; + } + if (count($this->msns) > 0 && !in_array($msn, $this->msns)) { + //msn shall not be logged + return false; + } + + return true; + } + protected function addUnsetVars($call) { static $expectedVars = array( diff --git a/src/callnotifier/Logger/CallDb.php b/src/callnotifier/Logger/CallDb.php index b455774..e2cdc6f 100644 --- a/src/callnotifier/Logger/CallDb.php +++ b/src/callnotifier/Logger/CallDb.php @@ -17,8 +17,11 @@ class Logger_CallDb extends Logger_CallBase * @param string $username Database username * @param string $password Database password */ - public function __construct($dsn, $username, $password) - { + public function __construct( + $dsn, $username, $password, + $callTypes = 'i', $msns = array() + ) { + parent::__construct($callTypes, $msns); $this->db = new \PDO( $dsn, $username, $password, array( @@ -60,6 +63,13 @@ 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( diff --git a/src/callnotifier/Logger/CallDreambox.php b/src/callnotifier/Logger/CallDreambox.php index 4720c4c..d617f1d 100644 --- a/src/callnotifier/Logger/CallDreambox.php +++ b/src/callnotifier/Logger/CallDreambox.php @@ -5,18 +5,26 @@ class Logger_CallDreambox extends Logger_CallBase { protected $host; - public function __construct($host) + public function __construct($host, $callTypes = 'i', $msns = array()) { + parent::__construct($callTypes, $msns); $this->host = $host; } public function log($type, $arData) { - switch ($type) { - case 'startingCall': - $this->displayStart($arData['call']); - break; + if ($type != 'startingCall') { + return; + } + + $call = $arData['call']; + if (!$this->hasValidType($call)) { + return; + } + if (!$this->hasValidMsn($call)) { + return; } + $this->displayStart($call); } diff --git a/src/callnotifier/Logger/CallEcho.php b/src/callnotifier/Logger/CallEcho.php index ad13406..70fad4a 100644 --- a/src/callnotifier/Logger/CallEcho.php +++ b/src/callnotifier/Logger/CallEcho.php @@ -7,12 +7,23 @@ class Logger_CallEcho extends Logger_CallBase { switch ($type) { case 'startingCall': - $this->displayStart($arData['call']); + $displayMethod = 'displayStart'; break; case 'finishedCall': - $this->displayFinished($arData['call']); + $displayMethod = 'displayFinished'; break; + default: + return; } + + $call = $arData['call']; + if (!$this->hasValidType($call)) { + return; + } + if (!$this->hasValidMsn($call)) { + return; + } + $this->$displayMethod($arData['call']); } diff --git a/src/callnotifier/Logger/CallFile.php b/src/callnotifier/Logger/CallFile.php index 850e840..12a5b08 100644 --- a/src/callnotifier/Logger/CallFile.php +++ b/src/callnotifier/Logger/CallFile.php @@ -5,8 +5,6 @@ class Logger_CallFile extends Logger_CallBase { protected $file; protected $fileHdl; - protected $callTypes; - protected $msns; /** * Create a new file call logger. It logs finished calls into a file. @@ -45,22 +43,10 @@ class Logger_CallFile extends Logger_CallBase } $call = $arData['call']; - - //check if call type matches - if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') { - return; - } - if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') { + if (!$this->hasValidType($call)) { return; } - - if ($call->type == CallMonitor_Call::INCOMING) { - $msn = $call->to; - } else { - $msn = $call->from; - } - if (count($this->msns) > 0 && !in_array($msn, $this->msns)) { - //msn shall not be logged + if (!$this->hasValidMsn($call)) { return; } diff --git a/src/callnotifier/Logger/CallNotifySend.php b/src/callnotifier/Logger/CallNotifySend.php index 477a253..74c930e 100644 --- a/src/callnotifier/Logger/CallNotifySend.php +++ b/src/callnotifier/Logger/CallNotifySend.php @@ -7,12 +7,23 @@ class Logger_CallNotifySend extends Logger_CallBase { switch ($type) { case 'startingCall': - $this->displayStart($arData['call']); + $displayMethod = 'displayStart'; break; case 'finishedCall': - $this->displayFinished($arData['call']); + $displayMethod = 'displayFinished'; break; + default: + return; } + + $call = $arData['call']; + if (!$this->hasValidType($call)) { + return; + } + if (!$this->hasValidMsn($call)) { + return; + } + $this->$displayMethod($arData['call']); } -- 2.30.2