Commit c9060154 authored by Chenu Denis's avatar Chenu Denis
Browse files

[feature] LimeSurvey 5 compatible version

[fix] Always encode user entered string
parent dd7348ac
......@@ -4,9 +4,9 @@
* {ANSWERTABLE} with filtered HTML usage (for question text)
*
* @author Denis Chenu <denis@sondages.pro>
* @copyright 2018-2020 Denis Chenu <http://sondages.pro>
* @copyright 2018-2022 Denis Chenu <http://sondages.pro>
* @license AGPL v3
* @version 2.1.1
* @version 2.2.0
*/
class adminNotificationHtmlAnswerTable extends PluginBase {
......@@ -113,14 +113,24 @@ class adminNotificationHtmlAnswerTable extends PluginBase {
if (!$this->getEvent()) {
throw new CHttpException(403);
}
$afterSurveyCompleteEvent = $this->getEvent();
$this->unsubscribe('afterFindSurvey');
$surveyId = $this->getEvent()->get('surveyId');
$oSurvey = Survey::model()->find("sid = :sid",array(":sid"=>$surveyId));// Don't use findByPk since it's cached
$surveyId = $afterSurveyCompleteEvent->get('surveyId');
$oSurvey = Survey::model()->find("sid = :sid",array(":sid"=>$surveyId)); // Don't use findByPk since it's cached
$emailnotificationto = $emailresponseto = null;
if(version_compare(Yii::app()->getConfig('versionnumber'),"3.14.1",">")) {
$emailnotificationto = $oSurvey->emailnotificationto;
$emailresponseto = $oSurvey->emailresponseto;
}
if(version_compare(Yii::app()->getConfig('versionnumber'),"4.0.0",">")) {
$aSurveyOptions = $oSurvey->aOptions;
if (!empty($aSurveyOptions['emailnotificationto'])) {
$emailnotificationto = $aSurveyOptions['emailnotificationto'];
}
if (!empty($aSurveyOptions['emailresponseto'])) {
$emailresponseto = $aSurveyOptions['emailresponseto'];
}
}
/* Always get current value (if user don't save survey after update) */
if(empty($emailnotificationto)) {
$emailnotificationto = $this->get('emailnotificationto','Survey',$surveyId,'');
......@@ -131,20 +141,115 @@ class adminNotificationHtmlAnswerTable extends PluginBase {
if(empty($emailnotificationto) && empty($emailresponseto) ) {
return;
}
//~ $this->getEvent()->getContent($this)
//~ ->addContent("<pre>".print_r($oSurvey->getAttributes(), true) . '</pre>');
//~ return;
$responseId = $this->getEvent()->get('responseId');
$responseId = $afterSurveyCompleteEvent->get('responseId');
if(empty($responseId)) {
return;
}
$this->ownSendNotificationEmail($afterSurveyCompleteEvent, $surveyId, $responseId, $emailnotificationto, $emailresponseto);
}
/**
* Send the final email
* @param \Event
* @param integer $responseId
* @param string $emailnotificationto
* @param string $emailresponseto
* @return void
*/
private function ownSendNotificationEmail($afterSurveyCompleteEvent, int $surveyId, int $responseId, string $emailnotificationto, string $emailresponseto)
{
if(version_compare(Yii::app()->getConfig('versionnumber'),"4.0.0","<")) {
return $this->ownSendNotificationEmail3LTS($afterSurveyCompleteEvent, $surveyId, $responseId, $emailnotificationto, $emailresponseto);
}
$debug = App()->getConfig('debug') || Permission::model()->hasSurveyPermission($surveyId, 'surveysettings', 'update');
$language = Yii::app()->getLanguage();
$oSurvey = Survey::model()->findByPk($surveyId);
$thissurvey = $aSurveyInfo = getSurveyInfo($surveyId,Yii::app()->getLanguage());
$sitename = Yii::app()->getConfig("sitename");
$bIsHTML = $oSurvey->getIsHtmlEmail();
$mailer = \LimeMailer::getInstance(\LimeMailer::ResetComplete);
$mailer->setSurvey($surveyId);
$mailer->aUrlsPlaceholders = ['VIEWRESPONSE','EDITRESPONSE','STATISTICS'];
$aReplacementVars = array();
$aReplacementVars['STATISTICSURL'] = App()->getController()->createAbsoluteUrl("/admin/statistics/sa/index/surveyid/{$surveyId}");
$aReplacementVars['ANSWERTABLE'] = $this->ownGetAnswerTable($surveyId, $responseId, $language, $bIsHTML);
$aReplacementVars['EDITRESPONSEURL'] = App()->getController()->createAbsoluteUrl("/admin/dataentry/sa/editdata/subaction/edit/surveyid/{$surveyId}/id/{$responseId}");
$aReplacementVars['VIEWRESPONSEURL'] = App()->getController()->createAbsoluteUrl("responses/view/", ['surveyId' => $surveyId, 'id' => $responseId]);
$aEmailResponseTo = array();
$aEmailNotificationTo = array();
$sResponseData = "";
if (!empty($emailnotificationto)) {
$aRecipient = explode(";", ReplaceFields($emailnotificationto, array('{ADMINEMAIL}' =>$oSurvey->adminemail), true));
foreach ($aRecipient as $sRecipient) {
$sRecipient = trim($sRecipient);
if ($mailer::validateAddress($sRecipient)) {
$aEmailNotificationTo[] = $sRecipient;
}
}
}
if (!empty($emailresponseto)) {
$aRecipient = explode(";", ReplaceFields($emailresponseto, array('{ADMINEMAIL}' =>$oSurvey->adminemail), true));
foreach ($aRecipient as $sRecipient) {
$sRecipient = trim($sRecipient);
if ($mailer::validateAddress($sRecipient)) {
$aEmailResponseTo[] = $sRecipient;
}
}
}
$reData = array('thissurvey' => $thissurvey);
templatereplace(
"{SID}",
array(), /* No tempvars update (except old Replacement like */
$reData /* Be sure to use current survey */
);
LimeExpressionManager::updateReplacementFields($aReplacementVars);
if (count($aEmailNotificationTo) > 0) {
$mailer = LimeMailer::getInstance();
$mailer->setTypeWithRaw('admin_notification', $language);
foreach ($aEmailNotificationTo as $sRecipient) {
$mailer->setTo($sRecipient);
if (!$mailer->SendMessage() && $debug) {
$afterSurveyCompleteEvent->getContent($this)->addContent(
sprintf($this->gT("Notification email could not be sent to %; Reason: %s"), CHtml::encode($sRecipient), CHtml::encode($mailer->$getError))
);
}
}
}
if (count($aEmailResponseTo) > 0) {
$mailer = LimeMailer::getInstance();
$mailer->setTypeWithRaw('admin_responses', $language);
foreach ($aEmailResponseTo as $sRecipient) {
$mailer->setTo($sRecipient);
if (!$mailer->SendMessage() && $debug) {
$afterSurveyCompleteEvent->getContent($this)->addContent(
sprintf($this->gT("Response email could not be sent to %; Reason: %s"), CHtml::encode($sRecipient), CHtml::encode($mailer->$getError))
);
}
}
}
}
/**
* Send the final email for old 3LTS
* @param \Event
* @param integer $responseId
* @param string $emailnotificationto
* @param string $emailresponseto
* @return void
*/
private function ownSendNotificationEmail3LTS($afterSurveyCompleteEvent, $surveyId, $responseId, $emailnotificationto, $emailresponseto)
{
global $maildebug;
$debug = Yii::app()->getConfig('debug');
// Usage of templatereplace
$aSurveyInfo = getSurveyInfo($surveyId,Yii::app()->getLanguage());
$homeurl = Yii::app()->getController()->createAbsoluteUrl('/admin');
$language = Yii::app()->getLanguage();
$oSurvey = Survey::model()->findByPk($surveyId);
$aSurveyInfo = getSurveyInfo($surveyId,App()->getLanguage());
$sitename = Yii::app()->getConfig("sitename");
$bIsHTML = ($oSurvey->htmlemail == 'Y');
$aReplacementVars = array();
......@@ -158,7 +263,7 @@ class adminNotificationHtmlAnswerTable extends PluginBase {
$aReplacementVars['EDITRESPONSEURL'] = "<a href='{$aReplacementVars['EDITRESPONSEURL']}'>{$aReplacementVars['EDITRESPONSEURL']}</a>";
$aReplacementVars['STATISTICSURL'] = "<a href='{$aReplacementVars['STATISTICSURL']}'>{$aReplacementVars['STATISTICSURL']}</a>";
}
$aReplacementVars['ANSWERTABLE'] = $this->_getAnswerTable($surveyId,$responseId,$language,$bIsHTML);
$aReplacementVars['ANSWERTABLE'] = $this->ownGetAnswerTable($surveyId,$responseId,$language,$bIsHTML);
$aEmailResponseTo = array();
$aEmailNotificationTo = array();
$sResponseData = "";
......@@ -200,13 +305,12 @@ class adminNotificationHtmlAnswerTable extends PluginBase {
// NOTE: those occurences of template replace should stay here. User from backend could use old replacement keyword
$sMessage = templatereplace($aSurveyInfo['email_admin_notification'], $aReplacementVars, $reData, 'admin_notification', $oSurvey->anonymized == "Y", null, array(), true);
$sSubject = templatereplace($aSurveyInfo['email_admin_notification_subj'], $aReplacementVars, $reData, 'admin_notification_subj', ($aSurveyInfo['anonymized'] == "Y"), null, array(), true);
foreach ($aEmailNotificationTo as $sRecipient) {
if (!SendEmailMessage($sMessage, $sSubject, $sRecipient, $sFrom, $sitename, $bIsHTML, getBounceEmail($surveyId), $aRelevantAttachments)) {
if ($debug > 0) {
$this->getEvent()->getContent($this)->addContent("Email could not be sent. Reason: ".$maildebug);
$afterSurveyCompleteEvent->getContent($this)->addContent("Email could not be sent. Reason: ".$maildebug);
}
} else {
//$this->getEvent()->getContent($this)->addContent("Email send with success to ".$sRecipient);
}
}
}
......@@ -227,14 +331,16 @@ class adminNotificationHtmlAnswerTable extends PluginBase {
foreach ($aEmailResponseTo as $sRecipient) {
if (!SendEmailMessage($sMessage, $sSubject, $sRecipient, $sFrom, $sitename, $bIsHTML, getBounceEmail($surveyId), $aRelevantAttachments)) {
if ($debug > 0) {
$this->getEvent()->getContent($this)->addContent("Email could not be sent. Reason: ".$maildebug);
$afterSurveyCompleteEvent->getContent($this)->addContent("Email could not be sent. Reason: ".$maildebug);
}
} else {
//$this->getEvent()->getContent($this)->addContent("Email send with success to ".$sRecipient);
}
}
}
}
/**
* see eventt
*/
public function afterFindSurvey() {
if (!$this->getEvent()) {
throw new CHttpException(403);
......@@ -247,7 +353,15 @@ class adminNotificationHtmlAnswerTable extends PluginBase {
}
}
private function _getAnswerTable($surveyid,$srid,$language,$bIsHTML) {
/**
* Get the answer table with HTMLM filtred only
* @param integer $surveyid
* @param integer $srid
* @param string $language
* @param boolean $bIsHTML
* @return string HTML for answer table
*/
private function ownGetAnswerTable($surveyid,$srid,$language,$bIsHTML) {
$showUnrelevantQuestion = $this->get('showUnrelevantQuestion',"Survey",$surveyid,$this->get('showUnrelevantQuestion',null,null,$this->settings['showUnrelevantQuestion']['default']));
$aFullResponseTable = getFullResponseTable($surveyid, $srid, $language,!$showUnrelevantQuestion);
$ResultTableHTML = "<table class='printouttable' >\n";
......@@ -272,12 +386,16 @@ class adminNotificationHtmlAnswerTable extends PluginBase {
$ResultTableText .="* {$questionText} \n";
} else {
if($showUnrelevantQuestion || LimeExpressionManager::ProcessStepString("{".$sFieldname.".relevanceStatus}") ) {
if( $encodeAnswer ) {
if(isset($tableSchemaColumns[$sFieldname]->dbType) && in_array($tableSchemaColumns[$sFieldname]->dbType,array('text')) ) {
$answer = CHtml::encode($answer);
} else {
/* Update by DB type */
if(isset($tableSchemaColumns[$sFieldname]->dbType) && in_array($tableSchemaColumns[$sFieldname]->dbType,array('varchar(5)')) ) {
$answer = viewHelper::purified($answer);
if( $encodeAnswer ) {
$answer = CHtml::encode($answer);
} else {
/* Update by DB type */
if(isset($tableSchemaColumns[$sFieldname]->dbType) && in_array($tableSchemaColumns[$sFieldname]->dbType,array('varchar(5)')) ) {
$answer = viewHelper::purified($answer);
}
}
}
if(empty($fname[1])) {
......@@ -299,7 +417,7 @@ class adminNotificationHtmlAnswerTable extends PluginBase {
return $ResultTableHTML;
}
private function _translate($string) {
private function translate($string) {
return parent::gT($string);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<config>
<metadata>
<name>adminNotificationHtmlAnswerTable</name>
<type>plugin</type>
<creationDate>2018-08-01</creationDate>
<last_update>2022-10-11</last_update>
<author>Denis Chenu</author>
<authorUrl>https://www.sondages.pro</authorUrl>
<license>GNU Affero General Public License version 3 or later</license>
<version>2.2.0</version>
<description><![CDATA[{ANSWERTABLE} with filtered HTML usage (for question text).]]></description>
</metadata>
<compatibility>
<version>3</version>
<version>4</version>
<version>5</version>
</compatibility>
<updaters disabled="disabled"></updaters>
</config>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment