xref: /plugin/recommend/action.php (revision e596e22bcda05faed2f8741d85ffbd96e92c6451)
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__).'/INFO.txt');
9    }
10
11    function register(&$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        $r_email = $_POST['r_email'];
107
108        if (!isset($_POST['s_email']) || !mail_isvalid($_POST['s_email'])) {
109            return 'Invalid sender email address submitted';
110        }
111        $s_email = $_POST['s_email'];
112
113        if (!isset($_POST['id']) || !page_exists($_POST['id'])) {
114            return 'Invalid page submitted';
115        }
116        $page = $_POST['id'];
117
118        if (!isset($_POST['r_name']) || trim($_POST['r_name']) === '') {
119            return 'Invalid recipient name submitted';
120        }
121        $r_name = $_POST['r_name'];
122
123        if (!isset($_POST['s_name']) || trim($_POST['s_name']) === '') {
124            return 'Invalid sender name submitted';
125        }
126        $s_name = $_POST['s_name'];
127
128        $comment = isset($_POST['comment']) ? $_POST['comment'] : null;
129
130        /* Prepare mail text. */
131        $mailtext = file_get_contents(dirname(__FILE__).'/template.txt');
132
133        global $conf;
134        global $USERINFO;
135        foreach (array('NAME' => $r_name,
136                       'PAGE' => $page,
137                       'SITE' => $conf['title'],
138                       'URL'  => wl($page, '', true),
139                       'COMMENT' => $comment,
140                       'AUTHOR' => "$s_name <$s_email>") as $var => $val) {
141            $mailtext = str_replace('@' . $var . '@', $val, $mailtext);
142        }
143        /* Limit to two empty lines. */
144        $mailtext = preg_replace('/\n{4,}/', "\n\n\n", $mailtext);
145
146        /* Perform stuff. */
147        mail_send($r_email, 'Page recommendation', $mailtext);
148        $log = new Plugin_Recommend_Log(date('Y-m'));
149        $log->writeEntry($page, "$s_name <$s_email>", "$r_name <$r_email>");
150        return false;
151    }
152}
153