forked from getchabooks/getchabooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.php
277 lines (235 loc) · 6.39 KB
/
utility.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
class CurlError extends GetchabooksError {}
function curl_get($url, array $params=array(), array $options=array()) {
if ($params) {
$url .= '?' . http_build_query($params);
}
return curl_request($url, $options);
}
function curl_post($url, $params=null, array $options=array()) {
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $params;
return curl_request($url, $options);
}
function curl_request($url, $options) {
$defaults = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_VERBOSE => false,
CURLOPT_ENCODING => "gzip,deflate"
);
$ch = curl_init();
curl_setopt_array($ch, $options + $defaults);
$result = curl_exec($ch);
if (!$result || curl_error($ch)) {
throw new CurlError("Curl failed: " . curl_error($ch));
}
curl_close($ch);
return $result;
}
function decho($var) {
global $dechoBaseIndent, $indent;
$dechoBaseIndent = isset($dechoBaseIndent) ? $dechoBaseIndent : count(debug_backtrace());
if (php_sapi_name() == 'cli') {
if (isset($indent)) {
echo str_repeat(" ", $indent);
} else {
echo str_repeat(" ", count(debug_backtrace()) - $dechoBaseIndent);
}
echo $var;
}
}
/**
* @param $num a number
* @return string that number formatted as currency to two decimal places,
* without any currency sign
*/
function money($num) {
if (!is_numeric($num)){
return "";
}
return number_format($num, 2, '.', ',');
}
function get_user_agent() {
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$_SESSION['UserAgent'] = $_SERVER['HTTP_USER_AGENT'];
}
if (!isset($_SESSION['UserAgent'])) {
require_once BASE_DIR . '/vendor/random-uagent/uagent.php';
$_SESSION['UserAgent'] = random_uagent();
}
return $_SESSION['UserAgent'];
}
function titleCase($string) {
$titleCase = array("The", "A", "An", "And", "But", "Or", "Not", "As",
"At", "By", "For", "In", "From", "Of", "To", "With");
$name = trim(strtolower($string));
$answer = preg_replace_callback("/\s[ivx]{1,4}(\Z|\W)/", function ($match) {
return strtoupper($match[0]);
}, $name);
$answer = rtrim(ucwords($answer));
$answer = preg_replace("/W\//", "w/", $answer);
foreach ($titleCase as $word) {
$answer = preg_replace("/\s$word\s/", " ".strtolower($word)." ", $answer);
}
return ucfirst(htmlspecialchars($answer));
}
// from php doc pages
function ucname($string) {
$string = ucwords(strtolower($string));
foreach (array('-', '\'', '/') as $delimiter) {
if (strpos($string, $delimiter)!==false) {
$string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
}
}
return $string;
}
function cardinal($num) {
switch ($num) {
case 0: return 'zero';
case 1: return 'one';
case 2: return 'two';
case 3: return 'three';
case 4: return 'four';
case 5: return 'five';
case 6: return 'six';
case 7: return 'seven';
case 8: return 'eight';
case 9: return 'nine';
case 10: return 'ten';
default: return $num;
}
}
function ordinal($num) {
$order = Array("Zeroth", "First", "Second", "Third", "Fourth", "Fifth",
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth", "Eleventh",
"Twelfth", "Thirteenth", "Fourteenth", "Fifteenth", "Sixteenth");
if ($num < count($order)) {
return $order[$num];
}
switch ($num % 10) {
case 1:
return $num . 'st';
case 2:
return $num . 'nd';
case 3:
return $num . 'rd';
default:
return $num . 'th';
}
}
/**
* This is used by the below function but also at least one other thing.
*/
$taxRates = array(
'AL' => 8.2,
'AK' => 1.4,
'AZ' => 8.15,
'AR' => 8.2,
'CA' => 9.15,
'CO' => 6.4,
'CT' => 6,
'DE' => 0,
'DC' => 6,
'FL' => 6.7,
'GA' => 6.95,
'HI' => 4.4,
'ID' => 6.05,
'IL' => 8.2,
'IN' => 7,
'IA' => 6.85,
'KS' => 8.05,
'KY' => 6,
'LA' => 8.75,
'ME' => 5,
'MD' => 6,
'MA' => 6.25,
'MI' => 6,
'MN' => 6.2,
'MS' => 7,
'MO' => 7.25,
'MT' => 0,
'NE' => 6,
'NV' => 7.85,
'NH' => 0,
'NJ' => 6.95,
'NM' => 6.55,
'NY' => 8.45,
'NC' => 7.85,
'ND' => 5.8,
'OH' => 6.8,
'OK' => 8.15,
'OR' => 0,
'PA' => 6.4,
'RI' => 7,
'SC' => 7.05,
'SD' => 5.5,
'TN' => 9.4,
'TX' => 8.05,
'UT' => 6.7,
'VT' => 6.05,
'VA' => 5,
'WA' => 8.75,
'WV' => 6,
'WI' => 5.45,
'WY' => 5.25
);
function taxRate($state) {
if (isset($GLOBALS['taxRates'][$state])) {
return $GLOBALS['taxRates'][$state];
} else {
return 0;
}
}
/**
* Remove any non-ASCII characters and convert known non-ASCII characters
* to their ASCII equivalents, if possible.
*
* @param string $string
* @return string $string
* @author Jay Williams <myd3.com>
* @license MIT License
* @link http://gist.github.com/119517
*/
function convert_ascii($string) {
// Replace Single Curly Quotes
$search[] = chr(226).chr(128).chr(152);
$replace[] = "'";
$search[] = chr(226).chr(128).chr(153);
$replace[] = "'";
// Replace Smart Double Curly Quotes
$search[] = chr(226).chr(128).chr(156);
$replace[] = '"';
$search[] = chr(226).chr(128).chr(157);
$replace[] = '"';
// Replace En Dash
$search[] = chr(226).chr(128).chr(147);
$replace[] = '--';
// Replace Em Dash
$search[] = chr(226).chr(128).chr(148);
$replace[] = '---';
// Replace Bullet
$search[] = chr(226).chr(128).chr(162);
$replace[] = '*';
// Replace Middle Dot
$search[] = chr(194).chr(183);
$replace[] = '*';
// Replace Ellipsis with three consecutive dots
$search[] = chr(226).chr(128).chr(166);
$replace[] = '...';
// Apply Replacements
$string = str_replace($search, $replace, $string);
// Remove any non-ASCII Characters
$string = preg_replace("/[^\x01-\x7F]/","", $string);
return $string;
}
function innerHTML($node) {
$doc = new DOMDocument();
foreach ($node->childNodes as $child) {
$doc->appendChild($doc->importNode($child, true));
}
return $doc->saveHTML();
}