Issues (563)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  Header Injection
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

app/Models/ProblemModel.php (1 issue)

1
<?php
2
3
namespace App\Models;
4
5
use GrahamCampbell\Markdown\Facades\Markdown;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\DB;
8
use App\Models\Submission\SubmissionModel;
9
use Illuminate\Support\Str;
10
use App\Models\Eloquent\OJ;
11
use Cache;
12
13
class ProblemModel extends Model
14
{
15
    protected $table='problem';
16
    protected $primaryKey='pid';
17
    const UPDATED_AT="update_date";
18
19
    public function detail($pcode, $cid=null)
20
    {
21
        $prob_detail=DB::table($this->table)->where("pcode", $pcode)->first();
22
        // [Depreciated] Joint Query was depreciated here for code maintenance reasons
23
        if (!is_null($prob_detail)) {
24
            if ($prob_detail["force_raw"]) {
25
                $prob_detail["parsed"]=[
26
                    "description"=>$prob_detail["description"],
27
                    "input"=>$prob_detail["input"],
28
                    "output"=>$prob_detail["output"],
29
                    "note"=>$prob_detail["note"],
30
                    "file"=>$prob_detail["file"]
31
                ];
32
            } elseif ($prob_detail["markdown"]) {
33
                $prob_detail["parsed"]=[
34
                    "description"=>clean(convertMarkdownToHtml($prob_detail["description"])),
35
                    "input"=>clean(convertMarkdownToHtml($prob_detail["input"])),
36
                    "output"=>clean(convertMarkdownToHtml($prob_detail["output"])),
37
                    "note"=>clean(convertMarkdownToHtml($prob_detail["note"])),
38
                    "file"=>clean(convertMarkdownToHtml($prob_detail["file"]))
39
                ];
40
            } else {
41
                $prob_detail["parsed"]=[
42
                    "description"=>$prob_detail["description"],
43
                    "input"=>$prob_detail["input"],
44
                    "output"=>$prob_detail["output"],
45
                    "note"=>$prob_detail["note"],
46
                    "file"=>$prob_detail["file"]
47
                ];
48
            }
49
            $prob_detail["pdf"]=false;
50
            $prob_detail["viewerShow"]=false;
51
            $prob_detail["file_ext"]=null;
52
            if ($prob_detail['file'] && !blank($prob_detail['file_url'])) {
53
                $prob_detail["file_ext"]=explode('.', basename($prob_detail['file_url']));
54
                $prob_detail["file_ext"]=end($prob_detail["file_ext"]);
55
                $prob_detail["pdf"]=Str::is("*.pdf", basename($prob_detail['file_url']));
56
                $prob_detail["viewerShow"]=blank($prob_detail["parsed"]["description"]) &&
57
                                            blank($prob_detail["parsed"]["input"]) &&
58
                                            blank($prob_detail["parsed"]["output"]) &&
59
                                            blank($prob_detail["parsed"]["note"]);
60
            }
61
            $prob_detail["update_date"]=date_format(date_create($prob_detail["update_date"]), 'm/d/Y H:i:s');
62
            $prob_detail["oj_detail"]=DB::table("oj")->where("oid", $prob_detail["OJ"])->first();
63
            $prob_detail["samples"]=DB::table("problem_sample")->where("pid", $prob_detail["pid"])->get()->all();
64
            $prob_detail["tags"]=DB::table("problem_tag")->where("pid", $prob_detail["pid"])->get()->all();
65
            if ($cid) {
66
                $frozen_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]);
67
                $prob_stat=DB::table("submission")->select(
68
                    DB::raw("count(sid) as submission_count"),
69
                    DB::raw("sum(verdict='accepted') as passed_count"),
70
                    DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
71
                )->where([
72
                    "pid"=>$prob_detail["pid"],
73
                    "cid"=>$cid,
74
                ])->where("submission_date", "<", $frozen_time)->first();
75
                $prob_detail['vcid']=DB::table("contest")->where(["cid"=>$cid])->select("vcid")->first()['vcid'];
76
                $prob_detail["points"]=DB::table("contest_problem")->where(["cid"=>$cid, "pid"=>$prob_detail["pid"]])->select("points")->first()["points"];
77
            } else {
78
                $prob_stat=DB::table("submission")->select(
79
                    DB::raw("count(sid) as submission_count"),
80
                    DB::raw("sum(verdict='accepted') as passed_count"),
81
                    DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
82
                )->where(["pid"=>$prob_detail["pid"]])->first();
83
                $prob_detail['vcid']=null;
84
                $prob_detail["points"]=0;
85
            }
86
            if ($prob_stat["submission_count"]==0) {
87
                $prob_detail["submission_count"]=0;
88
                $prob_detail["passed_count"]=0;
89
                $prob_detail["ac_rate"]=0;
90
            } else {
91
                $prob_detail["submission_count"]=$prob_stat["submission_count"];
92
                $prob_detail["passed_count"]=$prob_stat["passed_count"];
93
                $prob_detail["ac_rate"]=round($prob_stat["ac_rate"], 2);
94
            }
95
        }
