mime_file()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 29
c 2
b 0
f 0
nc 8
nop 1
dl 0
loc 38
ccs 0
cts 25
cp 0
crap 90
rs 8.0555
1
<?php
2
if (! function_exists('supports_ansi_colors')) {
3
    /**
4
     * Функция позволяет узнать, поддерживает ли консоль цвета и другое форматирование.
5
     * @see: https://gostash.it/ru/stashes/1601-podderzivaet-li-konsol-cveta
6
     * @return bool Результат проверки
7
     *
8
     * DIRECTORY_SEPARATOR === '\\' — проверка на Windows.
9
     * getenv('ANSICON') !== false — проверка запуска через ANSICON.
10
     * getenv('ConEmuANSI') === 'ON' — проверка запуска через ConEmu.
11
     * function_exists('posix_isatty') && @posix_isatty(\STDOUT) — проверка на интерактивный терминал UNIX.
12
     */
13
    function supports_ansi_colors()
14
    {
15
        return DIRECTORY_SEPARATOR === '\\'
16
            ? getenv('ANSICON') !== false || getenv('ConEmuANSI') === 'ON'
17
            : function_exists('posix_isatty') && @posix_isatty(\STDOUT);
18
    }
19
}
20
21
if (! function_exists('check_email')) {
22
    /**
23
     * Проверка строки с email на наличие ошибок
24
     * Если e-mail валидный, то в ответ будет получено false
25
     * В противном случае имя ошибки
26
     *     dns - ошибка проверки MX и A записи почтового домена
27
     *     format - ошибка формата email
28
     *
29
     * @param string $email проверяемый email
30
     * @param bool $dns проверять ли DNS записи
31
     * @return false|string Результат проверки почтового ящика
32
     */
33
    function check_email($email, $dns = true)
34
    {
35 7
        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
36 2
            list(, $domain) = explode("@", $email, 2);
37 2
            if (! $dns || ($dns && checkdnsrr($domain, "MX") && checkdnsrr($domain, "A"))) {
38 1
                $error = false;
39
            } else {
40 2
                $error = 'dns';
41
            }
42
        } else {
43 5
            $error = 'format';
44
        }
45
46 7
        return $error;
47
    }
