· 8 years ago · Nov 23, 2017, 03:32 PM
1<?php
2
3namespace App\ESI;
4
5use Illuminate\Contracts\Bus\SelfHandling;
6use Illuminate\Database\Eloquent\Model;
7use Carbon\Carbon;
8use Cache;
9use Response;
10
11class ESITest extends Model
12{
13 private $builtUri;
14 private $apiHost;
15 protected $token;
16
17 private $clientID = '';
18 private $secretKey = '';
19
20 public $data;
21
22 public function __construct($token = null){
23
24 $this->token = $token;
25 $this->uri = 'null';
26 $this->uriData = [];
27 $this->data = [];
28 $this->builtUri = '';
29 $this->apiHost = "https://esi.tech.ccp.is/latest/";
30
31 }
32
33 public function buildUri($uri, $uriData){
34
35 foreach($uriData as $search => $replace){
36 $uri = str_replace("{" . $search . "}", $replace, $uri);
37 }
38
39 $this->builtUri = $uri;
40 return $uri;
41 }
42
43 private function checkCache(){
44
45 $log = \App\ESI\ESILog::where('uri', '=', $this->builtUri);
46
47 if($this->token)
48 $log->where('esi_id', '=', $this->token->id);
49
50 $entry = $log->orderBy('created_at', 'desc')->first();
51
52 if(is_object($entry) && Carbon::parse($entry->expires) > Carbon::now()){
53 return 1;
54 }else{
55 return 0;
56 }
57
58 }
59
60 private function logEsi(){
61
62 $logging = new \App\ESI\ESILog;
63 $logging->uri = $this->builtUri;
64 $expires = Carbon::now()->addSeconds(3600);
65 $logging->expires = $expires;
66 $logging->esi_id = null;
67 if($this->token)
68 $logging->esi_id = $this->token->id;
69 $logging->save();
70 return 1;
71
72 }
73
74 private function retrieveRequest($uri, $uriData = array(), $method = "GET"){
75
76 if($this->token){
77 if(Carbon::parse($this->token->expires) < Carbon::now())
78 $this->refreshToken($this->token);
79 }
80
81 $this->buildUri($uri, $uriData);
82 $data = array();
83
84 if($this->checkCache()){
85 echo 'Still cached';
86 return 0;
87 }
88
89 if($this->token){
90 $options = array(
91 'http' => array(
92 'header' => "Authorization: Bearer ". $this->token->access_token ."\r\nContent-type: application/x-www-form-urlencoded\r\nUser-Agent: Ch3ss/0.1b",
93 'method' => $method,
94 'content' => http_build_query($data),
95 ),
96 );
97 }else{
98 $options = array(
99 'http' => array(
100 'header' => "Content-type: application/x-www-form-urlencoded\r\nUser-Agent: Ch3ss/0.1b",
101 'method' => $method,
102 'content' => http_build_query($data),
103 ),
104 );
105 }
106
107 $context = stream_context_create($options);
108 $result = file_get_contents($this->apiHost . $this->builtUri, false, $context);
109 $response = json_decode($result);
110
111 $this->logEsi();
112
113 return $response;
114
115 }
116
117
118 private function refreshToken(){
119
120 $header = 'Authorization: Basic ' . base64_encode($this->clientID . ':' . $this->secretKey);
121 $fields_string = '';
122 $fields = array(
123 'grant_type' => 'refresh_token',
124 'refresh_token' => $this->token->refresh_token
125 );
126
127 foreach ($fields as $key => $value)
128 $fields_string .= $key . '=' . $value . '&';
129
130 rtrim($fields_string, '&');
131 $ch = curl_init();
132
133 curl_setopt_array(
134 $ch,
135 array(
136 CURLOPT_URL => 'https://login.eveonline.com/oauth/token',
137 CURLOPT_POST => true,
138 CURLOPT_POSTFIELDS => $fields_string,
139 CURLOPT_HTTPHEADER => array($header),
140 CURLOPT_RETURNTRANSFER => true,
141 CURLOPT_USERAGENT => 'Ch3ss/0.1b',
142 CURLOPT_SSL_VERIFYPEER => true,
143 CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1,
144 )
145 );
146
147 $resBody = curl_exec($ch);
148 $info = curl_getinfo($ch);
149 $err = curl_errno($ch);
150 $errmsg = curl_error($ch);
151 if ($err != 0)
152 throw new Exception($errmsg, $err);
153 if (!in_array($info['http_code'], array(200, 302)))
154 throw new Exception(
155 'HTTP response not OK: ' . (int)$info['http_code'] . '. Response body: ' . $resBody, $info['http_code']
156 );
157
158 curl_close($ch);
159 $response = json_decode($resBody);
160
161 $this->token->access_token = $response->access_token;
162 $this->token->refresh_token = $response->refresh_token;
163 $expires = Carbon::now();
164 $expires->addSeconds($response->expires_in);
165 $this->token->expires = $expires;
166 $this->token->save();
167 return 1;
168
169 }
170
171
172 public function marketPrices(){
173 $uri = 'markets/prices/';
174 $request = $this->retrieveRequest($uri);
175 return $request;
176 }
177
178
179 public function wallet(){
180
181 $uriData['character_id'] = $this->token->character_id;
182 $uri = 'characters/{character_id}/wallet/';
183 $request = $this->retrieveRequest($uri, $uriData);
184 return $request;
185 }
186
187
188
189
190
191
192/* TODO
193
194/characters/{character_id}/assets/
195Get character assets
196
197POST /characters/{character_id}/assets/names/
198Get character asset names
199
200POST /characters/{character_id}/assets/locations/
201Get character asset locations
202
203
204
205
206*/
207
208}