From 688171d016412f30a3ed60d4dc70feb64a19621b Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Wed, 15 Jun 2016 06:25:31 -0400 Subject: Rename page feature added. Fixed bug in git commit function and other minor fixes. --- include/wiki.php | 55 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/wiki.php b/include/wiki.php index 4aa3fc1b4..d60f4a3a7 100644 --- a/include/wiki.php +++ b/include/wiki.php @@ -231,6 +231,34 @@ function wiki_create_page($name, $resource_id) { } +function wiki_rename_page($arr) { + $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : ''); + $pageNewName = ((array_key_exists('pageNewName',$arr)) ? $arr['pageNewName'] : ''); + $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : ''); + $w = wiki_get_wiki($resource_id); + if (!$w['path']) { + return array('message' => 'Wiki not found.', 'success' => false); + } + $page_path_old = $w['path'].'/'.$pageUrlName.'.md'; + logger('$page_path_old: ' . $page_path_old); + if (!is_readable($page_path_old) === true) { + return array('message' => 'Cannot read wiki page: ' . $page_path_old, 'success' => false); + } + $page = array('rawName' => $pageNewName, 'htmlName' => escape_tags($pageNewName), 'urlName' => urlencode(escape_tags($pageNewName)), 'fileName' => urlencode(escape_tags($pageNewName)).'.md'); + $page_path_new = $w['path'] . '/' . $page['fileName'] ; + logger('$page_path_new: ' . $page_path_new); + if (is_file($page_path_new)) { + return array('message' => 'Page already exists.', 'success' => false); + } + // Rename the page file in the wiki repo + if(!rename($page_path_old, $page_path_new)) { + return array('message' => 'Error renaming page file.', 'success' => false); + } else { + return array('page' => $page, 'message' => '', 'success' => true); + } + +} + function wiki_get_page_content($arr) { $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : ''); $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : ''); @@ -342,7 +370,7 @@ function wiki_revert_page($arr) { } } } catch (\PHPGit\Exception\GitException $e) { - json_return_and_die(array('content' => $content, 'message' => 'GitRepo error thrown', 'success' => false)); + return array('content' => $content, 'message' => 'GitRepo error thrown', 'success' => false); } return array('content' => $content, 'message' => '', 'success' => true); } else { @@ -352,9 +380,18 @@ function wiki_revert_page($arr) { function wiki_git_commit($arr) { $files = ((array_key_exists('files', $arr)) ? $arr['files'] : null); + $all = ((array_key_exists('all', $arr)) ? $arr['all'] : false); $commit_msg = ((array_key_exists('commit_msg', $arr)) ? $arr['commit_msg'] : 'Repo updated'); - $resource_id = ((array_key_exists('resource_id', $arr)) ? $arr['resource_id'] : json_return_and_die(array('message' => 'Wiki resource_id required for git commit', 'success' => false))); - $observer = ((array_key_exists('observer', $arr)) ? $arr['observer'] : json_return_and_die(array('message' => 'Observer required for git commit', 'success' => false))); + if(array_key_exists('resource_id', $arr)) { + $resource_id = $arr['resource_id']; + } else { + return array('message' => 'Wiki resource_id required for git commit', 'success' => false); + } + if(array_key_exists('observer', $arr)) { + $observer = $arr['observer']; + } else { + return array('message' => 'Observer required for git commit', 'success' => false); + } $w = wiki_get_wiki($resource_id); if (!$w['path']) { return array('message' => 'Error reading wiki', 'success' => false); @@ -369,23 +406,23 @@ function wiki_git_commit($arr) { if ($files === null) { $options = array('all' => true); // git commit option to include all changes } else { - $options = array(); // git commit options + $options = array('all' => $all); // git commit options\ foreach ($files as $file) { if (!$git->git->add($file)) { // add specified files to the git repo stage if (!$git->git->reset->hard()) { - json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file . '. Error resetting git repo.', 'success' => false)); + return array('message' => 'Error adding file to git stage: ' . $file . '. Error resetting git repo.', 'success' => false); } - json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file, 'success' => false)); + return array('message' => 'Error adding file to git stage: ' . $file, 'success' => false); } } } if ($git->commit($commit_msg, $options)) { - json_return_and_die(array('message' => 'Wiki repo commit succeeded', 'success' => true)); + return array('message' => 'Wiki repo commit succeeded', 'success' => true); } else { - json_return_and_die(array('message' => 'Wiki repo commit failed', 'success' => false)); + return array('message' => 'Wiki repo commit failed', 'success' => false); } } catch (\PHPGit\Exception\GitException $e) { - json_return_and_die(array('message' => 'GitRepo error thrown', 'success' => false)); + return array('message' => 'GitRepo error thrown', 'success' => false); } } -- cgit v1.2.3 From fa48de33c2f6cefbac8bfec7cde75b75390d5f39 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 15 Jun 2016 19:44:15 -0700 Subject: provide syntax based [colour] highlighting on code blocks for popular languages. I'm not happy with the line height on the list elements but couldn't see where this was defaulted. This uses the syntax [code=xxx]some code snippet[/code], where xxx represents a code/language style - with about 18 builtins. --- include/bbcode.php | 14 ++++++++++++++ include/text.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) (limited to 'include') diff --git a/include/bbcode.php b/include/bbcode.php index 63a475779..3fc1e2ea4 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -491,6 +491,11 @@ function bb_code($match) { return '' . trim($match[1]) . ''; } +function bb_highlight($match) { + if(in_array($match[1],['php','css','mysql','sql','abap','diff','html','perl','ruby', + 'vbscript','avrc','dtd','java','xml','cpp','python','javascript','sh'])) + return text_highlight($match[2],$match[1]); +} // BBcode 2 HTML was written by WAY2WEB.net @@ -566,6 +571,15 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $cache = false) $Text = str_replace(">", ">", $Text); + // Check for [code] text here, before the linefeeds are messed with. + // The highlighter will unescape and re-escape the content. + + if (strpos($Text,'[code=') !== false) { + $Text = preg_replace_callback("/\[code=(.*?)\](.*?)\[\/code\]/ism", 'bb_highlight', $Text); + } + + + // Convert new line chars to html
tags // nlbr seems to be hopelessly messed up diff --git a/include/text.php b/include/text.php index bd59aa732..7b43ece43 100644 --- a/include/text.php +++ b/include/text.php @@ -2881,3 +2881,41 @@ function flatten_array_recursive($arr) { } return($ret); } + +function text_highlight($s,$lang) { + + if(! strpos('Text_Highlighter',get_include_path())) { + set_include_path(get_include_path() . PATH_SEPARATOR . 'library/Text_Highlighter'); + head_add_css('/library/Text_Highlighter/sample.css'); + } + require_once('library/Text_Highlighter/Text/Highlighter.php'); + require_once('library/Text_Highlighter/Text/Highlighter/Renderer/Html.php'); + $options = array( + 'numbers' => HL_NUMBERS_LI, + 'tabsize' => 4, + ); + $tag_added = false; + $s = trim(html_entity_decode($s,ENT_COMPAT)); + $s = str_replace(" ","\t",$s); + if($lang === 'php') { + if(strpos('setRenderer($renderer); + $o = $hl->highlight($s); + $o = str_replace(" ","    ",$o); + + if($tag_added) { + $b = substr($o,0,strpos($o,'
  • ')); + $e = substr($o,strpos($o,'
  • ')); + $o = $b . $e; + } + + return('' . $o . ''); +} + -- cgit v1.2.3 From 854c23a751bc8a24e4c43e2d16175bcef6021663 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 15 Jun 2016 19:59:30 -0700 Subject: This was the reason for the large line widths - an extra linefeed after each li element --- include/text.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/text.php b/include/text.php index 7b43ece43..ec42c650d 100644 --- a/include/text.php +++ b/include/text.php @@ -2908,7 +2908,7 @@ function text_highlight($s,$lang) { $hl = Text_Highlighter::factory($lang); $hl->setRenderer($renderer); $o = $hl->highlight($s); - $o = str_replace(" ","    ",$o); + $o = str_replace([" ","\n"],["    ",''],$o); if($tag_added) { $b = substr($o,0,strpos($o,'
  • ')); -- cgit v1.2.3 From 1dc35db1fe68514b55ddcc70e60dd06b117f81b7 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 15 Jun 2016 20:25:19 -0700 Subject: ignore case in language names and add 'js' as an alias for 'javascript' --- include/bbcode.php | 6 +++--- include/text.php | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/bbcode.php b/include/bbcode.php index 3fc1e2ea4..82fa33527 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -492,9 +492,9 @@ function bb_code($match) { } function bb_highlight($match) { - if(in_array($match[1],['php','css','mysql','sql','abap','diff','html','perl','ruby', - 'vbscript','avrc','dtd','java','xml','cpp','python','javascript','sh'])) - return text_highlight($match[2],$match[1]); + if(in_array(strtolower($match[1]),['php','css','mysql','sql','abap','diff','html','perl','ruby', + 'vbscript','avrc','dtd','java','xml','cpp','python','javascript','js','sh'])) + return text_highlight($match[2],strtolower($match[1])); } diff --git a/include/text.php b/include/text.php index ec42c650d..59190a9ca 100644 --- a/include/text.php +++ b/include/text.php @@ -2884,9 +2884,11 @@ function flatten_array_recursive($arr) { function text_highlight($s,$lang) { + if($lang === 'js') + $lang = 'javascript'; + if(! strpos('Text_Highlighter',get_include_path())) { set_include_path(get_include_path() . PATH_SEPARATOR . 'library/Text_Highlighter'); - head_add_css('/library/Text_Highlighter/sample.css'); } require_once('library/Text_Highlighter/Text/Highlighter.php'); require_once('library/Text_Highlighter/Text/Highlighter/Renderer/Html.php'); -- cgit v1.2.3 From bc5f73e6c32cebd2add7a36173404208e7140af3 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 15 Jun 2016 20:51:55 -0700 Subject: provide a default for undefined languages --- include/bbcode.php | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/bbcode.php b/include/bbcode.php index 82fa33527..b720355af 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -495,6 +495,7 @@ function bb_highlight($match) { if(in_array(strtolower($match[1]),['php','css','mysql','sql','abap','diff','html','perl','ruby', 'vbscript','avrc','dtd','java','xml','cpp','python','javascript','js','sh'])) return text_highlight($match[2],strtolower($match[1])); + return $match[0]; } -- cgit v1.2.3 From d457f11717da40f44d98eeba5b88e6f1e5636247 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 15 Jun 2016 21:25:26 -0700 Subject: custom request methods for curl --- include/network.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/network.php b/include/network.php index 0dd10e29b..a595a03d1 100644 --- a/include/network.php +++ b/include/network.php @@ -21,15 +21,16 @@ function get_capath() { * TRUE if asked to return binary results (file download) * @param int $redirects default 0 * internal use, recursion counter - * @param array $opts (optional parameters) assoziative array with: + * @param array $opts (optional parameters) associative array with: * * \b accept_content => supply Accept: header with 'accept_content' as the value * * \b timeout => int seconds, default system config value or 60 seconds * * \b http_auth => username:password * * \b novalidate => do not validate SSL certs, default is to validate using our CA list * * \b nobody => only return the header * * \b filep => stream resource to write body to. header and body are not returned when using this option. + * * \b custom => custom request method: e.g. 'PUT', 'DELETE' * - * @return array an assoziative array with: + * @return array an associative array with: * * \e int \b return_code => HTTP return code or 0 if timeout or failure * * \e boolean \b success => boolean true (if HTTP 2xx result) or false * * \e string \b header => HTTP headers @@ -65,6 +66,9 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) { if(x($opts,'nobody')) @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']); + if(x($opts,'custom')) + @curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $opts['custom']); + if(x($opts,'timeout') && intval($opts['timeout'])) { @curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']); } @@ -165,7 +169,9 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) { * 'http_auth' => username:password * 'novalidate' => do not validate SSL certs, default is to validate using our CA list * 'filep' => stream resource to write body to. header and body are not returned when using this option. - * @return array an assoziative array with: + * 'custom' => custom request method: e.g. 'PUT', 'DELETE' + * + * @return array an associative array with: * * \e int \b return_code => HTTP return code or 0 if timeout or failure * * \e boolean \b success => boolean true (if HTTP 2xx result) or false * * \e string \b header => HTTP headers @@ -205,6 +211,10 @@ logger('headers: ' . print_r($opts['headers'],true) . 'redir: ' . $redirects); if(x($opts,'nobody')) @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']); + if(x($opts,'custom')) + @curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $opts['custom']); + + if(x($opts,'timeout') && intval($opts['timeout'])) { @curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']); } -- cgit v1.2.3