This file is indexed.

/usr/share/z-push/lib/request/ping.php is in z-push-common 2.3.8-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
<?php
/***********************************************
* File      :   ping.php
* Project   :   Z-Push
* Descr     :   Provides the PING command
*
* Created   :   16.02.2012
*
* Copyright 2007 - 2016 Zarafa Deutschland GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* Consult LICENSE file for details
************************************************/

class Ping extends RequestProcessor {

    /**
     * Handles the Ping command
     *
     * @param int       $commandCode
     *
     * @access public
     * @return boolean
     */
    public function Handle($commandCode) {
        $interval = (defined('PING_INTERVAL') && PING_INTERVAL > 0) ? PING_INTERVAL : 30;
        $pingstatus = false;
        $fakechanges = array();
        $foundchanges = false;

        // Contains all requested folders (containers)
        $sc = new SyncCollections();

        // read from stream to see if the symc params are being sent
        $params_present = self::$decoder->getElementStartTag(SYNC_PING_PING);

        // Load all collections - do load states, check permissions and allow unconfirmed states
        try {
            $sc->LoadAllCollections(true, true, true, true, false);
        }
        catch (StateInvalidException $siex) {
            // if no params are present, indicate to send params, else do hierarchy sync
            if (!$params_present) {
                $pingstatus = SYNC_PINGSTATUS_FAILINGPARAMS;
                self::$topCollector->AnnounceInformation("StateInvalidException: require PingParameters", true);
            }
            elseif (self::$deviceManager->IsHierarchySyncRequired()) {
                // we could be in a looping  - see LoopDetection->ProcessLoopDetectionIsHierarchySyncAdvised()
                $pingstatus = SYNC_PINGSTATUS_FOLDERHIERSYNCREQUIRED;
                self::$topCollector->AnnounceInformation("Potential loop detection: require HierarchySync", true);
            }
            else {
                // we do not have a ping status for this, but SyncCollections should have generated fake changes for the folders which are broken
                $fakechanges = $sc->GetChangedFolderIds();
                $foundchanges = true;

                self::$topCollector->AnnounceInformation("StateInvalidException: force sync", true);
            }
        }
        catch (StatusException $stex) {
            $pingstatus = SYNC_PINGSTATUS_FOLDERHIERSYNCREQUIRED;
            self::$topCollector->AnnounceInformation("StatusException: require HierarchySync", true);
        }

        ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandlePing(): reference PolicyKey for PING: %s", $sc->GetReferencePolicyKey()));

        // receive PING initialization data
        if($params_present) {
            self::$topCollector->AnnounceInformation("Processing PING data");
            ZLog::Write(LOGLEVEL_DEBUG, "HandlePing(): initialization data received");

            if(self::$decoder->getElementStartTag(SYNC_PING_LIFETIME)) {
                $sc->SetLifetime(self::$decoder->getElementContent());
                self::$decoder->getElementEndTag();
            }

            if(($el = self::$decoder->getElementStartTag(SYNC_PING_FOLDERS)) && $el[EN_FLAGS] & EN_FLAGS_CONTENT) {
                // cache requested (pingable) folderids
                $pingable = array();

                while(self::$decoder->getElementStartTag(SYNC_PING_FOLDER)) {
                    WBXMLDecoder::ResetInWhile("pingFolder");
                    while(WBXMLDecoder::InWhile("pingFolder")) {
                        if(self::$decoder->getElementStartTag(SYNC_PING_SERVERENTRYID)) {
                            $folderid = self::$decoder->getElementContent();
                            self::$decoder->getElementEndTag();
                        }
                        if(self::$decoder->getElementStartTag(SYNC_PING_FOLDERTYPE)) {
                            $class = self::$decoder->getElementContent();
                            self::$decoder->getElementEndTag();
                        }

                        $e = self::$decoder->peek();
                        if($e[EN_TYPE] == EN_TYPE_ENDTAG) {
                            self::$decoder->getElementEndTag();
                            break;
                        }
                    }

                    $spa = $sc->GetCollection($folderid);
                    if (! $spa) {
                        // The requested collection is not synchronized.
                        // check if the HierarchyCache is available, if not, trigger a HierarchySync
                        try {
                            self::$deviceManager->GetFolderClassFromCacheByID($folderid);
                            // ZP-907: ignore all folders with SYNC_FOLDER_TYPE_UNKNOWN
                            if (self::$deviceManager->GetFolderTypeFromCacheById($folderid) == SYNC_FOLDER_TYPE_UNKNOWN) {
                                ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandlePing(): ignoring folder id '%s' as it's of type UNKNOWN ", $folderid));
                                continue;
                            }
                        }
                        catch (NoHierarchyCacheAvailableException $nhca) {
                            ZLog::Write(LOGLEVEL_INFO, sprintf("HandlePing(): unknown collection '%s', triggering HierarchySync", $folderid));
                            $pingstatus = SYNC_PINGSTATUS_FOLDERHIERSYNCREQUIRED;
                        }

                        // Trigger a Sync request because then the device will be forced to resync this folder.
                        $fakechanges[$folderid] = 1;
                        $foundchanges = true;
                    }
                    else if ($this->isClassValid($class, $spa)) {
                        $pingable[] = $folderid;
                        ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandlePing(): using saved sync state for '%s' id '%s'", $spa->GetContentClass(), $folderid));
                    }
                }
                if(!self::$decoder->getElementEndTag())
                    return false;

                // update pingable flags
                foreach ($sc as $folderid => $spa) {
                    // if the folderid is in $pingable, we should ping it, else remove the flag
                    if (in_array($folderid, $pingable)) {
                        $spa->SetPingableFlag(true);
                    }
                    else  {
                        $spa->DelPingableFlag();
                    }
                }
            }
            if(!self::$decoder->getElementEndTag())
                return false;

            if(!$this->lifetimeBetweenBound($sc->GetLifetime())){
                $pingstatus = SYNC_PINGSTATUS_HBOUTOFRANGE;
                ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandlePing(): ping lifetime not between bound (higher bound:'%d' lower bound:'%d' current lifetime:'%d'. Returning SYNC_PINGSTATUS_HBOUTOFRANGE.", PING_HIGHER_BOUND_LIFETIME, PING_LOWER_BOUND_LIFETIME, $sc->GetLifetime()));
            }
            // save changed data
            foreach ($sc as $folderid => $spa)
                $sc->SaveCollection($spa);
        } // END SYNC_PING_PING
        else {
            // if no ping initialization data was sent, we check if we have pingable folders
            // if not, we indicate that there is nothing to do.
            if (! $sc->PingableFolders()) {
                $pingstatus = SYNC_PINGSTATUS_FAILINGPARAMS;
                ZLog::Write(LOGLEVEL_DEBUG, "HandlePing(): no pingable folders found and no initialization data sent. Returning SYNC_PINGSTATUS_FAILINGPARAMS.");
            }
            elseif(!$this->lifetimeBetweenBound($sc->GetLifetime())){
                $pingstatus = SYNC_PINGSTATUS_FAILINGPARAMS;
                ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandlePing(): ping lifetime not between bound (higher bound:'%d' lower bound:'%d' current lifetime:'%d'. Returning SYNC_PINGSTATUS_FAILINGPARAMS.", PING_HIGHER_BOUND_LIFETIME, PING_LOWER_BOUND_LIFETIME, $sc->GetLifetime()));
            }
        }


        // Check for changes on the default LifeTime, set interval and ONLY on pingable collections
        try {
            if (!$pingstatus && empty($fakechanges)) {
                self::$deviceManager->DoAutomaticASDeviceSaving(false);
                $foundchanges = $sc->CheckForChanges($sc->GetLifetime(), $interval, true);
            }
        }
        catch (StatusException $ste) {
            switch($ste->getCode()) {
                case SyncCollections::ERROR_NO_COLLECTIONS:
                    $pingstatus = SYNC_PINGSTATUS_FAILINGPARAMS;
                    break;
                case SyncCollections::ERROR_WRONG_HIERARCHY:
                    $pingstatus = SYNC_PINGSTATUS_FOLDERHIERSYNCREQUIRED;
                    self::$deviceManager->AnnounceProcessStatus(false, $pingstatus);
                    break;
                case SyncCollections::OBSOLETE_CONNECTION:
                    $foundchanges = false;
                    break;
                case SyncCollections::HIERARCHY_CHANGED:
                    $pingstatus = SYNC_PINGSTATUS_FOLDERHIERSYNCREQUIRED;
                    break;
            }
        }

        self::$encoder->StartWBXML();
        self::$encoder->startTag(SYNC_PING_PING);
        {
            self::$encoder->startTag(SYNC_PING_STATUS);
            if (isset($pingstatus) && $pingstatus)
                self::$encoder->content($pingstatus);
            else
                self::$encoder->content($foundchanges ? SYNC_PINGSTATUS_CHANGES : SYNC_PINGSTATUS_HBEXPIRED);
            self::$encoder->endTag();

            if (! $pingstatus) {
                self::$encoder->startTag(SYNC_PING_FOLDERS);

                if (empty($fakechanges))
                    $changes = $sc->GetChangedFolderIds();
                else
                    $changes = $fakechanges;

                $announceAggregated = false;
                if (count($changes) > 1) {
                    $announceAggregated = 0;
                }
                foreach ($changes as $folderid => $changecount) {
                    if ($changecount > 0) {
                        self::$encoder->startTag(SYNC_PING_FOLDER);
                        self::$encoder->content($folderid);
                        self::$encoder->endTag();
                        if ($announceAggregated === false) {
                            if (empty($fakechanges)) {
                                self::$topCollector->AnnounceInformation(sprintf("Found change in %s", $sc->GetCollection($folderid)->GetContentClass()), true);
                            }
                        }
                        else {
                            $announceAggregated += $changecount;
                        }
                        self::$deviceManager->AnnounceProcessStatus($folderid, SYNC_PINGSTATUS_CHANGES);
                    }
                }
                if ($announceAggregated !== false) {
                    self::$topCollector->AnnounceInformation(sprintf("Found %d changes in %d folders", $announceAggregated, count($changes)), true);
                }
                self::$encoder->endTag();
            }
            elseif($pingstatus == SYNC_PINGSTATUS_HBOUTOFRANGE){
                self::$encoder->startTag(SYNC_PING_LIFETIME);
                if($sc->GetLifetime() > PING_HIGHER_BOUND_LIFETIME){
                    self::$encoder->content(PING_HIGHER_BOUND_LIFETIME);
                }
                else{
                    self::$encoder->content(PING_LOWER_BOUND_LIFETIME);
                }
                self::$encoder->endTag();
            }
        }

        self::$encoder->endTag();

        // update the waittime waited
        self::$waitTime = $sc->GetWaitedSeconds();

        return true;
    }

    /**
     * Return true if the ping lifetime is between the specified bound (PING_HIGHER_BOUND_LIFETIME and PING_LOWER_BOUND_LIFETIME). If no bound are specified, it returns true.
     *
     * @param int       $lifetime
     *
     * @access private
     * @return boolean
     */
    private function lifetimeBetweenBound($lifetime){
        if(PING_HIGHER_BOUND_LIFETIME !== false && PING_LOWER_BOUND_LIFETIME !== false){
            return ($lifetime <= PING_HIGHER_BOUND_LIFETIME && $lifetime >= PING_LOWER_BOUND_LIFETIME);
        }
        if(PING_HIGHER_BOUND_LIFETIME !== false){
            return $lifetime <= PING_HIGHER_BOUND_LIFETIME;
        }
        if(PING_LOWER_BOUND_LIFETIME !== false){
            return $lifetime >= PING_LOWER_BOUND_LIFETIME;
        }
        return true;
    }

    /**
     * Checks if a sent folder class is valid for that SyncParameters object.
     *
     * @param string $class
     * @param SycnParameters $spa
     *
     * @access public
     * @return boolean
     */
    private function isClassValid($class, $spa) {
        // ZP-907: Outlook might request a ping for such a folder, but we shouldn't answer it in any way
        if (Request::IsOutlook() && self::$deviceManager->GetFolderTypeFromCacheById($spa->GetFolderId()) == SYNC_FOLDER_TYPE_UNKNOWN) {
            ZLog::Write(LOGLEVEL_DEBUG, sprintf("HandlePing()->isClassValid(): ignoring folder id '%s' as it's of type UNKNOWN ", $spa->GetFolderId()));
            return false;
        }
        if ($class == $spa->GetContentClass() ||
                // KOE ZO-42: Notes are synched as Appointments
                (self::$deviceManager->IsKoe() && KOE_CAPABILITY_NOTES && $class == "Calendar" && $spa->GetContentClass() == "Notes")
            ) {
            return true;
        }
        return false;
    }
}