xref: /plugin/recommend/action.php (revision 70f4a43540fdf426571ddb807d625a4c9a577649)
1<?php
2require_once DOKU_PLUGIN . 'action.php';
3require_once DOKU_INC . 'inc/form.php';
4
5class action_plugin_recommend extends DokuWiki_Action_Plugin {
6    function getInfo(){
7        return confToHash(dirname(__FILE__).'/INFO.txt');
8    }
9
10    function register($controller) {
11        foreach (array('ACTION_ACT_PREPROCESS', 'AJAX_CALL_UNKNOWN',
12                       'TPL_ACT_UNKNOWN') as $event) {
13            $controller->register_hook($event, 'BEFORE', $this, '_handle');
14        }
15    }
16
17    function _handle($event, $param) {
18        if (!in_array($event->data, array('recommend', 'plugin_recommend')) ||
19            !isset($_SERVER['REMOTE_USER'])) {
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        $name    = isset($_REQUEST['r_name']) ? $_REQUEST['r_name'] : '';
52        $mail    = isset($_REQUEST['r_email']) ? $_REQUEST['r_email'] : '';
53        $comment = isset($_REQUEST['comment']) ? $_REQUEST['r_comment'] : '';
54        if (isset($_REQUEST['id'])) {
55            $id  = $_REQUEST['id'];
56        } else {
57            global $ID;
58            if (!isset($ID)) {
59                throw new Exception('Unknown page');
60            }
61            $id  = $ID;
62        }
63        $form = new Doku_Form('recommend_plugin', '?do=recommend');
64        $form->addHidden('id', $id);
65        $form->startFieldset('Recommend page “' . hsc($id). '”');
66        $form->addElement(form_makeTextField('r_name', $name, 'Recipient name'));
67        $form->addElement(form_makeTextField('r_email', $mail,
68                                             'Recipient email address'));
69        $form->addElement('<label><span>'.hsc('Additional comment').'</span>'.
70                          '<textarea name="comment" rows="3" cols="10" ' .
71                          'class="edit">' . $comment . '</textarea></label>');
72        $form->addElement(form_makeButton('submit', '', 'Send recommendation'));
73        $form->printForm();
74    }
75
76    function _handle_post() {
77        /* Validate input. */
78        if (!isset($_POST['r_email']) || !mail_isvalid($_POST['r_email'])) {
79            return 'Invalid email address submitted';
80        }
81        $email = $_POST['r_email'];
82
83        if (!isset($_POST['id']) || !page_exists($_POST['id'])) {
84            return 'Invalid page submitted';
85        }
86        $page = $_POST['id'];
87
88        if (!isset($_POST['r_name']) || trim($_POST['r_name']) === '') {
89            return 'Invalid name submitted';
90        }
91        $name = $_POST['r_name'];
92
93        $comment = isset($_POST['comment']) ? $_POST['comment'] : null;
94
95        /* Prepare mail text. */
96        $mailtext = file_get_contents(dirname(__FILE__).'/template.txt');
97
98        global $conf;
99        global $USERINFO;
100        foreach (array('NAME' => $name,
101                       'PAGE' => $page,
102                       'SITE' => $conf['title'],
103                       'URL'  => wl($page, '', true),
104                       'COMMENT' => $comment,
105                       'AUTHOR' => $USERINFO['name']) as $var => $val) {
106            $mailtext = str_replace('@' . $var . '@', $val, $mailtext);
107        }
108        /* Limit to two empty lines. */
109        $mailtext = preg_replace('/\n{4,}/', "\n\n\n", $mailtext);
110
111        /* Perform stuff. */
112        mail_send($email, 'Page recommendation', $mailtext);
113        $this->_log($USERINFO['mail'], $email);
114        return false;
115    }
116
117    function _log($sender, $receiver) {
118        global $ID;
119        $path = DOKU_INC.'data/cache/recommend';
120        if (!file_exists($path)) {
121            mkdir($path);
122        }
123        file_put_contents($path . '/' . date('Y-m') . '.log', date('r') . ': ' .
124                          "“${sender}” recommended “${ID}” to “${receiver}”.",
125                          FILE_APPEND);
126    }
127}
128