all repos — nasg @ 6f6b4d020ab191d21371c78bab645057a97008f8

templates/Webhook.j2.php (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
<?php

function _syslog($msg) {
    $trace = debug_backtrace();
    $caller = $trace[1];
    $parent = $caller['function'];
    if (isset($caller['class']))
        $parent = $caller['class'] . '::' . $parent;

    return error_log( "{$parent}: {$msg}" );
}

$raw = file_get_contents("php://input");
try {
    $payload = json_decode($raw, TRUE);
}
catch (Exception $e) {
    header('HTTP/1.1 422 Unprocessable Entity');
    _syslog('[webhook] json_decode failed on:' . $raw);
    die('Unprocessable Entity');
}

if(isset($payload['secret']) && $payload['secret'] == '{{ webmentionio.secret }}' ) {
    $msg = sprintf('
Type: %s
Source: %s
Target: %s
From: %s

%s
',
    $payload['post']['wm-property'],
    $payload['source'],
    $payload['target'],
    $payload['post']['author']['name'],
    $payload['post']['content']['text']
    );

    _syslog('[webhook] accepted from webmention.io');
    mail("{{ author.email }}", "[webmention] {$payload['source']}", $msg);
    header('HTTP/1.1 202 Accepted');
    exit(0);
}
elseif(isset($payload['secret']) && $payload['secret'] == '{{ zapier.secret }}' ) {
    $logfile = sprintf('%s/zapier.log', $_SERVER['HOME']);
    $msg = sprintf("%s %s %s\n", $payload['source'], $payload['network'], $payload['url']);
    file_put_contents($logfile, $msg, FILE_APPEND | LOCK_EX);
    _syslog('[webhook] accepted from zapier');
    header('HTTP/1.1 202 Accepted');
    exit(0);
}


header('HTTP/1.1 400 Bad Request');
_syslog('[webhook] bad request:' . $raw);
die('Bad Request');