Completed
Pull Request — master (#9)
by
unknown
01:19
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">
23
                        <option value="" selected disabled hidden>Choose here</option>
24
                        <?php foreach($langcodes as $language)
25
                        {
26
                            echo"<option value='$language[0]'>$language[1]</option>";
27
                        }?>
28
                    </select>
29
                </div>
30
            </div>
31
            <div class="form-group">
32
                <label class="col-md-4 control-label" for="dir">Directories:</label>
33
                <div class="col-md-4">
34
                <select id="dir" name="dir" class="form-control">
35
                    <option value="1">base, boot</option>
36
                    <option value="2" <?php if (isset($_GET['dir']) && $_GET['dir'] == '2') {
37
    echo 'selected';
38
}?>>dll</option>
39
                    <option value="3" <?php if (isset($_GET['dir']) && $_GET['dir'] == '3') {
40
    echo 'selected';
41
}?>>media, subsystems, win32ss</option>
42
                    <option value="100" <?php if (isset($_GET['dir']) && $_GET['dir'] == '100') {
43
    echo 'selected';
44
}?>>All ReactOS Source dir</option>
45
                </select>
46
                </div>
47
            </div>
48
            <div class="form-group">
49
                <button type="submit" class="btn btn-primary">Search</button>
50
            </div>
51
        </fieldset>
52
    </form>
53
</center>
54
<br>
55
56
<?php
57
if (isset($_GET['lang']) && !empty($_GET['lang']) && isset($_GET['dir']) && is_numeric($_GET['dir'])) {
58
    $it = new AppendIterator();
59
60
    // Switch for directories
61 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...
62
        case '1':
63
            $directories = [
64
                new RecursiveDirectoryIterator($ROSDir.'base/applications'),
65
                new RecursiveDirectoryIterator($ROSDir.'base/setup'),
66
                new RecursiveDirectoryIterator($ROSDir.'base/shell'),
67
                new RecursiveDirectoryIterator($ROSDir.'base/system'),
68
                new RecursiveDirectoryIterator($ROSDir.'boot/freeldr/fdebug'),
69
            ];
70
            break;
71
72
        case '2':
73
            $directories = [
74
                new RecursiveDirectoryIterator($ROSDir.'dll/cpl'),
75
                new RecursiveDirectoryIterator($ROSDir.'dll/shellext'),
76
                new RecursiveDirectoryIterator($ROSDir.'dll/win32'),
77
            ];
78
            break;
79
80
        case '3':
81
            $directories = [
82
                new RecursiveDirectoryIterator($ROSDir.'media/themes'),
83
                new RecursiveDirectoryIterator($ROSDir.'subsystems/mvdm/ntvdm'),
84
                new RecursiveDirectoryIterator($ROSDir.'win32ss/user'),
85
            ];
86
            break;
87
88
        case '100':
89
            $directories = [
90
                new RecursiveDirectoryIterator($ROSDir),
91
            ];
92
            break;
93
94
        default:
95
            echo 'Something is wrong! Please try again.';
96
            exit;
97
    }
98
99
    foreach ($directories as $directory) {
100
        $it->append(new RecursiveIteratorIterator($directory));
101
    }
102
103
    $regex = new RegexIterator($it, '/^.+'.$langDir.'.+('.$originLang.')\.'.$fileExt.'$/i', RecursiveRegexIterator::GET_MATCH);
104
105
    $allWrongEnc = 0;
106
107
    $lang = htmlspecialchars($_GET['lang']);
108
    // Search for eg. PL,Pl,pl
109
    $fileSearch = strtoupper($lang).','.ucfirst($lang).','.strtolower($lang);
110
111
    // UTF-8 BOM starts with EF BB BF
112
    define('UTF8_BOM', chr(0xEF).chr(0xBB).chr(0xBF));
113
114
    $regex->rewind();
115
    while ($regex->valid()) {
116
        if (!$regex->isDot()) {
117
            $file = glob($regex->getPathInfo().'/*{'.$fileSearch.'}*.'.$fileExt, GLOB_BRACE);
118
119
            $isFile = array_filter($file);
120
121
            if (!empty($isFile)) {
122
                $text = file_get_contents($file[0]);
123
                // UTF-8 is good
124
                if (mb_check_encoding($text, 'UTF-8')) {
125
                    $first3 = substr($text, 0, 3);
126
                    // But UTF-8 with BOM not!
127
                    if ($first3 === UTF8_BOM) {
128
                        echo 'Detected <b>UTF-8 BOM</b> in '.$file[0].'<br>';
129
                        $allWrongEnc++;
130
                    }
131
                } else {
132
                    // Other encoding
133
                    echo 'Detected <b>other encoding</b> in '.$file[0].'<br>';
134
                    $allWrongEnc++;
135
                }
136
            }
137
        }
138
        $regex->next();
139
    }
140
    echo "<h3>All files with wrong encoding: $allWrongEnc</h3>";
141
}
142
143
include_once 'footer.php';
144