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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
<?php
include_once 'SyncML/Command.php';
include_once 'SyncML/Command/Status.php';
include_once 'SyncML/Command/Alert.php';
include_once 'SyncML/Command/Sync.php';
include_once 'SyncML/Command/Final.php';
include_once 'SyncML/Sync.php';
include_once 'SyncML/Backend.php';
/**
* The SyncML_SyncHdr and SyncML_SyncBody classes provides
* a SyncHdr and SyncBody in SyncML Representation Protocol, version
* 1.1 5.2.2 and 5.2.3. Most of the work is passed on to
* SyncML_Command_Alert and SyncML_Command_Sync.
*
* There are two global objects that are used by SyncML:
* 1) $_SESSION['SyncML.state']:
* session object used to maintain the state between the individual
* SyncML messages.
*
* 2) $GLOBALS['backend']
* Backend to handle the communication with the datastore. Currently the
* horde backend is the only backend provided.
*
* $Horde: framework/SyncML/SyncML.php,v 1.21.10.10 2006/01/01 21:28:35 jan Exp $
*
* Copyright 2003-2006 Anthony Mills <amills@pyramid6.com>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Anthony Mills <amills@pyramid6.com>
* @author Karsten Fourmont <fourmont@gmx.de>
* @since Horde 3.0
* @package SyncML
*/
class SyncML_ContentHandler {
/**
* Output ContentHandler used to output XML events.
*
* @var object
*/
var $_output;
/**
* Stack for holding the xml elements during creation of the object from
* the xml event flow.
*
* @var array
*/
var $_Stack = array();
/**
* @var string
*/
var $_chars;
function setOutput(&$output)
{
$this->_output = &$output;
}
function startElement($uri, $element, $attrs)
{
$this->_Stack[] = $element;
}
function endElement($uri, $element)
{
if (isset($this->_chars)) {
unset($this->_chars);
}
array_pop($this->_Stack);
}
function characters($str)
{
if (isset($this->_chars)) {
$this->_chars = $this->_chars . $str;
} else {
$this->_chars = $str;
}
}
}
/**
* Defined in SyncML Representation Protocol, version 1.1 5.2.2
*
* @package SyncML
*/
class SyncML_SyncMLHdr extends SyncML_ContentHandler {
/**
* Defined in SyncML Representation Protocol, version 1.1 5.1.9. User name.
*
* @var string
*/
var $_locName;
/**
* Defined in SyncML Representation Protocol, version 1.1 5.1.18
*
* @var string
*/
var $_sessionID;
/**
* Defined in SyncML Representation Protocol, version 1.1. Must be 1.0 (0)
* or 1.1 (1).
*
* @var string
*/
var $_version;
/**
* Defined in SyncML Representation Protocol, version 1.1 5.1.12
*
* @var string
*/
var $_msgID;
/**
* Defined in SyncML Representation Protocol, version 1.1 5.1.10
*
* @var string
*/
var $_targetURI;
/**
* Defined in SyncML Representation Protocol, version 1.1 5.1.10, 5.1.20
*
* @var string
*/
var $_sourceURI;
var $_credData;
var $_credFormat;
var $_credType;
var $_maxMsgSize;
function getStateFromSession($sourceURI, $locName, $sessionID)
{
$GLOBALS['backend'] = &new SyncML_Backend_Horde();
// Reload the Horde SessionHandler if necessary.
Horde::setupSessionHandler();
// SyncML protocol does not require the client to send the
// username on any messages but the first. So unfortunately
// we can't use it to make a unique session id.
session_id('syncml' . preg_replace('/[^a-zA-Z0-9]/',
'', $sourceURI . $sessionID));
session_start();
// It would seem multisync does not send the user name once it
// has been authorized. Make sure we have a valid session id.
session_id('syncml' . preg_replace('/[^a-zA-Z0-9]/', '', $sourceURI . $sessionID));
@session_start();
if (!isset($_SESSION['SyncML.state'])) {
// Create a new state if one does not already exist.
$GLOBALS['backend']->logMessage('New session created: '
. session_id(),
__FILE__, __LINE__, PEAR_LOG_DEBUG);
$_SESSION['SyncML.state'] = &new SyncML_State($sourceURI,
$locName,
$sessionID);
} else {
$GLOBALS['backend']->logMessage('Existing session continued: '
. session_id(),
__FILE__, __LINE__, PEAR_LOG_DEBUG);
}
return $_SESSION['SyncML.state'];
}
function startElement($uri, $element, $attrs)
{
parent::startElement($uri, $element, $attrs);
}
function endElement($uri, $element)
{
switch (count($this->_Stack)) {
case 1:
// </SyncHdr></SyncML>
// Find the state.
$state = $this->getStateFromSession($this->_sourceURI, $this->_locName, $this->_sessionID);
$state->setVersion($this->_version);
$state->setMsgID($this->_msgID);
$state->setTargetURI($this->_targetURI);
$state->setPassword($this->_credData);
$state->setWBXML(is_a($this->_output, 'XML_WBXML_Encoder'));
// Store login name in state if not already set
if (empty($state->_locName) && !empty($this->_locName)) {
$state->setLocName($this->_locName);
}
if (!empty($this->_maxMsgSize)) {
$state->setMaxMsgSize($this->_maxMsgSize);
}
$str = 'authorized=' . $state->isAuthorized() .
' version=' . $state->getVersion() .
' msgid=' . $state->getMsgID() .
' source=' . $state->getSourceURI() .
' target=' . $state->getTargetURI() .
' user=' . $state->getLocName() .
' charset=' . NLS::getCharset() .
' wbxml=' . $state->isWBXML()
;
$_SESSION['SyncML.state'] = $state;
$GLOBALS['backend']->logMessage($str, __FILE__, __LINE__, PEAR_LOG_DEBUG);
// Got the state; now write our SyncHdr header.
$this->outputSyncHdr($this->_output);
break;
case 2:
if ($element == 'VerProto') {
// </VerProto></SyncHdr></SyncML>
if (trim($this->_chars) == 'SyncML/1.1') {
$this->_version = 1;
} else {
$this->_version = 0;
}
} elseif ($element == 'SessionID') {
// </SessionID></SyncHdr></SyncML>
$this->_sessionID = trim($this->_chars);
} elseif ($element == 'MsgID') {
// </MsgID></SyncHdr></SyncML>
$this->_msgID = intval(trim($this->_chars));
} elseif ($element == 'Cred') {
// </Cred></SyncHdr></SyncML>
$this->_credData = base64_decode($this->_credData);
$tmp = explode(':', $this->_credData);
$this->_locName = $tmp[0];
$this->_credData = $tmp[1];
Horde::logMessage('SyncML: received credentials for user='
. $this->_locName , 'cred='
. $this->_credData,
__FILE__, __LINE__, PEAR_LOG_DEBUG);
}
break;
case 3:
if ($element == 'LocURI') {
if ($this->_Stack[1] == 'Source') {
// </LocURI></Source></SyncHdr></SyncML>
$this->_sourceURI = trim($this->_chars);
} elseif ($this->_Stack[1] == 'Target') {
// </LocURI></Target></SyncHdr></SyncML>
$this->_targetURI = trim($this->_chars);
}
} elseif ($element == 'LocName') {
if ($this->_Stack[1] == 'Source') {
// </LocName></Source></SyncHdr></SyncML>
$this->_locName = trim($this->_chars);
}
} elseif ($element == 'Data') {
// </Data></Cred></SyncHdr></SyncML>
if ($this->_Stack[1] == 'Cred') {
$this->_credData = trim($this->_chars);
}
} elseif ($element == 'MaxMsgSize') {
// </MaxMsgSize></Meta></SyncHdr></SyncML>
$this->_maxMsgSize = intval($this->_chars);
}
break;
case 4:
if ($this->_Stack[1] == 'Cred') {
if ($element == 'Format') {
// </Format></Meta></Cred></SyncHdr></SyncML>
$this->_credFormat = trim($this->_chars);
} elseif ($element == 'Type') {
// </Type></Meta></Cred></SyncHdr></SyncML>
$this->_credType = trim($this->_chars);
}
}
break;
}
parent::endElement($uri, $element);
}
function outputSyncHdr(&$output)
{
$attrs = array();
$state = &$_SESSION['SyncML.state'];
$uri = $state->getURI();
$uriMeta = $state->getURIMeta();
$output->startElement($uri, 'SyncHdr', $attrs);
$output->startElement($uri, 'VerDTD', $attrs);
$chars = ($this->_version == 1) ? '1.1' : '1.0';
$output->characters($chars);
$output->endElement($uri, 'VerDTD');
$output->startElement($uri, 'VerProto', $attrs);
$chars = ($this->_version == 1) ? 'SyncML/1.1' : 'SyncML/1.0';
$output->characters($chars);
$output->endElement($uri, 'VerProto');
$output->startElement($uri, 'SessionID', $attrs);
$output->characters($this->_sessionID);
$output->endElement($uri, 'SessionID');
$output->startElement($uri, 'MsgID', $attrs);
$output->characters($this->_msgID);
$output->endElement($uri, 'MsgID');
$output->startElement($uri, 'Target', $attrs);
$output->startElement($uri, 'LocURI', $attrs);
$output->characters($this->_sourceURI);
$output->endElement($uri, 'LocURI');
$output->startElement($uri, 'LocName', $attrs);
$output->characters($state->getLocName());
$output->endElement($uri, 'LocName');
$output->endElement($uri, 'Target');
$output->startElement($uri, 'Source', $attrs);
$output->startElement($uri, 'LocURI', $attrs);
$output->characters($this->_targetURI);
$output->endElement($uri, 'LocURI');
$output->endElement($uri, 'Source');
/*
Do not send RespURI Element. It's optional and may be misleading:
$this->_targetURI is data from the client and not guaranteed to be
the correct URL for access. Let the client just stay with the same
URL it has used for the previous request(s).
*/
//$output->startElement($uri, 'RespURI', $attrs);
//$output->characters($this->_targetURI);
//$output->endElement($uri, 'RespURI');
/*
$output->startElement($uri, 'Meta', $attrs);
// Dummy Max MsqSize, this is just put in to make the packet
// work, it is not a real value.
$output->startElement($uriMeta, 'MaxMsgSize', $attrs);
$chars = '50000';
$output->characters($chars);
$output->endElement($uriMeta, 'MaxMsgSize');
// Dummy MaxObjSize, this is just put in to make the packet
// work, it is not a real value.
$output->startElement($uriMeta, 'MaxObjSize', $attrs);
$chars = '4000000';
$output->characters($chars);
$output->endElement($uriMeta, 'MaxObjSize');
$output->endElement($uri, 'Meta');
*/
$output->endElement($uri, 'SyncHdr');
}
function getSourceURI()
{
return $this->_sourceURI;
}
function getLocName()
{
return $this->_locName;
}
function getSessionID()
{
return $this->_sessionID;
}
function getVersion()
{
return $this->_version;
}
function getMsgID()
{
return $this->_msgID;
}
function getTargetURI()
{
return $this->_targetURI;
}
function opaque($o)
{
}
}
/**
* Defined in SyncML Representation Protocol, version 1.1 5.2.3
*
* @package SyncML
*/
class SyncML_SyncMLBody extends SyncML_ContentHandler {
var $_currentCmdID = 1;
var $_currentCommand;
var $_actionCommands = false;
function startElement($uri, $element, $attrs)
{
parent::startElement($uri, $element, $attrs);
switch (count($this->_Stack)) {
case 1:
$state = &$_SESSION['SyncML.state'];
$this->_actionCommands = false; // so far, we have not seen commands that require action from our side
// <SyncML><SyncBody>
$uri = $state->getURI();
$this->_output->startElement($uri, $element, $attrs);
// Right our status about the header.
$status = &new SyncML_Command_Status(($state->isAuthorized()) ?
RESPONSE_AUTHENTICATION_ACCEPTED : RESPONSE_INVALID_CREDENTIALS, 'SyncHdr');
$status->setSourceRef($state->getSourceURI());
$status->setTargetRef($state->getTargetURI());
$status->setCmdRef(0);
$this->_currentCmdID = $status->output($this->_currentCmdID, $this->_output);
break;
case 2:
// <SyncML><SyncBody><[Command]>
$this->_currentCommand = SyncML_Command::factory($element);
$this->_currentCommand->startElement($uri, $element, $attrs);
if ($element != 'Status' && $element != 'Map' && $element != 'Final') {
// We've got to do something! This can't be the last
// packet.
$this->_actionCommands = true;
}
break;
default:
// <SyncML><SyncBody><Command><...>
$this->_currentCommand->startElement($uri, $element, $attrs);
break;
}
}
function endElement($uri, $element)
{
switch (count($this->_Stack)) {
case 1:
// </SyncBody></SyncML>
$state = &$_SESSION['SyncML.state'];
if ($state->hasPendingElements()) {
/* still something to do: don't close session */
$this->_actionCommands = true;
}
$this->_currentCmdID = SyncML_Command_Final::outputFinal(
$this->_currentCmdID,
$this->_output);
$this->_output->endElement($uri, $element);
if (!$this->_actionCommands) {
// This packet did not contain any real actions, just
// status and map. This means we're done. The session
// can be closed and the anchors saved for the next
// sync.
$GLOBALS['backend']->logMessage('sync' . session_id() .
' completed successfully!' .
' Storing Client-TS ' .
$state->_clientAnchorNext,
__FILE__, __LINE__, PEAR_LOG_INFO);
$GLOBALS['backend']->writeSyncSummary(
$state->getSyncIdentifier(),
$state->_clientAnchorNext,
$state->_serverAnchorNext);
$log = $state->getLog();
$s = '';
foreach ($log as $k => $v) {
$s .= " $k=$v";
}
$GLOBALS['backend']->logMessage('Summary:' . $s, __FILE__, __LINE__, PEAR_LOG_INFO);
// Session can be closed here.
session_unset();
session_destroy();
} else {
$GLOBALS['backend']->logMessage('SyncML: return message completed',
__FILE__, __LINE__, PEAR_LOG_DEBUG);
}
break;
case 2:
// </[Command]></SyncBody></SyncML>
$this->_currentCommand->endElement($uri, $element);
$this->_currentCmdID = $this->_currentCommand->output($this->_currentCmdID, $this->_output);
unset($this->_currentCommand);
break;
default:
// </...></[Command]></SyncBody></SyncML>
$this->_currentCommand->endElement($uri, $element);
break;
}
parent::endElement($uri, $element);
}
function characters($str)
{
if (isset($this->_currentCommand)) {
$this->_currentCommand->characters($str);
}
}
}
|