Skip to content

Commit

Permalink
Merge pull request #339 from Jan-E/master
Browse files Browse the repository at this point in the history
Fix #09776: Mail registered participants with RemoteControl API & Fix #09783: Use "adminname <adminemail>" as from-header in emailTokens
  • Loading branch information
c-schmitz committed Jul 22, 2015
2 parents 5077f70 + 08447cf commit 1f382c3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
11 changes: 8 additions & 3 deletions application/helpers/admin/token_helper.php
Expand Up @@ -17,7 +17,7 @@
*
* @param mixed $iSurveyID
* @param array $aResultTokens
* @param string $sType type of notification invite|remind
* @param string $sType type of notification invite|register|remind
* @return array of results
*/
function emailTokens($iSurveyID,$aResultTokens,$sType)
Expand Down Expand Up @@ -78,7 +78,7 @@ function emailTokens($iSurveyID,$aResultTokens,$sType)

$fieldsarray["{ADMINNAME}"] = $oSurvey['admin'];
$fieldsarray["{ADMINEMAIL}"] = $oSurvey['adminemail'];
$from = $fieldsarray["{ADMINEMAIL}"];
$from = $fieldsarray["{ADMINNAME}"] . ' <' . $fieldsarray["{ADMINEMAIL}"] . '>';
if($from == '')
$from = Yii::app()->getConfig('siteadminemail');

Expand Down Expand Up @@ -117,6 +117,11 @@ function emailTokens($iSurveyID,$aResultTokens,$sType)
$sSubject = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_invite_subj'];
$sMessage = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_invite'];
}
else if($sType == 'register')
{
$sSubject = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_register_subj'];
$sMessage = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_register'];
}
else
{
$sSubject = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_remind_subj'];
Expand Down Expand Up @@ -159,7 +164,7 @@ function emailTokens($iSurveyID,$aResultTokens,$sType)
'email'=>$fieldsarray["{EMAIL}"],
'status'=>'OK');

if($sType == 'invite')
if($sType == 'invite' || $sType == 'register')
$oTokens->updateByPk($aTokenRow['tid'], array('sent' => dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"))));

if($sType == 'remind')
Expand Down
86 changes: 86 additions & 0 deletions application/helpers/remotecontrol/remotecontrol_handle.php
Expand Up @@ -2016,6 +2016,92 @@ public function activate_tokens($sSessionKey, $iSurveyID, $aAttributeFields=arra
return array('status' => 'No permission');
}

/**
* RPC Routine to send register mails to participants in a survey
* Returns array of results of sending
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the survey that participants belong
* @param array $overrideAllConditions replace the default conditions, like this:
* $overrideAllConditions = Array();
* $overrideAllConditions[] = 'tid = 2';
* $response = $myJSONRPCClient->mail_registered_participants( $sessionKey, $survey_id, $overrideAllConditions );
* @return array Result of the action
*/
public function mail_registered_participants($sSessionKey, $iSurveyID, $overrideAllConditions=Array() )
{
Yii::app()->loadHelper('admin/token');
if (!$this->_checkSessionKey($sSessionKey))
return array('status' => 'Invalid session key');

$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey))
return array('status' => 'Error: Invalid survey ID');

if (Permission::model()->hasSurveyPermission($iSurveyID, 'tokens', 'update'))
{

if(!tableExists("{{tokens_$iSurveyID}}"))
return array('status' => 'Error: No token table');

$command = new CDbCriteria();
$command->condition = '';
if (count($overrideAllConditions)) {
foreach($overrideAllConditions as $condition)
{
$command->addCondition($condition);
}
}
else
{
$command->addCondition('usesleft > 0');
$command->addCondition("sent = 'N'");
$command->addCondition("remindersent = 'N'");
$command->addCondition("(completed ='N') or (completed='')");
$command->addCondition('ISNULL(validfrom) OR validfrom < NOW()');
$command->addCondition('ISNULL(validuntil) OR validuntil > NOW()');
$command->addCondition('emailstatus = "OK"');
}
$command->order = 'tid';

$aAllTokens = Token::model($iSurveyID)->findAll( $command );
$iAllTokensCount=count($aAllTokens);
unset($aAllTokens);

$iMaxEmails = (int)Yii::app()->getConfig("maxemails");
$command->limit = $iMaxEmails;
$aResultTokens = Token::model($iSurveyID)->findAll( $command );

if (empty($aResultTokens))
return array('status' => 'Error: No candidate tokens');

foreach($aResultTokens as $key=>$oToken)
{
//pattern taken from php_filter_validate_email PHP_5_4/ext/filter/logical_filters.c
$pattern = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';

//if(!filter_var($emailaddress, FILTER_VALIDATE_EMAIL))
if (preg_match($pattern, $oToken['email']) !== 1)
{
unset($aResultTokens[$key]);
//subtract from 'left to send'
$iAllTokensCount--;
}
}

if (empty($aResultTokens))
return array('status' => 'Error: No candidate tokens');
$aResult = emailTokens($iSurveyID,$aResultTokens,'register');
$iLeft = $iAllTokensCount - count($aResultTokens);
$aResult['status'] = $iLeft . " left to send";

return $aResult;
}
else
return array('status' => 'No permission');
}

/**
* RPC Routine to invite participants in a survey
* Returns array of results of sending
Expand Down

0 comments on commit 1f382c3

Please sign in to comment.