Back to index

d-push  2.0
Public Member Functions | Static Public Member Functions | Static Protected Attributes
FolderSync Class Reference
Inheritance diagram for FolderSync:
Inheritance graph
[legend]
Collaboration diagram for FolderSync:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Handle ($commandCode)
 Handles the FolderSync command.

Static Public Member Functions

static Authenticate ()
 Authenticates the remote user The sent HTTP authentication information is used to on Backend->Logon().
static isUserAuthenticated ()
 Indicates if the user was "authenticated".
static Initialize ()
 Initialize the RequestProcessor.
static HandleRequest ()
 Loads the command handler and processes a command sent from the mobile.

Static Protected Attributes

static $backend
static $deviceManager
static $topCollector
static $decoder
static $encoder
static $userIsAuthenticated

Detailed Description

Definition at line 44 of file foldersync.php.


Member Function Documentation

static RequestProcessor::Authenticate ( ) [static, inherited]

Authenticates the remote user The sent HTTP authentication information is used to on Backend->Logon().

As second step the GET-User verified by Backend->Setup() for permission check Request::GetGETUser() is usually the same as the Request::GetAuthUser(). If the GETUser is different from the AuthUser, the AuthUser MUST HAVE admin permissions on GETUsers data store. Only then the Setup() will be sucessfull. This allows the user 'john' to do operations as user 'joe' if he has sufficient privileges.

public

Returns:
Exceptions:
AuthenticationRequiredException

Definition at line 72 of file requestprocessor.php.

                                          {
        self::$userIsAuthenticated = false;

        $backend = ZPush::GetBackend();
        if($backend->Logon(Request::GetAuthUser(), Request::GetAuthDomain(), Request::GetAuthPassword()) == false)
            throw new AuthenticationRequiredException("Access denied. Username or password incorrect");

        // mark this request as "authenticated"
        self::$userIsAuthenticated = true;

        // check Auth-User's permissions on GETUser's store
        if($backend->Setup(Request::GetGETUser(), true) == false)
            throw new AuthenticationRequiredException(sprintf("Not enough privileges of '%s' to setup for user '%s': Permission denied", Request::GetAuthUser(), Request::GetGETUser()));
    }

Here is the call graph for this function:

Here is the caller graph for this function:

FolderSync::Handle ( commandCode)

Handles the FolderSync command.

Parameters:
int$commandCodepublic
Returns:
boolean

Reimplemented from RequestProcessor.

