mirror of
https://github.com/renbaoshuo/S2OJ.git
synced 2024-11-14 04:18:41 +00:00
Baoshuo
bae26f1c16
All checks were successful
continuous-integration/drone/push Build is passing
ref: http://htmlpurifier.org/docs/enduser-customize.html#optimized
243 lines
8.1 KiB
PHP
243 lines
8.1 KiB
PHP
<?php
|
|
|
|
class HTML {
|
|
public static function escape($str) {
|
|
return htmlspecialchars($str);
|
|
}
|
|
public static function stripTags($str) {
|
|
return strip_tags($str);
|
|
}
|
|
public static function avatar_addr($user, $size) {
|
|
if ($user['avatar_source'] == 'qq' && $user['qq']) {
|
|
$s = '5';
|
|
|
|
if ($size <= 40) {
|
|
$s = '2';
|
|
} elseif ($size <= 100) {
|
|
$s = '3';
|
|
} elseif ($size <= 140) {
|
|
$s = '4';
|
|
}
|
|
|
|
return "https://q1.qlogo.cn/g?b=qq&nk={$user['qq']}&s=$s";
|
|
}
|
|
|
|
return '//gravatar.loli.net/avatar/' . md5(strtolower(trim($user['email']))) . "?d=mm&s=$size";
|
|
}
|
|
|
|
public static function tablist($tabs_info, $cur, $type = 'nav-tabs') {
|
|
$html = '<ul class="nav '.$type.'" role="tablist">';
|
|
foreach ($tabs_info as $id => $tab) {
|
|
$html .= '<li class="nav-item"><a class="nav-link'.($cur == $id ? ' active' : '').'" href="'.$tab['url'].'" role="tab">'.$tab['name'].'</a></li>';
|
|
}
|
|
$html .= '</ul>';
|
|
return $html;
|
|
}
|
|
|
|
public static function navListGroup($tabs_info, $cur) {
|
|
$html = '<div class="list-group">';
|
|
foreach ($tabs_info as $id => $tab) {
|
|
$html .= '<a role="button" class="list-group-item list-group-item-action'.($cur == $id ? ' active' : '').'" href="'.$tab['url'].'">'.$tab['name'].'</a>';
|
|
}
|
|
$html .= '</div>';
|
|
return $html;
|
|
}
|
|
|
|
public static function hiddenToken() {
|
|
return '<input type="hidden" name="_token" value="'.crsf_token().'" />';
|
|
}
|
|
public static function div_vinput($name, $type, $label_text, $default_value) {
|
|
return '<div id="'."div-$name".'" class="mb-3">'
|
|
. '<label for="'."input-$name".'" class="control-label form-label">'.$label_text.'</label>'
|
|
. '<input type="'.$type.'" class="form-control" name="'.$name.'" id="'."input-$name".'" value="'.HTML::escape($default_value).'" />'
|
|
. '<span class="help-block invalid-feedback" id="'."help-$name".'"></span>'
|
|
. '</div>';
|
|
}
|
|
public static function div_vtextarea($name, $label_text, $default_value) {
|
|
return '<div id="'."div-$name".'" class="mb-3">'
|
|
. '<label for="'."input-$name".'" class="control-label">'.$label_text.'</label>'
|
|
. '<textarea class="form-control" name="'.$name.'" id="'."input-$name".'">'.HTML::escape($default_value).'</textarea>'
|
|
. '<span class="help-block" id="'."help-$name".'"></span>'
|
|
. '</div>';
|
|
}
|
|
public static function checkbox($name, $default_value) {
|
|
$status = $default_value ? 'checked="checked" ' : '';
|
|
return '<input type="checkbox" id="'."input-$name".'" name="'.$name.'" '.$status.'/>';
|
|
}
|
|
|
|
public static function blog_url($username, $uri) {
|
|
switch (UOJConfig::$data['switch']['blog-domain-mode']) {
|
|
case 1:
|
|
$port = ((UOJConfig::$data['web']['blog']['protocol'] === "http" && UOJConfig::$data['web']['blog']['port'] == 80) || (UOJConfig::$data['web']['blog']['protocol'] === "https" && UOJConfig::$data['web']['blog']['port'] == 443)) ? '' : (':'.UOJConfig::$data['web']['blog']['port']);
|
|
$url = UOJConfig::$data['web']['blog']['protocol'].'://'.blog_name_encode($username).'.'.UOJConfig::$data['web']['blog']['host'].$port;
|
|
break;
|
|
case 2:
|
|
$port = ((UOJConfig::$data['web']['blog']['protocol'] === "http" && UOJConfig::$data['web']['blog']['port'] == 80) || (UOJConfig::$data['web']['blog']['protocol'] === "https" && UOJConfig::$data['web']['blog']['port'] == 443)) ? '' : (':'.UOJConfig::$data['web']['blog']['port']);
|
|
$url = UOJConfig::$data['web']['blog']['protocol'].'://'.UOJConfig::$data['web']['blog']['host'].$port.'/'.blog_name_encode($username);
|
|
break;
|
|
case 3:
|
|
$url = HTML::url('/blog/'.blog_name_encode($username));
|
|
break;
|
|
}
|
|
$url .= $uri;
|
|
$url = rtrim($url, '/');
|
|
return HTML::escape($url);
|
|
}
|
|
public static function blog_list_url() {
|
|
switch (UOJConfig::$data['switch']['blog-domain-mode']) {
|
|
case 1:
|
|
case 2:
|
|
$port = ((UOJConfig::$data['web']['blog']['protocol'] === "http" && UOJConfig::$data['web']['blog']['port'] == 80) || (UOJConfig::$data['web']['blog']['protocol'] === "https" && UOJConfig::$data['web']['blog']['port'] == 443)) ? '' : (':'.UOJConfig::$data['web']['blog']['port']);
|
|
$url = UOJConfig::$data['web']['blog']['protocol'].'://'.UOJConfig::$data['web']['blog']['host'].$port;
|
|
break;
|
|
case 3:
|
|
$url = HTML::url('/blogs');
|
|
break;
|
|
}
|
|
return HTML::escape(rtrim($url, '/'));
|
|
}
|
|
|
|
public static function url($uri, $config = array()) {
|
|
$config = array_merge(array(
|
|
'location' => 'main',
|
|
'params' => null
|
|
), $config);
|
|
|
|
$path = strtok($uri, '?');
|
|
$qs = strtok('?');
|
|
parse_str($qs, $param);
|
|
|
|
if ($config['params'] != null) {
|
|
$param = array_merge($param, $config['params']);
|
|
}
|
|
|
|
$url = ''; // '//'.UOJConfig::$data['web'][$config['location']]['host'];
|
|
if ($param) {
|
|
$url .= $path.'?'.HTML::query_string_encode($param);
|
|
} elseif ($path != '/') {
|
|
$url .= rtrim($path, '/');
|
|
} else {
|
|
$url .= $path;
|
|
}
|
|
return HTML::escape($url);
|
|
}
|
|
public static function timeanddate_url($time, $config = array()) {
|
|
$url = UOJConfig::$data['web']['blog']['protocol'].'://';
|
|
$url .= 'www.timeanddate.com/worldclock/fixedtime.html';
|
|
$url .= '?'.'iso='.$time->format('Ymd\THi');
|
|
$url .= '&'.'p1=33';
|
|
if (isset($config['duration']) && $config['duration'] < 3600) {
|
|
$url .= '&'.'ah='.floor($config['duration'] / 60);
|
|
if ($config['duration'] % 60 != 0) {
|
|
$url .= '&'.'am='.($config['duration'] % 60);
|
|
}
|
|
}
|
|
$url = HTML::escape($url);
|
|
return $url;
|
|
}
|
|
|
|
public static function js_src($uri, $config = array('location' => 'main')) {
|
|
return '<script src="'.HTML::url($uri, $config).'"></script>';
|
|
}
|
|
public static function css_link($uri, $config = array('location' => 'main')) {
|
|
return '<link type="text/css" rel="stylesheet" href="'.HTML::url($uri, $config).'" />';
|
|
}
|
|
|
|
public static function query_string_encode($q, $array_name = null) {
|
|
if (!is_array($q)) {
|
|
return false;
|
|
}
|
|
$r = array();
|
|
foreach ((array)$q as $k => $v) {
|
|
if ($array_name !== null) {
|
|
if (is_numeric($k)) {
|
|
$k = $array_name."[]";
|
|
} else {
|
|
$k = $array_name."[$k]";
|
|
}
|
|
}
|
|
if (is_array($v) || is_object($v)) {
|
|
$r[] = self::query_string_encode($v, $k);
|
|
} else {
|
|
$r[] = urlencode($k)."=".urlencode($v);
|
|
}
|
|
}
|
|
return implode("&", $r);
|
|
}
|
|
|
|
public static function purifier() {
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
$config->set('Output.Newline', true);
|
|
$def = $config->getHTMLDefinition(true);
|
|
|
|
$def->addElement('section', 'Block', 'Flow', 'Common');
|
|
$def->addElement('nav', 'Block', 'Flow', 'Common');
|
|
$def->addElement('article', 'Block', 'Flow', 'Common');
|
|
$def->addElement('aside', 'Block', 'Flow', 'Common');
|
|
$def->addElement('header', 'Block', 'Flow', 'Common');
|
|
$def->addElement('footer', 'Block', 'Flow', 'Common');
|
|
|
|
$extra_allowed_html = [
|
|
'span' => ['data-realname' => 'Text', 'data-uoj-username' => 'Number'],
|
|
];
|
|
|
|
foreach ($extra_allowed_html as $element => $attributes) {
|
|
foreach ($attributes as $attribute => $type) {
|
|
$def->addAttribute($element, $attribute, $type);
|
|
}
|
|
}
|
|
|
|
return new HTMLPurifier($config);
|
|
}
|
|
|
|
public static function purifier_inline() {
|
|
$allowed_html = [
|
|
'a' => ['href' => 'URI'],
|
|
'b' => [],
|
|
'i' => [],
|
|
'u' => [],
|
|
's' => [],
|
|
'em' => [],
|
|
'strong' => [],
|
|
'sub' => [],
|
|
'sup' => [],
|
|
'small' => [],
|
|
'del' => [],
|
|
'br' => [],
|
|
'span' => ['data-realname' => 'Text', 'data-uoj-username' => 'Number'],
|
|
];
|
|
|
|
$allowed_elements = [];
|
|
$allowed_attributes = [];
|
|
|
|
foreach ($allowed_html as $element => $attributes) {
|
|
$allowed_elements[$element] = true;
|
|
foreach ($attributes as $attribute => $type) {
|
|
$allowed_attributes["$element.$attribute"] = true;
|
|
}
|
|
}
|
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
$config->set('HTML.AllowedElements', $allowed_elements);
|
|
$config->set('HTML.AllowedAttributes', $allowed_attributes);
|
|
$def = $config->getHTMLDefinition(true);
|
|
|
|
foreach ($allowed_html as $element => $attributes) {
|
|
foreach ($attributes as $attribute => $type) {
|
|
$def->addAttribute($element, $attribute, $type);
|
|
}
|
|
}
|
|
|
|
return new HTMLPurifier($config);
|
|
}
|
|
|
|
public static function parsedown() {
|
|
return new UOJMarkdown([
|
|
'math' => [
|
|
'enabled' => true,
|
|
'matchSingleDollar' => true
|
|
]
|
|
]);
|
|
}
|
|
}
|