/**
  * 	fillInTemplate
  *
  * 	fills the template with object data
  *
  * @param object $template
  * 	email template
  * @param string $object
  * 	object type
  *
  * @return object
  * 	the filled template
  */
 function fillInTemplate($template, $object)
 {
     if (!is_object($object)) {
         $template->body = str_replace('{messagebody}', $object, $template->body);
     } else {
         $fields = get_object_vars($object);
         foreach ($fields as $fname => $fvalue) {
             $value = (EasyContactFormsUtils::endsWith($fname, 'date') || EasyContactFormsUtils::endsWith($fname, 'deadline')) && is_numeric($fvalue) ? EasyContactFormsUtils::getDate($fvalue) : $fvalue;
             if (isset($template->ishtml) && $template->ishtml) {
                 $value = nl2br('' . $value);
             }
             if (isset($template->body)) {
                 $fldcheck = EasyContactFormsBackOffice::fldTemplateReplace($template->body, $fname, $value);
                 if (!$fldcheck) {
                     $template->body = str_replace('{' . $fname . '}', $value, $template->body);
                 } else {
                     $template->body = $fldcheck;
                 }
             }
             if (isset($template->subject)) {
                 $fldcheck = EasyContactFormsBackOffice::fldTemplateReplace($template->subject, $fname, $value);
                 if (!$fldcheck) {
                     $template->subject = str_replace('{' . $fname . '}', $value, $template->body);
                 } else {
                     $template->subject = $fldcheck;
                 }
             }
         }
     }
     return $template;
 }
 /**
  * 	doEmailing
  *
  * @param  $submission
  * 
  * @param  $emessagedata
  * 
  * @param  $clientemail
  * 
  * @param  $clientid
  * 
  * @param  $attachments
  * 
  *
  * @return
  * 
  */
 function doEmailing($submission, $emessagedata, $clientemail, $clientid, $attachments)
 {
     require_once 'easy-contact-forms-backoffice.php';
     $bo = new EasyContactFormsBackOffice();
     $submission = htmlspecialchars_decode($submission);
     if ($this->get('SendConfirmation')) {
         if (empty($clientemail) && !empty($clientid)) {
             $userdata = $bo->getSenderData($clientid);
             $clientemail = $userdata->email;
         }
         if (!empty($clientemail)) {
             $message = (object) array();
             $message->ishtml = $this->get('SendConfirmationAsText') ? false : true;
             $message->subject = $this->get('ConfirmationSubject');
             $message->body = '' . $this->get('ConfirmationText');
             $message = EasyContactFormsBackOffice::fillInTemplate($message, $emessagedata);
             $message->body = str_replace('{submission}', $submission, $message->body);
             if (!$this->isEmpty('ConfirmationStyleSheet') && $message->ishtml) {
                 $message->body = '<style>' . $this->get('ConfirmationStyleSheet') . '</style>' . $message->body;
             }
             $message->body = stripslashes($message->body);
             $sender = (object) array();
             $sender->name = $this->get('SendFrom');
             $email = $this->get('SendFromAddress');
             if (empty($email)) {
                 $email = get_option('admin_email');
             }
             if (!$this->isEmpty('ConfirmationReplyToName') || !$this->isEmpty('ConfirmationReplyToAddress')) {
                 $message->replyto = (object) array();
                 if (!$this->isEmpty('ConfirmationReplyToName')) {
                     $message->replyto->name = $this->get('ConfirmationReplyToName');
                     if (empty($sender->name)) {
                     }
                 }
                 if (!$this->isEmpty('ConfirmationReplyToAddress')) {
                     $message->replyto->email = $this->get('ConfirmationReplyToAddress');
                 }
             }
             $list = array($clientemail);
             $bo->send($message, $list, $sender, $email, $attachments->confirmation);
         }
     }
     $recps = $bo->getListMemberEmails('CustomForms', $this->get('id'));
     if (count($recps) == 0) {
         return;
     }
     $message = (object) array();
     $message->ishtml = $this->get('SendNotificationAsText') ? false : true;
     $message->subject = $this->get('NotificationSubject');
     $message->body = '' . $this->get('NotificationText');
     $message = EasyContactFormsBackOffice::fillInTemplate($message, $emessagedata);
     if ($this->isEmpty('NotificationText') || $this->get('NotificationText') == '<p></p>') {
         if ($message->ishtml) {
             $submission = '<style>div.ufo-form-envelope div{padding:2px 10px;}label.ufo-cform-label {font-weight:bold;margin:2px -10px;}</style>' . $submission;
         } else {
             $submisstiontext = array();
             $xml = simplexml_load_string($submission);
             foreach ($xml->children() as $parent) {
                 foreach ($parent->children() as $child) {
                     $label = (string) $child->label;
                     $str = (string) $child;
                     if ($label != '') {
                         $submisstiontext[] = '  ' . $label;
                     } else {
                         $submisstiontext[] = '    ' . $str;
                     }
                 }
             }
             $submission = implode(chr(13) . chr(10), $submisstiontext);
         }
         $message->body = $submission;
     }
     $message->body = stripslashes($message->body);
     if (!$this->isEmpty('NotificationStyleSheet') && $message->ishtml) {
         $message->body = '<style>' . $this->get('NotificationStyleSheet') . '</style>' . $message->body;
     }
     $replyto = $this->get('IncludeVisitorsAddressInReplyTo') && !empty($clientemail);
     if ($replyto) {
         $message->replyto = (object) array();
         $message->replyto->email = $clientemail;
         if (!$this->isEmpty('ReplyToNameTemplate')) {
             $fake = (object) array();
             $fake->subject = '';
             $fake->body = $this->get('ReplyToNameTemplate');
             $fake = EasyContactFormsBackOffice::fillInTemplate($fake, $emessagedata);
             $message->replyto->name = $fake->body;
         }
     }
     $sender = (object) array();
     $sender->name = $this->get('Description');
     $sender->email = EasyContactFormsApplicationSettings::getInstance()->get('SendFrom');
     if (empty($sender->email)) {
         $sender->email = get_option('admin_email');
     }
     $bo->send($message, $recps, $sender, '', $attachments->notification);
 }
 /**
  * 	update. Overrides EasyContactFormsBase::update()
  *
  * 	updates an object with request data
  *
  * @param array $request
  * 	request data
  * @param int $id
  * 	object id
  */
 function update($request, $id)
 {
     $query = "SELECT\n\t\t\t\tUsers.Role,\n\t\t\t\tUsers.CMSId\n\t\t\tFROM\n\t\t\t\t#wp__easycontactforms_users AS Users\n\t\t\tWHERE\n\t\t\t\tUsers.id='{$id}'";
     $objs = EasyContactFormsDB::getObjects($query);
     $usr = $objs[0];
     global $current_user;
     $currentadmin = $usr->Role == 1 && isset($request->Role) && isset($current_user) && $current_user->ID == $usr->CMSId;
     if ($currentadmin) {
         $as = EasyContactFormsApplicationSettings::getInstance();
         $as->addMessage(EasyContactFormsT::get('CannotChangeCurrentUserRole'));
         $request->Role = 1;
     }
     $request = EasyContactFormsUtils::parseRequest($request, 'ContactType', 'int');
     $request = EasyContactFormsUtils::parseRequest($request, 'Birthday', 'date');
     $request = EasyContactFormsUtils::parseRequest($request, 'Role', 'int');
     $request = EasyContactFormsUtils::parseRequest($request, 'CMSId', 'int');
     if (isset($this->user)) {
         require_once 'easy-contact-forms-backoffice.php';
         $bo = new EasyContactFormsBackOffice();
         $request = $bo->processHistory($request, $this->type, $id, $this->user->id);
     }
     parent::update($request, $id);
 }