2018-06-26 11:43:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2019-07-26 08:01:03 +02:00
|
|
|
use Watson\Rememberable\Rememberable;
|
2018-06-26 11:43:11 +02:00
|
|
|
|
|
|
|
class Mailbox extends Model
|
|
|
|
{
|
2019-07-26 08:01:03 +02:00
|
|
|
use Rememberable;
|
2020-01-19 10:09:42 +01:00
|
|
|
// This is obligatory.
|
|
|
|
public $rememberCacheDriver = 'array';
|
2020-03-20 07:50:30 +01:00
|
|
|
|
2018-07-24 08:34:28 +02:00
|
|
|
/**
|
|
|
|
* From Name: name that will appear in the From field when a customer views your email.
|
|
|
|
*/
|
2018-06-26 11:43:11 +02:00
|
|
|
const FROM_NAME_MAILBOX = 1;
|
|
|
|
const FROM_NAME_USER = 2;
|
|
|
|
const FROM_NAME_CUSTOM = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Status: when you reply to a message, this status will be set by default (also applies to email integration).
|
|
|
|
*/
|
|
|
|
const TICKET_STATUS_ACTIVE = 1;
|
|
|
|
const TICKET_STATUS_PENDING = 2;
|
|
|
|
const TICKET_STATUS_CLOSED = 3;
|
|
|
|
|
|
|
|
/**
|
2018-07-24 08:34:28 +02:00
|
|
|
* Default Assignee.
|
2018-06-26 11:43:11 +02:00
|
|
|
*/
|
|
|
|
const TICKET_ASSIGNEE_ANYONE = 1;
|
|
|
|
const TICKET_ASSIGNEE_REPLYING_UNASSIGNED = 2;
|
|
|
|
const TICKET_ASSIGNEE_REPLYING = 3;
|
|
|
|
|
|
|
|
/**
|
2018-07-24 08:34:28 +02:00
|
|
|
* Email Template.
|
2018-06-26 11:43:11 +02:00
|
|
|
*/
|
|
|
|
const TEMPLATE_FANCY = 1;
|
|
|
|
const TEMPLATE_PLAIN = 2;
|
|
|
|
|
|
|
|
/**
|
2018-08-02 18:17:13 +02:00
|
|
|
* Outgoing method. Must be listed in getMailDriverName.
|
2018-06-26 11:43:11 +02:00
|
|
|
*/
|
|
|
|
const OUT_METHOD_PHP_MAIL = 1;
|
|
|
|
const OUT_METHOD_SENDMAIL = 2;
|
|
|
|
const OUT_METHOD_SMTP = 3;
|
2018-07-06 05:19:49 +02:00
|
|
|
//const OUT_METHOD_GMAIL = 3; // todo
|
2018-06-26 11:43:11 +02:00
|
|
|
// todo: mailgun, sendgrid, mandrill, etc
|
2018-07-24 08:34:28 +02:00
|
|
|
|
2018-06-26 11:43:11 +02:00
|
|
|
/**
|
2018-08-03 20:56:06 +02:00
|
|
|
* Outgoing encryption.
|
2018-06-26 11:43:11 +02:00
|
|
|
*/
|
2018-08-03 20:56:06 +02:00
|
|
|
const OUT_ENCRYPTION_NONE = 1;
|
|
|
|
const OUT_ENCRYPTION_SSL = 2;
|
|
|
|
const OUT_ENCRYPTION_TLS = 3;
|
|
|
|
|
|
|
|
public static $out_encryptions = [
|
|
|
|
self::OUT_ENCRYPTION_NONE => '',
|
2018-08-08 09:56:03 +02:00
|
|
|
self::OUT_ENCRYPTION_SSL => 'ssl',
|
|
|
|
self::OUT_ENCRYPTION_TLS => 'tls',
|
2018-08-03 20:56:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Incoming protocol.
|
|
|
|
*/
|
|
|
|
const IN_PROTOCOL_IMAP = 1;
|
|
|
|
const IN_PROTOCOL_POP3 = 2;
|
|
|
|
|
|
|
|
public static $in_protocols = [
|
|
|
|
self::IN_PROTOCOL_IMAP => 'imap',
|
|
|
|
self::IN_PROTOCOL_POP3 => 'pop3',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Incoming encryption.
|
|
|
|
*/
|
|
|
|
const IN_ENCRYPTION_NONE = 1;
|
|
|
|
const IN_ENCRYPTION_SSL = 2;
|
|
|
|
const IN_ENCRYPTION_TLS = 3;
|
|
|
|
|
|
|
|
public static $in_encryptions = [
|
|
|
|
self::IN_ENCRYPTION_NONE => '',
|
2018-08-08 09:56:03 +02:00
|
|
|
self::IN_ENCRYPTION_SSL => 'ssl',
|
|
|
|
self::IN_ENCRYPTION_TLS => 'tls',
|
2018-08-03 20:56:06 +02:00
|
|
|
];
|
2018-06-26 11:43:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ratings Playcement: place ratings text above/below signature.
|
|
|
|
*/
|
|
|
|
const RATINGS_PLACEMENT_ABOVE = 1;
|
|
|
|
const RATINGS_PLACEMENT_BELOW = 2;
|
|
|
|
|
2020-09-25 11:59:45 +02:00
|
|
|
/**
|
|
|
|
* Access permissions.
|
|
|
|
*/
|
|
|
|
const ACCESS_PERM_EDIT = 'edit';
|
|
|
|
const ACCESS_PERM_PERMISSIONS = 'perm';
|
|
|
|
const ACCESS_PERM_AUTO_REPLIES = 'auto';
|
|
|
|
const ACCESS_PERM_SIGNATURE = 'sig';
|
|
|
|
|
|
|
|
public static $access_permissions = [
|
|
|
|
self::ACCESS_PERM_EDIT,
|
|
|
|
self::ACCESS_PERM_PERMISSIONS,
|
|
|
|
self::ACCESS_PERM_AUTO_REPLIES,
|
|
|
|
self::ACCESS_PERM_SIGNATURE,
|
|
|
|
];
|
|
|
|
|
|
|
|
public static $access_routes = [
|
|
|
|
self::ACCESS_PERM_EDIT => 'mailboxes.update',
|
|
|
|
self::ACCESS_PERM_PERMISSIONS => 'mailboxes.permissions',
|
|
|
|
self::ACCESS_PERM_AUTO_REPLIES => 'mailboxes.auto_reply',
|
|
|
|
self::ACCESS_PERM_SIGNATURE => 'mailboxes.update',
|
|
|
|
];
|
|
|
|
|
2018-08-24 14:24:11 +02:00
|
|
|
/**
|
|
|
|
* Default signature set when mailbox created.
|
|
|
|
*/
|
2018-11-08 17:54:29 +01:00
|
|
|
const DEFAULT_SIGNATURE = '<br><span style="color:#808080;">--<br>
|
2019-07-01 12:58:24 +02:00
|
|
|
{%mailbox.name%}</span>';
|
2018-08-24 14:24:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default values.
|
|
|
|
*/
|
|
|
|
protected $attributes = [
|
|
|
|
'signature' => self::DEFAULT_SIGNATURE,
|
|
|
|
];
|
|
|
|
|
2018-06-26 11:43:11 +02:00
|
|
|
/**
|
2018-07-24 08:34:28 +02:00
|
|
|
* Attributes fillable using fill() method.
|
|
|
|
*
|
2018-06-26 11:43:11 +02:00
|
|
|
* @var [type]
|
|
|
|
*/
|
2020-05-28 06:19:44 +02:00
|
|
|
protected $fillable = ['name', 'email', 'aliases', 'auto_bcc', 'from_name', 'from_name_custom', 'ticket_status', 'ticket_assignee', 'template', 'before_reply', 'signature', 'out_method', 'out_server', 'out_username', 'out_password', 'out_port', 'out_encryption', 'in_server', 'in_port', 'in_username', 'in_password', 'in_protocol', 'in_encryption', 'in_validate_cert', 'auto_reply_enabled', 'auto_reply_subject', 'auto_reply_message', 'office_hours_enabled', 'ratings', 'ratings_placement', 'ratings_text', 'imap_sent_folder'];
|
2018-06-26 11:43:11 +02:00
|
|
|
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
2020-05-28 06:19:44 +02:00
|
|
|
// self::created(function (Mailbox $model) {
|
|
|
|
// $model->slug = strtolower(substr(md5(Hash::make($model->id)), 0, 16));
|
|
|
|
// });
|
2018-06-26 11:43:11 +02:00
|
|
|
}
|
2018-06-29 07:07:00 +02:00
|
|
|
|
2018-11-13 15:04:26 +01:00
|
|
|
/**
|
2018-11-13 15:06:16 +01:00
|
|
|
* Automatically encrypt password on save.
|
2018-11-13 15:04:26 +01:00
|
|
|
*/
|
2018-11-13 15:06:56 +01:00
|
|
|
public function setInPasswordAttribute($value)
|
2018-11-13 15:06:16 +01:00
|
|
|
{
|
2020-03-29 05:44:51 +02:00
|
|
|
if ($value != '') {
|
|
|
|
$this->attributes['in_password'] = encrypt($value);
|
|
|
|
} else {
|
|
|
|
$this->attributes['in_password'] = '';
|
|
|
|
}
|
2018-11-13 15:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-13 15:06:16 +01:00
|
|
|
* Automatically decrypt password on read.
|
2018-11-13 15:04:26 +01:00
|
|
|
*/
|
2018-11-13 15:06:56 +01:00
|
|
|
public function getInPasswordAttribute($value)
|
2018-11-13 15:06:16 +01:00
|
|
|
{
|
2018-11-18 05:49:00 +01:00
|
|
|
if (!$value) {
|
|
|
|
return '';
|
|
|
|
}
|
2018-11-24 11:11:51 +01:00
|
|
|
|
2018-11-13 15:04:26 +01:00
|
|
|
try {
|
|
|
|
return decrypt($value);
|
2018-11-17 14:59:13 +01:00
|
|
|
} catch (\Exception $e) {
|
2018-11-13 15:04:26 +01:00
|
|
|
// do nothing if decrypt wasn't succefull
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 07:07:00 +02:00
|
|
|
/**
|
2018-07-24 08:34:28 +02:00
|
|
|
* Get users having access to the mailbox.
|
2018-06-29 07:07:00 +02:00
|
|
|
*/
|
|
|
|
public function users()
|
|
|
|
{
|
2020-02-06 11:52:06 +01:00
|
|
|
return $this->belongsToMany('App\User');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function usersWithSettings()
|
|
|
|
{
|
2020-02-17 06:13:05 +01:00
|
|
|
return $this->belongsToMany('App\User')->as('settings')
|
|
|
|
->withPivot('after_send')
|
|
|
|
->withPivot('hide')
|
2020-09-23 01:51:25 +02:00
|
|
|
->withPivot('mute')
|
|
|
|
->withPivot('access');
|
2018-06-29 07:07:00 +02:00
|
|
|
}
|
2018-07-14 03:23:37 +02:00
|
|
|
|
2019-11-25 11:48:23 +01:00
|
|
|
/**
|
|
|
|
* Get users having access to the mailbox.
|
|
|
|
*/
|
|
|
|
public function users_cached()
|
|
|
|
{
|
|
|
|
return $this->users()->rememberForever();
|
|
|
|
}
|
|
|
|
|
2018-07-14 03:23:37 +02:00
|
|
|
/**
|
2018-07-24 08:34:28 +02:00
|
|
|
* Get mailbox conversations.
|
2018-07-14 03:23:37 +02:00
|
|
|
*/
|
|
|
|
public function conversations()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Conversation');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 08:34:28 +02:00
|
|
|
* Get mailbox folders.
|
2018-07-14 03:23:37 +02:00
|
|
|
*/
|
|
|
|
public function folders()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Folder');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create personal folders for users.
|
2018-07-24 08:34:28 +02:00
|
|
|
*
|
|
|
|
* @param mixed $users
|
2018-07-14 03:23:37 +02:00
|
|
|
*/
|
2018-08-10 16:28:35 +02:00
|
|
|
public function syncPersonalFolders($users = null)
|
2018-07-14 03:23:37 +02:00
|
|
|
{
|
2018-08-10 16:28:35 +02:00
|
|
|
if (!empty($users) && is_array($users)) {
|
2018-07-14 03:23:37 +02:00
|
|
|
$user_ids = $users;
|
|
|
|
} else {
|
2018-08-10 16:28:35 +02:00
|
|
|
$user_ids = $this->users()->pluck('users.id')->toArray();
|
2018-07-14 03:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add admins
|
|
|
|
$admin_user_ids = User::where('role', User::ROLE_ADMIN)->pluck('id')->toArray();
|
|
|
|
$user_ids = array_merge($user_ids, $admin_user_ids);
|
|
|
|
|
2018-08-10 16:28:35 +02:00
|
|
|
self::createUsersFolders($user_ids, $this->id, Folder::$personal_types);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created folders of specific type for passed users.
|
|
|
|
*/
|
|
|
|
public static function createUsersFolders($user_ids, $mailbox_id, $folder_types)
|
|
|
|
{
|
2018-07-14 03:23:37 +02:00
|
|
|
$cur_users = Folder::select('user_id')
|
2018-08-10 16:28:35 +02:00
|
|
|
->where('mailbox_id', $mailbox_id)
|
2018-07-14 03:23:37 +02:00
|
|
|
->whereIn('user_id', $user_ids)
|
|
|
|
->groupBy('user_id')
|
|
|
|
->pluck('user_id')
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
foreach ($user_ids as $user_id) {
|
|
|
|
if (in_array($user_id, $cur_users)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-10 16:28:35 +02:00
|
|
|
foreach ($folder_types as $type) {
|
2018-07-24 08:34:28 +02:00
|
|
|
$folder = new Folder();
|
2018-08-10 16:28:35 +02:00
|
|
|
$folder->mailbox_id = $mailbox_id;
|
2018-07-14 03:23:37 +02:00
|
|
|
$folder->user_id = $user_id;
|
|
|
|
$folder->type = $type;
|
|
|
|
$folder->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:28:35 +02:00
|
|
|
public function createPublicFolders()
|
|
|
|
{
|
|
|
|
foreach (Folder::$public_types as $type) {
|
|
|
|
$folder = new Folder();
|
|
|
|
$folder->mailbox_id = $this->id;
|
|
|
|
$folder->type = $type;
|
|
|
|
$folder->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 03:23:37 +02:00
|
|
|
public function createAdminPersonalFolders()
|
|
|
|
{
|
|
|
|
$user_ids = User::where('role', User::ROLE_ADMIN)->pluck('id')->toArray();
|
2018-08-10 16:28:35 +02:00
|
|
|
self::createUsersFolders($user_ids, $this->id, Folder::$personal_types);
|
|
|
|
}
|
2018-07-14 03:23:37 +02:00
|
|
|
|
2018-08-10 16:28:35 +02:00
|
|
|
public static function createAdminPersonalFoldersAllMailboxes($user_ids = null)
|
|
|
|
{
|
|
|
|
if (empty($user_ids)) {
|
|
|
|
$user_ids = User::where('role', User::ROLE_ADMIN)->pluck('id')->toArray();
|
|
|
|
}
|
2018-08-10 16:34:10 +02:00
|
|
|
$mailbox_ids = self::pluck('id');
|
2018-08-10 16:28:35 +02:00
|
|
|
foreach ($mailbox_ids as $mailbox_id) {
|
|
|
|
self::createUsersFolders($user_ids, $mailbox_id, Folder::$personal_types);
|
2018-07-14 03:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get folders for the dashboard.
|
|
|
|
*/
|
|
|
|
public function getMainFolders()
|
|
|
|
{
|
|
|
|
return $this->folders()
|
|
|
|
->where(function ($query) {
|
|
|
|
$query->whereIn('type', [Folder::TYPE_UNASSIGNED, Folder::TYPE_ASSIGNED, Folder::TYPE_DRAFTS])
|
|
|
|
->orWhere(function ($query2) {
|
|
|
|
$query2->where(['type' => Folder::TYPE_MINE]);
|
|
|
|
$query2->where(['user_id' => auth()->user()->id]);
|
2018-12-08 10:00:20 +01:00
|
|
|
})
|
|
|
|
->orWhere(function ($query3) {
|
|
|
|
$query3->where(['type' => Folder::TYPE_STARRED]);
|
|
|
|
$query3->where(['user_id' => auth()->user()->id]);
|
2018-07-14 03:23:37 +02:00
|
|
|
});
|
|
|
|
})
|
2018-07-14 14:17:11 +02:00
|
|
|
->orderBy('type')
|
2018-07-14 03:23:37 +02:00
|
|
|
->get();
|
|
|
|
}
|
2018-07-14 14:17:11 +02:00
|
|
|
|
2019-06-03 07:02:58 +02:00
|
|
|
/**
|
|
|
|
* Get folder by it's type.
|
|
|
|
*/
|
|
|
|
public function getFolderByType($type)
|
|
|
|
{
|
|
|
|
return $this->folders()
|
|
|
|
->where('type', $type)
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
|
2018-07-14 14:17:11 +02:00
|
|
|
/**
|
|
|
|
* Get folders available for the current user.
|
|
|
|
*/
|
|
|
|
public function getAssesibleFolders()
|
|
|
|
{
|
2019-12-20 10:41:06 +01:00
|
|
|
$folders = $this->folders()
|
2018-07-14 14:17:11 +02:00
|
|
|
->where(function ($query) {
|
2019-12-20 08:06:34 +01:00
|
|
|
$query->whereIn('type', \Eventy::filter('mailbox.folders.public_types', Folder::$public_types))
|
2018-07-14 14:17:11 +02:00
|
|
|
->orWhere(function ($query2) {
|
|
|
|
$query2->whereIn('type', Folder::$personal_types);
|
|
|
|
$query2->where(['user_id' => auth()->user()->id]);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
->orderBy('type')
|
|
|
|
->get();
|
2019-12-20 10:41:06 +01:00
|
|
|
|
|
|
|
return \Eventy::filter('mailbox.folders', $folders);
|
2018-07-14 14:17:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update total and active counters for folders.
|
|
|
|
*/
|
2018-09-14 16:52:16 +02:00
|
|
|
public function updateFoldersCounters($folder_type = null)
|
2018-07-14 14:17:11 +02:00
|
|
|
{
|
2018-09-14 16:52:16 +02:00
|
|
|
if (!$folder_type) {
|
|
|
|
$folders = $this->folders;
|
|
|
|
} else {
|
|
|
|
$folders = $this->folders()->where('folders.type', $folder_type)->get();
|
|
|
|
}
|
2018-09-19 10:00:59 +02:00
|
|
|
|
2018-07-14 14:17:11 +02:00
|
|
|
foreach ($folders as $folder) {
|
2018-09-14 16:52:16 +02:00
|
|
|
$folder->updateCounters();
|
2018-07-14 14:17:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-16 09:03:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is mailbox available for using.
|
2018-07-24 08:34:28 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2018-07-16 09:03:24 +02:00
|
|
|
*/
|
|
|
|
public function isActive()
|
|
|
|
{
|
2018-07-24 08:34:28 +02:00
|
|
|
return $this->isInActive() && $this->isOutActive();
|
2018-07-16 09:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is receiving emails configured for the mailbox.
|
2018-07-24 08:34:28 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2018-07-16 09:03:24 +02:00
|
|
|
*/
|
|
|
|
public function isInActive()
|
|
|
|
{
|
2018-08-03 20:56:06 +02:00
|
|
|
if ($this->in_protocol && $this->in_server && $this->in_port && $this->in_username && $this->in_password) {
|
2018-07-16 09:03:24 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is sending emails configured for the mailbox.
|
2018-07-24 08:34:28 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2018-07-16 09:03:24 +02:00
|
|
|
*/
|
|
|
|
public function isOutActive()
|
|
|
|
{
|
|
|
|
if ($this->out_method != self::OUT_METHOD_PHP_MAIL && $this->out_method != self::OUT_METHOD_SENDMAIL
|
2019-07-08 11:51:02 +02:00
|
|
|
&& (!$this->out_server /*|| !$this->out_username || !$this->out_password*/)
|
2018-07-16 09:03:24 +02:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 08:01:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get users who have access to the mailbox.
|
|
|
|
*/
|
2019-12-26 13:38:04 +01:00
|
|
|
public function usersHavingAccess($cache = false, $fields = 'users.*', $sort = true)
|
2018-07-27 08:01:34 +02:00
|
|
|
{
|
2019-12-26 13:38:04 +01:00
|
|
|
$admins = User::where('role', User::ROLE_ADMIN)->select($fields)->remember(\Helper::cacheTime($cache))->get();
|
2018-07-27 08:03:12 +02:00
|
|
|
|
2020-02-06 11:52:06 +01:00
|
|
|
$users = $this->users()->select($fields)->remember(\Helper::cacheTime($cache))->get()->merge($admins)->unique();
|
2018-12-12 07:43:58 +01:00
|
|
|
|
|
|
|
// Exclude deleted users (better to do it in PHP).
|
|
|
|
foreach ($users as $i => $user) {
|
2020-02-22 06:53:51 +01:00
|
|
|
if (!$user->isActive()) {
|
2018-12-12 07:43:58 +01:00
|
|
|
$users->forget($i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 13:38:04 +01:00
|
|
|
// Sort by full name
|
|
|
|
if ($sort) {
|
2020-02-06 11:52:06 +01:00
|
|
|
$users = User::sortUsers($users);
|
2019-12-26 13:38:04 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 07:43:58 +01:00
|
|
|
return $users;
|
2018-07-27 08:01:34 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 11:52:06 +01:00
|
|
|
public function usersAssignable($cache = true)
|
|
|
|
{
|
|
|
|
// Exclude hidden admins.
|
|
|
|
$mailbox_id = $this->id;
|
|
|
|
$admins = User::select(['users.*', 'mailbox_user.hide'])
|
|
|
|
->leftJoin('mailbox_user', function ($join) use ($mailbox_id) {
|
|
|
|
$join->on('mailbox_user.user_id', '=', 'users.id');
|
|
|
|
$join->where('mailbox_user.mailbox_id', $mailbox_id);
|
|
|
|
})
|
|
|
|
->where('role', User::ROLE_ADMIN)
|
|
|
|
->remember(\Helper::cacheTime($cache))
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$users = $this->users()->select('users.*')->remember(\Helper::cacheTime($cache))->get()->merge($admins)->unique();
|
|
|
|
|
|
|
|
foreach ($users as $i => $user) {
|
|
|
|
if (!empty($user->hide)) {
|
|
|
|
$users->forget($i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exclude deleted users (better to do it in PHP).
|
|
|
|
foreach ($users as $i => $user) {
|
2020-02-22 06:53:51 +01:00
|
|
|
if (!$user->isActive()) {
|
2020-02-06 11:52:06 +01:00
|
|
|
$users->forget($i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by full name
|
|
|
|
$users = User::sortUsers($users);
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2018-07-27 08:01:34 +02:00
|
|
|
/**
|
|
|
|
* Get users IDs who have access to the mailbox.
|
|
|
|
*/
|
|
|
|
public function userIdsHavingAccess()
|
|
|
|
{
|
2018-12-12 07:43:58 +01:00
|
|
|
return $this->usersHavingAccess(false, ['users.id', 'users.status'])->pluck('id')->toArray();
|
|
|
|
|
|
|
|
/*$user_ids = $this->users()->pluck('users.id');
|
2018-07-27 08:01:34 +02:00
|
|
|
$admin_ids = User::where('role', User::ROLE_ADMIN)->pluck('id');
|
2018-07-27 08:03:12 +02:00
|
|
|
|
2018-12-12 07:43:58 +01:00
|
|
|
return $user_ids->merge($admin_ids)->unique()->toArray();*/
|
2018-07-27 08:01:34 +02:00
|
|
|
}
|
2018-07-30 17:11:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if user has access to the mailbox.
|
2018-07-30 17:13:33 +02:00
|
|
|
*
|
2018-07-30 17:11:35 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-04-26 06:27:26 +02:00
|
|
|
public function userHasAccess($user_id, $user = null)
|
2018-07-30 17:11:35 +02:00
|
|
|
{
|
2019-04-26 06:27:26 +02:00
|
|
|
if (!$user) {
|
|
|
|
$user = User::find($user_id);
|
|
|
|
}
|
2018-07-30 17:11:35 +02:00
|
|
|
if ($user && $user->isAdmin()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2018-07-30 17:13:33 +02:00
|
|
|
return (bool) $this->users()->where('users.id', $user_id)->count();
|
2018-07-30 17:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-02 18:17:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get From array for the Mail function.
|
2018-08-02 18:18:32 +02:00
|
|
|
*
|
|
|
|
* @param App\User $from_user
|
|
|
|
*
|
2018-08-02 18:17:13 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMailFrom($from_user = null)
|
|
|
|
{
|
2018-10-24 12:50:05 +02:00
|
|
|
// Mailbox name by default
|
2018-08-02 18:17:13 +02:00
|
|
|
$name = $this->name;
|
|
|
|
|
|
|
|
if ($this->from_name == self::FROM_NAME_CUSTOM && $this->from_name_custom) {
|
|
|
|
$name = $this->from_name_custom;
|
2018-10-24 12:50:05 +02:00
|
|
|
} elseif ($this->from_name == self::FROM_NAME_USER && $from_user) {
|
2018-08-02 18:17:13 +02:00
|
|
|
$name = $from_user->getFullName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['address' => $this->email, 'name' => $name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get corresponding Laravel mail driver name.
|
|
|
|
*/
|
|
|
|
public function getMailDriverName()
|
|
|
|
{
|
|
|
|
switch ($this->out_method) {
|
|
|
|
case self::OUT_METHOD_PHP_MAIL:
|
|
|
|
return 'mail';
|
|
|
|
|
|
|
|
case self::OUT_METHOD_SENDMAIL:
|
|
|
|
return 'sendmail';
|
|
|
|
|
|
|
|
case self::OUT_METHOD_SMTP:
|
|
|
|
return 'smtp';
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 'mail';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get domain part of the mailbox email.
|
2018-08-02 18:18:32 +02:00
|
|
|
*
|
2018-08-02 18:17:13 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getEmailDomain()
|
|
|
|
{
|
|
|
|
return explode('@', $this->email)[1];
|
|
|
|
}
|
2018-08-03 20:56:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get outgoing email encryption protocol.
|
2018-08-08 09:56:03 +02:00
|
|
|
*
|
2018-08-03 20:56:06 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getOutEncryptionName()
|
|
|
|
{
|
|
|
|
return self::$out_encryptions[$this->out_encryption];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get incoming email encryption protocol.
|
2018-08-08 09:56:03 +02:00
|
|
|
*
|
2018-08-03 20:56:06 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getInEncryptionName()
|
|
|
|
{
|
|
|
|
return self::$in_encryptions[$this->in_encryption];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get incoming protocol name.
|
2018-08-08 09:56:03 +02:00
|
|
|
*
|
2018-08-03 20:56:06 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getInProtocolName()
|
|
|
|
{
|
2020-05-28 05:57:24 +02:00
|
|
|
return self::$in_protocols[$this->in_protocol] ?? '';
|
2018-08-03 20:56:06 +02:00
|
|
|
}
|
2018-08-10 16:28:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get pivot table parameters for the user.
|
|
|
|
*/
|
|
|
|
public function getUserSettings($user_id)
|
|
|
|
{
|
2020-02-06 11:52:06 +01:00
|
|
|
$mailbox_user = $this->usersWithSettings()->where('users.id', $user_id)->first();
|
2018-08-10 16:28:35 +02:00
|
|
|
if ($mailbox_user) {
|
|
|
|
return $mailbox_user->settings;
|
|
|
|
} else {
|
|
|
|
// Admin may have no record in mailbox_user table
|
2020-09-25 11:59:45 +02:00
|
|
|
return self::getDummySettings();
|
2018-08-10 16:28:35 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 09:01:26 +02:00
|
|
|
|
2020-09-25 11:59:45 +02:00
|
|
|
/**
|
|
|
|
* Create dummy object with default parameters
|
|
|
|
* @return [type] [description]
|
|
|
|
*/
|
|
|
|
public static function getDummySettings()
|
|
|
|
{
|
|
|
|
$settings = new \StdClass();
|
|
|
|
$settings->after_send = MailboxUser::AFTER_SEND_NEXT;
|
|
|
|
$settings->hide = false;
|
|
|
|
$settings->mute = false;
|
|
|
|
$settings->access = [];
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
|
2020-02-17 06:13:05 +01:00
|
|
|
public function fetchUserSettings($user_id)
|
|
|
|
{
|
|
|
|
$settings = $this->getUserSettings($user_id);
|
|
|
|
|
|
|
|
$this->after_send = $settings->after_send;
|
|
|
|
$this->hide = $settings->hide;
|
|
|
|
$this->mute = $settings->mute;
|
2020-09-23 01:51:25 +02:00
|
|
|
$this->access = $settings->access;
|
2020-02-17 06:13:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-15 09:01:26 +02:00
|
|
|
/**
|
|
|
|
* Get main email and aliases.
|
2018-11-12 10:12:57 +01:00
|
|
|
*
|
2018-08-15 09:01:26 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getEmails()
|
|
|
|
{
|
|
|
|
$emails = [$this->email];
|
|
|
|
|
|
|
|
if ($this->aliases) {
|
|
|
|
$aliases = explode(',', $this->aliases);
|
|
|
|
foreach ($aliases as $alias) {
|
|
|
|
$alias = Email::sanitizeEmail($alias);
|
|
|
|
if ($alias) {
|
|
|
|
$emails[] = $alias;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-12 10:12:57 +01:00
|
|
|
|
2018-08-15 09:01:26 +02:00
|
|
|
return $emails;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove mailbox email and aliases from the list of emails.
|
|
|
|
*
|
2018-11-12 10:12:57 +01:00
|
|
|
* @param array $list
|
2018-08-15 09:01:26 +02:00
|
|
|
* @param Mailbox $mailbox
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function removeMailboxEmailsFromList($list)
|
|
|
|
{
|
|
|
|
if (!is_array($list)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$mailbox_emails = $this->getEmails();
|
|
|
|
foreach ($list as $i => $email) {
|
|
|
|
if (in_array($email, $mailbox_emails)) {
|
|
|
|
unset($list[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
2018-12-03 10:03:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all active mailboxes.
|
2018-12-05 08:06:54 +01:00
|
|
|
*
|
2018-12-03 10:03:23 +01:00
|
|
|
* @return [type] [description]
|
|
|
|
*/
|
|
|
|
public static function getActiveMailboxes()
|
|
|
|
{
|
|
|
|
$active = [];
|
|
|
|
|
|
|
|
// It is more effective to retrive all mailboxes and filter them in PHP.
|
2018-12-05 08:06:54 +01:00
|
|
|
$mailboxes = self::all();
|
2018-12-03 10:03:23 +01:00
|
|
|
foreach ($mailboxes as $mailbox) {
|
|
|
|
if ($mailbox->isActive()) {
|
|
|
|
$active[] = $mailbox;
|
|
|
|
}
|
|
|
|
}
|
2018-12-05 08:06:54 +01:00
|
|
|
|
2018-12-03 10:03:23 +01:00
|
|
|
return $active;
|
|
|
|
}
|
2018-12-15 15:00:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get mailbox URL.
|
2018-12-21 10:10:07 +01:00
|
|
|
*
|
2018-12-15 15:00:48 +01:00
|
|
|
* @return [type] [description]
|
|
|
|
*/
|
|
|
|
public function url()
|
|
|
|
{
|
|
|
|
return route('mailboxes.view', ['id' => $this->id]);
|
|
|
|
}
|
2018-12-21 07:15:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill the model with an array of attributes.
|
2018-12-21 10:10:07 +01:00
|
|
|
*
|
|
|
|
* @param array $attributes [description]
|
|
|
|
*
|
|
|
|
* @return [type] [description]
|
2018-12-21 07:15:41 +01:00
|
|
|
*/
|
|
|
|
public function fill(array $attributes)
|
|
|
|
{
|
|
|
|
$this->fillable(array_merge($this->getFillable(), \Eventy::filter('mailbox.fillable_fields', [])));
|
|
|
|
|
|
|
|
return parent::fill($attributes);
|
|
|
|
}
|
2019-07-05 12:18:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set phones as JSON.
|
|
|
|
*
|
|
|
|
* @param array $phones_array
|
|
|
|
*/
|
|
|
|
public function setInImapFolders(array $in_imap_folders)
|
|
|
|
{
|
|
|
|
$this->in_imap_folders = json_encode($in_imap_folders);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of imap folders.
|
|
|
|
*/
|
|
|
|
public function getInImapFolders()
|
|
|
|
{
|
2020-03-29 06:33:24 +02:00
|
|
|
$in_imap_folders = \Helper::jsonToArray($this->in_imap_folders);
|
|
|
|
if (count($in_imap_folders)) {
|
|
|
|
return $in_imap_folders;
|
|
|
|
} else {
|
|
|
|
return ["INBOX"];
|
|
|
|
}
|
2019-07-05 12:18:15 +02:00
|
|
|
}
|
2019-09-06 10:12:54 +02:00
|
|
|
|
|
|
|
public function outPasswordSafe()
|
|
|
|
{
|
|
|
|
return \Helper::safePassword($this->out_password);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function inPasswordSafe()
|
|
|
|
{
|
|
|
|
return \Helper::safePassword($this->in_password);
|
|
|
|
}
|
2020-02-17 06:13:05 +01:00
|
|
|
|
|
|
|
public static function findOrFailWithSettings($id, $user_id)
|
|
|
|
{
|
2020-09-23 01:51:25 +02:00
|
|
|
return Mailbox::select(['mailboxes.*', 'mailbox_user.hide', 'mailbox_user.mute', 'mailbox_user.access'])
|
2020-02-17 06:13:05 +01:00
|
|
|
->where('mailboxes.id', $id)
|
|
|
|
->leftJoin('mailbox_user', function ($join) use ($user_id) {
|
|
|
|
$join->on('mailbox_user.mailbox_id', '=', 'mailboxes.id');
|
|
|
|
$join->where('mailbox_user.user_id', $user_id);
|
|
|
|
})->firstOrFail();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*public static function getUserSettings($mailbox_id, $user_id)
|
|
|
|
{
|
|
|
|
return MailboxUser::where('mailbox_id', $mailbox_id)
|
|
|
|
->where('user_id', $user_id)
|
|
|
|
->first();
|
|
|
|
}*/
|
2020-09-25 11:59:45 +02:00
|
|
|
|
|
|
|
public static function getAccessPermissionName($perm)
|
|
|
|
{
|
|
|
|
$access_permissions = [
|
|
|
|
self::ACCESS_PERM_EDIT => __('Edit Mailbox'),
|
|
|
|
self::ACCESS_PERM_PERMISSIONS => __('Permissions'),
|
|
|
|
self::ACCESS_PERM_AUTO_REPLIES => __('Auto Replies'),
|
|
|
|
self::ACCESS_PERM_SIGNATURE => __('Email Signature'),
|
|
|
|
];
|
|
|
|
$access_permissions = \Eventy::filter('mailbox.access_permissions_list', $access_permissions);
|
|
|
|
|
|
|
|
return $access_permissions[$perm] ?? '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getAccessPermissionRoute($perm)
|
|
|
|
{
|
|
|
|
$route = self::$access_routes[$perm] ?? '';
|
|
|
|
$route = \Eventy::filter('mailbox.access_permissions_route', $route, $perm);
|
|
|
|
|
|
|
|
return $route;
|
|
|
|
}
|
2018-06-26 11:43:11 +02:00
|
|
|
}
|