Completed
Push — next ( 5f2bc0...cef70f )
by Thomas
25s queued 12s
created

restorecaches.php ➔ get_archive_data()   F

Complexity

Conditions 40
Paths > 20000

Size

Total Lines 364

Duplication

Lines 118
Ratio 32.42 %

Importance

Changes 0
Metric Value
cc 40
nc 293576400
nop 1
dl 118
loc 364
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 *
5
 *
6
 * TODO:
7
 *        - limit / clean up archive ?
8
 *        - do not archive anything with is not from our node
9
 ***************************************************************************/
10
11
/*
12
    The following tables are monitored. On changes the OLD data is recorded
13
    (except for cache_coordinates and cache_countries, where the NEW data is recorded
14
    for historical reasons; I = on insert, U = on update, D = on delete;
15
    further explanations below:
16
                                                                recording    limited #  max.
17
    data table         archive tables        ops    recorded by   date         per cache  one p.d.
18
    ----------         --------------        ---    -----------   ----------   --------   --------
19
- caches             cache_coordinates     IU     trigger       datetime     yes        no
20
-                    cache_countries       IU     trigger       datetime     yes        no
21
c                    caches_modified        U     trigger       date         yes        yes
22
c cache_attributes   cache_attr._modified  I D    trigger       date         yes        yes
23
c cache_desc         cache_desc_modified   IUD    trigger       date         yes        yes
24
x cache_logs         cache_logs_archived     D    removelog     datetime     no         no
25
x                    cache_logs_restored   I*     here          datetime     no         no
26
  pictures           pictures_modified     IUD    trigger       datetime     no         no
27
28
    * only insertions on restore
29
30
    (Additional waypoints in table 'coordinates' are currently not monitored. Archiving
31
    and restoring waypoints would be as complex as log entries and is not important at
32
    vandalism-restore.)
33
34
    The whole mechanism heavily relies on autoincrement IDs being unique on one system
35
    and NEVER be reused after record deletion. DON'T EVEN THINK ABOUT TRUNCATING AND
36
    RENUMBERING ANY OF THE ABOVE 'DATA TABLES' UNDER ANY CIRCUMSTANCES! Use bigint IDs
37
    if you worry about how to store your 3 billion logs.
38
39
    Special fields used for recovery are:
40
        - date_modified in all tables except for logs.deletion_date and coords/countries.date_created:
41
            the date when the change took place and was recorded
42
        - deleted_by in cache_logs_archived: thee user_id who deleted the data
43
            (to determine if it can be vandalism by cache owner);
44
        - restored_by in all tables except for cache_logs_*: the restoring admin's user id (or 0)
45
        - operation in pictures_modified: I/U/D for a recorded insert/delete/update
46
        - was_set in attributes_modified: 1 if the attribute was set before the change
47
        - original_id in cache_logs_restored and pictures_modified: the original id of restored
48
            objects, needed to maintain archive integrity
49
50
    Insertion of a new record is flagged by:
51
        - caches_attributes_modified:  was_set = 0
52
        - cache_desc_modified:         desc = null
53
        - pictures_modified:           operation = 'I'
54
        - cache_logs_restored:         presence of data record (only for restored logs)
55
56
    uuids are not recorded (except for cache_logs_archived, which is used elsewhere).
57
    Restored records are just copies of the old records, not the old records themselves,
58
    so they receive new uuids!
59
    The old records stay untouched and deleted in the archives for further reference!
60
61
    To save log space, the following is NOT recorded:
62
        - changes on any data which
63
              - need not to be restored (e.g. cache status),
64
                - is recreated on restore (e.g. uuids) or
65
                - can be restored by other means (e.g. statistics, thumbnails)
66
        - additional changes on the same dataset on the same day for caches_modified,
67
            cache_desc_modified and cache_attributes_modified. They are blocked by unique
68
            indexes which include the date_modified (no time there!).
69
        - operations on the same day when the cache was created
70
        - operations on own logs, including pictures: own logs are excempt from vandalism-restore
71
        - cache attribs, desc and picture changes on the same day when the record was created
72
73
  On coordinates and countries every change is recorded for XML interface reasons.
74
  On logs and pictures, every change must be recorded, because recognizing multiple restores
75
    of the same record would not be feasible.
76
77
    On or after restore, the following is automatically fixed:
78
        - caches.desc_languages und .default_desclang
79
                             - by cache_desc triggers
80
        - cache_location     - by high-frequency cache_location cronjob (per last_modified)
81
        - cache_npa_areas    - by cache_npa_areas cronjob (per modify-trigger -> need_npa_recalc)
82
        - stat_caches        - by cache_logs triggers
83
        - stat_cache_logs    - by cache_logs triggers
84
        - cache_logs.picture - by picture triggers
85
        - pic thumbnails     - by thumbs.php (per thumb_last_generated default value)
86
87
    There is no special treatment of changes by restore operations, so they are recorded
88
    and revertible, too, if not done on the same day as the vandalism.
89
90
    The table listing_restored keeps track of the admins who and when restored listings.
91
92
    Code updates:
93
        When adding fields to caches, cache_attributes, cache_desc, cache_logs or pictures
94
        which are not filled automatically by triggers or cronjobs, those fields must be added
95
        to the archive-and-restore mechanism, i.e.
96
97
            - to the corresponding lib2/logic classes (currently used only for logs and pics,
98
                but anyway ...)
99
            - to maintain.php (archiving triggers; use INSERT IGNORE for all recording)
100
            - to restorecaches.php (functions get_archive_data and restore_caches),
101
            - to the *_modified tables and
102
            - eventually to restorecaches.tpl (step 4).
103
104
        While the restore mechanism itself is robust against database extensions and will not
105
        crash when fields are added, the archives would grow incomplete and so would be the
106
        restored listings. Also, CC-ND license at OC.de requires listings to be published
107
        unchanged, so all-or-nothing-restore per listing is desireable.
108
109
        When adding new TABLES for new listing-related data which can be vandalized, decide if -
110
        preferrably - max. one recorded change per day is (a) sufficient or (b) not. It is
111
        sufficient if the number of records inserted for one cache is limited by some property
112
        (like attribute types oder languages). It is insufficient if there can be an arbitrary
113
        number of records (like logs and pictures).
114
        for (a)
115
            - define date_modified field as 'date'
116
            - define an unique index on parent id, date_modified and the limiting property/ies
117
                  (e.g. see caches_attributes_modified 'cache_id' index)
118
            - use INSERT ... ON DUPLICATE UPDATE ...  on restore
119
        for (b)
120
            - define date_modified field as 'datetime'
121
            - truncate date_modified in get_archive_data() to the date, i.e. left 10 chars
122
            - order ascending by (full) date_modified in get_archive_data()
123
            - define an 'original_id' field and implement a mechanism like get_current_picid()
124
                  to take care of changing ids and original_ids
125
            - use the corresponding parent table's mechanism for parent ids when restoring
126
                  deleted records, if the parent table is also an arbitrary-number table
127
                     (see call to get_current_logid() when restoring deleted pictures)
128
            - for index definitions see e.g. cache_logs_restored
129
130
        If you can't handle these requirements, don't add the new fields/tables.
131
132
*/
133
134
require __DIR__ . '/lib2/web.inc.php';
135
require_once __DIR__ . '/lib2/logic/labels.inc.php';
136
137
$tpl->name = 'restorecaches';
138
$tpl->menuitem = MNU_ADMIN_RESTORE;
139
$tpl->assign('error', '');
140
$tpl->assign('step', 0);
141
142
$login->verify();
143
if ($login->userid == 0) {
144
    $tpl->redirect_login();
145
}
146
147
if (!$login->hasAdminPriv(ADMIN_RESTORE)) {
148
    $tpl->error(ERROR_NO_ACCESS);
149
}
150
151
// params
152
if (isset($_REQUEST['finduser']) && isset($_REQUEST['username'])) {
153
    // STEP 2: verify username
154
155
    $tpl->assign('step', 1);
156
    $tpl->assign('username', $_REQUEST['username']);
157
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
158
        "SELECT `user_id`, `username`, `is_active_flag` FROM `user` WHERE `username`='&1'",
159
        $_REQUEST['username']
160
    );
