1: <?php
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: class Horde_ActiveSync_Sync
33: {
34: 35: 36: 37: 38:
39: protected $_changes;
40:
41: 42: 43: 44: 45:
46: protected $_step = 0;
47:
48: 49: 50: 51: 52:
53: protected $_folderId;
54:
55: 56: 57: 58: 59:
60: protected $_collection;
61:
62: 63: 64: 65: 66:
67: protected $_backend;
68:
69: 70: 71: 72: 73:
74: protected $_flags;
75:
76: 77: 78: 79: 80:
81: protected $_state;
82:
83: 84: 85: 86: 87:
88: protected $_exporter;
89:
90: 91: 92: 93: 94:
95: protected $_logger;
96:
97: 98: 99: 100: 101:
102: public function __construct(Horde_ActiveSync_Driver_Base $backend)
103: {
104: $this->_backend = $backend;
105: }
106:
107: 108: 109: 110: 111: 112: 113: 114: 115:
116: public function init(Horde_ActiveSync_State_Base &$stateMachine,
117: Horde_ActiveSync_Connector_Exporter $exporter = null,
118: array $collection = array())
119: {
120: $this->_stateMachine = &$stateMachine;
121:
122:
123:
124: $this->_exporter = $exporter;
125: $this->_folderId = !empty($collection['id']) ? $collection['id'] : false;
126: $this->_changes = $stateMachine->getChanges();
127: $this->_truncation = !empty($collection['truncation']) ? $collection['truncation'] : 0;
128: }
129:
130: public function setLogger($logger)
131: {
132: $this->_logger = $logger;
133: }
134:
135: 136: 137: 138: 139: 140: 141: 142:
143: public function syncronize($flags = 0)
144: {
145: $progress = array();
146:
147: if ($this->_folderId == false) {
148: if ($this->_step < count($this->_changes)) {
149: $change = $this->_changes[$this->_step];
150: switch($change['type']) {
151: case 'change':
152: $folder = $this->_backend->getFolder($change['id']);
153: $stat = $this->_backend->statFolder($change['id']);
154: if (!$folder) {
155: return;
156: }
157: if ($flags & Horde_ActiveSync::BACKEND_DISCARD_DATA ||
158: $this->_exporter->folderChange($folder)) {
159:
160: $this->_stateMachine->updateState('foldersync', $stat);
161: }
162: break;
163: case 'delete':
164: if ($flags & Horde_ActiveSync::BACKEND_DISCARD_DATA ||
165: $this->_exporter->folderDeletion($change['id'])) {
166:
167: $this->_stateMachine->updateState('foldersync', $change);
168: }
169: break;
170: }
171: $this->_step++;
172: $progress = array();
173: $progress['steps'] = count($this->_changes);
174: $progress['progress'] = $this->_step;
175: return $progress;
176: } else {
177: return false;
178: }
179: } else {
180: if ($this->_step < count($this->_changes)) {
181: $change = $this->_changes[$this->_step];
182:
183:
184:
185: while (empty($change['id']) && $this->_step < count($this->_changes) - 1) {
186: $this->_logger->err('Missing UID value for an entry in: ' . $this->_folderId);
187: $this->_step++;
188: $change = $this->_changes[$this->_step];
189: }
190:
191: switch($change['type']) {
192: case 'change':
193: $truncsize = self::_getTruncSize($this->_truncation);
194: if (!$message = $this->_backend->getMessage($this->_folderId, $change['id'], $truncsize)) {
195: return false;
196: }
197:
198:
199: $message->flags = (isset($change['flags'])) ? $change['flags'] : 0;
200: if ($flags & Horde_ActiveSync::BACKEND_DISCARD_DATA || $this->_exporter->messageChange($change['id'], $message) == true) {
201: $this->_stateMachine->updateState('change', $change);
202: }
203: break;
204:
205: case 'delete':
206: if ($flags & Horde_ActiveSync::BACKEND_DISCARD_DATA || $this->_exporter->messageDeletion($change['id']) == true) {
207: $this->_stateMachine->updateState('delete', $change);
208: }
209: break;
210:
211: case 'flags':
212: if ($flags & Horde_ActiveSync::BACKEND_DISCARD_DATA || $this->_exporter->messageReadFlag($change['id'], $change['flags']) == true) {
213: $this->_stateMachine->updateState('flags', $change);
214: }
215: break;
216:
217: case 'move':
218: if ($flags & Horde_ActiveSync::BACKEND_DISCARD_DATA || $this->_exporter->messageMove($change['id'], $change['parent']) == true) {
219: $this->_stateMachine->updateState('move', $change);
220: }
221: break;
222: }
223:
224: $this->_step++;
225:
226: $progress = array();
227: $progress['steps'] = count($this->_changes);
228: $progress['progress'] = $this->_step;
229:
230: return $progress;
231: } else {
232: return false;
233: }
234: }
235: }
236:
237: public function getChangeCount()
238: {
239: return count($this->_changes);
240: }
241:
242: 243: 244: 245: 246:
247: private static function _getTruncSize($truncation)
248: {
249: switch($truncation) {
250: case Horde_ActiveSync::TRUNCATION_HEADERS:
251: return 0;
252: case Horde_ActiveSync::TRUNCATION_512B:
253: return 512;
254: case Horde_ActiveSync::TRUNCATION_1K:
255: return 1024;
256: case Horde_ActiveSync::TRUNCATION_5K:
257: return 5 * 1024;
258: case Horde_ActiveSync::TRUNCATION_SEVEN:
259: case Horde_ActiveSync::TRUNCATION_ALL:
260: return 1024 * 1024;
261: default:
262: return 1024;
263: }
264: }
265:
266: }
267: