1<?php
2require_once DOKU_PLUGIN . 'action.php';
3require_once DOKU_INC . 'inc/form.php';
4require_once dirname(__FILE__) . '/log.php';
5
6class action_plugin_recommend extends DokuWiki_Action_Plugin {
7    function getInfo(){
8        return confToHash(dirname(__FILE__).'/plugin.info.txt');
9    }
10
11    function register(Doku_Event_Handler $controller) {
12        foreach (array('ACTION_ACT_PREPROCESS', 'AJAX_CALL_UNKNOWN',
13                       'TPL_ACT_UNKNOWN') as $event) {
14            $controller->register_hook($event, 'BEFORE', $this, '_handle');
15        }
16    }
17
18    function _handle(&$event, $param) {
19        if (!in_array($event->data, array('recommend', 'plugin_recommend'))) {
20            return;
21        }
22
23        $event->preventDefault();
24
25        if ($event->name === 'ACTION_ACT_PREPROCESS') {
26            return;
27        }
28
29        $event->stopPropagation();
30
31        if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
32            isset($_POST['sectok']) &&
33            !($err = $this->_handle_post())) {
34            if ($event->name === 'AJAX_CALL_UNKNOWN') {
35                /* To signal success to AJAX. */
36                header('HTTP/1.1 204 No Content');
37                return;
38            }
39            echo 'Thanks for recommending our site.';
40            return;
41        }
42        /* To display msgs even via AJAX. */
43        echo ' ';
44        if (isset($err)) {
45            msg($err, -1);
46        }
47        $this->_show_form();
48    }
49
50    function _show_form() {
51        $r_name  = isset($_REQUEST['r_name']) ? $_REQUEST['r_name'] : '';
52        $r_email = isset($_REQUEST['r_email']) ? $_REQUEST['r_email'] : '';
53        $s_name  = isset($_REQUEST['s_name']) ? $_REQUEST['s_name'] : '';
54        $s_email = isset($_REQUEST['s_email']) ? $_REQUEST['s_email'] : '';
55        $comment = isset($_REQUEST['comment']) ? $_REQUEST['r_comment'] : '';
56        if (isset($_REQUEST['id'])) {
57            $id  = $_REQUEST['id'];
58        } else {
59            global $ID;
60            if (!isset($ID)) {
61                msg('Unknown page', -1);
62                return;
63            }
64            $id  = $ID;
65        }
66        $form = new Doku_Form('recommend_plugin', '?do=recommend');
67        $form->addHidden('id', $id);
68        $form->startFieldset('Recommend page “' . hsc($id). '”');
69        if (isset($_SERVER['REMOTE_USER'])) {
70            global $USERINFO;
71            $form->addHidden('s_name', $USERINFO['name']);
72            $form->addHidden('s_email', $USERINFO['mail']);
73        } else {
74            $form->addElement(form_makeTextField('s_name', $s_name, 'Your name'));
75            $form->addElement(form_makeTextField('s_email', $s_email,
76                                                 'Your email address'));
77        }
78        $form->addElement(form_makeTextField('r_name', $r_name, 'Recipient name'));
79        $form->addElement(form_makeTextField('r_email', $r_email,
80                                             'Recipient email address'));
81        $form->addElement('<label><span>'.hsc('Additional comment').'</span>'.
82                          '<textarea name="comment" rows="3" cols="10" ' .
83                          'class="edit">' . $comment . '</textarea></label>');
84        $helper = null;
85        if(@is_dir(DOKU_PLUGIN.'captcha')) $helper = plugin_load('helper','captcha');
86        if(!is_null($helper) && $helper->isEnabled()){
87            $form->addElement($helper->getHTML());
88        }
89
90        $form->addElement(form_makeButton('submit', '', 'Send recommendation'));
91        $form->addElement(form_makeButton('submit', 'cancel', 'Cancel'));
92        $form->printForm();
93    }
94
95    function _handle_post() {
96        $helper = null;
97        if(@is_dir(DOKU_PLUGIN.'captcha')) $helper = plugin_load('helper','captcha');
98        if(!is_null($helper) && $helper->isEnabled() && !$helper->check()) {
99            return 'Wrong captcha';
100        }
101
102        /* Validate input. */
103        if (!isset($_POST['r_email']) || !mail_isvalid($_POST['r_email'])) {
104            return 'Invalid recipient email address submitted';
105        }
106        if (!isset($_POST['r_name']) || trim($_POST['r_name']) === '') {
107            return 'Invalid recipient name submitted';
108        }
109        $r_name    = $_POST['r_name'];
110        $recipient = $r_name . ' <' . $_POST['r_email'] . '>';
111
112        if (!isset($_POST['s_email']) || !mail_isvalid($_POST['s_email'])) {
113            return 'Invalid sender email address submitted';
114        }
115        if (!isset($_POST['s_name']) || trim($_POST['s_name']) === '') {
116            return 'Invalid sender name submitted';
117        }
118        $s_name = $_POST['s_name'];
119        $sender = $s_name . ' <' . $_POST['s_email'] . '>';
120
121        if (!isset($_POST['id']) || !page_exists($_POST['id'])) {
122            return 'Invalid page submitted';
123        }
124        $page = $_POST['id'];
125
126        $comment = isset($_POST['comment']) ? $_POST['comment'] : null;
127
128        /* Prepare mail text. */
129        $mailtext = file_get_contents(dirname(__FILE__).'/template.txt');
130
131        global $conf;
132        global $USERINFO;
133        foreach (array('NAME' => $r_name,
134                       'PAGE' => $page,
135                       'SITE' => $conf['title'],
136                       'URL'  => wl($page, '', true),
137                       'COMMENT' => $comment,
138                       'AUTHOR' => $s_name) as $var => $val) {
139            $mailtext = str_replace('@' . $var . '@', $val, $mailtext);
140        }
141        /* Limit to two empty lines. */
142        $mailtext = preg_replace('/\n{4,}/', "\n\n\n", $mailtext);
143
144        /* Perform stuff. */
145        mail_send($recipient, 'Page recommendation', $mailtext, $sender);
146        $log = new Plugin_Recommend_Log(date('Y-m'));
147        $log->writeEntry($page, $sender, $recipient, $comment);
148        return false;
149    }
150}
151