api.compat.php 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. <?php
  2. /**
  3. * Some legacy workaround here
  4. */
  5. if (!function_exists('__')) {
  6. /**
  7. * Dummy i18n function
  8. *
  9. * @param string $str
  10. * @return string
  11. */
  12. function __($str) {
  13. global $lang;
  14. if (isset($lang['def'][$str])) {
  15. if (!empty($lang['def'][$str])) {
  16. $str = $lang['def'][$str];
  17. }
  18. }
  19. return ($str);
  20. }
  21. }
  22. if (!function_exists('log_register')) {
  23. /**
  24. * Dummy function wrapper around logEvent system logging
  25. *
  26. * @param string $data
  27. *
  28. * @return void
  29. */
  30. function log_register($data) {
  31. global $system;
  32. $system->logEvent($data);
  33. }
  34. }
  35. if (!function_exists('cfr')) {
  36. /**
  37. * Checks is some right available for current user
  38. *
  39. * @global object $system
  40. * @param string $right
  41. *
  42. * @return bool
  43. */
  44. function cfr($right) {
  45. global $system;
  46. return ($system->checkForRight($right));
  47. }
  48. }
  49. if (!function_exists('whoami')) {
  50. /**
  51. * Returns current user login
  52. *
  53. * @global object $system
  54. *
  55. * @return string
  56. */
  57. function whoami() {
  58. global $system;
  59. return ($system->getLoggedInUsername());
  60. }
  61. }
  62. /**
  63. * Dummy rcms localisation function
  64. *
  65. * @param string $str
  66. *
  67. * @return string
  68. */
  69. function rcms_date_localise($str) {
  70. global $lang;
  71. if (isset($lang['datetime'][$str])) {
  72. $str = $lang['datetime'][$str];
  73. }
  74. return ($str);
  75. }
  76. /**
  77. * Returns current locale as two-letters code extracted form YalfCore
  78. *
  79. * @return string
  80. */
  81. function curlang() {
  82. global $system;
  83. $locale = $system->getCurLang();
  84. return ($locale);
  85. }
  86. if (!function_exists('curdatetime')) {
  87. /**
  88. * Returns current date and time in mysql DATETIME view
  89. *
  90. * @return string
  91. */
  92. function curdatetime() {
  93. $currenttime = date("Y-m-d H:i:s");
  94. return ($currenttime);
  95. }
  96. }
  97. if (!function_exists('rcms_redirect')) {
  98. /**
  99. * Shows redirection javascript.
  100. *
  101. * @param string $url
  102. * @param bool $header
  103. */
  104. function rcms_redirect($url, $header = false) {
  105. if ($header) {
  106. @header('Location: ' . $url);
  107. } else {
  108. echo '<script language="javascript">document.location.href="' . $url . '";</script>';
  109. }
  110. }
  111. }
  112. if (!function_exists('ispos')) {
  113. /**
  114. * Checks for substring in string
  115. *
  116. * @param string $string
  117. * @param string $search
  118. * @return bool
  119. */
  120. function ispos($string, $search) {
  121. if (strpos($string, $search) === false) {
  122. return (false);
  123. } else {
  124. return (true);
  125. }
  126. }
  127. }
  128. if (!function_exists('zb_convertSize')) {
  129. /**
  130. * Converts bytes into human-readable values like Kb, Mb, Gb...
  131. *
  132. * @param int $fs
  133. * @param string $traffsize
  134. *
  135. * @return string
  136. */
  137. function zb_convertSize($fs, $traffsize = 'float') {
  138. if ($traffsize == 'float') {
  139. if ($fs >= (1073741824 * 1024))
  140. $fs = round($fs / (1073741824 * 1024) * 100) / 100 . ' ' . __('Tb');
  141. elseif ($fs >= 1073741824)
  142. $fs = round($fs / 1073741824 * 100) / 100 . ' ' . __('Gb');
  143. elseif ($fs >= 1048576)
  144. $fs = round($fs / 1048576 * 100) / 100 . ' ' . __('Mb');
  145. elseif ($fs >= 1024)
  146. $fs = round($fs / 1024 * 100) / 100 . ' ' . __('Kb');
  147. else
  148. $fs = $fs . ' ' . __('b');
  149. return ($fs);
  150. }
  151. if ($traffsize == 'b') {
  152. return ($fs);
  153. }
  154. if ($traffsize == 'Kb') {
  155. $fs = round($fs / 1024 * 100) / 100 . ' ' . __('Kb');
  156. return ($fs);
  157. }
  158. if ($traffsize == 'Mb') {
  159. $fs = round($fs / 1048576 * 100) / 100 . ' ' . __('Mb');
  160. return ($fs);
  161. }
  162. if ($traffsize == 'Gb') {
  163. $fs = round($fs / 1073741824 * 100) / 100 . ' ' . __('Gb');
  164. return ($fs);
  165. }
  166. if ($traffsize == 'Tb') {
  167. $fs = round($fs / (1073741824 * 1024) * 100) / 100 . ' ' . __('Tb');
  168. return ($fs);
  169. }
  170. }
  171. }
  172. if (!function_exists('zb_TraffToGb')) {
  173. /**
  174. * Convert bytes to human-readable Gb values. Much faster than stg_convert_size()/zb_convertSize()
  175. *
  176. * @param int $fs
  177. *
  178. * @return string
  179. */
  180. function zb_TraffToGb($fs) {
  181. $fs = round($fs / 1073741824, 2) . ' Gb';
  182. return ($fs);
  183. }
  184. }
  185. /**
  186. * Advanced php5 scandir analog wit some filters
  187. *
  188. * @param string $directory Directory to scan
  189. * @param string $exp Filter expression - like *.ini or *.dat
  190. * @param string $type Filter type - all or dir
  191. * @param bool $do_not_filter
  192. *
  193. * @return array
  194. */
  195. function rcms_scandir($directory, $exp = '', $type = 'all', $do_not_filter = false) {
  196. $dir = $ndir = array();
  197. if (!empty($exp)) {
  198. $exp = '/^' . str_replace('*', '(.*)', str_replace('.', '\\.', $exp)) . '$/';
  199. }
  200. if (!empty($type) && $type !== 'all') {
  201. $func = 'is_' . $type;
  202. }
  203. if (is_dir($directory)) {
  204. $fh = opendir($directory);
  205. while (false !== ($filename = readdir($fh))) {
  206. if (substr($filename, 0, 1) != '.' || $do_not_filter) {
  207. if ((empty($type) || $type == 'all' || $func($directory . '/' . $filename)) && (empty($exp) || preg_match($exp, $filename))) {
  208. $dir[] = $filename;
  209. }
  210. }
  211. }
  212. closedir($fh);
  213. natsort($dir);
  214. }
  215. return $dir;
  216. }
  217. /**
  218. * Parses standard INI-file structure and returns this as key=>value array
  219. *
  220. * @param string $filename Existing file name
  221. * @param bool $blocks Section parsing flag
  222. *
  223. * @return array
  224. */
  225. function rcms_parse_ini_file($filename, $blocks = false) {
  226. $array1 = file($filename);
  227. $section = '';
  228. foreach ($array1 as $filedata) {
  229. $dataline = trim($filedata);
  230. $firstchar = substr($dataline, 0, 1);
  231. if ($firstchar != ';' && !empty($dataline)) {
  232. if ($blocks && $firstchar == '[' && substr($dataline, -1, 1) == ']') {
  233. $section = strtolower(substr($dataline, 1, -1));
  234. } else {
  235. $delimiter = strpos($dataline, '=');
  236. if ($delimiter > 0) {
  237. preg_match("/^[\s]*(.*?)[\s]*[=][\s]*(\"|)(.*?)(\"|)[\s]*$/", $dataline, $matches);
  238. $key = $matches[1];
  239. $value = $matches[3];
  240. if ($blocks) {
  241. if (!empty($section)) {
  242. $array2[$section][$key] = stripcslashes($value);
  243. }
  244. } else {
  245. $array2[$key] = stripcslashes($value);
  246. }
  247. } else {
  248. if ($blocks) {
  249. if (!empty($section)) {
  250. $array2[$section][trim($dataline)] = '';
  251. }
  252. } else {
  253. $array2[trim($dataline)] = '';
  254. }
  255. }
  256. }
  257. }
  258. }
  259. return (!empty($array2)) ? $array2 : false;
  260. }
  261. if (!function_exists('vf')) {
  262. /**
  263. * Returns cutted down data entry
  264. * Available modes:
  265. * 1 - digits, letters
  266. * 2 - only letters
  267. * 3 - only digits
  268. * 4 - digits, letters, "-", "_", "."
  269. * 5 - current lang alphabet + digits + punctuation
  270. * default - filter only blacklist chars
  271. *
  272. * @param string $data
  273. * @param int $mode
  274. *
  275. * @return string
  276. */
  277. function vf($data, $mode = 0) {
  278. switch ($mode) {
  279. case 1:
  280. return preg_replace("#[^a-z0-9A-Z]#Uis", '', $data); // digits, letters
  281. break;
  282. case 2:
  283. return preg_replace("#[^a-zA-Z]#Uis", '', $data); // letters
  284. break;
  285. case 3:
  286. return preg_replace("#[^0-9]#Uis", '', $data); // digits
  287. break;
  288. case 4:
  289. return preg_replace("#[^a-z0-9A-Z\-_\.]#Uis", '', $data); // digits, letters, "-", "_", "."
  290. break;
  291. case 5:
  292. return preg_replace("#[^ [:punct:]" . ('a-zA-Z') . "0-9]#Uis", '', $data); // current lang alphabet + digits + punctuation
  293. break;
  294. default:
  295. return preg_replace("#[~@\+\?\%\/\;=\*\>\<\"\'\-]#Uis", '', $data); // black list anyway
  296. break;
  297. }
  298. }
  299. }
  300. /**
  301. * Fast debug text data output
  302. *
  303. * @param string $data
  304. */
  305. function deb($data) {
  306. show_window('DEBUG', $data);
  307. }
  308. /**
  309. * Fast debug output of array
  310. *
  311. * @param string $data
  312. */
  313. function debarr($data) {
  314. $result = print_r($data, true);
  315. $result = '<pre>' . $result . '</pre>';
  316. show_window('DEBUG', $result);
  317. }
  318. /**
  319. * Returns current date and time in mysql DATETIME view
  320. *
  321. * @return string
  322. */
  323. function curdatetime() {
  324. $currenttime = date("Y-m-d H:i:s");
  325. return ($currenttime);
  326. }
  327. /**
  328. * returns current time in mysql DATETIME view
  329. *
  330. * @return string
  331. */
  332. function curtime() {
  333. $currenttime = date("H:i:s");
  334. return ($currenttime);
  335. }
  336. /**
  337. * Returns current date in mysql DATETIME view
  338. *
  339. * @return string
  340. */
  341. function curdate() {
  342. $currentdate = date("Y-m-d");
  343. return ($currentdate);
  344. }
  345. /**
  346. * Returns current year-month in mysql DATETIME view
  347. *
  348. * @return string
  349. */
  350. function curmonth() {
  351. $currentmonth = date("Y-m");
  352. return ($currentmonth);
  353. }
  354. /**
  355. * Returns previous year-month in mysql DATETIME view
  356. *
  357. * @return string
  358. */
  359. function prevmonth() {
  360. $result = date("Y-m", strtotime("-1 months"));
  361. return ($result);
  362. }
  363. /**
  364. * Returns current year as just Y
  365. *
  366. * @return string
  367. */
  368. function curyear() {
  369. $currentyear = date("Y");
  370. return ($currentyear);
  371. }
  372. /**
  373. * Returns all months with names in two digit notation
  374. *
  375. * @param string $number
  376. * @return array/string
  377. */
  378. function months_array($number = null) {
  379. $months = array(
  380. '01' => 'January',
  381. '02' => 'February',
  382. '03' => 'March',
  383. '04' => 'April',
  384. '05' => 'May',
  385. '06' => 'June',
  386. '07' => 'July',
  387. '08' => 'August',
  388. '09' => 'September',
  389. '10' => 'October',
  390. '11' => 'November',
  391. '12' => 'December'
  392. );
  393. if (empty($number)) {
  394. return $months;
  395. } else {
  396. return $months[$number];
  397. }
  398. }
  399. /**
  400. * Retuns all months with names without begin zeros
  401. *
  402. * @return array
  403. */
  404. function months_array_wz() {
  405. $months = array(
  406. '1' => 'January',
  407. '2' => 'February',
  408. '3' => 'March',
  409. '4' => 'April',
  410. '5' => 'May',
  411. '6' => 'June',
  412. '7' => 'July',
  413. '8' => 'August',
  414. '9' => 'September',
  415. '10' => 'October',
  416. '11' => 'November',
  417. '12' => 'December'
  418. );
  419. return ($months);
  420. }
  421. /**
  422. * Allocates array with full timeline as hh:mm=>0
  423. *
  424. * @return array
  425. */
  426. function allocDayTimeline() {
  427. $result = array();
  428. for ($h = 0; $h <= 23; $h++) {
  429. for ($m = 0; $m < 60; $m++) {
  430. $hLabel = ($h > 9) ? $h : '0' . $h;
  431. $mLabel = ($m > 9) ? $m : '0' . $m;
  432. $timeLabel = $hLabel . ':' . $mLabel;
  433. $result[$timeLabel] = 0;
  434. }
  435. }
  436. return ($result);
  437. }
  438. /**
  439. * Returns visual bar with count/total proportional size
  440. *
  441. * @param float $count
  442. * @param float $total
  443. * @return string
  444. */
  445. function web_bar($count, $total) {
  446. $barurl = 'skins/bar.png';
  447. if ($total != 0) {
  448. $width = ($count / $total) * 100;
  449. } else {
  450. $width = 0;
  451. }
  452. $code = wf_img_sized($barurl, '', $width . '%', '14');
  453. return ($code);
  454. }
  455. /**
  456. * Calculates percent value
  457. *
  458. * @param float $sum
  459. * @param float $percent
  460. *
  461. * @return float
  462. */
  463. function zb_Percent($sum, $percent) {
  464. $result = $percent / 100 * $sum;
  465. return ($result);
  466. }
  467. /**
  468. * Counts percentage between two values
  469. *
  470. * @param float $valueTotal
  471. * @param float $value
  472. *
  473. * @return float
  474. */
  475. function zb_PercentValue($valueTotal, $value) {
  476. $result = 0;
  477. if ($valueTotal != 0) {
  478. $result = round((($value * 100) / $valueTotal), 2);
  479. }
  480. return ($result);
  481. }
  482. /**
  483. * UTF8-safe translit function
  484. *
  485. * @param $string string to be transliterated
  486. * @param $bool Save case state
  487. *
  488. * @return string
  489. */
  490. function zb_TranslitString($string, $caseSensetive = false) {
  491. if ($caseSensetive) {
  492. $replace = array(
  493. "'" => "",
  494. "`" => "",
  495. "а" => "a",
  496. "А" => "A",
  497. "б" => "b",
  498. "Б" => "B",
  499. "в" => "v",
  500. "В" => "V",
  501. "г" => "g",
  502. "Г" => "G",
  503. "д" => "d",
  504. "Д" => "D",
  505. "е" => "e",
  506. "Е" => "E",
  507. "ё" => "e",
  508. "Ё" => "E",
  509. "ж" => "zh",
  510. "Ж" => "Zh",
  511. "з" => "z",
  512. "З" => "Z",
  513. "и" => "i",
  514. "И" => "I",
  515. "й" => "y",
  516. "Й" => "Y",
  517. "к" => "k",
  518. "К" => "K",
  519. "л" => "l",
  520. "Л" => "L",
  521. "м" => "m",
  522. "М" => "M",
  523. "н" => "n",
  524. "Н" => "N",
  525. "о" => "o",
  526. "О" => "O",
  527. "п" => "p",
  528. "П" => "P",
  529. "р" => "r",
  530. "Р" => "R",
  531. "с" => "s",
  532. "С" => "S",
  533. "т" => "t",
  534. "Т" => "T",
  535. "у" => "u",
  536. "У" => "U",
  537. "ф" => "f",
  538. "Ф" => "F",
  539. "х" => "h",
  540. "Х" => "H",
  541. "ц" => "c",
  542. "Ц" => "C",
  543. "ч" => "ch",
  544. "Ч" => "Ch",
  545. "ш" => "sh",
  546. "Ш" => "Sh",
  547. "щ" => "sch",
  548. "Щ" => "Sch",
  549. "ъ" => "",
  550. "Ъ" => "",
  551. "ы" => "y",
  552. "Ы" => "Y",
  553. "ь" => "",
  554. "Ь" => "",
  555. "э" => "e",
  556. "Э" => "E",
  557. "ю" => "yu",
  558. "Ю" => "Yu",
  559. "я" => "ya",
  560. "Я" => "Ya",
  561. "і" => "i",
  562. "І" => "I",
  563. "ї" => "yi",
  564. "Ї" => "Yi",
  565. "є" => "e",
  566. "Є" => "E",
  567. "ґ" => "g",
  568. "Ґ" => "G"
  569. );
  570. } else {
  571. $replace = array(
  572. "'" => "",
  573. "`" => "",
  574. "а" => "a",
  575. "А" => "a",
  576. "б" => "b",
  577. "Б" => "b",
  578. "в" => "v",
  579. "В" => "v",
  580. "г" => "g",
  581. "Г" => "g",
  582. "д" => "d",
  583. "Д" => "d",
  584. "е" => "e",
  585. "Е" => "e",
  586. "ё" => "e",
  587. "Ё" => "e",
  588. "ж" => "zh",
  589. "Ж" => "zh",
  590. "з" => "z",
  591. "З" => "z",
  592. "и" => "i",
  593. "И" => "i",
  594. "й" => "y",
  595. "Й" => "y",
  596. "к" => "k",
  597. "К" => "k",
  598. "л" => "l",
  599. "Л" => "l",
  600. "м" => "m",
  601. "М" => "m",
  602. "н" => "n",
  603. "Н" => "n",
  604. "о" => "o",
  605. "О" => "o",
  606. "п" => "p",
  607. "П" => "p",
  608. "р" => "r",
  609. "Р" => "r",
  610. "с" => "s",
  611. "С" => "s",
  612. "т" => "t",
  613. "Т" => "t",
  614. "у" => "u",
  615. "У" => "u",
  616. "ф" => "f",
  617. "Ф" => "f",
  618. "х" => "h",
  619. "Х" => "h",
  620. "ц" => "c",
  621. "Ц" => "c",
  622. "ч" => "ch",
  623. "Ч" => "ch",
  624. "ш" => "sh",
  625. "Ш" => "sh",
  626. "щ" => "sch",
  627. "Щ" => "sch",
  628. "ъ" => "",
  629. "Ъ" => "",
  630. "ы" => "y",
  631. "Ы" => "y",
  632. "ь" => "",
  633. "Ь" => "",
  634. "э" => "e",
  635. "Э" => "e",
  636. "ю" => "yu",
  637. "Ю" => "yu",
  638. "я" => "ya",
  639. "Я" => "ya",
  640. "і" => "i",
  641. "І" => "i",
  642. "ї" => "yi",
  643. "Ї" => "yi",
  644. "є" => "e",
  645. "Є" => "e",
  646. "ґ" => "g",
  647. "Ґ" => "g"
  648. );
  649. }
  650. return $str = iconv("UTF-8", "UTF-8//IGNORE", strtr($string, $replace));
  651. }
  652. /**
  653. * Returns random alpha-numeric string of some lenght
  654. *
  655. * @param int $size
  656. * @return string
  657. */
  658. function zb_rand_string($size = 4) {
  659. $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
  660. $string = "";
  661. for ($p = 0; $p < $size; $p++) {
  662. $string .= $characters[mt_rand(0, (strlen($characters) - 1))];
  663. }
  664. return ($string);
  665. }
  666. /**
  667. * Converts CIDR mask into decimal like 24 => 255.255.255.0
  668. *
  669. * @param int $mask_bits
  670. *
  671. * @return string
  672. */
  673. function multinet_cidr2mask($mask_bits) {
  674. if ($mask_bits > 31 || $mask_bits < 0)
  675. return ("0.0.0.0");
  676. $host_bits = 32 - $mask_bits;
  677. $num_hosts = pow(2, $host_bits) - 1;
  678. $netmask = ip2int("255.255.255.255") - $num_hosts;
  679. return int2ip($netmask);
  680. }
  681. /**
  682. * Converts IP to integer value
  683. *
  684. * @param string $src
  685. *
  686. * @return int
  687. */
  688. function ip2int($src) {
  689. $t = explode('.', $src);
  690. return count($t) != 4 ? 0 : 256 * (256 * ((float) $t[0] * 256 + (float) $t[1]) + (float) $t[2]) + (float) $t[3];
  691. }
  692. /**
  693. * Converts integer into IP
  694. *
  695. * @param int $src
  696. *
  697. * @return string
  698. */
  699. function int2ip($src) {
  700. $s1 = (int) ($src / 256);
  701. $i1 = $src - 256 * $s1;
  702. $src = (int) ($s1 / 256);
  703. $i2 = $s1 - 256 * $src;
  704. $s1 = (int) ($src / 256);
  705. return sprintf('%d.%d.%d.%d', $s1, $src - 256 * $s1, $i2, $i1);
  706. }
  707. /**
  708. * Returns exploded array of some multi-lined strings
  709. *
  710. * @param string $data
  711. *
  712. * @return array
  713. */
  714. function explodeRows($data) {
  715. $result = explode("\n", $data);
  716. return ($result);
  717. }
  718. /**
  719. * Initializes file download procedure
  720. *
  721. * @param string $filePath
  722. * @param string $contentType
  723. * @throws Exception
  724. */
  725. function zb_DownloadFile($filePath, $contentType = '') {
  726. if (!empty($filePath)) {
  727. if (file_exists($filePath)) {
  728. log_register("DOWNLOAD FILE `" . $filePath . "`");
  729. if (($contentType == '') or ($contentType == 'default')) {
  730. $contentType = 'application/octet-stream';
  731. } else {
  732. //additional content types
  733. if ($contentType == 'docx') {
  734. $contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  735. }
  736. if ($contentType == 'csv') {
  737. $contentType = 'text/csv; charset=Windows-1251';
  738. }
  739. if ($contentType == 'text') {
  740. $contentType = 'text/plain;';
  741. }
  742. if ($contentType == 'jpg') {
  743. $contentType = 'Content-Type: image/jpeg';
  744. }
  745. }
  746. header('Content-Type: ' . $contentType);
  747. header("Content-Transfer-Encoding: Binary");
  748. header("Content-disposition: attachment; filename=\"" . basename($filePath) . "\"");
  749. header("Content-Description: File Transfer");
  750. header("Content-Length: " . filesize($filePath));
  751. flush(); // this doesn't really matter.
  752. $fp = fopen($filePath, "r");
  753. while (!feof($fp)) {
  754. echo fread($fp, 65536);
  755. flush(); // this is essential for large downloads
  756. }
  757. fclose($fp);
  758. die();
  759. } else {
  760. throw new Exception('DOWNLOAD_FILEPATH_NOT_EXISTS');
  761. }
  762. } else {
  763. throw new Exception('DOWNLOAD_FILEPATH_EMPTY');
  764. }
  765. }
  766. /**
  767. * Returns data that contained between two string tags
  768. *
  769. * @param string $openTag - open tag string. Examples: "(", "[", "{", "[sometag]"
  770. * @param string $closeTag - close tag string. Examples: ")", "]", "}", "[/sometag]"
  771. * @param string $stringToParse - just string that contains some data to parse
  772. * @param bool $mutipleResults - extract just first result as string or all matches as array like match=>match
  773. *
  774. * @return string/array
  775. */
  776. function zb_ParseTagData($openTag, $closeTag, $stringToParse = '', $mutipleResults = false) {
  777. $result = '';
  778. if (!empty($openTag) and !empty($closeTag) and !empty($stringToParse)) {
  779. $replacements = array(
  780. '(' => '\(',
  781. ')' => '\)',
  782. '[' => '\[',
  783. ']' => '\]',
  784. );
  785. foreach ($replacements as $eachReplaceTag => $eachReplace) {
  786. $openTag = str_replace($eachReplaceTag, $eachReplace, $openTag);
  787. $closeTag = str_replace($eachReplaceTag, $eachReplace, $closeTag);
  788. }
  789. $pattern = '!' . $openTag . '(.*?)' . $closeTag . '!si';
  790. if ($mutipleResults) {
  791. $result = array();
  792. if (preg_match_all($pattern, $stringToParse, $matches)) {
  793. if (isset($matches[1])) {
  794. if (!empty($matches[1])) {
  795. foreach ($matches[1] as $io => $each) {
  796. $result[$each] = $each;
  797. }
  798. }
  799. }
  800. }
  801. } else {
  802. if (preg_match($pattern, $stringToParse, $matches)) {
  803. if (isset($matches[1])) {
  804. $result = $matches[1];
  805. }
  806. }
  807. }
  808. }
  809. return ($result);
  810. }
  811. /**
  812. * Renders time duration in seconds into formatted human-readable view
  813. *
  814. * @param int $seconds
  815. *
  816. * @return string
  817. */
  818. function zb_formatTime($seconds) {
  819. $init = $seconds;
  820. $days = floor($seconds / 86400);
  821. $hours = floor(round($seconds / 3600));
  822. $minutes = floor(round(($seconds / 60)) % 60);
  823. $seconds = (round($seconds) % 60);
  824. if ($init < 3600) {
  825. //less than 1 hour
  826. if ($init < 60) {
  827. //less than minute
  828. $result = $seconds . ' ' . __('sec.');
  829. } else {
  830. //more than one minute
  831. $result = $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
  832. }
  833. } else {
  834. if ($init < 86400) {
  835. //more than hour
  836. $result = $hours . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
  837. } else {
  838. $hoursLeft = $hours - ($days * 24);
  839. $result = $days . ' ' . __('days') . ' ' . $hoursLeft . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
  840. }
  841. }
  842. return ($result);
  843. }
  844. /**
  845. * Renders time duration in seconds into formatted human-readable view without seconds
  846. *
  847. * @param int $seconds
  848. *
  849. * @return string
  850. */
  851. function wr_formatTimeArchive($seconds) {
  852. $init = $seconds;
  853. $days = floor($seconds / 86400);
  854. $hours = floor($seconds / 3600);
  855. $minutes = floor(($seconds / 60) % 60);
  856. $seconds = $seconds % 60;
  857. if ($init < 3600) {
  858. //less than 1 hour
  859. if ($init < 60) {
  860. //less than minute
  861. $result = $seconds . ' ' . __('sec.');
  862. } else {
  863. //more than one minute
  864. $result = $minutes . ' ' . __('minutes');
  865. }
  866. } else {
  867. if ($init < 86400) {
  868. //more than hour
  869. $result = $hours . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes');
  870. } else {
  871. $hoursLeft = $hours - ($days * 24);
  872. $result = $days . ' ' . __('days') . ' ' . $hoursLeft . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes');
  873. }
  874. }
  875. return ($result);
  876. }
  877. /**
  878. * Validate a Gregorian date
  879. *
  880. * @param string $date Date in MySQL format
  881. * @return bool
  882. */
  883. function zb_checkDate($date) {
  884. $explode = explode('-', $date);
  885. @$year = $explode[0];
  886. @$month = $explode[1];
  887. @$day = $explode[2];
  888. $result = @checkdate($month, $day, $year);
  889. return ($result);
  890. }
  891. /**
  892. * Checks is time between some other time ranges?
  893. *
  894. * @param string $fromTime start time (format hh:mm OR hh:mm:ss with seconds)
  895. * @param string $toTime end time
  896. * @param string $checkTime time to check
  897. * @param bool $seconds
  898. *
  899. * @return bool
  900. */
  901. function zb_isTimeBetween($fromTime, $toTime, $checkTime, $seconds = false) {
  902. if ($seconds) {
  903. $formatPostfix = ':s';
  904. } else {
  905. $formatPostfix = '';
  906. }
  907. $checkTime = strtotime($checkTime);
  908. $checkTime = date("H:i" . $formatPostfix, $checkTime);
  909. $f = DateTime::createFromFormat('!H:i' . $formatPostfix, $fromTime);
  910. $t = DateTime::createFromFormat('!H:i' . $formatPostfix, $toTime);
  911. $i = DateTime::createFromFormat('!H:i' . $formatPostfix, $checkTime);
  912. if ($f > $t) {
  913. $t->modify('+1 day');
  914. }
  915. return ($f <= $i && $i <= $t) || ($f <= $i->modify('+1 day') && $i <= $t);
  916. }
  917. /**
  918. * Checks is date between some other date ranges?
  919. *
  920. * @param string $fromDate start date (format Y-m-d)
  921. * @param string $toDate end date
  922. * @param string $checkDate date to check
  923. * @param bool $seconds
  924. *
  925. * @return bool
  926. */
  927. function zb_isDateBetween($fromDate, $toDate, $checkDate) {
  928. $result = false;
  929. $fromDate = strtotime($fromDate);
  930. $toDate = strtotime($toDate);
  931. $checkDate = strtotime($checkDate);
  932. $checkDate = date("Y-m-d", $checkDate);
  933. $checkDate = strtotime($checkDate);
  934. if ($checkDate >= $fromDate and $checkDate <= $toDate) {
  935. $result = true;
  936. }
  937. return ($result);
  938. }
  939. /**
  940. * Checks is timestamp between some other time ranges?
  941. *
  942. * @param int $fromTime start time
  943. * @param int $toTime end time
  944. * @param int $checkTime time to check
  945. *
  946. * @return bool
  947. */
  948. function zb_isTimeStampBetween($fromTime, $toTime, $checkTime) {
  949. $result = false;
  950. if ($checkTime >= $fromTime and $checkTime <= $toTime) {
  951. $result = true;
  952. }
  953. return ($result);
  954. }
  955. if (!function_exists('json_validate')) {
  956. /**
  957. * Validates a JSON string. PHP <8.3 replacement.
  958. *
  959. * @param string $json The JSON string to validate.
  960. * @param int $depth Maximum depth. Must be greater than zero.
  961. * @param int $flags Bitmask of JSON decode options.
  962. * @return bool Returns true if the string is a valid JSON, otherwise false.
  963. */
  964. function json_validate($json, $depth = 512, $flags = 0) {
  965. if (!is_string($json)) {
  966. return false;
  967. }
  968. try {
  969. json_decode($json, false, $depth, $flags | JSON_THROW_ON_ERROR);
  970. return true;
  971. } catch (\JsonException $e) {
  972. return false;
  973. }
  974. }
  975. }