diff --git a/CHANGELOG.md b/CHANGELOG.md index 8094647ca..f523db0af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - Fix locked SQLite database for the CLI tools (#8035) - Fix importing contacts with no email address (#8227) - Fix so session's search scope is not used if search is not active (#8199) +- Fix some PHP8 warnings (#8239) ## Release 1.5.0 diff --git a/program/include/rcmail_attachment_handler.php b/program/include/rcmail_attachment_handler.php index b37f1fe64..2ee02e07f 100644 --- a/program/include/rcmail_attachment_handler.php +++ b/program/include/rcmail_attachment_handler.php @@ -229,7 +229,7 @@ class rcmail_attachment_handler } } else { - $data = $attachment['data']; + $data = $attachment['data'] ?? ''; if (!$data && $attachment['path']) { $data = file_get_contents($attachment['path']); } diff --git a/program/lib/Roundcube/rcube_addressbook.php b/program/lib/Roundcube/rcube_addressbook.php index 571e815f5..463d46f8b 100644 --- a/program/lib/Roundcube/rcube_addressbook.php +++ b/program/lib/Roundcube/rcube_addressbook.php @@ -685,20 +685,34 @@ abstract class rcube_addressbook static $compose_mode; if (!isset($compose_mode)) { - $compose_mode = rcube::get_instance()->config->get('addressbook_name_listing', 0); + $compose_mode = (int) rcube::get_instance()->config->get('addressbook_name_listing', 0); } + $get_names = function ($contact, $fields) { + $result = []; + foreach ($fields as $field) { + if (!empty($contact[$field])) { + $result[] = $contact[$field]; + } + } + return $result; + }; + switch ($compose_mode) { case 3: - $fn = implode(' ', [$contact['surname'] . ',', $contact['firstname'], $contact['middlename']]); + $names = $get_names($contact, ['firstname', 'middlename']); + if (!empty($contact['surname'])) { + array_unshift($names, $contact['surname'] . ','); + } + $fn = implode(' ', $names); break; case 2: $keys = ['surname', 'firstname', 'middlename']; - $fn = implode(' ', array_filter(array_intersect_key($contact, array_flip($keys)))); + $fn = implode(' ', $get_names($contact, $keys)); break; case 1: $keys = ['firstname', 'middlename', 'surname']; - $fn = implode(' ', array_filter(array_intersect_key($contact, array_flip($keys)))); + $fn = implode(' ', $get_names($contact, $keys)); break; case 0: if (!empty($contact['name'])) { @@ -706,7 +720,7 @@ abstract class rcube_addressbook } else { $keys = ['prefix', 'firstname', 'middlename', 'surname', 'suffix']; - $fn = implode(' ', array_filter(array_intersect_key($contact, array_flip($keys)))); + $fn = implode(' ', $get_names($contact, $keys)); } break; default: diff --git a/program/lib/Roundcube/rcube_mime.php b/program/lib/Roundcube/rcube_mime.php index d56da5651..ca5bfa66d 100644 --- a/program/lib/Roundcube/rcube_mime.php +++ b/program/lib/Roundcube/rcube_mime.php @@ -494,7 +494,9 @@ class rcube_mime // remove quote chars $line = substr($line, $q); // remove (optional) space-staffing - if ($line[0] === ' ') $line = substr($line, 1); + if (isset($line[0]) && $line[0] === ' ') { + $line = substr($line, 1); + } // The same paragraph (We join current line with the previous one) when: // - the same level of quoting diff --git a/tests/Framework/Addressbook.php b/tests/Framework/Addressbook.php index d0a44f989..73116c3e5 100644 --- a/tests/Framework/Addressbook.php +++ b/tests/Framework/Addressbook.php @@ -27,4 +27,66 @@ class Framework_Addressbook extends PHPUnit\Framework\TestCase $this->assertSame(['home' => ['test@test.com']], $result); } + + /** + * Test for compose_list_name() method + */ + function test_compose_list_name() + { + $contact = []; + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('', $result); + + $contact = ['email' => 'email@address.tld']; + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('email@address.tld', $result); + + $contact = ['email' => 'email@address.tld', 'organization' => 'Org']; + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('Org', $result); + + $contact['firstname'] = 'First'; + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('First', $result); + + $contact['surname'] = 'Last'; + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('First Last', $result); + + $contact['name'] = 'Name'; + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('Name', $result); + + unset($contact['name']); + $contact['prefix'] = 'Dr.'; + $contact['suffix'] = 'Jr.'; + $contact['middlename'] = 'M.'; + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('Dr. First M. Last Jr.', $result); + + // TODO: Test different modes + /* + rcube::get_instance()->config->set('addressbook_name_listing', 3); + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('Last, First M.', $result); + + rcube::get_instance()->config->set('addressbook_name_listing', 2); + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('Last First M.', $result); + + rcube::get_instance()->config->set('addressbook_name_listing', 1); + $result = rcube_addressbook::compose_list_name($contact); + + $this->assertSame('First M. Last', $result); + */ + } }