Skip to content

Commit

Permalink
Merge pull request #5 from Ente/feature-update
Browse files Browse the repository at this point in the history
Adding missing API endpoints
  • Loading branch information
efebagri authored Oct 16, 2024
2 parents 2cb1862 + 695f047 commit 5435700
Show file tree
Hide file tree
Showing 22 changed files with 786 additions and 6 deletions.
Binary file modified README.md
Binary file not shown.
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
"name": "Efe Bagri",
"email": "[email protected]",
"homepage": "https://www.exbil.net"
},
{
"name": "Kees van Kempen",
"homepage": "https://github.com/keessaus"
},
{
"name": "Bryan Böhnke-Avan",
"email": "[email protected]",
"homepage": "https://openducks.org"
}
],
"support": {
Expand All @@ -34,6 +43,8 @@
"autoload": {
"psr-4": {
"Exbil\\Mailcow\\": "src/"
}
},
"files": ["src/MailCowAPI.php", "src/Credentials.php"],
"classmap": ["src"]
}
}
59 changes: 59 additions & 0 deletions src/AddressRewrite/AddressRewrite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
namespace Exbil\Mailcow\AddressRewrite;

use Exbil\MailCowAPI;

class AddressRewrite {
private $MailCowAPI;

public function __construct(MailCowAPI $MailCowAPI){
$this->MailCowAPI = $MailCowAPI;
}

/**
* addBccMap - Forward all mails for a domain to a specified mailbox via bcc
* @param string $destination Mailbox to recieve all emails in bcc
* @param string $local_destination The domain the mails should be forwarded from
* @param string $type e.g. "sender"
* @return array
*/
public function addBccMap(string $destination, string $local_destination, string $type = "sender"){
return $this->MailCowAPI->post('add/bcc', [
"active" => 1,
"bcc_dest" => $destination,
"local_dest" => $local_destination,
"type" => $type
]);
}

/**
* addRecipientMap - Redirect mails from one to another mailbox
* @param string $recipient_new The mailbox to forward the mails to
* @param string $recipient_old The mailbox to forward the mails from
* @return array
*/
public function addRecipientMap(string $recipient_new, string $recipient_old){
return $this->MailCowAPI->post('add/recipient_map', [
"active" => 1,
"recipient_map_new" => $recipient_new,
"recipient_map_old" => $recipient_old
]);
}

public function deleteBccMap(int $id){
return $this->MailCowAPI->post('delete/bcc', [$id]);
}

public function deleteRecipientMap(int $id){
return $this->MailCowAPI->post('delete/recipient_map', [$id]);
}

public function getBccMap(int $id){
return $this->MailCowAPI->get('get/bcc/' . $id);
}

public function getRecipientMap(int $id){
return $this->MailCowAPI->get('get/recipient_map/' . $id);
}

}
2 changes: 1 addition & 1 deletion src/Aliases/Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Exbil\Mailcow\Aliases;

use Exbil\Mailcow\MailCowAPI;
use Exbil\MailCowAPI;

