/** * @param $input JitFilter * @return array * @throws Exception * @throws Services_Exception * @throws Services_Exception_BadRequest * @throws Services_Exception_Denied * @throws Services_Exception_Disabled * @throws Services_Exception_NotFound */ function action_email_wikipage($input) { Services_Exception_Disabled::check('feature_wiki'); Services_Exception_Denied::checkGlobal('admin_users'); $check = Services_Exception_BadRequest::checkAccess(); //first pass - show confirm popup if (!empty($check['ticket'])) { $users = $input->asArray('checked'); if (count($users) > 0) { //provide redirect if js is not enabled $referer = Services_Utilities_Controller::noJsPath(); return ['title' => tra('Send wiki page content by email to selected users'), 'confirmAction' => $input->action->word(), 'confirmController' => 'user', 'customMsg' => tra('For these selected users:'), 'items' => $users, 'extra' => ['referer' => $referer], 'ticket' => $check['ticket'], 'modal' => '1', 'confirm' => 'y']; } else { throw new Services_Exception(tra('No users were selected. Please select one or more users.'), 409); } //after confirm submit - perform action and return success feedback } elseif ($check === true && $_SERVER['REQUEST_METHOD'] === 'POST') { $wikiTpl = $input['wikiTpl']; $tikilib = TikiLib::lib('tiki'); $pageinfo = $tikilib->get_page_info($wikiTpl); if (!$pageinfo) { throw new Services_Exception_NotFound(tra('Page not found')); } if (empty($pageinfo['description'])) { throw new Services_Exception(tra('The page does not have a description, which is mandatory to perform this action.')); } $bcc = $input['bcc']; include_once 'lib/webmail/tikimaillib.php'; $mail = new TikiMail(); if (!empty($bcc)) { if (!validate_email($bcc)) { throw new Services_Exception(tra('Invalid bcc email address.')); } $mail->setBcc($bcc); $bccmsg = tr('and blind copied (bcc) to %0', $bcc); } $foo = parse_url($_SERVER['REQUEST_URI']); $machine = $tikilib->httpPrefix(true) . dirname($foo['path']); $machine = preg_replace('!/$!', '', $machine); // just in case global $smarty, $user; $smarty->assign_by_ref('mail_machine', $machine); $users = json_decode($input['items'], true); $logslib = TikiLib::lib('logs'); foreach ($users as $mail_user) { $smarty->assign_by_ref('user', $mail_user); $mail->setUser($mail_user); $mail->setSubject($pageinfo['description']); $text = $smarty->fetch('wiki:' . $wikiTpl); if (empty($text)) { throw new Services_Exception(tra('The template page has no text or the text cannot be extracted.')); } $mail->setHtml($text); if (!$mail->send($this->lib->get_user_email($mail_user))) { $errormsg = tra('Unable to send mail'); if (Perms::get()->admin) { $mailerrors = print_r($mail->errors, true); $errormsg .= $mailerrors; } throw new Services_Exception($errormsg); } else { if (!empty($bcc)) { $logmsg = sprintf(tra('Mail sent to user %s'), $mail_user); } $logmsg = !empty($bccmsg) ? $logmsg . ' ' . $bccmsg : $logmsg; if (!empty($msg)) { $logslib->add_log('adminusers', $logmsg, $user); } } $smarty->assign_by_ref('user', $user); } //return to page //if javascript is not enabled $extra = json_decode($input['extra'], true); if (!empty($extra['referer'])) { $this->access->redirect($extra['referer'], tra('Page sent'), null, 'feedback'); } $msg = count($users) === 1 ? tr('The page %0 has been emailed to the following user:'******'The page %0 has been emailed to the following users:', $wikiTpl); $toMsg = !empty($bcc) ? tr('And blind copied to %0.', $bcc) : ''; return ['extra' => 'post', 'feedback' => ['ajaxtype' => 'feedback', 'ajaxheading' => tra('Success'), 'ajaxitems' => $users, 'ajaxmsg' => $msg, 'ajaxtoMsg' => $toMsg, 'modal' => '1']]; } }