Completed
Push — master ( 4c73cf...fabd1c )
by Adam
01:31
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
?>
10
11
<h1>Search for wrong encoded files</h1>
12
13
<div id="body">
14
15
<?php
16
17
 require_once('config.php');
18
?>
19
20
<center>
21
    <form method="GET" class="form-horizontal">
22
        <fieldset>
23
            <legend>Please type your <a href="https://beta.wikiversity.org/wiki/List_of_ISO_639-1_codes">language code in ISO 639-1</a>. For example: pl for Polish, de for German</legend>
24
            <div class="form-group">
25
                <label class="col-md-4 control-label" for="lang">Language code:</label>
26
                <div class="col-md-4">
27
                    <input type="text" value="<?php echo isset($_SESSION['lang']) ? $_SESSION['lang'] : '' ?>" id="lang" name="lang" class="form-control input-md" required="required" autofocus="autofocus" pattern="[A-Za-z]{2}" title="Two letter language code"/>
28
                </div>
29
            </div>
30
            <div class="form-group">
31
                <label class="col-md-4 control-label" for="dir">Directories:</label>
32
                <div class="col-md-4">
33
                <select id="dir" name="dir" class="form-control">
34
                    <option value="1">base, boot</option>
35
                    <option value="2" <?php if (isset($_GET['dir']) && $_GET['dir'] == '2') {
36
    echo 'selected';
37
}?>>dll</option>
38
                    <option value="3" <?php if (isset($_GET['dir']) && $_GET['dir'] == '3') {
39
    echo 'selected';
40
}?>>media, subsystems, win32ss</option>
41
                    <option value="100" <?php if (isset($_GET['dir']) && $_GET['dir'] == '100') {
42
    echo 'selected';
43
}?>>All ReactOS Source dir</option>
44
                </select>
45
                </div>
46
            </div>
47
            <div class="form-group">
48
                <button type="submit" class="btn btn-primary">Search</button>
49
            </div>
50
        </fieldset>
51
    </form>
52
</center>
53
<br>
54
55
<?php
56
if (isset($_GET['lang']) && !empty($_GET['lang']) && isset($_GET['dir']) && is_numeric($_GET['dir'])) {
57
    // Switch for directories
58 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...
59
        case '1':
60
            $directory1 = new RecursiveDirectoryIterator($ROSDir.'base/applications');
61
            $directory2 = new RecursiveDirectoryIterator($ROSDir.'base/setup');
62
            $directory3 = new RecursiveDirectoryIterator($ROSDir.'base/shell');
63
            $directory4 = new RecursiveDirectoryIterator($ROSDir.'base/system');
64
            $directory5 = new RecursiveDirectoryIterator($ROSDir.'boot/freeldr/fdebug');
65
66
            $it = new AppendIterator();
67
            $it->append(new RecursiveIteratorIterator($directory1));
68
            $it->append(new RecursiveIteratorIterator($directory2));
69
            $it->append(new RecursiveIteratorIterator($directory3));
70
            $it->append(new RecursiveIteratorIterator($directory4));
71
            $it->append(new RecursiveIteratorIterator($directory5));
72
            break;
73
74
        case '2':
75
            $directory6 = new RecursiveDirectoryIterator($ROSDir.'dll/cpl');
76
            $directory7 = new RecursiveDirectoryIterator($ROSDir.'dll/shellext');
77
            $directory8 = new RecursiveDirectoryIterator($ROSDir.'dll/win32');
78
79
            $it = new AppendIterator();
80
            $it->append(new RecursiveIteratorIterator($directory6));
81
            $it->append(new RecursiveIteratorIterator($directory7));
82
            $it->append(new RecursiveIteratorIterator($directory8));
83
            break;
84
85
        case '3':
86
            $directory9 = new RecursiveDirectoryIterator($ROSDir.'media/themes');
87
            $directory10 = new RecursiveDirectoryIterator($ROSDir.'subsystems/mvdm/ntvdm');
88
            $directory11 = new RecursiveDirectoryIterator($ROSDir.'win32ss/user');
89
90
            $it = new AppendIterator();
91
            $it->append(new RecursiveIteratorIterator($directory9));
92
            $it->append(new RecursiveIteratorIterator($directory10));
93
            $it->append(new RecursiveIteratorIterator($directory11));
94
            break;
95
96
        // Search in source dir - only for test
97
        case '100':
98
            $directory = new RecursiveDirectoryIterator($ROSDir);
99
100
            $it = new AppendIterator();
101
            $it->append(new RecursiveIteratorIterator($directory));
102
            break;
103
104
        default:
105
            echo 'Something is wrong! Please try again.';
106
            exit;
107
    }
108
109
    $regex = new RegexIterator($it, '/^.+'.$langDir.'.+('.$originLang.')\.'.$fileExt.'$/i', RecursiveRegexIterator::GET_MATCH);
110
111
    $allWrongEnc = 0;
112
113
    $lang = htmlspecialchars($_GET['lang']);
114
    // Search for eg. PL,Pl,pl
115
    $fileSearch = strtoupper($lang).','.ucfirst($lang).','.strtolower($lang);
116
117
    // UTF-8 BOM starts with EF BB BF
118
    define('UTF8_BOM', chr(0xEF).chr(0xBB).chr(0xBF));
119
120
    $regex->rewind();
121
    while ($regex->valid()) {
122
        if (!$regex->isDot()) {
123
            $file = glob($regex->getPathInfo().'/*{'.$fileSearch.'}*.'.$fileExt, GLOB_BRACE);
124
125
            $isFile = array_filter($file);
126
127
            if (!empty($isFile)) {
128
                $text = file_get_contents($file[0]);
129
                // UTF-8 is good
130
                if (mb_check_encoding($text, 'UTF-8')) {
131
                    $first3 = substr($text, 0, 3);
132
                    // But UTF-8 with BOM not!
133
                    if ($first3 === UTF8_BOM) {
134
                        echo 'Detected <b>UTF-8 BOM</b> in '.$file[0].'<br>';
135
                        $allWrongEnc++;
136
                    }
137
                } else {
138
                    // Other encoding
139
                    echo 'Detected <b>other encoding</b> in '.$file[0].'<br>';
140
                    $allWrongEnc++;
141
                }
142
            }
143
        }
144
        $regex->next();
145
    }
146
    echo "<h3>All files with wrong encoding: $allWrongEnc</h3>";
147
}
148
149
include_once 'footer.php';
150