Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 261... Línea 261...
261
            }
261
            }
262
        }
262
        }
Línea 263... Línea 263...
263
 
263
 
264
        return null;
264
        return null;
-
 
265
    }
-
 
266
 
-
 
267
    /**
-
 
268
     * Normalize internet address.
-
 
269
     *
-
 
270
     * Accepted input formats are :
-
 
271
     * - a valid range or full ip address (e.g.: 192.168.0.0/16, fe80::ffff, 127.0.0.1 or fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80)
-
 
272
     * - a valid domain name or pattern (e.g.: www.moodle.com or *.moodle.org)
-
 
273
     *
-
 
274
     * Convert forbidden syntaxes since MDL-74289 to allowed values. For examples:
-
 
275
     * - 192.168. => 192.168.0.0/16
-
 
276
     * - .domain.tld => *.domain.tld
-
 
277
     *
-
 
278
     * @param string $address The input string to normalize.
-
 
279
     *
-
 
280
     * @return string If $address is not normalizable, an empty string is returned.
-
 
281
     */
-
 
282
    public static function normalize_internet_address(string $address): string {
-
 
283
        $address = str_replace([" ", "\n", "\r", "\t", "\v", "\x00"], '', strtolower($address));
-
 
284
 
-
 
285
        // Replace previous allowed "192.168." format to CIDR format (192.168.0.0/16).
-
 
286
        if (str_ends_with($address, '.') && preg_match('/^[0-9\.]+$/', $address) === 1) {
-
 
287
            $count = substr_count($address, '.');
-
 
288
 
-
 
289
            // Remove final dot.
-
 
290
            $address = substr($address, 0, -1);
-
 
291
 
-
 
292
            // Fill address with missing ".0".
-
 
293
            $address .= str_repeat('.0', 4 - $count);
-
 
294
 
-
 
295
            // Add subnet mask.
-
 
296
            $address .= '/' . ($count * 8);
-
 
297
        }
-
 
298
 
-
 
299
        if (self::is_ip_address($address) ||
-
 
300
            self::is_ipv4_range($address) || self::is_ipv6_range($address)) {
-
 
301
 
-
 
302
            // Keep full or range ip addresses.
-
 
303
            return $address;
-
 
304
        }
-
 
305
 
-
 
306
        // Replace previous allowed ".domain.tld" format to "*.domain.tld" format.
-
 
307
        if (str_starts_with($address, '.')) {
-
 
308
            $address = '*'.$address;
-
 
309
        }
-
 
310
 
-
 
311
        // Usually the trailing dot (null label) is omitted, but is valid if supplied. We'll just remove it and validate as normal.
-
 
312
        $address = rtrim($address, '.');
-
 
313
 
-
 
314
        if (self::is_domain_name($address) || self::is_domain_matching_pattern($address)) {
-
 
315
            // Keep valid or pattern domain name.
-
 
316
            return $address;
-
 
317
        }
-
 
318
 
-
 
319
        // Return empty string for invalid values.
-
 
320
        return '';
-
 
321
    }
-
 
322
 
-
 
323
    /**
-
 
324
     * Normalize a list of internet addresses.
-
 
325
     *
-
 
326
     * This function will:
-
 
327
     * - normalize internet addresses {@see normalize_internet_address()}
-
 
328
     * - remove invalid values
-
 
329
     * - remove duplicate values
-
 
330
     *
-
 
331
     * @param string $addresslist A string representing a list of internet addresses separated by a common value.
-
 
332
     * @param string $separator A separator character used within the list string.
-
 
333
     *
-
 
334
     * @return string
-
 
335
     */
-
 
336
    public static function normalize_internet_address_list(string $addresslist, string $separator = ','): string {
-
 
337
        $addresses = [];
-
 
338
        foreach (explode($separator, $addresslist) as $value) {
-
 
339
            $address = self::normalize_internet_address($value);
-
 
340
 
-
 
341
            if (empty($address)) {
-
 
342
                // Ignore invalid input.
-
 
343
                continue;
-
 
344
            }
-
 
345
 
-
 
346
            if (in_array($address, $addresses, true)) {
-
 
347
                // Ignore duplicate value.
-
 
348
                continue;
-
 
349
            }
-
 
350
 
-
 
351
            $addresses[] = $address;
-
 
352
        }
-
 
353
 
-
 
354
        return implode($separator, $addresses);
265
    }
355
    }