Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1979 | Rev 4113 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Library;
6
 
7
abstract class Functions
8
{
9
    public static function getUserIP()
10
    {
11
        $client  = isset($_SERVER['HTTP_CLIENT_IP'])  ? $_SERVER['HTTP_CLIENT_IP'] : '';
12
        $forward = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
13
        $remote  = $_SERVER['REMOTE_ADDR'];
14
 
15
        if(filter_var($client, FILTER_VALIDATE_IP)) {
16
            $ip = $client;
17
        } elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
18
            $ip = $forward;
19
        } else {
20
            $ip = $remote;
21
        }
22
 
23
        return $ip;
24
    }
25
 
26
 
27
    /**
28
     *
29
     * @param string $what
30
     * @param string $date
31
     * @return string
32
     */
33
    public static function convertDate($what, $date) {
34
        if ($what == 'wherecond') {
35
            return date('Y-m-d', strtotime($date));
36
        }
37
        else if ($what == 'display') {
38
            return date('d M, Y h:i A', strtotime($date));
39
        }
40
        else if ($what == 'displayWeb') {
41
            return date('d M Y', strtotime($date));
42
        }
43
        else if ($what == 'onlyDate') {
44
            return date(PHP_DATE_FORMAT, strtotime($date));
45
        }
46
        else if ($what == 'monthYear') {
47
            return date(PHP_DATE_FORMAT_MONTH_YEAR, strtotime($date));
48
        }
49
        else if ($what == 'onlyMonth') {
50
            return date(PHP_DATE_FORMAT_MONTH, strtotime($date));
51
        }
52
        else if ($what == 'gmail') {
53
            return date('D, M d, Y - h:i A', strtotime($date));
54
        }
55
        else if ($what == 'onlyDateForCSV') {
56
            return date('M d,Y', strtotime($date));
57
        }
58
        else {
59
            return date('Y-m-d', strtotime($date));
60
        }
61
    }
62
 
63
    /**
3138 efrain 64
     *
65
     * @param string $timestamp
66
     * @param string $now
1 www 67
     * @return string
68
     */
3138 efrain 69
    public static function timeAgo($timestamp, $now = '')
70
    {
71
 
72
        if($now) {
73
            $datetime1 = \DateTime::createFromFormat('Y-m-d H:i:s', $now);
74
        } else {
75
            $now = date('Y-m-d H:i:s');
76
            $datetime1 = date_create($now);
77
        }
78
        $datetime2 = date_create($timestamp);
79
 
80
        $diff = date_diff($datetime1, $datetime2);
81
        $timemsg = '';
82
        if ($diff->y > 0) {
83
            $timemsg = $diff->y . ' año' . ($diff->y > 1 ? "s" : '');
84
        } else if ($diff->m > 0) {
85
            $timemsg = $diff->m . ' mes' . ($diff->m > 1 ? "es" : '');
86
        } else if ($diff->d > 0) {
87
            $timemsg = $diff->d . ' dia' . ($diff->d > 1 ? "s" : '');
88
        } else if ($diff->h > 0) {
89
            $timemsg = $diff->h . ' hora' . ($diff->h > 1 ? "s" : '');
90
        } else if ($diff->i > 0) {
91
            $timemsg = $diff->i . ' minuto' . ($diff->i > 1 ? "s" : '');
92
        } else if ($diff->s > 0) {
93
            $timemsg = $diff->s . ' segundo' . ($diff->s > 1 ? "s" : '');
94
        }
95
        if (!$timemsg) {
96
            $timemsg = "Ahora";
97
        } else {
98
            $timemsg = $timemsg . '';
99
        }
100
        return $timemsg;
101
    }
102
 
103
    /*
1 www 104
    public static function timeElapsedString(int $ptime)
105
    {
106
        $etime = time() - $ptime;
107
        if ($etime < 1) {
108
            return 'LABEL_ZERO_SECOND';
109
        }
110
        $a = array(365 * 24 * 60 * 60 => 'LABEL_YEAR_SMALL',
111
            30 * 24 * 60 * 60 => 'LABEL_MONTH_SMALL',
112
            24 * 60 * 60 => 'LABEL_DAY_SMALL',
113
            60 * 60 => 'LABEL_HOUR_SMALL',
114
            60 => 'LABEL_MINUTE_SMALL',
115
            1 => 'LABEL_SECOND_SMALL'
116
        );
117
        $a_plural = array('LABEL_YEAR_SMALL' => 'LABEL_YEARS_SMALL',
118
            'LABEL_MONTH_SMALL' => 'LABEL_MONTHS_SMALL',
119
            'LABEL_DAY_SMALL' => 'LABEL_DAYS_SMALL',
120
            'LABEL_HOUR_SMALL' => 'LABEL_HOURS_SMALL',
121
            'LABEL_MINUTE_SMALL' => 'LABEL_MINUTES_SMALL',
122
            'LABEL_SECOND_SMALL' => 'LABEL_SECONDS_SMALL'
123
        );
124
 
125
        foreach ($a as $secs => $str) {
126
            $d = $etime / $secs;
127
            if ($d >= 1) {
128
                $r = round($d);
129
                return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str);
130
            }
131
        }
3138 efrain 132
    }*/
1 www 133
 
134
 
135
    /**
136
     *
137
     * @param string $date1
138
     * @param string $date2
139
     * @param string $format
140
     */
141
    public static function getYears(string $date1, string $date2, string $format = 'YearMonth')
142
    {
143
        $date1 = new \DateTime($date1);
144
        $date2 = new \DateTime($date2);
145
        $interval = date_diff($date1, $date2);
146
        $months = $interval->m + ($interval->y * 12);
147
        return(int) ($months / 12);
148
    }
149
 
150
    /**
151
     *
152
     * @param number $length
153
     * @param string $seeds
154
     * @return string
155
     */
156
    public static function genrateRandom($length = 8, $seeds = 'alphanum')
157
    {
158
        $seedings = [
159
            'alpha' => 'abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
160
            'numeric' => '0123456789',
161
            'alphanum' => 'abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
162
            'hexidec' => '0123456789abcdef',
163
        ];
164
 
165
        if (isset($seedings[$seeds])) {
166
            $seeds = $seedings[$seeds];
167
        }
168
        list($usec, $sec) = explode(' ', microtime());
169
        $seed = (float) $sec + ((float) $usec * 100000);
1979 efrain 170
 
171
 
172
 
173
        mt_srand(intval($seed));
1 www 174
        $str = '';
175
        $seeds_count = strlen($seeds);
176
        for ($i = 0; $length > $i; $i++) {
177
            $pos = mt_rand(0, $seeds_count - 1);
178
 
179
            $str .= substr($seeds, $pos, $pos + 1);
180
        }
181
        return $str;
182
    }
183
 
184
    /**
185
     *
186
     * @param string $date1
187
     * @param string $date2
188
     * @param string $timeFormat
189
     * @param bool $positive
190
     * @return number
191
     */
192
    public static function getDateDiff(string $date1, string $date2, string $timeFormat = 'sec', bool $positive = false)
193
    {
194
        $dtTime1 = strtotime($date1);
195
        $dtTime2 = strtotime($date2);
196
        if ($positive === true) {
197
            if ($dtTime2 < $dtTime1) {
198
                $tmp = $dtTime1;
199
                $dtTime1 = $dtTime2;
200
                $dtTime2 = $tmp;
201
            }
202
        }
203
        $diff = $dtTime2 - $dtTime1;
204
        if ($timeFormat == 'sec') {
205
            return $diff;
206
        } else if ($timeFormat == 'day') {
207
            return $diff / 86400;
208
        }
209
    }
210
 
211
 
212
    /**
213
     *
214
     * @param string $date1
215
     * @param string $date2
216
     * @param string $format
217
     * @return string
218
     */
219
    public static function getDifference(string $date1, string $date2, string $format = 'YearMonth')
220
    {
221
        $difference = '';
222
        $datetime1 = date_create($date1);
223
        $datetime2 = date_create($date2);
224
        $interval = date_diff($datetime1, $datetime2);
225
        $years = $interval->format('%y');
226
        $months = $interval->format('%m');
227
        $days = $interval->format('%d');
228
        $years_text = $months_text = $days_text = '';
229
        if ($years == 1) {
230
            $years_text = '1 LABEL_YEAR';
231
        } else if ($years > 1) {
232
            $years_text = $years . ' LABEL_YEARS';
233
        }
234
        if ($months == 1) {
235
            $months_text = '1 LABEL_MONTH';
236
        } else if ($months > 1) {
237
            $months_text = $months . ' LABEL_MONTHS';
238
        }
239
        if ($days == 1) {
240
            $days_text = '1 LABEL_DAY';
241
        } else if ($days > 1) {
242
            $days_text = $days . ' LABEL_DAYS';
243
        }
244
        if ($format == 'Year') {
245
            return trim($years_text);
246
        } else if ($format == 'YearMonth') {
247
            $difference = trim($years_text) . ' ' . trim($months_text);
248
            return trim($difference);
249
        } else if ($format == 'YearMonthDay') {
250
            $difference = trim($years_text) . ' ' . trim($months_text) . ' ' . trim($days_text);
251
            return trim($difference);
252
        }
253
    }
