PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
← All changes | app/Models/MailBox.php +153 -7 1.4.1 → 2.4.5 View file →
@@ -1,8 +1,12 @@
1 1 <?php
2 2
3 3 namespace FluentSupport\App\Models;
4 4
5 +use Exception;
6 +use FluentSupport\App\Modules\PermissionManager;
7 +use FluentSupport\App\Services\EmailNotification\Settings;
8 +use FluentSupport\App\Services\Helper;
5 9 use FluentSupport\Framework\Support\Arr;
6 10
7 11 class MailBox extends Model
8 12 {
@@ -29,8 +33,10 @@
29 33 ];
30 34
31 35 public static function boot()
32 36 {
37 + parent::boot();
38 +
33 39 static::creating(function ($model) {
34 40 $model->slug = static::slugify($model->name);
35 41 $model->settings = $model->settings ?: [
36 42 'admin_email_address' => ''
@@ -44,11 +50,43 @@
44 50 }
45 51
46 52 public function getSettingsAttribute($value)
47 53 {
48 - return \maybe_unserialize($this->attributes['settings']);
54 + if (array_key_exists('settings', $this->attributes)) {
55 + return Helper::safeUnserialize($this->attributes['settings']);
56 + }
57 +
58 + return [];
49 59 }
50 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 +
51 89 public static function slugify($title)
52 90 {
53 91 $slug = sanitize_title($title, 'business', 'display');
54 92 if (MailBox::where('slug', $slug)->first()) {
@@ -56,14 +94,25 @@
56 94 }
57 95 return $slug;
58 96 }
59 97
60 - public function getMailerHeader()
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)
61 109 {
62 110 $headers = [];
63 - $fromString = $this->email; //$this->name;
64 - if($this->name) {
65 - $fromString = $this->name.' <'.$fromString.'>';
111 + $fromString = $this->email;
112 + $fromName = $this->getFromName($agent);
113 + if ($fromName) {
114 + $fromString = $fromName . ' <' . $fromString . '>';
66 115 }
67 116
68 117 if ($fromString) {
69 118 $headers[] = 'From: '. $fromString;
@@ -68,14 +117,59 @@
68 117 if ($fromString) {
69 118 $headers[] = 'From: '. $fromString;
70 119 }
71 120
72 - // Set Reply-To Header
73 121 $headers[] = 'Reply-To: '. $fromString;
74 122
75 123 return $headers;
76 124 }
77 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 +
78 172 public function getMeta($key, $default = '')
79 173 {
80 174 $class = __NAMESPACE__ . '\MailBox';
81 175
@@ -84,9 +178,9 @@
84 178 ->where('key', $key)
85 179 ->first();
86 180
87 181 if($meta) {
88 - return maybe_unserialize($meta->value);
182 + return Helper::safeUnserialize($meta->value);
89 183 }
90 184
91 185 return $default;
92 186 }
@@ -131,6 +225,58 @@
131 225
132 226 $meta = Meta::where('object_type', $class)
133 227 ->where('object_id', $this->id)
134 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;
135 281 }
136 282 }