Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix E_NOTICE on undefined $socket property #99

Merged
merged 5 commits into from
Jul 30, 2020
Merged

Fix E_NOTICE on undefined $socket property #99

merged 5 commits into from
Jul 30, 2020

Conversation

cedric-anne
Copy link
Contributor

Q A
Documentation no
Bugfix yes
BC Break no
New Feature no
RFC no
QA no

Description

I am working with current state of develop branch and code is generating some E_NOTICE.

To reproduce :

  • create a protocol instance without initializing the connection,
  • destruct this protocol instance.
    This is not a common use case, so I only experiencing it in an automated test suite.

Declaring the property $socket on Laminas\Mail\Protocol\ProtocolTrait should fix this issue.

Error E_NOTICE in /var/glpi/tests/imap/Toolbox.php on line 105, generated by file /var/glpi/vendor/laminas/laminas-mail/src/Protocol/Imap.php on line 411:
Undefined property: Laminas\Mail\Protocol\Imap::$socket
Error E_NOTICE in /var/glpi/tests/imap/Toolbox.php on line 105, generated by file /var/glpi/vendor/laminas/laminas-mail/src/Protocol/Pop3.php on line 194:
Undefined property: Laminas\Mail\Protocol\Pop3::$socket

@cedric-anne
Copy link
Contributor Author

I am not sure about the best way to handle conflict between $socket property of AbstractProtocol and ProtocolTrait.

Maybe the ProtocolTrait should be used directly in AbstractProtocol, so AbstractProtocol::_connect() could use ProtocolTrait::setupSocket() and $socket proerty declaration could be removed from AbstractProtocol.

But I do not get why AbstractProtocol::_connect() takes a string as argument instead of directly use $this properties ($this->transport, $this->host, $this->port). If behaviour of this method change (i.e. making $remote param optionnal and deprecate its usage), I do not know how you want to handle this.

Last point, I was wondering if following code should be also moved in ProtocolTrait::setupSocket()

        if (($result = stream_set_timeout($this->socket, self::TIMEOUT_CONNECTION)) === false) {
            throw new Exception\RuntimeException('Could not set stream timeout');
        }

}

$this->setupSocket($matches['host'], $matches['port'], self::TIMEOUT_CONNECTION);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part I'm not certain about.

In #94, @trasher specifically did NOT add the ProtocolTrait to the SMTP implementation. As such, we run a risk when changing this, UNLESS those changes also apply to the SMTP connection protocol.

Can somebody check this, please?

Also, I'm not sure why we'd add the extra verifications here, and not in setupSocket().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked, and we should be supporting self-signed certs for SMTP connections made over SSL or TLS as well.

I'm about to push a set of changes back to your branch that addresses this issue more comprehensively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it make sense to support it for SMTP connection.
I may not have time to work on it for next 2 weeks, but I can, at least, take time to review som changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got the changes pushed. :)

cedric-anne and others added 5 commits July 30, 2020 10:35
Signed-off-by: Cédric Anne <cedric.anne@gmail.com>
Signed-off-by: Cédric Anne <cedric.anne@gmail.com>
Signed-off-by: Cédric Anne <cedric.anne@gmail.com>
…ties per-protocol.

This patch does the following:

- Refactors the `ProtocolTrait` to add scalar and return type hints (as
  current develop branch targets PHP 7.1+).
- Updates the `ProtocolTrait::setupSocket()` method as follows:
  - Adds a new initial `string` argument, `$transport`.
  - Returns the socket created.
  - Uses the transport, host, and port to create the `$remote` argument
    for `stream_socket_client()`.
  - Adds a verification that we can set the stream timeout.
- Updates each of the `Imap` and `Pop3` protocol implementations to:
  - Add a `$socket` property.
  - Set the `$socket` property from the return value of
    `$this->setupSocket()`.
  - Marshal the `$transport` argument to send to `setupSocket()`, and no
    longer prepend it to the `$host` argument. By default, the
    `$transport` is `tcp` (per the `stream_socket_client()`
    documentation).
- Adds a `$socket` property to the `AbstractProtocol` class.
- Updates the `Smtp` proptocol implementation as follows:
  - It now composes the `ProtocolTrait`.
  - It checks for the `novalidatecert` configuration option during
    construction, and passes the value to `setNoValidateCert()`.
  - `connect()` now proxies to `setupSocket()`, and assign its inherited
    `$socket` property to the return value.  This ensures that the SMTP
    protocol can also talk to servers with self-signed certificates (e.g.,
    during development or QA).
- Deprecates the `AbstractProtocol::_connect()` method, as it is no
  longer used internally.

Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
@weierophinney weierophinney merged commit 93f1d78 into laminas:develop Jul 30, 2020
@cedric-anne cedric-anne deleted the bugfix/undefined-socket branch July 30, 2020 16:12
artemii-karkusha pushed a commit to artemii-karkusha/laminas-mail that referenced this pull request May 24, 2023
Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants