file call logger
authorChristian Weiske <cweiske@cweiske.de>
Mon, 6 Aug 2012 15:16:23 +0000 (17:16 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 6 Aug 2012 15:16:23 +0000 (17:16 +0200)
.gitignore [new file with mode: 0644]
src/callnotifier/CLI.php
src/callnotifier/Logger/CallFile.php [new file with mode: 0644]
src/callnotifier/MessageHandler.php

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..397b4a7
--- /dev/null
@@ -0,0 +1 @@
+*.log
index 16c95b0f6f6ea5845634632929ba0cf7eab9fc7b..6165f15550a56cd71be319125782e28f7be827a6 100644 (file)
@@ -36,6 +36,7 @@ class CLI
         );
 
         $callMonitor = new CallMonitor($this->config, $log);
         );
 
         $callMonitor = new CallMonitor($this->config, $log);
+        /*
         $callMonitor->addDetailler(
             new CallMonitor_Detailler_LDAP(
                 array(
         $callMonitor->addDetailler(
             new CallMonitor_Detailler_LDAP(
                 array(
@@ -53,6 +54,16 @@ class CLI
                 'opengeodb'
             )
         );
                 'opengeodb'
             )
         );
+        */
+
+        $log->addLogger(
+            new Logger_CallFile('incoming.log', 'i', '40862'),
+            array('finishedCall')
+        );
+        $log->addLogger(
+            new Logger_CallFile('all.log'),
+            array('finishedCall')
+        );
 
         $handler = new MessageHandler($this->config, $log, $callMonitor);
 
 
         $handler = new MessageHandler($this->config, $log, $callMonitor);
 
diff --git a/src/callnotifier/Logger/CallFile.php b/src/callnotifier/Logger/CallFile.php
new file mode 100644 (file)
index 0000000..85bf287
--- /dev/null
@@ -0,0 +1,118 @@
+<?php
+namespace callnotifier;
+
+class Logger_CallFile implements Logger
+{
+    protected $file;
+    protected $fileHdl;
+    protected $callTypes;
+    protected $msns;
+
+    /**
+     * Create a new file call logger. It logs finished calls into a file.
+     *
+     * @param string $file      Path to the file to log the calls in.
+     * @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(
+        $file,
+        $callTypes = 'io',
+        $msns = array()
+    ) {
+        $this->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;
+            }
+        }
+    }
+
+}
+
+?>
index 78a726de60ada2566d71b3bc4263a07e11b0844a..90cd87cafa4da619e6fa6d2252029897b6db2ffa 100644 (file)
@@ -88,7 +88,7 @@ class MessageHandler
         }
         $this->dumpHdl = fopen($this->config->dumpFile, 'w');
         if (!$this->dumpHdl) {
         }
         $this->dumpHdl = fopen($this->config->dumpFile, 'w');
         if (!$this->dumpHdl) {
-            throw new Exception('Cannot open dump file for writing');
+            throw new \Exception('Cannot open dump file for writing');
         }
     }
 
         }
     }