api.compat.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Returns userstats config as array
  4. *
  5. * @return array
  6. */
  7. function zbs_LoadConfig() {
  8. $config = parse_ini_file('config/userstats.ini');
  9. return ($config);
  10. }
  11. /**
  12. * Loads required locale lang and returns array of loalized strings
  13. *
  14. * @param type $language
  15. * @return array
  16. */
  17. function zbs_LoadLang($language) {
  18. $language = vf($language);
  19. $language = preg_replace('/\0/s', '', $language);
  20. if (file_exists('languages/' . $language . '/lang.php')) {
  21. include('languages/' . $language . '/lang.php');
  22. //additional locale
  23. if (file_exists('languages/' . $language . '/addons.php')) {
  24. include('languages/' . $language . '/addons.php');
  25. }
  26. } else {
  27. include('languages/english/lang.php');
  28. }
  29. return ($lang);
  30. }
  31. /**
  32. * Returns localized string by current lang
  33. *
  34. * @global string $langglobal
  35. * @param string $str
  36. * @return string
  37. */
  38. function __($str) {
  39. global $langglobal;
  40. if ((isset($langglobal['def'][$str])) and (!empty($langglobal['def'][$str]))) {
  41. return ($langglobal['def'][$str]);
  42. } else {
  43. return ($str);
  44. }
  45. }
  46. /**
  47. * Returns current skin path
  48. *
  49. * @param array $usConfig preloaded usrstats config as array
  50. *
  51. * @return string
  52. */
  53. function zbs_GetCurrentSkinPath($usConfig = array()) {
  54. if (empty($usConfig)) {
  55. $usConfig = zbs_LoadConfig();
  56. }
  57. $basePath = 'skins/';
  58. $skinName = 'default';
  59. if (isset($usConfig['SKIN'])) {
  60. $skinName = $usConfig['SKIN'];
  61. }
  62. $result = $basePath . $skinName . '/';
  63. return ($result);
  64. }
  65. /**
  66. * Renders default userstats template
  67. *
  68. * @global string $ContentContainer
  69. */
  70. function zbs_ShowTemplate() {
  71. global $ContentContainer;
  72. $skinPath = zbs_GetCurrentSkinPath();
  73. if (file_exists($skinPath)) {
  74. include($skinPath . 'template.html');
  75. } else {
  76. print('Skin path not exists: ' . $skinPath);
  77. }
  78. }
  79. /**
  80. * Shows data in primary content container
  81. *
  82. * @global string $ContentContainer
  83. * @param string $title
  84. * @param string $data
  85. */
  86. function show_window($title, $data) {
  87. global $ContentContainer;
  88. $window_content = '
  89. <table width="100%" border="0">
  90. <tr>
  91. <td><h2>' . @$title . '</h2></td>
  92. </tr>
  93. <tr>
  94. <td valign="top">
  95. ' . @$data . '
  96. </td>
  97. </tr>
  98. </table>
  99. ';
  100. $ContentContainer = $ContentContainer . $window_content;
  101. }
  102. /**
  103. * Default debug output
  104. *
  105. * @param string $data
  106. */
  107. function deb($data) {
  108. show_window('DEBUG', $data);
  109. }
  110. /**
  111. * Default array debug output
  112. *
  113. * @param array $data
  114. */
  115. function debarr($data) {
  116. show_window('DEBUG', '<pre>' . print_r($data, true) . '</pre>');
  117. }
  118. /**
  119. * Returns array of files in selected directory
  120. *
  121. * @param string $directory
  122. * @param string $exp
  123. * @param string $type
  124. * @param bool $do_not_filter
  125. * @return array
  126. */
  127. function rcms_scandir($directory, $exp = '', $type = 'all', $do_not_filter = false) {
  128. $dir = $ndir = array();
  129. if (!empty($exp)) {
  130. $exp = '/^' . str_replace('*', '(.*)', str_replace('.', '\\.', $exp)) . '$/';
  131. }
  132. if (!empty($type) && $type !== 'all') {
  133. $func = 'is_' . $type;
  134. }
  135. if (is_dir($directory)) {
  136. $fh = opendir($directory);
  137. while (false !== ($filename = readdir($fh))) {
  138. if (substr($filename, 0, 1) != '.' || $do_not_filter) {
  139. if ((empty($type) || $type == 'all' || $func($directory . '/' . $filename)) && (empty($exp) || preg_match($exp, $filename))) {
  140. $dir[] = $filename;
  141. }
  142. }
  143. }
  144. closedir($fh);
  145. natsort($dir);
  146. }
  147. return $dir;
  148. }
  149. /**
  150. * Loads some general module by its name
  151. *
  152. * @param string $modulename
  153. */
  154. function zbs_LoadModule($modulename) {
  155. $modulename = vf($modulename);
  156. $modulename = preg_replace('/\0/s', '', $modulename);
  157. $module_path = 'modules/general/';
  158. if (file_exists($module_path . $modulename . '/index.php')) {
  159. include($module_path . $modulename . '/index.php');
  160. } else {
  161. die('Wrong module');
  162. }
  163. }
  164. /**
  165. * Returns current date and time in mysql DATETIME format
  166. *
  167. * @return string
  168. */
  169. function curdatetime() {
  170. $currenttime = date("Y-m-d H:i:s");
  171. return ($currenttime);
  172. }
  173. /**
  174. * Returns random string with some length
  175. *
  176. * @param int $size
  177. * @return string
  178. */
  179. function zbs_rand_string($size = 4) {
  180. $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
  181. $string = "";
  182. for ($p = 0; $p < $size; $p++) {
  183. $string .= $characters[mt_rand(0, (strlen($characters) - 1))];
  184. }
  185. return ($string);
  186. }
  187. /**
  188. * Pushes default file-download subroutine
  189. *
  190. * @param string $filePath
  191. * @param string $contentType
  192. * @throws Exception
  193. */
  194. function zbs_DownloadFile($filePath, $contentType = '') {
  195. if (!empty($filePath)) {
  196. if (file_exists($filePath)) {
  197. $fileContent = file_get_contents($filePath);
  198. log_register("DOWNLOAD FILE `" . $filePath . "`");
  199. if (($contentType == '') or ($contentType == 'default')) {
  200. $contentType = 'application/octet-stream';
  201. } else {
  202. //additional content types
  203. if ($contentType == 'docx') {
  204. $contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  205. }
  206. }
  207. header('Content-Type: ' . $contentType);
  208. header('Content-Type: application/octet-stream');
  209. header("Content-Transfer-Encoding: Binary");
  210. header("Content-disposition: attachment; filename=\"" . basename($filePath) . "\"");
  211. header("Content-Description: File Transfer");
  212. header("Content-Length: " . filesize($filePath));
  213. die($fileContent);
  214. } else {
  215. throw new Exception('DOWNLOAD_FILEPATH_NOT_EXISTS');
  216. }
  217. } else {
  218. throw new Exception('DOWNLOAD_FILEPATH_EMPTY');
  219. }
  220. }
  221. /**
  222. * Converts IP to integer value
  223. *
  224. * @param string $src
  225. *
  226. * @return int
  227. */
  228. function ip2int($src) {
  229. $t = explode('.', $src);
  230. return count($t) != 4 ? 0 : 256 * (256 * ((float) $t[0] * 256 + (float) $t[1]) + (float) $t[2]) + (float) $t[3];
  231. }
  232. /**
  233. * Converts integer into IP
  234. *
  235. * @param int $src
  236. *
  237. * @return string
  238. */
  239. function int2ip($src) {
  240. $s1 = (int) ($src / 256);
  241. $i1 = $src - 256 * $s1;
  242. $src = (int) ($s1 / 256);
  243. $i2 = $s1 - 256 * $src;
  244. $s1 = (int) ($src / 256);
  245. return sprintf('%d.%d.%d.%d', $s1, $src - 256 * $s1, $i2, $i1);
  246. }
  247. /**
  248. * Checks for substring in string
  249. *
  250. * @param string $string
  251. * @param string $search
  252. *
  253. * @return bool
  254. */
  255. function ispos($string, $search) {
  256. if (strpos($string, $search) === false) {
  257. return (false);
  258. } else {
  259. return (true);
  260. }
  261. }
  262. /**
  263. * Pushes native redirect
  264. *
  265. * @param string $url
  266. * @param bool $header
  267. */
  268. function rcms_redirect($url, $header = false) {
  269. if ($header) {
  270. @header('Location: ' . $url);
  271. } else {
  272. print('<script language="javascript">document.location.href="' . $url . '";</script>');
  273. }
  274. }