96
        return $prob_detail;
97
    }
98
99
    public function basic($pid)
100
    {
101
        return DB::table($this->table)->where("pid", $pid)->first();
102
    }
103
104
    public function tags()
105
    {
106
        return DB::table("problem_tag")->groupBy('tag')->select("tag", DB::raw('count(*) as tag_count'))->orderBy('tag_count', 'desc')->limit(12)->get()->all();
107
    }
108
109
    public function ojs()
110
    {
111
        return DB::table("oj")->where("status", 1)->orderBy('oid', 'asc')->get()->all();
112
    }
113
114
    public function ojdetail($oid)
115
    {
116
        return DB::table("oj")->where('oid', $oid)->first();
117
    }
118
119
    public function solutionList($pid, $uid=null)
120
    {
121
        if (is_null($uid)) {
122
            $details=DB::table("problem_solution")->join(
123
                "users",
124
                "id",
125
                "=",
126
                "problem_solution.uid"
127
            )->where([
128
                'problem_solution.pid'=>$pid,
129
                'problem_solution.audit'=>1
130
            ])->orderBy(
131
                "problem_solution.votes",
132
                "desc"
133
            )->get()->all();
134
        } else {
135
            $votes=DB::table("problem_solution_vote")->where([
136
                "uid"=>$uid
137
            ])->get()->all();
138
            foreach ($votes as $v) {
139
                $userVotes[$v["psoid"]]=$v["type"];
140
            }
141
            $details=DB::table("problem_solution")->join(
142
                "users",
143
                "id",
144
                "=",
145
                "problem_solution.uid"
146
            )->where([
147
                'problem_solution.pid'=>$pid,
148
                'problem_solution.audit'=>1
149
            ])->select([
150
                "problem_solution.psoid as psoid",
151
                "problem_solution.uid as uid",
152
                "problem_solution.pid as pid",
153
                "problem_solution.content as content",
154
                "problem_solution.audit as audit",
155
                "problem_solution.votes as votes",
156
                "problem_solution.created_at as created_at",
157
                "problem_solution.updated_at as updated_at",
158
                "avatar",
159
                "name"
160
            ])->orderBy("problem_solution.votes", "desc")->get()->all();
161
            foreach ($details as &$d) {
162
                $d["type"]=isset($userVotes[$d["psoid"]]) ? $userVotes[$d["psoid"]] : null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userVotes seems to be defined by a foreach iteration on line 138. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
163
            }
164
            unset($d);
165
        }
166
        foreach ($details as &$d) {
167
            $d["content_parsed"]=clean(convertMarkdownToHtml($d["content"]));
168
        }
169
        return $details;
170
    }
171
172
    public function solution($pid, $uid)
173
    {
174
        $details=DB::table("problem_solution")->join("users", "id", "=", "uid")->where(['pid'=>$pid, 'uid'=>$uid])->first();
175
        return $details;
176
    }
