1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Api\Comments; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\CommentAnswer; |
6
|
|
|
use Apps\ActiveRecord\CommentPost; |
7
|
|
|
use Ffcms\Core\App; |
8
|
|
|
use Ffcms\Core\Arch\Model; |
9
|
|
|
use Ffcms\Core\Exception\JsonException; |
10
|
|
|
use Ffcms\Core\Helper\Date; |
11
|
|
|
use Ffcms\Core\Helper\Type\Str; |
12
|
|
|
|
13
|
|
|
class CommentAdd extends Model |
14
|
|
|
{ |
15
|
|
|
public $pathway; |
16
|
|
|
public $message; |
17
|
|
|
public $replayTo = 0; |
18
|
|
|
public $guestName; |
19
|
|
|
public $ip; |
20
|
|
|
public $user_id = 0; |
21
|
|
|
|
22
|
|
|
public $response = [ |
23
|
|
|
'status' => 0, |
24
|
|
|
'message' => 'unknown error' |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
private $_configs = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* CommentAdd constructor. Pass add comment data inside model |
31
|
|
|
* @param array|null $configs |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $configs = null) |
34
|
|
|
{ |
35
|
|
|
$this->_configs = $configs; |
|
|
|
|
36
|
|
|
parent::__construct(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get and store to model properties other data |
41
|
|
|
*/ |
42
|
|
|
public function before() |
43
|
|
|
{ |
44
|
|
|
$this->ip = App::$Request->getClientIp(); |
45
|
|
|
if (App::$User->isAuth()) { |
46
|
|
|
$this->user_id = App::$User->identity()->getId(); |
47
|
|
|
} |
48
|
|
|
parent::before(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function check() |
52
|
|
|
{ |
53
|
|
|
// check if user is auth'd or guest name is defined |
54
|
|
View Code Duplication |
if (!App::$User->isAuth() && ((int)$this->_configs['guestAdd'] !== 1 || Str::length($this->guestName) < 2)) { |
|
|
|
|
55
|
|
|
throw new JsonException(__('Guest name is not defined')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// check if pathway is empty |
59
|
|
|
if (Str::likeEmpty($this->pathway)) { |
60
|
|
|
throw new JsonException(__('Wrong target pathway')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// check if message length is correct |
64
|
|
View Code Duplication |
if (Str::length($this->message) < (int)$this->_configs['minLength'] || Str::length($this->message) > (int)$this->_configs['maxLength']) { |
|
|
|
|
65
|
|
|
throw new JsonException(__('Message length is incorrect. Current: %cur% , min - %min%, max - %max%', [ |
66
|
|
|
'cur' => Str::length($this->message), |
67
|
|
|
'min' => $this->_configs['minLength'], |
68
|
|
|
'max' => $this->_configs['maxLength'] |
69
|
|
|
])); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// sounds like answer, lets try to find post thread comment |
73
|
|
|
if ($this->replayTo > 0) { |
74
|
|
|
$count = CommentPost::where('id', '=', $this->replayTo)->count(); |
75
|
|
|
if ($count !== 1) { |
76
|
|
|
throw new JsonException(__('Comment post thread is not founded')); |
77
|
|
|
} |
78
|
|
|
// check for prevent spam |
79
|
|
|
$query = CommentAnswer::where(function($q) { |
80
|
|
|
$q->where('user_id', '=', $this->user_id) |
81
|
|
|
->orWhere('ip', '=', $this->ip); |
82
|
|
|
})->orderBy('created_at', 'DESC') |
83
|
|
|
->first(); |
84
|
|
|
|
85
|
|
|
// something is founded :D |
86
|
|
View Code Duplication |
if ($query !== null) { |
|
|
|
|
87
|
|
|
$answerTime = Date::convertToTimestamp($query->created_at); |
88
|
|
|
$delay = $answerTime + $this->_configs['delay'] - time(); |
89
|
|
|
if ($delay > 0) { // sounds like config time is not passed now |
90
|
|
|
throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} else { // sounds like post, lets try to check latest post |
94
|
|
|
$query = CommentPost::where(function($q) { |
95
|
|
|
$q->where('user_id', '=', $this->user_id) |
96
|
|
|
->orWhere('ip', '=', $this->ip); |
97
|
|
|
})->orderBy('created_at', 'DESC') |
98
|
|
|
->first(); |
99
|
|
|
|
100
|
|
|
// check if latest post time for this user is founded |
101
|
|
View Code Duplication |
if ($query !== null) { |
|
|
|
|
102
|
|
|
$postTime = Date::convertToTimestamp($query->created_at); |
103
|
|
|
$delay = $postTime + $this->_configs['delay'] - time(); |
104
|
|
|
if ($delay > 0) { |
105
|
|
|
throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function make() |
112
|
|
|
{ |
113
|
|
|
$record = null; |
|
|
|
|
114
|
|
|
$type = null; |
|
|
|
|
115
|
|
|
// sounds like answer |
116
|
|
|
if ($this->replayTo > 0) { |
117
|
|
|
$type = 'answer'; |
|
|
|
|
118
|
|
|
$record = new CommentAnswer(); |
119
|
|
|
$record->comment_id = $this->replayTo; |
120
|
|
|
$record->user_id = $this->user_id; |
121
|
|
|
$record->guest_name = $this->guestName; |
122
|
|
|
$record->message = $this->message; |
123
|
|
|
$record->ip = $this->ip; |
124
|
|
|
$record->save(); |
125
|
|
|
} else { // sounds like new post |
126
|
|
|
$type = 'post'; |
|
|
|
|
127
|
|
|
$record = new CommentPost(); |
128
|
|
|
$record->pathway = $this->pathway; |
129
|
|
|
$record->user_id = $this->user_id; |
130
|
|
|
$record->guest_name = $this->guestName; |
131
|
|
|
$record->message = $this->message; |
132
|
|
|
$record->lang = App::$Request->getLanguage(); |
133
|
|
|
$record->save(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.