Completed
Push — master ( 914472...99cacd )
by Adam
01:40 queued 14s
created

encoding.php (1 issue)

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
/* PROJECT:     ReactOS Translation Tool
3
 * LICENSE:     GPL
4
 * AUTHORS:     Adam Stachowicz <[email protected]>
5
 * AUTHOR URL:  http://it-maniak.pl/
6
 */
7
8
include_once 'header.php';
9
include_once 'langcodes.php';
10
?>
11
12
<h1>Search for wrong encoded files</h1>
13
14
<div id="body">
15
<center>
16
    <form method="GET" class="form-horizontal">
17
        <fieldset>
18
            <legend>Please choose your language and the directories, where you want to search for untranslated strings, from the lists below.</legend>
19
            <div class="form-group">
20
                <label class="col-md-4 control-label" for="lang">Language:</label>
21
                <div class="col-md-4">
22
                    <select id="lang" name="lang" class="form-control" required="required">
23 View Code Duplication
                        <?php foreach ($langcodes as $language) {
24
    echo '<option value="'.$language[0].'" ';
25
    if (isset($_SESSION['lang']) && $language[0] == $_SESSION['lang']) {
26
        echo 'selected';
27
    }
28
    echo '> $language[1]</option>';
29
}?>
30
                    </select>
31
                </div>
32
            </div>
33
            <div class="form-group">
34
                <label class="col-md-4 control-label" for="dir">Directories:</label>
35
                <div class="col-md-4">
36
                <select id="dir" name="dir" class="form-control">
37
                    <option value="1">base, boot</option>
38
                    <option value="2" <?php if (isset($_GET['dir']) && $_GET['dir'] == '2') {
39
    echo 'selected';
40
}?>>dll</option>
41
                    <option value="3" <?php if (isset($_GET['dir']) && $_GET['dir'] == '3') {
42
    echo 'selected';
43
}?>>media, subsystems, win32ss</option>
44
                    <option value="100" <?php if (isset($_GET['dir']) && $_GET['dir'] == '100') {
45
    echo 'selected';
46
}?>>All ReactOS Source dir</option>
47
                </select>
48
                </div>
49
            </div>
50
            <div class="form-group">
51
                <button type="submit" class="btn btn-primary">Search</button>
52
            </div>
53
        </fieldset>
54
    </form>
55
</center>
56
<br>
57
58
<?php
59
if (isset($_GET['lang']) && !empty($_GET['lang']) && isset($_GET['dir']) && is_numeric($_GET['dir'])) {
60
    $it = new AppendIterator();
61
62
    // Switch for directories
63 View Code Duplication
    switch ($_GET['dir']) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
        case '1':
65
            $directories = [
66
                new RecursiveDirectoryIterator($ROSDir.'base/applications'),
67
                new RecursiveDirectoryIterator($ROSDir.'base/setup'),
68
                new RecursiveDirectoryIterator($ROSDir.'base/shell'),
69
                new RecursiveDirectoryIterator($ROSDir.'base/system'),
70
                new RecursiveDirectoryIterator($ROSDir.'boot/freeldr/fdebug'),
71
            ];
72
            break;
73
74
        case '2':
75
            $directories = [
76
                new RecursiveDirectoryIterator($ROSDir.'dll/cpl'),
77
                new RecursiveDirectoryIterator($ROSDir.'dll/shellext'),
78
                new RecursiveDirectoryIterator($ROSDir.'dll/win32'),
79
            ];
80
            break;
81
82
        case '3':
83
            $directories = [
84
                new RecursiveDirectoryIterator($ROSDir.'media/themes'),
85
                new RecursiveDirectoryIterator($ROSDir.'subsystems/mvdm/ntvdm'),
86
                new RecursiveDirectoryIterator($ROSDir.'win32ss/user'),
87
            ];
88
            break;
89
90
        case '100':
91
            $directories = [
92
                new RecursiveDirectoryIterator($ROSDir),
93
            ];
94
            break;
95
96
        default:
97
            echo 'Something is wrong! Please try again.';
98
            exit;
99
    }
100
101
    foreach ($directories as $directory) {
102
        $it->append(new RecursiveIteratorIterator($directory));
103
    }
104
105
    $regex = new RegexIterator($it, '/^.+'.$langDir.'.+('.$originLang.')\.'.$fileExt.'$/i', RecursiveRegexIterator::GET_MATCH);
106
107
    $allWrongEnc = 0;
108
109
    $lang = htmlspecialchars($_GET['lang']);
110
    // Search for eg. PL,Pl,pl
111
    $fileSearch = strtoupper($lang).','.ucfirst($lang).','.strtolower($lang);
112
113
    // UTF-8 BOM starts with EF BB BF
114
    define('UTF8_BOM', chr(0xEF).chr(0xBB).chr(0xBF));
115
116
    $regex->rewind();
117
    while ($regex->valid()) {
118
        if (!$regex->isDot()) {
119
            $file = glob($regex->getPathInfo().'/*{'.$fileSearch.'}*.'.$fileExt, GLOB_BRACE);
120
121
            $isFile = array_filter($file);
122
123
            if (!empty($isFile)) {
124
                $text = file_get_contents($file[0]);
125
                // UTF-8 is good
126
                if (mb_check_encoding($text, 'UTF-8')) {
127
                    $first3 = substr($text, 0, 3);
128
                    // But UTF-8 with BOM not!
129
                    if ($first3 === UTF8_BOM) {
130
                        echo 'Detected <b>UTF-8 BOM</b> in '.$file[0].'<br>';
131
                        $allWrongEnc++;
132
                    }
133
                } else {
134
                    // Other encoding
135
                    echo 'Detected <b>other encoding</b> in '.$file[0].'<br>';
136
                    $allWrongEnc++;
137
                }
138
            }
139
        }
140
        $regex->next();
141
    }
142
    echo "<h3>All files with wrong encoding: $allWrongEnc</h3>";
143
}
144
145
include_once 'footer.php';
146