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->addElement(form_makeButton('submit', 'cancel', 'Cancel')); 74 $form->printForm(); 75 } 76 77 function _handle_post() { 78 /* Validate input. */ 79 if (!isset($_POST['r_email']) || !mail_isvalid($_POST['r_email'])) { 80 return 'Invalid email address submitted'; 81 } 82 $email = $_POST['r_email']; 83 84 if (!isset($_POST['id']) || !page_exists($_POST['id'])) { 85 return 'Invalid page submitted'; 86 } 87 $page = $_POST['id']; 88 89 if (!isset($_POST['r_name']) || trim($_POST['r_name']) === '') { 90 return 'Invalid name submitted'; 91 } 92 $name = $_POST['r_name']; 93 94 $comment = isset($_POST['comment']) ? $_POST['comment'] : null; 95 96 /* Prepare mail text. */ 97 $mailtext = file_get_contents(dirname(__FILE__).'/template.txt'); 98 99 global $conf; 100 global $USERINFO; 101 foreach (array('NAME' => $name, 102 'PAGE' => $page, 103 'SITE' => $conf['title'], 104 'URL' => wl($page, '', true), 105 'COMMENT' => $comment, 106 'AUTHOR' => $USERINFO['name']) as $var => $val) { 107 $mailtext = str_replace('@' . $var . '@', $val, $mailtext); 108 } 109 /* Limit to two empty lines. */ 110 $mailtext = preg_replace('/\n{4,}/', "\n\n\n", $mailtext); 111 112 /* Perform stuff. */ 113 mail_send($email, 'Page recommendation', $mailtext); 114 $this->_log($USERINFO['mail'], $email); 115 return false; 116 } 117 118 function _log($sender, $receiver) { 119 global $ID; 120 $path = DOKU_INC.'data/cache/recommend'; 121 if (!file_exists($path)) { 122 mkdir($path); 123 } 124 file_put_contents($path . '/' . date('Y-m') . '.log', date('r') . ': ' . 125 "“${sender}” recommended “${ID}” to “${receiver}”.", 126 FILE_APPEND); 127 } 128} 129