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 throw new Exception('Unknown page'); 61 } 62 $id = $ID; 63 } 64 $form = new Doku_Form('recommend_plugin', '?do=recommend'); 65 $form->addHidden('id', $id); 66 $form->startFieldset('Recommend page “' . hsc($id). '”'); 67 $form->addElement(form_makeTextField('r_name', $name, 'Recipient name')); 68 $form->addElement(form_makeTextField('r_email', $mail, 69 'Recipient email address')); 70 $form->addElement('<label><span>'.hsc('Additional comment').'</span>'. 71 '<textarea name="comment" rows="3" cols="10" ' . 72 'class="edit">' . $comment . '</textarea></label>'); 73 $form->addElement(form_makeButton('submit', '', 'Send recommendation')); 74 $form->addElement(form_makeButton('submit', 'cancel', 'Cancel')); 75 $form->printForm(); 76 } 77 78 function _handle_post() { 79 /* Validate input. */ 80 if (!isset($_POST['r_email']) || !mail_isvalid($_POST['r_email'])) { 81 return 'Invalid email address submitted'; 82 } 83 $email = $_POST['r_email']; 84 85 if (!isset($_POST['id']) || !page_exists($_POST['id'])) { 86 return 'Invalid page submitted'; 87 } 88 $page = $_POST['id']; 89 90 if (!isset($_POST['r_name']) || trim($_POST['r_name']) === '') { 91 return 'Invalid name submitted'; 92 } 93 $name = $_POST['r_name']; 94 95 $comment = isset($_POST['comment']) ? $_POST['comment'] : null; 96 97 /* Prepare mail text. */ 98 $mailtext = file_get_contents(dirname(__FILE__).'/template.txt'); 99 100 global $conf; 101 global $USERINFO; 102 foreach (array('NAME' => $name, 103 'PAGE' => $page, 104 'SITE' => $conf['title'], 105 'URL' => wl($page, '', true), 106 'COMMENT' => $comment, 107 'AUTHOR' => $USERINFO['name']) as $var => $val) { 108 $mailtext = str_replace('@' . $var . '@', $val, $mailtext); 109 } 110 /* Limit to two empty lines. */ 111 $mailtext = preg_replace('/\n{4,}/', "\n\n\n", $mailtext); 112 113 /* Perform stuff. */ 114 mail_send($email, 'Page recommendation', $mailtext); 115 $log = new Plugin_Recommend_Log(date('Y-m')); 116 $log->writeEntry($page, $USERINFO['mail'], $email); 117 return false; 118 } 119} 120