177
178
    public function addSolution($pid, $uid, $content)
179
    {
180
        $details=DB::table("problem_solution")->where(['pid'=>$pid, 'uid'=>$uid])->first();
181
        if (empty($details)) {
182
            DB::table("problem_solution")->insert([
183
                "uid"=>$uid,
184
                "pid"=>$pid,
185
                "content"=>$content,
186
                "votes"=>0,
187
                "audit"=>$this->inteliAudit($uid, $content),
188
                "created_at"=>date("Y-m-d H:i:s"),
189
                "updated_at"=>date("Y-m-d H:i:s"),
190
            ]);
191
            return true;
192
        }
193
        return false;
194
    }
195
196
    private function inteliAudit($uid, $content)
197
    {
198
        if (strpos($content, '```')!==false) {
199
            $userSolutionHistory=DB::table("problem_solution")->where(['uid'=>$uid])->orderByDesc('updated_at')->first();
200
            if (!empty($userSolutionHistory) && $userSolutionHistory["audit"]==1) {
201
                return 1;
202
            }
203
        }
204
        return 0;
205
    }
206
207
    public function voteSolution($psoid, $uid, $type)
208
    {
209
        $val=$type ? 1 : -1;
210
        $details=DB::table("problem_solution")->where(['psoid'=>$psoid])->first();
211
        if (empty($details)) {
212
            return ["ret"=>false];
213
        }
214
215
        $userVote=DB::table("problem_solution_vote")->where(['uid'=>$uid, "psoid"=>$psoid])->first();
216
217
        if (!empty($userVote)) {
218
            DB::table("problem_solution_vote")->where(['uid'=>$uid, "psoid"=>$psoid])->delete();
219
            if ($userVote["type"]==$type) {
220
                DB::table("problem_solution")->where([
221
                    'psoid'=>$psoid
222
                ])->update([
223
                    "votes"=>$details["votes"]+($userVote["type"]==1 ?-1 : 1),
224
                ]);
225
                return ["ret"=>true, "votes"=>$details["votes"]+($userVote["type"]==1 ?-1 : 1), "select"=>-1]; //disvote
226
            } elseif ($userVote["type"]==1) {
227
                $val--;
228
            } else {
229
                $val++;
230
            }
231
        }
232
233
        DB::table("problem_solution")->where([
234
            'psoid'=>$psoid
235
        ])->update([
236
            "votes"=>$details["votes"]+$val,
237
        ]);
238
239
        DB::table("problem_solution_vote")->insert([
240
            "uid"=>$uid,
241
            "psoid"=>$psoid,
242
            "type"=>$type,
243
        ]);
244
245
        return ["ret"=>true, "votes"=>$details["votes"]+$val, "select"=>$type];
246
    }
247
248
    public function removeSolution($psoid, $uid)
249
    {
250
        if (empty(DB::table("problem_solution")->where(['psoid'=>$psoid, 'uid'=>$uid])->first())) {
251
            return false;
252
        }
253
        DB::table("problem_solution")->where(['psoid'=>$psoid, 'uid'=>$uid])->delete();
254
        return true;
255
    }
256
257
    public function updateSolution($psoid, $uid, $content)
258
    {
259
        if (empty(DB::table("problem_solution")->where(['psoid'=>$psoid, 'uid'=>$uid])->first())) {
260
            return false;
261
        }
262
        DB::table("problem_solution")->where(['psoid'=>$psoid, 'uid'=>$uid])->update([
263
            "content"=>$content,
264
            "audit"=>$this->inteliAudit($uid, $content),
265
            "updated_at"=>date("Y-m-d H:i:s"),
266
        ]);
267
        return true;
268
    }
269
270
    public function isBlocked($pid, $cid=null)
