Completed
Push — master ( 394ecf...2ae0a1 )
by Adam
02:35 queued 01:17
created

encoding.php (2 issues)

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