api.compat.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. return($months);
  419. }
  420. /**
  421. * Returns visual bar with count/total proportional size
  422. *
  423. * @param float $count
  424. * @param float $total
  425. * @return string
  426. */
  427. function web_bar($count, $total) {
  428. $barurl = 'skins/bar.png';
  429. if ($total != 0) {
  430. $width = ($count / $total) * 100;
  431. } else {
  432. $width = 0;
  433. }
  434. $code = wf_img_sized($barurl, '', $width . '%', '14');
  435. return($code);
  436. }
  437. /**
  438. * Calculates percent value
  439. *
  440. * @param float $sum
  441. * @param float $percent
  442. *
  443. * @return float
  444. */
  445. function zb_Percent($sum, $percent) {
  446. $result = $percent / 100 * $sum;
  447. return ($result);
  448. }
  449. /**
  450. * Counts percentage between two values
  451. *
  452. * @param float $valueTotal
  453. * @param float $value
  454. *
  455. * @return float
  456. */
  457. function zb_PercentValue($valueTotal, $value) {
  458. $result = 0;
  459. if ($valueTotal != 0) {
  460. $result = round((($value * 100) / $valueTotal), 2);
  461. }
  462. return ($result);
  463. }
  464. /**
  465. * UTF8-safe translit function
  466. *
  467. * @param $string string to be transliterated
  468. * @param $bool Save case state
  469. *
  470. * @return string
  471. */
  472. function zb_TranslitString($string, $caseSensetive = false) {
  473. if ($caseSensetive) {
  474. $replace = array(
  475. "'" => "",
  476. "`" => "",
  477. "а" => "a", "А" => "A",
  478. "б" => "b", "Б" => "B",
  479. "в" => "v", "В" => "V",
  480. "г" => "g", "Г" => "G",
  481. "д" => "d", "Д" => "D",
  482. "е" => "e", "Е" => "E",
  483. "ё" => "e", "Ё" => "E",
  484. "ж" => "zh", "Ж" => "Zh",
  485. "з" => "z", "З" => "Z",
  486. "и" => "i", "И" => "I",
  487. "й" => "y", "Й" => "Y",
  488. "к" => "k", "К" => "K",
  489. "л" => "l", "Л" => "L",
  490. "м" => "m", "М" => "M",
  491. "н" => "n", "Н" => "N",
  492. "о" => "o", "О" => "O",
  493. "п" => "p", "П" => "P",
  494. "р" => "r", "Р" => "R",
  495. "с" => "s", "С" => "S",
  496. "т" => "t", "Т" => "T",
  497. "у" => "u", "У" => "U",
  498. "ф" => "f", "Ф" => "F",
  499. "х" => "h", "Х" => "H",
  500. "ц" => "c", "Ц" => "C",
  501. "ч" => "ch", "Ч" => "Ch",
  502. "ш" => "sh", "Ш" => "Sh",
  503. "щ" => "sch", "Щ" => "Sch",
  504. "ъ" => "", "Ъ" => "",
  505. "ы" => "y", "Ы" => "Y",
  506. "ь" => "", "Ь" => "",
  507. "э" => "e", "Э" => "E",
  508. "ю" => "yu", "Ю" => "Yu",
  509. "я" => "ya", "Я" => "Ya",
  510. "і" => "i", "І" => "I",
  511. "ї" => "yi", "Ї" => "Yi",
  512. "є" => "e", "Є" => "E",
  513. "ґ" => "g", "Ґ" => "G"
  514. );
  515. } else {
  516. $replace = array(
  517. "'" => "",
  518. "`" => "",
  519. "а" => "a", "А" => "a",
  520. "б" => "b", "Б" => "b",
  521. "в" => "v", "В" => "v",
  522. "г" => "g", "Г" => "g",
  523. "д" => "d", "Д" => "d",
  524. "е" => "e", "Е" => "e",
  525. "ё" => "e", "Ё" => "e",
  526. "ж" => "zh", "Ж" => "zh",
  527. "з" => "z", "З" => "z",
  528. "и" => "i", "И" => "i",
  529. "й" => "y", "Й" => "y",
  530. "к" => "k", "К" => "k",
  531. "л" => "l", "Л" => "l",
  532. "м" => "m", "М" => "m",
  533. "н" => "n", "Н" => "n",
  534. "о" => "o", "О" => "o",
  535. "п" => "p", "П" => "p",
  536. "р" => "r", "Р" => "r",
  537. "с" => "s", "С" => "s",
  538. "т" => "t", "Т" => "t",
  539. "у" => "u", "У" => "u",
  540. "ф" => "f", "Ф" => "f",
  541. "х" => "h", "Х" => "h",
  542. "ц" => "c", "Ц" => "c",
  543. "ч" => "ch", "Ч" => "ch",
  544. "ш" => "sh", "Ш" => "sh",
  545. "щ" => "sch", "Щ" => "sch",
  546. "ъ" => "", "Ъ" => "",
  547. "ы" => "y", "Ы" => "y",
  548. "ь" => "", "Ь" => "",
  549. "э" => "e", "Э" => "e",
  550. "ю" => "yu", "Ю" => "yu",
  551. "я" => "ya", "Я" => "ya",
  552. "і" => "i", "І" => "i",
  553. "ї" => "yi", "Ї" => "yi",
  554. "є" => "e", "Є" => "e",
  555. "ґ" => "g", "Ґ" => "g"
  556. );
  557. }
  558. return $str = iconv("UTF-8", "UTF-8//IGNORE", strtr($string, $replace));
  559. }
  560. /**
  561. * Returns random alpha-numeric string of some lenght
  562. *
  563. * @param int $size
  564. * @return string
  565. */
  566. function zb_rand_string($size = 4) {
  567. $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
  568. $string = "";
  569. for ($p = 0; $p < $size; $p++) {
  570. $string .= $characters[mt_rand(0, (strlen($characters) - 1))];
  571. }
  572. return ($string);
  573. }
  574. /**
  575. * Converts CIDR mask into decimal like 24 => 255.255.255.0
  576. *
  577. * @param int $mask_bits
  578. *
  579. * @return string
  580. */
  581. function multinet_cidr2mask($mask_bits) {
  582. if ($mask_bits > 31 || $mask_bits < 0)
  583. return("0.0.0.0");
  584. $host_bits = 32 - $mask_bits;
  585. $num_hosts = pow(2, $host_bits) - 1;
  586. $netmask = ip2int("255.255.255.255") - $num_hosts;
  587. return int2ip($netmask);
  588. }
  589. /**
  590. * Converts IP to integer value
  591. *
  592. * @param string $src
  593. *
  594. * @return int
  595. */
  596. function ip2int($src) {
  597. $t = explode('.', $src);
  598. return count($t) != 4 ? 0 : 256 * (256 * ((float) $t[0] * 256 + (float) $t[1]) + (float) $t[2]) + (float) $t[3];
  599. }
  600. /**
  601. * Converts integer into IP
  602. *
  603. * @param int $src
  604. *
  605. * @return string
  606. */
  607. function int2ip($src) {
  608. $s1 = (int) ($src / 256);
  609. $i1 = $src - 256 * $s1;
  610. $src = (int) ($s1 / 256);
  611. $i2 = $s1 - 256 * $src;
  612. $s1 = (int) ($src / 256);
  613. return sprintf('%d.%d.%d.%d', $s1, $src - 256 * $s1, $i2, $i1);
  614. }
  615. /**
  616. * Returns exploded array of some multi-lined strings
  617. *
  618. * @param string $data
  619. *
  620. * @return array
  621. */
  622. function explodeRows($data) {
  623. $result = explode("\n", $data);
  624. return ($result);
  625. }
  626. /**
  627. * Initializes file download procedure
  628. *
  629. * @param string $filePath
  630. * @param string $contentType
  631. * @throws Exception
  632. */
  633. function zb_DownloadFile($filePath, $contentType = '') {
  634. if (!empty($filePath)) {
  635. if (file_exists($filePath)) {
  636. log_register("DOWNLOAD FILE `" . $filePath . "`");
  637. if (($contentType == '') OR ( $contentType == 'default')) {
  638. $contentType = 'application/octet-stream';
  639. } else {
  640. //additional content types
  641. if ($contentType == 'docx') {
  642. $contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  643. }
  644. if ($contentType == 'csv') {
  645. $contentType = 'text/csv; charset=Windows-1251';
  646. }
  647. if ($contentType == 'text') {
  648. $contentType = 'text/plain;';
  649. }
  650. if ($contentType == 'jpg') {
  651. $contentType = 'Content-Type: image/jpeg';
  652. }
  653. }
  654. header('Content-Type: ' . $contentType);
  655. header("Content-Transfer-Encoding: Binary");
  656. header("Content-disposition: attachment; filename=\"" . basename($filePath) . "\"");
  657. header("Content-Description: File Transfer");
  658. header("Content-Length: " . filesize($filePath));
  659. flush(); // this doesn't really matter.
  660. $fp = fopen($filePath, "r");
  661. while (!feof($fp)) {
  662. echo fread($fp, 65536);
  663. flush(); // this is essential for large downloads
  664. }
  665. fclose($fp);
  666. die();
  667. } else {
  668. throw new Exception('DOWNLOAD_FILEPATH_NOT_EXISTS');
  669. }
  670. } else {
  671. throw new Exception('DOWNLOAD_FILEPATH_EMPTY');
  672. }
  673. }
  674. /**
  675. * Returns data that contained between two string tags
  676. *
  677. * @param string $openTag - open tag string. Examples: "(", "[", "{", "[sometag]"
  678. * @param string $closeTag - close tag string. Examples: ")", "]", "}", "[/sometag]"
  679. * @param string $stringToParse - just string that contains some data to parse
  680. * @param bool $mutipleResults - extract just first result as string or all matches as array like match=>match
  681. *
  682. * @return string/array
  683. */
  684. function zb_ParseTagData($openTag, $closeTag, $stringToParse = '', $mutipleResults = false) {
  685. $result = '';
  686. if (!empty($openTag) AND !empty($closeTag) AND !empty($stringToParse)) {
  687. $replacements = array(
  688. '(' => '\(',
  689. ')' => '\)',
  690. '[' => '\[',
  691. ']' => '\]',
  692. );
  693. foreach ($replacements as $eachReplaceTag => $eachReplace) {
  694. $openTag = str_replace($eachReplaceTag, $eachReplace, $openTag);
  695. $closeTag = str_replace($eachReplaceTag, $eachReplace, $closeTag);
  696. }
  697. $pattern = '!' . $openTag . '(.*?)' . $closeTag . '!si';
  698. if ($mutipleResults) {
  699. $result = array();
  700. if (preg_match_all($pattern, $stringToParse, $matches)) {
  701. if (isset($matches[1])) {
  702. if (!empty($matches[1])) {
  703. foreach ($matches[1] as $io => $each) {
  704. $result[$each] = $each;
  705. }
  706. }
  707. }
  708. }
  709. } else {
  710. if (preg_match($pattern, $stringToParse, $matches)) {
  711. if (isset($matches[1])) {
  712. $result = $matches[1];
  713. }
  714. }
  715. }
  716. }
  717. return($result);
  718. }
  719. /**
  720. * Renders time duration in seconds into formatted human-readable view
  721. *
  722. * @param int $seconds
  723. *
  724. * @return string
  725. */
  726. function zb_formatTime($seconds) {
  727. $init = $seconds;
  728. $days = floor($seconds / 86400);
  729. $hours = floor(round($seconds / 3600));
  730. $minutes = floor(round(($seconds / 60)) % 60);
  731. $seconds = (round($seconds) % 60);
  732. if ($init < 3600) {
  733. //less than 1 hour
  734. if ($init < 60) {
  735. //less than minute
  736. $result = $seconds . ' ' . __('sec.');
  737. } else {
  738. //more than one minute
  739. $result = $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
  740. }
  741. } else {
  742. if ($init < 86400) {
  743. //more than hour
  744. $result = $hours . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
  745. } else {
  746. $hoursLeft = $hours - ($days * 24);
  747. $result = $days . ' ' . __('days') . ' ' . $hoursLeft . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes') . ' ' . $seconds . ' ' . __('seconds');
  748. }
  749. }
  750. return ($result);
  751. }
  752. /**
  753. * Renders time duration in seconds into formatted human-readable view without seconds
  754. *
  755. * @param int $seconds
  756. *
  757. * @return string
  758. */
  759. function wr_formatTimeArchive($seconds) {
  760. $init = $seconds;
  761. $days = floor($seconds / 86400);
  762. $hours = floor($seconds / 3600);
  763. $minutes = floor(($seconds / 60) % 60);
  764. $seconds = $seconds % 60;
  765. if ($init < 3600) {
  766. //less than 1 hour
  767. if ($init < 60) {
  768. //less than minute
  769. $result = $seconds . ' ' . __('sec.');
  770. } else {
  771. //more than one minute
  772. $result = $minutes . ' ' . __('minutes');
  773. }
  774. } else {
  775. if ($init < 86400) {
  776. //more than hour
  777. $result = $hours . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes');
  778. } else {
  779. $hoursLeft = $hours - ($days * 24);
  780. $result = $days . ' ' . __('days') . ' ' . $hoursLeft . ' ' . __('hour') . ' ' . $minutes . ' ' . __('minutes');
  781. }
  782. }
  783. return ($result);
  784. }
  785. /**
  786. * Validate a Gregorian date
  787. *
  788. * @param string $date Date in MySQL format
  789. * @return bool
  790. */
  791. function zb_checkDate($date) {
  792. $explode = explode('-', $date);
  793. @$year = $explode[0];
  794. @$month = $explode[1];
  795. @$day = $explode[2];
  796. $result = @checkdate($month, $day, $year);
  797. return ($result);
  798. }
  799. /**
  800. * Checks is time between some other time ranges?
  801. *
  802. * @param string $fromTime start time (format hh:mm OR hh:mm:ss with seconds)
  803. * @param string $toTime end time
  804. * @param string $checkTime time to check
  805. * @param bool $seconds
  806. *
  807. * @return bool
  808. */
  809. function zb_isTimeBetween($fromTime, $toTime, $checkTime, $seconds = false) {
  810. if ($seconds) {
  811. $formatPostfix = ':s';
  812. } else {
  813. $formatPostfix = '';
  814. }
  815. $checkTime = strtotime($checkTime);
  816. $checkTime = date("H:i" . $formatPostfix, $checkTime);
  817. $f = DateTime::createFromFormat('!H:i' . $formatPostfix, $fromTime);
  818. $t = DateTime::createFromFormat('!H:i' . $formatPostfix, $toTime);
  819. $i = DateTime::createFromFormat('!H:i' . $formatPostfix, $checkTime);
  820. if ($f > $t) {
  821. $t->modify('+1 day');
  822. }
  823. return ($f <= $i && $i <= $t) || ($f <= $i->modify('+1 day') && $i <= $t);
  824. }
  825. /**
  826. * Checks is date between some other date ranges?
  827. *
  828. * @param string $fromDate start date (format Y-m-d)
  829. * @param string $toDate end date
  830. * @param string $checkDate date to check
  831. * @param bool $seconds
  832. *
  833. * @return bool
  834. */
  835. function zb_isDateBetween($fromDate, $toDate, $checkDate) {
  836. $result = false;
  837. $fromDate = strtotime($fromDate);
  838. $toDate = strtotime($toDate);
  839. $checkDate = strtotime($checkDate);
  840. $checkDate = date("Y-m-d", $checkDate);
  841. $checkDate = strtotime($checkDate);
  842. if ($checkDate >= $fromDate AND $checkDate <= $toDate) {
  843. $result = true;
  844. }
  845. return($result);
  846. }
  847. /**
  848. * Checks is timestamp between some other time ranges?
  849. *
  850. * @param int $fromTime start time
  851. * @param int $toTime end time
  852. * @param int $checkTime time to check
  853. *
  854. * @return bool
  855. */
  856. function zb_isTimeStampBetween($fromTime, $toTime, $checkTime) {
  857. $result = false;
  858. if ($checkTime >= $fromTime AND $checkTime <= $toTime) {
  859. $result = true;
  860. }
  861. return ($result);
  862. }