Passed
Pull Request — master (#340)
by
unknown
11:22 queued 02:20
created

util.inc.php ➔ explode_multi()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 23
rs 8.5906
1
<?php
2
/***************************************************************************
3
 *  For license information see doc/license.txt
4
 *
5
 *  Unicode Reminder メモ
6
 ***************************************************************************/
7
8
/*
9
 * RFC(2)822 Email Parser
10
 *
11
 * By Cal Henderson <[email protected]>
12
 * This code is licensed under a Creative Commons Attribution-ShareAlike 2.5 License
13
 * http://creativecommons.org/licenses/by-sa/2.5/
14
 *
15
 * Revision 4
16
 */
17
18
function is_valid_email_address($email)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
19
{
20
    /*
21
     * NO-WS-CTL       =       %d1-8 /         ; US-ASCII control characters
22
     *                         %d11 /          ;  that do not include the
23
     *                         %d12 /          ;  carriage return, line feed,
24
     *                         %d14-31 /       ;  and white space characters
25
     *                         %d127
26
     * ALPHA          =  %x41-5A / %x61-7A   ; A-Z / a-z
27
     * DIGIT          =  %x30-39
28
     */
29
30
    $no_ws_ctl = "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]";
31
    $alpha = "[\\x41-\\x5a\\x61-\\x7a]";
32
    $digit = "[\\x30-\\x39]";
33
    $cr = "\\x0d";
34
    $lf = "\\x0a";
35
    $crlf = "($cr$lf)";
36
37
38
    /*
39
     *
40
     *obs-char        =       %d0-9 / %d11 /          ; %d0-127 except CR and
41
     *                         %d12 / %d14-127         ;  LF
42
     * obs-text        =       *LF *CR *(obs-char *LF *CR)
43
     * text            =       %d1-9 /         ; Characters excluding CR and LF
44
     *                         %d11 /
45
     *                         %d12 /
46
     *                         %d14-127 /
47
     *                         obs-text
48
     * obs-qp          =       "\" (%d0-127)
49
     * quoted-pair     =       ("\" text) / obs-qp
50
     */
51
52
    $obs_char = "[\\x00-\\x09\\x0b\\x0c\\x0e-\\x7f]";
53
    $obs_text = "($lf*$cr*($obs_char$lf*$cr*)*)";
54
    $text = "([\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f]|$obs_text)";
55
    $obs_qp = "(\\x5c[\\x00-\\x7f])";
56
    $quoted_pair = "(\\x5c$text|$obs_qp)";
57
58
59
    /*
60
     *
61
     * obs-FWS         =       1*WSP *(CRLF 1*WSP)
62
     * FWS             =       ([*WSP CRLF] 1*WSP) /   ; Folding white space
63
     *                         obs-FWS
64
     * ctext           =       NO-WS-CTL /     ; Non white space controls
65
     *                         %d33-39 /       ; The rest of the US-ASCII
66
     *                         %d42-91 /       ;  characters not including "(",
67
     *                         %d93-126        ;  ")", or "\"
68
     * ccontent        =       ctext / quoted-pair / comment
69
     * comment         =       "(" *([FWS] ccontent) [FWS] ")"
70
     * CFWS            =       *([FWS] comment) (([FWS] comment) / FWS)
71
     */
72
73
    /*
74
     * note: we translate ccontent only partially to avoid an infinite loop
75
     * instead, we'll recursively strip comments before processing the input
76
     */
77
78
    $wsp = "[\\x20\\x09]";
79
    $obs_fws = "($wsp+($crlf$wsp+)*)";
80
    $fws = "((($wsp*$crlf)?$wsp+)|$obs_fws)";
81
    $ctext = "($no_ws_ctl|[\\x21-\\x27\\x2A-\\x5b\\x5d-\\x7e])";
82
    $ccontent = "($ctext|$quoted_pair)";
83
    $comment = "(\\x28($fws?$ccontent)*$fws?\\x29)";
84
    $cfws = "(($fws?$comment)*($fws?$comment|$fws))";
0 ignored issues
show
Unused Code introduced by
$cfws is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
    $cfws = "$fws*";
86
87
88
    /*
89
     *
90
     * atext           =       ALPHA / DIGIT / ; Any character except controls,
91
     *                         "!" / "#" /     ;  SP, and specials.
92
     *                         "$" / "%" /     ;  Used for atoms
93
     *                         "&" / "'" /
94
     *                         "*" / "+" /
95
     *                         "-" / "/" /
96
     *                         "=" / "?" /
97
     *                         "^" / "_" /
98
     *                         "" / "{" /
99
     *                         "|" / "}" /
100
     *                         "~"
101
     * atom            =       [CFWS] 1*atext [CFWS]
102
     */
103
104
    $atext = "($alpha|$digit|[\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2e\\x3d\\x3f\\x5e\\x5f\\x60\\x7b-\\x7e])";
105
    $atom = "($cfws?$atext+$cfws?)";
106
107
108
    /*
109
     *
110
     * qtext           =       NO-WS-CTL /     ; Non white space controls
111
     *                         %d33 /          ; The rest of the US-ASCII
112
     *                         %d35-91 /       ;  characters not including "\"
113
     *                         %d93-126        ;  or the quote character
114
     * qcontent        =       qtext / quoted-pair
115
     * quoted-string   =       [CFWS]
116
     *                         DQUOTE *([FWS] qcontent) [FWS] DQUOTE
117
     *                         [CFWS]
118
     * word            =       atom / quoted-string
119
     */
120
121
    $qtext = "($no_ws_ctl|[\\x21\\x23-\\x5b\\x5d-\\x7e])";
122
    $qcontent = "($qtext|$quoted_pair)";
123
    $quoted_string = "($cfws?\\x22($fws?$qcontent)*$fws?\\x22$cfws?)";
124
    $word = "($atom|$quoted_string)";
125
126
127
    /*
128
     *
129
     * obs-local-part  =       word *("." word)
130
     * obs-domain      =       atom *("." atom)
131
     */
132
133
    $obs_local_part = "($word(\\x2e$word)*)";
134
    $obs_domain = "($atom(\\x2e$atom)*)";
135
136
137
    /*
138
     *
139
     * dot-atom-text   =       1*atext *("." 1*atext)
140
     * dot-atom        =       [CFWS] dot-atom-text [CFWS]
141
     */
142
143
    $dot_atom_text = "($atext+(\\x2e$atext+)*)";
144
    $dot_atom = "($cfws?$dot_atom_text$cfws?)";
145
146
147
    /*
148
     *
149
     * domain-literal  =       [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS]
150
     * dcontent        =       dtext / quoted-pair
151
     * dtext           =       NO-WS-CTL /     ; Non white space controls
152
     *
153
     *                         %d33-90 /       ; The rest of the US-ASCII
154
     *                         %d94-126        ;  characters not including "[",
155
     *                                         ;  "]", or "\"
156
     */
157
158
    $dtext = "($no_ws_ctl|[\\x21-\\x5a\\x5e-\\x7e])";
159
    $dcontent = "($dtext|$quoted_pair)";
160
    $domain_literal = "($cfws?\\x5b($fws?$dcontent)*$fws?\\x5d$cfws?)";
161
162
163
    /*
164
     *
165
     * local-part      =       dot-atom / quoted-string / obs-local-part
166
     * domain          =       dot-atom / domain-literal / obs-domain
167
     * addr-spec       =       local-part "@" domain
168
     */
169
170
    $local_part = "($dot_atom|$quoted_string|$obs_local_part)";
171
    $domain = "($dot_atom|$domain_literal|$obs_domain)";
172
    $addr_spec = "($local_part\\x40$domain)";
173
174
175
    /*
176
     * we need to strip comments first (repeat until we can't find any more)
177
     */
178
179
    $done = 0;
180
181
    while (!$done) {
182
        $new = preg_replace("!$comment!", '', $email);
183
        if (strlen($new) == strlen($email)) {
184
            $done = 1;
185
        }
186
        $email = $new;
187
    }
188
189
190
    /*
191
     * now match what's left
192
     */
193
194
    return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
195
}
196
197 View Code Duplication
function mb_trim($str)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
Best Practice introduced by
The function mb_trim() has been defined more than once; this definition is ignored, only the first definition in htdocs/lib/clicompatbase.inc.php (L508-533) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
198
{
199
    $bLoop = true;
200
    while ($bLoop == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
201
        $sPos = mb_substr($str, 0, 1);
202
203
        if ($sPos == ' ' || $sPos == "\r" || $sPos == "\n" || $sPos == "\t" || $sPos == "\x0B" || $sPos == "\0") {
204
            $str = mb_substr($str, 1, mb_strlen($str) - 1);
205
        } else {
206
            $bLoop = false;
207
        }
208
    }
209
210
    $bLoop = true;
211
    while ($bLoop == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
212
        $sPos = mb_substr($str, - 1, 1);
0 ignored issues
show
introduced by
A unary operator statement must not be followed by a space
Loading history...
213
214
        if ($sPos == ' ' || $sPos == "\r" || $sPos == "\n" || $sPos == "\t" || $sPos == "\x0B" || $sPos == "\0") {
215
            $str = mb_substr($str, 0, mb_strlen($str) - 1);
216
        } else {
217
            $bLoop = false;
218
        }
219
    }
220
221
    return $str;
222
}
223
224
// explode with more than one separator
225
function explode_multi($str, $sep)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
226
{
227
    $ret = array();
228
    $nCurPos = 0;
229
230
    while ($nCurPos < mb_strlen($str)) {
231
        $nNextSep = mb_strlen($str);
232
        for ($nSepPos = 0; $nSepPos < mb_strlen($sep); $nSepPos ++) {
0 ignored issues
show
introduced by
No whitespace should be between variable and incrementor.
Loading history...
233
            $nThisPos = mb_strpos($str, mb_substr($sep, $nSepPos, 1), $nCurPos);
234
            if ($nThisPos !== false) {
235
                if ($nNextSep > $nThisPos) {
236
                    $nNextSep = $nThisPos;
237
                }
238
            }
239
        }
240
241
        $ret[] = mb_substr($str, $nCurPos, $nNextSep - $nCurPos);
242
243
        $nCurPos = $nNextSep + 1;
244
    }
245
246
    return $ret;
247
}
248
249
function getSysConfig($name, $default)
0 ignored issues
show
Best Practice introduced by
The function getSysConfig() has been defined more than once; this definition is ignored, only the first definition in htdocs/lib/clicompatbase.inc.php (L135-138) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
250
{
251
    return sql_value("SELECT `value` FROM `sysconfig` WHERE `name`='&1'", $default, $name);
252
}
253
254
function setSysConfig($name, $value)
0 ignored issues
show
Best Practice introduced by
The function setSysConfig() has been defined more than once; this definition is ignored, only the first definition in htdocs/lib/clicompatbase.inc.php (L140-155) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
255
{
256
    sql(
257
        "INSERT INTO `sysconfig` (`name`, `value`) VALUES ('&1', '&2') ON DUPLICATE KEY UPDATE `value`='&2'",
258
        $name,
259
        $value
260
    );
261
}
262
263
function read_file($filename, $maxlength)
0 ignored issues
show
Unused Code introduced by
The parameter $maxlength is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
The function read_file() has been defined more than once; this definition is ignored, only the first definition in htdocs/lib/clicompatbase.inc.php (L84-94) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
264
{
265
    $content = '';
266
267
    $f = fopen($filename, 'r');
268
    if ($f === false) {
269
        return false;
270
    }
271
272
    while ($line = fread($f, 4096)) {
273
        $content .= $line;
274
    }
275
    fclose($f);
276
277
    return $content;
278
}
279
280
// encodes &<>"'
281
function xmlentities($str)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
282
{
283
    return htmlspecialchars(xmlfilterevilchars($str), ENT_QUOTES, 'UTF-8');
284
}
285
286
// encodes &<>
287
// This is ok for XML content text between tags, but not for XML attribute contents.
288
function text_xmlentities($str)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
289
{
290
    return htmlspecialchars(xmlfilterevilchars($str), ENT_NOQUOTES, 'UTF-8');
291
}
292
293
function xmlfilterevilchars($str)
294
{
295
    // the same for for ISO-8859-1 and UTF-8
296
    // following 2016-3-1: allowed Tabs (\x{09})
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
297
    return mb_ereg_replace('[\x{00}-\x{08}\x{0B}\x{0C}\x{0E}-\x{1F}]*', '', $str);
298
}
299
300
301
// decimal longitude to string E/W hhh°mm.mmm
302 View Code Duplication
function help_lonToDegreeStr($lon)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
303
{
304
    if ($lon < 0) {
305
        $retval = 'W ';
306
        $lon = - $lon;
0 ignored issues
show
introduced by
A unary operator statement must not be followed by a space
Loading history...
307
    } else {
308
        $retval = 'E ';
309
    }
310
311
    $retval = $retval . sprintf("%03d", floor($lon)) . '° ';
312
    $lon = $lon - floor($lon);
313
    $retval = $retval . sprintf("%06.3f", round($lon * 60, 3)) . '\'';
314
315
    return $retval;
316
}
317
318
// decimal latitude to string N/S hh°mm.mmm
319 View Code Duplication
function help_latToDegreeStr($lat)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
320
{
321
    if ($lat < 0) {
322
        $retval = 'S ';
323
        $lat = - $lat;
0 ignored issues
show
introduced by
A unary operator statement must not be followed by a space
Loading history...
324
    } else {
325
        $retval = 'N ';
326
    }
327
328
    $retval = $retval . sprintf("%02d", floor($lat)) . '° ';
329
    $lat = $lat - floor($lat);
330
    $retval = $retval . sprintf("%06.3f", round($lat * 60, 3)) . '\'';
331
332
    return $retval;
333
}
334
335
336
function escape_javascript($text)
0 ignored issues
show
Best Practice introduced by
The function escape_javascript() has been defined more than once; this definition is ignored, only the first definition in htdocs/lib/clicompatbase.inc.php (L96-99) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
337
{
338
    return str_replace('\'', '\\\'', str_replace('"', '&quot;', $text));
339
}
340
341
342
// perform str_rot13 without renaming parts in []
343
function str_rot13_gc($str)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
344
{
345
    $delimiter[0][0] = '[';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$delimiter was never initialized. Although not strictly required by PHP, it is generally a good practice to add $delimiter = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
346
    $delimiter[0][1] = ']';
347
348
    $retval = '';
349
350 View Code Duplication
    while (mb_strlen($retval) < mb_strlen($str)) {
351
        $nNextStart = false;
352
        $sNextEndChar = '';
353
        foreach ($delimiter as $del) {
354
            $nThisStart = mb_strpos($str, $del[0], mb_strlen($retval));
355
356
            if ($nThisStart !== false) {
357
                if (($nNextStart > $nThisStart) || ($nNextStart === false)) {
358
                    $nNextStart = $nThisStart;
359
                    $sNextEndChar = $del[1];
360
                }
361
            }
362
        }
363
364
        if ($nNextStart === false) {
365
            $retval .= str_rot13(mb_substr($str, mb_strlen($retval), mb_strlen($str) - mb_strlen($retval)));
366
        } else {
367
            // crypted part
368
            $retval .= str_rot13(mb_substr($str, mb_strlen($retval), $nNextStart - mb_strlen($retval)));
369
370
            // uncrypted part
371
            $nNextEnd = mb_strpos($str, $sNextEndChar, $nNextStart);
372
373
            if ($nNextEnd === false) {
374
                $retval .= mb_substr($str, $nNextStart, mb_strlen($str) - mb_strlen($retval));
375
            } else {
376
                $retval .= mb_substr($str, $nNextStart, $nNextEnd - $nNextStart + 1);
377
            }
378
        }
379
    }
380
381
    return $retval;
382
}
383
384
385
// format number with 1000s dots
386
function number1000($n)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
387
{
388
    global $opt;
389
390
    if (isset($opt['locale'][$opt['template']['locale']]['format']['dot1000']) &&
391
        $opt['locale'][$opt['template']['locale']]['format']['dot1000'] == ','
392
    ) {
393
        return number_format($n);
394
    } else {
0 ignored issues
show
introduced by
Unneeded T_ELSE detected.
Loading history...
395
        return str_replace(',', '.', number_format($n));
396
    }
397
}
398