161
    $r = sql_fetch_assoc($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
162
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
163
    if ($r == false) {
164
        $tpl->assign('error', 'userunknown');
165
        $tpl->display();
166
    }
167
    $tpl->assign('username', $r['username']);
168
169
    // get cache set for this user
170
    $user_id = $r['user_id'];
171
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
172
        "SELECT `cache_id`,
173
                `wp_oc`,
174
                `name`,
175
                `latitude`,
176
                `longitude`,
177
                `status`,
178
                LEFT(`listing_last_modified`,10) AS `last_modified`,
179
                (SELECT COUNT(*) FROM `cache_logs` WHERE `cache_logs`.`cache_id`=`caches`.`cache_id`) AS `logs`
180
         FROM `caches`
181
         WHERE `user_id`='&1'
182
         AND `status`!=5",
183
        $user_id
184
    );
185
    $caches = array();
186
    while ($rCache = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
187
        $coord = new coordinate($rCache['latitude'], $rCache['longitude']);
188
        $rCache['coordinates'] = $coord->getDecimalMinutes();
189
        $rCache['data'] = get_archive_data(array($rCache['cache_id']));
190
        if (count($rCache['data'])) {
191
            $keys = array_keys($rCache['data']);
192
            $rCache['date'] = $keys[0];
193
        }
194
        $caches[] = $rCache;
195
    }
196
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
197
198
    if (count($caches) == 0) {
199
        $tpl->assign('error', 'nocaches');
200
    } else {
201
        // STEP 3: select caches to restore
202
        $tpl->assign('step', 3);
203
        $tpl->assign('aCaches', $caches);
204
        $tpl->assign('disabled', $r['is_active_flag'] == 0);
205
    }
206
} elseif (isset($_REQUEST['username']) && isset($_REQUEST['caches'])) {
207
    // STEP 4: select date
208
209
    $tpl->assign('step', 4);
210
    $tpl->assign('username', $_REQUEST['username']);
211
    $tpl->assign(
212
        'disabled',
213
        sql_value(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
214
            "SELECT NOT `is_active_flag` FROM `user` WHERE `username`='&1'",
215
            0,
216
            $_REQUEST['username']
217
        )
218
    );
219
220
    $cacheids = array();
221
    foreach ($_REQUEST as $param => $value) {
222
        if (substr($param, 0, 6) == 'cache_') {
223
            $cacheids[] = substr($param, 6);
224
        }
225
    }
226
227
    if (count($cacheids) == 0) {
228
        $tpl->assign('error', 'nocaches');
229
        $tpl->display();
230
    }
231
232
    $dates = get_archive_data($cacheids);
233
    if (count($dates) == 0) {
234
        $tpl->assign('error', 'nodata');
235
        $tpl->display();
236
    }
237
238
    $today = sql_value("SELECT LEFT(NOW(),10)", 0);
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
239
    $today_usermod = false;
240
    if (isset($dates[$today])) {
241
        foreach ($dates[$today] as $cache_changed) {
242
            if (strpos($cache_changed, "userchange")) {
243
                $today_usermod = true;
244
            }
245
        }
246
    }
247
248
    $tpl->assign('cachelist', urlencode(implode(',', $cacheids)));
249
    $tpl->assign('dates', $dates);
250
    $tpl->assign('today', $today_usermod);
251
    $tpl->assign('rootadmin', $opt['page']['develsystem'] && $login->hasAdminPriv(ADMIN_ROOT));
252
    $tpl->display();
253
} elseif (isset($_REQUEST['username']) && ($_REQUEST['cacheids']) && isset($_REQUEST['doit'])) {
254
    // STEP 5: restore data
255
256
    $tpl->assign('step', 5);
257
    $tpl->assign('username', $_REQUEST['username']);
258
    $disabled =
259
        sql_value(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
260
            "SELECT NOT `is_active_flag` FROM `user` WHERE `username`='&1'",
261
            0,
262
            $_REQUEST['username']
263
        );
264
    $tpl->assign('disabled', $disabled);
265
266
    $simulate = isset($_REQUEST['simulate']) && $_REQUEST['simulate'];
267
    $tpl->assign('simulate', $simulate);
268
269
    $restore_date = isset($_REQUEST['dateselect']) ? $_REQUEST['dateselect'] : "";
270
    $restore_options = array();
271
    foreach ($_REQUEST as $param => $value) {
272
        if (substr($param, 0, 8) == "restore_") {
273
            $restore_options[] = substr($param, 8);
274
        }
275
    }
276
277
    $allowance = isset($_REQUEST['allowance']) && $_REQUEST['allowance'];
278
    if (!$disabled && !$simulate && !$allowance) {
279
        $tpl->assign('error', 'noallowance');
280
        $tpl->display();
281
    } elseif ($restore_date == '') {
282
        $tpl->assign('error', 'nodate');
283
        $tpl->display();
284
    } elseif (count($restore_options) == 0) {
285
        $tpl->assign('error', 'nochecks');
286
        $tpl->display();
287
    }
288
    if ((!isset($_REQUEST['sure']) || !$_REQUEST['sure']) && !$simulate) {
289
        $tpl->assign('error', 'notsure');
290
        $tpl->display();
291
    }
292
293
    $cacheids = explode(',', urldecode($_REQUEST['cacheids']));
294
    $tpl->assign(
295
        'restored',
296
        restore_listings($cacheids, $restore_date, $restore_options, $simulate)
297
    );
298
    $tpl->assign('date', $restore_date);
299
} else {
300
    // STEP 1: ask for username
301
302
    $tpl->assign('step', 1);
303
}
304
305
$tpl->display();
306
307
308
// get readable list of recorded data changes for a list of cache(id)s
309
310
function get_archive_data($caches)
311
{
312
    $cachelist = '(' . implode(',', $caches) . ')';
313
    $data = array();
314
    $admins = array();
315
316
    // make waypoint index
317
    $rs = sql('SELECT `cache_id`, `wp_oc` FROM `caches` WHERE `cache_id` IN ' . $cachelist);
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
318
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
319
        $wp_oc[$r['cache_id']] = $r['wp_oc'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$wp_oc was never initialized. Although not strictly required by PHP, it is generally a good practice to add $wp_oc = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
320
    }
321
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
322
323
    // cache coordinates
324
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
325
        'SELECT `cache_id`,
326
                LEFT(`date_created`,10) AS `date_modified`,
327
                `longitude`,
328
                `latitude`,
329
                `restored_by`
330
         FROM `cache_coordinates`
331
         WHERE `cache_id` IN ' . $cachelist . '
332
         ORDER BY `date_created` ASC'
333
    );
