-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataSanitizer.inc.php
More file actions
259 lines (240 loc) · 9.12 KB
/
dataSanitizer.inc.php
File metadata and controls
259 lines (240 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/* Copyright (c) 2012 Association France-ioi, MIT License http://opensource.org/licenses/MIT */
class DataSanitizer
{
// Format firstName and lastName
// Will try to format both names
//
// return (firstName, lastName, saniValid)
static function formatUserNames($firstName, $lastName)
{
$saniValid = 1;
$msgs = "";
foreach (array("firstName", "lastName") as $field)
{
// Let's try to sanitize it
try {
$$field = DataSanitizer::formatName($$field);
}
catch (Exception $e) {
// Oups : do not modify it, it will be checked in the future
// Statistically : 0.5% of names only
$saniValid = 0;
$msgs .= $e->getMessage().";";
}
}
return array($firstName, $lastName, $saniValid, $msgs);
}
static function formatSchool($name, $city, $country)
{
$saniValid = 1;
$msgs = "";
foreach (array("city", "country") as $field)
{
// Let's try to sanitize it
try {
$$field = DataSanitizer::formatNameComplex($$field);
}
catch (Exception $e) {
// Oups : do not modify it, it will be checked in the future
// Statistically : 0.5% of names only
$saniValid = 0;
$msgs .= $e->getMessage().";";
}
}
// The name has been fixed, let's try to format the school category
try {
$name = DataSanitizer::postFormatSchoolName($name);
}
catch (Exception $e) {
$saniValid = 0;
$msgs .= $e->getMessage().";";
}
return array($name, $city, $country, $saniValid, $msgs);
}
////////// Data sanitizing //////////
// Some special variables
static $particles = array("de", "du");
static $wordsNoMaj = array();//array_merge(self::$particles, array("le", "la", "et", "les", "lès"));
static $apostrophe = array("d", "l"); // d', l'
// Check if the given string respect the following "grammar"
// Will clean and apply correct casing to the string
//
// - The string must be a set of words separated by spaces.
// - Each word can be a set of sub-words separated by a dash.
// - Each subword can start with an apostrophe "l', "d'"...
// and must be constituted only of letters.
// - If "$allowNumbers" is true, number are also authorized at the start of each word.
// For example : "12ème" of "Cedex 20"
// - Some special words will not start with an uppercase letter : "de", "sur"...
//
// THROW AN EXCEPTION IF THE THIS FORMAT IS NOT RESPECTED
static function formatName($s, $allowNumbers = false)
{
$s = self::formatNameRec($s, $allowNumbers);
// First letter must be upper
$s = self::strtoupper(mb_substr($s, 0, 1)).mb_substr($s, 1);
return $s;
}
// Similar to "formatName" but we also accept parenthesis
// in the string. The parenthesis must contains valid words.
// For example :
// "Reunion Island (France)"
static function formatNameComplex($s, $allowNumbers = true)
{
$dummmy = "Azerty";
// We allow some parenthesis in the name => country, city..
preg_match_all('/\(([^\)]*)\)/', $s, $matches);
$s = preg_replace('/\([^\)]*\)/', $dummmy, $s);
$s = self::formatName($s, $allowNumbers);
foreach ($matches[1] as $match)
{
$s = preg_replace("/$dummmy/", "(".self::formatName($match, $allowNumbers).")", $s, 1);
}
return $s;
}
// Utility function to remove un-necessary spaces
static function rmMultipleSpaces($s)
{
$s = trim($s);
$s = preg_replace('/ +/', ' ', $s);
$s = preg_replace('/ $/', '', $s);
return $s;
}
// Utility function to extract the optional apostrophe
// Will return:
// - the apotrophe letter (with the apostrophe in correct case
// - the rest of the word
static function getApos($oriWord)
{
$word = $oriWord;
if (mb_substr($word, 1, 1) != "'")
return array("", $word);
$letter = self::strtoupper(mb_substr($word, 0, 1));
$word = mb_substr($word, 2);
// Lowercase apostophe letters
if (in_array($letter, array("D", "L")))
$letter = self::strtolower($letter);
else
$letter = self::strtoupper($letter);
return array("$letter'", $word);
}
// Internal function that does the work described in "formatName"
private static function formatNameRec($s, $allowNumbers = false)
{
// White-spaces cleaning
$s = self::rmMultipleSpaces($s);
// A name contains multiple words, separated by spaces
$words = explode(" ", $s);
foreach ($words as $i => $word)
{
// If there is a "dash", let's recurse on each sub-word
if (preg_match('/-/', $word))
{
$subWords = explode("-", $word);
foreach ($subWords as $j => $sub)
{
$subWords[$j] = self::formatNameRec($sub, $allowNumbers);
}
$word = join("-", $subWords);
}
else
{
list($apos, $word) = self::getApos($word);
// Special words that needs to be lower-case
if (in_array(self::strtolower($word), array("le", "la", "et", "de", "du", "sur", "les", "lès", "en")))
{
$word = self::strtolower($word);
}
// Generic case : first letter is upper, others are lower
else
{
$word = self::ucfirst(self::strtolower($word));
}
// Test letters
$allowed = join('', self::getLower())."\-\'";
if (!(
preg_match('/^['.$allowed.']+$/', self::strtolower($word)) ||
(preg_match('/^[0-9]+$/', self::strtolower($word)) && $allowNumbers)
) || $word == "'")
throw new Exception("Invalid letters in '$word'");
$word = $apos.$word;
}
$words[$i] = $word;
}
$s = join(" ", $words);
return $s;
}
// Utility function to post-correct schools names with common mistakes.
static function postFormatSchoolName($name)
{
// Already formatted, post-verification
// Check if "valid", for human use only (no automatic correction)
// Fix Typos
$typos = array(
"Ecole" => "École",
"Clg" => "Collège",
"College" => "Collège",
"Collége" => "Collège",
"Lycee" => "Lycée",
"Athenee" => "Athénée",
"Vzw" => "VZW",
"Asbl" => "ASBL"
);
$name = str_replace(array_keys($typos), array_values($typos), $name);
// $allowed = array("École", "Collège", "Lycée", "Institution", "Groupe Scolaire", "Cours", "Ensemble", "Cité Scolaire", "Internat");
// foreach ($allowed as $cat)
// if (preg_match("/^$cat /", $name))
return $name;
// throw new Exception("Invalid school category for '$name'");
}
////////// LowerCase / UpperCase functions //////////
// List of all LowerCase caracters in Western alphabet
static function getLower()
{
return array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
"ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "а", "б", "в", "г", "д", "е", "ё", "ж",
"з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы",
"ь", "э", "ю", "я", "œ"
);
}
// List of all UpperCase caracters in Western alphabet
static function getUpper()
{
return array(
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï",
"Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж",
"З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ъ",
"Ь", "Э", "Ю", "Я", "Œ"
);
}
// UTF-8 compliant implementation of "strtolower"
static function strtolower($string)
{
return str_replace(self::getUpper(), self::getLower(), $string);
}
// UTF-8 compliant implementation of "strtoupper"
static function strtoupper($string)
{
return str_replace(self::getLower(), self::getUpper(), $string);
}
// UTF-8 compliant implementation of "ucfirst"
static function ucfirst($string)
{
return self::strtoupper(mb_substr($string, 0, 1)).self::strtolower(mb_substr($string, 1));
}
// UTF-8 compliant implementation of "words"
static function ucwords($string)
{
$words = explode(" ", $string);
foreach ($words as $i => $word)
{
$words[$i] = self::ucfirst($word);
}
return join(" ", $words);
}
}
?>