|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Object; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use DateTimeInterface; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
|
|
10
|
|
|
// From Pimple |
|
11
|
|
|
use Pimple\Container; |
|
12
|
|
|
|
|
13
|
|
|
// From 'charcoal-core' |
|
14
|
|
|
use Charcoal\Model\AbstractModel; |
|
15
|
|
|
|
|
16
|
|
|
// From 'charcoal-translator' |
|
17
|
|
|
use Charcoal\Translator\TranslatorAwareTrait; |
|
18
|
|
|
|
|
19
|
|
|
// From 'charcoal-object' |
|
20
|
|
|
use Charcoal\Object\UserDataInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* User Data is a base model for objects typically submitted by the end-user of the application. |
|
24
|
|
|
* |
|
25
|
|
|
* Although it is not abstract, it is typically used by extending into a subclass. |
|
26
|
|
|
*/ |
|
27
|
|
|
class UserData extends AbstractModel implements |
|
|
|
|
|
|
28
|
|
|
UserDataInterface |
|
29
|
|
|
{ |
|
30
|
|
|
use TranslatorAwareTrait; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Client IP address of the end-user. |
|
34
|
|
|
* |
|
35
|
|
|
* @var integer|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $ip; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Language of the end-user or source URI. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string|null |
|
43
|
|
|
*/ |
|
44
|
|
|
private $lang; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Source URL or identifier of end-user submission. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string|null |
|
50
|
|
|
*/ |
|
51
|
|
|
private $origin; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Creation timestamp of submission. |
|
55
|
|
|
* |
|
56
|
|
|
* @var DateTimeInterface|null |
|
57
|
|
|
*/ |
|
58
|
|
|
private $ts; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Dependencies |
|
62
|
|
|
* @param Container $container DI Container. |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function setDependencies(Container $container) |
|
66
|
|
|
{ |
|
67
|
|
|
parent::setDependencies($container); |
|
68
|
|
|
|
|
69
|
|
|
$this->setTranslator($container['translator']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set the client IP address. |
|
74
|
|
|
* |
|
75
|
|
|
* @param integer|null $ip The remote IP at object creation. |
|
76
|
|
|
* @return self |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setIp($ip) |
|
79
|
|
|
{ |
|
80
|
|
|
if ($ip === null) { |
|
81
|
|
|
$this->ip = null; |
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (is_string($ip)) { |
|
86
|
|
|
$ip = ip2long($ip); |
|
87
|
|
|
} elseif (is_numeric($ip)) { |
|
88
|
|
|
$ip = intval($ip); |
|
89
|
|
|
} else { |
|
90
|
|
|
$ip = 0; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->ip = $ip; |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Retrieve the client IP address. |
|
100
|
|
|
* |
|
101
|
|
|
* @return integer|null |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getIp() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->ip; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Set the origin language. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $lang The language code. |
|
112
|
|
|
* @throws InvalidArgumentException If the argument is not a string. |
|
113
|
|
|
* @return self |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function setLang($lang) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
if ($lang !== null) { |
|
118
|
|
|
if (!is_string($lang)) { |
|
119
|
|
|
throw new InvalidArgumentException( |
|
120
|
|
|
'Language must be a string' |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->lang = $lang; |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Retrieve the language. |
|
132
|
|
|
* |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getLang() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->lang; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Set the origin of the object submission. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $origin The source URL or identifier of the submission. |
|
144
|
|
|
* @throws InvalidArgumentException If the argument is not a string. |
|
145
|
|
|
* @return self |
|
146
|
|
|
*/ |
|
147
|
|
View Code Duplication |
public function setOrigin($origin) |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
|
|
if ($origin !== null) { |
|
150
|
|
|
if (!is_string($origin)) { |
|
151
|
|
|
throw new InvalidArgumentException( |
|
152
|
|
|
'Origin must be a string.' |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$this->origin = $origin; |
|
158
|
|
|
|
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Resolve the origin of the user data. |
|
164
|
|
|
* |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
public function resolveOrigin() |
|
168
|
|
|
{ |
|
169
|
|
|
$host = getenv('HTTP_HOST'); |
|
170
|
|
|
$uri = ''; |
|
171
|
|
|
if ($host) { |
|
172
|
|
|
$uri = 'http'; |
|
173
|
|
|
|
|
174
|
|
|
if (getenv('HTTPS') === 'on') { |
|
175
|
|
|
$uri .= 's'; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$uri .= '://'.$host; |
|
179
|
|
|
} |
|
180
|
|
|
$uri .= getenv('REQUEST_URI'); |
|
181
|
|
|
|
|
182
|
|
|
return $uri; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Retrieve the origin of the object submission. |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getOrigin() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->origin; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Set when the object was created. |
|
197
|
|
|
* |
|
198
|
|
|
* @param DateTimeInterface|string|null $timestamp The timestamp at object's creation. |
|
199
|
|
|
* NULL is accepted and instances of DateTimeInterface are recommended; |
|
200
|
|
|
* any other value will be converted (if possible) into one. |
|
201
|
|
|
* @throws InvalidArgumentException If the timestamp is invalid. |
|
202
|
|
|
* @return self |
|
203
|
|
|
*/ |
|
204
|
|
View Code Duplication |
public function setTs($timestamp) |
|
|
|
|
|
|
205
|
|
|
{ |
|
206
|
|
|
if ($timestamp === null) { |
|
207
|
|
|
$this->ts = null; |
|
208
|
|
|
return $this; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
if (is_string($timestamp)) { |
|
212
|
|
|
try { |
|
213
|
|
|
$timestamp = new DateTime($timestamp); |
|
214
|
|
|
} catch (Exception $e) { |
|
215
|
|
|
throw new InvalidArgumentException(sprintf( |
|
216
|
|
|
'Invalid timestamp: %s', |
|
217
|
|
|
$e->getMessage() |
|
218
|
|
|
), 0, $e); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
if (!$timestamp instanceof DateTimeInterface) { |
|
223
|
|
|
throw new InvalidArgumentException( |
|
224
|
|
|
'Invalid timestamp value. Must be a date/time string or a DateTime object.' |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$this->ts = $timestamp; |
|
229
|
|
|
|
|
230
|
|
|
return $this; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Retrieve the creation timestamp. |
|
235
|
|
|
* |
|
236
|
|
|
* @return DateTimeInterface|null |
|
237
|
|
|
*/ |
|
238
|
|
|
public function getTs() |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->ts; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Event called before _creating_ the object. |
|
245
|
|
|
* |
|
246
|
|
|
* @see Charcoal\Source\StorableTrait::preSave() For the "create" Event. |
|
247
|
|
|
* @return boolean |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function preSave() |
|
250
|
|
|
{ |
|
251
|
|
|
$result = parent::preSave(); |
|
252
|
|
|
|
|
253
|
|
|
$this->setTs('now'); |
|
254
|
|
|
|
|
255
|
|
|
if (getenv('REMOTE_ADDR')) { |
|
256
|
|
|
$this->setIp(getenv('REMOTE_ADDR')); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
if (!isset($this->origin)) { |
|
260
|
|
|
$this->setOrigin($this->resolveOrigin()); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $result; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|