Skip to content

Commit 694a89b

Browse files
Merge branch 'master' of github.com:freescout-helpdesk/freescout into dist
2 parents ab2aa14 + b1d3595 commit 694a89b

File tree

13 files changed

+92
-25
lines changed

13 files changed

+92
-25
lines changed

app/Console/Commands/FetchEmails.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,8 +1232,9 @@ public function saveUserThread($mailbox, $message_id, $prev_thread, $user, $from
12321232

12331233
// Respect mailbox settings for "Status After Replying
12341234
$prev_status = $conversation->status;
1235-
$conversation->status = ($mailbox->ticket_status == Mailbox::TICKET_STATUS_KEEP_CURRENT ? $conversation->status : $mailbox->ticket_status);
1236-
if ($conversation->status != $mailbox->ticket_status) {
1235+
$new_status = ($mailbox->ticket_status == Mailbox::TICKET_STATUS_KEEP_CURRENT ? $conversation->status : $mailbox->ticket_status);
1236+
if ($new_status != $prev_status) {
1237+
$conversation->setStatus($new_status, $user, $update_folder = false);
12371238
\Eventy::action('conversation.status_changed', $conversation, $user, true, $prev_status);
12381239
}
12391240
$conversation->last_reply_at = $now;

app/Conversation.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,17 @@ public function getReplies()
390390
*
391391
* @return Collection
392392
*/
393-
public function getThreads($skip = null, $take = null, $types = [])
393+
public function getThreads($skip = null, $take = null, $types = [], $states = [Thread::STATE_PUBLISHED])
394394
{
395395
$query = $this->threads()
396-
->where('state', Thread::STATE_PUBLISHED)
397396
->orderBy('created_at', 'desc');
398397

398+
if (count($states) == 1 && !empty($states[0])) {
399+
$query->where('state', $states[0]);
400+
} else {
401+
$query->whereIn('state', $states);
402+
}
403+
399404
if (!is_null($skip)) {
400405
$query->skip($skip);
401406
}
@@ -622,12 +627,14 @@ public function getStatus()
622627
*
623628
* @param int $status
624629
*/
625-
public function setStatus($status, $user = null)
630+
public function setStatus($status, $user = null, $update_folder = true)
626631
{
627632
$now = date('Y-m-d H:i:s');
628633

629634
$this->status = $status;
630-
$this->updateFolder();
635+
if ($update_folder) {
636+
$this->updateFolder();
637+
}
631638
$this->user_updated_at = $now;
632639

633640
if ($user && $status == self::STATUS_CLOSED) {

app/Http/Controllers/MailboxesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,10 +967,10 @@ public function oauth(Request $request)
967967
return __('Mailbox not found').': '.$mailbox_id;
968968
}
969969
if ($in_out == 'in') {
970-
$username = $mailbox->in_username;
970+
$username = $mailbox->getInOauthClientId();
971971
$password = $mailbox->in_password;
972972
} else {
973-
$username = $mailbox->out_username;
973+
$username = $mailbox->getOutOauthClientId();
974974
$password = $mailbox->out_password;
975975
}
976976
if (empty($username)) {

app/Http/Controllers/UsersController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public function permissions($id)
304304
abort(404);
305305
}
306306

307-
$mailboxes = Mailbox::all();
307+
$mailboxes = Mailbox::all()->sortBy('name');
308308

309309
$users = $this->getUsersForSidebar($id);
310310

app/Mailbox.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,16 +980,50 @@ public function oauthGetParam($param)
980980
public function inOauthEnabled()
981981
{
982982
return $this->oauthEnabled()
983-
&& $this->in_username !== null && !strstr($this->in_username, '@');
983+
&& $this->in_username !== null
984+
&& $this->isInUsernameOauth();
984985
}
985986

986987
public function outOauthEnabled()
987988
{
988989
return $this->oauthEnabled()
989-
&& $this->out_username !== null && !strstr($this->out_username, '@')
990+
&& $this->out_username !== null
991+
&& $this->isOutUsernameOauth()
990992
&& $this->out_server !== null && trim($this->out_server) == \MailHelper::OAUTH_MICROSOFT_SMTP;
991993
}
992994

995+
// For oAuth Username may have the following format:
996+
// [email protected]:123-456-789
997+
public function getInOauthUsername()
998+
{
999+
return preg_replace("#:.*#", '', $this->in_username ?? '');
1000+
}
1001+
1002+
public function getInOauthClientId()
1003+
{
1004+
return preg_replace("#.*:#", '', $this->in_username ?? '');
1005+
}
1006+
1007+
public function getOutOauthUsername()
1008+
{
1009+
return preg_replace("#:.*#", '', $this->out_username ?? '');
1010+
}
1011+
1012+
public function getOutOauthClientId()
1013+
{
1014+
return preg_replace("#.*:#", '', $this->out_username ?? '');
1015+
}
1016+
1017+
public function isInUsernameOauth()
1018+
{
1019+
return (!strstr($this->in_username, '@') || preg_match("#.*@.*:.*#", $this->in_username));
1020+
}
1021+
1022+
public function isOutUsernameOauth()
1023+
{
1024+
return (!strstr($this->out_username, '@') || preg_match("#.*@.*:.*#", $this->out_username));
1025+
}
1026+
9931027
public function setEmailAttribute($value)
9941028
{
9951029
if ($value) {

app/Misc/Helper.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ public static function getRemoteFileContents($url, $follow_redirects = true)
18021802
}
18031803
}
18041804

1805-
public static function sanitizeRemoteUrl($url)
1805+
public static function sanitizeRemoteUrl($url, $throw_exception = false)
18061806
{
18071807
$parts = parse_url($url ?? '');
18081808

@@ -1815,6 +1815,11 @@ public static function sanitizeRemoteUrl($url)
18151815
if (empty($parts['host'])) {
18161816
return '';
18171817
}
1818+
1819+
$host_white_list_str = str_replace(' ', '', mb_strtolower(config('app.remote_host_white_list')));
1820+
$host_white_list = explode(',', $host_white_list_str);
1821+
1822+
// Sanitize host name.
18181823
$parts['host'] = mb_strtolower($parts['host']);
18191824
$hostname = gethostname();
18201825
$host_ip = gethostbyname($hostname);
@@ -1830,13 +1835,24 @@ public static function sanitizeRemoteUrl($url)
18301835
$_SERVER['LOCAL_ADDR'] ?? ''
18311836
];
18321837

1833-
if (in_array($parts['host'], $restricted_hosts)) {
1834-
return '';
1838+
if (in_array($parts['host'], $restricted_hosts) && !in_array($parts['host'], $host_white_list)) {
1839+
if ($throw_exception) {
1840+
throw new \Exception(__('Domain or IP address is not allowed: :%host%. Whitelist it via APP_REMOTE_HOST_WHITE_LIST .env parameter.', ['%host%' => $parts['host']]), 1);
1841+
} else {
1842+
return '';
1843+
}
18351844
}
18361845

1846+
// Sanitize host IP address.
18371847
$remote_host_ip = gethostbyname($parts['host']);
1838-
if (in_array($remote_host_ip, ['0.0.0.0', '127.0.0.1', $host_ip, $_SERVER['SERVER_ADDR'] ?? '', $_SERVER['LOCAL_ADDR'] ?? ''])) {
1839-
return '';
1848+
if (in_array($remote_host_ip, ['0.0.0.0', '127.0.0.1', $host_ip, $_SERVER['SERVER_ADDR'] ?? '', $_SERVER['LOCAL_ADDR'] ?? ''])
1849+
&& !in_array($remote_host_ip, $host_white_list)
1850+
) {
1851+
if ($throw_exception) {
1852+
throw new \Exception(__('Domain or IP address is not allowed: :%host%. Whitelist it via APP_REMOTE_HOST_WHITE_LIST .env parameter.', ['%host%' => $remote_host_ip]), 1);
1853+
} else {
1854+
return '';
1855+
}
18401856
}
18411857

18421858
return $url;

app/Misc/Mail.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public static function setMailDriver($mailbox = null, $user_from = null, $conver
151151
if ((strtotime($mailbox->oauthGetParam('issued_on')) + (int)$mailbox->oauthGetParam('expires_in')) < time()) {
152152
// Try to get an access token (using the authorization code grant)
153153
$token_data = \MailHelper::oauthGetAccessToken(\MailHelper::OAUTH_PROVIDER_MICROSOFT, [
154-
'client_id' => $mailbox->out_username,
154+
'client_id' => $mailbox->getOutOauthClientId(),
155155
'client_secret' => $mailbox->out_password,
156156
'refresh_token' => $mailbox->oauthGetParam('r_token'),
157157
]);
@@ -179,7 +179,7 @@ public static function setMailDriver($mailbox = null, $user_from = null, $conver
179179
\Config::set('mail.port', $mailbox->out_port);
180180
if ($oauth) {
181181
\Config::set('mail.auth_mode', 'XOAUTH2');
182-
\Config::set('mail.username', $mailbox->email);
182+
\Config::set('mail.username', $mailbox->getOutOauthUsername());
183183
\Config::set('mail.password', $mailbox->oauthGetParam('a_token'));
184184
} else {
185185
\Config::set('mail.auth_mode', '');
@@ -790,7 +790,7 @@ public static function getMailboxClient($mailbox)
790790
'port' => $mailbox->in_port,
791791
'encryption' => $mailbox->getInEncryptionName(),
792792
'validate_cert' => $mailbox->in_validate_cert,
793-
'username' => $mailbox->email,
793+
'username' => $mailbox->getInOauthUsername(),
794794
'password' => $mailbox->oauthGetParam('a_token'),
795795
'protocol' => $mailbox->getInProtocolName(),
796796
'authentication' => 'oauth',

config/app.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
| or any other location as required by the application or its packages.
1919
*/
2020

21-
'version' => '1.8.197',
21+
'version' => '1.8.198',
2222

2323
/*
2424
|--------------------------------------------------------------------------
@@ -521,6 +521,15 @@
521521
*/
522522
'alternative_reply_separation' => env('APP_ALTERNATIVE_REPLY_SEPARATION', false),
523523

524+
/*
525+
|--------------------------------------------------------------------------
526+
| Comma separated list of white listed hosts.
527+
| If some input containing URL becomes blank after saving it - add its host or IP here.
528+
| Example: example.org,test.example.org,192.168.1.97
529+
|-------------------------------------------------------------------------
530+
*/
531+
'remote_host_white_list' => env('APP_REMOTE_HOST_WHITE_LIST', ''),
532+
524533
/*
525534
|--------------------------------------------------------------------------
526535
| Autoloaded Service Providers

overrides/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ protected function throwException(Swift_TransportException $e)
449449
} else {
450450
list($last_command) = explode(' ', $last_command);
451451
}
452-
$e = new $exception_type('Swift_Transport_AbstractSmtpTransport::'.$caller_function.'(); '.($last_command ? 'Last Command: '.$last_command.'; ' : '').$e->getMessage());
452+
$e = new $exception_type($caller_function.'(); '.($last_command ? 'Last Command: '.$last_command.'; ' : '').$e->getMessage());
453453
}
454454

455455
if ($evt = $this->eventDispatcher->createTransportExceptionEvent($this, $e)) {

resources/views/mailboxes/connection.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
<p class="form-help">
106106
<small @if ($mailbox->oauthGetParam('provider') == \MailHelper::OAUTH_PROVIDER_MICROSOFT && $out_oauth_enabled) class="text-success" @endif>Microsoft Exchange</small>
107107
@if (!$mailbox->oauthEnabled())
108-
@if ($mailbox->out_username && $mailbox->out_password && !strstr($mailbox->out_username, '@'))
108+
@if ($mailbox->out_username && $mailbox->out_password && $mailbox->isOutUsernameOauth())
109109
– <a href="{{ route('mailboxes.oauth', ['id' => $mailbox->id, 'provider' => \MailHelper::OAUTH_PROVIDER_MICROSOFT, 'in_out' => 'out']) }}" target="_blank">{{ __('Connect') }}</a>
110110
@endif
111111
@elseif ($mailbox->oauthGetParam('provider') == \MailHelper::OAUTH_PROVIDER_MICROSOFT && $out_oauth_enabled)

0 commit comments

Comments
 (0)