254
 
255
    /**
256
     *
257
     * @param string $source
258
     * @param string $destination
259
     * @param int $quality
260
     * @return string
261
     */
262
    public static function compress(string $source, string $destination, int $quality = null)
263
    {
264
        $info = getimagesize($source);
265
        if ($info['mime'] == 'image/jpeg') {
266
            $image = imagecreatefromjpeg($source);
267
        }
268
        elseif ($info['mime'] == 'image/gif') {
269
            $image = imagecreatefromgif($source);
270
        }
271
        elseif ($info['mime'] == 'image/png') {
272
            $image = imagecreatefrompng($source);
273
        }
274
 
275
        imagejpeg($image, $destination, $quality);
276
        return $destination;
277
    }
278
 
279
    /**
280
     *
281
     * @param string $filename
282
     * @param string $newfilename
283
     * @param int $max_width
284
     * @param int $max_height
285
     * @param bool $withSampling
286
     * @param array $crop_coords
287
     * @return boolean
288
     */
289
    public static function resizeImage(string $filename, string $newfilename = "", int $max_width  = 0, int $max_height = 0, bool $withSampling = true, $crop_coords = array())
290
    {
291
        if (empty($newfilename)) {
292
            $newfilename = $filename;
293
        }
294
        $fileExtension = strtolower(self::getExt($filename));
295
        if ($fileExtension == 'jpg' || $fileExtension == 'jpeg') {
296
            $img = imagecreatefromjpeg($filename);
297
        } else if ($fileExtension == 'png') {
298
            $img = imagecreatefrompng($filename);
299
        } else if ($fileExtension == 'gif') {
300
            $img = imagecreatefromgif($filename);
301
        } else {
302
            $img = imagecreatefromjpeg($filename);
303
        }
304
        $width = imageSX($img);
305
        $height = imageSY($img);
306
        $target_width = $max_width;
307
        $target_height = $max_height;
308
        $target_ratio = $target_width / $target_height;
309
        $img_ratio = $width / $height;
310
        if (empty($crop_coords)) {
311
            if ($target_ratio > $img_ratio) {
312
                $new_height = $target_height;
313
                $new_width = $img_ratio * $target_height;
314
            } else {
315
                $new_height = $target_width / $img_ratio;
316
                $new_width = $target_width;
317
            }
318
            if ($new_height > $target_height) {
319
                $new_height = $target_height;
320
            }
321
            if ($new_width > $target_width) {
322
                $new_height = $target_width;
323
            }
324
            $new_img = imagecreatetruecolor($target_width, $target_height);
325
            $white = imagecolorallocate($new_img, 255, 255, 255);
326
            imagecolortransparent($new_img);
327
            imagefilledrectangle($new_img, 0, 0, $target_width - 1, $target_height - 1, $white);
328
            imagecopyresampled($new_img, $img, ($target_width - $new_width) / 2, ($target_height - $new_height) / 2, 0, 0, $new_width, $new_height, $width, $height);
329
        } else {
330
            $new_img = imagecreatetruecolor($target_width, $target_height);
331
            $white = imagecolorallocate($new_img, 255, 255, 255);
332
            imagefilledrectangle($new_img, 0, 0, $target_width - 1, $target_height - 1, $white);
333
            imagecopyresampled($new_img, $img, 0, 0, $crop_coords['x1'], $crop_coords['y1'], $target_width, $target_height, $crop_coords['x2'], $crop_coords['y2']);
334
        }
335
        if ($fileExtension == 'jpg' || $fileExtension == 'jpeg') {
336
            $createImageSave = imagejpeg($new_img, $newfilename);
337
        } else if ($fileExtension == 'png') {
338
            $createImageSave = imagepng($new_img, $newfilename);
339
        } else if ($fileExtension == 'gif') {
340
            $createImageSave = imagegif($new_img, $newfilename);
341
        } else {
342
            $createImageSave = imagejpeg($new_img, $newfilename);
343
        }
344
 
345
        return $createImageSave;
346
    }
347
 
