[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/SyncML/Command/ -> Alert.php (source)

   1  <?php
   2  
   3  include_once  'SyncML/Command.php';
   4  
   5  /**
   6   * The SyncML_Alert class provides a SyncML implementation of
   7   * the Alert command as defined in SyncML Representation Protocol,
   8   * version 1.1 5.5.2.
   9   *
  10   * $Horde: framework/SyncML/SyncML/Command/Alert.php,v 1.18.10.9 2006/01/31 17:55:31 jan Exp $
  11   *
  12   * Copyright 2003-2006 Anthony Mills <amills@pyramid6.com>
  13   *
  14   * See the enclosed file COPYING for license information (LGPL). If you
  15   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  16   *
  17   * @author  Anthony Mills <amills@pyramid6.com>
  18   * @since   Horde 3.0
  19   * @package SyncML
  20   */
  21  class SyncML_Command_Alert extends SyncML_Command {
  22  
  23      /**
  24       * @var integer
  25       */
  26      var $_alert;
  27  
  28      /**
  29       * @var string
  30       */
  31      var $_sourceLocURI;
  32  
  33      /**
  34       * @var string
  35       */
  36      var $_targetLocURI;
  37  
  38      /**
  39       * @var string
  40       */
  41      var $_metaAnchorNext;
  42  
  43      /**
  44       * @var integer
  45       */
  46      var $_metaAnchorLast;
  47  
  48      /**
  49       * Creates a new instance of Alert.
  50       */
  51      function SyncML_Command_Alert($alert = null)
  52      {
  53          if ($alert != null) {
  54              $this->_alert = $alert;
  55          }
  56      }
  57  
  58      function output($currentCmdID, &$output)
  59      {
  60          global $backend;
  61  
  62          $attrs = array();
  63          $state = &$_SESSION['SyncML.state'];
  64  
  65          // Handle unauthorized first.
  66          if (!$state->isAuthorized()) {
  67              $status = &new SyncML_Command_Status(RESPONSE_INVALID_CREDENTIALS, 'Alert');
  68              $status->setCmdRef($this->_cmdID);
  69              $currentCmdID = $status->output($currentCmdID, $output);
  70              return $currentCmdID;
  71          }
  72  
  73          $database = $this->_targetLocURI;
  74  
  75          // Store client's Next Anchor in State.  After successful sync
  76          // this is then written to persistence for negotiation of
  77          // further syncs.
  78          $state->setClientAnchorNext($database, $this->_metaAnchorNext);
  79  
  80          $info = $backend->getSyncSummary($state->getSyncIdentifier(),
  81                                           $this->_targetLocURI);
  82  
  83          if (is_a($info, 'DataTreeObject')) {
  84              $x = $info->get('ClientAnchor');
  85              $clientlast = $x[$database];
  86              $backend->logMessage(sprintf('previous sync found for database: %s; client-ts: %s',
  87                                           $database,
  88                                           $clientlast),
  89                                   __FILE__, __LINE__, PEAR_LOG_DEBUG);
  90              $x = $info->get('ServerAnchor');
  91              $state->setServerAnchorLast($database, $x[$database]);
  92          } else {
  93              $backend->logMessage(sprintf('SyncML: No info about previous syncs found for id %s and database %s',
  94                                           $state->getSyncIdentifier(),
  95                                           $database),
  96                                   __FILE__, __LINE__, PEAR_LOG_DEBUG);
  97              $clientlast = 0;
  98              $state->setServerAnchorLast($database, 0);
  99          }
 100  
 101          // Set Server Anchor for this sync to current time.
 102          $state->setServerAnchorNext($database, time());
 103          if (($clientlast || $clientlast === '0')
 104                   && $clientlast == $this->_metaAnchorLast) {
 105              // Last Sync Anchor matches, TwoWaySync will do.
 106              $code = RESPONSE_OK;
 107              $backend->logMessage("SyncML: Anchor match, TwoWaySync since "
 108                                  . $clientlast,
 109                                 __FILE__, __LINE__, PEAR_LOG_DEBUG);
 110          } else {
 111              if ($clientlast) {
 112                  $backend->logMessage('client requested sync with anchor ts ' .
 113                                       $this->_metaAnchorLast . ' but server ' .
 114                                       'has timestamp' . $clientlast .
 115                                       ' on file',
 116                                       __FILE__, __LINE__, PEAR_LOG_INFO);
 117              }
 118              $backend->logMessage("SyncML: Anchor mismatch, enforcing SlowSync",
 119                                   __FILE__, __LINE__, PEAR_LOG_DEBUG);
 120              $clientlast = 0;
 121              $state->setServerAnchorLast($database, 0);
 122              if ($this->_alert == 201) {
 123                  /* Slowsync requested from client anyway: just acknowledge. */
 124                  $code = RESPONSE_OK;
 125              } else {
 126                  /* Mismatch, enforce slow sync. */
 127                  $this->_alert = 201;
 128                  $code = 508;
 129              }
 130          }
 131  
 132          $status = &new SyncML_Command_Status($code, 'Alert');
 133          $status->setCmdRef($this->_cmdID);
 134          if ($this->_sourceLocURI != null) {
 135              $status->setSourceRef($this->_sourceLocURI);
 136          }
 137          if ($this->_targetLocURI != null) {
 138              $status->setTargetRef($this->_targetLocURI);
 139          }
 140  
 141          // Mirror Next Anchor from client back to client.
 142          if (isset($this->_metaAnchorNext)) {
 143              $status->setItemDataAnchorNext($this->_metaAnchorNext);
 144          }
 145  
 146          // Mirror Last Anchor from client back to client.
 147          if (isset($this->_metaAnchorLast)) {
 148              $status->setItemDataAnchorLast($this->_metaAnchorLast);
 149          }
 150  
 151          $currentCmdID = $status->output($currentCmdID, $output);
 152  
 153          if ($state->isAuthorized()) {
 154              $output->startElement($state->getURI(), 'Alert', $attrs);
 155  
 156              $output->startElement($state->getURI(), 'CmdID', $attrs);
 157              $chars = $currentCmdID;
 158              $output->characters($chars);
 159              $output->endElement($state->getURI(), 'CmdID');
 160  
 161              $output->startElement($state->getURI(), 'Data', $attrs);
 162              $chars = $this->_alert;
 163              $output->characters($chars);
 164              $output->endElement($state->getURI(), 'Data');
 165  
 166              $output->startElement($state->getURI(), 'Item', $attrs);
 167  
 168              if ($this->_sourceLocURI != null) {
 169                  $output->startElement($state->getURI(), 'Target', $attrs);
 170                  $output->startElement($state->getURI(), 'LocURI', $attrs);
 171                  $chars = $this->_sourceLocURI;
 172                  $output->characters($chars);
 173                  $output->endElement($state->getURI(), 'LocURI');
 174                  $output->endElement($state->getURI(), 'Target');
 175              }
 176  
 177              if ($this->_targetLocURI != null) {
 178                  $output->startElement($state->getURI(), 'Source', $attrs);
 179                  $output->startElement($state->getURI(), 'LocURI', $attrs);
 180                  $chars = $this->_targetLocURI;
 181                  $output->characters($chars);
 182                  $output->endElement($state->getURI(), 'LocURI');
 183                  $output->endElement($state->getURI(), 'Source');
 184              }
 185  
 186              $output->startElement($state->getURI(), 'Meta', $attrs);
 187  
 188              $output->startElement($state->getURIMeta(), 'Anchor', $attrs);
 189  
 190              $output->startElement($state->getURIMeta(), 'Last', $attrs);
 191              $chars = $state->getServerAnchorLast($database);
 192              $output->characters($chars);
 193              $output->endElement($state->getURIMeta(), 'Last');
 194  
 195              $output->startElement($state->getURIMeta(), 'Next', $attrs);
 196              $chars = $state->getServerAnchorNext($database);
 197              $output->characters($chars);
 198              $output->endElement($state->getURIMeta(), 'Next');
 199  
 200              $output->endElement($state->getURIMeta(), 'Anchor');
 201              $output->endElement($state->getURI(), 'Meta');
 202              $output->endElement($state->getURI(), 'Item');
 203              $output->endElement($state->getURI(), 'Alert');
 204  
 205              $currentCmdID++;
 206          }
 207  
 208          return $currentCmdID;
 209      }
 210  
 211      /**
 212       * Setter for property sourceURI.
 213       *
 214       * @param string $sourceURI  New value of property sourceURI.
 215       */
 216      function setSourceLocURI($sourceURI)
 217      {
 218          $this->_sourceURI = $sourceURI;
 219      }
 220  
 221      function getTargetLocURI()
 222      {
 223          return $this->_targetURI;
 224      }
 225  
 226      /**
 227       * Setter for property targetURI.
 228       *
 229       * @param string $targetURI  New value of property targetURI.
 230       */
 231      function setTargetURI($targetURI)
 232      {
 233          $this->_targetURI = $targetURI;
 234      }
 235  
 236      function startElement($uri, $element, $attrs)
 237      {
 238          parent::startElement($uri, $element, $attrs);
 239  
 240      }
 241  
 242      function endElement($uri, $element)
 243      {
 244          global $backend;
 245  
 246          switch (count($this->_Stack)) {
 247          case 1:
 248              $state = &$_SESSION['SyncML.state'];
 249              if ($this->_targetLocURI != 'tasks'
 250                  && $this->_targetLocURI != 'calendar'
 251                  && $this->_targetLocURI != 'notes'
 252                  && $this->_targetLocURI != 'contacts') {
 253                  $backend->logMessage('Error! Invalid database: '
 254                                       . $this->_targetLocURI
 255                                       . '. Only tasks, calendar, notes,'
 256                                       . ' and contacts allowed!',
 257                                       __FILE__, __LINE__, PEAR_LOG_ERR);
 258                  die("error: invalid database!");
 259              }
 260  
 261              $backend->logMessage('looking for sync for ' .
 262                                   $this->_targetLocURI,
 263                                   __FILE__, __LINE__, PEAR_LOG_DEBUG);
 264              $sync = & $state->getSync($this->_targetLocURI);
 265  
 266              if (!$sync) {
 267                  $backend->logMessage('Create new sync for ' .
 268                                       $this->_targetLocURI,
 269                                       __FILE__, __LINE__, PEAR_LOG_DEBUG);
 270  
 271                  $sync = &new SyncML_Sync($this->_alert,
 272                                           $this->_targetLocURI,
 273                                           $this->_sourceLocURI);
 274                  $state->setSync($this->_targetLocURI, $sync);
 275              }
 276              break;
 277  
 278          case 2:
 279              if ($element == 'Data') {
 280                  $this->_alert = intval(trim($this->_chars));
 281              }
 282              break;
 283  
 284          case 4:
 285              if ($element == 'LocURI') {
 286                  if ($this->_Stack[2] == 'Source') {
 287                      $this->_sourceLocURI = trim($this->_chars);
 288                  } elseif ($this->_Stack[2] == 'Target') {
 289                      $this->_targetLocURI = basename(preg_replace('/\?.*$/', '', trim($this->_chars)));
 290                  }
 291              }
 292              break;
 293  
 294          case 5:
 295              if ($element == 'Next') {
 296                  $this->_metaAnchorNext = trim($this->_chars);
 297              } elseif ($element == 'Last') {
 298                  $this->_metaAnchorLast = trim($this->_chars);
 299              }
 300              break;
 301          }
 302  
 303          parent::endElement($uri, $element);
 304      }
 305  
 306      function getAlert()
 307      {
 308          return $this->_alert;
 309      }
 310  
 311      function setAlet($alert)
 312      {
 313          $this->_alert = $alert;
 314      }
 315  
 316  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7