| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Models; |
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | +use FluentSupport\App\Modules\PermissionManager; | |
| 6 | 7 | use FluentSupport\App\Services\EmailNotification\Settings; |
| 7 | 8 | use FluentSupport\App\Services\Helper; |
| 8 | 9 | use FluentSupport\Framework\Support\Arr; |
| 9 | 10 | |
| @@ -56,8 +57,36 @@ | ||
| 56 | 57 | |
| 57 | 58 | return []; |
| 58 | 59 | } |
| 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 | + | |
| 60 | 89 | public static function slugify($title) |
| 61 | 90 | { |
| 62 | 91 | $slug = sanitize_title($title, 'business', 'display'); |
| 63 | 92 | if (MailBox::where('slug', $slug)->first()) { |
| @@ -65,14 +94,25 @@ | ||
| 65 | 94 | } |
| 66 | 95 | return $slug; |
| 67 | 96 | } |
| 68 | 97 | |
| 69 | - 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) | |
| 70 | 109 | { |
| 71 | 110 | $headers = []; |
| 72 | - $fromString = $this->email; //$this->name; | |
| 73 | - if($this->name) { | |
| 74 | - $fromString = $this->name.' <'.$fromString.'>'; | |
| 111 | + $fromString = $this->email; | |
| 112 | + $fromName = $this->getFromName($agent); | |
| 113 | + if ($fromName) { | |
| 114 | + $fromString = $fromName . ' <' . $fromString . '>'; | |
| 75 | 115 | } |
| 76 | 116 | |
| 77 | 117 | if ($fromString) { |
| 78 | 118 | $headers[] = 'From: '. $fromString; |
| @@ -77,15 +117,59 @@ | ||
| 77 | 117 | if ($fromString) { |
| 78 | 118 | $headers[] = 'From: '. $fromString; |
| 79 | 119 | } |
| 80 | 120 | |
| 81 | - // Set Reply-To Header | |
| 82 | 121 | $headers[] = 'Reply-To: '. $fromString; |
| 83 | - $headers[] = 'In-Reply-To: '. $fromString; | |
| 84 | 122 | |
| 85 | 123 | return $headers; |
| 86 | 124 | } |
| 87 | 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 | + | |
| 88 | 172 | public function getMeta($key, $default = '') |
| 89 | 173 | { |
| 90 | 174 | $class = __NAMESPACE__ . '\MailBox'; |
| 91 | 175 | |
| @@ -143,9 +227,8 @@ | ||
| 143 | 227 | ->where('object_id', $this->id) |
| 144 | 228 | ->delete(); |
| 145 | 229 | } |
| 146 | 230 | /** |
| 147 | - * This `getMailBox` method is used to get a mailbox by id. | |
| 148 | 231 | * @param int $id |
| 149 | 232 | * @return MailBox |
| 150 | 233 | */ |
| 151 | 234 | public function getMailBox ($id ) |
| @@ -153,9 +236,8 @@ | ||
| 153 | 236 | return MailBox::findOrFail($id); |
| 154 | 237 | } |
| 155 | 238 | |
| 156 | 239 | /** |
| 157 | - * This `createMailBox` method is used to create a new MailBox. | |
| 158 | 240 | * @param array $data |
| 159 | 241 | * @return MailBox |
| 160 | 242 | */ |
| 161 | 243 | public function createMailBox ( $data ) |
| @@ -178,9 +260,8 @@ | ||
| 178 | 260 | } |
| 179 | 261 | |
| 180 | 262 | |
| 181 | 263 | /** |
| 182 | - * This `updateMailBox` method is used to update a mailbox. | |
| 183 | 264 | * @param array $data |
| 184 | 265 | * @param int $mailBoxId |
| 185 | 266 | * @return object $mailbox |
| 186 | 267 | */ |