334
    // order is relevant, because multiple changes per day possible
335
    $lastcoord = array();
336
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
337
        $coord = new coordinate($r['latitude'], $r['longitude']);
338
        $coord = $coord->getDecimalMinutes();
339
        $coord = $coord['lat'] . " " . $coord['lon'];
340 View Code Duplication
        if (isset($lastcoord[$r['cache_id']]) && $coord != $lastcoord[$r['cache_id']]) {
341
            // the database contains lots of old coord records with unchanged coords, wtf?
342
            append_data($data, $admins, $wp_oc, $r, "coord", $lastcoord[$r['cache_id']], $coord);
0 ignored issues
show
Bug introduced by
The variable $wp_oc does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
343
        }
344
        $lastcoord[$r['cache_id']] = $coord;
345
    }
346
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
347
348
    // cache country
349
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
350
        'SELECT `cache_id`,
351
                LEFT(`date_created`,10) AS `date_modified`,
352
                `country`,
353
                `restored_by`
354
         FROM `cache_countries`
355
         WHERE `cache_id` IN ' . $cachelist . '
356
         ORDER BY `date_created` ASC'
357
    );
358
    // order is relevant, because multiple changes per day possible
359
    $lastcountry = array();
360
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
361 View Code Duplication
        if (isset($lastcountry[$r['cache_id']]) && $r['country'] != $lastcountry[$r['cache_id']]) {
362
            // the database contains some old country records with unchanged coords, wtf?
363
            append_data($data, $admins, $wp_oc, $r, "country", $lastcountry[$r['cache_id']], $r['country']);
364
        }
365
        $lastcountry[$r['cache_id']] = $r['country'];
366
    }
367
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
368
369
    // all other cache data
370
    // first the current data ...
371
    $nextcd = array();
372
    $rs = sql("SELECT * FROM `caches` WHERE `cache_id` IN " . $cachelist);
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
373
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
374
        $nextcd[$r['wp_oc']] = $r;
375
        $user_id = $r['user_id']; // is used later for logs
376
    }
377
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
378
379
    // .. and then the changes
380
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
381
        'SELECT * FROM `caches_modified`
382
         WHERE `cache_id` IN ' . $cachelist . '
383
         ORDER BY `date_modified` DESC'
384
    );
385
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
386
        $wp = $wp_oc[$r['cache_id']];
387 View Code Duplication
        if ($r['name'] != $nextcd[$wp]['name']) {
388
            append_data($data, $admins, $wp_oc, $r, 'name', $r['name'], $nextcd[$wp]['name']);
389
        }
390 View Code Duplication
        if ($r['type'] != $nextcd[$wp]['type']) {
391
            append_data(
392
                $data,
393
                $admins,
394
                $wp_oc,
395
                $r,
396
                'type',
397
                labels::getLabelValue('cache_type', $r['type']),
398
                labels::getLabelValue('cache_type', $nextcd[$wp]['type'])
399
            );
400
        }
401 View Code Duplication
        if ($r['size'] != $nextcd[$wp]['size']) {
402
            append_data(
403
                $data,
404
                $admins,
405
                $wp_oc,
406
                $r,
407
                "size",
408
                labels::getLabelValue('cache_size', $r['size']),
409
                labels::getLabelValue('cache_size', $nextcd[$wp]['size'])
410
            );
411
        }
412 View Code Duplication
        if ($r['difficulty'] != $nextcd[$wp]['difficulty']) {
413
            append_data($data, $admins, $wp_oc, $r, "D", $r['difficulty'] / 2, $nextcd[$wp]['difficulty'] / 2);
414
        }
415 View Code Duplication
        if ($r['terrain'] != $nextcd[$wp]['terrain']) {
416
            append_data($data, $admins, $wp_oc, $r, "T", $r['terrain'] / 2, $nextcd[$wp]['terrain'] / 2);
417
        }
418 View Code Duplication
        if ($r['search_time'] != $nextcd[$wp]['search_time']) {
419
            append_data(
420
                $data,
421
                $admins,
422
                $wp_oc,
423
                $r,
424
                'time',
425
                $r['search_time'] . '&nbsp;h',
426
                $nextcd[$wp]['search_time'] . '&nbsp;h'
427
            );
428
        }
429 View Code Duplication
        if ($r['way_length'] != $nextcd[$wp]['way_length']) {
430
            append_data(
431
                $data,
432
                $admins,
433
                $wp_oc,
434
                $r,
435
                'way',
436
                $r['way_length'] . '&nbsp;km',
437
                $nextcd[$wp]['way_length'] . '&nbsp;km'
438
            );
439
        }
