2021-01-24 06:10:44 +00:00
|
|
|
<?php
|
|
|
|
namespace OCA\CTAutoMailHooks\Hooks;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCA\CTAutoMailHooks\Config;
|
|
|
|
class UserHooks {
|
|
|
|
private $logger;
|
|
|
|
private $config;
|
|
|
|
private $userManager;
|
|
|
|
private $logContext = ['app' => 'ct_auto_mail_hooks'];
|
|
|
|
public function __construct(
|
|
|
|
ILogger $logger, Config $config, IUserManager $userManager) {
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
}
|
|
|
|
public function register() {
|
|
|
|
$this->userManager->listen(
|
|
|
|
'\OC\User', 'postCreateUser',$this->createUserCB());
|
|
|
|
$this->userManager->listen(
|
|
|
|
'\OC\User', 'preDelete', $this->deleteUserCB());
|
2021-02-22 03:06:57 +00:00
|
|
|
$this->userManager->listen(
|
|
|
|
'\OC\User', 'postSetPassword', $this->updateEmailPasswordCB());
|
2021-01-24 06:10:44 +00:00
|
|
|
}
|
|
|
|
private function createUserCB() {
|
|
|
|
return function (\OC\User\User $user, string $password) {
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$name = $user->getDisplayName();
|
|
|
|
$email_suffix = $this->config->getEmailAddressSuffix();
|
2021-01-24 07:40:17 +00:00
|
|
|
$quota = $this->config->getEmailQuotaMB();
|
2021-01-24 06:10:44 +00:00
|
|
|
$newuser = array(
|
|
|
|
'userid' => $uid,
|
|
|
|
'password' => $password,
|
|
|
|
'name' => $name,
|
2021-01-24 07:40:17 +00:00
|
|
|
'email' => $uid . $email_suffix,
|
|
|
|
'quota' => $quota // in MB
|
2021-01-24 06:10:44 +00:00
|
|
|
);
|
|
|
|
if($this->createEmailAccount($newuser)) {
|
|
|
|
$this->logger->warning(
|
|
|
|
"Automatically created mail account for uid " . $uid
|
|
|
|
. " with e-mail address \"" . $newuser["email"]
|
|
|
|
. " e-mail address suffix was \"". $email_suffix . "\"."
|
|
|
|
, $this->logContext);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->logger->error(
|
|
|
|
"Error creating mail account OR mail folders for uid "
|
|
|
|
. $uid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
private function deleteUserCB() {
|
|
|
|
return function (\OC\User\User $user) {
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$email_suffix = $this->config->getEmailAddressSuffix();
|
|
|
|
$email = $uid . $email_suffix;
|
|
|
|
$user_data = array('email' => $email);
|
|
|
|
if($this->deleteEmailAccount($user_data))
|
|
|
|
$this->logger->warning(
|
|
|
|
"Deleted mail account: " . $email, $this->logContext);
|
|
|
|
else $this->logger->error("Error deleting mail account" . $uid);
|
|
|
|
};
|
|
|
|
}
|
2021-02-22 03:06:57 +00:00
|
|
|
private function updateEmailPasswordCB() {
|
|
|
|
return function (\OC\User\User $user, string $password) {
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$email_suffix = $this->config->getEmailAddressSuffix();
|
|
|
|
$email = $uid . $email_suffix;
|
|
|
|
$user_data = array(
|
|
|
|
'email' => $email,
|
|
|
|
'password' => $password
|
|
|
|
);
|
|
|
|
if($this->updateEmailPassword($user_data))
|
|
|
|
$this->logger->warning(
|
|
|
|
"Updated glesys password for account: " . $email,
|
|
|
|
$this->logContext);
|
2021-02-22 03:29:12 +00:00
|
|
|
else $this->logger->error(
|
|
|
|
"Error updating mail password for account" . $uid);
|
|
|
|
};
|
|
|
|
}
|
2021-02-22 03:06:57 +00:00
|
|
|
private function createImapFolders($user_data) {
|
2021-01-24 06:10:44 +00:00
|
|
|
$server = 'mail.glesys.se';
|
2021-02-22 03:06:57 +00:00
|
|
|
$email = $user_data['email'];
|
|
|
|
$passw = $user_data['password'];
|
2021-01-24 06:10:44 +00:00
|
|
|
$folders = array('Sent', 'Drafts', 'Trash', 'Junk', 'Archive');
|
|
|
|
$mbox = \imap_open("{" . $server . "}", $email, $passw, OP_HALFOPEN);
|
|
|
|
if(!$mbox){
|
|
|
|
$message = "can't connect: " . \imap_last_error();
|
|
|
|
$this->logger->error($message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($folders as $folder) {
|
2021-01-24 08:18:26 +00:00
|
|
|
if (! @\imap_createmailbox(
|
|
|
|
$mbox,
|
|
|
|
\imap_utf7_encode("{".$server."}INBOX.".$folder))) {
|
|
|
|
$message = 'Error creating ' . $folder . ' folder: ' .
|
|
|
|
\imap_last_error();
|
2021-01-24 06:10:44 +00:00
|
|
|
$this->logger->error($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
\imap_close($mbox);
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-22 03:06:57 +00:00
|
|
|
private function createEmailAccount($user_data) {
|
2021-01-24 06:10:44 +00:00
|
|
|
$keys = $this->config->getGlesysKeys();
|
|
|
|
$fields = array(
|
2021-02-22 03:06:57 +00:00
|
|
|
'emailaccount' => $user_data['email'],
|
|
|
|
'password' => $user_data['password'],
|
|
|
|
'quota' => $user_data['quota']
|
2021-01-24 06:10:44 +00:00
|
|
|
);
|
|
|
|
$query = http_build_query($fields);
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL,
|
|
|
|
'https://api.glesys.com/email/createaccount/');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, $keys);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
if (curl_errno($ch)) {
|
|
|
|
$message = 'Error:' . curl_error($ch);
|
|
|
|
$this->logger->error($message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
$xml = simplexml_load_string($result);
|
|
|
|
$code = $xml->status->code;
|
2021-02-22 03:06:57 +00:00
|
|
|
if ($code == 200) return $this->createImapFolders($user_data);
|
|
|
|
else $this->logger->warning(
|
|
|
|
"Error: non-200 status: " . $xml->status->text);
|
2021-01-24 06:10:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-22 03:06:57 +00:00
|
|
|
private function deleteEmailAccount($user_data){
|
2021-01-24 06:10:44 +00:00
|
|
|
$ch = curl_init();
|
|
|
|
$keys = $this->config->getGlesysKeys();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, 'https://api.glesys.com/email/delete/');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
2021-02-22 03:06:57 +00:00
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user_data["email"]);
|
2021-01-24 06:10:44 +00:00
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, $keys);
|
|
|
|
$result = curl_exec($ch);
|
2021-02-22 03:06:57 +00:00
|
|
|
if (curl_errno($ch)) {
|
|
|
|
$message = 'Error: ' . curl_error($ch);
|
|
|
|
$this->logger->error($message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
private function updateEmailPassword($user_data) {
|
|
|
|
$keys = $this->config->getGlesysKeys();
|
|
|
|
$fields = array(
|
|
|
|
'emailaccount' => $user_data['email'],
|
|
|
|
'password' => $user_data['password']
|
|
|
|
);
|
|
|
|
$query = http_build_query($fields);
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL,
|
|
|
|
'https://api.glesys.com/email/editaccount/');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, $keys);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
|
|
|
|
$result = curl_exec($ch);
|
2021-01-24 06:10:44 +00:00
|
|
|
if (curl_errno($ch)) {
|
|
|
|
$message = 'Error:' . curl_error($ch);
|
|
|
|
$this->logger->error($message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
curl_close($ch);
|
2021-02-22 03:06:57 +00:00
|
|
|
$xml = simplexml_load_string($result);
|
|
|
|
$code = $xml->status->code;
|
|
|
|
if ($code != 200){
|
|
|
|
$this->logger->warning(
|
|
|
|
"Error: non-200 status: " . $xml->status->text);
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-24 06:10:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|