271
    {
272
        $conflictContests=DB::table("contest")
273
                            ->join("contest_problem", "contest.cid", "=", "contest_problem.cid")
274
                            ->where("end_time", ">", date("Y-m-d H:i:s"))
275
                            ->where(["verified"=>1, "pid"=>$pid])
276
                            ->select(["contest_problem.cid as cid"])
277
                            ->get()
278
                            ->all();
279
        if (empty($conflictContests)) {
280
            return false;
281
        }
282
        foreach ($conflictContests as $c) {
283
            if ($cid==$c["cid"]) {
284
                return false;
285
            }
286
        }
287
        // header("HTTP/1.1 403 Forbidden");
288
        // exit();
289
        return true;
290
    }
291
292
    public function list($filter, $uid=null)
293
    {
294
        // $prob_list = DB::table($this->table)->select("pid","pcode","title")->get()->all(); // return a array
295
        $submissionModel=new SubmissionModel();
296
        $preQuery=DB::table($this->table)->where('hide', '=', 0);
297
        if ($filter['oj']) {
298
            $OJ=OJ::find($filter['oj']);
299
            if (blank($OJ) || !$OJ->status) {
300
                return null;
301
            }
302
            $preQuery=$preQuery->where(["OJ"=>$filter['oj']]);
303
        }
304
        if ($filter['tag']) {
305
            $preQuery=$preQuery->join("problem_tag", "problem.pid", "=", "problem_tag.pid")->where(["tag"=>$filter['tag']]);
306
        }
307
        $paginator=$preQuery->select("problem.pid as pid", "pcode", "title")->orderBy(
308
            "OJ",
309
            "ASC"
310
        )->orderBy(
311
            "order_index",
312
            "ASC"
313
        )->orderBy(
314
            DB::raw("length(contest_id)"),
315
            "ASC"
316
        )->orderBy(
317
            "contest_id",
318
            "ASC"
319
        )->orderBy(
320
            DB::raw("length(index_id)"),
321
            "ASC"
322
        )->orderBy(
323
            "index_id",
324
            "ASC"
325
        )->orderBy(
326
            "pcode",
327
            "ASC"
328
        )->paginate(max(config('pagination.problem.per_page'), 1));
329
        $prob_list=$paginator->all();
330
331
        if (empty($prob_list)) {
332
            return null;
333
        }
334
        foreach ($prob_list as &$p) {
335
            $prob_stat=DB::table("submission")->select(
336
                DB::raw("count(sid) as submission_count"),
337
                DB::raw("sum(verdict='accepted') as passed_count"),
338
                DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
339
            )->where(["pid"=>$p["pid"]])->first();
340
            if ($prob_stat["submission_count"]==0) {
341
                $p["submission_count"]=0;
342
                $p["passed_count"]=0;
343
                $p["ac_rate"]=0;
344
            } else {
345
                $p["submission_count"]=$prob_stat["submission_count"];
346
                $p["passed_count"]=$prob_stat["passed_count"];
347
                $p["ac_rate"]=round($prob_stat["ac_rate"], 2);
348
            }
349
            if (!is_null($uid)) {
350
                $prob_status=$submissionModel->getProblemStatus($p["pid"], $uid);
351
                if (empty($prob_status)) {
352
                    $p["prob_status"]=[
353
                        "icon"=>"checkbox-blank-circle-outline",
354
                        "color"=>"wemd-grey-text"
355
                    ];
356
                } else {
357
                    $p["prob_status"]=[
358
                        "icon"=>$prob_status["verdict"]=="Accepted" ? "checkbox-blank-circle" : "cisco-webex",
359
                        "color"=>$prob_status["color"]
360
                    ];
361
                }
362
            } else {
363
                $p["prob_status"]=[
364
                    "icon"=>"checkbox-blank-circle-outline",
365
                    "color"=>"wemd-grey-text"
366
                ];
367
            }
368
        }
369
        return [
370
            'paginator' => $paginator,
371
            'problems' => $prob_list,
372
        ];
373
    }
374
375
    public function existPCode($pcode)
