Skip to content

Instantly share code, notes, and snippets.

@milo
Last active October 29, 2024 15:45
Show Gist options
  • Select an option

  • Save milo/daed6e958ea534e4eba3 to your computer and use it in GitHub Desktop.

Select an option

Save milo/daed6e958ea534e4eba3 to your computer and use it in GitHub Desktop.

Revisions

  1. milo revised this gist Aug 25, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions github-webhook-handler.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    /**
    * GitHub webhook handler template.
    *
    * @see https://developer.github.com/webhooks/
    * @see https://docs.github.com/webhooks/
    * @author Miloslav Hůla (https://github.com/milo)
    */

    @@ -20,16 +20,16 @@
    die();
    });

    $rawPost = NULL;
    if ($hookSecret !== NULL) {
    $rawPost = null;
    if ($hookSecret !== null) {
    if (!isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
    throw new \Exception("HTTP header 'X-Hub-Signature' is missing.");
    } elseif (!extension_loaded('hash')) {
    throw new \Exception("Missing 'hash' extension to check the secret code validity.");
    }

    list($algo, $hash) = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE'], 2) + array('', '');
    if (!in_array($algo, hash_algos(), TRUE)) {
    if (!in_array($algo, hash_algos(), true)) {
    throw new \Exception("Hash algorithm '$algo' is not supported.");
    }

  2. milo revised this gist Aug 25, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion github-webhook-handler.php
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@
    }

    $rawPost = file_get_contents('php://input');
    if ($hash !== hash_hmac($algo, $rawPost, $hookSecret)) {
    if (!hash_equals($hash, hash_hmac($algo, $rawPost, $hookSecret))) {
    throw new \Exception('Hook secret does not match.');
    }
    };
  3. milo revised this gist Jan 13, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions github-webhook-handler.php
    Original file line number Diff line number Diff line change
    @@ -39,13 +39,13 @@
    }
    };

    if (!isset($_SERVER['HTTP_CONTENT_TYPE'])) {
    if (!isset($_SERVER['CONTENT_TYPE'])) {
    throw new \Exception("Missing HTTP 'Content-Type' header.");
    } elseif (!isset($_SERVER['HTTP_X_GITHUB_EVENT'])) {
    throw new \Exception("Missing HTTP 'X-Github-Event' header.");
    }

    switch ($_SERVER['HTTP_CONTENT_TYPE']) {
    switch ($_SERVER['CONTENT_TYPE']) {
    case 'application/json':
    $json = $rawPost ?: file_get_contents('php://input');
    break;
    @@ -55,7 +55,7 @@
    break;

    default:
    throw new \Exception("Unsupported content type: $_SERVER[HTTP_CONTENT_TYPE]");
    throw new \Exception("Unsupported content type: $_SERVER[CONTENT_TYPE]");
    }

    # Payload structure depends on triggered event
  4. milo created this gist Aug 18, 2014.
    81 changes: 81 additions & 0 deletions github-webhook-handler.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    <?php

    /**
    * GitHub webhook handler template.
    *
    * @see https://developer.github.com/webhooks/
    * @author Miloslav Hůla (https://github.com/milo)
    */

    $hookSecret = 's.e.c.r.e.t'; # set NULL to disable check


    set_error_handler(function($severity, $message, $file, $line) {
    throw new \ErrorException($message, 0, $severity, $file, $line);
    });

    set_exception_handler(function($e) {
    header('HTTP/1.1 500 Internal Server Error');
    echo "Error on line {$e->getLine()}: " . htmlSpecialChars($e->getMessage());
    die();
    });

    $rawPost = NULL;
    if ($hookSecret !== NULL) {
    if (!isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
    throw new \Exception("HTTP header 'X-Hub-Signature' is missing.");
    } elseif (!extension_loaded('hash')) {
    throw new \Exception("Missing 'hash' extension to check the secret code validity.");
    }

    list($algo, $hash) = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE'], 2) + array('', '');
    if (!in_array($algo, hash_algos(), TRUE)) {
    throw new \Exception("Hash algorithm '$algo' is not supported.");
    }

    $rawPost = file_get_contents('php://input');
    if ($hash !== hash_hmac($algo, $rawPost, $hookSecret)) {
    throw new \Exception('Hook secret does not match.');
    }
    };

    if (!isset($_SERVER['HTTP_CONTENT_TYPE'])) {
    throw new \Exception("Missing HTTP 'Content-Type' header.");
    } elseif (!isset($_SERVER['HTTP_X_GITHUB_EVENT'])) {
    throw new \Exception("Missing HTTP 'X-Github-Event' header.");
    }

    switch ($_SERVER['HTTP_CONTENT_TYPE']) {
    case 'application/json':
    $json = $rawPost ?: file_get_contents('php://input');
    break;

    case 'application/x-www-form-urlencoded':
    $json = $_POST['payload'];
    break;

    default:
    throw new \Exception("Unsupported content type: $_SERVER[HTTP_CONTENT_TYPE]");
    }

    # Payload structure depends on triggered event
    # https://developer.github.com/v3/activity/events/types/
    $payload = json_decode($json);

    switch (strtolower($_SERVER['HTTP_X_GITHUB_EVENT'])) {
    case 'ping':
    echo 'pong';
    break;

    // case 'push':
    // break;

    // case 'create':
    // break;

    default:
    header('HTTP/1.0 404 Not Found');
    echo "Event:$_SERVER[HTTP_X_GITHUB_EVENT] Payload:\n";
    print_r($payload); # For debug only. Can be found in GitHub hook log.
    die();
    }