X-Git-Url: https://git.cweiske.de/auerswald-callnotifier.git/blobdiff_plain/7a39f860d268b6afd1e170cb4007726691d5c2b5..f4bd5ef0c711aa9a3d0f6b468370a8c14c0e247a:/src/callnotifier/Logger/CallFile.php diff --git a/src/callnotifier/Logger/CallFile.php b/src/callnotifier/Logger/CallFile.php new file mode 100644 index 0000000..85bf287 --- /dev/null +++ b/src/callnotifier/Logger/CallFile.php @@ -0,0 +1,118 @@ +file = $file; + $this->callTypes = $callTypes; + $this->msns = (array)$msns; + + $this->fileHdl = fopen($this->file, 'a'); + if (!$this->fileHdl) { + throw new \Exception( + 'Cannot open call log file for writing: ' . $this->file + ); + } + } + + public function log($type, $arData) + { + if ($type != 'finishedCall') { + return; + } + + $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') { + 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 + return; + } + + fwrite($this->fileHdl, $this->createLogEntry($call)); + } + + + protected function createLogEntry(CallMonitor_Call $call) + { + $this->addUnsetVars($call); + $str = date('Y-m-d H:i:s', $call->start); + if ($call->type == CallMonitor_Call::INCOMING) { + $str .= ' ' . $call->to + . ' von ' . $call->fromName; + if ($call->fromLocation) { + $str .= ' aus ' . $call->fromLocation; + } + $str .= ' ' . $this->getNumber($call->from); + } else { + $str .= ' ' . $call->from + . ' nach ' . $call->toName; + if ($call->toLocation) { + $str .= ' aus ' . $call->toLocation; + } + $str .= ' ' . $this->getNumber($call->to); + } + + $str .= ', Dauer ' . date('H:i:s', $call->end - $call->start - 3600); + + return $str . "\n"; + } + + protected function getNumber($number) + { + if ($number == '') { + $number = '*anonym*'; + } + return str_pad($number, 12, ' ', STR_PAD_RIGHT); + } + + protected function addUnsetVars($call) + { + static $expectedVars = array( + 'toName', 'fromName', 'toLocation', 'fromLocation' + ); + foreach ($expectedVars as $varName) { + if (!isset($call->$varName)) { + $call->$varName = null; + } + } + } + +} + +?>