48
}
49
50
if (! function_exists('generate_password')) {
51
    /**
52
     * Генерация пароля
53
     *
54
     * @param string $len длина пароля
55
     * @param string $data правила генерации пароля
56
     * @return string Строка с паролем
57
     *
58
     * Расшифровка значений $data
59
     *     "A": A-Z буквы
60
     *     "a": a-z буквы
61
     *     "0": цифры
62
     *     ".": все печатные символы
63
     *
64
     * @example
65
     *     generate_password(10,"Aa"); //nwlTVzFdIt
66
     *     generate_password(8,"0"); //71813728
67
     *     generate_password(11,"A"); //VOLRTMEFAEV
68
     *     generate_password(5,"a0"); //4hqi7
69
     *     generate_password(5,"."); //2_Vt}
70
     *     generate_password(20,"."); //AMV,>&?J)v55,(^g}Z06
71
     *     generate_password(20,"aaa0aaa.A"); //rtvKja5xb0\KpdiRR1if
72
     */
73
    function generate_password($len, $data = '')
74
    {
75 3
        if ($data == '') {
76
            $data = 'Aa0.';
77
        }
78 3
        $opt = strlen($data);
79 3
        $pass = [];
80 3
        for ($i = $len; $i > 0; $i--) {
81 3
            switch ($data[rand(0, ($opt - 1))]) {
82 3
                case 'A':
83 1
                    $tmp = rand(65, 90);
84 1
                    break;
85 3
                case 'a':
86 1
                    $tmp = rand(97, 122);
87 1
                    break;
88 2
                case '0':
89 1
                    $tmp = rand(48, 57);
90 1
                    break;
91
                default:
92 1
                    $tmp = rand(33, 126);
93
            }
94 3
            $pass[] = chr($tmp);
95
        }
96 3
        $pass = implode("", $pass);
97
98 3
        return $pass;
99
    }
100
}
101
102
if (! function_exists('get_gravatar')) {
103
    /**
104
     * Получение ссылки на аватарку с gravatar
105
     *
106
     * @param   string $email Почта
107
     * @param   integer $size Размер аватарки
108
     * @return  string
109
     */
110
    function get_gravatar($email, $size = 32)
111
    {
112 2
        $url = '//www.gravatar.com/avatar/' . md5(is_scalar($email) ? $email : '') . '?s=' . (int)abs($size);
0 ignored issues
show
introduced by
The condition is_scalar($email) is always true.
Loading history...
113
114 2
        return $url;
115
    }
116
}
117
118
if (! function_exists('share_vk')) {
119
    /**
120
     * Получение ссылки поделиться для "Вконтакте"
121
     *
122
     * @param  string $url Страница которой следует поделиться
123
     * @param  string $title Заголовок
124
     * @return string
125
     */
126
    function share_vk($url, $title = '')
127
    {
128
        return 'http://vkontakte.ru/share.php?url=' . urlencode($url) . '&title=' . urlencode($title);
129
    }
130
}
131
132
if (! function_exists('share_ok')) {
133
    /**
134
     * Получение ссылки поделиться для "Одноклассников"
135
     *
136
     * @param  string $url Страница которой следует поделиться
137
     * @param  string $title Заголовок
138
     * @return string
139
     */
140
    function share_ok($url, $title = '')
141
    {
142
        return 'http://www.odnoklassniki.ru/dk?st.cmd=addShare' .
143
            '&st._surl=' . urlencode($url) .
144
            '&st.comments=' . urlencode($title);
145
    }
146
}
147
148
if (! function_exists('share_google')) {
149
    /**
150
     * Получение ссылки поделиться для "Google+"
151
     *
152
     * @param  string $url Страница которой следует поделиться
153
     * @return string
154
     */
155
    function share_google($url)
156
    {
157
        return 'https://plus.google.com/share?url=' . urlencode($url);
158
    }
159
}
160
161
if (! function_exists('share_facebook')) {
162
    /**
163
     * Получение ссылки поделиться для "Facebook"
164
     *
165
     * @param  string $url Страница которой следует поделиться
166
     * @param  string $title Заголовок
167
     * @return string
168
     */
169
    function share_facebook($url, $title = '')
170
    {
171
        return 'http://www.facebook.com/sharer/sharer.php?s=100' .
172
            '&p[url]=' . urlencode($url) .
173
            '&p[title]=' . urlencode($title);
174
    }
175
}
176
177
if (! function_exists('share_twitter')) {
178
    /**
179
     * Получение ссылки поделиться для "Twitter"
180
     *
181
     * @param  string $url Страница которой следует поделиться
182
     * @param  string $title Заголовок
183
     * @return string
184
     */
185
    function share_twitter($url, $title = '')
186
    {
187
        return 'https://twitter.com/intent/tweet?url=' . urlencode($url) . '&text=' . urlencode($title);
188
    }
189
}
190
191
if (! function_exists('share_mail')) {
192
    /**
193
     * Получение ссылки поделиться для "Mail.ru"
194
     *
195
     * @param  string $url Страница которой следует поделиться
196
     * @param  string $title Заголовок
197
     * @return string
198
     */
199
    function share_mail($url, $title = '')
200
    {
201
        return 'http://connect.mail.ru/share?share_url=' . urlencode($url) . '&title=' . urlencode($title);
202
    }
203
}
204
205
if (! function_exists('share_linkedin')) {
206
    /**
207
     * Получение ссылки поделиться для "LinkedIN"
208
     *
209
     * @param  string $url Страница которой следует поделиться
210
     * @param  string $title Заголовок
211
     * @return string
212
     */
213
    function share_linkedin($url, $title = '')
214
    {
215
        return 'http://www.linkedin.com/shareArticle?mini=true&url=' . urlencode($url) . '&title=' . urlencode($title);
216
    }
217
}
218
219
if (! function_exists('qr_code')) {
220
    /**
221
     * Генерация QR-кода для строки
222
     *
223
     * @param string $str строка
224
     * @param int $size размер картинки
225
     * @return string
226
     */
227
    function qr_code($str, $size = 230)
228
    {
229
        $size = (int)$size;
230
        $size = implode("x", [$size, $size]);
231
232
        return '//chart.apis.google.com/chart?cht=qr&chs=' . $size . '&chl=' . (is_scalar($str) ? urlencode($str) : '');
0 ignored issues
show
introduced by
The condition is_scalar($str) is always true.
Loading history...
233
    }
234
}
235
236
if (! function_exists('get_user_ip')) {
237
    /**
238
     * Получение реального ip текущего пользователя
239
     *
240
     * @param string $out IP адрес который будет отдан функцией, если больше ничего не обнаружено
241
     * @return string IP пользователя
242
     *
243
     * @see http://stackoverflow.com/questions/5036443/php-how-to-block-proxies-from-my-site
244
     */
245
    function get_user_ip($out = '127.0.0.1')
246
    {
247
        $_getEnv = function ($data) {
248
            switch (true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $tmp = getenv($data) of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
249
                case (isset($_SERVER[$data])):
250
                    $out = $_SERVER[$data];
251
                    break;
252
                case (isset($_ENV[$data])):
253
                    $out = $_ENV[$data];
254
                    break;
255
                case ($tmp = getenv($data)):
256
                    $out = $tmp;
257
                    break;
258
                case (function_exists('apache_getenv') && $tmp = apache_getenv($data, true)):
259
                    $out = $tmp;
260
                    break;
261
                default:
262
                    $out = false;
263
            }
264
            unset($tmp);
265
266
            return $out;
267
        };
268
269
        //Порядок условий зависит от приоритетов
270
        switch (true === true) {
271
            case ($tmp = $_getEnv('HTTP_COMING_FROM')):
272
                $out = $tmp;
273
                break;
274
            case ($tmp = $_getEnv('HTTP_X_COMING_FROM')):
275
                $out = $tmp;
276
                break;
277
            case ($tmp = $_getEnv('HTTP_VIA')):
278
                $out = $tmp;
279
                break;
280
            case ($tmp = $_getEnv('HTTP_FORWARDED')):
281
                $out = $tmp;
282
                break;
283
            case ($tmp = $_getEnv('HTTP_FORWARDED_FOR')):
284
                $out = $tmp;
285
                break;
286
            case ($tmp = $_getEnv('HTTP_X_FORWARDED')):
287
                $out = $tmp;
288
                break;
289
            case ($tmp = $_getEnv('HTTP_X_FORWARDED_FOR')):
290
                $out = $tmp;
291
                break;
292
            case (! empty($_SERVER['REMOTE_ADDR'])):
293
                $out = $_SERVER['REMOTE_ADDR'];
294
                break;
295
        }
296
        unset($tmp);
297
298
        return (is_scalar($out) && preg_match('|^(?:[0-9]{1,3}\.){3,3}[0-9]{1,3}$|', $out, $matches)) ? $out : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return is_scalar($out) &...matches) ? $out : false could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
299
    }
