/
Utils.php
132 lines (123 loc) · 3.63 KB
/
Utils.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
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
<?php
namespace Munee;
class Utils
{
/**
* Creates directories
*
* @param $dir
*
* @return bool
* @throws ErrorException
*/
public static function createDir($dir)
{
if (! is_dir($dir) && ! mkdir($dir, 0777, true)) {
throw new ErrorException("The follow directory could not be made, please create it: {$dir}");
}
return true;
}
/**
* Recursively remove a directory and all of it's contents
*
* @param $dir
*
* @throws ErrorException
*/
public static function removeDir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ('.' != $object && '..' != $object) {
if ('dir' == filetype($dir . DS . $object)) {
static::removeDir($dir . DS . $object);
} else {
if (! unlink($dir . DS . $object)) {
throw new ErrorException(
'The following file could not be deleted: ' . $dir . DS . $object
);
}
}
}
}
if (! rmdir($dir)) {
throw new ErrorException("The following directory could not be deleted: {$dir}");
}
}
}
/**
* Check to see if a string is unserializable
*
* @param $value
* @param null $result
*
* @return boolean
*
* @see https://gist.github.com/1415653
*/
public static function isSerialized($value, &$result = null)
{
// Bit of a give away this one
if (! is_string($value)) {
return false;
}
// Serialized FALSE, return TRUE. unserialize() returns FALSE on an
// invalid string or it could return FALSE if the string is serialized
// FALSE, eliminate that possibility.
if ('b:0;' === $value) {
$result = false;
return true;
}
$length = strlen($value);
$end = '';
if (isset($value[0])) {
switch ($value[0]) {
case 's':
if ('"' !== $value[$length - 2]) {
return false;
}
break;
case 'b':
case 'i':
case 'd':
// This looks odd but it is quicker than isset()ing
$end .= ';';
case 'a':
case 'O':
$end .= '}';
if (':' !== $value[1]) {
return false;
}
switch ($value[2]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
default:
return false;
}
case 'N':
$end .= ';';
if ($value[$length - 1] !== $end[0]) {
return false;
}
break;
default:
return false;
}
}
if (($result = @unserialize($value)) === false) {
$result = null;
return false;
}
return true;
}
}