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