Completed
Push — development ( f93eb8...ffa1a0 )
by Thomas
20s
created

htdocs/api/gc2oc.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 *
5
 *
6
 *  returns GC -> OC waypoint translation table, if available,
7
 *  or reports a GC waypoint to the team
8
 *
9
 *  file format: gcwp,ocwp,checked
10
 *  checked = 1 if the GC waypoint has been verified,
11
 *            0 if it is directly taken from an OC cache listing.
12
 *
13
 ***************************************************************************/
14
15
$opt['rootpath'] = __DIR__ . '/../';
16
require __DIR__ . '/../lib2/web.inc.php';
17
18
if (isset($_REQUEST['report']) && $_REQUEST['report']) {
19
    header('Content-type: text/plain');
20
21
    if ($opt['cron']['gcwp']['report']) {
22
        if (isset($_REQUEST['ocwp']) && isset($_REQUEST['gcwp']) && isset($_REQUEST['source'])) {
23
            $ocWp = trim($_REQUEST['ocwp']);
24
            $gcWp = trim($_REQUEST['gcwp']);
25
            $source = trim($_REQUEST['source']);
26
27
            if (!preg_match('/^OC[0-9A-F]{4,6}$/', $ocwp)) {
28
                echo "error: invalid ocwp\n";
29
            } elseif (!sql_value("SELECT 1 FROM `caches` WHERE `wp_oc` = '&1'", 0, $ocWp)) {
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...
30
                echo "error: unknown ocwp\n";
31
            } elseif (!preg_match('/^GC[0-9A-HJ-NPQRTVWXYZ]{3,7}$/', $gcWp)) {
32
                echo "error: invalid gcwp\n";
33
            } else {
34
                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...
35
                    "
36
                    INSERT INTO `waypoint_reports`
37
                    (`date_reported`, `wp_oc`, `wp_external`, `source`)
38
                    VALUES (NOW(), '&1', '&2', '&3')",
39
                    $ocWp,
40
                    $gcWp,
41
                    $source
42
                );
43
                echo 'ok';
44
            }
45
        } else {
46
            echo 'error: missing parameter(s)';
47
        }
48
    }
49
} else {
50
    header('Content-type: application/x-gzip');
51
    header('Content-Disposition: attachment; filename=gc2oc.gz');
52
53
    /*
54
     * caches.wp_gc_maintained is intended for map and search filtering and
55
     * therefore e.g. does not contain WPs of active OC listings that are
56
     * archived at GC. So it is not useful for GC->OC translation.
57
     * If a better external source is available, we can use data from there.
58
     *
59
     * This may be refined by combining different sources and/or internal data.
60
     * Also, it may be optimized by allowing to request a single GC code or a
61
     * list of GC codes.
62
     *
63
     * Note that it is not possible to create a 100% reliable translation table.
64
     * There are many wrong GC wps in listings, and maintained tables always
65
     * are incomplete. DO NOT RELY ON THE CORRECTNESS OF SUCH DATA!
66
     */
67
68
    if ($opt['cron']['gcwp']['fulllist']) {
69
        $gzipped_data = '';
70
        $cacheFile = '../var/cache2/gc2oc.gz';
71
        if (!file_exists($cacheFile) || time() - filemtime($cacheFile) > 3600 * 4) {
72
            $gc2oc = file_get_contents($opt['cron']['gcwp']['fulllist']);
73
            if ($gc2oc) {
74
                $gzipped_data = gzencode($gc2oc);
75
                file_put_contents($cacheFile, $gzipped_data);
76
            }
77
        }
78
79
        if (!$gzipped_data && file_exists($cacheFile)) {
80
            $gzipped_data = file_get_contents($cacheFile);
81
        }
82
83
        echo $gzipped_data;
84
    }
85
}
86