Definition at line 54 of file foldersync.php.

                                          {
        // Maps serverid -> clientid for items that are received from the PIM
        $map = array();

        // Parse input
        if(!self::$decoder->getElementStartTag(SYNC_FOLDERHIERARCHY_FOLDERSYNC))
            return false;

        if(!self::$decoder->getElementStartTag(SYNC_FOLDERHIERARCHY_SYNCKEY))
            return false;

        $synckey = self::$decoder->getElementContent();

        if(!self::$decoder->getElementEndTag())
            return false;

        $status = SYNC_FSSTATUS_SUCCESS;
        $newsynckey = $synckey;
        try {
            $syncstate = self::$deviceManager->GetStateManager()->GetSyncState($synckey);

            // We will be saving the sync state under 'newsynckey'
            $newsynckey = self::$deviceManager->GetStateManager()->GetNewSyncKey($synckey);
        }
        catch (StateNotFoundException $snfex) {
                $status = SYNC_FSSTATUS_SYNCKEYERROR;
        }
        catch (StateInvalidException $sive) {
                $status = SYNC_FSSTATUS_SYNCKEYERROR;
        }

        // The ChangesWrapper caches all imports in-memory, so we can send a change count
        // before sending the actual data.
        // the HierarchyCache is notified and the changes from the PIM are transmitted to the actual backend
        $changesMem = self::$deviceManager->GetHierarchyChangesWrapper();

        // the hierarchyCache should now fully be initialized - check for changes in the additional folders
        $changesMem->Config(ZPush::GetAdditionalSyncFolders());

        // process incoming changes
        if(self::$decoder->getElementStartTag(SYNC_FOLDERHIERARCHY_CHANGES)) {
            // Ignore <Count> if present
            if(self::$decoder->getElementStartTag(SYNC_FOLDERHIERARCHY_COUNT)) {
                self::$decoder->getElementContent();
                if(!self::$decoder->getElementEndTag())
                    return false;
            }

            // Process the changes (either <Add>, <Modify>, or <Remove>)
            $element = self::$decoder->getElement();

            if($element[EN_TYPE] != EN_TYPE_STARTTAG)
                return false;

            $importer = false;
            while(1) {
                $folder = new SyncFolder();
                if(!$folder->Decode(self::$decoder))
                    break;

                try {
                    if ($status == SYNC_FSSTATUS_SUCCESS && !$importer) {
                        // Configure the backends importer with last state
                        $importer = self::$backend->GetImporter();
                        $importer->Config($syncstate);
                        // the messages from the PIM will be forwarded to the backend
                        $changesMem->forwardImporter($importer);
                    }

                    if ($status == SYNC_FSSTATUS_SUCCESS) {
                        switch($element[EN_TAG]) {
                            case SYNC_ADD:
                            case SYNC_MODIFY:
                                $serverid = $changesMem->ImportFolderChange($folder);
                                break;
                            case SYNC_REMOVE:
                                $serverid = $changesMem->ImportFolderDeletion($folder);
                                break;
                        }

                        // TODO what does $map??
                        if($serverid)
                            $map[$serverid] = $folder->clientid;
                    }
                    else {
                        ZLog::Write(LOGLEVEL_WARN, sprintf("Request->HandleFolderSync(): ignoring incoming folderchange for folder '%s' as status indicates problem.", $folder->displayname));
                        self::$topCollector->AnnounceInformation("Incoming change ignored", true);
                    }
                }
                catch (StatusException $stex) {
                   $status = $stex->getCode();
                }
            }

            if(!self::$decoder->getElementEndTag())
                return false;
        }
        // no incoming changes
        else {
            // check for a potential process loop like described in Issue ZP-5
            if ($synckey != "0" && self::$deviceManager->IsHierarchyFullResyncRequired())
                $status = SYNC_FSSTATUS_SYNCKEYERROR;
                self::$deviceManager->AnnounceProcessStatus(false, $status);
        }

        if(!self::$decoder->getElementEndTag())
            return false;

        // We have processed incoming foldersync requests, now send the PIM
        // our changes

        // Output our WBXML reply now
        self::$encoder->StartWBXML();

        self::$encoder->startTag(SYNC_FOLDERHIERARCHY_FOLDERSYNC);
        {
            if ($status == SYNC_FSSTATUS_SUCCESS) {
                try {
                    // do nothing if this is an invalid device id (like the 'validate' Androids internal client sends)
                    if (!Request::IsValidDeviceID())
                        throw new StatusException(sprintf("Request::IsValidDeviceID() indicated that '%s' is not a valid device id", Request::GetDeviceID()), SYNC_FSSTATUS_SERVERERROR);

                    // Changes from backend are sent to the MemImporter and processed for the HierarchyCache.
                    // The state which is saved is from the backend, as the MemImporter is only a proxy.
                    $exporter = self::$backend->GetExporter();

                    $exporter->Config($syncstate);
                    $exporter->InitializeExporter($changesMem);

                    // Stream all changes to the ImportExportChangesMem
                    while(is_array($exporter->Synchronize()));

                    // get the new state from the backend
                    $newsyncstate = (isset($exporter))?$exporter->GetState():"";
                }
                catch (StatusException $stex) {
                    if ($stex->getCode() == SYNC_FSSTATUS_CODEUNKNOWN)
                        $status = SYNC_FSSTATUS_SYNCKEYERROR;
                    else
                        $status = $stex->getCode();
                }
            }

            self::$encoder->startTag(SYNC_FOLDERHIERARCHY_STATUS);
            self::$encoder->content($status);
            self::$encoder->endTag();

            if ($status == SYNC_FSSTATUS_SUCCESS) {
                self::$encoder->startTag(SYNC_FOLDERHIERARCHY_SYNCKEY);
                $synckey = ($changesMem->IsStateChanged()) ? $newsynckey : $synckey;
                self::$encoder->content($synckey);
                self::$encoder->endTag();

                // Stream folders directly to the PDA
                $streamimporter = new ImportChangesStream(self::$encoder, false);
                $changesMem->InitializeExporter($streamimporter);
                $changeCount = $changesMem->GetChangeCount();

                self::$encoder->startTag(SYNC_FOLDERHIERARCHY_CHANGES);
                {
                    self::$encoder->startTag(SYNC_FOLDERHIERARCHY_COUNT);
                    self::$encoder->content($changeCount);
                    self::$encoder->endTag();
                    while($changesMem->Synchronize());
                }
                self::$encoder->endTag();
                self::$topCollector->AnnounceInformation(sprintf("Outgoing %d folders",$changeCount), true);

                // everything fine, save the sync state for the next time
                if ($synckey == $newsynckey)
                    self::$deviceManager->GetStateManager()->SetSyncState($newsynckey, $newsyncstate);
            }
        }
        self::$encoder->endTag();

        return true;
    }

Here is the call graph for this function:

static RequestProcessor::HandleRequest ( ) [static, inherited]

Loads the command handler and processes a command sent from the mobile.

public

Returns:
boolean

Definition at line 122 of file requestprocessor.php.

                                           {
        $handler = ZPush::GetRequestHandlerForCommand(Request::GetCommandCode());

        // TODO handle WBXML exceptions here and print stack
        return $handler->Handle(Request::GetCommandCode());
    }

Here is the call graph for this function:

static RequestProcessor::Initialize ( ) [static, inherited]

Initialize the RequestProcessor.

public

Returns:

Definition at line 105 of file requestprocessor.php.

                                        {
        self::$backend = ZPush::GetBackend();
        self::$deviceManager = ZPush::GetDeviceManager();
        self::$topCollector = ZPush::GetTopCollector();

        if (!ZPush::CommandNeedsPlainInput(Request::GetCommandCode()))
            self::$decoder = new WBXMLDecoder(Request::GetInputStream());

        self::$encoder = new WBXMLEncoder(Request::GetOutputStream());
    }

Here is the call graph for this function:

static RequestProcessor::isUserAuthenticated ( ) [static, inherited]

Indicates if the user was "authenticated".

public

Returns:
boolean

Definition at line 93 of file requestprocessor.php.

                                                 {
        if (!isset(self::$userIsAuthenticated))
            return false;
        return self::$userIsAuthenticated;
    }

Here is the caller graph for this function:


Member Data Documentation

RequestProcessor::$backend [static, protected, inherited]

Definition at line 52 of file requestprocessor.php.

RequestProcessor::$decoder [static, protected, inherited]

Definition at line 55 of file requestprocessor.php.

RequestProcessor::$deviceManager [static, protected, inherited]

Definition at line 53 of file requestprocessor.php.

RequestProcessor::$encoder [static, protected, inherited]

Definition at line 56 of file requestprocessor.php.

RequestProcessor::$topCollector [static, protected, inherited]

Definition at line 54 of file requestprocessor.php.

RequestProcessor::$userIsAuthenticated [static, protected, inherited]

Definition at line 57 of file requestprocessor.php.


The documentation for this class was generated from the following file: