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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
<?php
namespace APWG\API\Groups;
use APWG\API\AbstractClient;
use GuzzleHttp\Psr7\Request;
/**
* Interact with the Groups API
*
* Class GroupsClient
* @package APWG\API\Groups
* @author Andrew Breksa <andrew@apwg.org>
* @copyright Copyright (c) 2017 The Anti-Phishing Working Group
*/
class GroupsClient extends AbstractClient {
/**
* The group ID
*
* @var string
*/
private $group_id = NULL;
/**
* Returns the YAML Swagger 2 specification for the API
*
* @param string|null $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function getSpec($group_id = NULL) {
return $this->_call('get', $this->getGroupUrl($group_id) . '/spec');
}
/**
* Get the URL for a group
*
* @param null|string $group_id
*
* @return string
*/
public function getGroupUrl($group_id = NULL) {
if(is_null($group_id)) {
$group_id = $this->getGroupId();
}
if(is_null($group_id)) {
throw new \InvalidArgumentException('no group id provided via the method call and none set');
}
return 'groups/' . $group_id;
}
/**
* Get the stored group id
* @return string
*/
public function getGroupId() {
return $this->group_id;
}
/**
* Set the stored group id, used as the default when none is provided to the class methods
*
* @param string $group_id
*/
public function setGroupId($group_id) {
$this->group_id = $group_id;
}
/**
* Returns the JSON schema for a specific action, defaults to the full group threat model schema (GET)
*
* @param array $options
* @param string|null $action
* @param string|null $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function getSchema($options = [], $action = NULL, $group_id = NULL) {
if(is_null($action)) {
$action = 'get';
}
return $this->_call('get', $this->getGroupUrl($group_id) . '/schema/' . $action, [
'query' => $options,
]);
}
/**
* Return a JSON schema for the group's GET:/groups/<group_id> endpoint parameters, useful for validating Alert queries
*
* @param array $options
* @param null $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function getParamSchema($options = [], $group_id = NULL) {
return $this->_call('get', $this->getGroupUrl($group_id) . '/param_schema', [
'query' => $options,
]);
}
/**
* Search the activity listing for the group's entities
*
* @param array $options
* @param null|string $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function activity($options = [], $group_id = NULL) {
return $this->_call('get', $this->getGroupUrl($group_id) . '/activity', [
'query' => $options,
]);
}
/**
* Get header information for a specific entity
*
* @param int $entity_id
* @param null|string $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function assetHead($entity_id, $group_id = NULL) {
return $this->_call('head', $this->getGroupUrl($group_id) . '/' . $entity_id);
}
/**
* Search the activity listing for a specific entity
*
* @param int $entity_id
* @param array $options
* @param null|string $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function assetActivity($entity_id, $options = [], $group_id = NULL) {
return $this->_call('get', $this->getGroupUrl($group_id) . '/' . $entity_id . '/activity', [
'query' => $options,
]);
}
/**
* Edit a group entity
*
* @param int $entity_id
* @param array $data
* @param array $options
* @param null|string $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function patch($entity_id, $data = [], $options = [], $group_id = NULL) {
return $this->_call('patch', $this->getGroupUrl($group_id) . '/' . $entity_id, [
'json' => $data,
'query' => $options,
]);
}
/**
* Get a specific entity
*
* @param int $entity_id
* @param array $options
* @param null|string $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function get($entity_id, $options = [], $group_id = NULL) {
return $this->_call('get', $this->getGroupUrl($group_id) . '/' . $entity_id, [
'query' => $options,
]);
}
/**
* Search the group's entities
*
* @param array $options
* @param null|string $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function search($options = [], $group_id = NULL) {
return $this->_call('get', $this->getGroupUrl($group_id), [
'query' => $options,
]);
}
/**
* Submit a new entity to this group
*
* @param array $data
* @param array $options
* @param null|string $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function create($data = [], $options = [], $group_id = NULL) {
return $this->_call('post', $this->getGroupUrl($group_id), [
'json' => $data,
'query' => $options,
]);
}
/**
* @param int $entity_id
* @param string $file_path
* @param array $options
* @param null|string $group_id
* @param callable $progress_callback Callback to be executed on transfer progress, as per http://docs.guzzlephp.org/en/latest/request-options.html#progress
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function uploadFile($entity_id, $file_path, $options = [], $group_id = NULL, $progress_callback = NULL) {
return $this->_call('post', $this->getGroupUrl($group_id) . '/' . $entity_id . '/files',
array_merge(
[
'query' => $options,
'multipart' => [
[
'name' => 'file',
'contents' => fopen($file_path, 'r'),
'filename' => pathinfo($file_path, PATHINFO_BASENAME),
],
],
],
is_null($progress_callback) ? [] : ['progress' => $progress_callback]
));
}
/**
* @param int $entity_id
* @param string $file_hash
* @param array $options
* @param null $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function getFile($entity_id, $file_hash, $options = [], $group_id = NULL) {
return $this->_call('get', $this->getGroupUrl($group_id) . '/' . $entity_id . '/files/' . $file_hash,
[
'query' => $options,
]
);
}
/**
* @param int $entity_id
* @param string $file_hash
* @param array $options
* @param null $group_id
* @param callable $progress_callback Callback to be executed on transfer progress, as per http://docs.guzzlephp.org/en/latest/request-options.html#progress
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function downloadFile($entity_id, $file_hash, $options = [], $group_id = NULL, $progress_callback = NULL) {
$response = $this->_call('get', $this->getGroupUrl($group_id) . '/' . $entity_id . '/files/' . $file_hash,
[
'query' => $options,
]
);
$request = new Request('GET',
json_decode($response->getBody()->getContents(), TRUE)['_links']['download']['href']);
return $this->getClient()->send($request,
is_null($progress_callback) ? [] : ['progress' => $progress_callback]);
}
/**
* @param int $entity_id
* @param array $options
* @param null|int $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function getFiles($entity_id, $options = [], $group_id = NULL) {
return $this->_call('get', $this->getGroupUrl($group_id) . '/' . $entity_id . '/files',
[
'query' => $options,
]
);
}
/**
* @param int $entity_id
* @param array $options
* @param null|int $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function getNotes($entity_id, $options = [], $group_id = NULL) {
return $this->_call('get', $this->getGroupUrl($group_id) . '/' . $entity_id . '/notes',
[
'query' => $options,
]
);
}
/**
* @param int $entity_id
* @param string $note
* @param null|int $group_id
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function addNote($entity_id, $note, $group_id = NULL) {
return $this->_call('post', $this->getGroupUrl($group_id) . '/' . $entity_id . '/notes', [
'json' => ['note' => $note],
]);
}
}