1<?php
2
3/**
4 * Class helper_plugin_loglog_main
5 */
6class helper_plugin_loglog_main extends DokuWiki_Plugin
7{
8    const LOGTYPE_AUTH_OK = 'auth_success';
9    const LOGTYPE_AUTH_FAIL = 'auth_failed';
10
11    /**
12     * @var helper_plugin_loglog_logging
13     */
14    protected $logHelper;
15
16    public function __construct()
17    {
18        $this->logHelper = $this->loadHelper('loglog_logging');
19    }
20
21    /**
22     * Deduce the type of logged event from message field. Those types are used in a dropdown filter
23     * in admin listing of activities, as well as when generating reports to send per email.
24     *
25     * @param string $msg
26     * @return string
27     */
28    public function getLogTypeFromMsg($msg)
29    {
30        $filter = 'other';
31        if (in_array(
32            $msg,
33            [
34                'logged in temporarily',
35                'logged in permanently',
36                'logged off',
37                'has been automatically logged off'
38            ]
39        )) {
40            $filter = 'auth_ok';
41        } elseif (in_array(
42            $msg,
43            [
44                'failed login attempt',
45            ]
46        )) {
47            $filter = 'auth_error';
48        } elseif (strpos($msg, 'admin') === 0) {
49            $filter = 'admin';
50        }
51
52        return $filter;
53    }
54
55    /**
56     * Sends emails
57     *
58     * @param string $email
59     * @param string $subject
60     * @param string $text
61     * @return bool
62     */
63    public function sendEmail($email, $subject, $text, $textrep = [])
64    {
65        $html = p_render('xhtml', p_get_instructions($text), $info);
66
67        $mail = new Mailer();
68        $mail->to($email);
69        $mail->subject($subject);
70        $mail->setBody($text, $textrep, null, $html);
71        return $mail->send();
72    }
73
74    /**
75     * Returns a string corresponding to $key in a given $context,
76     * empty string if nothing has been found in the string repository.
77     *
78     * @param string $context
79     * @param string $key
80     * @return string
81     */
82    public function getNotificationString($context, $key)
83    {
84        $stringRepo = [
85            self::LOGTYPE_AUTH_FAIL => [
86                'msgNeedle' => 'failed login attempt',
87                'emailSubjectLang' => 'email_max_failed_logins_subject'
88            ],
89            self::LOGTYPE_AUTH_OK => [
90                'msgNeedle' => 'logged in',
91                'emailSubjectLang' => 'email_max_success_logins_subject'
92            ],
93        ];
94
95        return isset($stringRepo[$context][$key]) ? $stringRepo[$context][$key] : '';
96    }
97}
98