1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\Feedback; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\FeedbackPost; |
6
|
|
|
use Ffcms\Core\App; |
7
|
|
|
use Ffcms\Core\Arch\Model; |
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
9
|
|
|
|
10
|
|
|
class FormFeedbackAdd extends Model |
11
|
|
|
{ |
12
|
|
|
public $name; |
13
|
|
|
public $email; |
14
|
|
|
public $captcha; |
15
|
|
|
public $message; |
16
|
|
|
|
17
|
|
|
private $_hash; |
18
|
|
|
|
19
|
|
|
private $_useCaptcha = true; |
20
|
|
|
|
21
|
|
|
public function __construct($captcha = true) |
22
|
|
|
{ |
23
|
|
|
$this->_useCaptcha = (bool)$captcha; |
24
|
|
|
parent::__construct(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set user data if he is logged in |
30
|
|
|
*/ |
31
|
|
|
public function before() |
32
|
|
|
{ |
33
|
|
|
if (App::$User->isAuth()) { |
34
|
|
|
$data = App::$User->identity(); |
35
|
|
|
$this->name = $data->getProfile()->nick; |
36
|
|
|
$this->email = $data->email; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Labels to display form |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function labels() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
'name' => __('Name'), |
47
|
|
|
'email' => __('Email'), |
48
|
|
|
'message' => __('Message'), |
49
|
|
|
'captcha' => __('Captcha') |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Rules to validate send form |
55
|
|
|
*/ |
56
|
|
|
public function rules() |
57
|
|
|
{ |
58
|
|
|
$rules = [ |
59
|
|
|
[['name', 'email', 'message'], 'required'], |
60
|
|
|
['name', 'length_min', '2'], |
61
|
|
|
['message', 'length_min', 10], |
62
|
|
|
['email', 'email'], |
63
|
|
|
['captcha', 'used'] |
64
|
|
|
]; |
65
|
|
|
if (true === $this->_useCaptcha) { |
66
|
|
|
$rules[] = ['captcha', 'App::$Captcha::validate']; |
67
|
|
|
} |
68
|
|
|
return $rules; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function make() |
72
|
|
|
{ |
73
|
|
|
// calculate security hash to direct-on access |
74
|
|
|
$hash = Str::randomLatinNumeric(mt_rand(16, 64)); |
75
|
|
|
|
76
|
|
|
// init new row and set row data |
77
|
|
|
$record = new FeedbackPost(); |
78
|
|
|
$record->name = App::$Security->strip_tags($this->name); |
79
|
|
|
$record->email = App::$Security->strip_tags($this->email); |
80
|
|
|
$record->message = App::$Security->strip_tags($this->message); |
81
|
|
|
$record->hash = $hash; |
82
|
|
|
if (App::$User->isAuth()) { |
83
|
|
|
$record->user_id = App::$User->identity()->getId(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$record->ip = App::$Request->getClientIp(); |
87
|
|
|
// save row to db |
88
|
|
|
$record->save(); |
89
|
|
|
|
90
|
|
|
return $record; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string|null |
95
|
|
|
*/ |
96
|
|
|
public function getHash() |
97
|
|
|
{ |
98
|
|
|
return $this->_hash; |
99
|
|
|
} |
100
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: