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