summaryrefslogtreecommitdiffstats
path: root/common.php
blob: 9d18e5fc536e84fdbe8d060e66d86ac70f800d4f (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * Copyright (C) 2010 Jonas Häggqvist
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 **************************************************************************/

define("PERL", '/usr/bin/perl');
define("STATS", 'scratch/stats.dat');
define("VERSIONS", 'scratch/versions.dat');
define("SMALL_FLAGSIZE", '22');
define("LARGE_FLAGSIZE", '150');
define("SMARTY_PATH", '/usr/share/php/Smarty');

require_once('templater.class.php');
$smarty = new templater(SMARTY_PATH);
$smarty->assign('languages', languageinfo());
$smarty->assign('updated', file_exists(STATS) ? filemtime(STATS) : 0);

function languageinfo() {
    return parse_ini_file('languages.ini', true);
}

function parselangfile($filename, $all = false) {
    $lines = @file($filename);
    if (!is_array($lines)) {
        return false;
    }
    $phrases = array();
    $empty = array(
        'source' => array(),
        'dest' => array(),
        'voice' => array(),
        'notes' => array(),
    );
    $thisphrase = $empty;

    $pos = 'phrase';

    foreach($lines as $lineno => $line) {
        $line = trim($line);

	if ($all == true) {
            $thisphrase['notes'][0] = "All fields are editable";
        }
        if (preg_match("/^### (.*)$/", $line, $matches)) {
            if (strpos($matches[1], "The phrase is not used. Skipped") === false) {
                $thisphrase['notes'][] = $matches[1];
            }
        }
        elseif (preg_match("/^ *#/", $line, $matches)) {
            continue;
        }
        elseif ($pos == 'phrase' && preg_match("/^([^:]+): ?(.*)$/", $line, $matches)) {
            $thisphrase[$pos][$matches[1]] = $matches[2];
        }
        elseif ($pos != 'phrase' && preg_match("/^([^:]+): ?\"?([^\"]*)\"?$/", $line, $matches)) {
            $subs = explode(',' , $matches[1]);
            foreach($subs as $sub) {
                $sub = trim($sub);
                $thisphrase[$pos][$sub] = $matches[2];
            }
        }

        switch($line) {
            case '</voice>':
            case '</dest>':
            case '</source>':
            case '<phrase>': $pos = 'phrase'; break;
            case '</phrase>':
                $phrases[$thisphrase['phrase']['id']] = $thisphrase;
                $thisphrase = $empty;
                $pos = 'lang';
                break;
            case '<source>': $pos = 'source'; break;
            case '<dest>': $pos = 'dest'; break;
            case '<voice>': $pos = 'voice'; break;
        }
    }
    return $phrases;
}

function combinetgts($tgtmap) {
    $strmap = array();
    $combined = array();

    foreach($tgtmap as $tgt => $string) {
        if ($tgt == '*') { continue; }
        if (isset($strmap[$string])) {
            $strmap[$string] .= ",$tgt";
        } else {
            $strmap[$string] = "$tgt";
        }
    }

    $combined['*'] = $tgtmap['*'];
    foreach ($strmap as $string => $tgt) {
        $combined[$tgt] = $string;
    }

    return $combined;
}

function printphrase($phrase) {
    $ret = '';
    $ret .= sprintf("<phrase>\n  id: %s\n  desc:%s\n  user:%s\n",
        $phrase['phrase']['id'],
        isset($phrase['phrase']['desc']) && $phrase['phrase']['desc'] != "" ? ' '.$phrase['phrase']['desc'] : '',
        isset($phrase['phrase']['user']) && $phrase['phrase']['user'] != "" ? ' '.$phrase['phrase']['user'] : ''
    );

    foreach(array('source', 'dest', 'voice') as $field) {
        $ret .= sprintf("  <%s>\n", $field);
        if (isset($phrase[$field])) {
            $phrase[$field] = combinetgts($phrase[$field]);
            /* If '*' is empty, we don't catch it on the edit-page */
            if (!isset($phrase[$field]['*'])) {
                $ret .= "    *: \"\"\n";
            }
            foreach($phrase[$field] as $target => $string) {
                if ($target == 'desc' || $target == 'user' || $target == 'id') continue;
                if (trim($string) == 'none') { $string = 'none'; }
                elseif (strtolower(trim($string)) == 'deprecated') { $string = 'deprecated'; }
                $format = ($string == 'none' || $string == 'deprecated' ? "    %s: %s\n" : "    %s: \"%s\"\n");
                    $ret .= sprintf($format, $target, $string);
            }
        }
        else {
            $ret .= "    *: \"\"\n";
        }
        $ret .= sprintf("  </%s>\n", $field);
    }
    $ret .= "</phrase>\n";
    return $ret;
}

function print_head() {
header("Content-type: text/html; charset=UTF-8");
echo <<<END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Rockbox Translations</title>
<link rel="stylesheet" href="rockbox.css" />
</head>
<body>
END;
}

function print_foot() {
$date = date('D M j H:i:s T Y', file_exists(STATS) ? filemtime(STATS) : 0);
echo <<<END
<hr />
<a href="//www.rockbox.org">
  <img src="//www.rockbox.org/rockbox100.png" border="0" width="100" height="32" alt="www.rockbox.org" title="Rockbox - Open Source Jukebox Firmware" />
</a>
<small>
Last updated $date. Flags copyright Wikimedia contributors.
</small>

</body>
</html>
END;
}
?>