376
    {
377
        $temp=DB::table($this->table)->where(["pcode"=>$pcode])->select("pcode")->first();
378
        return empty($temp) ? null : $temp["pcode"];
379
    }
380
381
    public function pid($pcode)
382
    {
383
        $temp=DB::table($this->table)->where(["pcode"=>$pcode])->select("pid")->first();
384
        return empty($temp) ? 0 : $temp["pid"];
385
    }
386
387
    public function pcode($pid)
388
    {
389
        $temp=DB::table($this->table)->where(["pid"=>$pid])->select("pcode")->first();
390
        return empty($temp) ? 0 : $temp["pcode"];
391
    }
392
393
    public function ocode($pid)
394
    {
395
        $temp=DB::table($this->table)->where(["pid"=>$pid])->select("OJ as oid")->first();
396
        return empty($temp) ? null : DB::table("oj")->where(["oid"=>$temp["oid"]])->select("ocode")->first()["ocode"];
397
    }
398
399
    public function oid($pid)
400
    {
401
        return DB::table($this->table)->where(["pid"=>$pid])->select("OJ as oid")->first()["oid"];
402
    }
403
404
    public function clearTags($pid)
405
    {
406
        DB::table("problem_tag")->where(["pid"=>$pid])->delete();
407
        return true;
408
    }
409
410
    public function addTags($pid, $tag)
411
    {
412
        DB::table("problem_tag")->insert(["pid"=>$pid, "tag"=>$tag]);
413
        return true;
414
    }
415
416
    public function getSolvedCount($oid)
417
    {
418
        return DB::table($this->table)->select("pid", "solved_count")->where(["OJ"=>$oid])->get()->all();
419
    }
420
421
    public function updateDifficulty($pid, $diff_level)
422
    {
423
        DB::table("problem_tag")->where(["pid"=>$pid])->update(["difficulty"=>$diff_level]);
424
        return true;
425
    }
426
427
    public function insertProblem($data)
428
    {
429
        $info=array_merge([
430
            'difficulty'=>-1,
431
            'update_date'=>date("Y-m-d H:i:s"),
432
        ], collect($data)->only([
433
            'file',
434
            'file_url',
435
            'title',
436
            'time_limit',
437
            'memory_limit',
438
            'OJ',
439
            'description',
440
            'input',
441
            'output',
442
            'note',
443
            'input_type',
444
            'output_type',
445
            'pcode',
446
            'contest_id',
447
            'index_id',
448
            'origin',
449
            'source',
450
            'solved_count',
451
            'tot_score',
452
            'partial',
453
            'markdown',
454
            'special_compiler',
455
            'order_index',
456
        ])->toArray());
457
458
        $pid=DB::table($this->table)->insertGetId($info);
459
460
        if (!empty($data["sample"])) {
461
            foreach ($data["sample"] as $d) {
462
                if (!isset($d['sample_note'])) {
463
                    $d['sample_note']=null;
464
                }
465
                DB::table("problem_sample")->insert([
466
                    'pid'=>$pid,
467
                    'sample_input'=>$d['sample_input'],
468
                    'sample_output'=>$d['sample_output'],
469
                    'sample_note'=>$d['sample_note'],
470
                ]);
471
            }
472
        }
473
474
        return $pid;
475
    }
476
477
    public function updateProblem($data)
