1<?php 2/** 3 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 4 * @author Michael Klier <chi@chimeric.de> 5 */ 6 7use dokuwiki\plugin\blogtng\entities\Comment; 8 9/** 10 * Class action_plugin_blogtng_comments 11 */ 12class action_plugin_blogtng_comments extends DokuWiki_Action_Plugin{ 13 14 /** @var helper_plugin_blogtng_comments */ 15 var $commenthelper = null; 16 /** @var helper_plugin_blogtng_tools */ 17 var $tools = null; 18 19 /** 20 * Constructor 21 */ 22 function __construct() { 23 $this->commenthelper = plugin_load('helper', 'blogtng_comments'); 24 $this->tools = plugin_load('helper', 'blogtng_tools'); 25 26 } 27 28 /** 29 * Registers a callback function for a given event 30 * 31 * @param Doku_Event_Handler $controller 32 */ 33 function register(Doku_Event_Handler $controller) { 34 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleCommentSaveAndSubscribeActions', array()); 35 } 36 37 /** 38 * Handle the preprocess event 39 * 40 * Takes care of handling all the post input from creating 41 * comments and saves them. Also handles optin and unsubscribe 42 * actions. 43 * 44 * @param Doku_Event $event event object by reference 45 * @param array $param empty array as passed to register_hook() 46 * @return bool 47 */ 48 function handleCommentSaveAndSubscribeActions(Doku_Event $event, $param) { 49 global $INFO, $ID, $INPUT, $BLOGTNG; 50 $BLOGTNG = []; 51 52 // optin 53 if ($INPUT->has('btngo')) { 54 $this->commenthelper->optin($INPUT->str('btngo')); 55 } 56 57 // unsubscribe 58 if ($INPUT->has('btngu')) { 59 $this->commenthelper->unsubscribe_by_key(md5($ID), $INPUT->str('btngu')); 60 } 61 62 63 $comment = new Comment(); 64 65 // prepare data for comment form 66 $comment->setSource($INPUT->post->str('comment-source')); //from: comment, pingback or trackback 67 $name = $INPUT->post->str('comment-name'); 68 $comment->setName($name ?: $INFO['userinfo']['name'] ?? ''); 69 $mail = $INPUT->post->str('comment-mail'); 70 $comment->setMail($mail ?: $INFO['userinfo']['mail'] ?? ''); 71 $web = $INPUT->post->str('comment-web'); 72 //add "http(s)://" to website 73 if ($web != '' && !preg_match('/^http/', $web)) { 74 $web = 'https://' . $web; 75 } 76 $comment->setWeb($web); 77 if($INPUT->post->has('wikitext')) { 78 $text = cleanText($INPUT->post->str('wikitext')); 79 } else { 80 $text = null; 81 } 82 $comment->setText($text); 83 $comment->setPid($INPUT->post->has('pid') ? $INPUT->post->str('pid') : null); 84// $comment->setPage(isset($_REQUEST['id']) ? $_REQUEST['id'] : null); FIXME seems to be not used...id general 85 $comment->setSubscribe($INPUT->post->has('comment-subscribe') ? 1 : null); 86 $comment->setIp(clientIP(true)); 87 88 // store data for helper::tpl_form() 89 $BLOGTNG['comment'] = $comment; 90 91 $action = act_clean($event->data); 92 if($action == 'comment_submit' || $action == 'comment_preview') { 93 94 if($action == 'comment_submit') { 95 $BLOGTNG['comment_action'] = 'submit'; 96 } else { 97 $BLOGTNG['comment_action'] = 'preview'; 98 } 99 100 // check for empty fields 101 $BLOGTNG['comment_submit_errors'] = []; 102 foreach(array('name', 'mail', 'text') as $field) { 103 $functionname = "get{$field}"; 104 if(empty($comment->$functionname())) { 105 $BLOGTNG['comment_submit_errors'][$field] = true; 106 } elseif($field == 'mail' && !mail_isvalid($comment->getMail())) { 107 $BLOGTNG['comment_submit_errors'][$field] = true; 108 } 109 } 110 111 // check CAPTCHA if available (on submit only) 112 $captchaok = true; 113 if($BLOGTNG['comment_action'] == 'submit'){ 114 /** @var helper_plugin_captcha $captcha */ 115 $captcha = $this->loadHelper('captcha', false); 116 if ($captcha && $captcha->isEnabled()) { 117 $captchaok = $captcha->check(); 118 } 119 } 120 121 // return to form on errors or if preview 122 if(!empty($BLOGTNG['comment_submit_errors']) || !$captchaok || $BLOGTNG['comment_action'] == 'preview') { 123 $event->data = 'show'; 124 $_SERVER['REQUEST_METHOD'] = 'get'; //hack to avoid redirect 125 return false; 126 } 127 128 // successful submit: save comment and redirect FIXME cid 129 $this->commenthelper->save($comment); 130 $event->data = 'redirect'; 131 return false; 132 } else { 133 return true; 134 } 135 } 136} 137