Skip to content

Commit 56f3032

Browse files
committed
fix: crrect getHostname() fallback logic in Email class
1 parent 327c7e1 commit 56f3032

File tree

5 files changed

+62
-16
lines changed

5 files changed

+62
-16
lines changed

system/Email/Email.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,12 +2137,16 @@ protected function getSMTPData()
21372137
*/
21382138
protected function getHostname()
21392139
{
2140-
if (isset($_SERVER['SERVER_NAME'])) {
2141-
return $_SERVER['SERVER_NAME'];
2140+
$superglobals = service('superglobals');
2141+
2142+
$serverName = $superglobals->server('SERVER_NAME');
2143+
if (! in_array($serverName, [null, ''], true)) {
2144+
return $serverName;
21422145
}
21432146

2144-
if (isset($_SERVER['SERVER_ADDR'])) {
2145-
return '[' . $_SERVER['SERVER_ADDR'] . ']';
2147+
$serverAddr = $superglobals->server('SERVER_ADDR');
2148+
if (! in_array($serverAddr, [null, ''], true)) {
2149+
return '[' . $serverAddr . ']';
21462150
}
21472151

21482152
$hostname = gethostname();

tests/system/Email/EmailTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616
use CodeIgniter\Events\Events;
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use CodeIgniter\Test\Mock\MockEmail;
19+
use CodeIgniter\Test\ReflectionHelper;
1920
use ErrorException;
2021
use PHPUnit\Framework\Attributes\DataProvider;
2122
use PHPUnit\Framework\Attributes\Group;
23+
use ReflectionException;
2224

2325
/**
2426
* @internal
2527
*/
2628
#[Group('Others')]
2729
final class EmailTest extends CIUnitTestCase
2830
{
31+
use ReflectionHelper;
32+
2933
public function testEmailValidation(): void
3034
{
3135
$config = config('Email');
@@ -215,4 +219,51 @@ public function testSetAttachmentCIDBufferString(): void
215219
$email->archive['body'],
216220
);
217221
}
222+
223+
/**
224+
* @throws ReflectionException
225+
*/
226+
public function testGetHostnameUsesServerName(): void
227+
{
228+
$email = $this->createMockEmail();
229+
230+
$superglobals = service('superglobals');
231+
$superglobals->setServer('SERVER_NAME', 'example.test');
232+
233+
$getHostname = self::getPrivateMethodInvoker($email, 'getHostname');
234+
235+
$this->assertSame('example.test', $getHostname());
236+
}
237+
238+
/**
239+
* @throws ReflectionException
240+
*/
241+
public function testGetHostnameUsesServerAddr(): void
242+
{
243+
$email = $this->createMockEmail();
244+
245+
$superglobals = service('superglobals');
246+
$superglobals->setServer('SERVER_NAME', '');
247+
$superglobals->setServer('SERVER_ADDR', '192.168.1.10');
248+
249+
$getHostname = self::getPrivateMethodInvoker($email, 'getHostname');
250+
251+
$this->assertSame('[192.168.1.10]', $getHostname());
252+
}
253+
254+
/**
255+
* @throws ReflectionException
256+
*/
257+
public function testGetHostnameFallsBackToGethostnameFunction(): void
258+
{
259+
$email = $this->createMockEmail();
260+
261+
$superglobals = service('superglobals');
262+
$superglobals->setServer('SERVER_NAME', '');
263+
$superglobals->setServer('SERVER_ADDR', '');
264+
265+
$getHostname = self::getPrivateMethodInvoker($email, 'getHostname');
266+
267+
$this->assertSame(gethostname(), $getHostname());
268+
}
218269
}

user_guide_src/source/changelogs/v4.6.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Bugs Fixed
3636
**********
3737

3838
- **Database:** Fixed a bug where ``when()`` and ``whenNot()`` in ``ConditionalTrait`` incorrectly evaluated certain falsy values (such as ``[]``, ``0``, ``0.0``, and ``'0'``) as truthy, causing callbacks to be executed unexpectedly. These methods now cast the condition to a boolean using ``(bool)`` to ensure consistent behavior with PHP's native truthiness.
39+
- **Email:** Fixed a bug where ``Email::getHostname()`` failed to use ``$_SERVER['SERVER_ADDR']`` when ``$_SERVER['SERVER_NAME']`` was not set.
3940
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
4041
- **Session:** Fixed a bug where using the ``DatabaseHandler`` with an unsupported database driver (such as ``SQLSRV``, ``OCI8``, or ``SQLite3``) did not throw an appropriate error.
4142

utils/phpstan-baseline/codeigniter.superglobalAccess.neon

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 83 errors
1+
# total 79 errors
22

33
parameters:
44
ignoreErrors:
@@ -67,16 +67,6 @@ parameters:
6767
count: 1
6868
path: ../../system/Config/Services.php
6969

70-
-
71-
message: '#^Accessing offset ''SERVER_ADDR'' directly on \$_SERVER is discouraged\.$#'
72-
count: 2
73-
path: ../../system/Email/Email.php
74-
75-
-
76-
message: '#^Accessing offset ''SERVER_NAME'' directly on \$_SERVER is discouraged\.$#'
77-
count: 2
78-
path: ../../system/Email/Email.php
79-
8070
-
8171
message: '#^Accessing offset ''HTTP_USER_AGENT'' directly on \$_SERVER is discouraged\.$#'
8272
count: 2

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 3234 errors
1+
# total 3230 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

0 commit comments

Comments
 (0)