440 View Code Duplication
        if ($r['wp_gc'] != $nextcd[$wp]['wp_gc']) {
441
            append_data(
442
                $data,
443
                $admins,
444
                $wp_oc,
445
                $r,
446
                'GC ',
447
                format_wp($r['wp_gc']),
448
                format_wp($nextcd[$wp]['wp_gc'])
449
            );
450
        }
451 View Code Duplication
        if ($r['wp_nc'] != $nextcd[$wp]['wp_nc']) {
452
            append_data(
453
                $data,
454
                $admins,
455
                $wp_oc,
456
                $r,
457
                'GC ',
458
                format_wp($r['wp_nc']),
459
                format_wp($nextcd[$wp]['wp_nc'])
460
            );
461
        }
462 View Code Duplication
        if ($r['date_hidden'] != $nextcd[$wp]['date_hidden']) {
463
            append_data($data, $admins, $wp_oc, $r, "hidden", $r['date_hidden'], $nextcd[$wp]['date_hidden']);
464
        }
465
466
        $nextcd[$wp] = $r;
467
    }
468
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
469
470
    // attributes
471
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
472
        'SELECT *
473
         FROM `caches_attributes_modified`
474
         WHERE `cache_id` IN ' . $cachelist . '  /* OConly attrib is shown, but not restorable */
475
         ORDER BY `date_modified` ASC'
476
    ); // order doesn't matter as long it is date only
477
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
478
        append_data(
479
            $data,
480
            $admins,
481
            $wp_oc,
482
            $r,
483
            'attrib',
484
            ($r['was_set'] ? '-' : '+') . labels::getLabelValue('cache_attrib', $r['attrib_id']),
485
            ''
486
        );
487
    }
488
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
489
490
    // descriptions
491
    // first the current data ...
492
    $nextdesc = array();
493
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
494
        'SELECT `cache_id`,
495
                `language`,
496
                LENGTH(`desc`) AS `dl`,
497
                LENGTH(`hint`) AS `hl`,
498
                LENGTH(`short_desc`) AS `sdl`
499
         FROM `cache_desc`
500
         WHERE `cache_id` IN ' . $cachelist
501
    );
502
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
503
        if (!isset($nextdesc[$r['cache_id']])) {
504
            $nextdesc[$r['cache_id']] = [];
505
        }
506
        $nextdesc[$r['cache_id']][$r['language']] = $r;
507
    }
508
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
509
510
    // ... and then the changes
511
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
512
        'SELECT `cache_id`,
513
                `date_modified`,
514
                `language`,
515
                LENGTH(`desc`) AS `dl`,
516
                LENGTH(`hint`) AS `hl`,
517
                LENGTH(`short_desc`) AS `sdl`,
518
                `restored_by`
519
         FROM `cache_desc_modified`
520
         WHERE `cache_id` IN ' . $cachelist . '
521
         ORDER BY `date_modified` DESC'
522
    );
523
    // order doesn't matter as long only one change per day is recorded
524
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
525
        $wp = $wp_oc[$r['cache_id']];
0 ignored issues
show
Unused Code introduced by
$wp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
526
        if (!isset($nextdesc[$r['cache_id']]) || !isset($nextdesc[$r['cache_id']][$r['language']])) {
527
            $next = [
528
                'dl' => 0,
529
                'hl' => 0,
530
                'sdl' => 0
531
            ];
532
        } else {
533
            $next = $nextdesc[$r['cache_id']][$r['language']];
534
        }
535
536 View Code Duplication
        if ($r['dl'] + 0 != $next['dl'] + 0) {
537
            append_data(
538
                $data,
539
                $admins,
540
                $wp_oc,
541
                $r,
542
                'desc(' . $r['language'] . ')',
543
                $r['dl'] + 0,
544
                ($next['dl'] + 0) . ' bytes'
545
            );
546
        }
547 View Code Duplication
        if ($r['hl'] + 0 != $next['hl'] + 0) {
548
            append_data(
549
                $data,
550
                $admins,
551
                $wp_oc,
552
                $r,
553
                'hint(' . $r['language'] . ')',
554
                $r['hl'] + 0,
555
                ($next['hl'] + 0) . ' bytes'
556
            );
557
        }
558 View Code Duplication
        if ($r['sdl'] + 0 != $next['sdl'] + 0) {
559
            append_data(
560
                $data,
561
                $admins,
562
                $wp_oc,
563
                $r,
564
                'shortdesc(' . $r['language'] . ')',
565
                $r['sdl'] + 0,
566
                ($next['sdl'] + 0) . ' bytes'
567
            );
568
        }
569
570
        $nextdesc[$r['cache_id']][$r['language']] = $r;
571
    }
572
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
573
574
    // logs
575
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
576
        'SELECT `op`,
577
                LEFT(`date_modified`,10) AS `date_modified`,
578
                `cache_id`,
579
                `logs`.`user_id`,
580
                `type`,
581
                `date`,
582
                `restored_by`,
583
                `username`
584
         FROM (SELECT 1 AS `op`,
585
                      `deletion_date` AS `date_modified`,
586
                      `cache_id`,
587
                      `user_id`,
588
                      `type`,
589
                      `date`,
590
                      `restored_by`
591
               FROM `cache_logs_archived`
592
               WHERE `cache_id` IN ' . $cachelist . "
593
                 AND `deleted_by`='&1' AND `user_id`<>'&1'
594
                 UNION
595
                  SELECT 2 AS `op`, `date_modified`, `cache_id`,
596
                       (SELECT `user_id` FROM `cache_logs_archived` WHERE `id`=`original_id`),
597
                       (SELECT `type` FROM `cache_logs_archived` WHERE `id`=`original_id`),
598
                       (SELECT `date` FROM `cache_logs_archived` WHERE `id`=`original_id`),
599
                       `restored_by`
600
                 FROM `cache_logs_restored`
601
                  WHERE `cache_id` IN " . $cachelist . '
602
                  ) `logs`
603
                INNER JOIN `user` ON `user`.`user_id`=`logs`.`user_id`
604
              ORDER BY `logs`.`date_modified` ASC',
605
        // order may not be exact when redoing reverts, because delete and insert
606
        // operations then are so quick that dates in both tables are the same
607
        $user_id
0 ignored issues
show
Bug introduced by
The variable $user_id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
608
    );
609
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
610
        append_data(
611
            $data,
612
            $admins,
613
            $wp_oc,
614
            $r,
615
            $r['op'] == 1 ? 'dellog' : 'restorelog',
616
            "<a href='viewprofile.php?userid=" . $r['user_id'] .
617
            "' target='_blank'>" . $r['username'] . '</a>/' . $r['date'],
618
            ''
619
        );