class Aliases
{
Expand Down
2 changes: 1 addition & 1 deletion src/AntiSpam/AntiSpam.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Exbil\Mailcow\AntiSpam;

use Exbil\Mailcow\MailCowAPI;
use Exbil\MailCowAPI;

class AntiSpam
{
Expand Down
65 changes: 65 additions & 0 deletions src/AppPasswords/AppPasswords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
namespace Exbil\Mailcow\AppPasswords;

use Exbil\MailCowAPI;

class AppPasswords {
private $MailCowAPI;

private $protocols = [
"imap_access",
"dav_access",
"smtp_access",
"eas_access",
"pop3_access",
"sieve_access"
];

public function __construct(MailCowAPI $MailCowAPI){
$this->MailCowAPI = $MailCowAPI;
define("PROTOCOL_IMAP", "imap_access");
define("PROTOCOL_DAV", "dav_access");
define("PROTOCOL_SMTP", "smtp_access");
define("PROTOCOL_EAS", "eas_access");
define("PROTOCOL_POP3", "pop3_access");
define("PROTOCOL_SIEVE", "sieve_access");
}

public function deletePassword(string $id){
return $this->MailCowAPI->post('delete/app-passwd', [$id]);
}

public function getPasswordsFromMailbox(string $mail){
return $this->MailCowAPI->get('get/app-passwd/all/' . $mail);
}

/**
* createPassword - Create an App password
* @param string $username Mailbox to create app password for
* @param string $appName App name
* @param string $appPasswd
* @param string $appPasswd2 Same password, just again
* @param array $protocols See class defined constants, e.g. [AppPasswords::PROTOCOL_POP3, AppPasswords::PROTOCOL_SMTP]
* @return array
*/
public function createPassword(string $username, string $appName, string $appPasswd, string $appPasswd2, array $protocols = null){
return $this->MailCowAPI->post('add/app-passwd', [
"username" => $username,
"app_name" => $appName,
"active" => "1",
"app_passwd" => $appPasswd,
"app_passwd2" => $appPasswd2,
"protocols" => $this->computeProtocolsArray($protocols)
]);
}

private function computeProtocolsArray(array $protocols){
$computed = [];
foreach($protocols as $protocol){
if(in_array($protocol, $this->protocols)){
array_push($computed, $protocol);
}
}
return $computed;
}
}
34 changes: 34 additions & 0 deletions src/DKIM/DKIM.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Exbil\Mailcow\DKIM;

use Exbil\MailCowAPI;

class DKIM {
private $MailCowAPI;

public function __construct(MailCowAPI $MailCowAPI){
$this->MailCowAPI = $MailCowAPI;
}

public function deleteKey(string $domain){
return $this->MailCowAPI->post('delete/dkim', [$domain]);
}

public function getKey(string $domain){
return $this->MailCowAPI->get('get/dkim/' . $domain);
}

public function generateKey(string $domains, string $selector = "dkim", int $keySize = 2048){
return $this->MailCowAPI->post('add/dkim', [
"dkim_selector" => $selector,
"domains" => $domains,
"key_size" => $keySize
]);
}
public function duplicateKey(string $fromDomain, string $toDomain){
return $this->MailCowAPI->post('add/dkim_duplicate', [
"from_domain" => $fromDomain,
"to_domain" => $toDomain
]);
}
}
105 changes: 105 additions & 0 deletions src/DomainAdmin/DomainAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
namespace Exbil\Mailcow\DomainAdmin;

use Exbil\MailCowAPI;

class DomainAdmin
{
private $MailCowAPI;

public $permissions = [
"syncjobs",
"quarantine",
"login_as",
"sogo_access",
"app_passwds",
"bcc_maps",
"pushover",
"filters",
"ratelimit",
"spam_policy",
"extend_sender_acl",
"unlimited_quota",
"protocol_access",
"smtp_ip_access",
"alias_domains",
"domain_desc"
];

public function __construct(MailCowAPI $MailCowAPI)
{
$this->MailCowAPI = $MailCowAPI;
}

/**
* addAdmin - Add an domain admin
* @param array $domains Array of domains the user should be domain admin of
* @param string $password
* @param string $password2 Same password, just again
* @param string $username User which gets to be the domain admin
* @return array
*/
public function addAdmin(array $domains, string $password, string $password2, string $username)
{
return $this->MailCowAPI->post('add/domain-admin', [
"active" => 1,
"domains" => $domains,
"password" => $password,
"password2" => $password2,
"username" => $username
]);
}

/**
* editDomainAdminACL - Edit the ACLs for Domain Admins
* @param string $acl - The ACL in question to edit
* @param array $permissions - An array of permissions. See $this->permissions, e.g. ["smtp_ip_access", "domain_desc", "alias_domain", ...]
* @return array
*/
public function editDomainAdminACL(string $acl, array $permissions)
{
return $this->MailCowAPI->post('edit/da-acl', [
"items" => $acl,
"attr" => $this->computePermissions($permissions)
]);
}


private function computePermissions(array $permissions)
{
$computed = [];
foreach($permissions as $permission){
if(in_array($permission, $this->permissions)){
array_push($computed, $permission);
}
}
return $computed;
}

public function deleteDomainAdmin(string $username){
return $this->MailCowAPI->post('delete/domain-admin', [$username]);
}

public function editDomainAdmin(string $username, array $domains, int $active = 1, int $username_new = null, string $password = null, string $password2 = null){
return $this->MailCowAPI->post('edit/domain-admin', [
"items" => [
$username
],
"attr" => [
"active" => [$active]
],
"username_new" => $username_new,
"domains" => [
$domains
],
"password" => $password,
"password2" => $password2
]);
}

public function getAllDomainAdmins(){
return $this->MailCowAPI->get('get/domain-admin/all');
}


}
2 changes: 1 addition & 1 deletion src/Domains/Domains.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Exbil\Mailcow\Domains;

use Exbil\Mailcow\MailCowAPI;
use Exbil\MailCowAPI;

class Domains
{
Expand Down
38 changes: 38 additions & 0 deletions src/Fail2Ban/Fail2Ban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace Exbil\Mailcow\Fail2Ban;

use Exbil\MailCowAPI;

class Fail2Ban {
private $MailCowAPI;

public function __construct(MailCowAPI $MailCowAPI){
$this->MailCowAPI = $MailCowAPI;
}

/**
* editConfig - Edit Fail2Ban config
* @param int $bantimeInMs Time in ms the IP gets banned
* @param string $blacklist Blacklist of IPs, e.g. "10.10.10.0/24, 10.100.8.4/32"
* @param string $whitelist A whitelist, e.g. "mydomain.com, anotherdomain.org"
* @return array
*/
public function editConfig(int $bantimeInMs, string $blacklist, int $max_attempts = 5, int $netban_ipv4 = 24, int $netban_ipv6 = 64, int $retry_window = 600, string $whitelist = null){
return $this->MailCowAPI->post('edit/fail2ban', [
"attr"=> [
"ban_time" => $bantimeInMs,
"blacklist" => $blacklist,
"max_attempts" => $max_attempts,
"netban_ipv4" => $netban_ipv4,
"netban_ipv6" => $netban_ipv6,
"retry_window" => $retry_window,
"whitelist" => $whitelist
],
"items" => "none"
]);
}

public function getConfig(){
return $this->MailCowAPI->get('get/fail2ban');
}
}
27 changes: 27 additions & 0 deletions src/FwdHost/FwdHost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Exbil\Mailcow\FwdHost;

use Exbil\MailCowAPI;

class FwdHost {
private $MailCowAPI;

public function __construct(MailCowAPI $MailCowAPI){
$this->MailCowAPI = $MailCowAPI;
}

public function addHost(string $hostname, bool $filter_spam = false){
return $this->MailCowAPI->post('add/fwdhost', [
"filter_spam" => (int)$filter_spam,
"hostname" => $hostname
]);
}

public function deleteHost(string $hostname){
return $this->MailCowAPI->post('delete/fwdhost', [$hostname]);
}

public function getAllHosts(){
return $this->MailCowAPI->get('get/fwdhost/all');
}
}
Loading

0 comments on commit 5435700

Please sign in to comment.