-
Notifications
You must be signed in to change notification settings - Fork 0
/
magerror.php
149 lines (128 loc) · 3.92 KB
/
magerror.php
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
<?php
/**
* Copyright © 2016 Magerror (https://www.magerror.com/)
* See LICENSE bundled with this file for license details.
*/
class Magerror
{
/**
* Client version
*
* @var string
*/
private $version = '0.3.0';
/**
* API token
*
* @var
*/
private $token;
/**
* API url
*
* @var string
*/
private $url = 'https://api.magerror.com/';
/**
* @var int
*/
private $quantity = 50;
/**
* Reports directory
*
* @var
*/
private $path;
/**
* @param $token
* @param $path
*/
public function __construct($token, $path)
{
$this->token = $token;
$this->path = $path;
}
/**
* Execute process
*/
public function run()
{
$directory = dir($this->path);
$counter = 0;
while ($file = $directory->read()) {
if (is_file($this->path . DIRECTORY_SEPARATOR . $file) && $counter < $this->quantity) {
$stat = stat($this->path . DIRECTORY_SEPARATOR . $file);
$file_name = $file;
$file_content = file_get_contents($this->path . DIRECTORY_SEPARATOR . $file);
$file_date = date('Y-m-d H:i:s', $stat['ctime']);
$request = array(
'name' => $file_name,
'content' => $file_content,
'date' => $file_date,
'version' => $this->version
);
$response = $this->postReport('report', $request);
if ($response['code'] == '204') {
unlink($this->path . DIRECTORY_SEPARATOR . $file);
}
$counter++;
} else {
continue;
}
}
if ($counter === 0) {
$this->callHeartbeat();
}
}
/**
* Perform post.
*
* @param $action
* @param array $fields
* @return mixed
*/
private function postReport($action, array $fields)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('token: ' . $this->token, 'Accept: application/json', 'Content-Type: application/json'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLINFO_HTTP_CODE, true);
curl_setopt($curl, CURLOPT_URL, $this->url . $action);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
$response['message'] = curl_exec($curl);
$response['code'] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $response;
}
/**
* @param $action
* @return mixed
*/
private function callHeartbeat()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('token: ' . $this->token, 'Accept: application/json', 'Content-Type: application/json'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLINFO_HTTP_CODE, true);
curl_setopt($curl, CURLOPT_URL, $this->url . 'heartbeat');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
$response['message'] = curl_exec($curl);
$response['code'] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $response;
}
}
$configuration = parse_ini_file("configuration.ini");
//Your API Token string
$token = $configuration['token'];
//Full path to your magento reports directory
$directory = $configuration['directory'];
$client = new Magerror($token, $directory);
$client->run();