From 0649ff427a92e933007e91cdc01bc3ffb9c71116 Mon Sep 17 00:00:00 2001 From: ahilespelid Date: Mon, 25 Nov 2024 14:26:34 +0300 Subject: [PATCH 01/11] add laravel path functions --- init.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/init.php b/init.php index 89334f5..0819487 100644 --- a/init.php +++ b/init.php @@ -1,4 +1,4 @@ - Date: Sat, 11 Jan 2025 23:12:30 +0300 Subject: [PATCH 02/11] add function format_date_intl Musa --- init.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/init.php b/init.php index 0819487..ff0c09e 100644 --- a/init.php +++ b/init.php @@ -177,3 +177,25 @@ function translit(string $t){ return $ret;}} ///*/ahilespelid///*/ + +// Функция для форматирования даты musa +if(!function_exists('format_date_intl')) { + function format_date_intl($date, $lang = 'ru_RU',$date_format = 'd MMMM yyyy') { + $formatter = new IntlDateFormatter( + 'ru_RU', + IntlDateFormatter::FULL, + IntlDateFormatter::NONE, + 'Europe/Moscow', + IntlDateFormatter::GREGORIAN, + $date_format + ); + $timestamp = strtotime($date); + return $formatter->format($timestamp); + } +} + +if(!function_exists('first_slash')) { + function first_slash($str) { + return ('/' == $str) ? $str : '/' . $str; + } +} From f073b0a4d2684250167759224cec02962094efc7 Mon Sep 17 00:00:00 2001 From: Ahi Les <47417767+ahilespelid@users.noreply.github.com> Date: Wed, 12 Feb 2025 11:56:33 +0300 Subject: [PATCH 03/11] Update init.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit добавил константу DS --- init.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/init.php b/init.php index ff0c09e..c26ee60 100644 --- a/init.php +++ b/init.php @@ -1,4 +1,6 @@ Date: Wed, 12 Mar 2025 10:40:27 +0300 Subject: [PATCH 04/11] new function formate date --- init.php | 67 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/init.php b/init.php index c26ee60..d4dcd76 100644 --- a/init.php +++ b/init.php @@ -121,7 +121,7 @@ function is_date($d){ ///*/ Фукция проверяет строку на json ///*/ if(!function_exists('is_json')){function is_json($json){ $decoded = @json_decode($json); -return \JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, \JSON_FORCE_OBJEC) : null;}} +return \JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, \JSON_FORCE_OBJECT) : null;}} ///*/ Фукция проверяет переменную на true ///*/ if(!function_exists('is_true')){function is_true($bool){ $ret = (true === is_bool($bool) && true === $bool) ? true : false; @@ -137,7 +137,7 @@ function translit(string $t){ return strtr($t, $converter);} ///*/ Функция разбивает строку по разделителям переданным в массиве///*/ if(!function_exists('mexplode')){function mexplode(array $delimiters, string $string){ - $chr = '::::::::::::::::::::::::::::::::::::::::::::::::'; + $chr = '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::'; return explode($chr, str_replace($delimiters, $chr, $string));}} //------------------------------------------------------------------------------STRING-GENERATORS------------------------------------------------------------------------------------------------------------------------// @@ -163,7 +163,7 @@ function translit(string $t){ $data[8] = chr(ord($data[8]) & 0x3f | 0x80); return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));}} -//------------------------------------------------------------------------------DATA-EXCHANGE-----------------------------------------------------------------------------------------------------------------------------------// +//------------------------------------------------------------------------------DATA-EXCHANGE----------------------------------------------------------------------------------------------------------------------------// ///*/ Метод эмитации post запроса из php///*/ if(!function_exists('post')){function post(string $url, array $data, array $headers = [], bool $data_json_encode = false){ $data = ($data_json_encode) ? json_encode($data) : http_build_query($data); @@ -179,25 +179,46 @@ function translit(string $t){ return $ret;}} ///*/ahilespelid///*/ +///*/musa///*/ +///*/Функция для форматирования даты///*/ -// Функция для форматирования даты musa -if(!function_exists('format_date_intl')) { - function format_date_intl($date, $lang = 'ru_RU',$date_format = 'd MMMM yyyy') { - $formatter = new IntlDateFormatter( - 'ru_RU', - IntlDateFormatter::FULL, - IntlDateFormatter::NONE, - 'Europe/Moscow', - IntlDateFormatter::GREGORIAN, - $date_format - ); - $timestamp = strtotime($date); - return $formatter->format($timestamp); - } -} +if(!function_exists('format_date_intl')){ + function format_date_intl(string $date, string $lang = 'russian'): ?string { + if(!$d = is_date($date)){return null;} + + $supportedLocales = ['russian' => 'ru_RU', 'english' => 'en_US']; + $locale = $supportedLocales[$lang] ?? 'ru_RU'; +// $d = DateTime::createFromFormat('Y-m-d H:i:s', $date) ?: new DateTime($date); + $now = new DateTime(); + $yesterday = (clone $now)->modify('-1 day'); + + $diff = $now->getTimestamp() - $d->getTimestamp(); + + if($diff < 3600 && $d->format('Y-m-d') === $now->format('Y-m-d')){ + $minutes = floor($diff / 60); + return $minutes > 0 + ? ($lang === 'ru' ? "$minutes мин. назад" : "$minutes min. ago") + : ($lang === 'ru' ? "только что" : "just now");} + + if($diff < 86400 && $d->format('Y-m-d') === $now->format('Y-m-d')){ + $hours = floor($diff / 3600); + return $lang === 'ru' ? "$hours ч. назад" : "$hours h. ago";} + + if($d->format('Y-m-d') === $yesterday->format('Y-m-d')){ + $timeFormat = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::SHORT); + return ($lang === 'ru' ? 'Вчера в ' : 'Yesterday at ') . $timeFormat->format($d);} + + $dateFormat = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE); +return $dateFormat->format($d);}} -if(!function_exists('first_slash')) { - function first_slash($str) { - return ('/' == $str) ? $str : '/' . $str; - } -} + +// if(!function_exists('format_date_intl')){function format_date_intl($date, $lang = 'ru_RU', $date_format = 'd MMMM yyyy'){ +// if(!($d = is_date($date))){return null;} +// $formatter = new IntlDateFormatter('ru_RU', IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/Moscow', IntlDateFormatter::GREGORIAN, $date_format); +// $timestamp = $d->getTimestamp(); +// return $formatter->format($timestamp);}} + +///*/ ВЫНЕСЕМ НА БРИФ ///*/ +if(!function_exists('first_slash')) {function first_slash($str){return ('/' == $str) ? $str : '/' . $str;}} + +///*/musa///*/ \ No newline at end of file From 466c6c8caef3bd5a869acbe0deb8eba509bf3fea Mon Sep 17 00:00:00 2001 From: Ahi Les <47417767+ahilespelid@users.noreply.github.com> Date: Wed, 19 Mar 2025 17:39:19 +0300 Subject: [PATCH 05/11] Update init.php add function is_ip --- init.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/init.php b/init.php index d4dcd76..958f3ea 100644 --- a/init.php +++ b/init.php @@ -118,6 +118,8 @@ function is_date($d){ return (preg_match('/^[7|8][0-9]{'.$minDigits.','.$maxDigits.'}\z/', $s)) ? $s : null;}} ///*/ahilespelid Проверка строки на email///*/ if(!function_exists('is_email')){function is_email(string $email){return (false !== filter_var($email, FILTER_VALIDATE_EMAIL)) ? $email : null;}} +///*/ahilespelid Проверка строки на ip///*/ +if(!function_exists('is_ip')){function is_ip(string $ip){return (false !== filter_var($ip, FILTER_VALIDATE_IP)) ? $ip : null;}} ///*/ Фукция проверяет строку на json ///*/ if(!function_exists('is_json')){function is_json($json){ $decoded = @json_decode($json); @@ -221,4 +223,4 @@ function format_date_intl(string $date, string $lang = 'russian'): ?string { ///*/ ВЫНЕСЕМ НА БРИФ ///*/ if(!function_exists('first_slash')) {function first_slash($str){return ('/' == $str) ? $str : '/' . $str;}} -///*/musa///*/ \ No newline at end of file +///*/musa///*/ From 20a74139f3b5a34de3b87411e7ae8dd0e47eac28 Mon Sep 17 00:00:00 2001 From: Ahi Les <47417767+ahilespelid@users.noreply.github.com> Date: Fri, 18 Apr 2025 11:17:16 +0300 Subject: [PATCH 06/11] add array_merge_save function --- init.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/init.php b/init.php index 958f3ea..00c9776 100644 --- a/init.php +++ b/init.php @@ -65,6 +65,12 @@ function array_unique_key($array, $key){ if(!function_exists('array_keys_starts_with')){function array_keys_starts_with(array $array, string $needle):?array{ $ret = array_filter($array, function($v, $k) use($needle) {return strpos(strtolower($k), strtolower($needle)) === 0;}, ARRAY_FILTER_USE_BOTH); return (empty($ret)) ? null : $ret;}} +///*/ функция объединяет любое количество массивов с сохранением структур и значений///*/ +if(!function_exists('array_merge_save')){function array_merge_save(...$arrays){ + return array_reduce($arrays, function($c, $i){ + return array_reduce(array_keys($i), function($c, $k) use ($i){ + return $c + [$k => $c[$k] ?? [] + [$i[$k]]];}, $c);}, []); +}} //------------------------------------------------------------------------------FILE-SYSTEM------------------------------------------------------------------------------------------------------------------------------// ///*/ ahilespelid Метод возвращает путь до папки local/php_interface Bitrix при учёте что текущий файл лежит в local/php_interface///*/ From 93a7c40da59eaae6b6a9136815193752831f1f1c Mon Sep 17 00:00:00 2001 From: Ahi Les <47417767+ahilespelid@users.noreply.github.com> Date: Fri, 18 Apr 2025 13:32:14 +0300 Subject: [PATCH 07/11] Update init.php --- init.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/init.php b/init.php index 00c9776..74d991c 100644 --- a/init.php +++ b/init.php @@ -68,9 +68,9 @@ function array_unique_key($array, $key){ ///*/ функция объединяет любое количество массивов с сохранением структур и значений///*/ if(!function_exists('array_merge_save')){function array_merge_save(...$arrays){ return array_reduce($arrays, function($c, $i){ - return array_reduce(array_keys($i), function($c, $k) use ($i){ + return array_reduce(array_keys($i ?? []), function($c, $k) use ($i){ return $c + [$k => $c[$k] ?? [] + [$i[$k]]];}, $c);}, []); -}} +}} /* $payload = (function_exists('array_merge_save')) ? array_merge_save($_REQUEST_JSON, $_GET, $_POST) : array_merge($_POST, $_GET, $_REQUEST_JSON); */ //------------------------------------------------------------------------------FILE-SYSTEM------------------------------------------------------------------------------------------------------------------------------// ///*/ ahilespelid Метод возвращает путь до папки local/php_interface Bitrix при учёте что текущий файл лежит в local/php_interface///*/ From c6711e4763717f49f7c636f52e3c84a363137b1d Mon Sep 17 00:00:00 2001 From: Ahi Les <47417767+ahilespelid@users.noreply.github.com> Date: Fri, 18 Apr 2025 15:05:28 +0300 Subject: [PATCH 08/11] array_flatten add --- init.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/init.php b/init.php index 74d991c..112b5c2 100644 --- a/init.php +++ b/init.php @@ -71,7 +71,14 @@ function array_unique_key($array, $key){ return array_reduce(array_keys($i ?? []), function($c, $k) use ($i){ return $c + [$k => $c[$k] ?? [] + [$i[$k]]];}, $c);}, []); }} /* $payload = (function_exists('array_merge_save')) ? array_merge_save($_REQUEST_JSON, $_GET, $_POST) : array_merge($_POST, $_GET, $_REQUEST_JSON); */ - +///*/ функция разглаживает многомерный ассоциативный массив в одномерный с точками в ключах///*/ +if(!function_exists('array_flatten')){function array_flatten($array, $prefix = ''){ + $ret = [];foreach($array as $k => $v){ + $cur_k = is_string($k) ? ($prefix !== '' ? $prefix.'.'.$k : $k) : $prefix; + if(is_array($v)){$ret = array_merge($ret, array_flatten($v, $cur_k));}else + if(is_string($k)){$ret[$cur_k] = $v;}else + if($cur_k !== ''){$ret[$cur_k] = $v;}} +return $ret;}} //------------------------------------------------------------------------------FILE-SYSTEM------------------------------------------------------------------------------------------------------------------------------// ///*/ ahilespelid Метод возвращает путь до папки local/php_interface Bitrix при учёте что текущий файл лежит в local/php_interface///*/ if(!function_exists('functions_path')){function functions_path(string $file=''){ From db22262f5d06c60a3133142f459f7e522e8bf183 Mon Sep 17 00:00:00 2001 From: Ahi Les <47417767+ahilespelid@users.noreply.github.com> Date: Wed, 21 May 2025 16:48:07 +0300 Subject: [PATCH 09/11] Update init.php --- init.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/init.php b/init.php index 112b5c2..fbfca38 100644 --- a/init.php +++ b/init.php @@ -154,6 +154,11 @@ function translit(string $t){ if(!function_exists('mexplode')){function mexplode(array $delimiters, string $string){ $chr = '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::'; return explode($chr, str_replace($delimiters, $chr, $string));}} +///*/ Функция переводчик google///*/ +if(!function_exists('google_translate')){function google_translate($text, $from_lan = 'ru', $to_lan = 'en', $key = 'AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw'){ + $obj = json_decode(file_get_contents('https://translation.googleapis.com/language/translate/v2?q='.urlencode($text).'&source='.$from_lan.'&target='.$to_lan .'&format=text&key='.$key), true); + $ret = trim($obj['data']['translations']['0']['translatedText']); +return (empty($ret)) ? false : $ret;}} //------------------------------------------------------------------------------STRING-GENERATORS------------------------------------------------------------------------------------------------------------------------// ///*/ Функция генерирует случайный префикс из ascii таблицы///*/ @@ -165,7 +170,6 @@ function translit(string $t){ ///*/ Функция генерирует GUID из com_create_guid///*/ if(!function_exists('GUID')){function GUID(){return strtoupper(com_create_guid());}} - //------------------------------------------------------------------------------ANALOG-----------------------------------------------------------------------------------------------------------------------------------// ///*/ Функция аналог str_starts_with из php 8 ///*/ if(!function_exists('str_starts_with')){function str_starts_with(string $haystack, string $needle){ From eb0223bf7e00e5cb597faa0837be618d4429c86a Mon Sep 17 00:00:00 2001 From: Ahi Les <47417767+ahilespelid@users.noreply.github.com> Date: Wed, 11 Jun 2025 06:45:35 +0300 Subject: [PATCH 10/11] Update init.php --- init.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.php b/init.php index fbfca38..53615ba 100644 --- a/init.php +++ b/init.php @@ -134,7 +134,7 @@ function is_date($d){ ///*/ahilespelid Проверка строки на ip///*/ if(!function_exists('is_ip')){function is_ip(string $ip){return (false !== filter_var($ip, FILTER_VALIDATE_IP)) ? $ip : null;}} ///*/ Фукция проверяет строку на json ///*/ -if(!function_exists('is_json')){function is_json($json){ +if(!function_exists('is_json')){function is_json(string $json){ $decoded = @json_decode($json); return \JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, \JSON_FORCE_OBJECT) : null;}} ///*/ Фукция проверяет переменную на true ///*/ From e8a99625c82de49871769cee6cc4f885c7ae62c4 Mon Sep 17 00:00:00 2001 From: Ahi Les <47417767+ahilespelid@users.noreply.github.com> Date: Wed, 11 Jun 2025 18:22:02 +0300 Subject: [PATCH 11/11] add word_contains function --- init.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/init.php b/init.php index 53615ba..536fdc8 100644 --- a/init.php +++ b/init.php @@ -159,6 +159,10 @@ function translit(string $t){ $obj = json_decode(file_get_contents('https://translation.googleapis.com/language/translate/v2?q='.urlencode($text).'&source='.$from_lan.'&target='.$to_lan .'&format=text&key='.$key), true); $ret = trim($obj['data']['translations']['0']['translatedText']); return (empty($ret)) ? false : $ret;}} +///*/ Функция проверяет вхождение слова в строку ///*/ +if(!function_exists('word_contains')){function word_contains(string $text, string $word): bool { + $escaped = preg_quote($word, '/'); +return (bool) preg_match("/(^|\W)" . $escaped . "(\W|$)/", $text);}} //------------------------------------------------------------------------------STRING-GENERATORS------------------------------------------------------------------------------------------------------------------------// ///*/ Функция генерирует случайный префикс из ascii таблицы///*/