1: <?php
2:
3: namespace Aurora\Modules\Mail\Models;
4:
5: use Aurora\Modules\Contacts\Models\Group;
6: use Aurora\System\Classes\Model;
7: use Aurora\Modules\Core\Models\User;
8: use Aurora\Modules\Mail\Module;
9:
10: class MailAccount extends Model
11: {
12: /**
13: * The attributes that are mass assignable.
14: *
15: * @var array
16: */
17: protected $foreignModel = User::class;
18: protected $foreignModelIdColumn = 'IdUser'; // Column that refers to an external table
19: protected $fillable = [
20: 'Id',
21: 'IsDisabled',
22: 'IdUser',
23: 'UseToAuthorize',
24: 'Email',
25: 'FriendlyName',
26: 'IncomingLogin',
27: 'IncomingPassword',
28: 'UseSignature',
29: 'Signature',
30: 'ServerId',
31: 'FoldersOrder',
32: 'UseThreading',
33: 'SaveRepliesToCurrFolder',
34: 'IncludeInUnifiedMailbox',
35: 'ShowUnifiedMailboxLabel',
36: 'UnifiedMailboxLabelText',
37: 'UnifiedMailboxLabelColor',
38: 'XOAuth'
39: ];
40:
41: /**
42: * The attributes that should be hidden for arrays.
43: *
44: * @var array
45: */
46: protected $hidden = [
47: ];
48:
49: protected $casts = [
50: 'Properties' => 'array',
51: 'IncomingPassword' => \Aurora\System\Casts\Encrypt::class,
52: 'IsDisabled' => 'boolean',
53: 'UseToAuthorize' => 'boolean',
54: 'IncludeInUnifiedMailbox' => 'boolean',
55: 'UseSignature' => 'boolean',
56: 'ShowUnifiedMailboxLabel' => 'boolean',
57: 'UseThreading' => 'boolean',
58: 'SaveRepliesToCurrFolder' => 'boolean',
59: 'FoldersOrder' => 'string'
60: ];
61:
62: protected $attributes = [
63: ];
64:
65: protected $appends = [
66: 'EntityId'
67: ];
68:
69: // public function getIncomingPasswordAttribute()
70: // {
71: // $sPassword = '';
72: // if (!$this->attributes['IncomingPassword']) // TODO: Legacy support
73: // {
74: // $sSalt = \Aurora\System\Api::$sSalt;
75: // \Aurora\System\Api::$sSalt = md5($sSalt);
76: // $sPassword = $this->attributes['IncomingPassword'];
77: // \Aurora\System\Api::$sSalt = $sSalt;
78: // }
79: // else
80: // {
81: // $sPassword = $this->attributes['IncomingPassword'];
82: // }
83:
84: // $sPassword = \Aurora\System\Utils::DecryptValue($sPassword);
85:
86: // if ($sPassword !== '' && strpos($sPassword, $this->IncomingLogin . ':') === false)
87: // {
88: // $this->IncomingPassword = $sPassword;
89: // \Aurora\System\Api::GetModule('Mail')->getAccountsManager()->updateAccount($this);
90: // }
91: // else
92: // {
93: // $sPassword = substr($sPassword, strlen($this->IncomingLogin) + 1);
94: // }
95:
96: // return $sPassword;
97: // }
98:
99: // public function setIncomingPasswordAttribute($sPassword)
100: // {
101: // $this->attributes['IncomingPassword'] = \Aurora\System\Utils::EncryptValue($this->IncomingLogin . ':' . $sPassword);
102: // }
103:
104: public function setPassword($sPassword)
105: {
106: $this->IncomingPassword = $sPassword;
107: }
108:
109: public function getPassword()
110: {
111: return $this->IncomingPassword;
112: }
113:
114: private function canBeUsedToAuthorize()
115: {
116: $oMailModule = \Aurora\System\Api::GetModule('Mail');
117: if ($oMailModule instanceof Module) {
118: return !$oMailModule->getAccountsManager()->useToAuthorizeAccountExists($this->Email, $this->Id);
119: } else {
120: return false;
121: }
122: }
123:
124: public function getDefaultTimeOffset()
125: {
126: return 0;
127: }
128:
129: public function toResponseArray()
130: {
131: $aResponse = parent::toResponseArray();
132: $aResponse['AccountID'] = $this->Id;
133: $aResponse['AllowFilters'] = false;
134: $aResponse['AllowForward'] = false;
135: $aResponse['AllowAutoresponder'] = false;
136: $aResponse['EnableAllowBlockLists'] = false;
137:
138: if (!isset($aResponse['Signature'])) {
139: $aResponse['Signature'] = '';
140: }
141:
142: $oServer = $this->getServer();
143: if ($oServer instanceof \Aurora\System\Classes\Model) {
144: $aResponse['Server'] = $oServer->toResponseArray();
145:
146: $oMailModule = \Aurora\System\Api::GetModule('Mail');
147: if ($oServer->EnableSieve && $oMailModule) {
148: $aResponse['AllowFilters'] = $oMailModule->getConfig('AllowFilters', '');
149: $aResponse['AllowForward'] = $oMailModule->getConfig('AllowForward', '');
150: $aResponse['AllowAutoresponder'] = $oMailModule->getConfig('AllowAutoresponder', '');
151: $aResponse['EnableAllowBlockLists'] = $oMailModule->getConfig('EnableAllowBlockLists', false);
152: }
153: }
154:
155: $aResponse['CanBeUsedToAuthorize'] = $this->canBeUsedToAuthorize();
156: // unset($aResponse['IncomingPassword']);
157:
158: $aArgs = ['Account' => $this];
159: \Aurora\System\Api::GetModule('Core')->broadcastEvent(
160: 'Mail::Account::ToResponseArray',
161: $aArgs,
162: $aResponse
163: );
164:
165: return $aResponse;
166: }
167:
168: public function getServer()
169: {
170: return $this->Server;
171: }
172:
173: public function getLogin()
174: {
175: $oServer = $this->getServer();
176: if ($oServer && !$oServer->UseFullEmailAddressAsLogin) {
177: return $this->Email;
178: }
179: return $this->IncomingLogin;
180: }
181:
182: public function Server()
183: {
184: return $this->belongsTo(Server::class, 'ServerId', 'Id');
185: }
186: }
187: