Exemplo n.º 1
0
/**
* Retrieves the attribute names from the related token table
*
* @param mixed $surveyid  The survey ID
* @param boolean $bOnlyAttributes Set this to true if you only want the fieldnames of the additional attribue fields - defaults to false
* @return array The fieldnames as key and names as value in an Array
*/
function getTokenFieldsAndNames($surveyid, $bOnlyAttributes = false)
{
    $aBasicTokenFields = array('firstname' => array('description' => gT('First name'), 'mandatory' => 'N', 'showregister' => 'Y'), 'lastname' => array('description' => gT('Last name'), 'mandatory' => 'N', 'showregister' => 'Y'), 'email' => array('description' => gT('Email address'), 'mandatory' => 'N', 'showregister' => 'Y'), 'emailstatus' => array('description' => gT("Email status"), 'mandatory' => 'N', 'showregister' => 'N'), 'token' => array('description' => gT('Token'), 'mandatory' => 'N', 'showregister' => 'Y'), 'language' => array('description' => gT('Language code'), 'mandatory' => 'N', 'showregister' => 'Y'), 'sent' => array('description' => gT('Invitation sent date'), 'mandatory' => 'N', 'showregister' => 'Y'), 'remindersent' => array('description' => gT('Last reminder sent date'), 'mandatory' => 'N', 'showregister' => 'Y'), 'remindercount' => array('description' => gT('Total numbers of sent reminders'), 'mandatory' => 'N', 'showregister' => 'Y'), 'usesleft' => array('description' => gT('Uses left'), 'mandatory' => 'N', 'showregister' => 'Y'));
    $aExtraTokenFields = getAttributeFieldNames($surveyid);
    $aSavedExtraTokenFields = Survey::model()->findByPk($surveyid)->tokenAttributes;
    // Drop all fields that are in the saved field description but not in the table definition
    $aSavedExtraTokenFields = array_intersect_key($aSavedExtraTokenFields, array_flip($aExtraTokenFields));
    // Now add all fields that are in the table but not in the field description
    foreach ($aExtraTokenFields as $sField) {
        if (!isset($aSavedExtraTokenFields[$sField])) {
            $aSavedExtraTokenFields[$sField] = array('description' => $sField, 'mandatory' => 'N', 'showregister' => 'N', 'cpdbmap' => '');
        } elseif (empty($aSavedExtraTokenFields[$sField]['description'])) {
            $aSavedExtraTokenFields[$sField]['description'] = $sField;
        }
    }
    if ($bOnlyAttributes) {
        return $aSavedExtraTokenFields;
    } else {
        return array_merge($aBasicTokenFields, $aSavedExtraTokenFields);
    }
}
/**
* Marks a tokens as completed and sends a confirmation email to the participiant.
* If $quotaexit is set to true then the user exited the survey due to a quota
* restriction and the according token is only marked as 'Q'
*
* @param mixed $quotaexit
*/
function submittokens($quotaexit = false)
{
    $surveyid = Yii::app()->getConfig('surveyID');
    if (isset($_SESSION['survey_' . $surveyid]['s_lang'])) {
        $thissurvey = getSurveyInfo($surveyid, $_SESSION['survey_' . $surveyid]['s_lang']);
    } else {
        $thissurvey = getSurveyInfo($surveyid);
    }
    $clienttoken = $_SESSION['survey_' . $surveyid]['token'];
    $sitename = Yii::app()->getConfig("sitename");
    $emailcharset = Yii::app()->getConfig("emailcharset");
    // Shift the date due to global timeadjust setting
    $today = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"));
    // check how many uses the token has left
    $token = Token::model($surveyid)->findByAttributes(array('token' => $clienttoken));
    if ($quotaexit == true) {
        $token->completed = 'Q';
        $token->usesleft--;
    } else {
        if ($token->usesleft <= 1) {
            // Finish the token
            if (isTokenCompletedDatestamped($thissurvey)) {
                $token->completed = $today;
            } else {
                $token->completed = 'Y';
            }
            if (isset($token->participant_id)) {
                $slquery = SurveyLink::model()->find('participant_id = :pid AND survey_id = :sid AND token_id = :tid', array(':pid' => $token->participant_id, ':sid' => $surveyid, ':tid' => $token->tid));
                if ($slquery) {
                    if (isTokenCompletedDatestamped($thissurvey)) {
                        $slquery->date_completed = $today;
                    } else {
                        // Update the survey_links table if necessary, to protect anonymity, use the date_created field date
                        $slquery->date_completed = $slquery->date_created;
                    }
                    $slquery->save();
                }
            }
        }
        $token->usesleft--;
    }
    $token->save();
    if ($quotaexit == false) {
        if ($token && trim(strip_tags($thissurvey['email_confirm'])) != "" && $thissurvey['sendconfirmation'] == "Y") {
            //   if($token->completed == "Y" || $token->completed == $today)
            //            {
            $from = "{$thissurvey['adminname']} <{$thissurvey['adminemail']}>";
            $subject = $thissurvey['email_confirm_subj'];
            $aReplacementVars = array();
            $aReplacementVars["ADMINNAME"] = $thissurvey['admin'];
            $aReplacementVars["ADMINEMAIL"] = $thissurvey['adminemail'];
            $aReplacementVars['ADMINEMAIL'] = $thissurvey['adminemail'];
            //Fill with token info, because user can have his information with anonimity control
            $aReplacementVars["FIRSTNAME"] = $token->firstname;
            $aReplacementVars["LASTNAME"] = $token->lastname;
            $aReplacementVars["TOKEN"] = $token->token;
            // added survey url in replacement vars
            $surveylink = Yii::app()->createAbsoluteUrl("/survey/index/sid/{$surveyid}", array('lang' => $_SESSION['survey_' . $surveyid]['s_lang'], 'token' => $token->token));
            $aReplacementVars['SURVEYURL'] = $surveylink;
            $attrfieldnames = getAttributeFieldNames($surveyid);
            foreach ($attrfieldnames as $attr_name) {
                $aReplacementVars[strtoupper($attr_name)] = $token->{$attr_name};
            }
            $dateformatdatat = getDateFormatData($thissurvey['surveyls_dateformat']);
            $numberformatdatat = getRadixPointData($thissurvey['surveyls_numberformat']);
            $redata = array('thissurvey' => $thissurvey);
            $subject = templatereplace($subject, $aReplacementVars, $redata, '', false, null, array(), true);
            $subject = html_entity_decode($subject, ENT_QUOTES, $emailcharset);
            if (getEmailFormat($surveyid) == 'html') {
                $ishtml = true;
            } else {
                $ishtml = false;
            }
            $message = $thissurvey['email_confirm'];
            //$message=ReplaceFields($message, $fieldsarray, true);
            $message = templatereplace($message, $aReplacementVars, $redata, '', false, null, array(), true);
            if (!$ishtml) {
                $message = strip_tags(breakToNewline(html_entity_decode($message, ENT_QUOTES, $emailcharset)));
            } else {
                $message = html_entity_decode($message, ENT_QUOTES, $emailcharset);
            }
            //Only send confirmation email if there is a valid email address
            $sToAddress = validateEmailAddresses($token->email);
            if ($sToAddress) {
                $aAttachments = unserialize($thissurvey['attachments']);
                $aRelevantAttachments = array();
                /*
                 * Iterate through attachments and check them for relevance.
                 */
                if (isset($aAttachments['confirmation'])) {
                    foreach ($aAttachments['confirmation'] as $aAttachment) {
                        $relevance = $aAttachment['relevance'];
                        // If the attachment is relevant it will be added to the mail.
                        if (LimeExpressionManager::ProcessRelevance($relevance) && file_exists($aAttachment['url'])) {
                            $aRelevantAttachments[] = $aAttachment['url'];
                        }
                    }
                }
                SendEmailMessage($message, $subject, $sToAddress, $from, $sitename, $ishtml, null, $aRelevantAttachments);
            }
            //   } else {
            // Leave it to send optional confirmation at closed token
            //          }
        }
    }
}
Exemplo n.º 3
0
 /**
  * import from csv
  */
 function import($iSurveyId)
 {
     $clang = $this->getController()->lang;
     $iSurveyId = (int) $iSurveyId;
     if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'import')) {
         Yii::app()->session['flashmessage'] = $clang->gT("You do not have sufficient rights to access this page.");
         $this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}"));
     }
     // CHECK TO SEE IF A TOKEN TABLE EXISTS FOR THIS SURVEY
     $bTokenExists = tableExists('{{tokens_' . $iSurveyId . '}}');
     if (!$bTokenExists) {
         self::_newtokentable($iSurveyId);
     }
     App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('adminscripts') . 'tokensimport.js');
     $aEncodings = aEncodingsArray();
     if (Yii::app()->request->getPost('submit')) {
         if (Yii::app()->request->getPost('csvcharset') && Yii::app()->request->getPost('csvcharset')) {
             $uploadcharset = Yii::app()->request->getPost('csvcharset');
             if (!array_key_exists($uploadcharset, $aEncodings)) {
                 $uploadcharset = 'auto';
             }
             $filterduplicatetoken = Yii::app()->request->getPost('filterduplicatetoken') && Yii::app()->request->getPost('filterduplicatetoken') == 'on';
             $filterblankemail = Yii::app()->request->getPost('filterblankemail') && Yii::app()->request->getPost('filterblankemail') == 'on';
         }
         $attrfieldnames = getAttributeFieldNames($iSurveyId);
         $duplicatelist = array();
         $invalidemaillist = array();
         $invalidformatlist = array();
         $firstline = array();
         $sPath = Yii::app()->getConfig('tempdir');
         $sFileTmpName = $_FILES['the_file']['tmp_name'];
         $sFilePath = $sPath . '/' . randomChars(20);
         if (!@move_uploaded_file($sFileTmpName, $sFilePath)) {
             $aData['sError'] = $clang->gT("Upload file not found. Check your permissions and path ({$sFilePath}) for the upload directory");
             $aData['aEncodings'] = $aEncodings;
             $aData['iSurveyId'] = $aData['surveyid'] = $iSurveyId;
             $aData['thissurvey'] = getSurveyInfo($iSurveyId);
             $this->_renderWrappedTemplate('token', array('tokenbar', 'csvupload'), $aData);
         } else {
             $xz = 0;
             $recordcount = 0;
             $xv = 0;
             // This allows to read file with MAC line endings too
             @ini_set('auto_detect_line_endings', true);
             // open it and trim the ednings
             $tokenlistarray = file($sFilePath);
             $sBaseLanguage = Survey::model()->findByPk($iSurveyId)->language;
             if (!Yii::app()->request->getPost('filterduplicatefields') || Yii::app()->request->getPost('filterduplicatefields') && count(Yii::app()->request->getPost('filterduplicatefields')) == 0) {
                 $filterduplicatefields = array('firstname', 'lastname', 'email');
             } else {
                 $filterduplicatefields = Yii::app()->request->getPost('filterduplicatefields');
             }
             $separator = returnGlobal('separator');
             foreach ($tokenlistarray as $buffer) {
                 $buffer = @mb_convert_encoding($buffer, "UTF-8", $uploadcharset);
                 if ($recordcount == 0) {
                     // Parse first line (header) from CSV
                     $buffer = removeBOM($buffer);
                     // We alow all field except tid because this one is really not needed.
                     $allowedfieldnames = array('participant_id', 'firstname', 'lastname', 'email', 'emailstatus', 'token', 'language', 'blacklisted', 'sent', 'remindersent', 'remindercount', 'validfrom', 'validuntil', 'completed', 'usesleft');
                     $allowedfieldnames = array_merge($attrfieldnames, $allowedfieldnames);
                     // Some header don't have same column name
                     $aReplacedFields = array('invited' => 'sent');
                     switch ($separator) {
                         case 'comma':
                             $separator = ',';
                             break;
                         case 'semicolon':
                             $separator = ';';
                             break;
                         default:
                             $comma = substr_count($buffer, ',');
                             $semicolon = substr_count($buffer, ';');
                             if ($semicolon > $comma) {
                                 $separator = ';';
                             } else {
                                 $separator = ',';
                             }
                     }
                     $firstline = str_getcsv($buffer, $separator, '"');
                     $firstline = array_map('trim', $firstline);
                     $ignoredcolumns = array();
                     // Now check the first line for invalid fields
                     foreach ($firstline as $index => $fieldname) {
                         $firstline[$index] = preg_replace("/(.*) <[^,]*>\$/", "\$1", $fieldname);
                         $fieldname = $firstline[$index];
                         if (!in_array($fieldname, $allowedfieldnames)) {
                             $ignoredcolumns[] = $fieldname;
                         }
                         if (array_key_exists($fieldname, $aReplacedFields)) {
                             $firstline[$index] = $aReplacedFields[$fieldname];
                         }
                     }
                     if (!in_array('firstname', $firstline) || !in_array('lastname', $firstline) || !in_array('email', $firstline)) {
                         $recordcount = count($tokenlistarray);
                         break;
                     }
                 } else {
                     $line = str_getcsv($buffer, $separator, '"');
                     if (count($firstline) != count($line)) {
                         $invalidformatlist[] = $recordcount;
                         $recordcount++;
                         continue;
                     }
                     $writearray = array_combine($firstline, $line);
                     //kick out ignored columns
                     foreach ($ignoredcolumns as $column) {
                         unset($writearray[$column]);
                     }
                     $dupfound = false;
                     $invalidemail = false;
                     if ($filterduplicatetoken != false) {
                         $dupquery = "SELECT count(tid) from {{tokens_" . intval($iSurveyId) . "}} where 1=1";
                         foreach ($filterduplicatefields as $field) {
                             if (isset($writearray[$field])) {
                                 $dupquery .= " and " . Yii::app()->db->quoteColumnName($field) . " = " . Yii::app()->db->quoteValue($writearray[$field]);
                             }
                         }
                         $dupresult = Yii::app()->db->createCommand($dupquery)->queryScalar();
                         if ($dupresult > 0) {
                             $dupfound = true;
                             $duplicatelist[] = Yii::app()->db->quoteValue($writearray['firstname']) . " " . Yii::app()->db->quoteValue($writearray['lastname']) . " (" . Yii::app()->db->quoteValue($writearray['email']) . ")";
                         }
                     }
                     $writearray['email'] = trim($writearray['email']);
                     //treat blank emails
                     if ($filterblankemail && $writearray['email'] == '') {
                         $invalidemail = true;
                         $invalidemaillist[] = $line[0] . " " . $line[1] . " ( )";
                     }
                     if ($writearray['email'] != '') {
                         $aEmailAddresses = explode(';', $writearray['email']);
                         foreach ($aEmailAddresses as $sEmailaddress) {
                             if (!validateEmailAddress($sEmailaddress)) {
                                 $invalidemail = true;
                                 $invalidemaillist[] = $line[0] . " " . $line[1] . " (" . $line[2] . ")";
                             }
                         }
                     }
                     if (isset($writearray['token'])) {
                         $writearray['token'] = sanitize_token($writearray['token']);
                     }
                     if (!$dupfound && !$invalidemail) {
                         // unset all empty value
                         foreach ($writearray as $key => $value) {
                             if ($writearray[$key] == "") {
                                 unset($writearray[$key]);
                             }
                             if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {
                                 // Fix CSV quote
                                 $value = substr($value, 1, -1);
                             }
                         }
                         // Some default value : to be moved to Token model rules in future release ?
                         // But think we have to accept invalid email etc ... then use specific scenario
                         $writearray['emailstatus'] = isset($writearray['emailstatus']) ? $writearray['emailstatus'] : "OK";
                         $writearray['language'] = isset($writearray['language']) ? $writearray['language'] : $sBaseLanguage;
                         $oToken = Token::create($iSurveyId);
                         foreach ($writearray as $key => $value) {
                             //if(in_array($key,$oToken->attributes)) Not needed because we filter attributes before
                             $oToken->{$key} = $value;
                         }
                         $ir = $oToken->save();
                         if (!$ir) {
                             $duplicatelist[] = $writearray['firstname'] . " " . $writearray['lastname'] . " (" . $writearray['email'] . ")";
                         } else {
                             $xz++;
                         }
                     }
                     $xv++;
                 }
                 $recordcount++;
             }
             $recordcount = $recordcount - 1;
             unlink($sFilePath);
             $aData['tokenlistarray'] = $tokenlistarray;
             $aData['xz'] = $xz;
             $aData['xv'] = $xv;
             $aData['recordcount'] = $recordcount;
             $aData['firstline'] = $firstline;
             $aData['duplicatelist'] = $duplicatelist;
             $aData['invalidformatlist'] = $invalidformatlist;
             $aData['invalidemaillist'] = $invalidemaillist;
             $aData['thissurvey'] = getSurveyInfo($iSurveyId);
             $aData['iSurveyId'] = $aData['surveyid'] = $iSurveyId;
             $this->_renderWrappedTemplate('token', array('tokenbar', 'csvpost'), $aData);
         }
     } else {
         $aData['aEncodings'] = $aEncodings;
         $aData['iSurveyId'] = $iSurveyId;
         $aData['thissurvey'] = getSurveyInfo($iSurveyId);
         $aData['surveyid'] = $iSurveyId;
         $aTokenTableFields = getTokenFieldsAndNames($iSurveyId);
         unset($aTokenTableFields['sent']);
         unset($aTokenTableFields['remindersent']);
         unset($aTokenTableFields['remindercount']);
         unset($aTokenTableFields['usesleft']);
         foreach ($aTokenTableFields as $sKey => $sValue) {
             if ($sValue['description'] != $sKey) {
                 $sValue['description'] .= ' - ' . $sKey;
             }
             $aNewTokenTableFields[$sKey] = $sValue['description'];
         }
         $aData['aTokenTableFields'] = $aNewTokenTableFields;
         $this->_renderWrappedTemplate('token', array('tokenbar', 'csvupload'), $aData);
     }
 }
function ldap_doTokenSearch($ds, $ldapq, &$ResArray, $surveyid)
{
    $ldap_queries = Yii::app()->getConfig('ldap_queries');
    $totalrescount = 0;
    $userattrs = array();
    // First let's lowercase the ldap query values
    prepareLdapQuery($ldapq);
    // Retrieve the ldap user attribute-list to read
    $userparams = array('firstname_attr', 'lastname_attr', 'email_attr', 'token_attr', 'language');
    //			'attr1', 'attr2');
    $aTokenAttr = getAttributeFieldNames($surveyid);
    foreach ($aTokenAttr as $thisattrfieldname) {
        $attridx = substr($thisattrfieldname, 10);
        // the 'attribute_' prefix is 10 chars long
        $userparams[] = "attr" . $attridx;
    }
    foreach ($userparams as $id => $attr) {
        if (array_key_exists($attr, $ldap_queries[$ldapq]) && $ldap_queries[$ldapq][$attr] != '') {
            $userattrs[] = $ldap_queries[$ldapq][$attr];
        }
    }
    // If ldap group filtering is required
    if (isset($ldap_queries[$ldapq]['groupfilter']) && $ldap_queries[$ldapq]['groupfilter'] != '') {
        $userCandidates = array();
        // list of candidates
        $groupscope = 'sub';
        // subtree search unless specified
        if (isset($ldap_queries[$ldapq]['groupscope']) && $ldap_queries[$ldapq]['groupscope'] != '') {
            $groupscope = $ldap_queries[$ldapq]['groupscope'];
        }
        $groupmemberattr = 'member';
        //use 'member' attribute unless specified
        if (isset($ldap_queries[$ldapq]['groupmemberattr']) && $ldap_queries[$ldapq]['groupmemberattr'] != '') {
            $groupmemberattr = $ldap_queries[$ldapq]['groupmemberattr'];
        }
        // Search for group candidates
        $search_groups = ldap_search_withScope($ds, $ldap_queries[$ldapq]['groupbase'], $ldap_queries[$ldapq]['groupfilter'], array($groupmemberattr), $groupscope);
        $rescount = @ldap_count_entries($ds, $search_groups);
        if ($rescount >= 1) {
            // at least 1 group was selected
            $group_info = ldap_get_entries($ds, $search_groups);
            // For each group candidate add members's id to $userCandidates[]
            for ($i = 0; $i < $group_info["count"]; $i++) {
                for ($j = 0; $j < $group_info[$i][$groupmemberattr]["count"]; $j++) {
                    // Only add the user's id if not already listed
                    // (avoids duplicates if this user is in several groups)
                    if (!in_array($group_info[$i][$groupmemberattr][$j], $userCandidates)) {
                        $userCandidates[] = $group_info[$i][$groupmemberattr][$j];
                    }
                }
            }
            // For each user, apply userfilter if defined
            // and get user attrs
            foreach ($userCandidates as $key => $user) {
                $user_is_dn = TRUE;
                // Suppose group members are DNs by default
                if (isset($ldap_queries[$ldapq]['groupmemberisdn']) && $ldap_queries[$ldapq]['groupmemberisdn'] == False) {
                    $user_is_dn = False;
                }
                if ($user_is_dn) {
                    // If group members are DNs
                    // Set userfilter (no filter by default)
                    $userfilter = '(objectclass=*)';
                    if (isset($ldap_queries[$ldapq]['userfilter']) && $ldap_queries[$ldapq]['userfilter'] != '') {
                        $userfilter = $ldap_queries[$ldapq]['userfilter'];
                    }
                    $userscope = 'sub';
                    // subtree search unless specified
                    if (isset($ldap_queries[$ldapq]['userscope']) && $ldap_queries[$ldapq]['userscope'] != '') {
                        $userscope = $ldap_queries[$ldapq]['userscope'];
                    }
                    // If a userbase is defined, then get user's RND
                    // and do a user search based on this RDN
                    // Note: User's RDN is supposed to be made
                    //	 of only ONE attribute by this function
                    if (isset($ldap_queries[$ldapq]['userbase']) && $ldap_queries[$ldapq]['userbase'] != '') {
                        // get user's rdn
                        $user_dn_tab = explode(",", $user);
                        $user_rdn = $user_dn_tab[0];
                        $userfilter_rdn = "(&(" . $user_rdn . ")" . $userfilter . ")";
                        $search_users = ldap_search_withScope($ds, $ldap_queries[$ldapq]['userbase'], $userfilter_rdn, $userattrs, $userscope);
                        $rescount = @ldap_count_entries($ds, $search_users);
                        if ($rescount >= 1) {
                            // DN match criteria
                            // add to result array
                            $user_info = @ldap_get_entries($ds, $search_users);
                            for ($i = 0; $i < $rescount; $i++) {
                                if ($user_info[$i]['dn'] == $user) {
                                    $ResArray[] = $user_info;
                                    $totalrescount++;
                                }
                            }
                        }
                    } else {
                        // There is no userbase defined
                        // Only apply userfilter to the user's DN
                        $search_users = ldap_search_withScope($ds, $user, $userfilter, $userattrs, 'base');
                        $rescount = @ldap_count_entries($ds, $search_users);
                        if ($rescount >= 1) {
                            // DN match criteria, add result to the result Array
                            $userentry = ldap_get_entries($ds, $search_users);
                            $ResArray[] = $userentry;
                            $totalrescount++;
                        }
                    }
                    // End of Member is DN and a userbase is NOT defined
                } else {
                    //$user is the user ID, not a DN
                    // Search given userid combined with userfilter
                    // Set userfilter ('open filter' by default)
                    $userfilter = '(objectclass=*)';
                    if (isset($ldap_queries[$ldapq]['userfilter']) && $ldap_queries[$ldapq]['userfilter'] != '') {
                        $userfilter = $ldap_queries[$ldapq]['userfilter'];
                    }
                    // Build the user filter from the RDN
                    $userfilter_notdn = "(&(" . $ldap_queries[$ldapq]['useridattr'] . "=" . $user . ")" . $userfilter . ")";
                    $search_users = ldap_search_withScope($ds, $ldap_queries[$ldapq]['userbase'], $userfilter_notdn, $userattrs, $ldap_queries[$ldapq]['userscope']);
                    $rescount = @ldap_count_entries($ds, $search_users);
                    if ($rescount >= 1) {
                        // user matches criteria, add result to the result Array
                        $user_info = ldap_get_entries($ds, $search_users);
                        $ResArray[] = $user_info;
                        $totalrescount += $rescount;
                    }
                }
                // End of the members are not DN case
            }
            // End of foreach user member in the group
        }
        // End of foreach group
    } else {
        // No groupfilter is defined
        // Apply a simple userfilter then
        $userscope = 'sub';
        // default to subtree search
        if (isset($ldap_queries[$ldapq]['userscope']) && $ldap_queries[$ldapq]['userscope'] != '') {
            $userscope = $ldap_queries[$ldapq]['userscope'];
        }
        $search_result = ldap_search_withScope($ds, $ldap_queries[$ldapq]['userbase'], $ldap_queries[$ldapq]['userfilter'], $userattrs, $userscope);
        $rescount = ldap_count_entries($ds, $search_result);
        if ($rescount >= 1) {
            $user_info = ldap_get_entries($ds, $search_result);
            $ResArray[] = $user_info;
            $totalrescount += $rescount;
        }
    }
    // End of no group filtering
    return $totalrescount;
}
Exemplo n.º 5
0
function tokensExport($iSurveyID)
{
    $sEmailFiter = trim(App()->request->getPost('filteremail'));
    $iTokenStatus = App()->request->getPost('tokenstatus');
    $iInvitationStatus = App()->request->getPost('invitationstatus');
    $iReminderStatus = App()->request->getPost('reminderstatus');
    $sTokenLanguage = App()->request->getPost('tokenlanguage');
    $oSurvey = Survey::model()->findByPk($iSurveyID);
    $bIsNotAnonymous = $oSurvey->anonymized == 'N' && $oSurvey->active == 'Y';
    // db table exist (survey_$iSurveyID) ?
    $bquery = "SELECT * FROM {{tokens_{$iSurveyID}}} where 1=1";
    $databasetype = Yii::app()->db->getDriverName();
    if (trim($sEmailFiter) != '') {
        if (in_array($databasetype, array('mssql', 'sqlsrv', 'dblib'))) {
            $bquery .= ' and CAST(email as varchar) like ' . dbQuoteAll('%' . $_POST['filteremail'] . '%', true);
        } else {
            $bquery .= ' and email like ' . dbQuoteAll('%' . $_POST['filteremail'] . '%', true);
        }
    }
    if ($_POST['tokenstatus'] == 1) {
        $bquery .= " and completed<>'N'";
    } elseif ($iTokenStatus == 2) {
        $bquery .= " and completed='N'";
    } elseif ($iTokenStatus == 3 && $bIsNotAnonymous) {
        $bquery .= " and completed='N' and token not in (select token from {{survey_{$iSurveyID}}} group by token)";
    } elseif ($iTokenStatus == 4 && $bIsNotAnonymous) {
        $bquery .= " and completed='N' and token in (select token from {{survey_{$iSurveyID}}} group by token)";
    }
    if ($iInvitationStatus == 1) {
        $bquery .= " and sent<>'N'";
    }
    if ($iInvitationStatus == 2) {
        $bquery .= " and sent='N'";
    }
    if ($iReminderStatus == 1) {
        $bquery .= " and remindersent<>'N'";
    }
    if ($iReminderStatus == 2) {
        $bquery .= " and remindersent='N'";
    }
    if ($sTokenLanguage != '') {
        $bquery .= " and language=" . dbQuoteAll($sTokenLanguage);
    }
    $bquery .= " ORDER BY tid";
    Yii::app()->loadHelper('database');
    $bresult = Yii::app()->db->createCommand($bquery)->query();
    //dbExecuteAssoc($bquery) is faster but deprecated!
    //HEADERS should be after the above query else timeout errors in case there are lots of tokens!
    header("Content-Disposition: attachment; filename=tokens_" . $iSurveyID . ".csv");
    header("Content-type: text/comma-separated-values; charset=UTF-8");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: cache");
    // Export UTF8 WITH BOM
    $tokenoutput = chr(hexdec('EF')) . chr(hexdec('BB')) . chr(hexdec('BF'));
    $tokenoutput .= "tid,firstname,lastname,email,emailstatus,token,language,validfrom,validuntil,invited,reminded,remindercount,completed,usesleft";
    $attrfieldnames = getAttributeFieldNames($iSurveyID);
    $attrfielddescr = getTokenFieldsAndNames($iSurveyID, true);
    foreach ($attrfieldnames as $attr_name) {
        $tokenoutput .= ", {$attr_name}";
        if (isset($attrfielddescr[$attr_name])) {
            $tokenoutput .= " <" . str_replace(",", " ", $attrfielddescr[$attr_name]['description']) . ">";
        }
    }
    $tokenoutput .= "\n";
    echo $tokenoutput;
    $tokenoutput = "";
    // Export token line by line and fill $aExportedTokens with token exported
    Yii::import('application.libraries.Date_Time_Converter', true);
    $aExportedTokens = array();
    while ($brow = $bresult->read()) {
        if (trim($brow['validfrom'] != '')) {
            $datetimeobj = new Date_Time_Converter($brow['validfrom'], "Y-m-d H:i:s");
            $brow['validfrom'] = $datetimeobj->convert('Y-m-d H:i');
        }
        if (trim($brow['validuntil'] != '')) {
            $datetimeobj = new Date_Time_Converter($brow['validuntil'], "Y-m-d H:i:s");
            $brow['validuntil'] = $datetimeobj->convert('Y-m-d H:i');
        }
        $tokenoutput .= '"' . trim($brow['tid']) . '",';
        $tokenoutput .= '"' . trim($brow['firstname']) . '",';
        $tokenoutput .= '"' . trim($brow['lastname']) . '",';
        $tokenoutput .= '"' . trim($brow['email']) . '",';
        $tokenoutput .= '"' . trim($brow['emailstatus']) . '",';
        $tokenoutput .= '"' . trim($brow['token']) . '",';
        $tokenoutput .= '"' . trim($brow['language']) . '",';
        $tokenoutput .= '"' . trim($brow['validfrom']) . '",';
        $tokenoutput .= '"' . trim($brow['validuntil']) . '",';
        $tokenoutput .= '"' . trim($brow['sent']) . '",';
        $tokenoutput .= '"' . trim($brow['remindersent']) . '",';
        $tokenoutput .= '"' . trim($brow['remindercount']) . '",';
        $tokenoutput .= '"' . trim($brow['completed']) . '",';
        $tokenoutput .= '"' . trim($brow['usesleft']) . '",';
        foreach ($attrfieldnames as $attr_name) {
            $tokenoutput .= '"' . trim($brow[$attr_name]) . '",';
        }
        $tokenoutput = substr($tokenoutput, 0, -1);
        // remove last comma
        $tokenoutput .= "\n";
        echo $tokenoutput;
        $tokenoutput = '';
        $aExportedTokens[] = $brow['tid'];
    }
    if (Yii::app()->request->getPost('tokendeleteexported') && !empty($aExportedTokens)) {
        Token::model($iSurveyID)->deleteByPk($aExportedTokens);
    }
}
Exemplo n.º 6
0
     $lname[0] = Yii::app()->session['adminlang'] . ":" . $getlangvalues[Yii::app()->session['adminlang']]['description'];
 }
 foreach ($getlangvalues as $keycode => $keydesc)
 {
     if (Yii::app()->session['adminlang'] != $keycode)
     {
         $cleanlangdesc = str_replace(";", " -", $keydesc['description']);
         $lname[$j] = $keycode . ":" . $cleanlangdesc;
         $j++;
     }
 }
 $langnames = implode(";", $lname);
 /* Build the columnNames for the extra attributes */
 /* and, build the columnModel */
 $names = getTokenFieldsAndNames($surveyid, true);
 $attributes = getAttributeFieldNames($surveyid);
 if (count($attributes) > 0)
 {
     foreach ($names as $name)
     {
         $attnames[] = '"' . $name . '"';
     }
     foreach ($attributes as $row)
     {
         $uidNames[] = '{ "name":"' . $row . '", "index":"' . $row . '", "sorttype":"string", "sortable": true, "align":"center", "editable":true, "width":75}';
     }
     $columnNames = implode(',', $attnames); //Add to the end of the standard list of columnNames
 }
 else
 {
     $columnNames = "";
Exemplo n.º 7
0
function getTokenData($surveyid, $token)
{
    Tokens_dynamic::sid($surveyid);
    $query = Tokens_dynamic::model()->findAll('token = :token', array(':token' => $token));
    // while($row=$result->FetchRow())
    $thistoken = array();
    // so has default value
    foreach ($query as $row) {
        $thistoken = array("firstname" => $row->firstname, "lastname" => $row->lastname, "email" => $row->email, "language" => $row->language);
        $attrfieldnames = getAttributeFieldNames($surveyid);
        foreach ($attrfieldnames as $attr_name) {
            $thistoken[$attr_name] = $row[$attr_name];
        }
    }
    // while
    return $thistoken;
}
Exemplo n.º 8
0
 /**
  * import from csv
  */
 public function import($iSurveyId)
 {
     $aData = array();
     $iSurveyId = (int) $iSurveyId;
     if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'import')) {
         Yii::app()->session['flashmessage'] = gT("You do not have permission to access this page.");
         $this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}"));
     }
     // CHECK TO SEE IF A TOKEN TABLE EXISTS FOR THIS SURVEY
     $bTokenExists = tableExists('{{tokens_' . $iSurveyId . '}}');
     if (!$bTokenExists) {
         self::_newtokentable($iSurveyId);
     }
     $surveyinfo = Survey::model()->findByPk($iSurveyId)->surveyinfo;
     $aData['sidemenu']['state'] = false;
     $aData["surveyinfo"] = $surveyinfo;
     $aData['title_bar']['title'] = $surveyinfo['surveyls_title'] . "(" . gT("ID") . ":" . $iSurveyId . ")";
     $aData['sidemenu']["token_menu"] = TRUE;
     $aData['token_bar']['closebutton']['url'] = 'admin/tokens/sa/index/surveyid/' . $iSurveyId;
     $this->registerScriptFile('ADMIN_SCRIPT_PATH', 'tokensimport.js');
     $aEncodings = aEncodingsArray();
     if (Yii::app()->request->isPostRequest) {
         $sUploadCharset = Yii::app()->request->getPost('csvcharset');
         if (!array_key_exists($sUploadCharset, $aEncodings)) {
             $sUploadCharset = 'auto';
         }
         $bFilterDuplicateToken = Yii::app()->request->getPost('filterduplicatetoken');
         $bFilterBlankEmail = Yii::app()->request->getPost('filterblankemail');
         $bAllowInvalidEmail = Yii::app()->request->getPost('allowinvalidemail');
         $aAttrFieldNames = getAttributeFieldNames($iSurveyId);
         $aDuplicateList = array();
         $aInvalidTokenList = array();
         $aInvalidEmailList = array();
         $aInvalidFormatList = array();
         $aModelErrorList = array();
         $aFirstLine = array();
         $oFile = CUploadedFile::getInstanceByName("the_file");
         $sPath = Yii::app()->getConfig('tempdir');
         $sFileName = $sPath . '/' . randomChars(20);
         if ($_FILES['the_file']['error'] == 1 || $_FILES['the_file']['error'] == 2) {
             Yii::app()->setFlashMessage(sprintf(gT("Sorry, this file is too large. Only files up to %01.2f MB are allowed."), getMaximumFileUploadSize() / 1024 / 1024), 'error');
         } elseif (strtolower($oFile->getExtensionName()) != 'csv') {
             Yii::app()->setFlashMessage(gT("Only CSV files are allowed."), 'error');
         } elseif (!@$oFile->saveAs($sFileName)) {
             Yii::app()->setFlashMessage(sprintf(gT("Upload file not found. Check your permissions and path (%s) for the upload directory"), $sPath), 'error');
         } else {
             $iRecordImported = 0;
             $iRecordCount = 0;
             $iRecordOk = 0;
             $iInvalidEmailCount = 0;
             // Count invalid email imported
             // This allows to read file with MAC line endings too
             @ini_set('auto_detect_line_endings', true);
             // open it and trim the ednings
             $aTokenListArray = file($sFileName);
             $sBaseLanguage = Survey::model()->findByPk($iSurveyId)->language;
             if (!Yii::app()->request->getPost('filterduplicatefields') || Yii::app()->request->getPost('filterduplicatefields') && count(Yii::app()->request->getPost('filterduplicatefields')) == 0) {
                 $aFilterDuplicateFields = array('firstname', 'lastname', 'email');
             } else {
                 $aFilterDuplicateFields = Yii::app()->request->getPost('filterduplicatefields');
             }
             $sSeparator = Yii::app()->request->getPost('separator');
             $aMissingAttrFieldName = $aInvalideAttrFieldName = array();
             foreach ($aTokenListArray as $buffer) {
                 $buffer = @mb_convert_encoding($buffer, "UTF-8", $sUploadCharset);
                 if ($iRecordCount == 0) {
                     // Parse first line (header) from CSV
                     $buffer = removeBOM($buffer);
                     // We alow all field except tid because this one is really not needed.
                     $aAllowedFieldNames = Token::model($iSurveyId)->tableSchema->getColumnNames();
                     if (($kTid = array_search('tid', $aAllowedFieldNames)) !== false) {
                         unset($aAllowedFieldNames[$kTid]);
                     }
                     // Some header don't have same column name
                     $aReplacedFields = array('invited' => 'sent', 'reminded' => 'remindersent');
                     switch ($sSeparator) {
                         case 'comma':
                             $sSeparator = ',';
                             break;
                         case 'semicolon':
                             $sSeparator = ';';
                             break;
                         default:
                             $comma = substr_count($buffer, ',');
                             $semicolon = substr_count($buffer, ';');
                             if ($semicolon > $comma) {
                                 $sSeparator = ';';
                             } else {
                                 $sSeparator = ',';
                             }
                     }
                     $aFirstLine = str_getcsv($buffer, $sSeparator, '"');
                     $aFirstLine = array_map('trim', $aFirstLine);
                     $aIgnoredColumns = array();
                     // Now check the first line for invalid fields
                     foreach ($aFirstLine as $index => $sFieldname) {
                         $aFirstLine[$index] = preg_replace("/(.*) <[^,]*>\$/", "\$1", $sFieldname);
                         $sFieldname = $aFirstLine[$index];
                         if (!in_array($sFieldname, $aAllowedFieldNames)) {
                             $aIgnoredColumns[] = $sFieldname;
                         }
                         if (array_key_exists($sFieldname, $aReplacedFields)) {
                             $aFirstLine[$index] = $aReplacedFields[$sFieldname];
                         }
                         // Attribute not in list
                         if (strpos($aFirstLine[$index], 'attribute_') !== false and !in_array($aFirstLine[$index], $aAttrFieldNames) and Yii::app()->request->getPost('showwarningtoken')) {
                             $aInvalideAttrFieldName[] = $aFirstLine[$index];
                         }
                     }
                     //compare attributes with source csv
                     if (Yii::app()->request->getPost('showwarningtoken')) {
                         $aMissingAttrFieldName = array_diff($aAttrFieldNames, $aFirstLine);
                         // get list of mandatory attributes
                         $allAttrFieldNames = GetParticipantAttributes($iSurveyId);
                         //if it isn't mandantory field we don't need to show in warning
                         if (!empty($aAttrFieldNames)) {
                             if (!empty($aMissingAttrFieldName)) {
                                 foreach ($aMissingAttrFieldName as $index => $AttrFieldName) {
                                     if (isset($allAttrFieldNames[$AttrFieldName]) and strtolower($allAttrFieldNames[$AttrFieldName]["mandatory"]) != "y") {
                                         unset($aMissingAttrFieldName[$index]);
                                     }
                                 }
                             }
                             if (isset($aInvalideAttrFieldName) and !empty($aInvalideAttrFieldName)) {
                                 foreach ($aInvalideAttrFieldName as $index => $AttrFieldName) {
                                     if (isset($allAttrFieldNames[$AttrFieldName]) and strtolower($allAttrFieldNames[$AttrFieldName]["mandatory"]) != "y") {
                                         unset($aInvalideAttrFieldName[$index]);
                                     }
                                 }
                             }
                         }
                     }
                 } else {
                     $line = str_getcsv($buffer, $sSeparator, '"');
                     if (count($aFirstLine) != count($line)) {
                         $aInvalidFormatList[] = sprintf(gT("Line %s"), $iRecordCount);
                         $iRecordCount++;
                         continue;
                     }
                     $aWriteArray = array_combine($aFirstLine, $line);
                     //kick out ignored columns
                     foreach ($aIgnoredColumns as $column) {
                         unset($aWriteArray[$column]);
                     }
                     $bDuplicateFound = false;
                     $bInvalidEmail = false;
                     $bInvalidToken = false;
                     $aWriteArray['email'] = isset($aWriteArray['email']) ? trim($aWriteArray['email']) : "";
                     $aWriteArray['firstname'] = isset($aWriteArray['firstname']) ? $aWriteArray['firstname'] : "";
                     $aWriteArray['lastname'] = isset($aWriteArray['lastname']) ? $aWriteArray['lastname'] : "";
                     $aWriteArray['language'] = isset($aWriteArray['language']) ? $aWriteArray['language'] : $sBaseLanguage;
                     if ($bFilterDuplicateToken) {
                         $aParams = array();
                         $oCriteria = new CDbCriteria();
                         $oCriteria->condition = "";
                         foreach ($aFilterDuplicateFields as $field) {
                             if (isset($aWriteArray[$field])) {
                                 $oCriteria->addCondition("{$field} = :{$field}");
                                 $aParams[":{$field}"] = $aWriteArray[$field];
                             }
                         }
                         if (!empty($aParams)) {
                             $oCriteria->params = $aParams;
                         }
                         $dupresult = TokenDynamic::model($iSurveyId)->count($oCriteria);
                         if ($dupresult > 0) {
                             $bDuplicateFound = true;
                             $aDuplicateList[] = sprintf(gT("Line %s : %s %s (%s)"), $iRecordCount, $aWriteArray['firstname'], $aWriteArray['lastname'], $aWriteArray['email']);
                         }
                     }
                     //treat blank emails
                     if (!$bDuplicateFound && $bFilterBlankEmail && $aWriteArray['email'] == '') {
                         $bInvalidEmail = true;
                         $aInvalidEmailList[] = sprintf(gT("Line %s : %s %s"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']));
                     }
                     if (!$bDuplicateFound && $aWriteArray['email'] != '') {
                         $aEmailAddresses = preg_split("/(,|;)/", $aWriteArray['email']);
                         foreach ($aEmailAddresses as $sEmailaddress) {
                             if (!validateEmailAddress($sEmailaddress)) {
                                 if ($bAllowInvalidEmail) {
                                     $iInvalidEmailCount++;
                                     if (empty($aWriteArray['emailstatus']) || strtoupper($aWriteArray['emailstatus'] == "OK")) {
                                         $aWriteArray['emailstatus'] = "invalid";
                                     }
                                 } else {
                                     $bInvalidEmail = true;
                                     $aInvalidEmailList[] = sprintf(gT("Line %s : %s %s (%s)"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']), CHtml::encode($aWriteArray['email']));
                                 }
                             }
                         }
                     }
                     if (!$bDuplicateFound && !$bInvalidEmail && isset($aWriteArray['token']) && trim($aWriteArray['token']) != '') {
                         if (trim($aWriteArray['token']) != sanitize_token($aWriteArray['token'])) {
                             $aInvalidTokenList[] = sprintf(gT("Line %s : %s %s (%s) - token : %s"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']), CHtml::encode($aWriteArray['email']), CHtml::encode($aWriteArray['token']));
                             $bInvalidToken = true;
                         }
                         // We allways search for duplicate token (it's in model. Allow to reset or update token ?
                         if (Token::model($iSurveyId)->count("token=:token", array(":token" => $aWriteArray['token']))) {
                             $bDuplicateFound = true;
                             $aDuplicateList[] = sprintf(gT("Line %s : %s %s (%s) - token : %s"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']), CHtml::encode($aWriteArray['email']), CHtml::encode($aWriteArray['token']));
                         }
                     }
                     if (!$bDuplicateFound && !$bInvalidEmail && !$bInvalidToken) {
                         // unset all empty value
                         foreach ($aWriteArray as $key => $value) {
                             if ($aWriteArray[$key] == "") {
                                 unset($aWriteArray[$key]);
                             }
                             if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {
                                 // Fix CSV quote
                                 $value = substr($value, 1, -1);
                             }
                         }
                         // Some default value : to be moved to Token model rules in future release ?
                         // But think we have to accept invalid email etc ... then use specific scenario
                         $oToken = Token::create($iSurveyId);
                         if ($bAllowInvalidEmail) {
                             $oToken->scenario = 'allowinvalidemail';
                         }
                         foreach ($aWriteArray as $key => $value) {
                             $oToken->{$key} = $value;
                         }
                         if (!$oToken->save()) {
                             $errors = $oToken->getErrors();
                             $aModelErrorList[] = sprintf(gT("Line %s : %s"), $iRecordCount, print_r($errors, true));
                         } else {
                             $iRecordImported++;
                         }
                     }
                     $iRecordOk++;
                 }
                 $iRecordCount++;
             }
             $iRecordCount = $iRecordCount - 1;
             unlink($sFileName);
             $aData['aTokenListArray'] = $aTokenListArray;
             // Big array in memory, just for success ?
             $aData['iRecordImported'] = $iRecordImported;
             $aData['iRecordOk'] = $iRecordOk;
             $aData['iRecordCount'] = $iRecordCount;
             $aData['aFirstLine'] = $aFirstLine;
             // Seem not needed
             $aData['aDuplicateList'] = $aDuplicateList;
             $aData['aInvalidTokenList'] = $aInvalidTokenList;
             $aData['aInvalidFormatList'] = $aInvalidFormatList;
             $aData['aInvalidEmailList'] = $aInvalidEmailList;
             $aData['aModelErrorList'] = $aModelErrorList;
             $aData['iInvalidEmailCount'] = $iInvalidEmailCount;
             $aData['thissurvey'] = getSurveyInfo($iSurveyId);
             $aData['iSurveyId'] = $aData['surveyid'] = $iSurveyId;
             $aData['aInvalideAttrFieldName'] = $aInvalideAttrFieldName;
             $aData['aMissingAttrFieldName'] = $aMissingAttrFieldName;
             $this->_renderWrappedTemplate('token', array('csvimportresult'), $aData);
             Yii::app()->end();
         }
     }
     // If there are error with file : show the form
     $aData['aEncodings'] = $aEncodings;
     asort($aData['aEncodings']);
     $aData['iSurveyId'] = $iSurveyId;
     $aData['thissurvey'] = getSurveyInfo($iSurveyId);
     $aData['surveyid'] = $iSurveyId;
     $aTokenTableFields = getTokenFieldsAndNames($iSurveyId);
     unset($aTokenTableFields['sent']);
     unset($aTokenTableFields['remindersent']);
     unset($aTokenTableFields['remindercount']);
     unset($aTokenTableFields['usesleft']);
     foreach ($aTokenTableFields as $sKey => $sValue) {
         if ($sValue['description'] != $sKey) {
             $sValue['description'] .= ' - ' . $sKey;
         }
         $aNewTokenTableFields[$sKey] = $sValue['description'];
     }
     $aData['aTokenTableFields'] = $aNewTokenTableFields;
     // Get default character set from global settings
     $thischaracterset = getGlobalSetting('characterset');
     // If no encoding was set yet, use the old "auto" default
     if ($thischaracterset == "") {
         $thischaracterset = "auto";
     }
     $aData['thischaracterset'] = $thischaracterset;
     $this->_renderWrappedTemplate('token', array('csvupload'), $aData);
 }
Exemplo n.º 9
0
                echo CHtml::dropDownList('separator', returnGlobal('separator'), $aSeparator, array('size' => '1'));
            ?>
        </li>
        <li>
            <label for='filterblankemail'><?php $clang->eT("Filter blank email addresses:"); ?></label>
            <input type='checkbox' id='filterblankemail' name='filterblankemail' checked='checked' />
        </li>
        <li>
            <label for='filterduplicatetoken'><?php $clang->eT("Filter duplicate records:"); ?></label>
            <input type='checkbox' id='filterduplicatetoken' name='filterduplicatetoken' checked='checked' />
        </li>
        <li id='lifilterduplicatefields'>
            <label for='filterduplicatefields'><?php $clang->eT("Duplicates are determined by:"); ?></label>
            <?php
                $aFilterDuplicateFields = array('firstname' => 'firstname', 'lastname' => 'lastname', 'email' => 'email', 'token' => 'token', 'language' => 'language');
                array_merge($aFilterDuplicateFields, getAttributeFieldNames($iSurveyId));
                echo CHtml::listBox('filterduplicatefields', array('firstname', 'lastname', 'email'), $aFilterDuplicateFields, array('multiple' => 'multiple', 'size' => '5'));
            ?>
        </li>
    </ul>
    <p>
        <input class='submit' type='submit' name='submit' value='<?php $clang->eT("Upload"); ?>' />
        <input type='hidden' name='subaction' value='upload' />
        <input type='hidden' name='sid' value='$iSurveyId' />
    </p>
</form>
<div class='messagebox ui-corner-all'>
    <div class='header ui-widget-header'><?php $clang->eT("CSV input format"); ?></div>
    <p><?php $clang->eT("File should be a standard CSV (comma delimited) file with optional double quotes around values (default for OpenOffice and Excel). The first line must contain the field names. The fields can be in any order."); ?></p>
    <span style="font-weight:bold;"><?php $clang->eT("Mandatory fields:"); ?></span> firstname, lastname, email<br />
    <span style="font-weight:bold;"><?php $clang->eT('Optional fields:'); ?></span> emailstatus, token, language, validfrom, validuntil, attribute_1, attribute_2, attribute_3, usesleft, ... .
Exemplo n.º 10
0
/**
 * Marks a tokens as completed and sends a confirmation email to the participiant.
 * If $quotaexit is set to true then the user exited the survey due to a quota
 * restriction and the according token is only marked as 'Q'
 *
 * @param mixed $quotaexit
 */
function submittokens($quotaexit = false)
{
    global $thissurvey;
    global $surveyid;
    global $clienttoken;
    $clang = Yii::app()->lang;
    $sitename = Yii::app()->getConfig("sitename");
    $emailcharset = Yii::app()->getConfig("emailcharset");
    // Shift the date due to global timeadjust setting
    $today = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"));
    // check how many uses the token has left
    $usesquery = "SELECT usesleft, participant_id, tid FROM {{tokens_{$surveyid}}} WHERE token='" . $clienttoken . "'";
    $usesresult = dbExecuteAssoc($usesquery);
    $usesrow = $usesresult->read();
    if (isset($usesrow)) {
        $usesleft = $usesrow['usesleft'];
        $participant_id = $usesrow['participant_id'];
        $token_id = $usesrow['tid'];
    }
    $utquery = "UPDATE {{tokens_{$surveyid}}}\n";
    if ($quotaexit == true) {
        $utquery .= "SET completed='Q', usesleft=usesleft-1\n";
    } elseif (isTokenCompletedDatestamped($thissurvey)) {
        if (isset($usesleft) && $usesleft <= 1) {
            $utquery .= "SET usesleft=usesleft-1, completed='{$today}'\n";
            if (!empty($participant_id)) {
                //Update the survey_links table if necessary
                $slquery = Survey_links::model()->find('participant_id = "' . $participant_id . '" AND survey_id = ' . $surveyid . ' AND token_id = ' . $token_id);
                $slquery->date_completed = $today;
                $slquery->save();
            }
        } else {
            $utquery .= "SET usesleft=usesleft-1\n";
        }
    } else {
        if (isset($usesleft) && $usesleft <= 1) {
            $utquery .= "SET usesleft=usesleft-1, completed='Y'\n";
            if (!empty($participant_id)) {
                //Update the survey_links table if necessary, to protect anonymity, use the date_created field date
                $slquery = Survey_links::model()->find('participant_id = "' . $participant_id . '" AND survey_id = ' . $surveyid . ' AND token_id = ' . $token_id);
                $slquery->date_completed = $slquery->date_created;
                $slquery->save();
            }
        } else {
            $utquery .= "SET usesleft=usesleft-1\n";
        }
    }
    $utquery .= "WHERE token='" . $clienttoken . "'";
    $utresult = dbExecuteAssoc($utquery) or safeDie("Couldn't update tokens table!<br />\n{$utquery}<br />\n");
    //Checked
    if ($quotaexit == false) {
        // TLR change to put date into sent and completed
        $cnfquery = "SELECT * FROM {{tokens_{$surveyid}}} WHERE token='" . $clienttoken . "' AND completed!='N' AND completed!=''";
        $cnfresult = dbExecuteAssoc($cnfquery);
        //Checked
        $cnfrow = $cnfresult->read();
        if (isset($cnfrow)) {
            $from = "{$thissurvey['adminname']} <{$thissurvey['adminemail']}>";
            $to = $cnfrow['email'];
            $subject = $thissurvey['email_confirm_subj'];
            $fieldsarray["{ADMINNAME}"] = $thissurvey['adminname'];
            $fieldsarray["{ADMINEMAIL}"] = $thissurvey['adminemail'];
            $fieldsarray["{SURVEYNAME}"] = $thissurvey['name'];
            $fieldsarray["{SURVEYDESCRIPTION}"] = $thissurvey['description'];
            $fieldsarray["{FIRSTNAME}"] = $cnfrow['firstname'];
            $fieldsarray["{LASTNAME}"] = $cnfrow['lastname'];
            $fieldsarray["{TOKEN}"] = $clienttoken;
            $attrfieldnames = getAttributeFieldNames($surveyid);
            foreach ($attrfieldnames as $attr_name) {
                $fieldsarray["{" . strtoupper($attr_name) . "}"] = $cnfrow[$attr_name];
            }
            $dateformatdatat = getDateFormatData($thissurvey['surveyls_dateformat']);
            $numberformatdatat = getRadixPointData($thissurvey['surveyls_numberformat']);
            $fieldsarray["{EXPIRY}"] = convertDateTimeFormat($thissurvey["expiry"], 'Y-m-d H:i:s', $dateformatdatat['phpdate']);
            $subject = ReplaceFields($subject, $fieldsarray, true);
            $subject = html_entity_decode($subject, ENT_QUOTES, $emailcharset);
            if (getEmailFormat($surveyid) == 'html') {
                $ishtml = true;
            } else {
                $ishtml = false;
            }
            if (trim(strip_tags($thissurvey['email_confirm'])) != "" && $thissurvey['sendconfirmation'] == "Y") {
                $message = $thissurvey['email_confirm'];
                $message = ReplaceFields($message, $fieldsarray, true);
                if (!$ishtml) {
                    $message = strip_tags(breakToNewline(html_entity_decode($message, ENT_QUOTES, $emailcharset)));
                } else {
                    $message = html_entity_decode($message, ENT_QUOTES, $emailcharset);
                }
                //Only send confirmation email if there is a valid email address
                if (validateEmailAddress($cnfrow['email'])) {
                    SendEmailMessage($message, $subject, $to, $from, $sitename, $ishtml);
                }
            } else {
                //There is nothing in the message or "Send confirmation emails" is set to "No" , so don't send a confirmation email
                //This section only here as placeholder to indicate new feature :-)
            }
        }
    }
}
Exemplo n.º 11
0
 /**
  * import from csv
  */
 function import($iSurveyId)
 {
     $iSurveyId = (int) $iSurveyId;
     if (!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'import')) {
         Yii::app()->session['flashmessage'] = gT("You do not have sufficient rights to access this page.");
         $this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}"));
     }
     // CHECK TO SEE IF A TOKEN TABLE EXISTS FOR THIS SURVEY
     $bTokenExists = tableExists('{{tokens_' . $iSurveyId . '}}');
     if (!$bTokenExists) {
         self::_newtokentable($iSurveyId);
     }
     App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('adminscripts') . 'tokensimport.js');
     $aEncodings = aEncodingsArray();
     if (Yii::app()->request->isPostRequest) {
         $sUploadCharset = Yii::app()->request->getPost('csvcharset');
         if (!array_key_exists($sUploadCharset, $aEncodings)) {
             $sUploadCharset = 'auto';
         }
         $bFilterDuplicateToken = Yii::app()->request->getPost('filterduplicatetoken');
         $bFilterBlankEmail = Yii::app()->request->getPost('filterblankemail');
         $bAllowInvalidEmail = Yii::app()->request->getPost('allowinvalidemail');
         $aAttrFieldNames = getAttributeFieldNames($iSurveyId);
         $aDuplicateList = array();
         $aInvalidEmailList = array();
         $aInvalidFormatList = array();
         $aModelErrorList = array();
         $aFirstLine = array();
         $oFile = CUploadedFile::getInstanceByName("the_file");
         $sPath = Yii::app()->getConfig('tempdir');
         $sFileName = $sPath . '/' . randomChars(20);
         //$sFileTmpName=$oFile->getTempName();
         /* More way to validate CSV ?
            $aCsvMimetypes = array(
                'text/csv',
                'text/plain',
                'application/csv',
                'text/comma-separated-values',
                'application/excel',
                'application/vnd.ms-excel',
                'application/vnd.msexcel',
                'text/anytext',
                'application/octet-stream',
                'application/txt',
            );
            */
         if (strtolower($oFile->getExtensionName()) != 'csv') {
             Yii::app()->setFlashMessage(gT("Only CSV files are allowed."), 'error');
         } elseif (!@$oFile->saveAs($sFileName)) {
             Yii::app()->setFlashMessage(sprintf(gT("Upload file not found. Check your permissions and path (%s) for the upload directory"), $sPath), 'error');
         } else {
             $iRecordImported = 0;
             $iRecordCount = 0;
             $iRecordOk = 0;
             $iInvalidEmailCount = 0;
             // Count invalid email imported
             // This allows to read file with MAC line endings too
             @ini_set('auto_detect_line_endings', true);
             // open it and trim the ednings
             $aTokenListArray = file($sFileName);
             $sBaseLanguage = Survey::model()->findByPk($iSurveyId)->language;
             if (!Yii::app()->request->getPost('filterduplicatefields') || Yii::app()->request->getPost('filterduplicatefields') && count(Yii::app()->request->getPost('filterduplicatefields')) == 0) {
                 $aFilterDuplicateFields = array('firstname', 'lastname', 'email');
             } else {
                 $aFilterDuplicateFields = Yii::app()->request->getPost('filterduplicatefields');
             }
             $sSeparator = Yii::app()->request->getPost('separator');
             foreach ($aTokenListArray as $buffer) {
                 $buffer = @mb_convert_encoding($buffer, "UTF-8", $sUploadCharset);
                 if ($iRecordCount == 0) {
                     // Parse first line (header) from CSV
                     $buffer = removeBOM($buffer);
                     // We alow all field except tid because this one is really not needed.
                     $aAllowedFieldNames = Token::model($iSurveyId)->tableSchema->getColumnNames();
                     if (($kTid = array_search('tid', $aAllowedFieldNames)) !== false) {
                         unset($aAllowedFieldNames[$kTid]);
                     }
                     // Some header don't have same column name
                     $aReplacedFields = array('invited' => 'sent', 'reminded' => 'remindersent');
                     switch ($sSeparator) {
                         case 'comma':
                             $sSeparator = ',';
                             break;
                         case 'semicolon':
                             $sSeparator = ';';
                             break;
                         default:
                             $comma = substr_count($buffer, ',');
                             $semicolon = substr_count($buffer, ';');
                             if ($semicolon > $comma) {
                                 $sSeparator = ';';
                             } else {
                                 $sSeparator = ',';
                             }
                     }
                     $aFirstLine = str_getcsv($buffer, $sSeparator, '"');
                     $aFirstLine = array_map('trim', $aFirstLine);
                     $aIgnoredColumns = array();
                     // Now check the first line for invalid fields
                     foreach ($aFirstLine as $index => $sFieldname) {
                         $aFirstLine[$index] = preg_replace("/(.*) <[^,]*>\$/", "\$1", $sFieldname);
                         $sFieldname = $aFirstLine[$index];
                         if (!in_array($sFieldname, $aAllowedFieldNames)) {
                             $aIgnoredColumns[] = $sFieldname;
                         }
                         if (array_key_exists($sFieldname, $aReplacedFields)) {
                             $aFirstLine[$index] = $aReplacedFields[$sFieldname];
                         }
                     }
                 } else {
                     $line = str_getcsv($buffer, $sSeparator, '"');
                     if (count($aFirstLine) != count($line)) {
                         $aInvalidFormatList[] = sprintf(gt("Line %s"), $iRecordCount);
                         $iRecordCount++;
                         continue;
                     }
                     $aWriteArray = array_combine($aFirstLine, $line);
                     //kick out ignored columns
                     foreach ($aIgnoredColumns as $column) {
                         unset($aWriteArray[$column]);
                     }
                     $bDuplicateFound = false;
                     $bInvalidEmail = false;
                     $aWriteArray['email'] = isset($aWriteArray['email']) ? trim($aWriteArray['email']) : "";
                     $aWriteArray['firstname'] = isset($aWriteArray['firstname']) ? $aWriteArray['firstname'] : "";
                     $aWriteArray['lastname'] = isset($aWriteArray['lastname']) ? $aWriteArray['lastname'] : "";
                     $aWriteArray['language'] = isset($aWriteArray['language']) ? $aWriteArray['language'] : $sBaseLanguage;
                     if ($bFilterDuplicateToken) {
                         $aParams = array();
                         $oCriteria = new CDbCriteria();
                         $oCriteria->condition = "";
                         foreach ($aFilterDuplicateFields as $field) {
                             if (isset($aWriteArray[$field])) {
                                 $oCriteria->addCondition("{$field} = :{$field}");
                                 $aParams[":{$field}"] = $aWriteArray[$field];
                             }
                         }
                         if (!empty($aParams)) {
                             $oCriteria->params = $aParams;
                         }
                         $dupresult = TokenDynamic::model($iSurveyId)->count($oCriteria);
                         if ($dupresult > 0) {
                             $bDuplicateFound = true;
                             $aDuplicateList[] = sprintf(gt("Line %s : %s %s (%s)"), $iRecordCount, $aWriteArray['firstname'], $aWriteArray['lastname'], $aWriteArray['email']);
                         }
                     }
                     //treat blank emails
                     if (!$bDuplicateFound && $bFilterBlankEmail && $aWriteArray['email'] == '') {
                         $bInvalidEmail = true;
                         $aInvalidEmailList[] = sprintf(gt("Line %s : %s %s"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']));
                     }
                     if (!$bDuplicateFound && $aWriteArray['email'] != '') {
                         $aEmailAddresses = explode(';', $aWriteArray['email']);
                         foreach ($aEmailAddresses as $sEmailaddress) {
                             if (!validateEmailAddress($sEmailaddress)) {
                                 if ($bAllowInvalidEmail) {
                                     $iInvalidEmailCount++;
                                     if (empty($aWriteArray['emailstatus']) || strtoupper($aWriteArray['emailstatus'] == "OK")) {
                                         $aWriteArray['emailstatus'] = "invalid";
                                     }
                                 } else {
                                     $bInvalidEmail = true;
                                     $aInvalidEmailList[] = sprintf(gt("Line %s : %s %s (%s)"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']), CHtml::encode($aWriteArray['email']));
                                 }
                             }
                         }
                     }
                     if (!$bDuplicateFound && !$bInvalidEmail && isset($aWriteArray['token'])) {
                         $aWriteArray['token'] = sanitize_token($aWriteArray['token']);
                         // We allways search for duplicate token (it's in model. Allow to reset or update token ?
                         if (Token::model($iSurveyId)->count("token=:token", array(":token" => $aWriteArray['token']))) {
                             $bDuplicateFound = true;
                             $aDuplicateList[] = sprintf(gt("Line %s : %s %s (%s) - token : %s"), $iRecordCount, CHtml::encode($aWriteArray['firstname']), CHtml::encode($aWriteArray['lastname']), CHtml::encode($aWriteArray['email']), CHtml::encode($aWriteArray['token']));
                         }
                     }
                     if (!$bDuplicateFound && !$bInvalidEmail) {
                         // unset all empty value
                         foreach ($aWriteArray as $key => $value) {
                             if ($aWriteArray[$key] == "") {
                                 unset($aWriteArray[$key]);
                             }
                             if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {
                                 // Fix CSV quote
                                 $value = substr($value, 1, -1);
                             }
                         }
                         // Some default value : to be moved to Token model rules in future release ?
                         // But think we have to accept invalid email etc ... then use specific scenario
                         $oToken = Token::create($iSurveyId);
                         if ($bAllowInvalidEmail) {
                             $oToken->scenario = 'allowinvalidemail';
                         }
                         foreach ($aWriteArray as $key => $value) {
                             $oToken->{$key} = $value;
                         }
                         if (!$oToken->save()) {
                             tracevar($oToken->getErrors());
                             $aModelErrorList[] = sprintf(gt("Line %s : %s"), $iRecordCount, Chtml::errorSummary($oToken));
                         } else {
                             $iRecordImported++;
                         }
                     }
                     $iRecordOk++;
                 }
                 $iRecordCount++;
             }
             $iRecordCount = $iRecordCount - 1;
             unlink($sFileName);
             $aData['aTokenListArray'] = $aTokenListArray;
             // Big array in memory, just for success ?
             $aData['iRecordImported'] = $iRecordImported;
             $aData['iRecordOk'] = $iRecordOk;
             $aData['iRecordCount'] = $iRecordCount;
             $aData['aFirstLine'] = $aFirstLine;
             // Seem not needed
             $aData['aDuplicateList'] = $aDuplicateList;
             $aData['aInvalidFormatList'] = $aInvalidFormatList;
             $aData['aInvalidEmailList'] = $aInvalidEmailList;
             $aData['aModelErrorList'] = $aModelErrorList;
             $aData['iInvalidEmailCount'] = $iInvalidEmailCount;
             $aData['thissurvey'] = getSurveyInfo($iSurveyId);
             $aData['iSurveyId'] = $aData['surveyid'] = $iSurveyId;
             $this->_renderWrappedTemplate('token', array('tokenbar', 'csvpost'), $aData);
             Yii::app()->end();
         }
     }
     // If there are error with file : show the form
     $aData['aEncodings'] = $aEncodings;
     $aData['iSurveyId'] = $iSurveyId;
     $aData['thissurvey'] = getSurveyInfo($iSurveyId);
     $aData['surveyid'] = $iSurveyId;
     $aTokenTableFields = getTokenFieldsAndNames($iSurveyId);
     unset($aTokenTableFields['sent']);
     unset($aTokenTableFields['remindersent']);
     unset($aTokenTableFields['remindercount']);
     unset($aTokenTableFields['usesleft']);
     foreach ($aTokenTableFields as $sKey => $sValue) {
         if ($sValue['description'] != $sKey) {
             $sValue['description'] .= ' - ' . $sKey;
         }
         $aNewTokenTableFields[$sKey] = $sValue['description'];
     }
     $aData['aTokenTableFields'] = $aNewTokenTableFields;
     $this->_renderWrappedTemplate('token', array('tokenbar', 'csvupload'), $aData);
 }
Exemplo n.º 12
0
 /**
  * import from csv
  */
 function import($iSurveyId)
 {
     // CHECK TO SEE IF A TOKEN TABLE EXISTS FOR THIS SURVEY
     $bTokenExists = tableExists('{{tokens_' . $iSurveyId . '}}');
     if (!$bTokenExists) {
         self::_newtokentable($iSurveyId);
     }
     $clang = $this->getController()->lang;
     $iSurveyId = (int) $iSurveyId;
     if (!hasSurveyPermission($iSurveyId, 'tokens', 'create')) {
         die('access denied');
     }
     $this->getController()->_js_admin_includes(Yii::app()->getConfig('adminscripts') . 'tokens.js');
     $aEncodings = array("armscii8" => $clang->gT("ARMSCII-8 Armenian"), "ascii" => $clang->gT("US ASCII"), "auto" => $clang->gT("Automatic"), "big5" => $clang->gT("Big5 Traditional Chinese"), "binary" => $clang->gT("Binary pseudo charset"), "cp1250" => $clang->gT("Windows Central European"), "cp1251" => $clang->gT("Windows Cyrillic"), "cp1256" => $clang->gT("Windows Arabic"), "cp1257" => $clang->gT("Windows Baltic"), "cp850" => $clang->gT("DOS West European"), "cp852" => $clang->gT("DOS Central European"), "cp866" => $clang->gT("DOS Russian"), "cp932" => $clang->gT("SJIS for Windows Japanese"), "dec8" => $clang->gT("DEC West European"), "eucjpms" => $clang->gT("UJIS for Windows Japanese"), "euckr" => $clang->gT("EUC-KR Korean"), "gb2312" => $clang->gT("GB2312 Simplified Chinese"), "gbk" => $clang->gT("GBK Simplified Chinese"), "geostd8" => $clang->gT("GEOSTD8 Georgian"), "greek" => $clang->gT("ISO 8859-7 Greek"), "hebrew" => $clang->gT("ISO 8859-8 Hebrew"), "hp8" => $clang->gT("HP West European"), "keybcs2" => $clang->gT("DOS Kamenicky Czech-Slovak"), "koi8r" => $clang->gT("KOI8-R Relcom Russian"), "koi8u" => $clang->gT("KOI8-U Ukrainian"), "latin1" => $clang->gT("cp1252 West European"), "latin2" => $clang->gT("ISO 8859-2 Central European"), "latin5" => $clang->gT("ISO 8859-9 Turkish"), "latin7" => $clang->gT("ISO 8859-13 Baltic"), "macce" => $clang->gT("Mac Central European"), "macroman" => $clang->gT("Mac West European"), "sjis" => $clang->gT("Shift-JIS Japanese"), "swe7" => $clang->gT("7bit Swedish"), "tis620" => $clang->gT("TIS620 Thai"), "ucs2" => $clang->gT("UCS-2 Unicode"), "ujis" => $clang->gT("EUC-JP Japanese"), "utf8" => $clang->gT("UTF-8 Unicode"));
     if (Yii::app()->request->getPost('submit')) {
         if (Yii::app()->request->getPost('csvcharset') && Yii::app()->request->getPost('csvcharset')) {
             $uploadcharset = Yii::app()->request->getPost('csvcharset');
             if (!array_key_exists($uploadcharset, $aEncodings)) {
                 $uploadcharset = 'auto';
             }
             $filterduplicatetoken = Yii::app()->request->getPost('filterduplicatetoken') && Yii::app()->request->getPost('filterduplicatetoken') == 'on';
             $filterblankemail = Yii::app()->request->getPost('filterblankemail') && Yii::app()->request->getPost('filterblankemail') == 'on';
         }
         $attrfieldnames = getAttributeFieldNames($iSurveyId);
         $duplicatelist = array();
         $invalidemaillist = array();
         $invalidformatlist = array();
         $firstline = array();
         $sPath = Yii::app()->getConfig('tempdir');
         $sFileName = $_FILES['the_file']['name'];
         $sFileTmpName = $_FILES['the_file']['tmp_name'];
         $sFilePath = $sPath . '/' . $sFileName;
         if (!@move_uploaded_file($sFileTmpName, $sFilePath)) {
             $aData['sError'] = $clang->gT("Upload file not found. Check your permissions and path ({$sFilePath}) for the upload directory");
             $aData['aEncodings'] = $aEncodings;
             $aData['iSurveyId'] = $aData['surveyid'] = $iSurveyId;
             $aData['thissurvey'] = getSurveyInfo($iSurveyId);
             $this->_renderWrappedTemplate('token', array('tokenbar', 'csvupload'), $aData);
         } else {
             $xz = 0;
             $recordcount = 0;
             $xv = 0;
             // This allows to read file with MAC line endings too
             @ini_set('auto_detect_line_endings', true);
             // open it and trim the ednings
             $tokenlistarray = file($sFilePath);
             $sBaseLanguage = Survey::model()->findByPk($iSurveyId)->language;
             if (!Yii::app()->request->getPost('filterduplicatefields') || Yii::app()->request->getPost('filterduplicatefields') && count(Yii::app()->request->getPost('filterduplicatefields')) == 0) {
                 $filterduplicatefields = array('firstname', 'lastname', 'email');
             } else {
                 $filterduplicatefields = Yii::app()->request->getPost('filterduplicatefields');
             }
             $separator = returnGlobal('separator');
             foreach ($tokenlistarray as $buffer) {
                 $buffer = @mb_convert_encoding($buffer, "UTF-8", $uploadcharset);
                 $firstname = "";
                 $lastname = "";
                 $email = "";
                 $emailstatus = "OK";
                 $token = "";
                 $language = "";
                 $attribute1 = "";
                 $attribute2 = "";
                 //Clear out values from the last path, in case the next line is missing a value
                 if ($recordcount == 0) {
                     // Pick apart the first line
                     $buffer = removeBOM($buffer);
                     $allowedfieldnames = array('firstname', 'lastname', 'email', 'emailstatus', 'token', 'language', 'validfrom', 'validuntil', 'usesleft');
                     $allowedfieldnames = array_merge($attrfieldnames, $allowedfieldnames);
                     switch ($separator) {
                         case 'comma':
                             $separator = ',';
                             break;
                         case 'semicolon':
                             $separator = ';';
                             break;
                         default:
                             $comma = substr_count($buffer, ',');
                             $semicolon = substr_count($buffer, ';');
                             if ($semicolon > $comma) {
                                 $separator = ';';
                             } else {
                                 $separator = ',';
                             }
                     }
                     $firstline = convertCSVRowToArray($buffer, $separator, '"');
                     $firstline = array_map('trim', $firstline);
                     $ignoredcolumns = array();
                     //now check the first line for invalid fields
                     foreach ($firstline as $index => $fieldname) {
                         $firstline[$index] = preg_replace("/(.*) <[^,]*>\$/", "\$1", $fieldname);
                         $fieldname = $firstline[$index];
                         if (!in_array($fieldname, $allowedfieldnames)) {
                             $ignoredcolumns[] = $fieldname;
                         }
                     }
                     if (!in_array('firstname', $firstline) || !in_array('lastname', $firstline) || !in_array('email', $firstline)) {
                         $recordcount = count($tokenlistarray);
                         break;
                     }
                 } else {
                     $line = convertCSVRowToArray($buffer, $separator, '"');
                     if (count($firstline) != count($line)) {
                         $invalidformatlist[] = $recordcount;
                         $recordcount++;
                         continue;
                     }
                     $writearray = array_combine($firstline, $line);
                     //kick out ignored columns
                     foreach ($ignoredcolumns as $column) {
                         unset($writearray[$column]);
                     }
                     $dupfound = false;
                     $invalidemail = false;
                     if ($filterduplicatetoken != false) {
                         $dupquery = "SELECT count(tid) from {{tokens_" . intval($iSurveyId) . "}} where 1=1";
                         foreach ($filterduplicatefields as $field) {
                             if (isset($writearray[$field])) {
                                 $dupquery .= " and " . Yii::app()->db->quoteColumnName($field) . " = " . Yii::app()->db->quoteValue($writearray[$field]);
                             }
                         }
                         $dupresult = Yii::app()->db->createCommand($dupquery)->queryScalar();
                         if ($dupresult > 0) {
                             $dupfound = true;
                             $duplicatelist[] = Yii::app()->db->quoteValue($writearray['firstname']) . " " . Yii::app()->db->quoteValue($writearray['lastname']) . " (" . Yii::app()->db->quoteValue($writearray['email']) . ")";
                         }
                     }
                     $writearray['email'] = trim($writearray['email']);
                     //treat blank emails
                     if ($filterblankemail && $writearray['email'] == '') {
                         $invalidemail = true;
                         $invalidemaillist[] = $line[0] . " " . $line[1] . " ( )";
                     }
                     if ($writearray['email'] != '') {
                         $aEmailAddresses = explode(';', $writearray['email']);
                         foreach ($aEmailAddresses as $sEmailaddress) {
                             if (!validateEmailAddress($sEmailaddress)) {
                                 $invalidemail = true;
                                 $invalidemaillist[] = $line[0] . " " . $line[1] . " (" . $line[2] . ")";
                             }
                         }
                     }
                     if (!isset($writearray['token'])) {
                         $writearray['token'] = '';
                     } else {
                         $writearray['token'] = sanitize_token($writearray['token']);
                     }
                     if (!$dupfound && !$invalidemail) {
                         if (!isset($writearray['emailstatus']) || $writearray['emailstatus'] == '') {
                             $writearray['emailstatus'] = "OK";
                         }
                         if (!isset($writearray['usesleft']) || $writearray['usesleft'] == '') {
                             $writearray['usesleft'] = 1;
                         }
                         if (!isset($writearray['language']) || $writearray['language'] == "") {
                             $writearray['language'] = $sBaseLanguage;
                         }
                         if (isset($writearray['validfrom']) && trim($writearray['validfrom'] == '')) {
                             unset($writearray['validfrom']);
                         }
                         if (isset($writearray['validuntil']) && trim($writearray['validuntil'] == '')) {
                             unset($writearray['validuntil']);
                         }
                         // sanitize it before writing into table
                         foreach ($writearray as $key => $value) {
                             if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {
                                 $value = substr($value, 1, -1);
                             }
                             $sanitizedArray[Yii::app()->db->quoteColumnName($key)] = Yii::app()->db->quoteValue($value);
                         }
                         $iq = "INSERT INTO {{tokens_{$iSurveyId}}} \n" . "(" . implode(',', array_keys($writearray)) . ") \n" . "VALUES (" . implode(",", $sanitizedArray) . ")";
                         $ir = Yii::app()->db->createCommand($iq)->execute();
                         if (!$ir) {
                             $duplicatelist[] = $writearray['firstname'] . " " . $writearray['lastname'] . " (" . $writearray['email'] . ")";
                         } else {
                             $xz++;
                         }
                     }
                     $xv++;
                 }
                 $recordcount++;
             }
             $recordcount = $recordcount - 1;
             unlink($sFilePath);
             $aData['tokenlistarray'] = $tokenlistarray;
             $aData['xz'] = $xz;
             $aData['xv'] = $xv;
             $aData['recordcount'] = $recordcount;
             $aData['firstline'] = $firstline;
             $aData['duplicatelist'] = $duplicatelist;
             $aData['invalidformatlist'] = $invalidformatlist;
             $aData['invalidemaillist'] = $invalidemaillist;
             $aData['thissurvey'] = getSurveyInfo($iSurveyId);
             $aData['iSurveyId'] = $aData['surveyid'] = $iSurveyId;
             $this->_renderWrappedTemplate('token', array('tokenbar', 'csvpost'), $aData);
         }
     } else {
         $aData['aEncodings'] = $aEncodings;
         $aData['iSurveyId'] = $iSurveyId;
         $aData['thissurvey'] = getSurveyInfo($iSurveyId);
         $aData['surveyid'] = $iSurveyId;
         $this->_renderWrappedTemplate('token', array('tokenbar', 'csvupload'), $aData);
     }
 }