-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.php
More file actions
165 lines (147 loc) · 5.46 KB
/
Base.php
File metadata and controls
165 lines (147 loc) · 5.46 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
162
163
164
165
<?php
namespace RedBrick\Shared\VirusTotal;
/**
* Class: Base
*
* Base functionality for interacting with the VirusTotal API. The Domain, File, Ip, and Url extend this class with
* resource-specific methods.
*
* @author Brad Melanson <brad.melanson@redbrickmedia.com>
* @copyright 2014 Red Brick Media
* @package RedBrick\Shared\VirusTotal
* @abstract
*/
abstract class Base
{
const API_ENDPOINT = 'https://www.virustotal.com/vtapi/v2/';
const MAX_PUBLIC_API_CALL_RESOURCES = 4;
const MAX_PRIVATE_API_CALL_RESOURCES = 25;
protected $_apiKey;
protected $_client;
protected $_access;
/**
* __construct
*
* @param string $api_key Your VirusTotal API key.
* @param string $access API access level.
* @param \GuzzleHttp\ClientInterface $client An instance of a class implementing Guzzle's Client interface. A new
* instance of Guzzle's HTTP Client will be generated if left null.
*
* @throws \InvalidArgumentException No API key was provided.
* @throws \InvalidArgumentException The API access level was not one of 'public' or 'private'.
* @return self
*/
public function __construct( $api_key, $access = 'public', \GuzzleHttp\ClientInterface $client = null )
{
if ( empty( $api_key ) )
{
throw new \InvalidArgumentException( 'VirusTotal API key must be set.' );
}
if ( $access != 'public' && $access != 'private' )
{
throw new \InvalidArgumentException( "API access level must be one of 'public' or 'private'" );
}
$this->_apiKey = $api_key;
$this->_client = $client ?: new \GuzzleHttp\Client( array( 'base_url' => self::API_ENDPOINT ) );
$this->_access = $access;
}
/**
* _sendFile
*
* Send a file to VirusTotal.
*
* @param string $endpoint The relative URL to send to. The API_ENDPOINT constant will be prepended if no base URL
* is set on the Guzzle client.
* @param resource $file The file to send.
*
* @throws RateLimitException The API rate limit was exceeded.
* @return string JSON response from the VirusTotal API.
*/
protected function _sendFile( $endpoint, $file )
{
if ( $this->_client->getConfig( 'base_url' ) === null )
{
$endpoint = self::API_ENDPOINT . $endpoint;
}
$response = $this->_client->post( $endpoint, array(
'headers' => array(
'Content-Type' => 'multipart/form-data'
),
'body' => array(
'apikey' => $this->_apiKey,
'file' => fopen( $file, 'r' )
)
) );
// VirusTotal's API returns HTTP status code 204 and no response body when the
// public API rate limit is exceeded
if ( $response->getStatusCode() == 204 )
{
throw new RateLimitException( 'API Rate limit exceeded.' );
}
return $response;
}
/**
* _sendResource
*
* Send a resource-identifier hash to VirusTotal.
*
* @param string $endpoint The relative URL to send to. The API_ENDPOINT constant will be prepended if no base URL
* is set on the Guzzle client.
* @param string $resource The resource-identifier hash provided by VirusTotal.
*
* @throws RateLimitException The API rate limit was exceeded.
* @return string JSON response from the VirusTotal API.
*/
protected function _sendResource( $endpoint, $resource )
{
if ( $this->_client->getConfig( 'base_url' ) === null )
{
$endpoint = self::API_ENDPOINT . $endpoint;
}
$response = $this->_client->post( $endpoint, array(
'body' => array(
'apikey' => $this->_apiKey,
'resource' => $resource
)
) );
// VirusTotal's API returns HTTP status code 204 and no response body when the
// public API rate limit is exceeded
if ( $response->getStatusCode() == 204 )
{
throw new RateLimitException( 'API Rate limit exceeded.' );
}
return $response;
}
/**
* _sendUrl
*
* Send a URL to VirusTotal.
*
* @param string $endpoint The relative URL to send to. The API_ENDPOINT constant will be prepended if no base URL
* is set on the Guzzle client.
* @param string $url The URL to send.
*
* @throws RateLimitException The API rate limit was exceeded.
* @return string JSON response from the VirusTotal API.
*/
protected function _sendUrl( $endpoint, $url )
{
if ( $this->_client->getConfig( 'base_url' ) === null )
{
$endpoint = self::API_ENDPOINT . $endpoint;
}
$response = $this->_client->post( $endpoint, array(
'body' => array(
'apikey' => $this->_apiKey,
'url' => $url
)
) );
// VirusTotal's API returns HTTP status code 204 and no response body when the
// public API rate limit is exceeded
if ( $response->getStatusCode() == 204 )
{
throw new RateLimitException( 'API Rate limit exceeded.' );
}
return $response;
}
}