-
Notifications
You must be signed in to change notification settings - Fork 9
/
model.php
1456 lines (1284 loc) · 46.2 KB
/
model.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once 'resources/includes/constants.php';
require_once ROOT . '/user.php';
/**
* Uploaded model class.
* Used for a more object oriented structure for models.
*
* @author Lauge Rud Knudsen <[email protected]>
* @since V2
*/
class Model {
const PLATFORM_PC = 'pc';
const PLATFORM_QUEST = 'quest';
const FORMAT_UAB = 'uabe';
const FORMAT_BMBF = 'bmbf';
/** @var int The id of the model */
protected $id;
/** @var string The type of the model */
protected $type;
/** @var string The name of the model */
protected $name;
/** @var string The author of the model */
protected $author;
/** @var string The filename of the model */
protected $filename;
/** @var string The filename of the model's image */
protected $image;
/** @var string[] An array of strings */
protected $tags;
/** @var string Not sure what this one is tbh */
protected $hash;
/**
* @var string Beast Saber username
* @deprecated Moved to the {@see User} class
* @see User
*/
protected $bsaber;
/** @var string The person that uploaded the model */
protected $uploader;
/** @var string Status of the model, example: 'unapproved', 'pending', or 'approved' */
protected $status;
/**
* @var boolean The approval status of the model
* @deprecated Replaced by {@see Model::$status}
* @see Model::$status
*/
protected $approved;
/** @var string The description of the model */
protected $description; //renamed from 'comments' for clarity
/** @var string|null The discord userID of the uploader */
protected $discordId;
/** @var string The discord username and discriminator of the uploader */
protected $discord;
/** @var string|null The id of the variation this model is in */
protected $variationId;
/** @var string|null The title of the variation this model is in */
protected $variationTitle;
/** @var string[]|null A list with all of the models in this variation */
protected $variationList;
/** @var int Contains the total amount of pages on read */
protected $totalRows;
/** @var int Contains the curent page on a model read */
protected $currentPage;
/** @var string[]|null Contains the comments */
protected $comments;
/** @var string Contains the device platform name */
protected $platform;
/** @var string Contains the file format */
protected $format;
protected $upvotes = 0;
protected $downvotes = 0;
protected $downloads = 0;
protected $updatedAt;
//Getters
public function getId() {
return $this->id;
}
public function getType() {
return $this->type;
}
public function getName() {
return $this->name;
}
public function getAuthor() {
return $this->author;
}
public function getFilename() {
return $this->filename;
}
public function getImage() {
// clearstatcache(true);
$dir = WEBROOT . '/files/' . $this->type . '/' . $this->id . '/';
$path = ROOT . '/files/' . $this->type . '/' . $this->id . '/';
if (file_exists($path . 'image.svg')) {
$output = $dir . 'image.svg';
} else if (file_exists($path . 'image.gif')) {
$output = $dir . 'image.gif';
} else if (file_exists($path . 'image.jpg')) {
$output = $dir . 'image.jpg';
} else if (file_exists($path . 'image.png')) {
$output = $dir . 'image.png';
} else {
if (file_exists($path . 'original.png')) {
$output = $dir . 'original.png';
} else if (file_exists($path . 'original.jpg')) {
$output = $dir . 'original.jpg';
} else if (file_exists($path . 'original.gif')) {
$output = $dir . 'original.gif';
} else {
$output = false;
}
}
return $output;
}
public function getVideo() {
// clearstatcache(true);
$dir = WEBROOT . '/files/' . $this->type . '/' . $this->id . '/';
$path = ROOT . '/files/' . $this->type . '/' . $this->id . '/';
if (file_exists($path . 'video.webm')) {
$output[] = $dir . 'video.webm';
}
if (file_exists($path . 'video.mp4')) {
$output[] = $dir . 'video.mp4';
}
if (!isset($output)) {
$output = false;
}
return $output;
}
public function getTags() {
return $this->tags;
}
public function getHash() {
return $this->hash;
}
public function getBsaber() {
return (isset($this->bsaber) && !empty($this->bsaber)) ? $this->bsaber : "";
}
public function getUploader() {
return $this->uploader;
}
public function getApproved() {
return $this->approved;
}
public function getStatus() {
return $this->status;
}
public function getDescription() {
return $this->description;
}
public function getDiscordId() {
return $this->discordId;
}
public function getDiscord() {
return $this->discord;
}
public function getVariationId() {
return $this->variationId;
}
public function getVariationTitle() {
return $this->variationTitle;
}
public function getLink() {
$link = str_replace("?", "%3F", WEBROOT . '/files/' . $this->type . "/" . $this->id . "/" . rawurlencode($this->filename));
return $link;
}
public function getTotalRows() {
return $this->totalRows;
}
public function getCurrentPage() {
return $this->currentPage;
}
public function getComments() {
return $this->comments;
}
public function getUpvotes() {
return $this->upvotes;
}
public function getDownvotes() {
return $this->downvotes;
}
public function getDownloads() {
return $this->downloads;
}
//Setters
public function setTags($tags) {
$this->tags = $tags;
}
public function setBsaber($bsaber) {
$this->bsaber = $bsaber;
}
public function setDescription($description) {
$this->description = $description;
}
//Methods
function __construct($data = []) {
if (count($data) > 0) {
$this->id = $data['id'];
$this->name = $data['name'];
$this->author = $data['author'];
$this->filename = $data['filename'];
// $this->image = $data['image'];
$this->tags = $data['tags'];
$this->type = $data['type'];
$this->status = $data['status'];
$this->discord = $data['discord'];
$this->discordId = $data['discordId'];
if (isset($data['bsaber'])) {
$this->bsaber = $data['bsaber'];
}
if (isset($data['description'])) {
$this->description = $data['description'];
}
if (isset($data['hash'])) {
$this->hash = $data['hash'];
}
if (isset($data['totalRows'])) {
$this->totalRows = $data['totalRows'];
}
if (isset($data['variationId'])) {
$this->variationId = $data['variationId'];
}
if (isset($data['variationTitle'])) {
$this->variationTitle = $data['variationTitle'];
}
if (isset($data['upvotes'])) {
$this->upvotes = $data['upvotes'];
}
if (isset($data['downvotes'])) {
$this->downvotes = $data['downvotes'];
}
return $this;
}
return;
}
/** @return boolean Returns true if the $status of this model is 'approved' */
public function isApproved() {
return ($this->status == 'approved' || $this->approved == TRUE);
}
/** @return boolean Returns true if the specified user is the author of this model */
public function isAuthor($userId) {
return ($userId == $this->discordId);
}
public function hasVideoThumbnail() {
$output = false;
$path = ROOT . '/files/' . $this->type . '/' . $this->id . '/';
if (file_exists($path . 'video.mp4') || file_exists($path . 'video.webm')) {
$output = true;
}
return $output;
}
/**
* Get the previous model in a variation list.
*
* @param boolean $next If true the function will search the next index.
* @return int|false The id of the requested model if found, false otherwise.
*/
public function getPrevious($next = false) {
$output = false;
if (!empty($this->variationList)) {
$index = array_search($this->id, $this->variationList);
if ($next == true) {
$index++;
} else {
$index--;
}
if (array_key_exists($index, $this->variationList)) {
$output = $this->variationList[$index];
}
}
return $output;
}
/**
* Get the next model in a variation list.
*
* @return int|false The id of the requested model if found, false otherwise.
*/
public function getNext() {
return $this->getPrevious(true);
}
/**
* Create
*
* Creates a new model
*
* @param string $id The id of the model.
* @param int $file The index of the file in the $_FILES superglobal
* @param int $img The index of the image in the $_FILES superglobal
* @param int|null $variationId The id of the variation group this model belongs to
* @return type
*/
public function create($model, $variationId = '') {
global $currentUser;
global $helper;
FishyUtils::getInstance()->log("Creating model");
$url = WEBROOT . '/';
if (!$currentUser->isVerified()) {
trigger_error('You must be logged in and have a accepted the terms of service to upload');
failed($url, 'You must be logged in and have a accepted the terms of service to upload');
die();
}
// $getInfo = $_POST;
$this->id = $model['model']['id'];
$variantTitle = $model['model']['variationTitle'];
if (empty($this->id)) {
trigger_error('ID isn\'t set', E_USER_ERROR);
failed($url, '');
die();
}
$this->platform = $model['upload']['platform'];
$this->format = $model['upload']['format'];
//File Setup
/*$tmp_name = $_FILES['file']['tmp_name'][$file];
$this->hash = md5_file($tmp_name);
//put 2>&1 at the end of a exec call to get the error code
exec("python getInfo.py " . $_FILES['file']['tmp_name'][$file] . "", $info);
$this->type = $info[0];
$this->name = $info[1];
$this->author = $info[2];*/
$this->hash = $model['model']['hash'];
$this->type = $model['model']['type'];
$this->name = $model['model']['name'];
$this->author = $model['model']['author'];
// $tmp_image_name = $_FILES['image']['tmp_name'][$img];
// $image_Extension = pathinfo($_FILES['image']['name'][$img], PATHINFO_EXTENSION);
$tags = "";
if (isset($model['model']['tags']) && !empty($model['model']['tags'])) {
foreach ($model['model']['tags'] as $tag) {
if ($tag != "") {
$tags .= $tag . ',';
}
}
}
//Checks if any of the models are missing anything
/*if (pathinfo($model['model']['author'], PATHINFO_FILENAME) != pathinfo($_FILES['file']['name'][$file], PATHINFO_FILENAME)) {
trigger_error('Models and images don\'t match', E_USER_ERROR);
failed($url, 'Models and images don\'t match');
die();
}*/
switch ($this->type) {
case "avatar":
$ext = 'avatar';
break;
case "saber":
$ext = 'saber';
break;
case "platform":
$ext = 'plat';
break;
case "bloq":
case "note":
$ext = 'bloq';
break;
case "trail":
$ext = 'trail';
break;
case "sign":
$ext = 'sign';
break;
case "misc":
$ext = 'misc';
break;
default:
trigger_error('Upload format is invalid', E_USER_ERROR);
failed($url, 'Upload format is invalid');
die();
}
if ($this->platform == self::PLATFORM_QUEST && $this->format == self::FORMAT_BMBF) {
$ext = 'zip';
}
if (!isset($this->hash) || empty($this->hash)) {
trigger_error('Hash isn\'t set', E_USER_ERROR);
failed($url, '');
die();
}
if (!isset($this->name) || empty($this->name)) {
trigger_error('Name isn\'t set', E_USER_ERROR);
failed($url, '');
die();
}
if (!isset($this->author) || empty($this->author)) {
trigger_error('Author isn\'t set', E_USER_ERROR);
failed($url, '');
die();
}
if ($model['license'] == 'INVALID') {
trigger_error('License is invalid', E_USER_ERROR);
failed($url, 'License is invalid');
die();
}
//Image Validation
$imageClass = new Image($model['model']['image']['file'], $model['model']['image']['advanced'], $model['model']['image']['extension']);
if (!$imageClass->isValid()) {
trigger_error('Uploaded image is invalid');
// failed($url, 'Image is invalid');
die();
}
/*$imageSize = getimagesize($model['model']['image']['file']);
$width = $imageSize[0];
$height = $imageSize[1];
if ($width != $height) {
trigger_error('Image is not 1:1 aspect ratio', E_USER_ERROR);
failed($url, 'Image is not 1:1 aspect ratio');
die();
}
if ($width < MINIMAGESIZE) {
trigger_error('Image is smaller than ' . MINIMAGESIZE, E_USER_ERROR);
failed($url, 'Image is smaller than ' . MINIMAGESIZE);
die();
}
switch ($model['model']['image']['extension']) {
case "jpg":
break;
case "png":
break;
case "gif":
break;
default:
trigger_error('Image format is invalid', E_USER_ERROR);
failed($url, 'Image format is invalid');
die();
}*/
$dir = '../files/' . $this->type . '/' . $this->id . '/';
mkdir($dir);
FishyUtils::getInstance()->log("Created directory named: $dir");
$image_Extension = $imageClass->getFiletype();
//File Upload
$fileName = str_replace("/", "_", $this->name);
if ($model['model']['image']['advanced'] == true) {
if (file_put_contents($dir . $fileName . '.' . $ext, $model['model']['file']) == false) {
$helper->deleteFiles($dir);
trigger_error('Uploaded file failed to move');
failed($url, 'Uploaded file failed to move');
die();
}
} else {
if (!move_uploaded_file($model['model']['file'], $dir . $fileName . '.' . $ext)) {
$helper->deleteFiles($dir);
trigger_error('Uploaded file failed to move');
failed($url, 'Uploaded file failed to move');
die();
}
}
if ($model['model']['image']['advanced'] == true) {
if (!imagepng($imageClass->getImage(), $dir . 'original.' . $image_Extension, 0)) {
$helper->deleteFiles($dir);
trigger_error('Uploaded image failed to move');
failed($url, 'Uploaded image failed to moves');
die();
}
} else {
if (!move_uploaded_file($model['model']['image']['file'], $dir . 'original.' . $image_Extension)) {
$helper->deleteFiles($dir);
trigger_error('Uploaded image failed to move');
failed($url, 'Uploaded image failed to move');
die();
}
}
if (!empty($model['embed'])) {
$fp = fopen($dir . 'embed.json', 'w');
fwrite($fp, json_encode($model['embed'], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
fclose($fp);
}
//Database Upload
$id = $this->id;
$type = $this->type;
$this->name = $this->name;
$this->author = $this->author;
$fileName = $fileName . '.' . $ext;
$this->hash = $this->hash;
$this->platform = $this->platform;
$this->format = $this->format;
$imageName = $dir . 'original.' . $image_Extension;
// $this->bsaber = pg_escape_literal($_POST['bsaber']);
$this->description = strip_tags($model['model']['description']);
$tags = '{' . substr($tags, 0, -1) . '}';
if (!empty($variationId)) {
$this->variationId = $variationId;
} else {
$this->variationId = '';
}
if ($currentUser->isTrusted()) {
$status = APPROVED;
} else {
$status = UNAPPROVED;
}
//Discord
$this->discordId = $currentUser->getDiscordId();
$this->discord = $currentUser->getUsername() . '#' . $currentUser->getDiscriminator();
$statement = dbConnection::getInstance()->prepare('INSERT INTO models ('
. 'id, '
. 'type, '
. 'name, '
. 'author, '
. 'filename, '
. 'tags, '
. 'hash, '
. 'comments, '
. 'discordid, '
. 'discord, '
. 'status, '
. 'platform, '
. 'format, '
. 'image, '
. 'variationid) '
. 'VALUES ('
. ':id, '
. ':type, '
. ':name, '
. ':author, '
. ':filename, '
. ':tags, '
. ':hash, '
. ':comments, '
. ':discordid, '
. ':discord, '
. ':status, '
. ':platform, '
. ':format, '
. ':image, '
. "NULLIF(:variationid,'')::integer)");
$statement->bindParam(':id', $id);
$statement->bindParam(':type', $type);
$statement->bindParam(':name', $this->name);
$statement->bindParam(':author', $this->author);
$statement->bindParam(':filename', $fileName);
$statement->bindParam(':tags', $tags);
$statement->bindParam(':hash', $this->hash);
$statement->bindParam(':comments', $this->description);
$statement->bindParam(':discordid', $this->discordId);
$statement->bindParam(':discord', $this->discord);
$statement->bindParam(':status', $status);
$statement->bindParam(':platform', $this->platform);
$statement->bindParam(':format', $this->format);
$statement->bindValue(':image', $imageName);
$statement->bindParam(':variationid', $this->variationId);
$statement->execute();
$isPrimary = true;
if (!empty($variationId)) {
if ($variationId == $this->id) {
$variantTitle = (!empty($variantTitle)) ? $variantTitle : 'Unnamed Variation';
$variant = '{' . $this->id . '}';
$statement = dbConnection::getInstance()->prepare('INSERT INTO variations (variationid, modelids, title) '
. 'VALUES (:variationid, :variant, :title)');
$statement->bindParam(':variationid', $this->variationId);
$statement->bindParam(':variant', $variant);
$statement->bindParam(':title', $variantTitle);
} else {
$variant = $this->id;
$statement = dbConnection::getInstance()->prepare('UPDATE variations '
. 'SET modelids = array_append(modelids, :variant) '
. 'WHERE variationid = :variationid');
$statement->bindParam(':variationid', $this->variationId);
$statement->bindParam(':variant', $variant);
$isPrimary = false;
}
$statement->execute();
}
if ($isPrimary && !$currentUser->isTrusted()) {
$admins = $currentUser->readAdmins();
$notification = new notification();
$notification->setLink(WEBROOT . '/' . getSuperType($this->type) . '?id=' . $this->id);
$notification->create('A model is ready for approval', 'danger');
$notification->addRelations($admins);
}
/*$thumbnail = str_replace("image", "thumbnail", $imageName);
$oldFile = $imageName;
$newFile = $thumbnail;
switch ($imageName) {
case "image.png":
case "image.jpg":
shell_exec("convert $oldFile -resize 64x64 $newFile");
break;
case "image.gif":
shell_exec("convert $oldFile -layers Coalesce -resize 64x64 -layers Optimize $newFile");
break;
}
unset($oldFile);
unset($newFile);*/
/*$user = new User();
if (!$user->exists($this->discordId)) {
$user->create($this->discordId, $discordUsername, $discordDiscriminator);
}*/
//Execute background script for image optimization
$path = ROOT . '/files/' . $this->type . '/' . $this->id . '/';
$imageName = 'original.' . $image_Extension;
execInBackground(PHP_CLI . " " . ROOT . "/Upload/optimization.php " . $path . ' ' . $imageName);
return $this->type;
}
public function readSingle($id, $useage = "model") {
require_once ROOT . '/discordOAuth.php';
require_once ROOT . '/resources/filter.php';
global $sort;
global $sort_dir;
global $limit;
global $total_rows;
global $filter;
global $page;
global $type;
global $status;
global $offset;
global $currentUser;
FishyUtils::getInstance()->log("Reading single model with parameters: $id, $useage");
// $directID = get('id');
$directID = $id;
if ($useage == 'Manage') {
if (isset($_POST['modelType'])) {
$type = filter_var($_POST['modelType'], FILTER_SANITIZE_EMAIL);
} else {
$type = 'all';
}
} else if ($useage == 'adminUpdate' || $useage == 'update') {
// $directID = $_POST['action'];
}
if ($type == 'all') {
$type = '(avatar|saber|platform|bloq|trail|sign|misc)';
}
if (isset($_POST['page']) && !empty($_POST['page'])) {
$current_page = filter_var($_POST['page'], FILTER_SANITIZE_NUMBER_INT);
} else {
$current_page = 1;
}
if (isset($page) && !empty($page)) {
$current_page = $page;
} else {
$page = 1;
}
$offset = (($current_page - 1) * $limit);
if ($currentUser->isVerified()) {
$discordID = $currentUser->getDiscordId();
} else {
$discordID = 0;
}
$devicePlatform = getDevicePlatform();
$this->id = $directID;
if ($currentUser->isVerified() && $currentUser->isAdmin()) {
switch ($useage) {
case 'Manage':
$where = "";
break;
default:
$where = "";
break;
}
} else {
$where = "AND
(((m.approved='true' OR m.status='approved') OR m.discordid='-1') OR
((m.approved='false' OR m.status!='approved') AND
m.discordid='$discordID'))";
}
$statement = dbConnection::getInstance()->prepare('SELECT '
. 'm.name, '
. 'm.author, '
. 'm.filename, '
. 'm.image, '
. 'm.tags, '
. 'm.hash, '
. 'm.bsaber, '
. 'm.uploader, '
. 'm.approved, '
. 'm.comments, '
. 'm.discordid, '
. 'm.discord, '
. 'm.status, '
. 'm.type, '
. 'm.variationid, '
. 'm.updatedat, '
. 'd.amount AS downloads, '
. '(SELECT COUNT(v.modelid) FROM votes as v WHERE v.modelid = m.id AND v.isupvote = true) AS upvotes, '
. '(SELECT COUNT(v.modelid) FROM votes as v WHERE v.modelid = m.id AND v.isupvote = false) AS downvotes '
. 'FROM models AS m '
. 'LEFT JOIN downloads AS d ON m.id = d.modelid '
. "WHERE m.id = :directid $where");
$statement->bindParam(':directid', $directID);
$statement->execute();
while ($row = $statement->fetch()) {
$this->name = $row['name'];
$this->author = $row['author'];
$this->filename = $row['filename'];
// $this->image = $row[3];
$this->tags = explode(',', str_replace("\"", "", substr($row['tags'], 1, -1)));
$this->hash = $row['hash'];
$this->bsaber = $row['bsaber'];
$this->uploader = $row['uploader']; //unused
$this->approved = ($row['approved'] == 't') ? true : false;
$this->description = $row['comments'];
$this->discordId = $row['discordid'];
$this->discord = $row['discord'];
$this->type = $row['type'];
$this->variationId = $row['variationid'];
$this->downloads = (!empty($row['downloads'])) ? $row['downloads'] : 0;
$this->upvotes = (!empty($row['upvotes'])) ? $row['upvotes'] : 0;
$this->downvotes = (!empty($row['downvotes'])) ? $row['downvotes'] : 0;
$this->updatedAt = $row['updatedat'];
if (!empty($row['status'])) {
$this->status = $row['status'];
} else {
if ($this->approved) {
$this->status = APPROVED;
} else {
$this->status = UNAPPROVED;
}
}
if (!empty($this->variationId)) {
$variation = $this->variationId;
$statement = dbConnection::getInstance()->prepare('SELECT modelids, title '
. 'FROM variations '
. 'WHERE modelids @> ARRAY[CAST(:variation AS bigint)]');
$statement->bindParam(':variation', $variation);
$statement->execute();
while ($row = $statement->fetch()) {
$this->variationList = explode(',', str_replace("\"", "", substr($row['modelids'], 1, -1)));
$this->variationTitle = $row['title'];
}
}
}
$this->image = WEBROOT . '/files/' . $this->type . '/' . $this->id . '/';
FishyUtils::getInstance()->log("Finished reading single model with parameters: $id, $useage");
return true;
}
public function readMultiple($mode = 'print', $useage = "model") {
require_once ROOT . '/discordOAuth.php';
require_once ROOT . '/resources/filter.php';
global $sort;
global $sort_dir;
global $limit;
global $total_rows;
global $filter;
// global $page;
global $type;
global $status;
global $offset;
global $currentUser;
FishyUtils::getInstance()->log("Reading multiple models with parameters: $mode, $useage");
if ($useage == 'Manage') {
if (isset($_POST['modelType'])) {
$type = filter_var($_POST['modelType'], FILTER_SANITIZE_STRING);
} else {
$type = 'all';
}
}
if (isset($_POST['page']) && !empty($_POST['page'])) {
$current_page = filter_var($_POST['page'], FILTER_SANITIZE_NUMBER_INT);
} else {
$current_page = 1;
}
/* if (isset($page) && !empty($page)) {
$current_page = $page;
} else {
$page = 1;
} */
$offset = (($current_page - 1) * $limit);
if ($currentUser->isVerified()) {
$discordID = $currentUser->getDiscordId();
} else {
$discordID = 0;
}
if ($useage == 'Profile') {
if (isset($_GET['user']) && !empty($_GET['user'])) {
$userId = $_GET['user'];
} else if (isset($_POST['user']) && !empty($_POST['user'])) {
$userId = $_POST['user'];
}
$type = 'all';
}
if ($type == 'all') {
$type = '(avatar|saber|platform|bloq|trail|sign|misc)';
}
if (getDevicePlatform() == 'quest') {
$platform = "m.platform LIKE 'quest' AND ";
} else {
$platform = "(m.platform NOT LIKE 'quest' OR m.platform IS NULL) AND ";
}
$where = "((m.approved='true' OR m.status='approved') OR
((m.approved='false' OR m.status!='approved') AND
m.discordid='$discordID')) AND";
$manager = false;
if ($useage == 'Variation') {
$variation = $_GET['variation'];
$variation = "m.variationid = CAST($variation AS bigint) AND ";
} else {
$variation = ' (m.variationid = m.id OR m.variationid IS NULL) AND ';
}
switch ($useage) {
case 'Manage':
if (!$currentUser->isVerified() || !$currentUser->isAdmin()) {
break;
}
$where = "m.status='$status' AND";
$manager = true;
break;
case 'Profile':
$where = "m.discordid = CAST($userId AS bigint) AND" . $where;
break;
default:
if ($currentUser->isVerified() && $currentUser->isAdmin()) {
$where = "";
}
break;
}
// $_filters = filter($filter);
$_filtersAr = filter($filter);
$_filters = $_filtersAr['query'];
if (!isset($type)) {
$type = strtolower(getUrlType());
$type = (substr($type, 0, strlen($type) - 1) == 's') ? $type : substr($type, 0, -1);
}
if ($manager) {
$statement = dbConnection::getInstance()->prepare('SELECT '
. 'm.id, '
. 'm.name, '
. 'm.author, '
. 'm.filename, '
. 'm.image, '
. 'm.tags, '
. 'count(m.id) OVER() AS full_count, '
. 'm.status, '
. 'm.approved, '
. 'm.discord, '
. 'm.discordid, '
. 'm.type, '
. 'm.variationid, '
. 'm.bsaber, '
. 'm.comments AS description, '
. 'm.hash, '
. '(SELECT COUNT(v.modelid) FROM votes as v WHERE v.modelid = m.id AND v.isupvote = true) AS votes, '
. '(SELECT COUNT(v.modelid) FROM votes as v WHERE v.modelid = m.id AND v.isupvote = false) AS downvotes '
. 'FROM models AS m '
. 'LEFT JOIN variations AS v ON m.variationid = v.variationid '
. 'LEFT JOIN users AS u ON m.discordid = u.discordid '
. "WHERE $where "
. "m.type ~ :type $_filters "
. "ORDER BY $sort $sort_dir "
. "LIMIT $limit "
. "OFFSET $offset");
// $statement->bindParam(':type', $type);
// $statement->bindParam(':sort', $sort);
// $statement->bindParam(':sortdir', $sort_dir);
// $statement->bindParam(':limit', $limit);
// $statement->bindParam(':offset', $offset);
$_filtersAr['params'][':type'] = $type;
} else {
$statement = dbConnection::getInstance()->prepare('SELECT '
. 'm.id, '
. 'm.name, '
. 'm.author, '
. 'm.filename, '
. 'm.image, '
. 'm.tags, '
. 'count(m.id) OVER() AS full_count, '
. 'm.status, '
. 'm.approved, '
. 'm.discord, '
. 'm.discordid, '
. 'm.type, '
. 'm.variationid, '
. 'v.title AS variationtitle, '
. '(SELECT COUNT(v.modelid) FROM votes as v WHERE v.modelid = m.id AND v.isupvote = true) AS votes, '
. '(SELECT COUNT(v.modelid) FROM votes as v WHERE v.modelid = m.id AND v.isupvote = false) AS downvotes '
. 'FROM models AS m '
. 'LEFT JOIN variations AS v ON m.variationid = v.variationid '
. 'LEFT JOIN users AS u ON m.discordid = u.discordid '
. "WHERE $variation $where "
. "$platform "
. "m.type ~ :type $_filters "
. "ORDER BY $sort $sort_dir "
. "LIMIT $limit "
. "OFFSET $offset");
// $statement->bindParam(':type', $type);
$_filtersAr['params'][':type'] = $type;
}
$statement->execute($_filtersAr['params']);
while ($row = $statement->fetch()) {
$modelTemp['id'] = $row['id'];
$modelTemp['name'] = $row['name'];
$modelTemp['author'] = $row['author'];
$modelTemp['filename'] = $row['filename'];
// $modelTemp['image'] = $row['image'];
$modelTemp['tags'] = explode(',', str_replace("\"", "", substr($row['tags'], 1, -1)));
$modelTemp['type'] = $row['type'];
$isStatusApproved = false;
if (!empty($row['status'])) {
$modelTemp['status'] = $row['status'];
if ($row['status'] == APPROVED || $row['status'] == VERIFIED) {
$isStatusApproved = true;
}
}
if (!$isStatusApproved && !empty($row['approved'])) {
$modelTemp['status'] = ($row['approved'] == 't') ? APPROVED : UNAPPROVED;
}
$modelTemp['discord'] = $row['discord'];
$modelTemp['discordId'] = $row['discordid'];
$modelTemp['totalRows'] = $row['full_count'];
$modelTemp['currentPage'] = $current_page;
if (isset($row['variationid'])) {
$modelTemp['variationId'] = $row['variationid'];
}
if (isset($row['variationtitle'])) {