summaryrefslogtreecommitdiff
path: root/app/Model/Webhook.php
blob: 241806ba01a82f543fbdd85b1fe67f129d96b320 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php

namespace Model;

use Event\WebhookListener;

/**
 * Webhook model
 *
 * @package  model
 * @author   Frederic Guillot
 */
class Webhook extends Base
{
    /**
     * HTTP connection timeout in seconds
     *
     * @var integer
     */
    const HTTP_TIMEOUT = 1;

    /**
     * Number of maximum redirections for the HTTP client
     *
     * @var integer
     */
    const HTTP_MAX_REDIRECTS = 3;

    /**
     * HTTP client user agent
     *
     * @var string
     */
    const HTTP_USER_AGENT = 'Kanboard Webhook';

    /**
     * URL to call for task creation
     *
     * @access private
     * @var string
     */
    private $url_task_creation = '';

    /**
     * URL to call for task modification
     *
     * @access private
     * @var string
     */
    private $url_task_modification = '';

    /**
     * Webook token
     *
     * @access private
     * @var string
     */
    private $token = '';

    /**
     * Attach events
     *
     * @access public
     */
    public function attachEvents()
    {
        $this->url_task_creation = $this->config->get('webhook_url_task_creation');
        $this->url_task_modification = $this->config->get('webhook_url_task_modification');
        $this->token = $this->config->get('webhook_token');

        if ($this->url_task_creation) {
            $this->attachCreateEvents();
        }

        if ($this->url_task_modification) {
            $this->attachUpdateEvents();
        }
    }

    /**
     * Attach events for task modification
     *
     * @access public
     */
    public function attachUpdateEvents()
    {
        $events = array(
            Task::EVENT_UPDATE,
            Task::EVENT_CLOSE,
            Task::EVENT_OPEN,
        );

        $listener = new WebhookListener($this->url_task_modification, $this);

        foreach ($events as $event_name) {
            $this->event->attach($event_name, $listener);
        }
    }

    /**
     * Attach events for task creation
     *
     * @access public
     */
    public function attachCreateEvents()
    {
        $this->event->attach(Task::EVENT_CREATE, new WebhookListener($this->url_task_creation, $this));
    }

    /**
     * Call the external URL
     *
     * @access public
     * @param  string   $url    URL to call
     * @param  array    $task   Task data
     */
    public function notify($url, array $task)
    {
        $headers = array(
            'Connection: close',
            'User-Agent: '.self::HTTP_USER_AGENT,
        );

        $context = stream_context_create(array(
            'http' => array(
                'method' => 'POST',
                'protocol_version' => 1.1,
                'timeout' => self::HTTP_TIMEOUT,
                'max_redirects' => self::HTTP_MAX_REDIRECTS,
                'header' => implode("\r\n", $headers),
                'content' => json_encode($task)
            )
        ));

        if (strpos($url, '?') !== false) {
            $url .= '&token='.$this->token;
        }
        else {
            $url .= '?token='.$this->token;
        }

        @file_get_contents($url, false, $context);
    }
}