1 | <?php |
---|
2 | /*********************************************** |
---|
3 | * File : statemachine.php |
---|
4 | * Project : Z-Push |
---|
5 | * Descr : This class handles state requests; |
---|
6 | * Each differential mechanism can |
---|
7 | * store its own state information, |
---|
8 | * which is stored through the |
---|
9 | * state machine. SyncKey's are |
---|
10 | * of the form {UUID}N, in which |
---|
11 | * UUID is allocated during the |
---|
12 | * first sync, and N is incremented |
---|
13 | * for each request to 'getNewSyncKey'. |
---|
14 | * A sync state is simple an opaque |
---|
15 | * string value that can differ |
---|
16 | * for each backend used - normally |
---|
17 | * a list of items as the backend has |
---|
18 | * sent them to the PIM. The backend |
---|
19 | * can then use this backend |
---|
20 | * information to compute the increments |
---|
21 | * with current data. |
---|
22 | * |
---|
23 | * Old sync states are not deleted |
---|
24 | * until a sync state is requested. |
---|
25 | * At that moment, the PIM is |
---|
26 | * apparently requesting an update |
---|
27 | * since sync key X, so any sync |
---|
28 | * states before X are already on |
---|
29 | * the PIM, and can therefore be |
---|
30 | * removed. This algorithm is |
---|
31 | * automatically enforced by the |
---|
32 | * StateMachine class. |
---|
33 | * |
---|
34 | * |
---|
35 | * Created : 01.10.2007 |
---|
36 | * |
---|
37 | * ᅵ Zarafa Deutschland GmbH, www.zarafaserver.de |
---|
38 | * This file is distributed under GPL v2. |
---|
39 | * Consult LICENSE file for details |
---|
40 | ************************************************/ |
---|
41 | |
---|
42 | |
---|
43 | class StateMachine { |
---|
44 | // Gets the sync state for a specified sync key. Requesting a sync key also implies |
---|
45 | // that previous sync states for this sync key series are no longer needed, and the |
---|
46 | // state machine will tidy up these files. |
---|
47 | function getSyncState($synckey) { |
---|
48 | // No sync state for sync key '0' |
---|
49 | if($synckey == "0" || $synckey == "s0") |
---|
50 | return ""; |
---|
51 | |
---|
52 | // Check if synckey is allowed |
---|
53 | if(!preg_match('/^s{0,1}\{([0-9A-Za-z-]+)\}([0-9]+)$/', $synckey, $matches)) { |
---|
54 | return false; |
---|
55 | } |
---|
56 | |
---|
57 | // Remember synckey GUID and ID |
---|
58 | $guid = $matches[1]; |
---|
59 | $n = $matches[2]; |
---|
60 | |
---|
61 | // Cleanup all older syncstates |
---|
62 | $dir = opendir(BASE_PATH . STATE_DIR); |
---|
63 | if(!$dir) |
---|
64 | return false; |
---|
65 | |
---|
66 | while($entry = readdir($dir)) { |
---|
67 | if(preg_match('/^s{0,1}\{([0-9A-Za-z-]+)\}([0-9]+)$/', $entry, $matches)) { |
---|
68 | if($matches[1] == $guid && $matches[2] < $n) { |
---|
69 | unlink(BASE_PATH . STATE_DIR . "/$entry"); |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | // Read current sync state |
---|
75 | $filename = BASE_PATH . STATE_DIR . "/$synckey"; |
---|
76 | |
---|
77 | if(file_exists($filename)) |
---|
78 | return file_get_contents(BASE_PATH . STATE_DIR . "/$synckey"); |
---|
79 | else return false; |
---|
80 | } |
---|
81 | |
---|
82 | // Gets the new sync key for a specified sync key. You must save the new sync state |
---|
83 | // under this sync key when done sync'ing (by calling setSyncState); |
---|
84 | function getNewSyncKey($synckey) { |
---|
85 | if(!isset($synckey) || $synckey == "0") { |
---|
86 | return "{" . $this->uuid() . "}" . "1"; |
---|
87 | } else { |
---|
88 | if(preg_match('/^s{0,1}\{([a-fA-F0-9-]+)\}([0-9]+)$/', $synckey, $matches)) { |
---|
89 | $n = $matches[2]; |
---|
90 | $n++; |
---|
91 | return "{" . $matches[1] . "}" . $n; |
---|
92 | } else return false; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | // Writes the sync state to a new synckey |
---|
97 | function setSyncState($synckey, $syncstate) { |
---|
98 | // Check if synckey is allowed |
---|
99 | if(!preg_match('/^s{0,1}\{[0-9A-Za-z-]+\}[0-9]+$/', $synckey)) { |
---|
100 | return false; |
---|
101 | } |
---|
102 | |
---|
103 | return file_put_contents(BASE_PATH . STATE_DIR . "/$synckey", $syncstate); |
---|
104 | } |
---|
105 | |
---|
106 | function uuid() |
---|
107 | { |
---|
108 | return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
---|
109 | mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), |
---|
110 | mt_rand( 0, 0x0fff ) | 0x4000, |
---|
111 | mt_rand( 0, 0x3fff ) | 0x8000, |
---|
112 | mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) ); |
---|
113 | } |
---|
114 | }; |
---|
115 | |
---|
116 | ?> |
---|