aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Logger/CallFileTop.php
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-08-24 07:50:59 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-08-24 07:50:59 +0200
commit3ea58f544baf54f65ae9b850f1bd7788163d5755 (patch)
tree169f1bedc62db5b8861b990cce96d839efc12a40 /src/callnotifier/Logger/CallFileTop.php
parentb0d0370b7269a8e5fc4254b3f47f266551957fc4 (diff)
downloadauerswald-callnotifier-3ea58f544baf54f65ae9b850f1bd7788163d5755.tar.gz
auerswald-callnotifier-3ea58f544baf54f65ae9b850f1bd7788163d5755.zip
file top logger
Diffstat (limited to 'src/callnotifier/Logger/CallFileTop.php')
-rw-r--r--src/callnotifier/Logger/CallFileTop.php100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/callnotifier/Logger/CallFileTop.php b/src/callnotifier/Logger/CallFileTop.php
new file mode 100644
index 0000000..6caa551
--- /dev/null
+++ b/src/callnotifier/Logger/CallFileTop.php
@@ -0,0 +1,100 @@
+<?php
+namespace callnotifier;
+
+/**
+ * Logs finished calls into a file, latest on top.
+ * The date is also show on a extra line, grouping the following calls.
+ *
+ * Suitable for Dreambox display with CurlyTx
+ *
+ * Example:
+ *
+ * 24.08.2012, Friday
+ * 07:48 nach Herbert 00:00:03
+ * 08:13 von 02426140162 00:02:21
+ */
+class Logger_CallFileTop extends Logger_CallBase
+{
+ protected $file;
+
+ /**
+ * Create a new file call logger.
+ *
+ * @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;
+
+ $fileHdl = fopen($this->file, 'a');
+ if (!$fileHdl) {
+ throw new \Exception(
+ 'Cannot open call log file for writing: ' . $this->file
+ );
+ }
+ fclose($fileHdl);
+ }
+
+ public function log($type, $arData)
+ {
+ if ($type != 'finishedCall') {
+ return;
+ }
+
+ $call = $arData['call'];
+ if (!$this->hasValidType($call)) {
+ return;
+ }
+ if (!$this->hasValidMsn($call)) {
+ return;
+ }
+
+ list($logline, $date) = $this->createLogEntry($call);
+ $arLines = file($this->file);
+ if (isset($arLines[0]) && $arLines[0] == $date) {
+ //same date as previous log entry
+ $arLines = array_pad($arLines, -count($arLines) - 1, '');
+ } else {
+ $arLines = array_pad($arLines, -count($arLines) - 2, '');
+ }
+ $arLines[0] = $date;
+ $arLines[1] = $logline;
+
+ //keep 50 lines only
+ array_splice($arLines, 50);
+
+ file_put_contents($this->file, implode('', $arLines));
+ }
+
+
+ protected function createLogEntry(CallMonitor_Call $call)
+ {
+ $this->addUnsetVars($call);
+ $str = ' ' . date('H:i', $call->start);
+ if ($call->type == CallMonitor_Call::INCOMING) {
+ $str .= ' von ' . str_pad($this->getNumberString($call, 'from'), 30);
+ } else {
+ $str .= ' nach ' . str_pad($this->getNumberString($call, 'to'), 30);
+ }
+
+ $str .= ' ' . date('H:i:s', $call->end - $call->start - 3600);
+
+ return array($str . "\n", date("d.m.Y, l\n", $call->start));
+ }
+
+}
+
+?>