1<?php 2require_once DOKU_PLUGIN . 'action.php'; 3 4class action_plugin_recommend extends DokuWiki_Action_Plugin { 5 function getInfo(){ 6 return confToHash(dirname(__FILE__).'/INFO.txt'); 7 } 8 9 function register($controller) { 10 foreach(array('ACTION_ACT_PREPROCESS', 'TPL_ACT_UNKNOWN') as $event) { 11 $controller->register_hook($event, 'BEFORE', $this, '_handle'); 12 } 13 } 14 15 function _handle($event, $param) { 16 global $INFO; 17 if($event->data != 'recommend' || !isset($INFO['userinfo'])) { 18 return; 19 } 20 21 $event->preventDefault(); 22 23 if ($event->name === 'ACTION_ACT_PREPROCESS') { 24 return; 25 } 26 27 $event->stopPropagation(); 28 29 if($_SERVER['REQUEST_METHOD'] == 'POST' && $this->_handle_post()) { 30 return; 31 } 32 $this->_show_form(); 33 } 34 35 function _show_form() { 36 global $ID; 37 $name = isset($_REQUEST['r_name']) ? $_REQUEST['r_name'] : ''; 38 $mail = isset($_REQUEST['r_email']) ? $_REQUEST['r_email'] : ''; 39 $comment = isset($_REQUEST['comment']) ? $_REQUEST['r_comment'] : ''; 40 41 $form = new Doku_Form('recommend_plugin', '?do=recommend'); 42 $form->addHidden('id', $ID); 43 $form->startFieldset('Recommend page “' . hsc($ID). '”'); 44 $form->addElement(form_makeTextField('r_name',$name,'Recipient name')); 45 $form->addElement(form_makeTextField('r_email',$mail,'Recipient email address')); 46 $form->addElement('<label><span>'.hsc('Additional comment').'</span>'. 47 '<textarea name="comment" rows="3" cols="10" class="edit">' . 48 $comment . '</textarea></label>'); 49 $form->addElement(form_makeButton('submit','', 'Send recommendation')); 50 $form->printForm(); 51 } 52 53 function _handle_post() { 54 /* Validate input. */ 55 if(!isset($_POST['r_email']) || !mail_isvalid($_POST['r_email'])) { 56 msg('Invalid email address submitted', -1); 57 return false; 58 } 59 $email = $_POST['r_email']; 60 61 if(!isset($_POST['id']) || !page_exists($_POST['id'])) { 62 msg('Invalid page submitted', -1); 63 return false; 64 } 65 $page = $_POST['id']; 66 67 if(!isset($_POST['r_name']) || trim($_POST['r_name']) === '') { 68 msg('Invalid name submitted', -1); 69 return false; 70 } 71 $name = $_POST['r_name']; 72 73 $comment = isset($_POST['comment']) ? $_POST['comment'] : null; 74 75 /* Prepare mail text. */ 76 $mailtext = file_get_contents(dirname(__FILE__).'/template.txt'); 77 78 global $conf; 79 global $USERINFO; 80 foreach(array('NAME' => $name, 81 'PAGE' => $page, 82 'SITE' => $conf['title'], 83 'URL' => wl($page, '', true), 84 'COMMENT' => $comment, 85 'AUTHOR' => $USERINFO['name']) as $var => $val) { 86 $mailtext = str_replace('@' . $var . '@', $val, $mailtext); 87 } 88 /* Limit to two empty lines. */ 89 $mailtext = preg_replace('/\n{4,}/', "\n\n\n", $mailtext); 90 91 /* Perform stuff. */ 92 mail_send($email, 'Page recommendation', $mailtext); 93 $this->_log($USERINFO['mail'], $email); 94 echo 'Thanks for recommending our site.'; 95 return true; 96 } 97 98 function _log($sender, $receiver) { 99 global $ID; 100 $path = DOKU_INC.'data/cache/recommend'; 101 if (!file_exists($path)) { 102 mkdir($path); 103 } 104 file_put_contents($path . '/' . date('Y-m') . '.log', date('r') . ': ' . 105 "“${sender}” recommended “${ID}” to “${receiver}”.", 106 FILE_APPEND); 107 } 108} 109