use multibyte functions
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallFileTop.php
1 <?php
2 namespace callnotifier;
3
4 /**
5  * Logs finished calls into a file, latest on top.
6  * The date is also show on a extra line, grouping the following calls.
7  *
8  * Suitable for Dreambox display with CurlyTx
9  *
10  * Example:
11  *
12  * 24.08.2012, Friday
13  *   07:48 nach Herbert                        00:00:03
14  *   08:13 von  02426140162                    00:02:21
15  */
16 class Logger_CallFileTop extends Logger_CallBase
17 {
18     protected $file;
19
20     /**
21      * Create a new file call logger.
22      *
23      * @param string $file      Path to the file to log the calls in.
24      * @param string $callTypes Which types of call to log:
25      *                          - "i" - incoming calls only
26      *                          - "o" - outgoing calls only
27      *                          - "io" - both incoming and outgoing calls
28      * @param array  $msns      Array of MSN (Multi Subscriber Number) that
29      *                          calls to shall get logged.
30      *                          If the array is empty, calls to all MSNs get
31      *                          logged.
32      */
33     public function __construct(
34         $file,
35         $callTypes = 'io',
36         $msns = array()
37     ) {
38         $this->file      = $file;
39         $this->callTypes = $callTypes;
40         $this->msns      = (array)$msns;
41
42         $fileHdl = fopen($this->file, 'a');
43         if (!$fileHdl) {
44             throw new \Exception(
45                 'Cannot open call log file for writing: ' . $this->file
46             );
47         }
48         fclose($fileHdl);
49     }
50
51     public function log($type, $arData)
52     {
53         if ($type != 'finishedCall') {
54             return;
55         }
56
57         $call = $arData['call'];
58         if (!$this->hasValidType($call)) {
59             return;
60         }
61         if (!$this->hasValidMsn($call)) {
62             return;
63         }
64
65         list($logline, $date) = $this->createLogEntry($call);
66         $arLines = file($this->file);
67         if (isset($arLines[0]) && $arLines[0] == $date) {
68             //same date as previous log entry
69             $arLines = array_pad($arLines, -count($arLines) - 1, '');
70         } else if (!isset($arLines[0])) {
71             //empty file
72             $arLines = array_pad($arLines, -count($arLines) - 2, '');
73         } else {
74             //new date
75             $arLines = array_pad($arLines, -count($arLines) - 3, '');
76             $arLines[2] = "\n";
77         }
78         $arLines[0] = $date;
79         $arLines[1] = $logline;
80
81         //keep 50 lines only
82         array_splice($arLines, 50);
83
84         file_put_contents($this->file, implode('', $arLines));
85     }
86
87
88     protected function createLogEntry(CallMonitor_Call $call)
89     {
90         $this->addUnsetVars($call);
91         $str = '  ' . date('H:i', $call->start);
92         if ($call->type == CallMonitor_Call::INCOMING) {
93             $prefix = ' von  ';
94             $numstr = $this->getNumberString($call, 'from');
95         } else {
96             $prefix = ' nach ';
97             $numstr = $this->getNumberString($call, 'to');
98         }
99
100         if ($this->callTypes == 'io') {
101             $str .= $prefix;
102             $str .= Functions::mb_str_pad($numstr, 20);
103         } else {
104             $str .= '  ' . Functions::mb_str_pad($numstr, 25);
105         }
106
107         $str .= ' ' . date('H:i:s', $call->end - $call->start - 3600);
108
109         setlocale(LC_TIME, 'de_DE.utf-8');
110         return array($str . "\n", strftime("%x, %A\n", $call->start));
111     }
112
113 }
114
115 ?>