620
    }
621
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
622
623
    // pictures
624
625
    /* For sake of simplification, we
626
     *   - have stored the name of inserted pictures in pictures_modified
627
     *   - give no detailed information on picture property changes. This will be very
628
     *       rare in case of vandalism ...
629
     */
630
631
    $piccacheid = "IF(`object_type`=2, `object_id`, IF(`object_type`=1, IFNULL((SELECT `cache_id` FROM `cache_logs` WHERE `id`=`object_id`),(SELECT `cache_id` FROM `cache_logs_archived` WHERE `id`=`object_id`)), 0))";
632
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
633
        'SELECT *, ' . $piccacheid . 'AS `cache_id` FROM `pictures_modified`
634
         WHERE ' . $piccacheid . ' IN ' . $cachelist . '
635
         ORDER BY `date_modified` ASC'
636
    ); // order is relevant for the case of restore-reverts
637
    while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
638
        $r['date_modified'] = substr($r['date_modified'], 0, 10);
639
        switch ($r['operation']) {
640
            case 'I':
641
                $picchange = 'add';
642
                break;
643
            case 'U':
644
                $picchange = 'mod';
645
                break;
646
            case 'D':
647
                $picchange = 'del';
648
                break;
649
        }
650
        switch ($r['object_type']) {
651
            case 1:
652
                $picchange .= '-log';
0 ignored issues
show
Bug introduced by
The variable $picchange does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
653
                break;
654
            case 2:
655
                $picchange .= '-cache';
656
                break;
657
        }
658
        append_data($data, $admins, $wp_oc, $r, $picchange . 'pic', $r['title'], '');
659
    }
660
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
661
662
    // admins
663
    foreach ($admins as $adate => $adata) {
664
        foreach ($adata as $awp => $alist) {
665
            $data[$adate][$awp] .= "<br /><strong class='adminrestore'>admins:</strong> " . implode(',', $alist);
666
        }
667
    }
668
669
    // done
670
    ksort($data);
671
672
    return array_reverse($data, true);
673
}
674
675
676
function format_wp($wp)
677
{
678
    if ($wp == '') {
679
        return '(leer)';
680
    } else {
681
        return $wp;
682
    }
683
}
684
685
686
/**
687
 * @param $data
688
 * @param $admins
689
 * @param $wp_oc
690
 * @param $r
691
 * @param string $field
692
 * @param $oldvalue
693
 * @param $newvalue
694
 */
695
function append_data(&$data, &$admins, $wp_oc, $r, $field, $oldvalue, $newvalue)
696
{
697
    if (!isset($r['date_modified'])) {
698
        die("internal error: date_modified not set for $field");
699
    }
700
    $mdate = $r['date_modified'];
701
    $wp = $wp_oc[$r['cache_id']];
702
    $byadmin = ($r['restored_by'] > 0);
703
704
    if (!isset($data[$mdate])) {
705
        $data[$mdate] = [];
706
    }
707
708
    $text = '<strong';
709
    if ($byadmin) {
710
        $text .= " class='adminrestore'";
711
    } else {
712
        $text .= " class='userchange'";
713
    }
714
    $text .= ">$field</strong>: $oldvalue" . ($newvalue != '' ? " &rarr; $newvalue" : '');
715
    if (isset($data[$mdate][$wp])) {
716
        $data[$mdate][$wp] .= ', ' . $text;
717
    } else {
718
        $data[$mdate][$wp] = $text;
719
    }
720
721
    if ($byadmin) {
722
        if (!isset($admins[$mdate])) {
723
            $admins[$mdate] = [];
724
        }
725
        if (!isset($admins[$mdate][$wp])) {
726
            $admins[$mdate][$wp] = [];
727
        }
728
        $admins[$mdate][$wp][$r['restored_by'] + 0]
729
            = "<a href='viewprofile.php?userid=" . $r['restored_by'] . "' target='_blank'>" .
730
            sql_value("SELECT `username` FROM `user` WHERE `user_id`='&1'", '', $r['restored_by']) .
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
731
            '</a>';
732
    }
733
}
734
735
736
function restore_listings($cacheids, $rdate, $roptions, $simulate)
737
{
738
    global $opt, $login;
739
740
    sql("SET @restoredby='&1'", $login->userid); // is evaluated by trigger functions
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
741
    sql_slave("SET @restoredby='&1'", $login->userid);
0 ignored issues
show
Deprecated Code introduced by
The function sql_slave() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
742
743
    $restored = array();
744
745
    foreach ($cacheids as $cacheid) {
746
        $modified = false;
0 ignored issues
show
Unused Code introduced by
$modified is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
747
748
        // get current cache data
749
        $rs = sql("SELECT * FROM `caches` WHERE `cache_id`='&1'", $cacheid);
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
750
        $cache = sql_fetch_assoc($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
751
        sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
752
        $wp = $cache['wp_oc'];
753
        $user_id = $cache['user_id'];
754
755
        // coordinates
756 View Code Duplication
        if (in_array('coords', $roptions) &&
757
            sql_value(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
758
                "SELECT `cache_id`
759
                 FROM `cache_coordinates`
760
                 WHERE `cache_id`='&1'
761
                   AND `date_created`>='&2'",
762
                0,
763
                $cacheid,
764
                $rdate
765
            )
766
        ) {
767
            $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
768
                "SELECT `latitude`,
769
                        `longitude`
770
                 FROM `cache_coordinates`
771
                 WHERE `cache_id`='&1'
772
                   AND `date_created` < '&2'
773
                 ORDER BY `date_created` DESC
774
                 LIMIT 1",
775
                $cacheid,
776
                $rdate
777
            );
778
            if ($r = sql_fetch_assoc($rs)) { // should always be true ...
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
779
                if (!$simulate) {
780
                    sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
781
                        "UPDATE `caches` SET `latitude`='&1', `longitude`='&2' WHERE `cache_id`='&3'",
782
                        $r['latitude'],
783
                        $r['longitude'],
784
                        $cacheid
785
                    );
786
                }
787
788
                $restored[$wp]['coords'] = true;
789
            }
790
            sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
791
        }
792
793
        // country
794 View Code Duplication
        if (in_array('coords', $roptions) &&
795
            sql_value(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
796
                "SELECT `cache_id`
797
                 FROM `cache_countries`
798
                 WHERE `cache_id`='&1'
799
                   AND `date_created`>='&2'",
800
                0,
801
                $cacheid,
802
                $rdate
803
            )
804
        ) {
805
            $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
806
                "SELECT `country`
807
                 FROM `cache_countries`
808
                 WHERE `cache_id`='&1'
809
                   AND `date_created` < '&2'
810
                 ORDER BY `date_created` DESC
811
                LIMIT 1",
812
                $cacheid,
813
                $rdate
814
            );
815
            if ($r = sql_fetch_assoc($rs)) { // should always be true ...
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
816
                if (!$simulate) {
817
                    sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
818
                        "UPDATE `caches` SET `country`='&1'  WHERE `cache_id`='&2'",
819
                        $r['country'],
820
                        $cacheid
821
                    );
822
                }
823
824
                $restored[$wp]['country'] = true;
825
            }
