uoj-utility-lib.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. function mergeConfig(&$config, $default_config) {
  3. foreach ($default_config as $key => $val) {
  4. if (!isset($config[$key])) {
  5. $config[$key] = $val;
  6. } elseif (is_array($config[$key])) {
  7. mergeConfig($config[$key], $val);
  8. }
  9. }
  10. }
  11. function printSize($size) {
  12. if ($size <= 1024) return "$size Bytes";
  13. if ($size <= 1048576 / 2) return sprintf("%.3lf KB", $size / 1024);
  14. return sprintf("%.3lf MB", $size / 1048576);
  15. }
  16. function strStartWith($str, $pre) {
  17. return substr($str, 0, strlen($pre)) === $pre;
  18. }
  19. function strEndWith($str, $suf) {
  20. return substr($str, -strlen($suf)) === $suf;
  21. }
  22. function strOmit($str, $len) {
  23. if (strlen($str) <= $len + 3) {
  24. return $str;
  25. } else {
  26. return substr($str, 0, $len) . '...';
  27. }
  28. }
  29. function uojTextEncode($str, $config = array()) {
  30. mergeConfig($config, [
  31. 'allow_CR' => false,
  32. 'html_escape' => false
  33. ]);
  34. $allow = array();
  35. for ($c = 32; $c <= 126; $c++) {
  36. $allow[chr($c)] = true;
  37. }
  38. $allow["\n"] = true;
  39. $allow[" "] = true;
  40. $allow["\t"] = true;
  41. if ($config['allow_CR']) {
  42. $allow["\r"] = true;
  43. }
  44. $len = strlen($str);
  45. $ok = true;
  46. for ($i = 0; $i < $len; $i++) {
  47. $c = $str[$i];
  48. if (!isset($allow[$c])) {
  49. $ok = false;
  50. }
  51. }
  52. if ($ok && mb_check_encoding($str, 'utf-8')) {
  53. if (!$config['html_escape']) {
  54. return $str;
  55. } else {
  56. return HTML::escape($str);
  57. }
  58. } else {
  59. $len = strlen($str);
  60. $res = '';
  61. $i = 0;
  62. while ($i < $len) {
  63. $c = $str[$i];
  64. if (ord($c) < 128) {
  65. if (isset($allow[$c])) {
  66. if ($config['html_escape']) {
  67. if ($c == '&') {
  68. $res .= '&amp;';
  69. } elseif ($c == '"') {
  70. $res .= '&quot;';
  71. } elseif ($c == '<') {
  72. $res .= '&lt;';
  73. } elseif ($c == '>') {
  74. $res .= '&gt;';
  75. } else {
  76. $res .= $c;
  77. }
  78. } else {
  79. $res .= $c;
  80. }
  81. } else {
  82. $res .= '<b>\x' . bin2hex($c) . '</b>';
  83. }
  84. $i++;
  85. } else {
  86. $ok = false;
  87. $cur = $c;
  88. for ($j = $i + 1; $j < $i + 4 && $j < $len; $j++) {
  89. $cur .= $str[$j];
  90. if (mb_check_encoding($cur, 'utf-8')) {
  91. $ok = true;
  92. break;
  93. }
  94. }
  95. if ($ok) {
  96. $res .= $cur;
  97. $i = $j + 1;
  98. } else {
  99. $res .= '<b>\x' . bin2hex($c) . '</b>';
  100. $i++;
  101. }
  102. }
  103. }
  104. return $res;
  105. }
  106. }
  107. function base64url_encode($data) {
  108. return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  109. }
  110. function base64url_decode($data) {
  111. return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
  112. }
  113. function blog_name_encode($name) {
  114. $name = str_replace('-', '_', $name);
  115. if (!strStartWith($name, '_') && !strEndWith($name, '_')) {
  116. $name = str_replace('_', '-', $name);
  117. }
  118. $name = strtolower($name);
  119. return $name;
  120. }
  121. function blog_name_decode($name) {
  122. $name = str_replace('-', '_', $name);
  123. $name = strtolower($name);
  124. return $name;
  125. }
  126. function isSuperUser($user) {
  127. return $user != null && $user['usergroup'] == 'S';
  128. }
  129. function getProblemExtraConfig($problem) {
  130. $extra_config = json_decode($problem['extra_config'], true);
  131. $default_extra_config = array(
  132. 'view_content_type' => 'ALL',
  133. 'view_all_details_type' => 'ALL',
  134. 'view_details_type' => 'ALL'
  135. );
  136. mergeConfig($extra_config, $default_extra_config);
  137. return $extra_config;
  138. }
  139. function getProblemSubmissionRequirement($problem) {
  140. return json_decode($problem['submission_requirement'], true);
  141. }
  142. function getProblemCustomTestRequirement($problem) {
  143. $extra_config = json_decode($problem['extra_config'], true);
  144. if (isset($extra_config['custom_test_requirement'])) {
  145. return $extra_config['custom_test_requirement'];
  146. } else {
  147. $answer = array(
  148. 'name' => 'answer',
  149. 'type' => 'source code',
  150. 'file_name' => 'answer.code'
  151. );
  152. foreach (getProblemSubmissionRequirement($problem) as $req) {
  153. if ($req['name'] == 'answer' && $req['type'] == 'source code' && isset($req['languages'])) {
  154. $answer['languages'] = $req['languages'];
  155. }
  156. }
  157. return array(
  158. $answer,
  159. array(
  160. 'name' => 'input',
  161. 'type' => 'text',
  162. 'file_name' => 'input.txt'
  163. )
  164. );
  165. }
  166. }
  167. function sendSystemMsg($username, $title, $content) {
  168. $content = DB::escape($content);
  169. $title = DB::escape($title);
  170. DB::insert("insert into user_system_msg (receiver, title, content, send_time) values ('$username', '$title', '$content', now())");
  171. }