indefero/src/IDF/Webhook.php

105 lines
3.5 KiB
PHP
Raw Normal View History

2010-05-10 08:11:27 +00:00
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
2011-04-02 19:37:07 +00:00
# Copyright (C) 2008-2011 Céondo Ltd and contributors.
2010-05-10 08:11:27 +00:00
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Management of the webhooks.
*
* The class provides the tools to perform the POST request with
* authentication for the webhooks.
*
*/
class IDF_Webhook
{
/**
* Perform the request given the webhook payload.
2010-05-10 08:11:27 +00:00
*
* @param array Payload
* @return bool Success or error
*/
public static function processNotification($payload)
2010-05-10 08:11:27 +00:00
{
$data = json_encode($payload['to_send']);
$sign = hash_hmac('md5', $data, $payload['authkey']);
$params = array('http' => array(
'method' => empty($payload['method']) ? 'POST' : $payload['method'],
2010-05-10 08:11:27 +00:00
'content' => $data,
'user_agent' => 'Indefero Hook Sender (http://www.indefero.net)',
'max_redirects' => 0,
2010-05-10 08:11:27 +00:00
'timeout' => 15,
'header'=> 'Post-Commit-Hook-Hmac: '.$sign."\r\n"
2010-05-10 08:11:27 +00:00
.'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
2010-05-10 08:11:27 +00:00
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 (!in_array($item->type, array('new_commit', 'upload'))) {
2010-05-10 08:11:27 +00:00
// We do nothing.
return;
}
if (isset($params['res']['IDF_Webhook::process']) and
2010-05-10 08:11:27 +00:00
$params['res']['IDF_Webhook::process'] == true) {
// Already processed.
return;
}
2010-05-10 08:43:47 +00:00
if ($item->payload['url'] == '') {
// We do nothing.
return;
}
2010-05-10 08:11:27 +00:00
// We have either to retry or to push for the first time.
$res = self::processNotification($item->payload);
2010-05-10 08:11:27 +00:00
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;
}
}
}