Source code taken from cacert-20130227.tar.bz2
[cacert-devel.git] / locale / escape_special_chars.php
1 #!/usr/bin/php -q
2 <?php
3 /*
4 LibreSSL - CAcert web application
5 Copyright (C) 2004-2012 CAcert Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; version 2 of the License.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /* Convert special characters in UTF-8 encoded PO files to HTML entities */
22
23 define('MSGSTR', 'msgstr');
24 define('MSGSTR_LEN', strlen(MSGSTR));
25 define('MSGID', 'msgid');
26 define('MSGID_LEN', strlen(MSGID));
27
28 function is_msgstr($line) {
29 if (strlen($line) < MSGSTR_LEN) {
30 return false;
31 }
32
33 return substr_compare($line, MSGSTR, 0, MSGSTR_LEN) === 0;
34 }
35
36 function is_msgid($line) {
37 if (strlen($line) < MSGID_LEN) {
38 return false;
39 }
40
41 return substr_compare($line, MSGID, 0, MSGID_LEN) === 0;
42 }
43
44
45 ////////////// Main //////////////
46
47 // Skip the metadata (first msgid/msgstr pair)
48 while (!feof(STDIN)) {
49 $line = fgets(STDIN);
50
51 echo $line;
52
53 if (is_msgstr($line)) {
54 break;
55 }
56 }
57
58 // determines if the current line belongs to a msgid or a msgstr
59 $msgstr = false;
60
61 while (!feof(STDIN)) {
62 $line = fgets(STDIN);
63
64 if (is_msgstr($line)) {
65 $msgstr = true;
66 } elseif (is_msgid($line)) {
67 $msgstr = false;
68 }
69
70 if ($msgstr) {
71 // Escape everything that has a special HTML entity such as
72 // &gt; or &auml; except quote characters
73 $line = htmlentities($line, ENT_NOQUOTES, "UTF-8");
74
75 // Escape everything else -> all characters that don't have a special
76 // HTML entity but are outside the ASCII range
77 $line = mb_convert_encoding($line, "HTML-ENTITIES", "UTF-8");
78 }
79 echo $line;
80 }