array( 'method' => 'POST', 'content' => $data, 'user_agent' => 'Indefero Hook Sender (http://www.indefero.net)', 'max_redirects' => 0, 'timeout' => 15, 'header'=> 'Post-Commit-Hook-Hmac: '.$sign."\r\n" .'Content-Type: application/json'."\r\n", ) ); $url = $payload['url']; $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx); if (!$fp) { return false; } $meta = stream_get_meta_data($fp); @fclose($fp); if (!isset($meta['wrapper_data'][0]) or $meta['timed_out']) { return false; } if (0 === strpos($meta['wrapper_data'][0], 'HTTP/1.1 2') or 0 === strpos($meta['wrapper_data'][0], 'HTTP/1.1 3')) { return true; } return false; } /** * Process the webhook. * */ public static function process($sender, &$params) { $item = $params['item']; if ($item->type != 'new_commit') { // We do nothing. return; } if (isset($params['res']['IDF_Webhook::process']) and $params['res']['IDF_Webhook::process'] == true) { // Already processed. return; } if ($item->payload['url'] == '') { // We do nothing. return; } // We have either to retry or to push for the first time. $res = self::postNotification($item->payload); if ($res) { $params['res']['IDF_Webhook::process'] = true; } elseif ($item->trials >= 9) { // We are at trial 10, give up $params['res']['IDF_Webhook::process'] = true; } else { // Need to try again $params['res']['IDF_Webhook::process'] = false; } } }