348
    /**
349
     *
350
     * @param string $start
351
     * @param string $end
352
     * @return array
353
     */
354
    public static function getTimeDifference(string $start, string $end) {
355
        $uts = [
356
            'start' => strtotime($start),
357
            'end' => strtotime($end)
358
        ];
359
        if ($uts['start'] !== -1 && $uts['end'] !== -1) {
360
            if ($uts['end'] >= $uts['start']) {
361
                $diff = $uts['end'] - $uts['start'];
362
                if ($days = intval((floor($diff / 86400)))) {
363
                    $diff = $diff % 86400;
364
                }
365
                if ($hours = intval((floor($diff / 3600)))) {
366
                    $diff = $diff % 3600;
367
                }
368
                if ($minutes = intval((floor($diff / 60)))) {
369
                    $diff = $diff % 60;
370
                }
371
                $diff = intval($diff);
372
                return [
373
                    'days' => $days,
374
                    'hours' => $hours,
375
                    'minutes' => $minutes,
376
                    'seconds' => $diff
377
                ];
378
            } else {
379
                trigger_error("Ending date/time is earlier than the start date/time", E_USER_WARNING);
380
            }
381
        } else {
382
            trigger_error("Invalid date/time data detected", E_USER_WARNING);
383
        }
384
        return;
385
    }
386
 
387
    /**
388
     *
389
     * @param number $length
390
     * @return string
391
     */
392
    public static function generatePassword($length = 8)
393
    {
394
        $password = '';
395
        $possible = '2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ';
396
        $maxlength = strlen($possible);
397
        if ($length > $maxlength) {
398
            $length = $maxlength;
399
        }
400
        $i = 0;
401
        while ($i < $length) {
402
            $char = substr($possible, mt_rand(0, $maxlength - 1), 1);
403
            if (!strstr($password, $char)) {
404
                $password .= $char;
405
                $i++;
406
            }
407
        }
408
        return $password;
409
    }
410
 
411
 
412
 
413
    /**
414
     *
415
     * @param string $date
416
     * @param boolean $time_required
417
     * @return string
418
     */
419
    public static function countRemainingDays(string $date, $time_required = true)
420
    {
421
        $datestr = $date;
422
        $date = strtotime($datestr);
423
        $diff = $date - time();
424
        if ($time_required) {
425
            $days = floor($diff / (60 * 60 * 24));
426
            $hours = round(($diff - $days * 60 * 60 * 24) / (60 * 60));
427
            return "$days days $hours hours remaining";
428
        } else {
429
            $days = ceil($diff / (60 * 60 * 24));
430
            return "$days days remaining";
431
        }
432
    }
433
 
434
    /**
435
     *
436
     * @param string $varPhoto
437
     * @param string $uploadDir
438
     * @param string $tmp_name
439
     * @param array $th_arr
440
     * @param string $file_nm
441
     * @param boolean $addExt
442
     * @param array $crop_coords
443
     * @return string|boolean
444
     */
445
    public static function generateThumbnail(string $varPhoto, string $uploadDir, string $tmp_name, $th_arr = array(), $file_nm = '', $addExt = true, $crop_coords = array())
446
    {
447
        $ext = '.' . strtolower(self::getExt($varPhoto));
448
        $tot_th = count($th_arr);
449
        if (($ext == ".jpg" || $ext == ".gif" || $ext == ".png" || $ext == ".bmp" || $ext == ".jpeg" || $ext == ".ico")) {
450
            if (!file_exists($uploadDir)) {
451
                mkdir($uploadDir, 0777);
452
            }
453
            if ($file_nm == '')
454
                $imagename = rand() . time();
455
                else
456
                    $imagename = $file_nm;
457
                    if ($addExt || $file_nm == '')
458
                        $imagename = $imagename . $ext;
459
                        $pathToImages = $uploadDir . $imagename;
460
                        $Photo_Source = copy($tmp_name, $pathToImages);
461
                        if ($Photo_Source) {
462
                            for ($i = 0; $i < $tot_th; $i++) {
463
                                Functions::resizeImage($uploadDir . $imagename, $uploadDir . 'th' . ($i + 1) . '_' . $imagename, $th_arr[$i]['width'], $th_arr[$i]['height'], false, $crop_coords);
464
                            }
465
                            return $imagename;
466
                        } else {
467
                            return false;
468
                        }
469
        } else {
470
            return false;
471
        }
472
    }
473
    /**
474
     *
475
     * @param string $file
476
     * @return mixed
477
     */
478
    public static function getExt(string $file)
