Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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