-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.php
58 lines (46 loc) · 1.33 KB
/
File.php
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
<?php
class ELSWAK_File
extends ELSWAK_Object {
protected $path;
protected $name;
protected $extension;
protected $type;
public function __construct($path) {
$this->setPath($path);
}
public function setPath($path) {
$this->path = $path;
$this->name = pathinfo($path, PATHINFO_BASENAME);
$this->extension = pathinfo($path, PATHINFO_EXTENSION);
$this->type = ELSWAK_File_Type_Detector::typeFromExtension($this->extension);
return $this;
}
protected function setName() {}
protected function setExtension() {}
protected function setType() {}
public static function sanitizeFileName($name, $pretty = true) {
$sanitized = '';
// filter the standard replacements
$replacements = array(
'/\s/' => '_',
'/\&/' => '_',
'/\+/' => '_',
'/__/' => '_',
'/\//' => '-',
);
// filter the "pretty" replacements if requested
if ($pretty) {
$replacements['/\&/'] = '_and_';
$replacements['/\+/'] = '_plus_';
}
$name = preg_replace(array_keys($replacements), $replacements, $name);
// filter out everything but alphanumeric, underscore, and dot characters
for ($i = 0; $i < strlen($name); ++$i) {
$character = substr($name, $i, 1);
if (ctype_alnum($character) == TRUE || $character == '_' || $character == '-' || $character == '.') {
$sanitized .= $character;
}
}
return $sanitized;
}
}