| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentSupport\App\Modules\PermissionManager; |
| 7 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 8 |
use FluentSupport\App\Services\Helper; |
| 9 |
use FluentSupport\Framework\Support\Arr; |
| 10 |
|
| 11 |
class MailBox extends Model |
| 12 |
{ |
| 13 |
protected $table = 'fs_mail_boxes'; |
| 14 |
|
| 15 |
protected $appends = ['settings']; |
| 16 |
|
| 17 |
/** |
| 18 |
* The attributes that are mass assignable. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
protected $fillable = [ |
| 23 |
'name', |
| 24 |
'slug', |
| 25 |
'email', |
| 26 |
'box_type', |
| 27 |
'mapped_email', |
| 28 |
'email_footer', |
| 29 |
'settings', |
| 30 |
'avatar', |
| 31 |
'created_by', |
| 32 |
'is_default' |
| 33 |
]; |
| 34 |
|
| 35 |
public static function boot() |
| 36 |
{ |
| 37 |
parent::boot(); |
| 38 |
|
| 39 |
static::creating(function ($model) { |
| 40 |
$model->slug = static::slugify($model->name); |
| 41 |
$model->settings = $model->settings ?: [ |
| 42 |
'admin_email_address' => '' |
| 43 |
]; |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
public function setSettingsAttribute($settings) |
| 48 |
{ |
| 49 |
$this->attributes['settings'] = \maybe_serialize($settings); |
| 50 |
} |
| 51 |
|
| 52 |
public function getSettingsAttribute($value) |
| 53 |
{ |
| 54 |
if (array_key_exists('settings', $this->attributes)) { |
| 55 |
return Helper::safeUnserialize($this->attributes['settings']); |
| 56 |
} |
| 57 |
|
| 58 |
return []; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Mailboxes the current agent isn't restricted from. The restriction list is |
| 63 |
* the only gate, so an unrestricted agent gets `settings` (incl. |
| 64 |
* admin_email_address) for every box. |
| 65 |
* |
| 66 |
* @param bool $withEmail Include the inbox address. Only pass true once the caller |
| 67 |
* has confirmed the current user holds `fst_sensitive_data`. |
| 68 |
* @return \FluentSupport\Framework\Database\Orm\Collection |
| 69 |
*/ |
| 70 |
public static function getAccessibleBoxes($withEmail = false) |
| 71 |
{ |
| 72 |
$columns = ['id', 'name', 'box_type', 'settings']; |
| 73 |
|
| 74 |
if ($withEmail) { |
| 75 |
$columns[] = 'email'; |
| 76 |
} |
| 77 |
|
| 78 |
$query = MailBox::select($columns); |
| 79 |
|
| 80 |
$restrictedBoxes = PermissionManager::getRestrictedMailboxIds(); |
| 81 |
|
| 82 |
if ($restrictedBoxes) { |
| 83 |
$query->whereNotIn('id', $restrictedBoxes); |
| 84 |
} |
| 85 |
|
| 86 |
return $query->get(); |
| 87 |
} |
| 88 |
|
| 89 |
public static function slugify($title) |
| 90 |
{ |
| 91 |
$slug = sanitize_title($title, 'business', 'display'); |
| 92 |
if (MailBox::where('slug', $slug)->first()) { |
| 93 |
$slug .= '-' . time(); |
| 94 |
} |
| 95 |
return $slug; |
| 96 |
} |
| 97 |
|
| 98 |
/** From-name choices for email an agent writes (settings.from_name_type). */ |
| 99 |
const FROM_NAME_MAILBOX = 'mailbox'; |
| 100 |
const FROM_NAME_AGENT = 'agent'; |
| 101 |
const FROM_NAME_CUSTOM = 'custom'; |
| 102 |
|
| 103 |
/** |
| 104 |
* @param \FluentSupport\App\Models\Agent|null $agent The agent who wrote the email. |
| 105 |
* Only agent-written email passes one; automatic email keeps the inbox name. |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
public function getMailerHeader($agent = null) |
| 109 |
{ |
| 110 |
$headers = []; |
| 111 |
$fromString = $this->email; |
| 112 |
$fromName = $this->getFromName($agent); |
| 113 |
if ($fromName) { |
| 114 |
$fromString = $fromName . ' <' . $fromString . '>'; |
| 115 |
} |
| 116 |
|
| 117 |
if ($fromString) { |
| 118 |
$headers[] = 'From: '. $fromString; |
| 119 |
} |
| 120 |
|
| 121 |
$headers[] = 'Reply-To: '. $fromString; |
| 122 |
|
| 123 |
return $headers; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Display name for the From header. The address is always the inbox email, so |
| 128 |
* replies thread back; only the name follows the inbox's From name setting. |
| 129 |
* |
| 130 |
* @param \FluentSupport\App\Models\Agent|null $agent |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
public function getFromName($agent = null) |
| 134 |
{ |
| 135 |
$settings = is_array($this->settings) ? $this->settings : []; |
| 136 |
$type = isset($settings['from_name_type']) ? $settings['from_name_type'] : self::FROM_NAME_MAILBOX; |
| 137 |
$name = ''; |
| 138 |
|
| 139 |
if ($agent && $type === self::FROM_NAME_AGENT) { |
| 140 |
$name = $agent->full_name; |
| 141 |
} elseif ($agent && $type === self::FROM_NAME_CUSTOM && !empty($settings['from_name_template'])) { |
| 142 |
$name = apply_filters('fluent_support/parse_smartcode_data', $settings['from_name_template'], [ |
| 143 |
'agent' => $agent, |
| 144 |
'business' => $this, |
| 145 |
]); |
| 146 |
} |
| 147 |
|
| 148 |
$name = self::cleanFromName($name) ?: self::cleanFromName($this->name); |
| 149 |
|
| 150 |
/* |
| 151 |
* Filter the From name of an email sent from this inbox. |
| 152 |
* |
| 153 |
* @since v2.4.1 |
| 154 |
* @param string $name |
| 155 |
* @param MailBox $mailbox |
| 156 |
* @param \FluentSupport\App\Models\Agent|null $agent Set for email an agent wrote. |
| 157 |
*/ |
| 158 |
return (string) apply_filters('fluent_support/mailbox_from_name', $name, $this, $agent); |
| 159 |
} |
| 160 |
|
| 161 |
// A header value: no line breaks, quotes or angle brackets, no unparsed smartcodes. |
| 162 |
private static function cleanFromName($name) |
| 163 |
{ |
| 164 |
$name = preg_replace('/{{[^}]*}}/', '', (string) $name); |
| 165 |
$name = wp_strip_all_tags($name, true); |
| 166 |
$name = preg_replace('/[\x00-\x1F\x7F"<>]+/u', ' ', $name); |
| 167 |
$name = trim(preg_replace('/\s+/u', ' ', $name)); |
| 168 |
|
| 169 |
return mb_substr($name, 0, 120); |
| 170 |
} |
| 171 |
|
| 172 |
public function getMeta($key, $default = '') |
| 173 |
{ |
| 174 |
$class = __NAMESPACE__ . '\MailBox'; |
| 175 |
|
| 176 |
$meta = Meta::where('object_type', $class) |
| 177 |
->where('object_id', $this->id) |
| 178 |
->where('key', $key) |
| 179 |
->first(); |
| 180 |
|
| 181 |
if($meta) { |
| 182 |
return Helper::safeUnserialize($meta->value); |
| 183 |
} |
| 184 |
|
| 185 |
return $default; |
| 186 |
} |
| 187 |
|
| 188 |
public function saveMeta($key, $value) |
| 189 |
{ |
| 190 |
$class = __NAMESPACE__ . '\MailBox'; |
| 191 |
|
| 192 |
$meta = Meta::where('object_type', $class) |
| 193 |
->where('object_id', $this->id) |
| 194 |
->where('key', $key) |
| 195 |
->first(); |
| 196 |
|
| 197 |
if($meta) { |
| 198 |
$meta->value = maybe_serialize($value); |
| 199 |
$meta->save(); |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
Meta::insert([ |
| 204 |
'object_type' => $class, |
| 205 |
'object_id' => $this->id, |
| 206 |
'key' => $key, |
| 207 |
'value' => maybe_serialize($value) |
| 208 |
]); |
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
public function deleteMeta($key) |
| 213 |
{ |
| 214 |
$class = __NAMESPACE__ . '\MailBox'; |
| 215 |
|
| 216 |
$meta = Meta::where('object_type', $class) |
| 217 |
->where('object_id', $this->id) |
| 218 |
->where('key', $key) |
| 219 |
->delete(); |
| 220 |
} |
| 221 |
|
| 222 |
public function deleteAllMeta() |
| 223 |
{ |
| 224 |
$class = __NAMESPACE__ . '\MailBox'; |
| 225 |
|
| 226 |
$meta = Meta::where('object_type', $class) |
| 227 |
->where('object_id', $this->id) |
| 228 |
->delete(); |
| 229 |
} |
| 230 |
/** |
| 231 |
* @param int $id |
| 232 |
* @return MailBox |
| 233 |
*/ |
| 234 |
public function getMailBox ($id ) |
| 235 |
{ |
| 236 |
return MailBox::findOrFail($id); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* @param array $data |
| 241 |
* @return MailBox |
| 242 |
*/ |
| 243 |
public function createMailBox ( $data ) |
| 244 |
{ |
| 245 |
if ($data['box_type'] == 'email') { |
| 246 |
$data['settings'] = [ |
| 247 |
'admin_email_address' => '' |
| 248 |
]; |
| 249 |
} else { |
| 250 |
$data['settings'] = [ |
| 251 |
'admin_email_address' => $data['email'] |
| 252 |
]; |
| 253 |
} |
| 254 |
|
| 255 |
if (!MailBox::first()) { |
| 256 |
$data['is_default'] = 'yes'; |
| 257 |
} |
| 258 |
|
| 259 |
return MailBox::create($data); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* @param array $data |
| 265 |
* @param int $mailBoxId |
| 266 |
* @return object $mailbox |
| 267 |
*/ |
| 268 |
public function updateMailBox ($data, $mailBoxId ) |
| 269 |
{ |
| 270 |
|
| 271 |
$mailbox = MailBox::findOrFail($mailBoxId); |
| 272 |
|
| 273 |
if ($data['box_type'] == 'email' && empty($data['mapped_email'])) { |
| 274 |
throw new \Exception('Mapped Email Address is required'); |
| 275 |
} |
| 276 |
|
| 277 |
$mailbox->fill($data); |
| 278 |
$mailbox->save(); |
| 279 |
|
| 280 |
return $mailbox; |
| 281 |
} |
| 282 |
} |
| 283 |
|