S2OJ/web/app/libs/uoj-rand-lib.php

34 lines
866 B
PHP
Raw Permalink Normal View History

2016-07-18 16:39:37 +00:00
<?php
function uojRand($l, $r) {
return mt_rand($l, $r);
}
2022-10-12 13:10:07 +00:00
function uojRandString($len, $charset = '0123456789abcdefghijklmnopqrstuvwxyz') {
2016-07-18 16:39:37 +00:00
$n_chars = strlen($charset);
$str = '';
for ($i = 0; $i < $len; $i++) {
$str .= $charset[uojRand(0, $n_chars - 1)];
}
return $str;
}
2022-10-12 13:10:07 +00:00
function uojRandAvaiableFileName($dir, $length = 20, $suffix = '') {
2016-07-18 16:39:37 +00:00
do {
2022-10-12 13:10:07 +00:00
$fileName = $dir . uojRandString($length);
2022-11-06 02:26:21 +00:00
} while (file_exists(UOJContext::storagePath() . $fileName . $suffix));
return $fileName . $suffix;
2016-07-18 16:39:37 +00:00
}
function uojRandAvaiableTmpFileName() {
return uojRandAvaiableFileName('/tmp/');
}
function uojRandAvaiableSubmissionFileName() {
$num = uojRand(1, 10000);
2022-11-06 02:26:21 +00:00
if (!file_exists(UOJContext::storagePath() . "/submission/$num")) {
mkdir(UOJContext::storagePath() . "/submission/$num", 0777, true);
2016-07-18 16:39:37 +00:00
}
return uojRandAvaiableFileName("/submission/$num/");
}