Esempio n. 1
0
 /**
  * Performs a ldap import
  *
  * @access public
  * @param int $iSurveyId
  * @return void
  */
 public function importldap($iSurveyId)
 {
     $iSurveyId = (int) $iSurveyId;
     $clang = $this->getController()->lang;
     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);
     }
     Yii::app()->loadConfig('ldap');
     Yii::app()->loadHelper('ldap');
     $tokenoutput = '';
     $aData['thissurvey'] = getSurveyInfo($iSurveyId);
     $aData['iSurveyId'] = $aData['surveyid'] = $iSurveyId;
     $aData['ldap_queries'] = Yii::app()->getConfig('ldap_queries');
     if (!Yii::app()->request->getPost('submit')) {
         $this->_renderWrappedTemplate('token', array('tokenbar', 'ldapform'), $aData);
     } else {
         $filterduplicatetoken = Yii::app()->request->getPost('filterduplicatetoken') && Yii::app()->request->getPost('filterduplicatetoken') == 'on';
         $filterblankemail = Yii::app()->request->getPost('filterblankemail') && Yii::app()->request->getPost('filterblankemail') == 'on';
         $ldap_queries = Yii::app()->getConfig('ldap_queries');
         $ldap_server = Yii::app()->getConfig('ldap_server');
         $duplicatelist = array();
         $invalidemaillist = array();
         $tokenoutput .= "\t<tr><td colspan='2' height='4'><strong>" . $clang->gT("Uploading LDAP Query") . "</strong></td></tr>\n" . "\t<tr><td align='center'>\n";
         $ldapq = Yii::app()->request->getPost('ldapQueries');
         // the ldap query id
         $ldap_server_id = $ldap_queries[$ldapq]['ldapServerId'];
         $ldapserver = $ldap_server[$ldap_server_id]['server'];
         $ldapport = $ldap_server[$ldap_server_id]['port'];
         if (isset($ldap_server[$ldap_server_id]['encoding']) && $ldap_server[$ldap_server_id]['encoding'] != 'utf-8' && $ldap_server[$ldap_server_id]['encoding'] != 'UTF-8') {
             $ldapencoding = $ldap_server[$ldap_server_id]['encoding'];
         } else {
             $ldapencoding = '';
         }
         // define $attrlist: list of attributes to read from users' entries
         $attrparams = array('firstname_attr', 'lastname_attr', 'email_attr', 'token_attr', 'language');
         $aTokenAttr = getAttributeFieldNames($iSurveyId);
         foreach ($aTokenAttr as $thisattrfieldname) {
             $attridx = substr($thisattrfieldname, 10);
             // the 'attribute_' prefix is 10 chars long
             $attrparams[] = "attr" . $attridx;
         }
         foreach ($attrparams as $id => $attr) {
             if (array_key_exists($attr, $ldap_queries[$ldapq]) && $ldap_queries[$ldapq][$attr] != '') {
                 $attrlist[] = $ldap_queries[$ldapq][$attr];
             }
         }
         // Open connection to server
         $ds = ldap_getCnx($ldap_server_id);
         if ($ds) {
             // bind to server
             $resbind = ldap_bindCnx($ds, $ldap_server_id);
             if ($resbind) {
                 $ResArray = array();
                 $resultnum = ldap_doTokenSearch($ds, $ldapq, $ResArray, $iSurveyId);
                 $xz = 0;
                 // imported token count
                 $xv = 0;
                 // meet minim requirement count
                 $xy = 0;
                 // check for duplicates
                 $duplicatecount = 0;
                 // duplicate tokens skipped count
                 $invalidemailcount = 0;
                 if ($resultnum >= 1) {
                     foreach ($ResArray as $responseGroupId => $responseGroup) {
                         for ($j = 0; $j < $responseGroup['count']; $j++) {
                             // first let's initialize everything to ''
                             $myfirstname = '';
                             $mylastname = '';
                             $myemail = '';
                             $mylanguage = '';
                             $mytoken = '';
                             $myattrArray = array();
                             // The first 3 attrs MUST exist in the ldap answer
                             // ==> send PHP notice msg to apache logs otherwise
                             $meetminirequirements = true;
                             if (isset($responseGroup[$j][$ldap_queries[$ldapq]['firstname_attr']]) && isset($responseGroup[$j][$ldap_queries[$ldapq]['lastname_attr']])) {
                                 // minimum requirement for ldap
                                 // * at least a firstanme
                                 // * at least a lastname
                                 // * if filterblankemail is set (default): at least an email address
                                 $myfirstname = ldap_readattr($responseGroup[$j][$ldap_queries[$ldapq]['firstname_attr']]);
                                 $mylastname = ldap_readattr($responseGroup[$j][$ldap_queries[$ldapq]['lastname_attr']]);
                                 if (isset($responseGroup[$j][$ldap_queries[$ldapq]['email_attr']])) {
                                     $myemail = ldap_readattr($responseGroup[$j][$ldap_queries[$ldapq]['email_attr']]);
                                     $myemail = $myemail;
                                     ++$xv;
                                 } elseif ($filterblankemail !== true) {
                                     $myemail = '';
                                     ++$xv;
                                 } else {
                                     $meetminirequirements = false;
                                 }
                             } else {
                                 $meetminirequirements = false;
                             }
                             // The following attrs are optionnal
                             if (isset($responseGroup[$j][$ldap_queries[$ldapq]['token_attr']])) {
                                 $mytoken = ldap_readattr($responseGroup[$j][$ldap_queries[$ldapq]['token_attr']]);
                             }
                             foreach ($aTokenAttr as $thisattrfieldname) {
                                 $attridx = substr($thisattrfieldname, 10);
                                 // the 'attribute_' prefix is 10 chars long
                                 if (isset($ldap_queries[$ldapq]['attr' . $attridx]) && isset($responseGroup[$j][$ldap_queries[$ldapq]['attr' . $attridx]])) {
                                     $myattrArray[$attridx] = ldap_readattr($responseGroup[$j][$ldap_queries[$ldapq]['attr' . $attridx]]);
                                 }
                             }
                             if (isset($responseGroup[$j][$ldap_queries[$ldapq]['language']])) {
                                 $mylanguage = ldap_readattr($responseGroup[$j][$ldap_queries[$ldapq]['language']]);
                             }
                             // In case Ldap Server encoding isn't UTF-8, let's translate
                             // the strings to UTF-8
                             if ($ldapencoding != '') {
                                 $myfirstname = @mb_convert_encoding($myfirstname, "UTF-8", $ldapencoding);
                                 $mylastname = @mb_convert_encoding($mylastname, "UTF-8", $ldapencoding);
                                 foreach ($aTokenAttr as $thisattrfieldname) {
                                     $attridx = substr($thisattrfieldname, 10);
                                     // the 'attribute_' prefix is 10 chars long
                                     @mb_convert_encoding($myattrArray[$attridx], "UTF-8", $ldapencoding);
                                 }
                             }
                             // Now check for duplicates or bad formatted email addresses
                             $dupfound = false;
                             $invalidemail = false;
                             if ($filterduplicatetoken) {
                                 $dupquery = "SELECT count(tid) from {{tokens_" . intval($iSurveyId) . "}} where email=:email and firstname=:firstname and lastname=:lastname";
                                 $dupresult = Yii::app()->db->createCommand($dupquery)->bindParam(":email", $myemail, PDO::PARAM_STR)->bindParam(":firstname", $myfirstname, PDO::PARAM_STR)->bindParam(":lastname", $mylastname, PDO::PARAM_STR)->queryScalar();
                                 if ($dupresult > 0) {
                                     $dupfound = true;
                                     $duplicatelist[] = $myfirstname . " " . $mylastname . " (" . $myemail . ")";
                                     $xy++;
                                 }
                             }
                             if ($filterblankemail && $myemail == '') {
                                 $invalidemail = true;
                                 $invalidemaillist[] = $myfirstname . " " . $mylastname . " ( )";
                             } elseif ($myemail != '' && !validateEmailAddress($myemail)) {
                                 $invalidemail = true;
                                 $invalidemaillist[] = $myfirstname . " " . $mylastname . " (" . $myemail . ")";
                             }
                             if ($invalidemail) {
                                 ++$invalidemailcount;
                             } elseif ($dupfound) {
                                 ++$duplicatecount;
                             } elseif ($meetminirequirements === true) {
                                 // No issue, let's import
                                 $iq = "INSERT INTO {{tokens_" . intval($iSurveyId) . "}} \n" . "(firstname, lastname, email, emailstatus, token, language";
                                 foreach ($aTokenAttr as $thisattrfieldname) {
                                     $attridx = substr($thisattrfieldname, 10);
                                     // the 'attribute_' prefix is 10 chars long
                                     if (!empty($myattrArray[$attridx])) {
                                         $iq .= ", " . Yii::app()->db->quoteColumnName($thisattrfieldname);
                                     }
                                 }
                                 $iq .= ") \n" . "VALUES (" . Yii::app()->db->quoteValue($myfirstname) . ", " . Yii::app()->db->quoteValue($mylastname) . ", " . Yii::app()->db->quoteValue($myemail) . ", 'OK', " . Yii::app()->db->quoteValue($mytoken) . ", " . Yii::app()->db->quoteValue($mylanguage) . "";
                                 foreach ($aTokenAttr as $thisattrfieldname) {
                                     $attridx = substr($thisattrfieldname, 10);
                                     // the 'attribute_' prefix is 10 chars long
                                     if (!empty($myattrArray[$attridx])) {
                                         $iq .= ", " . Yii::app()->db->quoteValue($myattrArray[$attridx]) . "";
                                     }
                                     // dbquote_all encloses str with quotes
                                 }
                                 $iq .= ")";
                                 $ir = Yii::app()->db->createCommand($iq)->execute();
                                 if (!$ir) {
                                     $duplicatecount++;
                                 }
                                 $xz++;
                                 // or die ("Couldn't insert line<br />\n$buffer<br />\n".htmlspecialchars($connect->ErrorMsg())."<pre style='text-align: left'>$iq</pre>\n");
                             }
                         }
                         // End for each entry
                     }
                     // End foreach responseGroup
                 }
                 // End of if resnum >= 1
                 $aData['duplicatelist'] = $duplicatelist;
                 $aData['invalidemaillist'] = $invalidemaillist;
                 $aData['invalidemailcount'] = $invalidemailcount;
                 $aData['resultnum'] = $resultnum;
                 $aData['xv'] = $xv;
                 $aData['xy'] = $xy;
                 $aData['xz'] = $xz;
                 $this->_renderWrappedTemplate('token', array('tokenbar', 'ldappost'), $aData);
             } else {
                 $aData['sError'] = $clang->gT("Can't bind to the LDAP directory");
                 $this->_renderWrappedTemplate('token', array('tokenbar', 'ldapform'), $aData);
             }
             @ldap_close($ds);
         } else {
             $aData['sError'] = $clang->gT("Can't connect to the LDAP directory");
             $this->_renderWrappedTemplate('token', array('tokenbar', 'ldapform'), $aData);
         }
     }
 }
Esempio n. 2
0
    $aTokenAttr=GetAttributeFieldNames($surveyid);
    foreach ($aTokenAttr as $thisattrfieldname)
    {
        $attridx=substr($thisattrfieldname,10); // the 'attribute_' prefix is 10 chars long
        $attrparams[] = "attr".$attridx;
    }

    foreach ($attrparams as $id => $attr) {
        if (array_key_exists($attr,$ldap_queries[$ldapq]) &&
        $ldap_queries[$ldapq][$attr] != '') {
            $attrlist[]=$ldap_queries[$ldapq][$attr];
        }
    }

    // Open connection to server
    $ds = ldap_getCnx($ldap_server_id);

    if ($ds) {
        // bind to server
        $resbind=ldap_bindCnx($ds, $ldap_server_id);

        if ($resbind) {
            $ResArray=array();
            $resultnum=ldap_doTokenSearch($ds, $ldapq, $ResArray);
            $xz = 0; // imported token count
            $xv = 0; // meet minim requirement count
            $xy = 0; // check for duplicates
            $duplicatecount = 0; // duplicate tokens skipped count
            $invalidemailcount = 0;

            if ($resultnum >= 1) {