summaryrefslogtreecommitdiffstats
path: root/maintdb.php
blob: 77a38caf9e8216e38a0038d7661d63bd99799197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
 * Mageia maintdb quick filter.
 * Make use of http://pkgsubmit.mageia.org/data/maintdb.txt data.
 *
 * This only supports mapping 1 user to several packages
 * or 1 package to 1 user.
 *
 * @param string $uid (GET param) user id
 * @param string $pkg (GET param) package name
 * @param mixed  $json (GET param) whether to return result in text or JSON
 *
 * @return string 
 *
 * Returned format is either text format:
 * <code>
 * package_name user_name
 * package_name2 user_name2
 * </code>
 *
 * either JSON format:
 * <code>
 * {"user_name": ["package_name1", "package_name2"]}
 * </code>
 * 
 * TODO check if preg_match_all() is more efficient than exec('grep ...')
 * TODO if so, check security concerns for $uid and $pkg
 *
 * @copyright Copyright (C) 2011 Mageia.Org
 * @author Romain d'Alverny
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License aspublished by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
*/

/** Path to maintdb.txt */
$maintdb = realpath(__DIR__) . '/data/maintdb.txt';

/** User name */
$uid = isset($_GET['uid']) ? trim(htmlentities(strip_tags($_GET['uid']))) : null;

/** Package name */
$pkg = isset($_GET['pkg']) ? trim(htmlentities(strip_tags($_GET['pkg']))) : null;

/** Return format */
$json = isset($_GET['json']) ? true : false;

/** Returned data */
$return  = null;

$s = file_get_contents($maintdb);

if (null !== $uid) {
    if (preg_match_all(sprintf('/(.*) %s\n?/', $uid), $s, $res)) {
        $return = array($uid => $res[1]);
    }
} elseif (null !== $pkg) {
    if (preg_match_all(sprintf('/%s (.*)\n?/', $pkg), $s, $res)) {
        $return = sprintf('%s %s', $res[1][0], $pkg);
    }
}

if ($json) {
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($return);
}
else {
    header('Content-Type: text/plain; charset: utf-8');
    if (is_array($return)) {
        foreach ($return as $u => $packages) {
            foreach ($packages as $p) {
                echo sprintf("%s %s\n", $p, $u);
            }
        }
    } else {
        echo $return;
    }
}