Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

283 рядки
7.6 KiB

  1. <?php
  2. /**
  3. * Convert unicode in json to utf-8 chars
  4. *
  5. * @param string $json String to convert
  6. * @return string utf-8 string
  7. */
  8. function json_unicode_to_utf8($json){
  9. $json = preg_replace_callback("/\\\\u([0-9a-f]{4})/", create_function('$match', '
  10. $val = intval($match[1], 16);
  11. $c = "";
  12. if($val < 0x7F){ // 0000-007F
  13. $c .= chr($val);
  14. } elseif ($val < 0x800) { // 0080-0800
  15. $c .= chr(0xC0 | ($val / 64));
  16. $c .= chr(0x80 | ($val % 64));
  17. } else { // 0800-FFFF
  18. $c .= chr(0xE0 | (($val / 64) / 64));
  19. $c .= chr(0x80 | (($val / 64) % 64));
  20. $c .= chr(0x80 | ($val % 64));
  21. }
  22. return $c;
  23. '), $json);
  24. return $json;
  25. }
  26. /**
  27. * Format JSON to pretty html
  28. *
  29. * @param string $json JSON to format
  30. * @return string
  31. */
  32. function json_format_html($json)
  33. {
  34. $json = json_unicode_to_utf8($json);
  35. $tab = "&nbsp;&nbsp;";
  36. $new_json = "";
  37. $indent_level = 0;
  38. $in_string = false;
  39. $len = strlen($json);
  40. for($c = 0; $c < $len; $c++)
  41. {
  42. $char = $json[$c];
  43. switch($char)
  44. {
  45. case '{':
  46. case '[':
  47. $char = "<font color=\"green\">" . $char . "</font>";//iwind
  48. if(!$in_string) {
  49. $new_json .= $char . "<br/>" . str_repeat($tab, $indent_level+1);
  50. $indent_level++;
  51. }
  52. else {
  53. $new_json .= "[";
  54. }
  55. break;
  56. case '}':
  57. case ']':
  58. $char = "<font color=\"green\">" . $char . "</font>";//iwind
  59. if(!$in_string)
  60. {
  61. $indent_level--;
  62. $new_json .= "<br/>" . str_repeat($tab, $indent_level) . $char;
  63. }
  64. else
  65. {
  66. $new_json .= "]";
  67. }
  68. break;
  69. case ',':
  70. $char = "<font color=\"green\">" . $char . "</font>";//iwind
  71. if(!$in_string) {
  72. $new_json .= ",<br/>" . str_repeat($tab, $indent_level);
  73. }
  74. else {
  75. $new_json .= ",";
  76. }
  77. break;
  78. case ':':
  79. $char = "<font color=\"green\">" . $char . "</font>";//iwind
  80. if($in_string) {
  81. $new_json .= ":";
  82. }
  83. else {
  84. $new_json .= $char;
  85. }
  86. break;
  87. case '"':
  88. if($c > 0 && $json[$c-1] != '\\') {
  89. $in_string = !$in_string;
  90. if ($in_string) {
  91. $new_json .= "<font color=\"#DD0000\" class=\"string_var\">" . $char;
  92. }
  93. else {
  94. $new_json .= $char . "</font>";
  95. }
  96. break;
  97. }
  98. else if ($c == 0) {
  99. $in_string = !$in_string;
  100. $new_json .= "<font color=\"red\">" . $char;
  101. break;
  102. }
  103. default:
  104. if (!$in_string && trim($char) !== "") {
  105. $char = "<font color=\"blue\">" . $char . "</font>";
  106. }
  107. else {
  108. if ($char == "&" || $char == "'" || $char == "\"" || $char == "<" || $char == ">") {
  109. $char = htmlspecialchars($char);
  110. }
  111. }
  112. $new_json .= $char;
  113. break;
  114. }
  115. }
  116. $new_json = preg_replace_callback("{(<font color=\"blue\">([\\da-zA-Z_\\.]+)</font>)+}", create_function('$match','
  117. $string = str_replace("<font color=\"blue\">", "", $match[0]);
  118. $string = str_replace("</font>", "", $string);
  119. return "<font color=\"blue\" class=\"no_string_var\">" . $string . "</font>";
  120. '), $new_json);
  121. return $new_json;
  122. }
  123. /**
  124. * PHP Integration of Open Flash Chart
  125. * Copyright (C) 2008 John Glazebrook <open-flash-chart@teethgrinder.co.uk>
  126. *
  127. * This library is free software; you can redistribute it and/or
  128. * modify it under the terms of the GNU Lesser General Public
  129. * License as published by the Free Software Foundation; either
  130. * version 2.1 of the License, or (at your option) any later version.
  131. *
  132. * This library is distributed in the hope that it will be useful,
  133. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  134. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  135. * Lesser General Public License for more details.
  136. *
  137. * You should have received a copy of the GNU Lesser General Public
  138. * License along with this library; if not, write to the Free Software
  139. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  140. */
  141. /**
  142. * Format JSON to pretty style
  143. *
  144. * @param string $json JSON to format
  145. * @return string
  146. */
  147. function json_format($json)
  148. {
  149. $tab = " ";
  150. $new_json = "";
  151. $indent_level = 0;
  152. $in_string = false;
  153. /*
  154. commented out by monk.e.boy 22nd May '08
  155. because my web server is PHP4, and
  156. json_* are PHP5 functions...
  157. $json_obj = json_decode($json);
  158. if($json_obj === false)
  159. return false;
  160. $json = json_encode($json_obj);
  161. */
  162. $len = strlen($json);
  163. for($c = 0; $c < $len; $c++)
  164. {
  165. $char = $json[$c];
  166. switch($char)
  167. {
  168. case '{':
  169. case '[':
  170. if(!$in_string)
  171. {
  172. $new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
  173. $indent_level++;
  174. }
  175. else
  176. {
  177. $new_json .= $char;
  178. }
  179. break;
  180. case '}':
  181. case ']':
  182. if(!$in_string)
  183. {
  184. $indent_level--;
  185. $new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
  186. }
  187. else
  188. {
  189. $new_json .= $char;
  190. }
  191. break;
  192. case ',':
  193. if(!$in_string)
  194. {
  195. $new_json .= ",\n" . str_repeat($tab, $indent_level);
  196. }
  197. else
  198. {
  199. $new_json .= $char;
  200. }
  201. break;
  202. case ':':
  203. if(!$in_string)
  204. {
  205. $new_json .= ": ";
  206. }
  207. else
  208. {
  209. $new_json .= $char;
  210. }
  211. break;
  212. case '"':
  213. if($c > 0 && $json[$c-1] != '\\')
  214. {
  215. $in_string = !$in_string;
  216. }
  217. default:
  218. $new_json .= $char;
  219. break;
  220. }
  221. }
  222. return $new_json;
  223. }
  224. /**
  225. * Format bytes to human size
  226. *
  227. * @param integer $bytes Size in byte
  228. * @param integer $precision Precision
  229. * @return string size in k, m, g..
  230. * @since 1.1.7
  231. */
  232. function r_human_bytes($bytes, $precision = 2) {
  233. if ($bytes == 0) {
  234. return 0;
  235. }
  236. if ($bytes < 1024) {
  237. return $bytes . "B";
  238. }
  239. if ($bytes < 1024 * 1024) {
  240. return round($bytes/1024, $precision) . "k";
  241. }
  242. if ($bytes < 1024 * 1024 * 1024) {
  243. return round($bytes/1024/1024, $precision) . "m";
  244. }
  245. if ($bytes < 1024 * 1024 * 1024 * 1024) {
  246. return round($bytes/1024/1024/1024, $precision) . "g";
  247. }
  248. return $bytes;
  249. }
  250. /**
  251. * Get collection display icon
  252. *
  253. * @param string $collectionName Collection name
  254. * @return string
  255. * @since 1.1.8
  256. */
  257. function r_get_collection_icon($collectionName) {
  258. if (preg_match("/\\.(files|chunks)$/", $collectionName)){
  259. return "grid";
  260. }
  261. if (preg_match("/^system\\.js$/", $collectionName)) {
  262. return "table-systemjs";
  263. }
  264. return "table";
  265. }
  266. ?>