/
class.timestampvalidator.php
62 lines (58 loc) · 1.61 KB
/
class.timestampvalidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* @package toolkit
*/
/**
* This class checks against the Database for the latest timestamp, to make
* sure the data being saved is the one the user saw.
*
* @since Symphony 2.7.0
*/
class TimestampValidator
{
/**
* The name of the table to check against
* @var string
*/
private $table = '';
/**
* Creates a new TimestampValidator object, for a particular table.
* The `tbl_` prefix is automatically added.
*
* @param string $table
* The table name
*/
public function __construct($table)
{
General::ensureType(array(
'table' => array('var' => $table, 'type'=> 'string'),
));
$this->table = 'tbl_' . MySQL::cleanValue($table);
}
/**
* Checks if the modified date of the record identified with $id
* if equal to the supplied $timestamp
*
* @param int|string $id
* The record id to check
* @param string $timestamp
* The user supplied timestamp
* @return boolean
* true if the $timestamp is the latest or the $id is invalid, false other wise
*/
public function check($id, $timestamp)
{
$id = General::intval(MySQL::cleanValue($id));
if ($id < 1) {
return false;
}
$timestamp = MySQL::cleanValue($timestamp);
$sql = "
SELECT `id` FROM `{$this->table}`
WHERE `id` = $id
AND `modification_date` = '$timestamp'
";
$results = Symphony::Database()->fetchVar('id', 0, $sql);
return !empty($results) && General::intval($results) === $id;
}
}