-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
151 lines (142 loc) · 3.31 KB
/
functions.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* PorPOISe
* Copyright 2009 SURFnet BV
* Released under a permissive license (see LICENSE)
*/
/**
* Miscellaneous functions
*
* @package PorPOISe
*/
/**
* Sort an array of objects by field name. The sort algorithm is a
* stable merge sort.
*
* @param string field The field to sort on
* @param array $ar The array to be sorted
* @param bool descending Sort descending (default FALSE)
* @return array
*/
function objectSort($field, array $ar, $descending = FALSE) {
// check stop condition
if (count($ar) < 2) {
return $ar;
}
// split in equal halves and sort both
$split = floor(count($ar) / 2);
$ar1 = objectSort($field, array_slice($ar, 0, $split), $descending);
$ar2 = objectSort($field, array_slice($ar, $split), $descending);
// now merge
$result = array();
$i = $j = 0;
while ($i < count($ar1) && $j < count($ar2)) {
if ($descending) {
if ($ar2[$j]->$field >= $ar1[$i]->$field) {
$result[] = $ar2[$j];
$j++;
} else {
$result[] = $ar1[$i];
$i++;
}
} else {
if ($ar1[$i]->$field <= $ar2[$j]->$field) {
$result[] = $ar1[$i];
$i++;
} else {
$result[] = $ar2[$j];
$j++;
}
}
}
// add trailing elements
for (; $i < count($ar1); $i++) {
$result[] = $ar1[$i];
}
for (; $j < count($ar2); $j++) {
$result[] = $ar2[$j];
}
return $result;
}
/**
* Undo magic quotes if they have been enabled
*
* @return void
*/
function undo_magic_quotes_gpc() {
if (!function_exists("get_magic_quotes_gpc")) {
// magic_quotes_gpc is deprecated in PHP 5.3, this function may disappear in the future
// so check for its existence
return;
}
if (get_magic_quotes_gpc()) {
/*foreach ($_REQUEST as $key => $value) $_REQUEST[$key] = stripslashes($value);
foreach ($_GET as $key => $value) $_GET[$key] = stripslashes($value);
foreach ($_POST as $key => $value) $_POST[$key] = stripslashes($value);
foreach ($_COOKIE as $key => $value) $_COOKIE[$key] = stripslashes($value);*/
stripslashes_array($_REQUEST);
stripslashes_array($_GET);
stripslashes_array($_POST);
stripslashes_array($_COOKIE);
}
}
/**
* Strip slashes from elements in an array (recursively if necessary)
*
* Alters the argument
*
* @param &$ar
* @return void
*/
function stripslashes_array(&$ar) {
foreach ($ar as $key => $value) {
if (is_array($value)) {
stripslashes_array($ar[$key]);
} else {
$ar[$key] = stripslashes($value);
}
}
}
/**
* Recursively utf8_encodes all the strings in an array
*
* @param array $ar
*
* @return array
*/
function utf8_encode_recursive($ar) {
$result = array();
foreach ($ar as $key => $el) {
if (is_array($el)) {
$result[$key] = utf8_encode_recursive($el);
} else if (is_string($el)) {
$result[$key] = utf8_encode($el);
} else {
$result[$key] = $el;
}
}
return $result;
}
/**
* Cast a variable to a certain type, but with special rules
*
* Accepts the same type values as settype(). Special casting behavior is as follows:
* * NULL stays NULL
* * empty string to int or float becomes NULL
*
* @param mixed $v
* @param string $type
* @return mixed $v NULL if $v === NULL, the cast value of $v
*/
function special_cast($v, $type) {
if ($v === NULL) {
return NULL;
}
if (in_array($type, array("int", "integer", "float", "double"))) {
if ((string)$v === "") {
return NULL;
}
}
settype($v, $type);
return $v;
}