Contact form - 500 internal server error on first call

Hi,
I just set up a easiest possible Grav+Admin installation (Antimatter default template) on an Arvixe ASP (Windows) hosting. For now, I have to use Grav 1.0.5 (one version behind), as Arvixe supports up to PHP 5.5.4, which is below the Grav 1.0.6 minimum requirement of PHP 5.5.9. What a pain in the a…!

Anyway, I’m using a contact form (with eMail) as described here: http://learn.getgrav.org/advanced/contact-form
This works, but on the first call (of the day?), after clicking “Send” the website loads very long and then shows 500 internal server error.
Pressing F5 (page reload) and answering the “send form data again?” question with yes, the success page is shown.

logs/grav.log contains the following:
[2016-01-04 10:30:04] grav.CRITICAL: file_get_contents(): Failed to enable crypto - Trace:
#0 [internal function]: Whoops\Run->handleError(2, ‘file_get_conten…’, ‘E:\HostingSpace…’, 105, Array)
#1 E:\HostingSpace\mydomain\wwwroot\cms\user\plugins\form\form.php(105): file_get_contents(‘https://www.goo…’)
#2 [internal function]: Grav\Plugin\FormPlugin->onFormProce ssed(Object(RocketTheme\Toolbox\Eve nt\Event), ‘onFormProcessed’, Object(RocketTheme\Toolbox\Event\EventDispatcher))
#3 E:\HostingSpace\mydomain\wwwroot\cms\vendor\symfony\event-dispatcher\E ventDispatcher.php(181): call_user_func(Array, Object(RocketTheme\Toolbox\Event\Event), ‘onFormProcessed’, Object(RocketTheme\Toolbox\Event\EventDispatcher))
#4 E:\HostingSpace\mydomain\wwwroot\cms\vendor\symfony\event-dispatcher\E ventDispatcher.php(46): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, ‘onFormProcessed’, Object(RocketTheme\Toolbox\Event\Event))
#5 E:\HostingSpace\mydomain\wwwroot\cms\vendor\rockettheme oolbox\Event\ src\EventDispatcher.php(23): Symfony\Component\EventDispatcher\EventDispatcher->dispatch(‘onFormPro cessed’, Object(RocketTheme\Toolbox\Event\Event))
#6 E:\HostingSpace\mydomain\wwwroot\cms\system\src\Grav\Common\Grav.php(4 11): RocketTheme\Toolbox\Event\EventDispatcher->dispatch(‘onFormProcessed’, Object(RocketTheme\Toolbox\Event\Event))
#7 E:\HostingSpace\mydomain\wwwroot\cms\user\plugins\form\classes\form.ph p(205): Grav\Common\Grav->fireEvent(‘onFormProcessed’, Object(RocketTheme\Toolbox\Event\Event))
#8 E:\HostingSpace\mydomain\wwwroot\cms\user\plugins\form\form.php(59): Grav\Plugin\Form->post()
#9 [internal function]: Grav\Plugin\FormPlugin->onPageInitialized(Object(RocketTheme\Toolbox\E vent\Event), ‘onPageInitializ…’, Object(RocketTheme\Toolbox\Event\EventDispatcher))
#10 E:\HostingSpace\mydomain\wwwroot\cms\vendor\symfony\event-dispatcher\E ventDispatcher.php(181): call_user_func(Array, Object(RocketTheme\Toolbox\Event\Event), ‘onPageInitializ…’, Object(RocketTheme\Toolbox\Event\EventDispatcher))
#11 E:\HostingSpace\mydomain\wwwroot\cms\vendor\symfony\event-dispatcher\E ventDispatcher.php(46): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, ‘onPageInitializ…’, Object(RocketTheme\Toolbox\Event\Event))
#12 E:\HostingSpace\mydomain\wwwroot\cms\vendor\rockettheme oolbox\Event\ src\EventDispatcher.php(23): Symfony\Component\EventDispatcher\EventDispatcher->dispatch(‘onPageIni tializ…’, Object(RocketTheme\Toolbox\Event\Event))
#13 E:\HostingSpace\mydomain\wwwroot\cms\system\src\Grav\Common\Grav.php(4 11): RocketTheme\Toolbox\Event\EventDispatcher->dispatch(‘onPageInitializ… .’, NULL)
#14 E:\HostingSpace\mydomain\wwwroot\cms\system\src\Grav\Common\Grav.php(2 51): Grav\Common\Grav->fireEvent(‘onPageInitializ…’)
#15 E:\HostingSpace\mydomain\wwwroot\cms\index.php(37): Grav\Common\Grav->process()
#16 {main} [] []

Your help is very appreciated!
Helmut

Google reCaptcha is in use, I created an API key and placed it where it should be. And it works (most of the time), so there is no configuration fault.

The error says “Failed to enable crypto”. Never seen such error, but seems related to SSL, might be a misconfiguration on the server, but I’m not sure what it could be.

Does it run if you remove the Captcha from the form?

Hi flaviocopes, thank you for your response.
Yes, it works without reCaptcha.
I just found a thread on SO, where a very similar issue is discussed. There seems to be a bug within PHP file_get_contents() when accessing SSL3 sites.

So I did the following with the file user\plugins\form\form.php:
Added new function getSSLPage():

/**
 * Replacement for file_get_contents with https (SSL).
 *
 * @param string $url
 * @return string
 */
private function getSSLPage($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSLVERSION,3); 
    curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem"); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

Modified function onFormProcessed():

$url = 'https://www.google.com/recaptcha/api/siteverify?'.$query;
//$response = json_decode(file_get_contents($url), true);
$response = json_decode($this->getSSLPage($url), true);

No more 500 internal server error so far. I don’t get form eMails currently, but that might be unrelated to this issue.

Thanks for sharing your solution. BTW it would be nice if MUUT had an option to edit the title of a post and add “SOLVED”. That would make searching for solutions much easier.