url_task_creation = $this->config->get('webhooks_url_task_creation'); $this->url_task_modification = $this->config->get('webhooks_url_task_modification'); $this->token = $this->config->get('webhooks_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() { $events = array( Task::EVENT_CREATE, ); $listener = new WebhookListener($this->url_task_creation, $this); foreach ($events as $event_name) { $this->event->attach($event_name, $listener); } } /** * 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); } }