479
    {
480
        $path_parts = pathinfo($file);
481
        $ext = $path_parts['extension'];
482
        return $ext;
483
    }
484
 
485
 
486
    /**
487
     *
488
     * @param string $source
489
     * @param string $target_path
490
     * @param string $target_filename
491
     * @param number $target_width
492
     * @param number $target_height
493
     * @return boolean
494
     */
495
    public  static function uploadImage($source, $target_path, $target_filename, $target_width, $target_height )
496
    {
497
        try {
498
            $data = file_get_contents($source);
499
            $img = imagecreatefromstring($data);
500
 
501
            if(!file_exists($target_path)) {
502
                mkdir($target_path, 0755);
503
            }
504
 
505
            if($img) {
506
                list($source_width, $source_height) = getimagesize($source);
507
 
508
                $width_ratio    = $target_width / $source_width;
509
                $height_ratio   = $target_height / $source_height;
510
                if($width_ratio > $height_ratio) {
511
                    $resized_width = $target_width;
512
                    $resized_height = $source_height * $width_ratio;
513
                } else {
514
                    $resized_height = $target_height;
515
                    $resized_width = $source_width * $height_ratio;
516
                }
517
 
518
                $resized_width = round($resized_width);
519
                $resized_height = round($resized_height);
520
 
521
                $offset_width = round(($target_width - $resized_width) / 2);
522
                $offset_height = round(($target_height - $resized_height) / 2);
523
 
524
 
525
                $new_image = imageCreateTrueColor($target_width, $target_height);
526
                imageAlphaBlending($new_image, False);
527
                imageSaveAlpha($new_image, True);
528
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
529
                imagefill($new_image, 0, 0, $transparent);
530
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
531
 
532
 
533
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
534
                if(file_exists($target)) {
535
                    @unlink($target);
536
                }
537
 
538
 
539
                imagepng($new_image, $target);
540
            }
541
 
542
            unlink($source);
543
 
544
            return true;
545
 
546
        }
547
        catch (\Throwable $e)
548
        {
549
            error_log($e->getTraceAsString());
550
            return false;
551
        }
552
    }
553
 
554
 
555
    /**
556
     *
557
     * @param string $source
558
     * @param string $target_path
559
     * @param string $target_filename
560
     * @return boolean
561
     */
562
    public  static function uploadFile($source, $target_path, $target_filename)
563
    {
564
        try {
565
 
566
            $target_filename = self::normalizeString(basename($target_filename));
567
 
568
            $parts = explode('.', $target_filename);
569
            $basename = trim($parts[0]);
570
            if(strlen($basename) > 220) {
571
                $basename = substr($basename, 0, 220);
572
            }
573
            $basename = $basename . '-' . uniqid() . '.' . $parts[1];
574
            $full_filename = $target_path  . DIRECTORY_SEPARATOR . $basename;
575
 
576
 
577
            return move_uploaded_file($source, $full_filename);
578
 
579
        }
580
        catch (\Throwable $e)
581
        {
582
            error_log($e->getTraceAsString());
583
            return false;
584
        }
585
    }
586
 
587
    /**
588
     *
589
     * @param string $path
590
     * @param string $prefix
591
     * @return boolean
592
     */
593
    public static function delete($path, $prefix)
594
    {
595
        try {
596
            if (is_dir($path)){
597
                if ($dh = opendir($path)) {
598
                    while (($file = readdir($dh)) !== false)
599
                    {
600
                        if($file == '.' || $file == '..') {
601
                            continue;
602
                        }
603
 
604
                        if(strpos($file, $prefix) !== false) {
605
                            unlink($path . DIRECTORY_SEPARATOR . $file);
606
                        }
607
                    }
608
                    closedir($dh);
609
                }
610
            }
611
 
612
            return true;
613
 
614
        }
615
        catch (\Throwable $e)
616
        {
617
            error_log($e->getTraceAsString());
618
            return false;
619
        }
620
    }
621
 
622
 
623
    /**
624
     *
625
     * @param string $str
626
     * @return string
627
     */
628
    public static function normalizeString ($str = '')
