international numbers have a +
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallFile.php
1 <?php
2 namespace callnotifier;
3
4 class Logger_CallFile extends Logger_CallBase
5 {
6     protected $file;
7     protected $fileHdl;
8     protected $callTypes;
9     protected $msns;
10
11     /**
12      * Create a new file call logger. It logs finished calls into a file.
13      *
14      * @param string $file      Path to the file to log the calls in.
15      * @param string $callTypes Which types of call to log:
16      *                          - "i" - incoming calls only
17      *                          - "o" - outgoing calls only
18      *                          - "io" - both incoming and outgoing calls
19      * @param array  $msns      Array of MSN (Multi Subscriber Number) that
20      *                          calls to shall get logged.
21      *                          If the array is empty, calls to all MSNs get
22      *                          logged.
23      */
24     public function __construct(
25         $file,
26         $callTypes = 'io',
27         $msns = array()
28     ) {
29         $this->file      = $file;
30         $this->callTypes = $callTypes;
31         $this->msns      = (array)$msns;
32
33         $this->fileHdl = fopen($this->file, 'a');
34         if (!$this->fileHdl) {
35             throw new \Exception(
36                 'Cannot open call log file for writing: ' . $this->file
37             );
38         }
39     }
40
41     public function log($type, $arData)
42     {
43         if ($type != 'finishedCall') {
44             return;
45         }
46
47         $call = $arData['call'];
48
49         //check if call type matches
50         if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') {
51             return;
52         }
53         if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') {
54             return;
55         }
56
57         if ($call->type == CallMonitor_Call::INCOMING) {
58             $msn = $call->to;
59         } else {
60             $msn = $call->from;
61         }
62         if (count($this->msns) > 0 && !in_array($msn, $this->msns)) {
63             //msn shall not be logged
64             return;
65         }
66
67         fwrite($this->fileHdl, $this->createLogEntry($call));
68     }
69
70
71     protected function createLogEntry(CallMonitor_Call $call)
72     {
73         $this->addUnsetVars($call);
74         $str = date('Y-m-d H:i:s', $call->start);
75         if ($call->type == CallMonitor_Call::INCOMING) {
76             $str .= ' ' . $call->to
77                 . ' von  ' . $call->fromName;
78             if ($call->fromLocation) {
79                 $str .= ' aus ' . $call->fromLocation;
80             }
81             $str .= ' ' . $this->getNumber($call->from);
82         } else {
83             $str .= ' ' . $call->from
84                 . ' nach ' . $call->toName;
85             if ($call->toLocation) {
86                 $str .= ' aus ' . $call->toLocation;
87             }
88             $str .= ' ' . $this->getNumber($call->to);
89         }
90
91         $str .= ', Dauer ' . date('H:i:s', $call->end - $call->start - 3600);
92
93         return $str . "\n";
94     }
95
96     protected function getNumber($number)
97     {
98         if ($number == '') {
99             $number = '*anonym*';
100         }
101         return str_pad($number, 12, ' ', STR_PAD_RIGHT);
102     }
103
104 }
105
106 ?>