-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctionspro.php
More file actions
391 lines (352 loc) · 10.4 KB
/
Copy pathfunctionspro.php
File metadata and controls
391 lines (352 loc) · 10.4 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/* =========================================================
* FunctionsPro.php 1.0
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/FunctionsPro.php
* ========================================================== */
/*
* Print array surrounded by PRE-tag
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function pre($array){
$output = '<pre style="background:yellow;">';
$output .= print_r($array, true);
$output .= '</pre>';
return $output;
}
/*
* Print email with javascript protection against spam robots
* Usage: printEmail(your@email.com, '<optional: user agent string>', 'Email click here', 'Test email', 'This is the body text');
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function printEmail($email, $agent='', $text='', $subject='', $body=''){
// Email supplied?
if(!isset($email) || empty($email)) return '(email is missing)';
// Email seems valid?
if(filter_var($email, FILTER_VALIDATE_EMAIL) == FALSE) return '(email is not valid)';
// Email parts
$email_parts = explode('@', $email);
// Body
$body = addslashes($body);
if(!empty($agent)){
$body .= '%0A%0A--%0A';
$body .= 'My browser is: '.addslashes($agent);
}
// Output
$output = '<script language="javascript">'."\n";
$output .= '<!--'."\n";
$output .= 'var part1 = "'.$email_parts[0].'";'."\n"; // user
$output .= 'var part2 = "y='.addslashes($body).'";'."\n"; // body
$output .= 'var part3 = "ject='.addslashes($subject).'";'."\n"; // subject
$output .= 'var part4 = "'.$email_parts[1].'";'."\n"; // domain
$output .= 'var part5 = '.(!empty($text)?'"'.addslashes($text).'"':'part1+\'@\'+part4').';'."\n"; // text
$output .= 'document.write(\'<a href="mai\'+\'lto:\'+part1+\'@\'+part4+\'?sub\'+part3+\'&bod\'+part2+\'" target="_blank">\');'."\n";
$output .= 'document.write(part5+\'</a>\');'."\n";
$output .= '// -->'."\n";
$output .= '</script>';
// Return
return $output;
}
/*
* Add HTTP to URL if missing
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function addhttp($url) {
if(!isset($url) || empty($url)) return false;
if(substr($url, 0, 1) == '/') return $url;
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
/*
* Remove empty HTML tags from string
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function removeEmptyHtmlTags($string){
$cleaned = preg_replace("/<p[^>]*>[\s| ]*<\/p>/", '', $string);
return $cleaned;
}
/*
* Strip HTML from string
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function stripHtml($input){
return preg_replace('/\s{2,}/', ' ', strip_tags($input));
}
/*
* Generate random string
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function generateRandStr($length=10){
$randstr = "";
for($i=0; strlen($randstr)<$length; $i++){
$randnum = mt_rand(0,61);
if($randnum>=36){
$randstr .= chr($randnum+61);
}
}
return $randstr;
}
/*
* Embed video (Youtube or Vimeo - others can easily be added)
* Usage: embedVideo('http://www.youtube.com/watch?v=_7Mi77iqMjA');
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function embedVideo($url="", $width=500, $autoplay=FALSE, $loop=FALSE){
if(empty($url)) return "Video url is missing";
// Size
$ratio = 0.5625;
$height = $width * $ratio;
// Random ID
$iframe_id = 'video'.generateRandStr();
if(strpos($url, 'youtube') !== FALSE){
// YOUTUBE
$parsed_url = parse_url($url, PHP_URL_QUERY);
parse_str($parsed_url, $parsed_string);
$video_id = $parsed_string['v'];
$embed_html = '<iframe id="'.$iframe_id.'" width="'.$width.'" height="'.$height.'" src="http://www.youtube.com/embed/*|VIDEOID|*?rel=0" frameborder="0" allowfullscreen></iframe>';
return str_replace('*|VIDEOID|*', $video_id, $embed_html);
} else if(strpos($url, 'vimeo') !== FALSE){
// VIMEO
$parsed_url = parse_url($url);
$video_id = str_replace('/', '', $parsed_url['path']);
$embed_html = '<iframe id="'.$iframe_id.'" src="http://player.vimeo.com/video/*|VIDEOID|*?title=0&byline=0&portrait=0&color=ffffff'.($autoplay?'&autoplay=1':'').($loop?'&loop=1':'').'&api=1&player_id='.$iframe_id.'" width="'.$width.'" height="'.$height.'" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
return str_replace('*|VIDEOID|*', $video_id, $embed_html);
}
// Not supported
return 'Video url not supported.';
}
/*
* Get title from URL
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function getTitle($Url){
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $Url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$str = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
//$str = file_get_contents($Url);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
} else {
return 'Unable to get title from link';
}
}
/*
* Get city from IP
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function getLocation($ip){
// url
$url = 'http://api.hostip.info/get_json.php?ip='.$ip.'&position=false';
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$str = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$location = json_decode($str, true);
if(!empty($location) && is_array($location)){
// Check city
if(!isset($location['city']) || empty($location['city']) || stristr($location['city'], 'unknown')){
unset($location['city']);
}
// Check lat/lng
if(!is_numeric($location['lat']) || !is_numeric($location['lng'])){
unset($location['lat']);
unset($location['lng']);
}
return $location;
}
return false;
}
/*
* Get URL friendly string (machine name)
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function slugify($text){
// Replace Æ
$text = preg_replace('/æ/', 'ae', $text);
$text = preg_replace('/Æ/', 'ae', $text);
// Replace Ø
$text = preg_replace('/ø/', 'oe', $text);
$text = preg_replace('/Ø/', 'oe', $text);
// Replace Å
$text = preg_replace('/å/', 'aa', $text);
$text = preg_replace('/Å/', 'aa', $text);
// Replace É
$text = preg_replace('/é/', 'e', $text);
$text = preg_replace('/É/', 'e', $text);
// replace all non letters or digits with -
$text = preg_replace('/[^&\w]+/', '-', $text);
// trim and lowercase
$text = strtolower(trim(htmlspecialchars($text), '-'));
return $text;
}
/*
* Get a files change date and return as no cache
* Usage: version('js/scripts.js');
* Output: 'js/scripts.js?1107722733'
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function version($file){
return $file.'?'.filemtime($_SERVER['DOCUMENT_ROOT'].'/'.$file);
}
/*
* Get a users age
* Usage: getAge(<birthday timestamp>, <current time timestamp>);
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function getAge($b, $t){
$age = ($b < 0) ? ( $t + ($b * -1) ) : $t - $b;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
return floor($ageYears);
}
/*
* Get the size of a file
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function getFileSize($path){
$size = filesize($path);
$units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$power = $size > 0 ? floor(log($size, 1024)) : 0;
return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
}
/*
* Get time ago or until, a specific time
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function time_since($current_time=NULL, $other_time=NULL){
if(empty($current_time) || empty($other_time)) return false;
$time_difference = $current_time - $other_time;
// If time is in the future
$ago = " ago";
$now = "Just now";
if($time_difference < 0){
$time_difference = abs($time_difference);
$ago = "";
$now = "in a few seconds";
}
$seconds = $time_difference ;
$minutes = round($time_difference / 60 );
$hours = round($time_difference / 3600 );
$days = round($time_difference / 86400 );
$weeks = round($time_difference / 604800 );
$months = round($time_difference / 2419200 );
$years = round($time_difference / 29030400 );
// Seconds
if($seconds <= 60){
return $now;
/*return "$seconds seconds ago";*/
} else if($minutes <=60){
//Minutes
if($minutes <= 5){
/*if($minutes==1){*/
return $now;
/*return "1 minute ago";*/
} else {
return "$minutes minutes".$ago;
}
} else if($hours <=24){
//Hours
if($hours==1){
return "1 hour".$ago;
} else {
return "$hours hours".$ago;
}
} else if($days <= 7){
//Days
if($days==1){
return "1 day".$ago;
} else {
return "$days days".$ago;
}
} else if($weeks < 4){
//Weeks
if($weeks==1){
return "1 week".$ago;
} else {
return "$weeks weeks".$ago;
}
} else if($months < 12){
//Months
if($months==1){
return "1 month".$ago;
} else {
return "$months months".$ago;
}
} else {
//Years
if($years==1){
return "1 year".$ago;
} else {
return "$years years".$ago;
}
}
}
/*
* Binary Search
*
* Author: Alexander Bech / www.alexanderbech.com
* http://github.com/AlexanderBech/
*/
function binarySearch($key, $array){
$low = 0;
$high = count($array) - 1;
while($low <= $high){
$mid = floor(($low + $high)/2);
$value = $array[$mid];
if($value < $key){
$low = $mid + 1;
} else if($value > $key){
$high = $mid - 1;
} else {
return $mid;
}
}
return null;
}
?>