api.compat.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * Framework abstraction functions and wrappers for old code backward compatibility
  4. */
  5. if (!function_exists('show_error')) {
  6. /**
  7. * Shows default error notice
  8. *
  9. * @param string $data
  10. */
  11. function show_error($data) {
  12. show_window('Error', $data);
  13. }
  14. }
  15. /**
  16. * Fast debug text data output
  17. *
  18. * @param string $data
  19. */
  20. function deb($data) {
  21. show_window('DEBUG', $data);
  22. }
  23. /**
  24. * Fast debug output of array
  25. *
  26. * @param string $data
  27. */
  28. function debarr($data) {
  29. $result = print_r($data, true);
  30. $result = '<pre>' . $result . '</pre>';
  31. show_window('DEBUG', $result);
  32. }
  33. /**
  34. * Returns current date and time in mysql DATETIME view
  35. *
  36. * @return string
  37. */
  38. function curdatetime() {
  39. $currenttime = date("Y-m-d H:i:s");
  40. return ($currenttime);
  41. }
  42. /**
  43. * returns current time in mysql DATETIME view
  44. *
  45. * @return string
  46. */
  47. function curtime() {
  48. $currenttime = date("H:i:s");
  49. return ($currenttime);
  50. }
  51. /**
  52. * Returns current date in mysql DATETIME view
  53. *
  54. * @return string
  55. */
  56. function curdate() {
  57. $currentdate = date("Y-m-d");
  58. return ($currentdate);
  59. }
  60. /**
  61. * Returns current year-month in mysql DATETIME view
  62. *
  63. * @return string
  64. */
  65. function curmonth() {
  66. $currentmonth = date("Y-m");
  67. return ($currentmonth);
  68. }
  69. /**
  70. * Returns previous year-month in mysql DATETIME view
  71. *
  72. * @return string
  73. */
  74. function prevmonth() {
  75. $result = date("Y-m", strtotime("-1 months"));
  76. return ($result);
  77. }
  78. /**
  79. * Returns current year as just Y
  80. *
  81. * @return string
  82. */
  83. function curyear() {
  84. $currentyear = date("Y");
  85. return ($currentyear);
  86. }
  87. /**
  88. * Just system logging subroutine
  89. *
  90. * @param string $event
  91. */
  92. function log_register($event) {
  93. $admin_login = whoami();
  94. @$ip = $_SERVER['REMOTE_ADDR'];
  95. if (!$ip) {
  96. $ip = '127.0.0.1';
  97. }
  98. $current_time = curdatetime();
  99. $event = mysql_real_escape_string($event);
  100. $query = "INSERT INTO `weblogs` (`id`,`date`,`admin`,`ip`,`event`) VALUES(NULL,'" . $current_time . "','" . $admin_login . "','" . $ip . "','" . $event . "')";
  101. nr_query($query);
  102. }
  103. if (!function_exists('stg_putlogevent')) {
  104. /**
  105. * stg_putlogevent dummy wrapper for log_register() - only for backward compat
  106. *
  107. * @param string $event
  108. */
  109. function stg_putlogevent($event) {
  110. log_register($event);
  111. }
  112. }
  113. if (!function_exists('__')) {
  114. /**
  115. * Dummy i18n function
  116. *
  117. * @param string $str
  118. * @return string
  119. */
  120. function __($str) {
  121. return ($str);
  122. }
  123. }
  124. if (!function_exists('show_window')) {
  125. /**
  126. * Replace for system content output
  127. *
  128. * @param string $title
  129. * @param string $data
  130. * @param string $align
  131. */
  132. function show_window($title, $data, $align = "left") {
  133. $result = '
  134. <table width="100%" border="0" id="window">
  135. <tr>
  136. <td align="center">
  137. <b>' . $title . '</b>
  138. </td>
  139. </tr>
  140. <tr>
  141. <td align="' . $align . '">
  142. ' . $data . '
  143. </td>
  144. </tr>
  145. </table>
  146. ';
  147. print($result);
  148. }
  149. }
  150. /**
  151. * Check for right via module
  152. *
  153. * @global object $system
  154. * @param string $right
  155. * @return bool
  156. */
  157. function cfr($right) {
  158. global $system;
  159. // uncomment following to run phpunit tests (realy ugly hack, i know)
  160. // run as: phpunit --bootstrap puboot.php tests
  161. // if (empty($system)) {@$system = new rcms_system(); }
  162. if ($system->checkForRight($right)) {
  163. return (true);
  164. } else {
  165. return (false);
  166. }
  167. }
  168. /**
  169. * Replace for $system->user['username']
  170. *
  171. * @global object $system
  172. * @return string
  173. */
  174. function whoami() {
  175. global $system;
  176. @$mylogin = $system->user['username'];
  177. if (empty($mylogin)) {
  178. $mylogin = 'external';
  179. }
  180. return ($mylogin);
  181. }
  182. if (!function_exists('rcms_redirect')) {
  183. /**
  184. * Shows redirection javascript.
  185. *
  186. * @param string $url
  187. * @param bool $header
  188. */
  189. function rcms_redirect($url, $header = false) {
  190. if ($header) {
  191. @header('Location: ' . $url);
  192. } else {
  193. echo '<script language="javascript">document.location.href="' . $url . '";</script>';
  194. }
  195. }
  196. }
  197. if (!function_exists('each2')) {
  198. /**
  199. * (PHP 4, PHP 5, PHP 7)<br/>
  200. * This function has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0.
  201. * Return the current key and value pair from an array and advance the array cursor
  202. * @link http://php.net/manual/en/function.each.php
  203. * @param array $array <p>
  204. * The input array.
  205. * </p>
  206. * @return array the current key and value pair from the array
  207. * <i>array</i>. This pair is returned in a four-element
  208. * array, with the keys 0, 1,
  209. * key, and value. Elements
  210. * 0 and key contain the key name of
  211. * the array element, and 1 and value
  212. * contain the data.
  213. * </p>
  214. * <p>
  215. * If the internal pointer for the array points past the end of the
  216. * array contents, <b>each</b> returns
  217. * <b>FALSE</b>.
  218. */
  219. function each2($arr) {
  220. $key = key($arr);
  221. $result = ($key === null) ? false : array($key, current($arr), 'key' => $key, 'value' => current($arr));
  222. next($arr);
  223. return $result;
  224. }
  225. }
  226. if (!function_exists('ispos_array')) {
  227. /**
  228. * Checks for substring in a string or array of strings
  229. *
  230. * @param string $string
  231. * @param string|array $search
  232. *
  233. * @return bool
  234. */
  235. function ispos_array($string, $search) {
  236. if (is_array($search)) {
  237. foreach ($search as $eachStr) {
  238. if (strpos($string, $eachStr) !== false) {
  239. return (true);
  240. }
  241. }
  242. return (false);
  243. } else {
  244. if (strpos($string, $search) === false) {
  245. return (false);
  246. } else {
  247. return (true);
  248. }
  249. }
  250. }
  251. }
  252. if (!function_exists('json_validate')) {
  253. /**
  254. * Validates a JSON string. PHP <8.3 replacement.
  255. *
  256. * @param string $json The JSON string to validate.
  257. * @param int $depth Maximum depth. Must be greater than zero.
  258. * @param int $flags Bitmask of JSON decode options.
  259. * @return bool Returns true if the string is a valid JSON, otherwise false.
  260. */
  261. function json_validate($json, $depth = 512, $flags = 0) {
  262. if (!is_string($json)) {
  263. return false;
  264. }
  265. try {
  266. json_decode($json, false, $depth, $flags | JSON_THROW_ON_ERROR);
  267. return true;
  268. } catch (\JsonException $e) {
  269. return false;
  270. }
  271. }
  272. }