-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffunctions.inc
More file actions
201 lines (188 loc) · 5.74 KB
/
ffunctions.inc
File metadata and controls
201 lines (188 loc) · 5.74 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* @file
* Handy non-Drupal utility functions.
*/
/**
* Log a message to the given file.
*
* This is handy for debugging.
*
* @param string $log
* Path to the log file on disk.
* @param string $message
* The message to log. If this is an array or obejct, then send print_r($variable, TRUE).
*/
function ffunctions_log_message($log, $message) {
$message = (is_object($message) || is_array($message)) ? print_r($message, TRUE) : $message;
file_put_contents($log, date('c') . ' - ' . $message, FILE_APPEND);
}
/**
* Convert a date string or timestamp to any given format.
*
* @param string $date_string
* A date string that is readable by php or a UNIX timestamp.
* @param string $format
* A valid date format as defined in php's date function.
* @param array $options
* (optional) timezone_to - The timezone for the returned date value.
* (optional) timezone_from - The timezone that the date_string is currently in.
* (optional) timestamp - Whether or not this is a UNIX timestamp.
*
* @return string
* The date in the format provided in the the given timezone(timezone_to).
*/
function ffunctions_get_formatted_date($date_string, $format = 'c', $options = array()) {
// Combine defaults with given options.
$options += array(
'timezone_to' => 'UTC',
'timezone_from' => 'UTC',
'timestamp' => FALSE,
);
// Special handlng of timestamps
if (!empty($options['timestamp'])) {
$date_string = "@$date_string";
$options['timezone_from'] = date_default_timezone_get();
}
$date = new DateTime($date_string, new DateTimeZone($options['timezone_from']));
$date->setTimeZone(new DateTimeZone($options['timezone_to']));
return $date->format($format);
}
/**
* Compute the number of business days between (and including) two dates.
*
* Note that this does not take holidays into account.
*
* @param int|string $start
* Preferably a data string compatible with strtotime,
* but can also be timestamp.
* @param int|string $end
* Preferably a data string compatible with strtotime,
* but can also be timestamp.
*/
function ffunctions_get_business_day_count($start, $end) {
$start = !is_int($start) ? strtotime($start) : $start;
$end = !is_int($end) ? strtotime($end) : $end;
$total_days = ($end - $start) / 86400 + 1;
$first_day = date('N', $start);
$last_day = date('N', $end);
$full_weeks = floor($total_days / 7);
$extra_days = fmod($total_days, 7);
// Check if the first and last day are within the same week.
if ($first_day <= $last_day) {
// Subtract a day if last day is on Saturday or later.
if ($first_day <= 6 && 6 <= $last_day) {
$extra_days--;
}
// Subtract a day if last day is on Sunday.
if ($first_day <= 7 && 7 <= $last_day) {
$extra_days--;
}
}
else {
// We're not in the same week, so the count will
// be different depending on the first day.
if ($first_day == 7) {
$extra_days--;
if ($last_day == 6) {
$extra_days--;
}
}
else {
// If the first day is not Sunday, then we subtract both weekend days.
$extra_days -= 2;
}
}
// Add up the the days in the full weeks and the extra days.
$day_count = 5*$full_weeks + ($extra_days > 0 ? $extra_days : 0);
return $day_count;
}
/**
* Find a single url in an html string
*/
function ffunctions_find_url($string, $attribute = 'src', $modifiers = '') {
$matches = array();
$pattern = sprintf('/%s=["|\']([^"|\']+)/%s', $attribute, $modifiers);
preg_match($pattern, $string, $matches);
return !(empty($matches[1])) ? $matches[1] : NULL;
}
/**
* Access callback for determining if given ip matches the allowed ips.
*
* @param array
* A list of ips in a list or cidr notation (e.g. array('45.45.45.45/26'))
* @return boolean
* Whether or not the ip is allowed.
*/
function ffunctions_ip_is_allowed($allowed_ips) {
$ip = !empty($_SERVER['HTTP_X_FORWARD_FOR']) ? $_SERVER['HTTP_X_FORWARD_FOR'] : $_SERVER['REMOTE_ADDR'];
$access = FALSE;
if (in_array($ip, $allowed_ips, TRUE)) {
$access = TRUE;
}
else {
foreach ($allowed_ips as $range) {
if (ffunctions_cidr_match($ip, $range)) {
$access = TRUE;
}
}
}
return $access;
}
/**
* Given a CIDR mask and an IP address, return TRUE or FALSE if the IP address
* matches or doesn't match the CIDR mask.
* Adapted from http://stackoverflow.com/questions/594112
*/
function ffunctions_cidr_match($ip, $range) {
list ($subnet, $bits) = explode('/', $range);
$ip = ip2long($ip);
$subnet = ip2long($subnet);
// Sanity check: ip2long() returns FALSE for an invalid IP address.
if (empty($subnet) || empty($bits) || empty($ip)) {
return FALSE;
}
$mask = -1 << (32 - $bits);
$subnet &= $mask;
return ($ip & $mask) == $subnet;
}
/**
* Clone an object that contains nested objects (recursive in a sense).
*
* @param object $object
* The object to clone.
*
* @return object
* A copy of the object.
*/
function ffunctions_deep_clone($object) {
return unserialize(serialize($object));
}
/**
* Replaces all words in a string with the provided word.
*
* Note that anything within an html tag is not touched.
*
* @param string $string
* The string to modify.
* @param string $hodor
* The word to replace all words.
*
* @return string
* The modified string.
*/
function ffunctions_hodor_filter_process($string, $hodor) {
$pattern = '/[^<>\s]+(?!([^<]+)?>)/m';
$pattern = '/[^<>(\s|\.|\!|\?|\,)]+(?!([^<]+)?>)/m';
$filtered_string = preg_replace_callback($pattern, function($matches) use ($hodor) {
if (ctype_upper($matches[0][0])) {
if (ctype_upper($matches[0][strlen($matches[0] - 1)])) {
return strtoupper($hodor);
}
return ucfirst($hodor);
}
return $hodor;
},
$string);
return $filtered_string;
}