300
}
301
302
if (! function_exists('whois_query')) {
303
    /**
304
     * Получение whois информации о домене
305
     * @see http://www.jonasjohn.de/snippets/php/whois-query.htm
306
     *
307
     * @param string $domain домен
308
     * @return string
309
     */
310
    function whois_query($domain)
311
    {
312
313
        // fix the domain name:
314
        $domain = strtolower(trim($domain));
315
        $domain = preg_replace('/^http:\/\//i', '', $domain);
316
        $domain = preg_replace('/^www\./i', '', $domain);
317
        $domain = explode('/', $domain);
318
        $domain = trim($domain[0]);
319
320
        // split the TLD from domain name
321
        $_domain = explode('.', $domain);
322
        $lst = count($_domain) - 1;
323
        $ext = $_domain[$lst];
324
325
        // You find resources and lists
326
        // like these on wikipedia:
327
        //
328
        // <a href="http://de.wikipedia.org/wiki/Whois">http://de.wikipedia.org/wiki/Whois</a>
329
        //
330
        $servers = [
331
            "biz"  => "whois.neulevel.biz",
332
            "com"  => "whois.internic.net",
333
            "us"   => "whois.nic.us",
334
            "coop" => "whois.nic.coop",
335
            "info" => "whois.nic.info",
336
            "name" => "whois.nic.name",
337
            "net"  => "whois.internic.net",
338
            "gov"  => "whois.nic.gov",
339
            "edu"  => "whois.internic.net",
340
            "mil"  => "rs.internic.net",
341
            "int"  => "whois.iana.org",
342
            "ac"   => "whois.nic.ac",
343
            "ae"   => "whois.uaenic.ae",
344
            "at"   => "whois.ripe.net",
345
            "au"   => "whois.aunic.net",
346
            "be"   => "whois.dns.be",
347
            "bg"   => "whois.ripe.net",
348
            "br"   => "whois.registro.br",
349
            "bz"   => "whois.belizenic.bz",
350
            "ca"   => "whois.cira.ca",
351
            "cc"   => "whois.nic.cc",
352
            "ch"   => "whois.nic.ch",
353
            "cl"   => "whois.nic.cl",
354
            "cn"   => "whois.cnnic.net.cn",
355
            "cz"   => "whois.nic.cz",
356
            "de"   => "whois.nic.de",
357
            "fr"   => "whois.nic.fr",
358
            "hu"   => "whois.nic.hu",
359
            "ie"   => "whois.domainregistry.ie",
360
            "il"   => "whois.isoc.org.il",
361
            "in"   => "whois.ncst.ernet.in",
362
            "ir"   => "whois.nic.ir",
363
            "mc"   => "whois.ripe.net",
364
            "to"   => "whois.tonic.to",
365
            "tv"   => "whois.tv",
366
            "ru"   => "whois.ripn.net",
367
            "org"  => "whois.pir.org",
368
            "aero" => "whois.information.aero",
369
            "nl"   => "whois.domain-registry.nl"
370
        ];
371
372
        if (! isset($servers[$ext])) {
373
            throw new ErrorException('No matching nic server found!');
374
        }
375
376
        $nic_server = $servers[$ext];
377
378
        $output = '';
379
380
        // connect to whois server:
381
        if ($conn = fsockopen($nic_server, 43)) {
382
            fputs($conn, $domain . "\r\n");
383
            while (! feof($conn)) {
384
                $output .= fgets($conn, 128);
385
            }
386
            fclose($conn);
387
        } else {
388
            throw new ErrorException('Could not connect to ' . $nic_server . '!');
389
        }
390
391
        return $output;
392
    }
393
}
394
395
if (! function_exists('copyright')) {
396
    /**
397
     * Геренатор года для подстановки в копирайты
398
     *
399
     * @param string $year год запуска проекта
400
     * @param string $sep разделитель годов
401
     * @return string
402
     */
403
    function copyright($year, $sep = ' - ')
404
    {
405
        $y = date('Y');
406
407
        return ($y != $year) ? ($year . $sep . $y) : $year;
408
    }
409
}
410
411
if (! function_exists('mime_file')) {
412
    /**
413
     * Получение MIME типа файла
414
     *
415
     * @param string $fname путь к файлу
416
     * @return string
417
     */
418
    function mime_file($fname)
419
    {
420
        $out = null;
421
        switch (true) {
422
            case class_exists('\finfo'):
423
                $fi = new \finfo(FILEINFO_MIME);
424
                $out = $fi->file($fname);
425
                break;
426
            case function_exists('mime_content_type'):
427
                list($out) = explode(';', @mime_content_type($fname));
428
                break;
429
            default:
430
                /**
431
                 * @see: http://www.php.net/manual/ru/function.finfo-open.php#112617
432
                 */
433
                $fh = fopen($fname, 'rb');
434
                if ($fh) {
0 ignored issues
show
introduced by
$fh is of type resource|false, thus it always evaluated to false.
Loading history...
435
                    $bytes6 = fread($fh, 6);
436
                    fclose($fh);
437
                    switch (true) {
438
                        case ($bytes6 === false):
439
                            break;
440
                        case (substr($bytes6, 0, 3) == "\xff\xd8\xff"):
441
                            $out = 'image/jpeg';
442
                            break;
443
                        case ($bytes6 == "\x89PNG\x0d\x0a"):
444
                            $out = 'image/png';
445
                            break;
446
                        case ($bytes6 == "GIF87a" || $bytes6 == "GIF89a"):
447
                            $out = 'image/gif';
448
                            break;
449
                        default:
450
                            $out = 'application/octet-stream';
451
                    }
452
                }
453
        }
454
455
        return $out;
456
    }
457
}
458
459
if (! function_exists('image_size')) {
460
    /**
461
     * Определение размеров картинки
462
     *
463
     * @param string $image путь к картинке
464
     * @param string|null $mode какой размер ширину/высоту или все вместе
465
     * @return array|int
466
     */
467
    function image_size($image, $mode = null)
468
    {
469
        $width = $height = 0;
470
        if (is_scalar($image) && is_file($image)) {
471
            $size = @getimagesize($image);
472
            $width = isset($size[0]) ? $size[0] : 0;
473
            $height = isset($size[1]) ? $size[1] : 0;
474
        }
475
        switch ($mode) {
476
            case 'w':
477
            case 'width':
478
                $out = $width;
479
                break;
480
            case 'h':
481
            case 'height':
482
                $out = $height;
483
                break;
484
            default:
485
                $out = [$width, $height];
486
        }
487
488
        return $out;
489
    }
490
}
491
492
if (! function_exists('plural')) {
493
    /**
494
     * Определение падежа слова в зависимости от числового значения
495
     *
496
     * @param int|string $number число
497
     * @param array $titles массив слов в разных склонениях (1 яблоко, 2 яблока, 5 яблок)
498
     * @return string
499
     */
500
    function plural($number, array $titles = [])
501
    {
502
        $cases = [2, 0, 1, 1, 1, 2];
503
        $number = abs((int)$number);
504
        $position = ($number % 100 > 4 && $number % 100 < 20) ? 2 : $cases[($number % 10 < 5) ? $number % 10 : 5];
505
        $out = isset($titles[$position]) ? $titles[$position] : '';
506
507
        return $out;
508
    }
509
}
510
511
if (! function_exists('validate_date')) {
512
    /**
513
     * Проверка валидности даты
514
     *
515
     * Пример валидации даты через дополнительную проверку $validator
516
     * function($date, $iterval){
517
     *        return ($iterval->format('%R') == '+');
518
     * }
519
     * Таким образом все даты которые уже прошли будут помечены как не валидные
520
     *
521
     * @param string $date проверяемая дата
522
     * @param string $fromFormat в каком формате записана исходная дата
523
     * @param string $toFormat в каком формате вернуть дату, если она валидна
524
     * @param Closure $validator метод для дополнительной проверки даты
525
     * @return null|string
526
     */
527
    function validate_date($date, $fromFormat = 'Y-m-d', $toFormat = 'Y-m-d', Closure $validator = null)
528
    {
529
        $validTime = false;
530
        $datetime2 = null;
531
        if (is_scalar($date)) {
0 ignored issues
show
introduced by
The condition is_scalar($date) is always true.
Loading history...
532
            $datetime1 = new \DateTime("NOW");
533
            $datetime2 = \DateTime::createFromFormat($fromFormat, $date);
534
            if ($datetime2 instanceof \DateTime) {
535
                $interval = $datetime1->diff($datetime2);
536
                $validTime = is_callable($validator) ? (bool)$validator($datetime2, $interval) : true;
537
            }
538
        }
539
540
        return $validTime ? $datetime2->format($toFormat) : null;
541
    }
542
}
543
if (! function_exists('format_bytes')) {
544
    /**
545
     * Преобразование из байт в другие порядки (кило, мега, гига) с добавлением префикса
546
     *
547
     * @param string $bytes Обрабатываемое число
548
     * @param integer $precision До какого числа после запятой округлять
549
     * @param array $suffixes Массив суффиксов
550
     * @return string
551
     */
552
    function format_bytes($bytes, $precision = 2, $suffixes = ['Байт', 'Кбайт', 'Мбайт', 'Гбайт', 'Тбайт'])
553
    {
554
        $bytes = (float)$bytes;
555
        if (empty($bytes)) {
556
            return 0;
557
        }
558
        $base = log($bytes, 1024);
559
560
        return trim(
561
            round(pow(1024, $base - floor($base)), $precision) .
562
            ' ' .
563
            get_key($suffixes, (int)$base, '', 'is_scalar')
0 ignored issues
show
Bug introduced by
'is_scalar' of type string is incompatible with the type Closure expected by parameter $validate of get_key(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

563
            get_key($suffixes, (int)$base, '', /** @scrutinizer ignore-type */ 'is_scalar')
Loading history...
564
        );
565
    }
566
}
567
568
if (! function_exists('format_microtime')) {
569
    /**
570
     * Форматирование microtime времени
571
     * @param string $time microtime время
572
     * @param int $len Кол-во символов после точки
573
     */
574
    function format_microtime($time, $len = 4)
575
    {
576
        return sprintf("%." . (int)$len . "f", $time);
577
    }
578
}
579
if (! function_exists('ip_in_range')) {
580
    /**
581
     * Входит ли указанный IP в заданный диапазон
582
     *
583
     * @param string $ip IP клиента
584
     * @param string $lower Начальный IP диапазона
585
     * @param string $upper Конечный IP диапазона
586
     * @return bool
587
     */
588
    function in_ip_range($ip, $lower, $upper)
589
    {
590
        return (ip2long($lower) <= ip2long($ip) && ip2long($upper) >= ip2long($ip)) ? true : false;
591
    }
592
}
593
594
if (! function_exists('make_csv')) {
595
    /**
596
     * Формирование правильной CSV строки
597
     *
598
     * @see: https://stackoverflow.com/questions/3933668/convert-array-into-csv
599
     * @param array $data Массив с данными
600
     * @return string
601
     */
602
    function make_csv($data, $separator = ",")
603
    {
604
        // Create a stream opening it with read / write mode
605
        $stream = fopen('data://text/plain,' . "", 'w+');
606
607
        // Iterate over the data, writting each line to the text stream
608
        fputcsv($stream, $data, $separator);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

608
        fputcsv(/** @scrutinizer ignore-type */ $stream, $data, $separator);
Loading history...
609
610
        // Rewind the stream
611
        rewind($stream);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

611
        rewind(/** @scrutinizer ignore-type */ $stream);
Loading history...
612
613
        // You can now echo it's content
614
        $out = stream_get_contents($stream);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of stream_get_contents() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

614
        $out = stream_get_contents(/** @scrutinizer ignore-type */ $stream);
Loading history...
615
616
        // Close the stream
617
        fclose($stream);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

617
        fclose(/** @scrutinizer ignore-type */ $stream);
Loading history...
618
619
        return $out;
620
    }
621
}
622