-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.utopian.php
More file actions
183 lines (166 loc) · 4.56 KB
/
class.utopian.php
File metadata and controls
183 lines (166 loc) · 4.56 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
// Author: @justyy
// License: MIT
class Utopian {
/*
define base API access end point
*/
private $API_BASE_URL = "https://api.utopian.io/api/";
/*
error string
*/
private $error = "";
/*
get current error
*/
public function GetError() {
return $this->error;
}
/*
reset status
*/
public function Reset() {
$this->error = '';
}
/*
constructor
*/
public function Utopian() {
}
/*
call API and set status
*/
public function CallAPI($api, $method, $data = null, $headers = null) {
$url = $this->API_BASE_URL . $api . '/';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if ($headers) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
switch ($method) {
case "GET":
curl_setopt($curl, CURLOPT_URL, $url . "?" . $data);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "POST":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
break;
case "PUT":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
break;
}
$response = curl_exec($curl);
$data = json_decode($response);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check the HTTP Status code
switch ($httpCode) {
case 200:
$this->error = "200: OK";
break;
case 404:
$this->error = "404: API Not found";
break;
case 500:
$this->error = "500: Servers replied with an error.";
break;
case 502:
$this->error = "502: Servers may be down or being upgraded. Hopefully they'll be OK soon!";
break;
case 503:
$this->error = "503: Service unavailable. Hopefully they'll be OK soon!";
break;
default:
$this->error = "Undocumented error: " . $httpCode . " : " . curl_error($curl);
break;
}
curl_close($curl);
return ($data);
}
/*
Get a list of moderators
*/
public function GetModerators() {
return $this->CallAPI("moderators", "GET");
}
/*
Get a list of sponsors
*/
public function GetSponsors() {
return $this->CallAPI("sponsors", "GET");
}
/*
Check if given account is moderator
*/
public function IsModerator($account) {
$moderators = $this->GetModerators();
if ($moderators == null || $moderators->results == null) {
throw "Error: API failure at IsModerator";
}
$account = strtolower($account);
foreach ($moderators->results as $acc) {
if (strtolower($acc->account) == $account) {
return true;
}
}
return false;
}
/*
Check if given account is sponser
*/
public function IsSponsor($account) {
$sponsors = $this->GetSponsors();
if ($sponsors == null || $sponsors->results == null) {
throw "Error: API failure at IsSponsor";
}
$account = strtolower($account);
foreach ($sponsors->results as $acc) {
if (strtolower($acc->account) == $account) {
return true;
}
}
return false;
}
/*
Get posts
*/
public function GetPosts($param = null) {
return $this->CallAPI("posts", "GET", $param);
}
/*
Get a Post
*/
public function GetPost($account, $permlink) {
return $this->CallAPI("posts/$account/$permlink", "GET");
}
/*
Get stats
*/
public function GetStats() {
return $this->CallAPI("stats", "GET")->stats;
}
/*
Check is bot voting
*/
public function IsBotVoting() {
$stats = $this->GetStats();
return $stats->bot_is_voting;
}
/*
Count
*/
public function GetCount($param = null) {
if (!$param) {
$param = array("limit" => 1);
} else {
$param['limit'] = 1;
}
return $this->GetPosts($param)->total;
}
}