1: <?php
2:
3: /**
4: * This code is licensed under AGPLv3 license or Afterlogic Software License
5: * if commercial version of the product was purchased.
6: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
7: */
8:
9: namespace Aurora\Modules\MailSignupDirectadmin;
10:
11: /**
12: * Allows users to create new email accounts for themselves on DirectAdmin.
13: *
14: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
15: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
16: * @copyright Copyright (c) 2023, Afterlogic Corp.
17: *
18: * @package Modules
19: */
20: class Module extends \Aurora\System\Module\AbstractModule
21: {
22: /**
23: * @var
24: */
25: private $oDAApi;
26:
27: public function init()
28: {
29: $this->subscribeEvent('MailSignup::Signup::before', [$this, 'onAfterSignup']);
30:
31: require_once __DIR__.'/da_api_sign.php';
32:
33: $sDaURL = $this->getConfig('DirectAdminURL', 'http://localhost:2222');
34: $sDaAdminUser = $this->getConfig('AdminUser', '');
35: $sDaAdminPassword = $this->getConfig('AdminPassword', '');
36: $iPos = strpos($sDaURL, '://');
37: $sDaFullURL = substr($sDaURL, 0, $iPos+3).$sDaAdminUser.':'.$sDaAdminPassword.'@'.substr($sDaURL, $iPos+3);
38: $this->oDAApi = new \DirectAdminAPI($sDaFullURL);
39: }
40:
41: /**
42: * Creates account with credentials specified in registration form
43: *
44: * @param array $aArgs New account credentials.
45: * @param type $mResult Is passed by reference.
46: */
47: public function onAfterSignup($aArgs, &$mResult)
48: {
49: if (isset($aArgs['Login']) && isset($aArgs['Password']) && !empty(trim($aArgs['Password'])) && !empty(trim($aArgs['Login']))) {
50: $sLogin = trim($aArgs['Login']);
51: $sPassword = trim($aArgs['Password']);
52: $sFriendlyName = isset($aArgs['Name']) ? trim($aArgs['Name']) : '';
53: $bSignMe = isset($aArgs['SignMe']) ? (bool) $aArgs['SignMe'] : false;
54: $iQuota = (int) $this->getConfig('UserDefaultQuotaMB', 20);
55:
56: $bPrevState = \Aurora\System\Api::skipCheckUserRole(true);
57: [$sUsername, $sDomain] = explode("@", $sLogin);
58: if (!empty($sDomain)) {
59: $aResult = array();
60: try {
61: $mResultDA = $this->oDAApi->CMD_API_POP("create", $sDomain, $sUsername, $sPassword, $sPassword, $iQuota, '');
62: parse_str(urldecode($mResultDA), $aResult);
63: \Aurora\System\Api::Log('API call result:\n'.$mResultDA, \Aurora\System\Enums\LogLevel::Full);
64: } catch(\Exception $oException) {
65: throw new \Aurora\System\Exceptions\ApiException(0, $oException, $oException->getMessage());
66: }
67: if (is_array($aResult) && isset($aResult['error']) && ($aResult['error']!="1")) {
68: $iUserId = \Aurora\Modules\Core\Module::Decorator()->CreateUser(0, $sLogin);
69: $oUser = \Aurora\System\Api::getUserById((int) $iUserId);
70: try {
71: $oAccount = \Aurora\Modules\Mail\Module::Decorator()->CreateAccount($oUser->Id, $sFriendlyName, $sLogin, $sLogin, $sPassword);
72: if ($oAccount instanceof \Aurora\Modules\Mail\Models\MailAccount) {
73: $iTime = $bSignMe ? 0 : time();
74: $sAuthToken = \Aurora\System\Api::UserSession()->Set(
75: [
76: 'token' => 'auth',
77: 'sign-me' => $bSignMe,
78: 'id' => $oAccount->IdUser,
79: 'account' => $oAccount->Id,
80: 'account_type' => $oAccount->getName()
81: ],
82: $iTime
83: );
84: $mResult = ['AuthToken' => $sAuthToken];
85: }
86: } catch (\Exception $oException) {
87: if ($oException instanceof \Aurora\Modules\Mail\Exceptions\Exception &&
88: $oException->getCode() === \Aurora\Modules\Mail\Enums\ErrorCodes::CannotLoginCredentialsIncorrect) {
89: \Aurora\Modules\Core\Module::Decorator()->DeleteUser($oUser->Id);
90: }
91: throw $oException;
92: }
93: } elseif (is_array($aResult) && isset($aResult['details'])) {
94: $bPrevState = \Aurora\System\Api::skipCheckUserRole(true);
95: \Aurora\System\Api::skipCheckUserRole($bPrevState);
96: throw new \Aurora\System\Exceptions\ApiException(0, null, trim(str_replace("<br>", "", $aResult['details'])));
97: }
98: }
99: \Aurora\System\Api::skipCheckUserRole($bPrevState);
100: }
101: return true; // break subscriptions to prevent account creation in other modules
102: }
103: }
104: