From 39daf5524c16894b08868a1691ebcacbdbaf4c52 Mon Sep 17 00:00:00 2001 From: Cassowary Date: Wed, 27 Sep 2023 11:38:50 -0700 Subject: [PATCH] Write essential hooks for mailu integration --- lib/Hooks/UserHooks.php | 162 +++++++++++++++++----------------------- 1 file changed, 69 insertions(+), 93 deletions(-) diff --git a/lib/Hooks/UserHooks.php b/lib/Hooks/UserHooks.php index 003eec3..94bd74c 100644 --- a/lib/Hooks/UserHooks.php +++ b/lib/Hooks/UserHooks.php @@ -72,106 +72,82 @@ class UserHooks { } }; } - private function createImapFolders($user_data) { - // $server = 'mail.glesys.se'; - // $email = $user_data['email']; - // $passw = $user_data['password']; - // $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) { - // if (! @\imap_createmailbox( - // $mbox, - // \imap_utf7_encode("{".$server."}INBOX.".$folder))) { - // $message = 'Error creating ' . $folder . ' folder: ' . - // \imap_last_error(); - // $this->logger->error($message); - // } - // } - // \imap_close($mbox); - // return true; - } private function createEmailAccount($user_data) { - // $keys = $this->config->getGlesysKeys(); - // $fields = array( - // 'emailaccount' => $user_data['email'], - // 'password' => $user_data['password'], - // 'quota' => $user_data['quota'] - // ); - // $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; - // if ($code == 200) return $this->createImapFolders($user_data); - // else $this->logger->warning( - // "Error: non-200 status: " . $xml->status->text); - // return false; + $ch = curl_init($this.config->getMailUAPI . 'user'); + $payload = json_encode($user_data); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload ); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', + 'Authorization:' . $this->config->getMailUKeys())); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); + $result = curl_exec($ch); + + if (curl_errno($ch)) { + $message = 'createEmailAccount Error:' . curl_error($ch); + $this->logger->error($message); + return false; + } + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($code == 200) { + return true; + } + + $this->logger->error("createEmailAccount returned ".$code." message: ".$result); + return false; } private function deleteEmailAccount($user_data) { - // $ch = curl_init(); - // $keys = $this->config->getGlesysKeys(); - // curl_setopt($ch, CURLOPT_URL, 'https://api.glesys.com/email/delete/'); - // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - // curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user_data["email"]); - // curl_setopt($ch, CURLOPT_POST, 1); - // curl_setopt($ch, CURLOPT_USERPWD, $keys); - // $result = curl_exec($ch); - // if (curl_errno($ch)) { - // $message = 'Error: ' . curl_error($ch); - // $this->logger->error($message); - // return false; - // } - // curl_close($ch); - // return true; + $ch = curl_init($this.config->getMailUAPI . 'user/' . urlencode($user_data->email)); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $this->config->getMailUKeys())); + + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); + + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); + $result = curl_exec($ch); + + if (curl_errno($ch)) { + $message = 'deleteEmailAccount Error:' . curl_error($ch); + $this->logger->error($message); + return false; + } + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($code == 200) { + return true; + } + + $this->logger->error("deleteEmailAccount returned ".$code." message: ".$result); + return false; } 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); - // 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; - // if ($code != 200){ - // $this->logger->warning( - // "Error: non-200 status: " . $xml->status->text); - // return false; - // } - // return true; + $ch = curl_init($this.config->getMailUAPI . 'user/' . urlencode($user_data->email)); + $payload = json_encode(array("raw_password" => $user_data['password'])); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload ); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', + 'Authorization:' . $this->config->getMailUKeys())); + + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); + $result = curl_exec($ch); + + if (curl_errno($ch)) { + $message = 'updateEmailPassword Error:' . curl_error($ch); + $this->logger->error($message); + return false; + } + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($code == 200) { + return true; + } + + $this->logger->error("updateEmailPassword returned ".$code." message: ".$result); + return false; } }