-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathCampfireNotify.php
More file actions
161 lines (133 loc) · 4.43 KB
/
Copy pathCampfireNotify.php
File metadata and controls
161 lines (133 loc) · 4.43 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace PHPCensor\Plugin;
use PHPCensor\Builder;
use PHPCensor\Common\Exception\InvalidArgumentException;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* Campfire Plugin - Allows Campfire API actions. Strongly based on icecube (http://labs.mimmin.com/icecube)
*
* @package PHP Censor
* @subpackage Application
*
* @author André Cianfarani <acianfa@gmail.com>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class CampfireNotify extends Plugin
{
protected $url;
protected $authToken;
protected $userAgent;
protected $cookie = 'php-censor-cookie';
protected $verbose = false;
protected $room;
protected $message;
/**
* @return string
*/
public static function pluginName()
{
return 'campfire_notify';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$this->message = $options['message'];
$version = $this->builder->interpolate('%SYSTEM_VERSION%');
$this->userAgent = 'PHP Censor/' . $version;
if (isset($options['verbose']) && $options['verbose']) {
$this->verbose = true;
}
$buildSettings = $this->builder->getConfig('build_settings');
if (isset($buildSettings['campfire_notify'])) {
$campfire = $buildSettings['campfire_notify'];
$this->url = $campfire['url'];
if (\array_key_exists('auth_token', $campfire)) {
$this->authToken = $this->builder->interpolate($campfire['auth_token'], true);
}
if (\array_key_exists('room', $campfire)) {
$this->room = $campfire['room'];
}
} else {
throw new InvalidArgumentException('No connection parameters given for Campfire plugin');
}
}
/**
* Run the Campfire plugin.
* @return bool|mixed
*/
public function execute()
{
$this->message = $this->builder->interpolate($this->message);
$this->joinRoom($this->room);
$status = $this->speak($this->message, $this->room);
$this->leaveRoom($this->room);
return $status;
}
/**
* Join a Campfire room.
*/
public function joinRoom($roomId)
{
$this->getPageByPost('/room/'.$roomId.'/join.json');
}
/**
* Leave a Campfire room.
*/
public function leaveRoom($roomId)
{
$this->getPageByPost('/room/'.$roomId.'/leave.json');
}
/**
* Send a message to a campfire room.
* @param bool $isPaste
* @return bool|mixed
*/
public function speak($message, $roomId, $isPaste = false)
{
$page = '/room/'.$roomId.'/speak.json';
if ($isPaste) {
$type = 'PasteMessage';
} else {
$type = 'TextMessage';
}
return $this->getPageByPost($page, ['message' => ['type' => $type, 'body' => $message]]);
}
/**
* Make a request to Campfire.
* @param null $data
* @return bool|mixed
*/
private function getPageByPost($page, $data = null)
{
$url = $this->url . $page;
// The new API allows JSON, so we can pass
// PHP data structures instead of old school POST
$json = \json_encode($data);
// cURL init & config
$handle = \curl_init();
\curl_setopt($handle, CURLOPT_URL, $url);
\curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($handle, CURLOPT_POST, 1);
\curl_setopt($handle, CURLOPT_USERAGENT, $this->userAgent);
\curl_setopt($handle, CURLOPT_VERBOSE, $this->verbose);
\curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
\curl_setopt($handle, CURLOPT_USERPWD, $this->authToken . ':x');
\curl_setopt($handle, CURLOPT_HTTPHEADER, ["Content-type: application/json"]);
\curl_setopt($handle, CURLOPT_COOKIEFILE, $this->cookie);
\curl_setopt($handle, CURLOPT_POSTFIELDS, $json);
$output = \curl_exec($handle);
\curl_close($handle);
// We tend to get one space with an otherwise blank response
$output = \trim($output);
if (\strlen($output)) {
/* Responses are JSON. Decode it to a data structure */
return \json_decode($output);
}
// Simple 200 OK response (such as for joining a room)
return true;
}
}