478
    {
479
        DB::table($this->table)->where(["pcode"=>$data['pcode']])->update(array_merge([
480
            'difficulty'=>-1,
481
            'update_date'=>date("Y-m-d H:i:s"),
482
        ], collect($data)->only([
483
            'file',
484
            'file_url',
485
            'title',
486
            'time_limit',
487
            'memory_limit',
488
            'OJ',
489
            'description',
490
            'input',
491
            'output',
492
            'note',
493
            'input_type',
494
            'output_type',
495
            'contest_id',
496
            'index_id',
497
            'origin',
498
            'source',
499
            'solved_count',
500
            'tot_score',
501
            'partial',
502
            'markdown',
503
            'special_compiler',
504
            'order_index',
505
        ])->toArray()));
506
507
        $pid=$this->pid($data['pcode']);
508
509
        DB::table("problem_sample")->where(["pid"=>$pid])->delete();
510
511
        if (!empty($data["sample"])) {
512
            foreach ($data["sample"] as $d) {
513
                if (!isset($d['sample_note'])) {
514
                    $d['sample_note']=null;
515
                }
516
                DB::table("problem_sample")->insert([
517
                    'pid'=>$pid,
518
                    'sample_input'=>$d['sample_input'],
519
                    'sample_output'=>$d['sample_output'],
520
                    'sample_note'=>$d['sample_note'],
521
                ]);
522
            }
523
        }
524
525
        return $pid;
526
    }
527
528
    public function discussionList($pid)
529
    {
530
        $paginator=DB::table('problem_discussion')->join(
531
            "users",
532
            "id",
533
            "=",
534
            "problem_discussion.uid"
535
        )->where([
536
            'problem_discussion.pid'=>$pid,
537
            'problem_discussion.audit'=>1
538
        ])->orderBy(
539
            'problem_discussion.created_at',
540
            'desc'
541
        )->select([
542
            'problem_discussion.pdid',
543
            'problem_discussion.title',
544
            'problem_discussion.updated_at',
545
            'users.avatar',
546
            'users.name',
547
            'users.id as uid'
548
        ])->paginate(15);
549
        $list=$paginator->all();
550
        foreach ($list as &$l) {
551
            $l['updated_at']=formatHumanReadableTime($l['updated_at']);
552
            $l['comment_count']=DB::table('problem_discussion_comment')->where('pdid', '=', $l['pdid'])->count();
553
        }
554
        return [
555
            'paginator' => $paginator,
556
            'list' => $list,
557
        ];
558
    }
559
560
    public function discussionDetail($pdid)
561
    {
562
        $main=DB::table('problem_discussion')->join(
563
            "users",
564
            "id",
565
            "=",
566
            "problem_discussion.uid"
567
        )->where(
568
            'problem_discussion.pdid',
569
            '=',
570
            $pdid
571
        )->select([
572
            'problem_discussion.pdid',
573
            'problem_discussion.title',
574
            'problem_discussion.content',
575
            'problem_discussion.votes',
576
            'problem_discussion.created_at',
577
            'users.avatar',
578
            'users.name',
579
            'users.id as uid'
580
        ])->get()->first();
581
        $main['created_at']=formatHumanReadableTime($main['created_at']);
582
        $main['content']=clean(Markdown::convertToHtml($main["content"]));
583
584
        $comment_count=DB::table('problem_discussion_comment')->where('pdid', '=', $pdid)->count();
585
586
        $paginator=DB::table('problem_discussion_comment')->join(
587
            "users",
588
            "id",
589
            "=",
590
            "problem_discussion_comment.uid"
591
        )->where([
592
            'problem_discussion_comment.pdid'=>$pdid,
593
            'problem_discussion_comment.reply_id'=>null,
594
            'problem_discussion_comment.audit'=>1
595
        ])->select([
596
            'problem_discussion_comment.pdcid',
597
            'problem_discussion_comment.pdid',
598
            'problem_discussion_comment.content',
599
            'problem_discussion_comment.votes',
600
            'problem_discussion_comment.created_at',
601
            'users.avatar',
602
            'users.name',
603
            'users.id as uid'
604
        ])->paginate(10);
605
        $comment=$paginator->all();
606
        foreach ($comment as &$c) {
607
            $c['content']=clean(Markdown::convertToHtml($c["content"]));
608
            $c['created_at']=formatHumanReadableTime($c['created_at']);
609
            $c['reply']=DB::table('problem_discussion_comment')->join(
610
                "users",
611
                "id",
612
                "=",
613
                "problem_discussion_comment.uid"
614
            )->where(
615
                'problem_discussion_comment.pdid',
616
                '=',
617
                $pdid
618
            )->where(
619
                'problem_discussion_comment.reply_id',
620
                '!=',
621
                null
622
            )->where(
623
                'problem_discussion_comment.audit',
624
                '=',
625
                1
626
            )->select([
627
                'problem_discussion_comment.pdcid',
628
                'problem_discussion_comment.pdid',
629
                'problem_discussion_comment.content',
630
                'problem_discussion_comment.reply_id',
631
                'problem_discussion_comment.votes',
632
                'problem_discussion_comment.created_at',
633
                'users.avatar',
634
                'users.name',
635
                'users.id as uid'
636
            ])->get()->all();
637
            foreach ($c['reply'] as $k=>&$cr) {
638
                $cr['content']=clean(Markdown::convertToHtml($cr["content"]));
639
                $cr['reply_uid']=DB::table('problem_discussion_comment')->where(
640
                    'pdcid',
641
                    '=',
642
                    $cr['reply_id']
643
                )->get()->first()['uid'];
644
                $cr['reply_name']=DB::table('users')->where(
645
                    'id',
646
                    '=',
647
                    $cr['reply_uid']
648
                )->get()->first()['name'];
649
                $cr['created_at']=formatHumanReadableTime($cr['created_at']);
650
                if ($this->replyParent($cr['pdcid'])!=$c['pdcid']) {
651
                    unset($c['reply'][$k]);
652
                }
653
            }
654
        }
655
        return [
656
            'main' => $main,
657
            'comment_count' => $comment_count,
658
            'paginator' => $paginator,
659
            'comment' => $comment
660
        ];
661
    }
