From 9fd210b9270d68352295409ebea964b7b475fc73 Mon Sep 17 00:00:00 2001 From: umorigu Date: Fri, 1 Jun 2018 06:01:37 +0900 Subject: [PATCH] BugTrack/2475 template plugin: Return template pages in JSON format --- lib/html.php | 86 ++++++++++++++++++++++++++----------------------- plugin/template.inc.php | 25 ++++++++++++-- 2 files changed, 69 insertions(+), 42 deletions(-) diff --git a/lib/html.php b/lib/html.php index bf80dde..438aef2 100644 --- a/lib/html.php +++ b/lib/html.php @@ -313,20 +313,17 @@ EOS; // Show 'edit' form function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE) { - global $vars, $rows, $cols, $hr, $function_freeze; + global $vars, $rows, $cols; global $_btn_preview, $_btn_repreview, $_btn_update, $_btn_cancel, $_msg_help; - global $whatsnew, $_btn_template, $_btn_load, $load_template_func; + global $_btn_template, $_btn_load, $load_template_func; global $notimeupdate; - global $_title_list; global $_msg_edit_cancel_confirm, $_msg_edit_unloadbefore_message; global $rule_page; $script = get_base_uri(); // Newly generate $digest or not if ($digest === FALSE) $digest = md5(join('', get_source($page))); - $refer = $template = ''; - // Add plugin $addtag = $add_top = ''; if(isset($vars['add'])) { @@ -339,46 +336,14 @@ function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE) '' . $_btn_addtop . '' . ''; } - if($load_template_func && $b_template) { - $tpage_names = array(); // Pages marked as template - $template_page = ':config/Templates'; - $page_max = 100; - foreach(get_source($template_page) as $_templates) { - $m = array(); - if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue; - $tpage = preg_replace('#^./#', "$template_page/", $m[1]); - if (! is_page($tpage)) continue; - $tpage_names[] = $tpage; - } - $page_names = array(); - $page_list = get_existpages(); - if (count($page_list) > $page_max) { - // Extract only template name pages - $target_pages = array(); - foreach ($page_list as $_page) { - if (preg_match('/template/i', $_page)) { - $target_pages[] = $_page; - } - } - } else { - $target_pages = $page_list; - } - foreach ($target_pages as $_page) { - if ($_page == $whatsnew || check_non_list($_page) || - !is_page_readable($_page)) { - continue; - } - $tpage_names[] = $_page; - } - $tpage_names2 = array_values(array_unique($tpage_names)); - natcasesort($tpage_names2); + $template_page_list = get_template_page_list(); $tpages = array(); // Template pages - foreach($tpage_names2 as $p) { + foreach($template_page_list as $p) { $ps = htmlsc($p); $tpages[] = ' '; } - if (count($tpage_names2) > 0) { + if (count($template_page_list) > 0) { $s_tpages = join("\n", $tpages); } else { $s_tpages = ' '; @@ -460,6 +425,47 @@ EOD; return $body; } +/** + * Get template page list. + */ +function get_template_page_list() +{ + global $whatsnew; + $tpage_names = array(); // Pages marked as template + $template_page = ':config/Templates'; + $page_max = 100; + foreach(get_source($template_page) as $_templates) { + $m = array(); + if (! preg_match('#\-\s*\[\[([^\[\]]+)\]\]#', $_templates, $m)) continue; + $tpage = preg_replace('#^./#', "$template_page/", $m[1]); + if (! is_page($tpage)) continue; + $tpage_names[] = $tpage; + } + $page_names = array(); + $page_list = get_existpages(); + if (count($page_list) > $page_max) { + // Extract only template name pages + $target_pages = array(); + foreach ($page_list as $_page) { + if (preg_match('/template/i', $_page)) { + $target_pages[] = $_page; + } + } + } else { + $target_pages = $page_list; + } + foreach ($target_pages as $_page) { + if ($_page == $whatsnew || check_non_list($_page) || + !is_page_readable($_page)) { + continue; + } + $tpage_names[] = $_page; + } + $tempalte_page_list = array_values(array_unique($tpage_names)); + natcasesort($tempalte_page_list); + return $tempalte_page_list; +} + // Related pages function make_related($page, $tag = '') { diff --git a/plugin/template.inc.php b/plugin/template.inc.php index 34e9ceb..971c46b 100644 --- a/plugin/template.inc.php +++ b/plugin/template.inc.php @@ -20,9 +20,13 @@ function plugin_template_action() $script = get_base_uri(); if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing'); - if (! isset($vars['refer']) || ! is_page($vars['refer'])) + if (! isset($vars['refer']) || ! is_page($vars['refer'])) { + if (isset($vars['action']) && $vars['action'] === 'list') { + plugin_template_output_list(); + exit; + } return FALSE; - + } $refer = $vars['refer']; // Ensure page is readable, or show Login UI and exit ensure_page_readable($refer); @@ -95,3 +99,20 @@ EOD; return $retvar; } + +function plugin_template_output_list() +{ + $template_page_key = 'template_pages'; + $empty_result = '{"' . $template_page_key . '":[]}'; + header('Content-Type: application/json; charset=UTF-8'); + // PHP 5.4+ + $enabled = defined('JSON_UNESCAPED_UNICODE') && defined('PKWK_UTF8_ENABLE'); + if (!$enabled) { + print($empty_result); + exit; + } + $template_pages = array_values(get_template_page_list()); + $ar = array($template_page_key => $template_pages); + print(json_encode($ar, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); + exit; +} -- 2.11.0