/**
  * 	copyright fedek6-at-gmail-dot-com
  *
  * @param  &$base
  * 
  * @param  $add
  * 
  *
  * @return
  * 
  */
 function mergeXML(&$base, $add)
 {
     $new = $base->addChild($add->getName(), $add[0]);
     foreach ($add->attributes() as $a => $b) {
         $new[$a] = $b;
     }
     foreach ($add->children() as $child) {
         EasyContactFormsSimpleXML::mergeXML($new, $child);
     }
 }
 /**
  * 	newEntry
  *
  * @param  $map
  * 
  *
  * @return
  * 
  */
 function newEntry($map)
 {
     if ($this->processSpam(!isset($map['ufo-sign']), $map)) {
         return;
     }
     $sign = isset($map['ufo-sign']) ? $map['ufo-sign'] : '';
     $time = substr($sign, 32);
     if ($this->processSpam(md5(EasyContactFormsSecurityManager::getServerPwd() . '' . $time) . '' . $time != $sign, $map)) {
         return;
     }
     $delta = time() - intval($time);
     $mintime = EasyContactFormsApplicationSettings::getInstance()->get('FormCompletionMinTime');
     $maxtime = EasyContactFormsApplicationSettings::getInstance()->get('FormCompletionMaxTime');
     if ($this->processSpam(!empty($mintime) && $delta < $mintime, $map)) {
         return;
     }
     if ($this->processSpam(!empty($maxtime) && $delta > $maxtime, $map)) {
         return;
     }
     $fldvalues = array();
     foreach ($map as $key => $value) {
         if (!EasyContactFormsUtils::beginsWith($key, 'id-')) {
             continue;
         }
         $fldid = intval(EasyContactFormsUtils::cutPrefix($key, 'id-'));
         $value = stripslashes($value);
         $fldvalues[$fldid] = htmlspecialchars($value, ENT_QUOTES);
     }
     if ($this->processSpam(count($fldvalues) == 0, $map)) {
         return;
     }
     $formid = $this->get('id');
     if ($this->processSpam(!isset($formid) || empty($formid), $map)) {
         return;
     }
     $s = implode(',', array_keys($fldvalues));
     $query = "SELECT\n\t\t\t\tCustomFormFields.id\n\t\t\tFROM\n\t\t\t\t#wp__easycontactforms_customformfields AS CustomFormFields\n\t\t\tWHERE\n\t\t\t\tCustomFormFields.CustomForms='{$formid}'\n\t\t\t\tAND CustomFormFields.id IN ({$s})";
     $fields = EasyContactFormsDB::getObjects($query);
     if ($this->processSpam(count($fields) == 0, $map)) {
         return;
     }
     $cfe = EasyContactFormsClassLoader::getObject('CustomFormsEntries', true);
     $cfe->set('Date', date(DATE_ATOM));
     $cfe->set('CustomForms', $formid);
     if (isset($map['form-pagename'])) {
         $cfe->set('PageName', $map['form-pagename']);
         $fpstat = EasyContactFormsClassLoader::getObject('CustomFormEntryStatistics');
         $fpstat->increaseConversions($formid, $map['form-pagename']);
     }
     $map['_new_entry_id'] = $cfe->get('id');
     $query = "SELECT\n\t\t\t\tCustomFormFields.id,\n\t\t\t\tCustomFormFields.Settings,\n\t\t\t\tCustomFormFieldTypes.id AS tid,\n\t\t\t\tCustomFormFieldTypes.Description,\n\t\t\t\tCustomFormFieldTypes.ValueField\n\t\t\tFROM\n\t\t\t\t#wp__easycontactforms_customformfields AS CustomFormFields\n\t\t\tINNER JOIN\n\t\t\t\t#wp__easycontactforms_customformfieldtypes AS CustomFormFieldTypes\n\t\t\t\t\tON\n\t\t\t\t\t\tCustomFormFields.Type=CustomFormFieldTypes.id\n\t\t\tWHERE\n\t\t\t\tCustomFormFields.CustomForms='{$formid}'\n\t\t\tORDER BY\n\t\t\t\tCustomFormFields.ListPosition";
     $fields = EasyContactFormsDB::getObjects($query);
     $text = new EasyContactFormsSimpleXML('<div/>');
     $clientid = 0;
     if (!isset($map['ufo-preview'])) {
         if (isset($map['easycontactusr']) && isset($map['easycontactusr']->id) && !empty($map['easycontactusr']->id)) {
             $clientid = intval($map['easycontactusr']->id);
             $text->addAttribute('userid', $clientid);
         }
         global $current_user;
         $siteuserid = $current_user->ID;
         if (!empty($siteuserid)) {
             $siteuserid = intval($siteuserid);
         }
     }
     $clientemail = '';
     $emessagedata = (object) array();
     $attachments = (object) array();
     $attachments->confirmation = array();
     $attachments->notification = array();
     foreach ($fields as $fld) {
         $process = $fld->ValueField || $fld->tid == 14;
         if (!$process) {
             continue;
         }
         $value = isset($fldvalues[$fld->id]) ? $fldvalues[$fld->id] : '';
         $xml = simplexml_load_string($fld->Settings);
         $default = $this->getFieldValue($xml, true, 'DefaultValue', 'SetDefaultValue');
         $required = (string) $xml->Required;
         $required = $required == 'on';
         $spam = $required && $fld->tid == 14 && $value != $default;
         if ($this->processSpam($spam, $map)) {
             return;
         }
         $spam = !$required && $fld->tid == 14 && $value != '';
         if ($this->processSpam($spam, $map)) {
             return;
         }
         if ($fld->tid == 14) {
             continue;
         }
         $validate = isset($xml->Validate) && (string) $xml->Validate == 'on';
         if ($clientemail == '' && $fld->tid == 5 && $validate) {
             $clientemail = $value;
         }
         $fldlabel = (string) $xml->Label;
         $fldlabel = $fldlabel == '' ? $fld->Description : $fldlabel;
         $displayvalue = $value;
         $displayxmlvalue = null;
         $fld = EasyContactFormsClassLoader::getObject('CustomFormFields', true, $fld->id);
         $phase = (object) array('index' => 6);
         include $fld->getTMPFileName('proc');
         $d = $this->getTemplateTag($fld->get('Description'), $fld->get('id'));
         $d = trim($d, '{}');
         $emessagedata->{$d} = $displayvalue;
         $fldlabel = $text->prepare($fldlabel);
         $displayvalue = $text->prepare($displayvalue);
         $value = $text->prepare($value);
         $iddiv = $text->addChild('div');
         $iddiv->addAttribute('id', $fld->get('id'));
         $h1 = $iddiv->addChild('h1', $value);
         $h1->addAttribute('style', 'display:none');
         $ldiv = $iddiv->addChild('div');
         $label = $ldiv->addChild('label', $fldlabel);
         $label->addAttribute('class', 'ufo-cform-label');
         if (is_null($displayxmlvalue)) {
             $iddiv->addChild('div', $displayvalue);
         } else {
             $div = $iddiv->addChild('div');
             EasyContactFormsSimpleXML::mergeXML($div, $displayxmlvalue);
         }
     }
     $cfe->set('Content', $text->asXML());
     if (!empty($siteuserid)) {
         $cfe->set('SiteUser', $siteuserid);
     }
     $cfe->save();
     unset($text->attributes()->userid);
     foreach ($text->children() as $child) {
         unset($child->attributes()->id);
         unset($child->h1);
     }
     $text->addAttribute('class', 'ufo-form-envelope');
     $this->doEmailing($text->asCHTML(), $emessagedata, $clientemail, $clientid, $attachments);
     $this->set('TotalEntries', $this->get('TotalEntries') + 1);
     $this->save();
 }
 /**
  * 	updates field settings
  *
  * @param array $map
  * 	request data
  */
 function updateFieldData($map)
 {
     $values = $map["a"];
     $values = json_decode(stripslashes($values));
     $form = $values[0];
     $objid = intval($form->oid);
     $data = $form->a;
     $fld = EasyContactFormsClassLoader::getObject('CustomFormFields', true, $objid);
     $flds = array();
     $type = $fld->get('Type');
     $query = "SELECT\n\t\t\t\tCustomFormFieldTypes.Settings\n\t\t\tFROM\n\t\t\t\t#wp__easycontactforms_customformfieldtypes AS CustomFormFieldTypes\n\t\t\tWHERE\n\t\t\t\tCustomFormFieldTypes.id={$type}";
     $typesettings = EasyContactFormsDB::getValue($query);
     $fieldsettings = $fld->get('Settings');
     $typexml = simplexml_load_string($typesettings);
     $fldxml = new EasyContactFormsSimpleXML($fieldsettings);
     foreach ($typexml->children() as $item) {
         $name = $item->getName();
         if (isset($data->{$name})) {
             $value = $data->{$name};
             $value = str_replace('&', '&amp;', $value);
             $value = str_replace(chr(39), '&#39;', $value);
             unset($fldxml->{$name});
             $fldxml->addCDATA($name, $value);
             if ($name == 'Label') {
                 $flds['Description'] = $value;
             }
         } else {
             $typeval = (string) $typexml->{$name};
             if (!isset($fldxml->{$name}) && !empty($typeval)) {
                 $fldxml->{$name} = $typeval;
             }
         }
     }
     $options = array();
     foreach ($data as $key => $value) {
         if (!EasyContactFormsUtils::beginsWith($key, 'ufo-fieldform-option-li')) {
             continue;
         }
         $options[$key] = $value;
     }
     if (sizeof($options) > 0) {
         unset($fldxml->Options);
         $fldxml->Options['type'] = 'items';
         foreach ($options as $key => $value) {
             $value = htmlspecialchars($value, ENT_QUOTES);
             $option = $fldxml->Options->addChild('option', $value);
             $oid = explode('-', $key);
             $oid = $oid[count($oid) - 1];
             $option['index'] = $oid;
             $dname = "ufo-fieldform-option-default-{$oid}";
             if ($data->{$dname} == 'on') {
                 $option['default'] = 'true';
             }
         }
     }
     $phase = (object) array('index' => 2);
     include $fld->getTMPFileName('proc');
     $xml = $fldxml->asXML();
     $flds['Settings'] = $xml;
     parent::update($flds, $objid);
     $fld->set('Settings', $xml);
     $fld->updateTemplate();
 }