Skip to content

Commit

Permalink
Merge pull request #132 from dokufreaks/update
Browse files Browse the repository at this point in the history
Update of the blogtng
  • Loading branch information
Klap-in committed Feb 16, 2023
2 parents 52ff4f7 + 293f772 commit c0a490f
Show file tree
Hide file tree
Showing 33 changed files with 1,446 additions and 1,100 deletions.
47 changes: 22 additions & 25 deletions action/ajax.php
Expand Up @@ -4,8 +4,7 @@
* @author Andreas Gohr <gohr@cosmocode.de>
*/

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
use dokuwiki\plugin\blogtng\entities\Comment;

/**
* Class action_plugin_blogtng_ajax
Expand All @@ -17,43 +16,41 @@ class action_plugin_blogtng_ajax extends DokuWiki_Action_Plugin{
*
* @param Doku_Event_Handler $controller
*/
function register(Doku_Event_Handler $controller) {
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call', array());
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'renderCommentPreview', array());
}

/**
* Callback function for event 'AJAX_CALL_UNKNOWN'.
*
* Callback function for event 'AJAX_CALL_UNKNOWN' to return a rendered preview of a comment
* (which will be shown below the comment input field)
*
* @param Doku_Event $event event object by reference
* @param array $param empty array as passed to register_hook()
*/
function handle_ajax_call(Doku_Event $event, $param) {
public function renderCommentPreview(Doku_Event $event, $param) {
/** @var DokuWiki_Auth_Plugin $auth */
global $auth;
global $auth, $INPUT;

if($event->data != 'blogtng__comment_preview') return;
$event->preventDefault();
$event->stopPropagation();

require_once DOKU_PLUGIN . 'blogtng/helper/comments.php';
$comment = new blogtng_comment();

$comment->data['text'] = $_REQUEST['text'];
$comment->data['name'] = $_REQUEST['name'];
$comment->data['mail'] = $_REQUEST['mail'];
$comment->data['web'] = isset($_REQUEST['web']) ? $_REQUEST['web'] : '';
$comment->data['cid'] = 'preview';
$comment->data['created'] = time();
$comment->data['status'] = 'visible';

if(!$comment->data['name'] && $_SERVER['REMOTE_USER']){
if($auth AND $info = $auth->getUserData($_SERVER['REMOTE_USER'])) {
$comment->data['name'] = $info['name'];
$comment->data['mail'] = $info['mail'];
$comment = new Comment();
$comment->setText($INPUT->post->str('text'));
$comment->setName($INPUT->post->str('name'));
$comment->setMail($INPUT->post->str('mail'));
$comment->setWeb($INPUT->post->str('web'));
$comment->setCid('preview');
$comment->setCreated(time());
$comment->setStatus('visible');

if(!$comment->getName() && $INPUT->server->str('REMOTE_USER')){
if($auth AND $info = $auth->getUserData($INPUT->server->str('REMOTE_USER'))) {
$comment->setName($info['name']);
$comment->setMail($info['mail']);
}
}

$comment->output($_REQUEST['tplname']);
$comment->output($INPUT->post->str('tplname'));
}
}
// vim:ts=4:sw=4:et:
94 changes: 48 additions & 46 deletions action/comments.php
Expand Up @@ -4,8 +4,7 @@
* @author Michael Klier <chi@chimeric.de>
*/

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
use dokuwiki\plugin\blogtng\entities\Comment;

/**
* Class action_plugin_blogtng_comments
Expand All @@ -32,7 +31,7 @@ function __construct() {
* @param Doku_Event_Handler $controller
*/
function register(Doku_Event_Handler $controller) {
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_act_preprocess', array());
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleCommentSaveAndSubscribeActions', array());
}

/**
Expand All @@ -46,89 +45,92 @@ function register(Doku_Event_Handler $controller) {
* @param array $param empty array as passed to register_hook()
* @return bool
*/
function handle_act_preprocess(Doku_Event $event, $param) {
global $INFO, $ID;
function handleCommentSaveAndSubscribeActions(Doku_Event $event, $param) {
global $INFO, $ID, $INPUT, $BLOGTNG;
$BLOGTNG = [];

// optin
if (isset($_REQUEST['btngo'])) {
$this->commenthelper->optin($_REQUEST['btngo']);
if ($INPUT->has('btngo')) {
$this->commenthelper->optin($INPUT->str('btngo'));
}

// unsubscribe
if (isset($_REQUEST['btngu'])) {
$this->commenthelper->unsubscribe_by_key(md5($ID), $_REQUEST['btngu']);
if ($INPUT->has('btngu')) {
$this->commenthelper->unsubscribe_by_key(md5($ID), $INPUT->str('btngu'));
}

global $BLOGTNG;
$BLOGTNG = array();

// prepare data for comment form
$comment = array();
$comment['source'] = $this->tools->getParam('comment/source');
$comment['name'] = (($commentname = $this->tools->getParam('comment/name'))) ? $commentname : $INFO['userinfo']['name'];
$comment['mail'] = (($commentmail = $this->tools->getParam('comment/mail'))) ? $commentmail : $INFO['userinfo']['mail'];
$comment['web'] = (($commentweb = $this->tools->getParam('comment/web'))) ? $commentweb : '';
$comment['text'] = isset($_REQUEST['wikitext']) ? cleanText($_REQUEST['wikitext']) : null;
$comment['pid'] = isset($_REQUEST['pid']) ? $_REQUEST['pid'] : null;
$comment['page'] = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
$comment['subscribe'] = isset($_REQUEST['blogtng']['subscribe']) ? $_REQUEST['blogtng']['subscribe'] : null;
$comment['ip'] = clientIP(true);
$comment = new Comment();

// prepare data for comment form
$comment->setSource($INPUT->post->str('comment-source')); //from: comment, pingback or trackback
$name = $INPUT->post->str('comment-name');
$comment->setName($name ?: $INFO['userinfo']['name'] ?? '');
$mail = $INPUT->post->str('comment-mail');
$comment->setMail($mail ?: $INFO['userinfo']['mail'] ?? '');
$web = $INPUT->post->str('comment-web');
//add "http(s)://" to website
if (!preg_match('/^http/',$comment['web']) && $comment['web'] != '') {
$comment['web'] = 'http://'.$comment['web'];
if ($web != '' && !preg_match('/^http/', $web)) {
$web = 'https://' . $web;
}
$comment->setWeb($web);
if($INPUT->post->has('wikitext')) {
$text = cleanText($INPUT->post->str('wikitext'));
} else {
$text = null;
}
$comment->setText($text);
$comment->setPid($INPUT->post->has('pid') ? $INPUT->post->str('pid') : null);
// $comment->setPage(isset($_REQUEST['id']) ? $_REQUEST['id'] : null); FIXME seems to be not used...id general
$comment->setSubscribe($INPUT->post->has('comment-subscribe') ? 1 : null);
$comment->setIp(clientIP(true));

// store data for helper::tpl_form()
$BLOGTNG['comment'] = $comment;

$action = act_clean($event->data);
if($action == 'comment_submit' || $action == 'comment_preview') {

if($action == 'comment_submit') {
$BLOGTNG['comment_action'] = 'submit';
}
else if($action == 'comment_preview') {
} else {
$BLOGTNG['comment_action'] = 'preview';
}

// check for empty fields
$BLOGTNG['comment_submit_errors'] = array();
$BLOGTNG['comment_submit_errors'] = [];
foreach(array('name', 'mail', 'text') as $field) {
if(empty($comment[$field])) {
$functionname = "get{$field}";
if(empty($comment->$functionname())) {
$BLOGTNG['comment_submit_errors'][$field] = true;
} elseif($field == 'mail' && !mail_isvalid($comment->getMail())) {
$BLOGTNG['comment_submit_errors'][$field] = true;
}
}

// check CAPTCHA if available (on submit only)
$captchaok = true;
if($BLOGTNG['comment_action'] == 'submit'){
/** @var helper_plugin_captcha $helper */
$helper = null;
if(@is_dir(DOKU_PLUGIN.'captcha')) $helper = plugin_load('helper','captcha');
if(!is_null($helper) && $helper->isEnabled()){
$captchaok = $helper->check();
/** @var helper_plugin_captcha $captcha */
$captcha = $this->loadHelper('captcha', false);
if ($captcha && $captcha->isEnabled()) {
$captchaok = $captcha->check();
}
}

// return on errors
if(!empty($BLOGTNG['comment_submit_errors']) || !$captchaok) {
// return to form on errors or if preview
if(!empty($BLOGTNG['comment_submit_errors']) || !$captchaok || $BLOGTNG['comment_action'] == 'preview') {
$event->data = 'show';
$_SERVER['REQUEST_METHOD'] = 'get'; //hack to avoid redirect
return false;
}

if($BLOGTNG['comment_action'] == 'submit') {
// save comment and redirect FIXME cid
$this->commenthelper->save($comment);
$event->data = 'redirect';
return false;
} elseif($BLOGTNG['comment_action'] == 'preview') {
$event->data = 'show';
$_SERVER['REQUEST_METHOD'] = 'get'; // hack to avoid redirect
return false;
}
// successful submit: save comment and redirect FIXME cid
$this->commenthelper->save($comment);
$event->data = 'redirect';
return false;
} else {
return true;
}
}
}
// vim:ts=4:sw=4:et:

0 comments on commit c0a490f

Please sign in to comment.