826
            sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
827
        }
828
829
        // other cache data
830
        $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
831
            "SELECT *
832
             FROM `caches_modified`
833
             WHERE `cache_id`='&1'
834
               AND `date_modified` >='&2'
835
             ORDER BY `date_modified` ASC
836
             LIMIT 1",
837
            $cacheid,
838
            $rdate
839
        );
840
841
        $fields = [
842
            'name' => 'settings',
843
            'type' => 'settings',
844
            'size' => 'settings',
845
            'date_hidden' => 'settings',
846
            'difficulty' => 'settings',
847
            'terrain' => 'settings',
848
            'search_time' => 'settings',
849
            'way_length' => 'settings',
850
            'wp_gc' => 'waypoints',
851
            'wp_nc' => 'waypoints'
852
        ];
853
854
        if ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
855
            // can be false
856
            $setfields = '';
857
            foreach ($fields as $field => $ropt) {
858
                if (in_array($ropt, $roptions) && $r[$field] != $cache[$field]) {
859
                    if ($setfields != '') {
860
                        $setfields .= ',';
861
                    }
862
                    $setfields .= "`$field`='" . sql_escape($r[$field]) . "'";
0 ignored issues
show
Deprecated Code introduced by
The function sql_escape() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
863
                    $restored[$wp][$field] = true;
864
                }
865
            }
866
            if ($setfields != '' && !$simulate) {
867
                sql('UPDATE `caches` SET ' . $setfields . " WHERE `cache_id`='&1'", $cacheid);
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
868
            }
869
        }
870
        sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
871
872
        // attributes
873 View Code Duplication
        if (in_array('settings', $roptions)) {
874
            $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
875
                "SELECT *
876
                 FROM `caches_attributes_modified`
877
                 WHERE `cache_id`='&1'
878
                   AND `date_modified`>='&2'
879
                   AND `attrib_id` != 6 /* OConly */
880
                 ORDER BY `date_modified` DESC",
881
                $cacheid,
882
                $rdate
883
            );
884
885
            // revert all attribute changes in reverse order.
886
            // recording limit of one change per attribute, cache and day ensures that no exponentially
887
            // growing list of recording entries can emerge from multiple reverts.
888
889
            while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
890
                if (!$simulate) {
891
                    if ($r['was_set']) {
892
                        sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
893
                            "INSERT IGNORE INTO `caches_attributes` (`cache_id`,`attrib_id`)
894
                             VALUES ('&1','&2')",
895
                            $cacheid,
896
                            $r['attrib_id']
897
                        );
898
                    } else {
899
                        sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
900
                            "DELETE FROM `caches_attributes` WHERE `cache_id`='&1' AND `attrib_id`='&2'",
901
                            $cacheid,
902
                            $r['attrib_id']
903
                        );
904
                    }
905
                }
906
                $restored[$wp]['attributes'] = true;
907
            }
908
            sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
909
        }
910
911
        // descriptions
912
        if (in_array('desc', $roptions)) {
913
            $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
914
                "SELECT *
915
                 FROM `cache_desc_modified`
916
                 WHERE `cache_id`='&1'
917
                   AND `date_modified`>='&2'
918
                 ORDER BY `date_modified` DESC",
919
                $cacheid,
920
                $rdate
921
            );
922
923
            // revert all desc changes in reverse order.
924
            // recording limit of one change per language, cache and day ensures that no exponentially
925
            // growing list of recording entries can emerge from restore-reverts.
926
927
            while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
928
                if (!$simulate) {
929
                    if ($r['desc'] === null) { // was newly created -> delete
930
                        sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
931
                            "DELETE FROM `cache_desc` WHERE `cache_id`='&1' AND `language`='&2'",
932
                            $cacheid,
933
                            $r['language']
934
                        );
935
                    } else {
936
                        // id, uuid, date_created and last_modified are set automatically
937
                        sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
938
                            "INSERT INTO `cache_desc`
939
                            (`node`, `cache_id`, `language`, `desc`, `desc_html`, `desc_htmledit`, `hint`, `short_desc`)
940
                            VALUES ('&1','&2','&3','&4','&5','&6','&7','&8')
941
                            ON DUPLICATE KEY UPDATE
942
                            `desc`='&4', `desc_html`='&5', `desc_htmledit`='&6', `hint`='&7', `short_desc`='&8'",
943
                            $opt['logic']['node']['id'],
944
                            $cacheid,
945
                            $r['language'],
946
                            $r['desc'],
947
                            $r['desc_html'],
948
                            $r['desc_htmledit'],
949
                            $r['hint'],
950
                            $r['short_desc']
951
                        );
952
                    }
953
                }
954
955
                $restored[$wp]['description(s)'] = true;
956
            }
957
            sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
958
        }
959
960
        // logs
961
        // ... before pictures, so that restored logpics have a parent
962
        if (in_array('logs', $roptions)) {
963
            $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
964
                "
965
                SELECT * FROM (
966
                    SELECT
967
                        `id`,
968
                        -1 AS `node`,
969
                        `date_modified`,
970
                        `cache_id`,
971
                        0 AS `user_id`,
972
                        0 AS `type`,
973
                        '0' AS `oc_team_comment`,
974
                        '0' AS `date`,
975
                        '' AS `text`,
976
                        0 AS `text_html`,
977
                        0 AS `text_htmledit`,
978
                        0 AS `needs_maintenance`,
979
                        0 AS `listing_outdated`,
980
                        `original_id`
981
                    FROM `cache_logs_restored`
982
                    WHERE `cache_id`='&1' AND `date_modified` >= '&2'
983
                    UNION
984
                    SELECT
985
                        `id`,
986
                        `node`,
987
                        `deletion_date`,
988
                        `cache_id`,
989
                        `user_id`,
990
                        `type`,
991
                        `oc_team_comment`,
992
                        `date`,
993
                        `text`,
994
                        `text_html`,
995
                        `text_htmledit`,
996
                        `needs_maintenance`,
997
                        `listing_outdated`,
998
                        0 AS `original_id`
999
                    FROM `cache_logs_archived`
1000
                    WHERE
1001
                        `cache_id`='&1'
1002
                        AND `deletion_date` >= '&2'
1003
                        AND `deleted_by`='&3'
1004
                        AND `user_id` != '&3'
1005
                ) `logs`
1006
                ORDER BY `date_modified` ASC",
1007
                $cacheid,
1008
                $rdate,
1009
                $user_id
1010
            );
