-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.php
700 lines (589 loc) · 21.6 KB
/
api.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
<?php
header('Access-Control-Allow-Origin: *');
include "etc/includes.php";
$json = file_get_contents('php://input');
$data = json_decode($json, true);
if (!$data) {
$data = $_GET;
}
if (!@$data["action"]) {
die();
}
foreach ($data as $key => $value) {
$data[$key] = trim($value, ". /");
}
$output["success"] = true;
/* VALIDATE KEY */
switch ($data["action"]) {
case "startSession":
case "checkName":
break;
default:
if ($data["key"]) {
$keyValid = @sql("SELECT * FROM `sessions` WHERE `id` = ?", [$data["key"]]);
if (!$keyValid) {
error("Invalid key");
}
}
else {
error("Missing key");
}
break;
}
/* VERIFY PERMISSIONS */
switch ($data["action"]) {
case "getConversations":
case "deleteMessage":
$verifyOwnership = isDomainInSession($data["domain"], $data["key"]);
if (!$verifyOwnership) {
error("This domain isn't part of your session.");
}
break;
case "getMessages":
switch (strlen($data["conversation"])) {
case 8:
$channelInfo = channelForID($data["conversation"]);
if (!$channelInfo) {
error("This channel doesn't exist?");
}
$verifyOwnership = isDomainInSession($data["domain"], $data["key"]);
if (!$verifyOwnership) {
error("This domain isn't part of your session.");
}
$domainInfo = domainForID($data["domain"]);
$tld = tldForDomain($domainInfo["domain"]);
if (!$channelInfo["public"] && $tld !== $channelInfo["name"]) {
$GLOBALS["needSLD"] = true;
error("You don't have access to this channel.");
}
else if ($domainInfo["locked"]) {
$GLOBALS["unverified"] = true;
error("You don't have access to this channel.");
}
break;
case 16:
$domains = domainIDSForSession($data["key"]);
$getConversation = sql("SELECT `users` FROM `conversations` WHERE `id` = ?", [$data["conversation"]])[0];
if ($getConversation) {
$users = json_decode($getConversation["users"]);
$hasPermissions = array_intersect($domains, $users);
if (!$hasPermissions) {
error("This conversation isn't part of your session.");
}
}
break;
default:
break;
}
break;
case "deleteAttachment":
$getAttachment = sql("SELECT * FROM `uploads` WHERE `id` = ? AND `session` = ?", [$data["id"], $data["key"]]);
if (!$getAttachment) {
error("This attachment isn't part of your session.");
}
break;
default:
break;
}
/* DO THE STUFF */
switch ($data["action"]) {
case "startSession":
$code = "V2-".generateCode("session");
sql("INSERT INTO `sessions` (id) VALUES (?)", [$code]);
$output["session"] = $code;
break;
case "listTLD":
$output["tlds"] = $specialDomains;
break;
case "checkTLD":
$userDomains = domainsForSession($data["key"]);
$tldMatches = [];
foreach ($userDomains as $key => $nameInfo) {
if (tldForDomain($nameInfo["domain"]) === $data["tld"]) {
array_push($tldMatches, $nameInfo["id"]);
}
}
$output["owned"] = $tldMatches;
if (in_array($data["tld"], $specialDomains)) {
$output["free"] = true;
}
else {
if (in_array($data["tld"], $allSpecialDomains)) {
$output["unavailable"] = true;
}
else {
$canPurchase = checkGateway($data["tld"]);
if ($canPurchase) {
$output["purchase"] = true;
}
else {
$output["unavailable"] = true;
}
}
}
break;
case "checkChannel":
$channelInfo = channelForName($data["channel"]);
if ($channelInfo) {
$output["channel"] = $channelInfo["id"];
}
break;
case "getInvite":
if (in_array($data["code"], $specialDomains)) {
$output["tld"] = $data["code"];
}
else {
$getTLD = @sql("SELECT `tld` FROM `invites` WHERE `code` = ?", [$data["code"]])[0];
if ($getTLD) {
$output["tld"] = $getTLD["tld"];
}
}
break;
case "getDomains":
$domains = [];
$getDomains = domainsForSession($data["key"]);
if ($getDomains) {
foreach ($getDomains as $key => $info) {
$domains[$info["id"]] = [
"domain" => $info["domain"],
"locked" => $info["locked"],
];
}
}
$output["domains"] = $domains;
break;
case "addDomain":
$data["domain"] = strtolower($data["domain"]);
if (strlen($data["domain"]) < 1) {
error("Well that can't be right.");
}
$tld = tldForDomain($data["domain"]);
if ($tld !== $data["domain"] && in_array($tld, $specialDomains)) {
error("Create a free SLD in the other field for this TLD.");
}
$verifyHandshake = domainIsHandshake($data["domain"]);
if (!$verifyHandshake) {
$verifyEthereum = domainIsEthereum($data["domain"]);
if (!$verifyEthereum) {
error("This name isn't a valid Handshake name.");
}
}
$getCode = @sql("SELECT `id`,`code` FROM `domains` WHERE `domain` = ? AND `session` = ? AND `deleted` = 0", [$data["domain"], $data["key"]])[0];
$getCodeAlt = @sql("SELECT `id`,`code` FROM `domains` WHERE `domain` = ? AND `session` IS NULL AND `deleted` = 0", [$data["domain"]])[0];
if ($getCode) {
$output["domain"] = $getCode["id"];
$output["code"] = "hns-chat=".$getCode["code"];
}
else if ($getCodeAlt) {
$output["domain"] = $getCodeAlt["id"];
$output["code"] = "hns-chat=".$getCodeAlt["code"];
}
else {
$domain = generateCode("domain");
$code = generateCode("code");
$addDomain = sql("INSERT INTO `domains` (id,domain,session,code,locked,created) VALUES (?,?,?,?,?,?)", [$domain, $data["domain"], $data["key"], $code, 1, time()]);
if ($addDomain) {
$output["domain"] = $domain;
$output["code"] = "hns-chat=".$code;
}
else {
error("An error occurred while tying this domain to your session. Try again?");
}
}
break;
case "addSLD":
$data["sld"] = strtolower($data["sld"]);
$data["tld"] = strtolower($data["tld"]);
$fullName = $data["sld"].".".$data["tld"];
if (strlen($data["sld"]) < 1 || strlen($data["tld"]) < 1) {
error("Well that can't be right.");
}
$validate = preg_match("/^(?:[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]|[A-Za-z0-9])$/", $data["sld"], $match);
if (!$match) {
error("A name can only contain letters, numbers, and hyphens, but can't start or end with a hyphen.");
}
if (!in_array($data["tld"], $specialDomains)) {
$validInvite = false;
if (@$data["invite"]) {
$checkInvite = @sql("SELECT * FROM `invites` WHERE `code` = ?", [$data["invite"]])[0];
if ($checkInvite) {
if ($checkInvite["tld"] === $data["tld"]) {
$validInvite = true;
}
}
}
if (!$validInvite) {
error("The selected TLD isn't available.");
}
}
if (in_array($data["sld"], $bannedNames)) {
error("The name you entered isn't available.");
}
$checkIfExists = sql("SELECT * FROM `domains` WHERE `domain` = ?", [$fullName]);
if ($checkIfExists) {
error("The name you entered isn't available.");
}
$domain = generateCode("domain");
$addDomain = sql("INSERT INTO `domains` (id,domain,session,locked,created) VALUES (?,?,?,?,?)", [$domain, $fullName, $data["key"], 0, time()]);
if ($addDomain) {
$output["domain"] = $domain;
}
else {
error("An error occurred while tying this domain to your session. Try again?");
}
break;
case "deleteDomain":
$deleteDomain = sql("UPDATE `domains` SET `deleted` = 1, `locked` = 1 WHERE `id` = ? AND `session` = ?", [$data["domain"], $data["key"]]);
if (!$deleteDomain) {
error("An error occurred while deleting this domain. Try again?");
}
break;
case "verifyDomain":
$getCode = @sql("SELECT * FROM `domains` WHERE `id` = ? AND `deleted` = 0", [$data["domain"]])[0];
if ($getCode) {
$verifyCode = $getCode["code"];
if (@$getCode["session"]) {
if ($getCode["session"] == $data["key"]) {
$domains = unlockedDomainIDSForSession($data["key"]);
if (in_array($data["domain"], $domains)) {
break;
}
else {
if (verifyCode($data["domain"], $verifyCode)) {
$unlockThisOne = sql("UPDATE `domains` SET `locked` = 0 WHERE `id` = ?", [$data["domain"]]);
$lockTheOthers = sql("UPDATE `domains` SET `locked` = 1 WHERE `domain` = ? AND `id` != ?", [$getCode["domain"], $data["domain"]]);
}
else {
error("The TXT record was not found.");
}
}
}
else {
error("This domain isn't part of your session.");
}
}
else {
if (verifyCode($data["domain"], $verifyCode)) {
$unlockThisOne = sql("UPDATE `domains` SET `locked` = 0, `session` = ? WHERE `id` = ?", [$data["key"], $data["domain"]]);
}
else {
error("The TXT record was not found.");
}
}
}
else {
error("We have no record of that domain.");
}
break;
case "verifySignature":
$domainInfo = domainForID($data["domain"]);
$verifyEthereum = domainIsEthereum($domainInfo["domain"]);
if ($verifyEthereum) {
if (verifySignature($domainInfo["domain"], $data["signature"], "hns-chat=".$domainInfo["code"], $data["account"])) {
$unlockThisOne = sql("UPDATE `domains` SET `locked` = 0, `signature` = ?, `eth` = ? WHERE `id` = ?", [$data["signature"], $data["account"], $data["domain"]]);
$lockTheOthers = sql("UPDATE `domains` SET `locked` = 1 WHERE `domain` = ? AND `id` != ?", [$domainInfo["domain"], $data["domain"]]);
}
else {
error("That didn't work :/");
}
}
else {
if (verifySignature($domainInfo["domain"], $data["signature"], "hns-chat=".$domainInfo["code"])) {
$unlockThisOne = sql("UPDATE `domains` SET `locked` = 0, `signature` = ? WHERE `id` = ?", [$data["signature"], $data["domain"]]);
$lockTheOthers = sql("UPDATE `domains` SET `locked` = 1 WHERE `domain` = ? AND `id` != ?", [$domainInfo["domain"], $data["domain"]]);
}
else {
error("That didn't work :/");
}
}
break;
case "getUsers":
$output["users"] = getUsers($allSpecialDomains);
break;
case "getConversations":
$conversations = [];
$allUsers = [];
$domainInfo = domainForID($data["domain"]);
$tld = tldForDomain($domainInfo["domain"]);
$getChannels = sql("SELECT * FROM `channels` WHERE `hidden` = 0");
if ($getChannels) {
$getUsers = sql("SELECT * FROM `domains` WHERE `claimed` = 1 AND `locked` = 0 AND `deleted` = 0 ORDER BY `domain` ASC");
foreach ($getUsers as $key => $userData) {
$allUsers[$userData["id"]] = [
"domain" => $userData["domain"]
];
}
foreach ($getChannels as $key => $info) {
$usersForChannel = $allUsers;
if (!$info["public"]) {
$tldUsers = [];
$theTLD = $info["name"];
$getTLDUsers = sql("SELECT * FROM `domains` WHERE `claimed` = 1 AND `locked` = 0 AND `deleted` = 0 AND (`domain` LIKE ? OR `domain` = ?) ORDER BY `domain` ASC", ["%.".$theTLD, $theTLD]);
if ($getTLDUsers) {
foreach ($getTLDUsers as $key => $userData) {
$tldUsers[$userData["id"]] = [
"domain" => $userData["domain"]
];
}
}
$usersForChannel = $tldUsers;
}
$latestMessage = [];
$getLatestMessage = @sql("SELECT * FROM `messages` WHERE `conversation` = ? ORDER BY `ai` DESC LIMIT 1", [$info["id"]])[0];
if ($getLatestMessage) {
$latestMessage = [
"user" => @$getLatestMessage["user"],
"message" => htmlentities(@$getLatestMessage["message"]),
"time" => @$getLatestMessage["time"]
];
}
if (!($info["public"] || $tld === $info["name"])) {
if (@$latestMessage["message"]) {
$latestMessage["message"] = "";
}
}
$unseenMessages = 0;
$unseenMentions = 0;
$signupDate = $domainInfo["created"];
if ($info["public"] || $tld === $info["name"]) {
$unseenMessages = @sql("SELECT * FROM `messages` WHERE `conversation` = ? AND `user` != ? AND NOT JSON_CONTAINS(`seen`, ?, '$') AND `time` >= ? LIMIT 1", [$info["id"], $data["domain"], '"'.$data["domain"].'"', $signupDate]);
$unseenMentions = @sql("SELECT * FROM `messages` WHERE `conversation` = ? AND `user` != ? AND NOT JSON_CONTAINS(`seen`, ?, '$') AND `time` >= ? AND `message` LIKE ? LIMIT 1", [$info["id"], $data["domain"], '"'.$data["domain"].'"', $signupDate, '%'.$data["domain"].'%']);
}
$conversations[$info["id"]] = [
"group" => 1,
"name" => $info["name"],
"users" => $usersForChannel,
"latestMessage" => $latestMessage,
"unreadMessages" => $unseenMessages ? 1 : 0,
"unreadMentions" => $unseenMentions ? 1 : 0,
"tldadmin" => $info["tldadmin"],
"admins" => json_decode($info["admins"], true)
];
}
}
$getConversations = sql("SELECT * FROM `conversations` WHERE JSON_CONTAINS(`users`, ?, '$')", ['"'.$data["domain"].'"']);
if ($getConversations) {
foreach ($getConversations as $key => $info) {
$users = [];
$decoded = json_decode($info["users"]);
foreach ($decoded as $user) {
$domainData = publicDataForDomainID($user);
$pubkey = publicKeyForDomain($user);
$domainData["pubkey"] = $pubkey;
$users[$user] = $domainData;
}
if ($users[$data["domain"]]["locked"]) {
foreach ($users as $key => $user) {
$users[$key]["locked"] = 1;
}
}
$latestMessage = [];
$getLatestMessage = @sql("SELECT * FROM `messages` WHERE `conversation` = ? ORDER BY `ai` DESC LIMIT 1", [$info["id"]])[0];
if ($getLatestMessage) {
$latestMessage = [
"user" => @$getLatestMessage["user"],
"message" => htmlentities(@$getLatestMessage["message"]),
"time" => @$getLatestMessage["time"]
];
}
$unseenMessages = @sql("SELECT * FROM `messages` WHERE `conversation` = ? AND `user` != ? AND NOT JSON_CONTAINS(`seen`, ?, '$') LIMIT 1", [$info["id"], $data["domain"], '"'.$data["domain"].'"']);
$conversations[$info["id"]] = [
"users" => $users,
"latestMessage" => $latestMessage,
"unreadMessages" => $unseenMessages ? 1 : 0,
];
}
}
$output["conversations"] = $conversations;
break;
case "getMessages":
$messages = [];
if (@$data["before"]) {
$getFirstMessage = @sql("SELECT `id` FROM `messages` WHERE `conversation` = ? ORDER BY `ai` ASC LIMIT 1", [$data["conversation"]])[0];
if ($getFirstMessage) {
$firstMessage = $getFirstMessage["id"];
}
$getMessages = sql("SELECT `id`,`time`,`conversation`,`user`,`message`,`replying`,`reactions` FROM `messages` WHERE `conversation` = ? AND `ai` < (SELECT `ai` FROM `messages` WHERE `id` = ?) ORDER BY `ai` DESC LIMIT 50", [$data["conversation"], $data["before"]]);
}
else if (@$data["after"]) {
$getMessages = sql("SELECT `id`,`time`,`conversation`,`user`,`message`,`replying`,`reactions` FROM `messages` WHERE `conversation` = ? AND `ai` > (SELECT `ai` FROM `messages` WHERE `id` = ?) ORDER BY `ai` DESC LIMIT 50", [$data["conversation"], $data["after"]]);
}
else {
$getFirstMessage = @sql("SELECT `id` FROM `messages` WHERE `conversation` = ? ORDER BY `ai` ASC LIMIT 1", [$data["conversation"]])[0];
if ($getFirstMessage) {
$firstMessage = $getFirstMessage["id"];
}
$getMessages = sql("SELECT `id`,`time`,`conversation`,`user`,`message`,`replying`,`reactions` FROM `messages` WHERE `conversation` = ? ORDER BY `ai` DESC LIMIT 50", [$data["conversation"]]);
}
if ($getMessages) {
foreach ($getMessages as $key => $info) {
if (@$firstMessage && $firstMessage == $info["id"]) {
$info["firstMessage"] = true;
}
if ($info["replying"]) {
$replyingMessage = @sql("SELECT `id`,`time`,`conversation`,`user`,`message` FROM `messages` WHERE `conversation` = ? AND `id` = ? ORDER BY `ai` DESC LIMIT 50", [$data["conversation"], $info["replying"]])[0];
if ($replyingMessage) {
$replyingMessage["reply"] = true;
$replyingMessage["message"] = htmlentities($replyingMessage["message"]);
$info["replying"] = $replyingMessage;
}
else {
$info["replying"] = null;
}
}
$info["message"] = htmlentities($info["message"]);
$messages[] = $info;
}
}
if (!@$data["before"]) {
$messages = array_reverse($messages);
}
$output["messages"] = $messages;
$markSeen = sql("UPDATE `messages` SET `seen` = JSON_ARRAY_APPEND(`seen`, '$', ?) WHERE `conversation` = ? AND NOT JSON_CONTAINS(`seen`, ?, '$') AND `user` != ?", [$data["domain"], $data["conversation"], '"'.$data["domain"].'"', $data["domain"]]);
break;
case "deleteAttachment":
$delete = sql("DELETE FROM `uploads` WHERE `id` = ? AND `session` = ?", [$data["id"], $data["key"]]);
if ($delete) {
unlink("/var/www/html/hnschat/uploads/".$data["id"]);
}
break;
case "setPublicKey":
$insert = sql("UPDATE `sessions` SET `pubkey` = ? WHERE `id` = ? AND `pubkey` IS NULL", [$data["pubkey"], $data["key"]]);
break;
case "getPublicKey":
$getKey = sql("SELECT `pubkey` FROM `sessions` WHERE `id` = ? AND `pubkey` IS NOT NULL", [$data["key"]])[0];
if ($getKey) {
$output["pubkey"] = $getKey["pubkey"];
}
break;
case "verifySession":
$checkSession = sql("SELECT * FROM `sessions` WHERE `id` = ? AND `pubkey` = ?", [$data["key"], $data["pubkey"]]);
if (!$checkSession) {
error("This session is invalid.");
}
break;
case "checkName":
$domainData = @publicDataForDomainID(@$data["from"]);
if ($domainData) {
if ($domainData["domain"] === $data["domain"]) {
error("Send a message to hnschat/ if you want to test :P");
}
}
if (strlen($data["domain"]) < 1) {
error("Well that can't be right.");
}
$getDomain = sql("SELECT * FROM `domains` WHERE `domain` = ? AND `claimed` = 1 AND `locked` = 0", [$data["domain"]]);
if (!$getDomain) {
error("This name is not either not claimed or not available right now.");
}
break;
case "getMetaTags":
$checkCache = @sql("SELECT `id`, `link`, `title`, `description`, `image` FROM `previews` WHERE `link` = ?", [$data["url"]])[0];
if ($checkCache) {
unset($checkCache["link"]);
foreach ($checkCache as $key => $value) {
if (!$value) {
unset($checkCache[$key]);
}
}
$tags = $checkCache;
}
else {
$tags = fetchMetaTags($data["url"]);
}
if (@$tags["id"]) {
if (@$tags["title"]) {
$output["tags"] = $tags;
}
if (@$output["tags"]["image"]) {
$output["tags"]["image"] = "/preview/".$tags["id"];
}
}
break;
case "getAddress":
$domainInfo = domainForID($data["domain"]);
$getAddress = fetchAddress($domainInfo["domain"]);
if (!$getAddress) {
error("This user isn't currently accepting payments.");
}
$output["address"] = trim($getAddress);
break;
case "saveSettings":
$settings = json_decode($data["settings"], true);
$domainInfo = domainForID($data["domain"]);
$tld = tldForDomain($domainInfo["domain"]);
if (@$settings["avatar"]) {
if (in_array($tld, $allSpecialDomains)) {
$settings["avatar"] = trim($settings["avatar"]);
if (!validImage($settings["avatar"])) {
error("The Avatar URL provided isn't a valid image.");
}
sql("UPDATE `domains` SET `avatar` = ? WHERE `id` = ? AND `session` = ?", [$settings["avatar"], $data["domain"], $data["key"]]);
$output["avatar"] = $settings["avatar"];
}
else {
error("Only SLD's of staked TLD's can set an Avatar here.");
}
}
if (@$settings["address"]) {
if (in_array($tld, $fullyStakedDomains)) {
$settings["address"] = trim($settings["address"]);
if (!validateAddress($settings["address"])) {
error("The HNS Address provided isn't valid.");
}
sql("UPDATE `domains` SET `address` = ? WHERE `id` = ? AND `session` = ?", [$settings["address"], $data["domain"], $data["key"]]);
}
else {
error("Only SLD's of certain staked TLD's can set an address here.");
}
}
break;
case "createPoll":
$insertPoll = sql("INSERT INTO `polls` (type,user,name,channel,private,description,tweet,time) VALUES (?,?,?,?,?,?,?,?)", [$data["type"], $data["user"], strtolower($data["name"]), $data["channel"], $data["private"], $data["description"], $data["tweet"], time()]);
if (!$insertPoll) {
error("Something went wrong :/");
}
break;
case "createChannel":
$data["name"] = strtolower($data["name"]);
$channelID = generateCode("channel");
$admins = json_encode([$data["user"]]);
$domainInfo = domainForID($data["user"]);
if ($data["tldadmin"] && $domainInfo["domain"] == $data["name"]) {
$admins = '[]';
}
$validate = preg_match("/^(?:[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]|[A-Za-z0-9])$/", $data["name"], $match);
if (!$match) {
error("A channel name can only contain letters, numbers, and hyphens, but can't start or end with a hyphen.");
}
if (channelForName($data["name"])) {
error("A channel name with this name already exists.");
}
$fee = $channelPrice.".".generateNumber(6);
$insertChannel = sql("INSERT INTO `channels` (id,name,public,tldadmin,admins,fee,created,hidden) VALUES (?,?,?,?,?,?,?,?)", [$channelID, $data["name"], $data["public"], $data["tldadmin"], $admins, $fee, time(), 1]);
if (!$insertChannel) {
error($admins);
}
$output["id"] = $channelID;
$output["fee"] = $fee;
break;
case "receivedPayment":
$channelInfo = channelForID($data["channel"]);
if ($data["amount"] != $channelInfo["fee"]) {
error("That's not the right amount.");
}
$validate = preg_match("/^(?:[a-z0-9]{64})$/", $data["tx"], $match);
if (!$match) {
error("Something is wrong with that transaction.");
}
$addTX = sql("UPDATE `channels` SET `tx` = ? WHERE `id` = ?", [$data["tx"], $data["channel"]]);
if (!$addTX) {
error("Something went wrong :/");
}
break;
default:
error("Not sure what you're trying to do...");
break;
}
die(json_encode($output));
?>