This commit is contained in:
Baoshuo Ren 2022-03-20 18:23:40 +08:00
parent e3fc955b75
commit 71214c0631
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -7,6 +7,8 @@ class UOJBlogEditor {
public $save; public $save;
public $cur_data = array(); public $cur_data = array();
public $post_data = array(); public $post_data = array();
public $show_editor = true;
public $show_tags = true;
public $label_text = array( public $label_text = array(
'title' => '标题', 'title' => '标题',
@ -78,7 +80,15 @@ class UOJBlogEditor {
} }
private function receivePostData() { private function receivePostData() {
$errors = array(); $errors = array();
foreach (array('title', 'content_md', 'tags') as $name) {
$keys = array('title');
if ($this->show_tags) {
$keys[] = 'tags';
}
if ($this->show_editor) {
$keys[] = 'content_md';
}
foreach ($keys as $name) {
$cur_err = $this->validate($name); $cur_err = $this->validate($name);
if ($cur_err) { if ($cur_err) {
$errors[$name] = $cur_err; $errors[$name] = $cur_err;
@ -95,34 +105,35 @@ class UOJBlogEditor {
$this->post_data['title'] = HTML::escape($this->post_data['title']); $this->post_data['title'] = HTML::escape($this->post_data['title']);
if ($this->type == 'blog') { if ($this->show_editor) {
$content_md = $_POST[$this->name . '_content_md']; if ($this->type == 'blog') {
try { $content_md = $_POST[$this->name . '_content_md'];
$v8 = new V8Js('POST'); try {
$v8->content_md = $this->post_data['content_md']; $v8 = new V8Js('POST');
$v8->executeString(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/js/marked.js'), 'marked.js'); $v8->content_md = $this->post_data['content_md'];
$this->post_data['content'] = $v8->executeString('marked(POST.content_md)'); $v8->executeString(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/js/marked.js'), 'marked.js');
} catch (V8JsException $e) { $this->post_data['content'] = $v8->executeString('marked(POST.content_md)');
die(json_encode(array('content_md' => '未知错误'))); } catch (V8JsException $e) {
} die(json_encode(array('content_md' => '未知错误')));
}
if (preg_match('/^.*<!--.*readmore.*-->.*$/m', $this->post_data['content'], $matches, PREG_OFFSET_CAPTURE)) { if (preg_match('/^.*<!--.*readmore.*-->.*$/m', $this->post_data['content'], $matches, PREG_OFFSET_CAPTURE)) {
$content_less = substr($this->post_data['content'], 0, $matches[0][1]); $content_less = substr($this->post_data['content'], 0, $matches[0][1]);
$content_more = substr($this->post_data['content'], $matches[0][1] + strlen($matches[0][0])); $content_more = substr($this->post_data['content'], $matches[0][1] + strlen($matches[0][0]));
$this->post_data['content'] = $purifier->purify($content_less).'<!-- readmore -->'.$purifier->purify($content_more); $this->post_data['content'] = $purifier->purify($content_less).'<!-- readmore -->'.$purifier->purify($content_more);
} else { } else {
$this->post_data['content'] = $purifier->purify($this->post_data['content']); $this->post_data['content'] = $purifier->purify($this->post_data['content']);
} }
} elseif ($this->type == 'slide') { } elseif ($this->type == 'slide') {
$content_array = yaml_parse($this->post_data['content_md']); $content_array = yaml_parse($this->post_data['content_md']);
if ($content_array === false || !is_array($content_array)) { if ($content_array === false || !is_array($content_array)) {
die(json_encode(array('content_md' => '不合法的 YAML 格式'))); die(json_encode(array('content_md' => '不合法的 YAML 格式')));
} }
try { try {
$v8 = new V8Js('PHP'); $v8 = new V8Js('PHP');
$v8->executeString(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/js/marked.js'), 'marked.js'); $v8->executeString(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/js/marked.js'), 'marked.js');
$v8->executeString(<<<EOD $v8->executeString(<<<EOD
marked.setOptions({ marked.setOptions({
getLangClass: function(lang) { getLangClass: function(lang) {
lang = lang.toLowerCase(); lang = lang.toLowerCase();
@ -146,47 +157,48 @@ marked.setOptions({
}) })
EOD EOD
); );
} catch (V8JsException $e) {
die(json_encode(array('content_md' => '未知错误')));
}
$marked = function($md) use ($v8, $purifier) {
try {
$v8->md = $md;
return $purifier->purify($v8->executeString('marked(PHP.md)'));
} catch (V8JsException $e) { } catch (V8JsException $e) {
die(json_encode(array('content_md' => '未知错误'))); die(json_encode(array('content_md' => '未知错误')));
} }
};
$config = array(); $marked = function($md) use ($v8, $purifier) {
$this->post_data['content'] = ''; try {
foreach ($content_array as $slide_name => $slide_content) { $v8->md = $md;
if (is_array($slide_content) && is_array($slide_content['config'])) { return $purifier->purify($v8->executeString('marked(PHP.md)'));
foreach (array('theme') as $config_key) { } catch (V8JsException $e) {
if (is_string($slide_content['config'][$config_key]) && strlen($slide_content['config'][$config_key]) <= 30) { die(json_encode(array('content_md' => '未知错误')));
$config[$config_key] = $slide_content['config'][$config_key]; }
};
$config = array();
$this->post_data['content'] = '';
foreach ($content_array as $slide_name => $slide_content) {
if (is_array($slide_content) && is_array($slide_content['config'])) {
foreach (array('theme') as $config_key) {
if (is_string($slide_content['config'][$config_key]) && strlen($slide_content['config'][$config_key]) <= 30) {
$config[$config_key] = $slide_content['config'][$config_key];
}
}
continue;
}
$this->post_data['content'] .= '<section>';
if (is_string($slide_content)) {
$this->post_data['content'] .= $marked($slide_content);
} elseif (is_array($slide_content)) {
if (is_array($slide_content['children'])) {
foreach ($slide_content['children'] as $cslide_name => $cslide_content) {
$this->post_data['content'] .= '<section>';
$this->post_data['content'] .= $marked($cslide_content);
$this->post_data['content'] .= '</section>';
}
} }
} }
continue; $this->post_data['content'] .= "</section>\n";
} }
$this->post_data['content'] = json_encode($config) . "\n" . $this->post_data['content'];
$this->post_data['content'] .= '<section>';
if (is_string($slide_content)) {
$this->post_data['content'] .= $marked($slide_content);
} elseif (is_array($slide_content)) {
if (is_array($slide_content['children'])) {
foreach ($slide_content['children'] as $cslide_name => $cslide_content) {
$this->post_data['content'] .= '<section>';
$this->post_data['content'] .= $marked($cslide_content);
$this->post_data['content'] .= '</section>';
}
}
}
$this->post_data['content'] .= "</section>\n";
} }
$this->post_data['content'] = json_encode($config) . "\n" . $this->post_data['content'];
} }
} }