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

Uncaught TypeError: stream_set_timeout(): Argument #1 ($stream) must be of type resource, bool given #4679

Closed
wants to merge 105 commits into from

Conversation

glwrba
Copy link

@glwrba glwrba commented Apr 4, 2023

When trying to establish the SMTP connection, the code assumes that the socket being obtained is a valid one, and performs a call to set the stream_set_timeout. That's correct for a successful connection, however if a connection cannot be established the call to fsockopen returns a boolean FALSE which leads to an exception when trying to call the @stream_set_timeout function since it expects a handle rather than a boolean, leading to the below stacktrace and exception:

Fatal error: Uncaught TypeError: stream_set_timeout(): Argument #1 ($stream) must be of type resource, bool given in /opt/apache-2.4/sites/microforum.openservers.org/inc/mailhandlers/smtp.php:281 Stack trace: #0 /opt/apache-2.4/sites/microforum.openservers.org/inc/mailhandlers/smtp.php(281): stream_set_timeout(false, 5, 0) #1 /opt/apache-2.4/sites/microforum.openservers.org/inc/mailhandlers/smtp.php(199): SmtpMail->connect() #2 /opt/apache-2.4/sites/microforum.openservers.org/inc/functions.php(677): SmtpMail->send() #3 /opt/apache-2.4/sites/microforum.openservers.org/member.php(621): my_mail('glwrba@yahoo.co...', 'New registratio...', 'admin,\n\nThere i...') #4 {main} thrown in /opt/apache-2.4/sites/microforum.openservers.org/inc/mailhandlers/smtp.php on line 281

The snippet of code that fails is as below

`(Line 281) of inc/mailhandlers/smtp.php

function connect()
{
global $lang, $mybb;

            $this->connection = @fsockopen($this->host, $this->port, $error_number, $error_string, $this->timeout);
    
            // DIRECTORY_SEPARATOR checks if running windows
            if(function_exists('stream_set_timeout') && DIRECTORY_SEPARATOR != '\\')
            {
                    @stream_set_timeout($this->connection, $this->timeout, 0);
            }

`

The solution is to check the handle being returned, and in case of getting a FALSE boolean, we should immediately log the condition and return from the function with a FALSE flagging that something has failed while trying to send the mail via SMTP.

The snippet below represents one potential way to fix this issue

`function connect()
{
global $lang, $mybb;

            $this->connection = @fsockopen($this->host, $this->port, $error_number, $error_string, $this->timeout);

            if ( ! $this->connection )
                    {
                          $this->fatal_error("A connection cannot be established to the mail server specified.");
                            return false;

                  }

Said that, this is not the only change required since after doing so, the user is sent a positive notification on the screen , rather than a message indicating that something went wrong, i'm afraid more work is required in the upstream call chain so that when a FALSE is returned it must be interpreted as a failure and some action taken. I didn't dig too much yet but i think these changes should be incorporated to turn the mail process robust enough. ( it seems quite rough for now )

dvz and others added 30 commits October 31, 2021 23:04
…more recently (#4479)

* Threaded Mode Can't get back to old threads if sorted more recently

From report here: https://community.mybb.com/thread-234049.html

* Remove redundant check
Thank you @yuliu 

Tested, works fine with one minor change.

Also fixed the indentation :)
…stence in `column_default` for `DB_PgSQL` and `PostgresPdoDbDriver`, rather than the field's primary key.
Remove <strong></strong> from lang file
Fix the space between checkbox and text
* Removes query part in SQLite's `optimize_table()` that causes errors.

* Refines comments.
Fixes #4512 - forumdisplay_forumsort template is not cached
yuliu and others added 29 commits December 5, 2022 14:00
#4626)

* Fixes #4624 Custom User Title Maximum Length (Not Working As Intended)

* Forgot Mod CP

* Reverted previous fixes; updated setting code
…osts > Moderation Queue > Posts Awaiting Moderation and Attachments Awaiting Moderation.
…intenance > System Email Log, User Email Log, and Statistics (> Overall Statistics).
…ad()` for possible repeatedly loading language files.
…plugin hooks in `my_mail()` and `get_my_mailhandler()`.
@glwrba glwrba closed this Apr 4, 2023
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.

None yet

7 participants