/
class.freewheel.php
307 lines (277 loc) · 8.64 KB
/
class.freewheel.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
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
<?php
class FreeWheel extends HTTP {
public $api_key = NULL;
public $api_url = "https://api.freewheel.tv";
public $total_records = 0;
function __construct($api_key=NULL)
{
if (is_null($api_key)) {
$this->set_error("API KEY was NULL. You must supply an API KEY to use this library.");
exit;
}
$this->api_key = $api_key;
$this->method = "GET";
}
function __destruct() {}
protected function call($path=NULL, $type="all")
{
$this->total_records = 0;
$this->errors = array();
if (is_null($path)) {
$this->set_error("The API path is not set. You cannot make any API calls without this path being set.");
return false;
}
$headers = array(
"Accept: application/xml",
"Content-Type: application/xml",
"X-FreeWheelToken: " . $this->api_key
);
$this->options[CURLOPT_HTTPHEADER] = $headers;
$this->url = $this->api_url . $path;
$xml = $this->connect(TRUE);
//If an error, address it
//GET, PUT, DELETE should return 200 on thumbs up
//POST should return 201 on thumbs up
$status_code = (strtoupper($this->method) == "POST") ? "201" : "200";
if (!stristr($this->get("status"), $status_code)) {
$this->set_error("Returned a status error: " . $this->get("status"));
try {
$errors = (array)@new SimpleXMLElement($xml);
if (is_array($errors)) {
foreach ($errors as $k => $v) {
$this->set_error($v);
}
}
}
catch (Exception $e) {}
return false;
}
//If no errors and method is DELETE, return true
if (strtoupper($this->method) == "DELETE") {
return true;
}
//Any other method (POST, GET, PUT); try to return the XML
try {
return $this->format((array)@new SimpleXMLElement($xml), $type, $this->key_name);
}
catch (Exception $e) {
$this->set_error("XML returned from FreeWheel is invalid.");
return false;
}
}
protected function cdata_add($str)
{
if (stristr($str, "<") || stristr($str, ">")) {
return "<![CDATA[" . $str . "]]>";
}
return $str;
}
protected function cdata_remove($str)
{
$pattern = "/((<|<)!\[CDATA\[)(.*?)(\]\](>|>))/is";
if (preg_match($pattern, $str)) {
return preg_replace($pattern, "$3", $str);
}
return $str;
}
public function countries($filepath=NULL, $find=NULL)
{
return $this->parse_files($filepath, $find, "countries");
}
protected function create_xml($arr, $keys)
{
$tmp_keys = array();
$required = array();
$total_errors = 0;
$xml = NULL;
$arr_keys = array();
foreach ($arr as $k => $v) {
$arr_keys[$k] = strtolower($k);
}
foreach ($keys as $k => $v) {
$tmp = explode(":", $k);
$key = strtolower($tmp[0]);
$tmp_keys[$key] = array();
if (strtolower($tmp[1]) == "required") {
if (!in_array($key, $arr_keys) && (strtoupper($this->method) == "POST" || $this->is_copy !== TRUE)) {
$this->set_error("Missing Required Field: " . $key);
$total_errors += 1;
}
$tmp_keys[$key]['required'] = TRUE;
}
else {
$tmp_keys[$key]['required'] = FALSE;
}
$tmp = explode(":", $v);
$type = $tmp[0];
$tmp_keys[$key]['type'] = strtolower($type);
$tmp = explode(",", $tmp[1]);
if ($type == "enum") {
$tmp_keys[$key]['enum'] = $tmp;
}
elseif ($type == "string") {
$tmp_keys[$key]['max'] = (count($tmp) > 1) ? $tmp[1] : $tmp[0];
$tmp_keys[$key]['min'] = (count($tmp) > 1) ? $tmp[0] : 0;
}
elseif ($type == "decimal") {
$tmp_keys[$key]['max'] = $tmp[0];
$tmp_keys[$key]['decimals'] = (isset($tmp[1])) ? $tmp[1] : NULL;
}
}
foreach ($arr as $k => $v) {
$ok = $k;
$k = strtolower($k);
$error_found = FALSE;
if (!array_key_exists($k, $tmp_keys)) {
$this->set_error("Invalid Key: " . $ok);
$error_found = TRUE;
$total_errors += 1;
}
else {
if ($tmp_keys[$k]['required'] === TRUE && (empty($v) || is_null($v))) {
$this->set_error("Field Cannot Be Blank: " . $ok);
$error_found = TRUE;
$total_errors += 1;
}
if ($tmp_keys[$k]['type'] == "string" && !is_string($v)) {
$this->set_error("Type Mismatch (string): " . $ok);
$error_found = TRUE;
$total_errors += 1;
}
if ($tmp_keys[$k]['type'] == "boolean" && ($v !== FALSE && $v !== TRUE && strolower($v) != "true" && strtolower($v) != "false")) {
$this->set_error("Type Mismatch (boolean): " . $ok);
$error_found = TRUE;
$total_errors += 1;
}
if ($tmp_keys[$k]['type'] == "datetime" && !preg_match("/(\d{4}-\d{2}-\d{2})(T|\s)(\d{2}:\d{2}:\d{2})((\+|-)(\d{2}:\d{2}))?/", $v)) {
$this->set_error("Invalid format (datetime): Must be YYYY-MM-DDThh:mm:ssTZD or YYYY-MM-DD hh:mm:ss, your defined: " . $v);
$error_found = TRUE;
$total_errors += 1;
}
if ($tmp_keys[$k]['type'] == "string" && strlen($v) < $tmp_keys[$k]['min']) {
$this->set_error("String too short: Length should be (" . $tmp_keys[$k]['min'] . ") was (" . strlen($v) . ")");
$error_found = TRUE;
$total_errors += 1;
}
if ($tmp_keys[$k]['type'] == "string" && strlen($v) > $tmp_keys[$k]['max']) {
$this->set_error("String too long: Length should be (" . $tmp_keys[$k]['max'] . ") was (" . strlen($v) . ")");
$error_found = TRUE;
$total_errors += 1;
}
if ($tmp_keys[$k]['type'] == "enum" && !in_array($v, $tmp_keys[$k]['enum'])) {
$this->set_error("Invalid Value: You sent - " . $ok . ", must be on of these - " . implode(",", $tmp_keys[$k]['enum']));
$error_found = TRUE;
$total_errors += 1;
}
if ($tmp_keys[$k]['type'] == "integer" || $tmp_keys[$k]['type'] == "decimal") {
if (!is_numeric($v)) {
$this->set_error("Type Mismatch (" . $tmp_keys[$k]['type'] . "): " . $ok);
$error_found = TRUE;
$total_errors += 1;
}
else {
if ($tmp_keys[$k]['type'] == "decimal") {
$tmp = explode(".", $v);
if (!empty($tmp_keys[$k]['max']) && strlen($tmp[0]) > $tmp_keys[$k]['max']) {
$this->set_error("Exceeding Numeric: " . $ok . " - Length should be (" . $tmp_keys[$k]['max'] . ") was (" . strlen($tmp[0]) . ")");
$error_found = TRUE;
$total_errors += 1;
}
if (!empty($tmp_keys[$k]['decimals']) && strlen($tmp[1]) > $tmp_keys[$k]['decimals']) {
$this->set_error("Exceeding Decimal: " . $ok . " - Length should be (" . $tmp_keys[$k]['decimals'] . ") was (" . strlen($tmp[1]) . ")");
$error_found = TRUE;
$total_errors += 1;
}
}
}
}
if (strlen($v) > $tmp_keys[$k]['max'] && !empty($tmp_keys[$k]['max'])) {
$this->set_error("Max Character Mismatch: " . $ok . " - Looking For (" . $tmp_keys[$k]['max'] . ") was (" . strlen($v) . ")");
$error_found = TRUE;
$total_errors += 1;
}
}
$xml .= ($error_found === FALSE) ? "<$k>" . $this->cdata_add($v) . "</$k>" : "";
}
return ($total_errors == 0) ? $xml : FALSE;
}
protected function format($xml, $var, $type)
{
$tmp = array();
$x = 0;
$xml = (is_numeric($var)) ? $xml : (array)$xml[$type];
$this->total_records = 0;
foreach ($xml as $k => $v) {
if (!is_numeric($k)) {
if ($k == "@attributes") {
$k = "uri";
$v = $xml['@attributes']['uri'];
}
$v = (is_object($v) && empty($v)) ? "" : $v;
$tmp[$k] = $v;
$this->total_records = 1;
}
else {
$data = (array)$xml[$k];
if (!is_array($tmp[$x])) { $tmp[$x] = array(); }
foreach ($data as $kk => $vv) {
if ($kk == "@attributes") {
$kk = "uri";
$vv = $data['@attributes']['uri'];
}
$vv = (is_object($vv) && empty($vv)) ? "" : $vv;
$tmp[$x][$kk] = $this->cdata_remove($vv);
}
$x += 1;
}
}
$this->total_records = ($this->total_records < 1) ? @count($tmp) : $this->total_records;
return $tmp;
}
protected function is_serialized($data)
{
return (@unserialize($data) !== false);
}
protected parse_files($filepath=NULL, $find=NULL, $sc="countries")
{
$filepath = (is_null($filepath)) ? "freewheel-api-$sc.txt" : $filepath;
if (file_exists($filepath)) {
$arr = array();
$str = file_get_contents($filepath);
if ($this->is_serialized($str)) {
$arr = unserialize($str);
}
else {
$str = explode("\n", $str);
foreach (explode("\n", $str) as $line) {
$tmp = explode("\t", $line);
$arr[$tmp[0]] = $tmp[1];
}
}
if (is_null($find)) {
return $arr;
}
else {
if (strlen($find) > 2) {
foreach ($arr as $k => $v) {
if (strtolower($find) == strtolower($v)) {
return $k;
}
}
}
else {
return $arr[strtoupper($find)];
}
}
}
else {
$this->set_error("Could not open your $sc file under $filepath");
return false;
}
}
public function states($filepath=NULL, $find=NULL)
{
return $this->parse_files($filepath, $find, "states");
}
}
?>