1011
1012
            // We start with the oldest entry and will touch each log ony once:
1013
            // After restoring its state, it is added to $logs_processed (by its last known id),
1014
            // and all further operations on the same log are ignored. This prevents unnecessary
1015
            // operations and flooding pictures_modified on restore-reverts.
1016
            $logs_processed = array();
1017
1018
            while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1019
                $error = "";
1020
                $logs_restored = false;
1021
1022
                // the log's id may have changed by multiple delete-and-restores
1023
                $revert_logid = get_current_logid($r['id']);
1024
                if (!in_array($revert_logid, $logs_processed)) {
1025
                    if ($r['node'] == - 1) {
1026
                        // if it was not already deleted by a later restore operation ...
1027
                        if (sql_value("SELECT `id` FROM `cache_logs` WHERE `id`='&1'", 0, $revert_logid) != 0) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1028
                            if (!$simulate) {
1029
                                sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1030
                                    "INSERT INTO `cache_logs_archived`
1031
                                     SELECT *, '0', '&2', '&3' FROM `cache_logs` WHERE `id`='&1'",
1032
                                    $revert_logid,
1033
                                    $user_id, // original deletor's ID and not restoring admin's ID!
1034
                                    $login->userid
1035
                                );
1036
                                sql("DELETE FROM `cache_logs` WHERE `id`='&1'", $revert_logid);
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1037
                                // This triggers an okapi_syncbase update, if OKAPI is installed:
1038
                                sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1039
                                    "UPDATE `cache_logs_archived` SET `deletion_date`=NOW() WHERE `id`='&1'",
1040
                                    $revert_logid
1041
                                );
1042
                            }
1043
                            $logs_restored = true;
1044
                        }
1045
                        // if it was not already restored by a later restore operation ...
1046
                    } elseif (sql_value("SELECT `id` FROM `cache_logs` WHERE `id`='&1'", 0, $revert_logid) == 0) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1047
                        // id, uuid, date_created and last_modified are set automatically;
1048
                        // picture will be updated automatically on picture-restore
1049
                        $log = new cachelog();
1050
                        $log->setNode($r['node']); // cachelog class currently does not initialize node field
1051
                        $log->setCacheId($r['cache_id']);
1052
                        $log->setUserId($r['user_id']);
1053
                        $log->setType($r['type'], true);
1054
                        $log->setOcTeamComment($r['oc_team_comment']);
1055
                        $log->setDate($r['date']);
1056
                        $log->setText($r['text']);
1057
                        $log->setTextHtml($r['text_html']);
1058
                        $log->setTextHtmlEdit($r['text_htmledit']);
1059
                        $log->setNeedsMaintenance($r['needs_maintenance']);
1060
                        $log->setListingOutdated($r['listing_outdated']);
1061
                        $log->setOwnerNotified(1);
1062
1063
                        if ($simulate) {
1064
                            $logs_restored = true;
1065
                        } else {
1066
                            if (!$log->save()) {
1067
                                $error = "restore";
1068
                            } else {
1069
                                sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1070
                                    "INSERT IGNORE INTO `cache_logs_restored`
1071
                                      (`id`, `date_modified`, `cache_id`, `original_id`, `restored_by`)
1072
                                    VALUES ('&1', NOW(), '&2', '&3', '&4')",
1073
                                    $log->getLogId(),
1074
                                    $log->getCacheId(),
1075
                                    $revert_logid,
1076
                                    $login->userid
1077
                                );
1078
                                sql("DELETE FROM `watches_logqueue` WHERE `log_id`='&1'", $log->getLogId());
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1079
                                // watches_logqueue entry was created by trigger
1080
                                $logs_processed[] = $log->getLogId();
1081
1082
                                /* no longer needed after implementing picture deletion in removelog.php
1083
1084
                                // log pic deleting is not completely implemented, orphan pictures are    [*p]
1085
                                // left over when directly deleting the log. We try to recover them ...
1086
                                sql("UPDATE `pictures` SET `object_id`='&1' WHERE `object_type`=1 AND `object_id`='&2'",
1087
                                    $log->getLogId(), $revert_logid);
1088
1089
                                // ... and then update the stats:
1090
                                $log->updatePictureStat();
1091
                                 */
1092
1093
                                $logs_restored = true;
1094
                            }
1095
                        }
1096
                    }  // restore deleted
1097
1098
                    $logs_processed[] = $revert_logid;
1099
                }  // not already processed
1100
1101
                if ($error != '') {
1102
                    $restored[$wp]['internal error - could not $error log ' . $r['id'] . '/' . $logid];
0 ignored issues
show
Bug introduced by
The variable $logid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
1103
                }
1104
                if ($logs_restored) {
1105
                    $restored[$wp]['logs'] = true;
1106
                }
1107
            }  // while (all relevant log records)
1108
            sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1109
        }  // if logs enabled per roptions
1110
1111
        // pictures
1112
        if (in_array('desc', $roptions) || in_array('logs', $roptions)) {
1113
            $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1114
                "SELECT *FROM `pictures_modified`
1115
                        WHERE ((`object_type`=2 AND '&2' AND `object_id`='&3') OR
1116
                                           (`object_type`=1 AND '&1'
1117
                                                  AND IFNULL((SELECT `user_id` FROM `cache_logs` WHERE `id`=`object_id`),(SELECT `user_id` FROM `cache_logs_archived` WHERE `id`=`object_id`)) != '&5'
1118
                                                  /* ^^ ignore changes of own log pics (shouldnt be in pictures_modified, anyway) */
1119
                                                  AND IFNULL((SELECT `cache_id` FROM `cache_logs` WHERE `id`=`object_id`),(SELECT `cache_id` FROM `cache_logs_archived` WHERE `id`=`object_id`)) = '&3'))
1120
                          AND `date_modified`>='&4'
1121
                                    ORDER BY `date_modified` ASC",
1122
                in_array("logs", $roptions) ? 1 : 0,
1123
                in_array("desc", $roptions) ? 1 : 0,
1124
                $cacheid,
1125
                $rdate,
1126
                $user_id
1127
            );
