WIP commit

This commit is contained in:
Cassowary Rusnov 2023-09-05 13:25:24 -07:00
parent 20c991ccf1
commit 4919aaf8d8

View File

@ -24,17 +24,18 @@ class UserHooks {
} }
private function createUserCB() { private function createUserCB() {
return function (\OC\User\User $user, string $password) { return function (\OC\User\User $user, string $password) {
$uid = $user->getUID();
$name = $user->getDisplayName();
$email_suffix = $this->config->getEmailAddressSuffix();
$quota = $this->config->getEmailQuotaMB();
$newuser = array( $newuser = array(
'userid' => $uid, 'email' => $user->getUID() . $this->config->getEmailAddressSuffix(),
'password' => $password, 'displayed_name' => $user->getDisplayName(),
'name' => $name, 'raw_password' => $password,
'email' => $uid . $email_suffix, 'quota_bytes' => $this->config->getEmailQuotaMB() * 1024 * 1024,
'quota' => $quota // in MB 'enabled' => true,
'enable_imap' => true,
'spam_enable' => true,
'spam_mark_as_read' => true,
'spam_threshold' => 80
); );
if($this->createEmailAccount($newuser)) { if($this->createEmailAccount($newuser)) {
$this->logger->warning( $this->logger->warning(
"Automatically created mail account for uid " . $uid "Automatically created mail account for uid " . $uid
@ -49,132 +50,128 @@ class UserHooks {
} }
}; };
} }
private function deleteUserCB() { private function deleteUserCB() {
return function (\OC\User\User $user) { return function (\OC\User\User $user) {
$uid = $user->getUID(); $email = $user->getUID() . $this->config->getEmailAddressSuffix();
$email_suffix = $this->config->getEmailAddressSuffix(); if($this->deleteEmailAccount($email)) {
$email = $uid . $email_suffix; $this->logger->warning("Deleted mail account: " . $email, $this->logContext);
$user_data = array('email' => $email); } else {
if($this->deleteEmailAccount($user_data)) $this->logger->error("Error deleting mail account" . $email);
$this->logger->warning( }
"Deleted mail account: " . $email, $this->logContext);
else $this->logger->error("Error deleting mail account" . $uid);
}; };
} }
private function updateEmailPasswordCB() { private function updateEmailPasswordCB() {
return function (\OC\User\User $user, string $password) { return function (\OC\User\User $user, string $password) {
$uid = $user->getUID(); $email = $user->getUID() . $this->config->getEmailAddressSuffix();
$email_suffix = $this->config->getEmailAddressSuffix(); if($this->updateEmailPassword($email, $password)) {
$email = $uid . $email_suffix; $this->logger->warning("Updated glesys password for account: " . $email, $this->logContext);
$user_data = array( } else {
'email' => $email, $this->logger->error("Error updating mail password for account" . $email);
'password' => $password }
);
if($this->updateEmailPassword($user_data))
$this->logger->warning(
"Updated glesys password for account: " . $email,
$this->logContext);
else $this->logger->error(
"Error updating mail password for account" . $uid);
}; };
} }
private function createImapFolders($user_data) { private function createImapFolders($user_data) {
$server = 'mail.glesys.se'; // $server = 'mail.glesys.se';
$email = $user_data['email']; // $email = $user_data['email'];
$passw = $user_data['password']; // $passw = $user_data['password'];
$folders = array('Sent', 'Drafts', 'Trash', 'Junk', 'Archive'); // $folders = array('Sent', 'Drafts', 'Trash', 'Junk', 'Archive');
$mbox = \imap_open("{" . $server . "}", $email, $passw, OP_HALFOPEN); // $mbox = \imap_open("{" . $server . "}", $email, $passw, OP_HALFOPEN);
if(!$mbox){ // if(!$mbox){
$message = "can't connect: " . \imap_last_error(); // $message = "can't connect: " . \imap_last_error();
$this->logger->error($message); // $this->logger->error($message);
return false; // return false;
} // }
foreach ($folders as $folder) { // foreach ($folders as $folder) {
if (! @\imap_createmailbox( // if (! @\imap_createmailbox(
$mbox, // $mbox,
\imap_utf7_encode("{".$server."}INBOX.".$folder))) { // \imap_utf7_encode("{".$server."}INBOX.".$folder))) {
$message = 'Error creating ' . $folder . ' folder: ' . // $message = 'Error creating ' . $folder . ' folder: ' .
\imap_last_error(); // \imap_last_error();
$this->logger->error($message); // $this->logger->error($message);
} // }
} // }
\imap_close($mbox); // \imap_close($mbox);
return true; // return true;
} }
private function createEmailAccount($user_data) { private function createEmailAccount($user_data) {
$keys = $this->config->getGlesysKeys(); // $keys = $this->config->getGlesysKeys();
$fields = array( // $fields = array(
'emailaccount' => $user_data['email'], // 'emailaccount' => $user_data['email'],
'password' => $user_data['password'], // 'password' => $user_data['password'],
'quota' => $user_data['quota'] // 'quota' => $user_data['quota']
); // );
$query = http_build_query($fields); // $query = http_build_query($fields);
$ch = curl_init(); // $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, // curl_setopt($ch, CURLOPT_URL,
'https://api.glesys.com/email/createaccount/'); // 'https://api.glesys.com/email/createaccount/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); // curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $keys); // curl_setopt($ch, CURLOPT_USERPWD, $keys);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); // curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$result = curl_exec($ch); // $result = curl_exec($ch);
if (curl_errno($ch)) { // if (curl_errno($ch)) {
$message = 'Error:' . curl_error($ch); // $message = 'Error:' . curl_error($ch);
$this->logger->error($message); // $this->logger->error($message);
return false; // return false;
} // }
curl_close($ch); // curl_close($ch);
$xml = simplexml_load_string($result); // $xml = simplexml_load_string($result);
$code = $xml->status->code; // $code = $xml->status->code;
if ($code == 200) return $this->createImapFolders($user_data); // if ($code == 200) return $this->createImapFolders($user_data);
else $this->logger->warning( // else $this->logger->warning(
"Error: non-200 status: " . $xml->status->text); // "Error: non-200 status: " . $xml->status->text);
return false; // return false;
} }
private function deleteEmailAccount($user_data) { private function deleteEmailAccount($user_data) {
$ch = curl_init(); // $ch = curl_init();
$keys = $this->config->getGlesysKeys(); // $keys = $this->config->getGlesysKeys();
curl_setopt($ch, CURLOPT_URL, 'https://api.glesys.com/email/delete/'); // curl_setopt($ch, CURLOPT_URL, 'https://api.glesys.com/email/delete/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user_data["email"]); // curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user_data["email"]);
curl_setopt($ch, CURLOPT_POST, 1); // curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $keys); // curl_setopt($ch, CURLOPT_USERPWD, $keys);
$result = curl_exec($ch); // $result = curl_exec($ch);
if (curl_errno($ch)) { // if (curl_errno($ch)) {
$message = 'Error: ' . curl_error($ch); // $message = 'Error: ' . curl_error($ch);
$this->logger->error($message); // $this->logger->error($message);
return false; // return false;
} // }
curl_close($ch); // curl_close($ch);
return true; // return true;
} }
private function updateEmailPassword($user_data) { private function updateEmailPassword($user_data) {
$keys = $this->config->getGlesysKeys(); // $keys = $this->config->getGlesysKeys();
$fields = array( // $fields = array(
'emailaccount' => $user_data['email'], // 'emailaccount' => $user_data['email'],
'password' => $user_data['password'] // 'password' => $user_data['password']
); // );
$query = http_build_query($fields); // $query = http_build_query($fields);
$ch = curl_init(); // $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, // curl_setopt($ch, CURLOPT_URL,
'https://api.glesys.com/email/editaccount/'); // 'https://api.glesys.com/email/editaccount/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); // curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $keys); // curl_setopt($ch, CURLOPT_USERPWD, $keys);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); // curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$result = curl_exec($ch); // $result = curl_exec($ch);
if (curl_errno($ch)) { // if (curl_errno($ch)) {
$message = 'Error:' . curl_error($ch); // $message = 'Error:' . curl_error($ch);
$this->logger->error($message); // $this->logger->error($message);
return false; // return false;
} // }
curl_close($ch); // curl_close($ch);
$xml = simplexml_load_string($result); // $xml = simplexml_load_string($result);
$code = $xml->status->code; // $code = $xml->status->code;
if ($code != 200){ // if ($code != 200){
$this->logger->warning( // $this->logger->warning(
"Error: non-200 status: " . $xml->status->text); // "Error: non-200 status: " . $xml->status->text);
return false; // return false;
} // }
return true; // return true;
} }
} }