662
663
    public function replyParent($pdcid)
664
    {
665
        $reply_id=DB::table('problem_discussion_comment')->where('pdcid', '=', $pdcid)->get()->first()['reply_id'];
666
        $top=DB::table('problem_discussion_comment')->where('pdcid', '=', $reply_id)->get()->first()['reply_id'];
667
        if (isset($top)) {
668
            return $this->replyParent($reply_id);
669
        } else {
670
            return $reply_id;
671
        }
672
    }
673
674
    public function pcodeByPdid($dcode)
675
    {
676
        $pid=DB::table('problem_discussion')->where('pdid', '=', $dcode)->get()->first()['pid'];
677
        $pcode=$this->pcode($pid);
678
        return $pcode;
679
    }
680
681
    public function addDiscussion($uid, $pid, $title, $content)
682
    {
683
        $pdid=DB::table("problem_discussion")->insertGetId([
684
            "uid"=>$uid,
685
            "pid"=>$pid,
686
            "title"=>$title,
687
            "content"=>$content,
688
            "votes"=>0,
689
            "audit"=>1,
690
            "created_at"=>date("Y-m-d H:i:s"),
691
            "updated_at"=>date("Y-m-d H:i:s"),
692
        ]);
693
        return $pdid;
694
    }
695
696
    public function pidByPdid($pdid)
697
    {
698
        $pid=DB::table('problem_discussion')->where('pdid', '=', $pdid)->get()->first()['pid'];
699
        return $pid;
700
    }
701
702
    public function addComment($uid, $pdid, $content, $reply_id)
703
    {
704
        $pid=$this->pidByPdid($pdid);
705
        $pdcid=DB::table('problem_discussion_comment')->insertGetId([
706
            'pdid'=>$pdid,
707
            'uid'=>$uid,
708
            'pid'=>$pid,
709
            'content'=>$content,
710
            'reply_id'=>$reply_id,
711
            'votes'=>0,
712
            'audit'=>1,
713
            'created_at'=>date("Y-m-d H:i:s"),
714
            'updated_at'=>date("Y-m-d H:i:s"),
715
        ]);
716
        return $pdcid;
717
    }
718
719
    public function isHidden($pid)
720
    {
721
        return DB::table('problem')->where('pid', '=', $pid)->get()->first()['hide'];
722
    }
723
}
724