12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085 |
- <?php
- /**
- * Some legacy workaround here
- */
- if (!function_exists('__')) {
- /**
- * Dummy i18n function
- *
- * @param string $str
- * @return string
- */
- function __($str) {
- global $lang;
- if (isset($lang['def'][$str])) {
- if (!empty($lang['def'][$str])) {
- $str = $lang['def'][$str];
- }
- }
- return ($str);
- }
- }
- if (!function_exists('log_register')) {
- /**
- * Dummy function wrapper around logEvent system logging
- *
- * @param string $data
- *
- * @return void
- */
- function log_register($data) {
- global $system;
- $system->logEvent($data);
- }
- }
- if (!function_exists('cfr')) {
- /**
- * Checks is some right available for current user
- *
- * @global object $system
- * @param string $right
- *
- * @return bool
- */
- function cfr($right) {
- global $system;
- return ($system->checkForRight($right));
- }
- }
- if (!function_exists('whoami')) {
- /**
- * Returns current user login
- *
- * @global object $system
- *
- * @return string
- */
- function whoami() {
- global $system;
- return ($system->getLoggedInUsername());
- }
- }
- /**
- * Dummy rcms localisation function
- *
- * @param string $str
- *
- * @return string
- */
- function rcms_date_localise($str) {
- global $lang;
- if (isset($lang['datetime'][$str])) {
- $str = $lang['datetime'][$str];
- }
- return ($str);
- }
- /**
- * Returns current locale as two-letters code extracted form YalfCore
- *
- * @return string
- */
- function curlang() {
- global $system;
- $locale = $system->getCurLang();
- return ($locale);
- }
- if (!function_exists('curdatetime')) {
- /**
- * Returns current date and time in mysql DATETIME view
- *
- * @return string
- */
- function curdatetime() {
- $currenttime = date("Y-m-d H:i:s");
- return ($currenttime);
- }
- }
- if (!function_exists('rcms_redirect')) {
- /**
- * Shows redirection javascript.
- *
- * @param string $url
- * @param bool $header
- */
- function rcms_redirect($url, $header = false) {
- if ($header) {
- @header('Location: ' . $url);
- } else {
- echo '<script language="javascript">document.location.href="' . $url . '";</script>';
- }
- }
- }
- if (!function_exists('ispos')) {
- /**
- * Checks for substring in string
- *
- * @param string $string
- * @param string $search
- * @return bool
- */
- function ispos($string, $search) {
- if (strpos($string, $search) === false) {
- return (false);
- } else {
- return (true);
- }
- }
- }
- if (!function_exists('zb_convertSize')) {
- /**
- * Converts bytes into human-readable values like Kb, Mb, Gb...
- *
- * @param int $fs
- * @param string $traffsize
- *
- * @return string
- */
- function zb_convertSize($fs, $traffsize = 'float') {
- if ($traffsize == 'float') {
- if ($fs >= (1073741824 * 1024))
- $fs = round($fs / (1073741824 * 1024) * 100) / 100 . ' ' . __('Tb');
- elseif ($fs >= 1073741824)
- $fs = round($fs / 1073741824 * 100) / 100 . ' ' . __('Gb');
- elseif ($fs >= 1048576)
- $fs = round($fs / 1048576 * 100) / 100 . ' ' . __('Mb');
- elseif ($fs >= 1024)
- $fs = round($fs / 1024 * 100) / 100 . ' ' . __('Kb');
- else
- $fs = $fs . ' ' . __('b');
- return ($fs);
- }
- if ($traffsize == 'b') {
- return ($fs);
- }
- if ($traffsize == 'Kb') {
- $fs = round($fs / 1024 * 100) / 100 . ' ' . __('Kb');
- return ($fs);
- }
- if ($traffsize == 'Mb') {
- $fs = round($fs / 1048576 * 100) / 100 . ' ' . __('Mb');
- return ($fs);
- }
- if ($traffsize == 'Gb') {
- $fs = round($fs / 1073741824 * 100) / 100 . ' ' . __('Gb');
- return ($fs);
- }
- if ($traffsize == 'Tb') {
- $fs = round($fs / (1073741824 * 1024) * 100) / 100 . ' ' . __('Tb');
- return ($fs);
- }
- }
- }
- if (!function_exists('zb_TraffToGb')) {
- /**
- * Convert bytes to human-readable Gb values. Much faster than stg_convert_size()/zb_convertSize()
- *
- * @param int $fs
- *
- * @return string
- */
- function zb_TraffToGb($fs) {
- $fs = round($fs / 1073741824, 2) . ' Gb';
- return ($fs);
- }
- }
- /**
- * Advanced php5 scandir analog wit some filters
- *
- * @param string $directory Directory to scan
- * @param string $exp Filter expression - like *.ini or *.dat
- * @param string $type Filter type - all or dir
- * @param bool $do_not_filter
- *
- * @return array
- */
- function rcms_scandir($directory, $exp = '', $type = 'all', $do_not_filter = false) {
- $dir = $ndir = array();
- if (!empty($exp)) {
- $exp = '/^' . str_replace('*', '(.*)', str_replace('.', '\\.', $exp)) . '$/';
- }
- if (!empty($type) && $type !== 'all') {
- $func = 'is_' . $type;
- }
- if (is_dir($directory)) {
- $fh = opendir($directory);
- while (false !== ($filename = readdir($fh))) {
- if (substr($filename, 0, 1) != '.' || $do_not_filter) {
- if ((empty($type) || $type == 'all' || $func($directory . '/' . $filename)) && (empty($exp) || preg_match($exp, $filename))) {
- $dir[] = $filename;
- }
- }
- }
- closedir($fh);
- natsort($dir);
- }
- return $dir;
- }
- /**
- * Parses standard INI-file structure and returns this as key=>value array
- *
- * @param string $filename Existing file name
- * @param bool $blocks Section parsing flag
- *
- * @return array
- */
- function rcms_parse_ini_file($filename, $blocks = false) {
- $array1 = file($filename);
- $section = '';
- foreach ($array1 as $filedata) {
- $dataline = trim($filedata);
- $firstchar = substr($dataline, 0, 1);
- if ($firstchar != ';' && !empty($dataline)) {
- if ($blocks && $firstchar == '[' && substr($dataline, -1, 1) == ']') {
- $section = strtolower(substr($dataline, 1, -1));
- } else {
- $delimiter = strpos($dataline, '=');
- if ($delimiter > 0) {
- preg_match("/^[\s]*(.*?)[\s]*[=][\s]*(\"|)(.*?)(\"|)[\s]*$/", $dataline, $matches);
- $key = $matches[1];
- $value = $matches[3];
- if ($blocks) {
- if (!empty($section)) {
- $array2[$section][$key] = stripcslashes($value);
- }
- } else {
- $array2[$key] = stripcslashes($value);
- }
- } else {
- if ($blocks) {
- if (!empty($section)) {
- $array2[$section][trim($dataline)] = '';
- }
- } else {
- $array2[trim($dataline)] = '';
- }
- }
- }
- }
- }
- return (!empty($array2)) ? $array2 : false;
- }
- if (!function_exists('vf')) {
- /**
- * Returns cutted down data entry
- * Available modes:
- * 1 - digits, letters
- * 2 - only letters
- * 3 - only digits
- * 4 - digits, letters, "-", "_", "."
- * 5 - current lang alphabet + digits + punctuation
- * default - filter only blacklist chars
- *
- * @param string $data
- * @param int $mode
- *
- * @return string
- */
- function vf($data, $mode = 0) {
- switch ($mode) {
- case 1:
- return preg_replace("#[^a-z0-9A-Z]#Uis", '', $data); // digits, letters
- break;
- case 2:
- return preg_replace("#[^a-zA-Z]#Uis", '', $data); // letters
- break;
- case 3:
- return preg_replace("#[^0-9]#Uis", '', $data); // digits
- break;
- case 4:
- return preg_replace("#[^a-z0-9A-Z\-_\.]#Uis", '', $data); // digits, letters, "-", "_", "."
- break;
- case 5:
- return preg_replace("#[^ [:punct:]" . ('a-zA-Z') . "0-9]#Uis", '', $data); // current lang alphabet + digits + punctuation
- break;
- default:
- return preg_replace("#[~@\+\?\%\/\;=\*\>\<\"\'\-]#Uis", '', $data); // black list anyway
- break;
- }
- }
- }
- /**
- * Fast debug text data output
- *
- * @param string $data
- */
- function deb($data) {
- show_window('DEBUG', $data);
- }
- /**
- * Fast debug output of array
- *
- * @param string $data
- */
- function debarr($data) {
- $result = print_r($data, true);
- $result = '<pre>' . $result . '</pre>';
- show_window('DEBUG', $result);
- }
- /**
- * Returns current date and time in mysql DATETIME view
- *
- * @return string
- */
- function curdatetime() {
- $currenttime = date("Y-m-d H:i:s");
- return ($currenttime);
- }
- /**
- * returns current time in mysql DATETIME view
- *
- * @return string
- */
- function curtime() {
- $currenttime = date("H:i:s");
- return ($currenttime);
- }
- /**
- * Returns current date in mysql DATETIME view
- *
- * @return string
- */
- function curdate() {
- $currentdate = date("Y-m-d");
- return ($currentdate);
- }
- /**
- * Returns current year-month in mysql DATETIME view
- *
- * @return string
- */
- function curmonth() {
- $currentmonth = date("Y-m");
- return ($currentmonth);
- }
- /**
- * Returns previous year-month in mysql DATETIME view
- *
- * @return string
- */
- function prevmonth() {
- $result = date("Y-m", strtotime("-1 months"));
- return ($result);
- }
- /**
- * Returns current year as just Y
- *
- * @return string
- */
- function curyear() {
- $currentyear = date("Y");
- return ($currentyear);
- }
- /**
- * Returns all months with names in two digit notation
- *
- * @param string $number
- * @return array/string
- */
- function months_array($number = null) {
- $months = array(
- '01' => 'January',
- '02' => 'February',
- '03' => 'March',
- '04' => 'April',
- '05' => 'May',
- '06' => 'June',
- '07' => 'July',
- '08' => 'August',
- '09' => 'September',
- '10' => 'October',
- '11' => 'November',
- '12' => 'December'
- );
- if (empty($number)) {
- return $months;
- } else {
- return $months[$number];
- }
- }
- /**
- * Retuns all months with names without begin zeros
- *
- * @return array
- */
- function months_array_wz() {
- $months = array(
- '1' => 'January',
- '2' => 'February',
- '3' => 'March',
- '4' => 'April',
- '5' => 'May',
- '6' => 'June',
- '7' => 'July',
- '8' => 'August',
- '9' => 'September',
- '10' => 'October',
- '11' => 'November',
- '12' => 'December'
- );
- return ($months);
- }
- /**
- * Allocates array with full timeline as hh:mm=>0
- *
- * @return array
- */
- function allocDayTimeline() {
- $result = array();
- for ($h = 0; $h <= 23; $h++) {
- for ($m = 0; $m < 60; $m++) {
- $hLabel = ($h > 9) ? $h : '0' . $h;
- $mLabel = ($m > 9) ? $m : '0' . $m;
- $timeLabel = $hLabel . ':' . $mLabel;
- $result[$timeLabel] = 0;
- }
- }
- return ($result);
- }
- /**
- * Returns visual bar with count/total proportional size
- *
- * @param float $count
- * @param float $total
- * @return string
- */
- function web_bar($count, $total) {
- $barurl = 'skins/bar.png';
- if ($total != 0) {
- $width = ($count / $total) * 100;
- } else {
- $width = 0;
- }
- $code = wf_img_sized($barurl, '', $width . '%', '14');
- return ($code);
- }
- /**
- * Calculates percent value
- *
- * @param float $sum
- * @param float $percent
- *
- * @return float
- */
- function zb_Percent($sum, $percent) {
- $result = $percent / 100 * $sum;
- return ($result);
- }
- /**
- * Counts percentage between two values
- *
- * @param float $valueTotal
- * @param float $value
- *
- * @return float
- */
- function zb_PercentValue($valueTotal, $value) {
- $result = 0;
- if ($valueTotal != 0) {
- $result = round((($value * 100) / $valueTotal), 2);
- }
- return ($result);
- }
- /**
- * UTF8-safe translit function
- *
- * @param $string string to be transliterated
- * @param $bool Save case state
- *
- * @return string
- */
- function zb_TranslitString($string, $caseSensetive = false) {
- if ($caseSensetive) {
- $replace = array(
- "'" => "",
- "`" => "",
- "а" => "a",
- "А" => "A",
- "б" => "b",
- "Б" => "B",
- "в" => "v",
- "В" => "V",
- "г" => "g",
- "Г" => "G",
- "д" => "d",
- "Д" => "D",
- "е" => "e",
- "Е" => "E",
- "ё" => "e",
- "Ё" => "E",
- "ж" => "zh",
- "Ж" => "Zh",
- "з" => "z",
- "З" => "Z",
- "и" => "i",
- "И" => "I",
- "й" => "y",
- "Й" => "Y",
- "к" => "k",
- "К" => "K",
- "л" => "l",
- "Л" => "L",
- "м" => "m",
- "М" => "M",
- "н" => "n",
- "Н" => "N",
- "о" => "o",
- "О" => "O",
- "п" => "p",
- "П" => "P",
- "р" => "r",
- "Р" => "R",
- "с" => "s",
- "С" => "S",
- "т" => "t",
- "Т" => "T",
- "у" => "u",
- "У" => "U",
- "ф" => "f",
- "Ф" => "F",
- "х" => "h",
- "Х" => "H",
- "ц" => "c",
- "Ц" => "C",
- "ч" => "ch",
- "Ч" => "Ch",
- "ш" => "sh",
- "Ш" => "Sh",
- "щ" => "sch",
- "Щ" => "Sch",
- "ъ" => "",
- "Ъ" => "",
- "ы" => "y",
- "Ы" => "Y",
- "ь" => "",
- "Ь" => "",
- "э" => "e",
- "Э" => "E",
- "ю" => "yu",
- "Ю" => "Yu",
- "я" => "ya",
- "Я" => "Ya",
- "і" => "i",
- "І" => "I",
- "ї" => "yi",
- "Ї" => "Yi",
- "є" => "e",
- "Є" => "E",
- "ґ" => "g",
- "Ґ" => "G"
- );
- } else {
- $replace = array(
- "'" => "",
- "`" => "",
- "а" => "a",
- "А" => "a",
- "б" => "b",
- "Б" => "b",
- "в" => "v",
- "В" => "v",
- "г" => "g",
- "Г" => "g",
- "д" => "d",
- "Д" => "d",
- "е" => "e",
- "Е" => "e",
- "ё" => "e",
- "Ё" => "e",
- "ж" => "zh",
- "Ж" => "zh",
- "з" => "z",
- "З" => "z",
- "и" => "i",
- "И" => "i",
- "й" => "y",
- "Й" => "y",
- "к" => "k",
- "К" => "k",
- "л" => "l",
- "Л" => "l",
- "м" => "m",
- "М" => "m",
- "н" => "n",
- "Н" => "n",
- "о" => "o",
- "О" => "o",
- "п" => "p",
- "П" => "p",
- "р" => "r",
- "Р" => "r",
- "с" => "s",
- "С" => "s",
- "т" => "t",
- "Т" => "t",
- "у" => "u",
- "У" => "u",
- "ф" => "f",
- "Ф" => "f",
- "х" => "h",
- "Х" => "h",
- "ц" => "c",
- "Ц" => "c",
- "ч" => "ch",
- "Ч" => "ch",
- "ш" => "sh",
- "Ш" => "sh",
- "щ" => "sch",
- "Щ" => "sch",
- "ъ" => "",
- "Ъ" => "",
- "ы" => "y",
- "Ы" => "y",
- "ь" => "",
- "Ь" => "",
- "э" => "e",
- "Э" => "e",
- "ю" => "yu",
- "Ю" => "yu",
- "я" => "ya",
- "Я" => "ya",
- "і" => "i",
- "І" => "i",
- "ї" => "yi",
- "Ї" => "yi",
- "є" => "e",
- "Є" => "e",
- "ґ" => "g",
- "Ґ" => "g"
- );
- }
- return $str = iconv("UTF-8", "UTF-8//IGNORE", strtr($string, $replace));
- }
- /**
- * Returns random alpha-numeric string of some lenght
- *
- * @param int $size
- * @return string
- */
- function zb_rand_string($size = 4) {
- $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
- $string = "";
- for ($p = 0; $p < $size; $p++) {
- $string .= $characters[mt_rand(0, (strlen($characters) - 1))];
- }
- return ($string);
- }
- /**
- * Converts CIDR mask into decimal like 24 => 255.255.255.0
- *
- * @param int $mask_bits
- *
- * @return string
- */
- function multinet_cidr2mask($mask_bits) {
- if ($mask_bits > 31 || $mask_bits < 0)
- return ("0.0.0.0");
- $host_bits = 32 - $mask_bits;
- $num_hosts = pow(2, $host_bits) - 1;
- $netmask = ip2int("255.255.255.255") - $num_hosts;
- return int2ip($netmask);
- }
- /**
- * Converts IP to integer value
- *
- * @param string $src
- *
- * @return int
- */
- function ip2int($src) {
- $t = explode('.', $src);
- return count($t) != 4 ? 0 : 256 * (256 * ((float) $t[0] * 256 + (float) $t[1]) + (float) $t[2]) + (float) $t[3];
- }
- /**
- * Converts integer into IP
- *
- * @param int $src
- *
- * @return string
- */
- function int2ip($src) {
- $s1 = (int) ($src / 256);
- $i1 = $src - 256 * $s1;
- $src = (int) ($s1 / 256);
- $i2 = $s1 - 256 * $src;
- $s1 = (int) ($src / 256);
- return sprintf('%d.%d.%d.%d', $s1, $src - 256 * $s1, $i2, $i1);
- }
- /**
- * Returns exploded array of some multi-lined strings
- *
- * @param string $data
- *
- * @return array
- */
- function explodeRows($data) {
- $result = explode("\n", $data);
- return ($result);
- }
- /**
- * Initializes file download procedure
- *
- * @param string $filePath
- * @param string $contentType
- * @throws Exception
- */
- function zb_DownloadFile($filePath, $contentType = '') {
- if (!empty($filePath)) {
- if (file_exists($filePath)) {
- log_register("DOWNLOAD FILE `" . $filePath . "`");
- if (($contentType == '') or ($contentType == 'default')) {
- $contentType = 'application/octet-stream';
- } else {
- //additional content types
- if ($contentType == 'docx') {
- $contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
- }
- if ($contentType == 'csv') {
- $contentType = 'text/csv; charset=Windows-1251';
- }
- if ($contentType == 'text') {
- $contentType = 'text/plain;';
- }
- if ($contentType == 'jpg') {
- $contentType = 'Content-Type: image/jpeg';
- }
- }
- header('Content-Type: ' . $contentType);
- header("Content-Transfer-Encoding: Binary");
- header("Content-disposition: attachment; filename=\"" . basename($filePath) . "\"");
- header("Content-Description: File Transfer");
- header("Content-Length: " . filesize($filePath));
- flush(); // this doesn't really matter.
- $fp = fopen($filePath, "r");
- while (!feof($fp)) {
- echo fread($fp, 65536);
- flush(); // this is essential for large downloads
- }
- fclose($fp);
- die();
- } else {
- throw new Exception('DOWNLOAD_FILEPATH_NOT_EXISTS');
- }
- } else {
- throw new Exception('DOWNLOAD_FILEPATH_EMPTY');
- }
- }
- /**
- * Returns data that contained between two string tags
- *
- * @param string $openTag - open tag string. Examples: "(", "[", "{", "[sometag]"
- * @param string $closeTag - close tag string. Examples: ")", "]", "}", "[/sometag]"
- * @param string $stringToParse - just string that contains some data to parse
- * @param bool $mutipleResults - extract just first result as string or all matches as array like match=>match
- *
- * @return string/array
- */
- function zb_ParseTagData($openTag, $closeTag, $stringToParse = '', $mutipleResults = false) {
- $result = '';
- if (!empty($openTag) and !empty($closeTag) and !empty($stringToParse)) {
- $replacements = array(
- '(' => '\(',
- ')' => '\)',
- '[' => '\[',
- ']' => '\]',
- );
- foreach ($replacements as $eachReplaceTag => $eachReplace) {
- $openTag = str_replace($eachReplaceTag, $eachReplace, $openTag);
- $closeTag = str_replace($eachReplaceTag, $eachReplace, $closeTag);
- }
- $pattern = '!' . $openTag . '(.*?)' . $closeTag . '!si';
- if ($mutipleResults) {
- $result = array();
- if (preg_match_all($pattern, $stringToParse, $matches)) {
- if (isset($matches[1])) {
- if (!empty($matches[1])) {
- foreach ($matches[1] as $io => $each) {
- $result[$each] = $each;
- }
- }
- }
- }
- } else {
- if (preg_match($pattern, $stringToParse, $matches)) {
- if (isset($matches[1])) {
- $result = $matches[1];
- }
- }
- }
- }
- return ($result);
- }
- /**
- * Renders time duration in seconds into formatted human-readable view
- *
- * @param int $seconds
- *
- * @return string
- */
- function zb_formatTime($seconds) {
- $init = $seconds;
- $days = floor($seconds / 86400);
- $hours = floor(round($seconds / 3600));
- $minutes = floor(round(($seconds / 60)) % 60);
- $seconds = (round($seconds) % 60);
- if ($init < 3600) {
- //less than 1 hour
- if ($init < 60) {
- //less than minute
- $result = $seconds . ' ' . __('sec.');
- } else {
- //more than one minute
- $result = $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
- }
- } else {
- if ($init < 86400) {
- //more than hour
- $result = $hours . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
- } else {
- $hoursLeft = $hours - ($days * 24);
- $result = $days . ' ' . __('days') . ' ' . $hoursLeft . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
- }
- }
- return ($result);
- }
- /**
- * Renders time duration in seconds into formatted human-readable view without seconds
- *
- * @param int $seconds
- *
- * @return string
- */
- function wr_formatTimeArchive($seconds) {
- $init = $seconds;
- $days = floor($seconds / 86400);
- $hours = floor($seconds / 3600);
- $minutes = floor(($seconds / 60) % 60);
- $seconds = $seconds % 60;
- if ($init < 3600) {
- //less than 1 hour
- if ($init < 60) {
- //less than minute
- $result = $seconds . ' ' . __('sec.');
- } else {
- //more than one minute
- $result = $minutes . ' ' . __('minutes');
- }
- } else {
- if ($init < 86400) {
- //more than hour
- $result = $hours . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes');
- } else {
- $hoursLeft = $hours - ($days * 24);
- $result = $days . ' ' . __('days') . ' ' . $hoursLeft . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes');
- }
- }
- return ($result);
- }
- /**
- * Validate a Gregorian date
- *
- * @param string $date Date in MySQL format
- * @return bool
- */
- function zb_checkDate($date) {
- $explode = explode('-', $date);
- @$year = $explode[0];
- @$month = $explode[1];
- @$day = $explode[2];
- $result = @checkdate($month, $day, $year);
- return ($result);
- }
- /**
- * Checks is time between some other time ranges?
- *
- * @param string $fromTime start time (format hh:mm OR hh:mm:ss with seconds)
- * @param string $toTime end time
- * @param string $checkTime time to check
- * @param bool $seconds
- *
- * @return bool
- */
- function zb_isTimeBetween($fromTime, $toTime, $checkTime, $seconds = false) {
- if ($seconds) {
- $formatPostfix = ':s';
- } else {
- $formatPostfix = '';
- }
- $checkTime = strtotime($checkTime);
- $checkTime = date("H:i" . $formatPostfix, $checkTime);
- $f = DateTime::createFromFormat('!H:i' . $formatPostfix, $fromTime);
- $t = DateTime::createFromFormat('!H:i' . $formatPostfix, $toTime);
- $i = DateTime::createFromFormat('!H:i' . $formatPostfix, $checkTime);
- if ($f > $t) {
- $t->modify('+1 day');
- }
- return ($f <= $i && $i <= $t) || ($f <= $i->modify('+1 day') && $i <= $t);
- }
- /**
- * Checks is date between some other date ranges?
- *
- * @param string $fromDate start date (format Y-m-d)
- * @param string $toDate end date
- * @param string $checkDate date to check
- * @param bool $seconds
- *
- * @return bool
- */
- function zb_isDateBetween($fromDate, $toDate, $checkDate) {
- $result = false;
- $fromDate = strtotime($fromDate);
- $toDate = strtotime($toDate);
- $checkDate = strtotime($checkDate);
- $checkDate = date("Y-m-d", $checkDate);
- $checkDate = strtotime($checkDate);
- if ($checkDate >= $fromDate and $checkDate <= $toDate) {
- $result = true;
- }
- return ($result);
- }
- /**
- * Checks is timestamp between some other time ranges?
- *
- * @param int $fromTime start time
- * @param int $toTime end time
- * @param int $checkTime time to check
- *
- * @return bool
- */
- function zb_isTimeStampBetween($fromTime, $toTime, $checkTime) {
- $result = false;
- if ($checkTime >= $fromTime and $checkTime <= $toTime) {
- $result = true;
- }
- return ($result);
- }
- if (!function_exists('json_validate')) {
- /**
- * Validates a JSON string. PHP <8.3 replacement.
- *
- * @param string $json The JSON string to validate.
- * @param int $depth Maximum depth. Must be greater than zero.
- * @param int $flags Bitmask of JSON decode options.
- * @return bool Returns true if the string is a valid JSON, otherwise false.
- */
- function json_validate($json, $depth = 512, $flags = 0) {
- if (!is_string($json)) {
- return false;
- }
- try {
- json_decode($json, false, $depth, $flags | JSON_THROW_ON_ERROR);
- return true;
- } catch (\JsonException $e) {
- return false;
- }
- }
- }
- /**
- * Trims the content of a log file if it exceeds a specified size.
- *
- * This function checks if the specified log file exists and if its size exceeds
- * the given maximum size. If the file size exceeds the limit, it trims the file
- * content by removing lines from the beginning until the file size is within the limit.
- *
- * @param string $fileName The path to the text log file.
- * @param int $size The maximum allowed size of the log file in megabytes (MB).
- *
- * @return void
- */
- function zb_TrimTextLog($fileName, $size) {
- if (file_exists($fileName)) {
- $maxSize = $size * 1024 * 1024; // in mb
- $fileSize = filesize($fileName);
- if ($fileSize >= $maxSize) {
- $fileContent = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
- if (!empty($fileContent)) {
- $fileContent = array_reverse($fileContent);
- $seekSize = 0;
- foreach ($fileContent as $io => $each) {
- $seekSize += strlen($each);
- if ($seekSize >= $maxSize) {
- unset($fileContent[$io]);
- }
- }
- $fileContent = array_reverse($fileContent);
- file_put_contents($fileName, implode(PHP_EOL, $fileContent).PHP_EOL);
- }
- }
- }
- }
|