Back to index

d-push  2.0
getitemestimate.php
Go to the documentation of this file.
00001 <?php
00002 /***********************************************
00003 * File      :   getitemestimate.php
00004 * Project   :   Z-Push
00005 * Descr     :   Provides the GETITEMESTIMATE command
00006 *
00007 * Created   :   16.02.2012
00008 *
00009 * Copyright 2007 - 2012 Zarafa Deutschland GmbH
00010 *
00011 * This program is free software: you can redistribute it and/or modify
00012 * it under the terms of the GNU Affero General Public License, version 3,
00013 * as published by the Free Software Foundation with the following additional
00014 * term according to sec. 7:
00015 *
00016 * According to sec. 7 of the GNU Affero General Public License, version 3,
00017 * the terms of the AGPL are supplemented with the following terms:
00018 *
00019 * "Zarafa" is a registered trademark of Zarafa B.V.
00020 * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
00021 * The licensing of the Program under the AGPL does not imply a trademark license.
00022 * Therefore any rights, title and interest in our trademarks remain entirely with us.
00023 *
00024 * However, if you propagate an unmodified version of the Program you are
00025 * allowed to use the term "Z-Push" to indicate that you distribute the Program.
00026 * Furthermore you may use our trademarks where it is necessary to indicate
00027 * the intended purpose of a product or service provided you use it in accordance
00028 * with honest practices in industrial or commercial matters.
00029 * If you want to propagate modified versions of the Program under the name "Z-Push",
00030 * you may only do so if you have a written permission by Zarafa Deutschland GmbH
00031 * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
00032 *
00033 * This program is distributed in the hope that it will be useful,
00034 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00035 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00036 * GNU Affero General Public License for more details.
00037 *
00038 * You should have received a copy of the GNU Affero General Public License
00039 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00040 *
00041 * Consult LICENSE file for details
00042 ************************************************/
00043 
00044 class GetItemEstimate extends RequestProcessor {
00045 
00056     public function Handle($commandCode) {
00057         $sc = new SyncCollections();
00058 
00059         if(!self::$decoder->getElementStartTag(SYNC_GETITEMESTIMATE_GETITEMESTIMATE))
00060             return false;
00061 
00062         if(!self::$decoder->getElementStartTag(SYNC_GETITEMESTIMATE_FOLDERS))
00063             return false;
00064 
00065         while(self::$decoder->getElementStartTag(SYNC_GETITEMESTIMATE_FOLDER)) {
00066             $spa = new SyncParameters();
00067             $spastatus = false;
00068 
00069             if (Request::GetProtocolVersion() >= 14.0) {
00070                 if(self::$decoder->getElementStartTag(SYNC_SYNCKEY)) {
00071                     try {
00072                         $spa->SetSyncKey(self::$decoder->getElementContent());
00073                     }
00074                     catch (StateInvalidException $siex) {
00075                         $spastatus = SYNC_GETITEMESTSTATUS_SYNCSTATENOTPRIMED;
00076                     }
00077 
00078                     if(!self::$decoder->getElementEndTag())
00079                         return false;
00080                 }
00081 
00082                 if(self::$decoder->getElementStartTag(SYNC_GETITEMESTIMATE_FOLDERID)) {
00083                     $spa->SetFolderId( self::$decoder->getElementContent());
00084 
00085                     if(!self::$decoder->getElementEndTag())
00086                         return false;
00087                 }
00088 
00089                 // conversation mode requested
00090                 if(self::$decoder->getElementStartTag(SYNC_CONVERSATIONMODE)) {
00091                     $spa->SetConversationMode(true);
00092                     if(($conversationmode = self::$decoder->getElementContent()) !== false) {
00093                         $spa->SetConversationMode((boolean)$conversationmode);
00094                         if(!self::$decoder->getElementEndTag())
00095                             return false;
00096                     }
00097                 }
00098 
00099                 if(self::$decoder->getElementStartTag(SYNC_OPTIONS)) {
00100                     while(1) {
00101                         if(self::$decoder->getElementStartTag(SYNC_FILTERTYPE)) {
00102                             $spa->SetFilterType(self::$decoder->getElementContent());
00103                             if(!self::$decoder->getElementEndTag())
00104                                 return false;
00105                         }
00106 
00107                         if(self::$decoder->getElementStartTag(SYNC_FOLDERTYPE)) {
00108                             $spa->SetContentClass(self::$decoder->getElementContent());
00109                             if(!self::$decoder->getElementEndTag())
00110                                 return false;
00111                         }
00112 
00113                         if(self::$decoder->getElementStartTag(SYNC_MAXITEMS)) {
00114                             $spa->SetWindowSize($maxitems = self::$decoder->getElementContent());
00115                             if(!self::$decoder->getElementEndTag())
00116                                 return false;
00117                         }
00118 
00119                         $e = self::$decoder->peek();
00120                         if($e[EN_TYPE] == EN_TYPE_ENDTAG) {
00121                             self::$decoder->getElementEndTag();
00122                             break;
00123                         }
00124                     }
00125                 }
00126             }
00127             else {
00128                 //get items estimate does not necessarily send the folder type
00129                 if(self::$decoder->getElementStartTag(SYNC_GETITEMESTIMATE_FOLDERTYPE)) {
00130                     $spa->SetContentClass(self::$decoder->getElementContent());
00131 
00132                     if(!self::$decoder->getElementEndTag())
00133                         return false;
00134                 }
00135 
00136                 if(self::$decoder->getElementStartTag(SYNC_GETITEMESTIMATE_FOLDERID)) {
00137                     $spa->SetFolderId(self::$decoder->getElementContent());
00138 
00139                     if(!self::$decoder->getElementEndTag())
00140                         return false;
00141                 }
00142 
00143                 if(!self::$decoder->getElementStartTag(SYNC_FILTERTYPE))
00144                     return false;
00145 
00146                 $spa->SetFilterType(self::$decoder->getElementContent());
00147 
00148                 if(!self::$decoder->getElementEndTag())
00149                     return false;
00150 
00151                 if(!self::$decoder->getElementStartTag(SYNC_SYNCKEY))
00152                     return false;
00153 
00154                 try {
00155                     $spa->SetSyncKey(self::$decoder->getElementContent());
00156                 }
00157                 catch (StateInvalidException $siex) {
00158                     $spastatus = SYNC_GETITEMESTSTATUS_SYNCSTATENOTPRIMED;
00159                 }
00160 
00161                 if(!self::$decoder->getElementEndTag())
00162                     return false;
00163             }
00164 
00165             if(!self::$decoder->getElementEndTag())
00166                 return false; //SYNC_GETITEMESTIMATE_FOLDER
00167 
00168             // Process folder data
00169 
00170             //In AS 14 request only collectionid is sent, without class
00171             if (! $spa->HasContentClass() && $spa->HasFolderId())
00172                 $spa->SetContentClass(self::$deviceManager->GetFolderClassFromCacheByID($spa->GetFolderId()));
00173 
00174             // compatibility mode AS 1.0 - get folderid which was sent during GetHierarchy()
00175             if (! $spa->HasFolderId() && $spa->HasContentClass()) {
00176                 $spa->SetFolderId(self::$deviceManager->GetFolderIdFromCacheByClass($spa->GetContentClass()));
00177             }
00178 
00179             // Add collection to SC and load state
00180             $sc->AddCollection($spa);
00181             if ($spastatus) {
00182                 // the CPO has a folder id now, so we can set the status
00183                 $sc->AddParameter($spa, "status", $spastatus);
00184             }
00185             else {
00186                 try {
00187                     $sc->AddParameter($spa, "state", self::$deviceManager->GetStateManager()->GetSyncState($spa->GetSyncKey()));
00188 
00189                     // if this is an additional folder the backend has to be setup correctly
00190                     if (!self::$backend->Setup(ZPush::GetAdditionalSyncFolderStore($spa->GetFolderId())))
00191                         throw new StatusException(sprintf("HandleGetItemEstimate() could not Setup() the backend for folder id '%s'", $spa->GetFolderId()), SYNC_GETITEMESTSTATUS_COLLECTIONINVALID);
00192                 }
00193                 catch (StateNotFoundException $snfex) {
00194                     // ok, the key is invalid. Question is, if the hierarchycache is still ok
00195                     //if not, we have to issue SYNC_GETITEMESTSTATUS_COLLECTIONINVALID which triggers a FolderSync
00196                     try {
00197                         self::$deviceManager->GetFolderClassFromCacheByID($spa->GetFolderId());
00198                         // we got here, so the HierarchyCache is ok
00199                         $sc->AddParameter($spa, "status", SYNC_GETITEMESTSTATUS_SYNCKKEYINVALID);
00200                     }
00201                     catch (NoHierarchyCacheAvailableException $nhca) {
00202                         $sc->AddParameter($spa, "status", SYNC_GETITEMESTSTATUS_COLLECTIONINVALID);
00203                     }
00204 
00205                     self::$topCollector->AnnounceInformation("StateNotFoundException ". $sc->GetParameter($spa, "status"), true);
00206                 }
00207                 catch (StatusException $stex) {
00208                     if ($stex->getCode() == SYNC_GETITEMESTSTATUS_COLLECTIONINVALID)
00209                         $sc->AddParameter($spa, "status", SYNC_GETITEMESTSTATUS_COLLECTIONINVALID);
00210                     else
00211                         $sc->AddParameter($spa, "status", SYNC_GETITEMESTSTATUS_SYNCSTATENOTPRIMED);
00212                     self::$topCollector->AnnounceInformation("StatusException ". $sc->GetParameter($spa, "status"), true);
00213                 }
00214             }
00215         }
00216         if(!self::$decoder->getElementEndTag())
00217             return false; //SYNC_GETITEMESTIMATE_FOLDERS
00218 
00219         if(!self::$decoder->getElementEndTag())
00220             return false; //SYNC_GETITEMESTIMATE_GETITEMESTIMATE
00221 
00222         self::$encoder->startWBXML();
00223         self::$encoder->startTag(SYNC_GETITEMESTIMATE_GETITEMESTIMATE);
00224         {
00225             $status = SYNC_GETITEMESTSTATUS_SUCCESS;
00226             // look for changes in all collections
00227 
00228             try {
00229                 $sc->CountChanges();
00230             }
00231             catch (StatusException $ste) {
00232                 $status = SYNC_GETITEMESTSTATUS_COLLECTIONINVALID;
00233             }
00234             $changes = $sc->GetChangedFolderIds();
00235 
00236             foreach($sc as $folderid => $spa) {
00237                 self::$encoder->startTag(SYNC_GETITEMESTIMATE_RESPONSE);
00238                 {
00239                     if ($sc->GetParameter($spa, "status"))
00240                         $status = $sc->GetParameter($spa, "status");
00241 
00242                     self::$encoder->startTag(SYNC_GETITEMESTIMATE_STATUS);
00243                     self::$encoder->content($status);
00244                     self::$encoder->endTag();
00245 
00246                     self::$encoder->startTag(SYNC_GETITEMESTIMATE_FOLDER);
00247                     {
00248                         self::$encoder->startTag(SYNC_GETITEMESTIMATE_FOLDERTYPE);
00249                         self::$encoder->content($spa->GetContentClass());
00250                         self::$encoder->endTag();
00251 
00252                         self::$encoder->startTag(SYNC_GETITEMESTIMATE_FOLDERID);
00253                         self::$encoder->content($spa->GetFolderId());
00254                         self::$encoder->endTag();
00255 
00256                         if (isset($changes[$folderid]) && $changes[$folderid] !== false) {
00257                             self::$encoder->startTag(SYNC_GETITEMESTIMATE_ESTIMATE);
00258                             self::$encoder->content($changes[$folderid]);
00259                             self::$encoder->endTag();
00260 
00261                             if ($changes[$folderid] > 0)
00262                                 self::$topCollector->AnnounceInformation(sprintf("%s %d changes", $spa->GetContentClass(), $changes[$folderid]), true);
00263                         }
00264                     }
00265                     self::$encoder->endTag();
00266                 }
00267                 self::$encoder->endTag();
00268             }
00269         }
00270         self::$encoder->endTag();
00271 
00272         return true;
00273     }
00274 }
00275 ?>