1128
1129
            // We start with the oldest entry and will touch each picture ony once:
1130
            // After restoring its state, it is added to $pics_processed (by its last known id),
1131
            // and all further operations on the same pic are ignored. This prevents unnecessary
1132
            // operations and flooding the _modified table on restore-reverts.
1133
            $pics_processed = array();
1134
1135
            while ($r = sql_fetch_assoc($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_assoc() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1136
                $pics_restored = false;
1137
1138
                // the picture id may have changed by multiple delete-and-restores
1139
                $revert_picid = get_current_picid($r['id']);
1140
                if (!in_array($revert_picid, $pics_processed)) {
1141
                    // .. as may have its uuid-based url
1142
                    $revert_url = sql_value(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1143
                        "SELECT `url` FROM `pictures_modified` WHERE `id`='&1'",
1144
                        $r['url'],
1145
                        $revert_picid
1146
                    );
1147
                    $error = "";
1148
1149
                    switch ($r['operation']) {
1150
                        case 'I':
1151
                            if (sql_value("SELECT `id` FROM `pictures` WHERE `id`='&1'", 0, $revert_picid) != 0) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1152
                                // if it was not already deleted by a later restore operation:
1153
                                // delete added (cache) picture
1154
                                $pic = new picture($revert_picid);
1155 View Code Duplication
                                if ($simulate) {
1156
                                    $pics_restored = true;
1157
                                } else {
1158
                                    if ($pic->delete(true)) {
1159
                                        $pics_restored = true;
1160
                                    } else {
1161
                                        $error = "delete";
1162
                                    }
1163
                                }
1164
                            }
1165
                            break;
1166
1167
                        case 'U':
1168
                            if (sql_value("SELECT `id` FROM `pictures` WHERE `id`='&1'", 0, $revert_picid) != 0) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1169
                                // if it was not deleted by a later restore operation:
1170
                                // restore modified (cache) picture properties
1171
                                $pic = new picture($revert_picid);
1172
                                $pic->setTitle($r['title']);
1173
                                $pic->setSpoiler($r['spoiler']);
1174
                                $pic->setDisplay($r['display']);
1175
                                // mappreview flag is not restored, because it seems unappropriate to
1176
                                // advertise for the listing of a vandalizing owner
1177
1178 View Code Duplication
                                if ($simulate) {
1179
                                    $pics_restored = true;
1180
                                } else {
1181
                                    if ($pic->save(true)) {
1182
                                        $pics_restored = true;
1183
                                    } else {
1184
                                        $error = "update";
1185
                                    }
1186
                                }
1187
                            }
1188
                            break;
1189
1190
                        case 'D':
1191
                            if (sql_value("SELECT `id` FROM `pictures` WHERE `id`='&1'", 0, $revert_picid) == 0) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1192
                                // if it was not already restored by a later restore operation:
1193
                                // restore deleted picture
1194
                                // id, uuid, date_created and last_modified are set automatically
1195
1196
                                // the referring log's id  may have changed by [multiple] delete-and-restore
1197
                                if ($r['object_type'] == 1) {
1198
                                    $r['object_id'] = get_current_logid($r['object_id']);
1199
                                }
1200
1201
                                // id, uuid, node, date_created, date_modified are automatically set;
1202
                                // url will be set on save;
1203
                                // last_url_check and thumb_last_generated stay at defaults until checked;
1204
                                // thumb_url will be set on thumb creation (old thumb was deleted)
1205
                                $pic = new picture();
1206
                                $pic->setTitle($r['title']);
1207
                                $pic->setObjectId($r['object_id']);
1208
                                $pic->setObjectType($r['object_type']);
1209
                                $pic->setSpoiler($r['spoiler']);
1210
                                $pic->setLocal(1);
1211
                                $pic->setUnknownFormat($r['unknown_format']);
1212
                                $pic->setDisplay($r['display']);
1213
                                // mappreview flag is not restored, because it seems unappropriate to
1214
                                // advertise for the listing of a vandalizing owner
1215
1216
                                if ($simulate) {
1217
                                    $pics_restored = true;
1218
                                } else {
1219
                                    if ($pic->save(true, $revert_picid, $revert_url)) {
1220
                                        $pics_restored = true;
1221
                                        $pics_processed[] = $pic->getPictureId();
1222
                                    } else {
1223
                                        $error = "restore";
1224
                                    }
1225
                                }
1226
                            }
1227
                            break;
1228
                    }  // switch
1229
1230
                    $pics_processed[] = $revert_picid;
1231
                }  // not already processed
1232
1233
                if ($error != '') {
0 ignored issues
show
Bug introduced by
The variable $error does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1234
                    $restored[$wp]['internal error - could not $error picture ' . $r['id'] . '/' . $picid] = true;
0 ignored issues
show
Bug introduced by
The variable $picid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
1235
                }
1236
                if ($pics_restored) {
1237
                    $restored[$wp]['pictures'] = true;
1238
                }
1239
            }  // while (all relevant pic records)
1240
1241
            sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1242
        }  // if pics enabled per roptions
1243
    }  // foreach cache(id)
1244
1245
    sql('SET @restoredby=0');
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1246
    sql_slave('SET @restoredby=0');
0 ignored issues
show
Deprecated Code introduced by
The function sql_slave() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1247
1248
    return $restored;
1249
}
1250
1251
1252
// determine new id of a log if it has been deleted and restored [multiple times]
1253
function get_current_logid($logid)
1254
{
1255
    do {
1256
        $new_logid = sql_value(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1257
            "SELECT `id` FROM `cache_logs_restored` WHERE `original_id`='&1'",
1258
            0,
1259
            $logid
1260
        );
1261
        if ($new_logid != 0) {
1262
            $logid = $new_logid;
1263
        }
1264
    } while ($new_logid != 0);
1265
1266
    return $logid;
1267
}
1268
1269
1270
// determine new id of a picture if it has been deleted and restored [multiple times]
1271
function get_current_picid($picid)
1272
{
1273
    do {
1274
        $new_picid = sql_value(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1275
            "SELECT `id` FROM `pictures_modified` WHERE `original_id`='&1'",
1276
            0,
1277
            $picid
1278
        );
1279
        if ($new_picid != 0) {
1280
            $picid = $new_picid;
1281
        }
1282
    } while ($new_picid != 0);
1283
1284
    return $picid;
1285
}
1286