Completed
Pull Request — master (#1863)
by
unknown
03:15
created

HmailAliasesPlugin::Init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
class HmailAliasesPlugin extends \RainLoop\Plugins\AbstractPlugin
4
{
5
    public function Init()
6
    {
7
        $this->addHook('event.login-post-login-provide', 'loginPostLoginProvide');
8
    }
9
10
    public function Supported()
11
    {
12
        if (!class_exists('mysqli')) {
13
            return 'The PHP exention mysqli must be installed to use this plugin';
14
        }
15
16
        return '';
17
    }
18
19
    /**
20
    * @param \RainLoop\Model\Account $oAccount
21
    * @throws \RainLoop\Exceptions\ClientException
22
    */
23
    public function loginPostLoginProvide(\RainLoop\Model\Account &$oAccount)
24
    {
25
        $dbservername = \trim($this->Config()->Get('plugin', 'hamilserver', ''));
26
        $dbusername = \trim($this->Config()->Get('plugin', 'hmaildbusername', ''));
27
        $dbpassword = \trim($this->Config()->Get('plugin', 'hmaildbpassword', ''));
28
        $dbname = \trim($this->Config()->Get('plugin', 'hmaildbtable', ''));
29
        $dbport = \trim($this->Config()->Get('plugin', 'hmaildbport', ''));
30
        $datadir = \trim($this->Config()->Get('plugin', 'rainloopDatalocation', ''));
31
32
        if ($datadir != ""){
33
          $userpath = $datadir.'data/_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities';
34
        } else {
35
          $userpath = APP_INDEX_ROOT_PATH.'_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities';
36
        }
37
38
        $hmailconn = mysqli_connect($dbservername, $dbusername, $dbpassword, $dbname, $dbport);
39
40
        // Check connection
41
        if (!$hmailconn) {
42
            echo "Hmail-aliases: connection to db failed";
43
            return;
44
        }
45
        //Get aliases
46
        $result = $hmailconn->query("SELECT * FROM " . $dbname . ".hm_aliases WHERE aliasvalue='".$oAccount->Email()."'");
47
48
        if ($result->num_rows > 0) {
49
            $newidentitiesobj = array();
50
51
            //Get user account
52
            $result2 = $hmailconn->query("SELECT * FROM " . $dbname . ".hm_accounts WHERE accountaddress='".$oAccount->Email()."'");
53
            $result2 = $result2->fetch_assoc();
54
            $firstname = $result2['accountpersonfirstname'];
55
            $lastname = $result2['accountpersonlastname'];
56
57
            if ($firstname == "" && $lastname == "") {
58
                $name = "";
59
            } else {
60
                $name = $firstname." ".$lastname;
61
            }
62
63
            //Get existing settings. If not a alias created by hmail. Transfer settings to the new array.
64
            $identities = file_get_contents($userpath, true);
65
            if ($identities != "") {
66
                $identities = json_decode($identities, true);
67
                error_log(print_r($identities, true));
68
                foreach ($identities as $row) {
69
                    if (strpos($row['Id'], 'HMAIL') === false) {
70
                        array_push($newidentitiesobj, $row);
71
                    }
72
                }
73
            }
74
75
            // output data of each row
76
            while ($row = $result->fetch_assoc()) {
77
                $obj = array();
78
                $obj['Id'] = "HMAIL".base64_encode($row["aliasname"]);
79
                $obj['Email'] = $row["aliasname"];
80
                $obj['Name'] = $name;
81
                $obj['ReplyTo'] = "";
82
                $obj['Bcc'] = "";
83
                $obj['Signature'] = "";
84
                $obj['SignatureInsertBefore'] = false;
85
                array_push($newidentitiesobj, $obj);
86
            }
87
            file_put_contents($userpath, json_encode($newidentitiesobj));
88
        }
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function configMapping()
95
    {
96
        return array(
97
          \RainLoop\Plugins\Property::NewInstance('hamilserver')->SetLabel('db-host')
98
            ->SetDefaultValue('localhost'),
99
          \RainLoop\Plugins\Property::NewInstance('hmaildbusername')->SetLabel('db-username')
100
            ->SetDefaultValue(''),
101
          \RainLoop\Plugins\Property::NewInstance('hmaildbpassword')->SetLabel('db-password')
102
            ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD),
103
          \RainLoop\Plugins\Property::NewInstance('hmaildbtable')->SetLabel('db-table')
104
            ->SetDefaultValue('mailserver'),
105
          \RainLoop\Plugins\Property::NewInstance('hmaildbport')->SetLabel('db-port')
106
            ->SetDefaultValue('3306')
107
            ->SetDescription('Connect to mysql hmailserver. The user must have rights to read "hm_aliases" table.'),
108
          \RainLoop\Plugins\Property::NewInstance('rainloopDatalocation')->SetLabel('Data folder location')
109
            ->SetDefaultValue('')
110
            ->SetDescription('Incase of custom data directory location. Eg. nextcloud/owncloud version (Leave blank for default)')
111
        );
112
    }
113
}
114