xref: /plugin/recommend/action.php (revision 85a03255f70b8ca8d24721d8e993ac07fff81252)
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            !isset($_SERVER['REMOTE_USER'])) {
21            return;
22        }
23
24        $event->preventDefault();
25
26        if ($event->name === 'ACTION_ACT_PREPROCESS') {
27            return;
28        }
29
30        $event->stopPropagation();
31
32        if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
33            isset($_POST['sectok']) &&
34            !($err = $this->_handle_post())) {
35            if ($event->name === 'AJAX_CALL_UNKNOWN') {
36                /* To signal success to AJAX. */
37                header('HTTP/1.1 204 No Content');
38                return;
39            }
40            echo 'Thanks for recommending our site.';
41            return;
42        }
43        /* To display msgs even via AJAX. */
44        echo ' ';
45        if (isset($err)) {
46            msg($err, -1);
47        }
48        $this->_show_form();
49    }
50
51    function _show_form() {
52        $name    = isset($_REQUEST['r_name']) ? $_REQUEST['r_name'] : '';
53        $mail    = isset($_REQUEST['r_email']) ? $_REQUEST['r_email'] : '';
54        $comment = isset($_REQUEST['comment']) ? $_REQUEST['r_comment'] : '';
55        if (isset($_REQUEST['id'])) {
56            $id  = $_REQUEST['id'];
57        } else {
58            global $ID;
59            if (!isset($ID)) {
60                msg('Unknown page', -1);
61                return;
62            }
63            $id  = $ID;
64        }
65        $form = new Doku_Form('recommend_plugin', '?do=recommend');
66        $form->addHidden('id', $id);
67        $form->startFieldset('Recommend page “' . hsc($id). '”');
68        $form->addElement(form_makeTextField('r_name', $name, 'Recipient name'));
69        $form->addElement(form_makeTextField('r_email', $mail,
70                                             'Recipient email address'));
71        $form->addElement('<label><span>'.hsc('Additional comment').'</span>'.
72                          '<textarea name="comment" rows="3" cols="10" ' .
73                          'class="edit">' . $comment . '</textarea></label>');
74        $form->addElement(form_makeButton('submit', '', 'Send recommendation'));
75        $form->addElement(form_makeButton('submit', 'cancel', 'Cancel'));
76        $form->printForm();
77    }
78
79    function _handle_post() {
80        /* Validate input. */
81        if (!isset($_POST['r_email']) || !mail_isvalid($_POST['r_email'])) {
82            return 'Invalid email address submitted';
83        }
84        $email = $_POST['r_email'];
85
86        if (!isset($_POST['id']) || !page_exists($_POST['id'])) {
87            return 'Invalid page submitted';
88        }
89        $page = $_POST['id'];
90
91        if (!isset($_POST['r_name']) || trim($_POST['r_name']) === '') {
92            return 'Invalid name submitted';
93        }
94        $name = $_POST['r_name'];
95
96        $comment = isset($_POST['comment']) ? $_POST['comment'] : null;
97
98        /* Prepare mail text. */
99        $mailtext = file_get_contents(dirname(__FILE__).'/template.txt');
100
101        global $conf;
102        global $USERINFO;
103        foreach (array('NAME' => $name,
104                       'PAGE' => $page,
105                       'SITE' => $conf['title'],
106                       'URL'  => wl($page, '', true),
107                       'COMMENT' => $comment,
108                       'AUTHOR' => $USERINFO['name']) as $var => $val) {
109            $mailtext = str_replace('@' . $var . '@', $val, $mailtext);
110        }
111        /* Limit to two empty lines. */
112        $mailtext = preg_replace('/\n{4,}/', "\n\n\n", $mailtext);
113
114        /* Perform stuff. */
115        mail_send($email, 'Page recommendation', $mailtext);
116        $log = new Plugin_Recommend_Log(date('Y-m'));
117        $log->writeEntry($page, $USERINFO['mail'], $email);
118        return false;
119    }
120}
121