1<?php 2 3class action_plugin_recommend extends DokuWiki_Action_Plugin { 4 5 public function register(Doku_Event_Handler $controller) { 6 foreach (array('ACTION_ACT_PREPROCESS', 'AJAX_CALL_UNKNOWN', 7 'TPL_ACT_UNKNOWN') as $event) { 8 $controller->register_hook($event, 'BEFORE', $this, 'handle'); 9 } 10 $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'handleMenu'); 11 } 12 13 public function handle(Doku_Event $event) { 14 if ($event->data !=='recommend') { 15 return; 16 } 17 18 $event->preventDefault(); 19 20 if ($event->name === 'ACTION_ACT_PREPROCESS') { 21 return; 22 } 23 24 $event->stopPropagation(); 25 26 global $INPUT; 27 28 // early output to trigger display msgs even via AJAX. 29 echo ' '; 30 tpl_flush(); 31 if ($INPUT->server->str('REQUEST_METHOD') === 'POST') { 32 try { 33 $this->handlePost(); 34 if ($event->name === 'AJAX_CALL_UNKNOWN') { 35 $this->ajaxSuccess(); // To signal success to AJAX. 36 } else { 37 msg($this->getLang('thanks'), 1); 38 } 39 return; // we're done here 40 } catch (\Exception $e) { 41 msg($e->getMessage(), -1); 42 } 43 } 44 45 echo $this->getForm(); 46 } 47 48 /** 49 * Page menu item 50 * 51 * @param Doku_Event $event 52 * @return void 53 */ 54 public function handleMenu(Doku_Event $event) 55 { 56 if ($event->data['view'] !== 'page') return; 57 58 array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\recommend\MenuItem()]); 59 } 60 61 /** 62 * Returns rendered form 63 * 64 * @return string 65 */ 66 protected function getForm() 67 { 68 global $INPUT; 69 70 $id = getID(); // we may run in AJAX context 71 if ($id === '') throw new \RuntimeException('No ID given'); 72 73 $form = new \dokuwiki\Form\Form([ 74 'action' => wl($id, ['do' => 'recommend'], false, '&'), 75 'id' => 'plugin__recommend', 76 ]); 77 $form->setHiddenField('id', $id); // we need it for the ajax call 78 79 /** @var helper_plugin_recommend_assignment $helper */ 80 $helper = plugin_load('helper', 'recommend_assignment'); 81 $template = $helper->loadMatchingTemplate(); 82 83 if ($INPUT->server->has('REMOTE_USER')) { 84 global $USERINFO; 85 $form->setHiddenField('s_name', $USERINFO['name']); 86 $form->setHiddenField('s_email', $USERINFO['mail']); 87 } else { 88 $form->addTextInput('s_name', $this->getLang('yourname'))->addClass('edit'); 89 $form->addTextInput('s_email', $this->getLang('youremailaddress'))->addClass('edit'); 90 } 91 92 $recipientEmails = $template['user'] ?? ''; 93 $message = $template['message'] ?? ''; 94 $form->addTextInput('r_email', $this->getLang('recipients'))->addClass('edit')->val($recipientEmails); 95 $form->addTextInput('subject', $this->getLang('subject'))->addClass('edit'); 96 $form->addTextarea('comment', $this->getLang('message')) 97 ->attr('rows', '8') 98 ->attr('cols', '40') 99 ->addClass('edit') 100 ->val($message); 101 102 /** @var helper_plugin_captcha $captcha */ 103 $captcha = plugin_load('helper', 'captcha'); 104 if ($captcha) $form->addHTML($captcha->getHTML()); 105 106 $form->addTagOpen('div')->addClass('buttons'); 107 $form->addButton('submit', $this->getLang('send'))->attr('type', 'submit'); 108 $form->addTagClose('div'); 109 110 return $form->toHTML(); 111 } 112 113 /** 114 * Handles form submission 115 * 116 * @throws Exception 117 */ 118 protected function handlePost() 119 { 120 global $INPUT; 121 122 if (!checkSecurityToken()) { 123 throw new \Exception('Security token did not match'); 124 } 125 126 /** @var helper_plugin_recommend_mail $mailHelper */ 127 $mailHelper = plugin_load('helper', 'recommend_mail'); 128 129 // Captcha plugin 130 $captcha = null; 131 if (@is_dir(DOKU_PLUGIN . 'captcha')) $captcha = plugin_load('helper','captcha'); 132 if (!is_null($captcha) && $captcha->isEnabled() && !$captcha->check()) { 133 throw new \Exception($this->getLang('err_captcha')); 134 } 135 136 /* Validate input */ 137 $recipients = $INPUT->str('r_email'); 138 139 if (empty($recipients)) { 140 throw new \Exception($this->getLang('err_recipient')); 141 } 142 143 $recipients = $mailHelper->resolveRecipients($recipients); 144 $recipients = implode(',', $recipients); 145 146 if (!isset($_POST['s_email']) || !mail_isvalid($_POST['s_email'])) { 147 throw new \Exception($this->getLang('err_sendermail')); 148 } 149 if (!isset($_POST['s_name']) || trim($_POST['s_name']) === '') { 150 throw new \Exception($this->getLang('err_sendername')); 151 } 152 $s_name = $_POST['s_name']; 153 $sender = $s_name . ' <' . $_POST['s_email'] . '>'; 154 155 $id = $INPUT->filter('cleanID')->str('id'); 156 if ($id === '' || !page_exists($id)) throw new \Exception($this->getLang('err_page')); 157 158 $comment = $INPUT->str('comment'); 159 160 /* Prepare mail text */ 161 $mailtext = file_get_contents($this->localFN('template')); 162 163 global $conf; 164 foreach (array('PAGE' => $id, 165 'SITE' => $conf['title'], 166 'URL' => wl($id, '', true), 167 'COMMENT' => $comment, 168 'AUTHOR' => $s_name) as $var => $val) { 169 $mailtext = str_replace('@' . $var . '@', $val, $mailtext); 170 } 171 /* Limit to two empty lines. */ 172 $mailtext = preg_replace('/\n{4,}/', "\n\n\n", $mailtext); 173 174 $mailHelper->sendMail($recipients, $mailtext, $sender); 175 176 /** @var helper_plugin_recommend_log $log */ 177 $log = new helper_plugin_recommend_log(date('Y-m')); 178 $log->writeEntry($id, $sender, $recipients, $comment); 179 } 180 181 /** 182 * show success message in ajax mode 183 */ 184 protected function ajaxSuccess() 185 { 186 echo '<form id="plugin__recommend" accept-charset="utf-8" method="post" action="?do=recommend">'; 187 echo '<div class="no">'; 188 echo '<span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 50px 0;"></span>'; 189 echo '<p>' . $this->getLang('done') . '</p>'; 190 echo '<button type="reset" class="button">' . $this->getLang('close') . '</button>'; 191 echo '</div>'; 192 echo '</form>'; 193 } 194} 195