1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Api\Comments; |
4
|
|
|
|
5
|
|
|
|
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
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CommentPostAdd. Model to parse and insert input comment post data. |
16
|
|
|
* @package Apps\Model\Api\Comments |
17
|
|
|
*/ |
18
|
|
|
class CommentPostAdd extends Model |
19
|
|
|
{ |
20
|
|
|
public $pathway; |
21
|
|
|
public $message; |
22
|
|
|
public $guestName; |
23
|
|
|
|
24
|
|
|
public $ip; |
25
|
|
|
|
26
|
|
|
private $_configs; |
27
|
|
|
private $_userId = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* CommentPostAdd constructor. Pass configuration inside. |
31
|
|
|
* @param array $configs |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $configs) |
34
|
|
|
{ |
35
|
|
|
$this->_configs = $configs; |
36
|
|
|
parent::__construct(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Prepare model data - user ip and other data |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function before() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
// set user ip |
45
|
|
|
$this->ip = App::$Request->getClientIp(); |
46
|
|
|
// set user object if auth done |
47
|
|
|
if (App::$User->isAuth()) { |
48
|
|
|
$this->_userId = App::$User->identity()->getId(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check comment add conditions. On bad conditions will be throw'd exception. |
54
|
|
|
* @throws JsonException |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
|
|
public function check() |
58
|
|
|
{ |
59
|
|
|
// check if user is auth'd or guest name is defined |
60
|
|
View Code Duplication |
if (!App::$User->isAuth() && ((int)$this->_configs['guestAdd'] !== 1 || Str::length($this->guestName) < 2)) { |
|
|
|
|
61
|
|
|
throw new JsonException(__('Guest name is not defined')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// check if pathway is empty |
65
|
|
|
if (Str::likeEmpty($this->pathway)) { |
66
|
|
|
throw new JsonException(__('Wrong target pathway')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// check if message length is correct |
70
|
|
View Code Duplication |
if (Str::length($this->message) < (int)$this->_configs['minLength'] || Str::length($this->message) > (int)$this->_configs['maxLength']) { |
|
|
|
|
71
|
|
|
throw new JsonException(__('Message length is incorrect. Current: %cur%, min - %min%, max - %max%', [ |
72
|
|
|
'cur' => Str::length($this->message), |
73
|
|
|
'min' => $this->_configs['minLength'], |
74
|
|
|
'max' => $this->_configs['maxLength'] |
75
|
|
|
])); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// guest moderation |
79
|
|
View Code Duplication |
if (!App::$User->isAuth() && (bool)$this->_configs['guestModerate']) { |
|
|
|
|
80
|
|
|
$captcha = App::$Request->request->get('captcha'); |
81
|
|
|
if (!App::$Captcha->validate($captcha)) { |
82
|
|
|
throw new JsonException(__('Captcha is incorrect! Click on image to refresh and try again')); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// check delay between 2 comments from 1 user or 1 ip |
87
|
|
|
$query = CommentPost::where('user_id', '=', $this->_userId) |
88
|
|
|
->orWhere('ip', '=', $this->ip) |
89
|
|
|
->orderBy('created_at', 'DESC') |
90
|
|
|
->first(); |
91
|
|
|
|
92
|
|
|
// check if latest post time for this user is founded |
93
|
|
View Code Duplication |
if ($query !== null) { |
|
|
|
|
94
|
|
|
$postTime = Date::convertToTimestamp($query->created_at); |
95
|
|
|
$delay = $postTime + $this->_configs['delay'] - time(); |
96
|
|
|
if ($delay > 0) { |
97
|
|
|
throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Insert new comment in table and return active record object |
106
|
|
|
* @return CommentPost |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function buildRecord() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$record = new CommentPost(); |
111
|
|
|
$record->pathway = $this->pathway; |
112
|
|
|
$record->user_id = $this->_userId; |
113
|
|
|
$record->guest_name = $this->guestName; |
114
|
|
|
$record->message = $this->message; |
115
|
|
|
$record->lang = App::$Request->getLanguage(); |
116
|
|
|
// check if premoderation is enabled and user is guest |
117
|
|
|
if ((int)$this->_configs['guestModerate'] === 1 && $this->_userId < 1) { |
118
|
|
|
$record->moderate = 1; |
119
|
|
|
} |
120
|
|
|
$record->save(); |
121
|
|
|
|
122
|
|
|
return $record; |
123
|
|
|
} |
124
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.