629
    {
630
        $str = strtolower($str);
631
        $str = trim($str);
632
        $str = strip_tags($str);
633
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
634
        $str = preg_replace('/[\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
635
        $str = strtolower($str);
636
        $str = html_entity_decode( $str, ENT_QUOTES, "utf-8" );
637
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
638
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
639
        $str = str_replace(' ', '-', $str);
640
        $str = rawurlencode($str);
641
        $str = str_replace('%', '-', $str);
642
        return trim(strtolower($str));
643
    }
644
 
645
 
646
 
647
 
648
    /**
649
     *
650
     * @return string
651
     */
652
    public static function genUUID()
653
    {
654
 
655
        $data = random_bytes(16);
656
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
657
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
658
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
659
    }
660
 
661
 
662
 
663
 
664
 
665
    /**
666
     *
667
     * @param string $dir
668
     */
669
    public static function rmDirRecursive(string $dir)
670
    {
671
        if (is_dir($dir)) {
672
            $objects = scandir($dir);
673
            foreach ($objects as $object)
674
            {
675
                if ($object != '.' && $object != '..') {
676
                    if (is_dir($dir . DIRECTORY_SEPARATOR . $object)) {
677
                        self::rmDirRecursive($dir . DIRECTORY_SEPARATOR . $object);
678
                        rrmdir($dir . DIRECTORY_SEPARATOR . $object);
679
                    } else {
680
                        unlink($dir . DIRECTORY_SEPARATOR . $object);
681
                    }
682
                }
683
            }
684
            rmdir($dir);
685
        }
686
    }
687
 
688
    /**
689
     *
690
     * @param string $dir
691
     */
692
    public static function deleteFiles(string $dir)
693
    {
694
        if (is_dir($dir)) {
695
            $objects = scandir($dir);
696
            foreach ($objects as $object)
697
            {
698
                if ($object != '.' && $object != '..') {
699
                    if (is_dir($dir . DIRECTORY_SEPARATOR . $object)) {
700
                        self::rmDirRecursive($dir . DIRECTORY_SEPARATOR . $object);
701
                        rrmdir($dir . DIRECTORY_SEPARATOR . $object);
702
                    } else {
703
                        unlink($dir . DIRECTORY_SEPARATOR . $object);
704
                    }
705
                }
706
            }
707
        }
708
    }
709
 
710
 
711
 
712
    /**
713
     *
714
     * @param string $address
715
     * @return  array
716
     */
717
    public static function reverseGeocode(string $address)
718
    {
719
        $array = [];
720
        $address = str_replace(" ", "+", $address);
721
        $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
722
        $ch = curl_init();
723
        curl_setopt($ch, CURLOPT_URL, $url);
724
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
725
        curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
726
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
727
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
728
        $response = curl_exec($ch);
729
        curl_close($ch);
730
        $json = json_decode($response);
731
        //print_r($json);
732
        foreach ($json->results as $result) {
733
            $address1='';
734
            foreach($result->address_components as $addressPart)
735
            {
736
                if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types))) {
737
                    $city = $addressPart->long_name;
738
                }
739
                else if((in_array('administrative_area_level_1', $addressPart->types)  && (in_array('political', $addressPart->types))) || (in_array('administrative_area_level_2', $addressPart->types) && (in_array('political', $addressPart->types)))){
740
                    $state = $addressPart->long_name;
741
                } else if((in_array('postal_code', $addressPart->types))){
742
                    $postal_code = $addressPart->long_name;
743
                } else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types))) {
744
                    $country = $addressPart->long_name;
745
                } else {
746
                    $address1 .= $addressPart->long_name.', ';
747
                }
748
            }
749
            if(($city != '') && ($state != '') && ($country != '')) {
750
                $address = $city.', '.$state.', '.$country;
751
            } else if(($city != '') && ($state != '')) {
752
                $address = $city.', '.$state;
753
            } else if(($state != '') && ($country != '')) {
754
                $address = $state.', '.$country;
755
            } else if($country != '') {
756
                $address = $country;
757
            }
758
 
759
            $address1=trim($address1, ',');
760
            $array['country']=$country;
761
            $array['state']=$state;
762
            $array['city']=$city;
763
            $array['address']=$address1;
764
            $array['postal_code']=$postal_code;
765
        }
766
        $array['status']=$json->status;
767
        $array['lat'] = $json->results[0]->geometry->location->lat;
768
        $array['long'] = $json->results[0]->geometry->location->lng;
769
        return $array;
770
    }
771
 
772
 
773
 
774
    /**
775
     *
776
     * @param number $number
777
     * @param number $significance
778
     * @return number|boolean
779
     */
780
    public static function ceiling($number, $significance = 1)
781
    {
782
        return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number / $significance) * $significance) : 0;
783
    }
784
 
785
 
786
 
787
 
788
 
789
 
790
}