Пример #1
0
 /**
  * Process form data, store it in the session and redirect to the jumpTo page
  *
  * @param array $arrSubmitted
  * @param array $arrLabels
  * @param array $arrFields
  */
 protected function processFormData($arrSubmitted, $arrLabels, $arrFields)
 {
     // HOOK: prepare form data callback
     if (isset($GLOBALS['TL_HOOKS']['prepareFormData']) && is_array($GLOBALS['TL_HOOKS']['prepareFormData'])) {
         foreach ($GLOBALS['TL_HOOKS']['prepareFormData'] as $callback) {
             $this->import($callback[0]);
             $this->{$callback}[0]->{$callback}[1]($arrSubmitted, $arrLabels, $arrFields, $this);
         }
     }
     // Send form data via e-mail
     if ($this->sendViaEmail) {
         $keys = array();
         $values = array();
         $fields = array();
         $message = '';
         foreach ($arrSubmitted as $k => $v) {
             if ($k == 'cc') {
                 continue;
             }
             $v = deserialize($v);
             // Skip empty fields
             if ($this->skipEmpty && !is_array($v) && !strlen($v)) {
                 continue;
             }
             // Add field to message
             $message .= (isset($arrLabels[$k]) ? $arrLabels[$k] : ucfirst($k)) . ': ' . (is_array($v) ? implode(', ', $v) : $v) . "\n";
             // Prepare XML file
             if ($this->format == 'xml') {
                 $fields[] = array('name' => $k, 'values' => is_array($v) ? $v : array($v));
             }
             // Prepare CSV file
             if ($this->format == 'csv') {
                 $keys[] = $k;
                 $values[] = is_array($v) ? implode(',', $v) : $v;
             }
         }
         $recipients = \StringUtil::splitCsv($this->recipient);
         // Format recipients
         foreach ($recipients as $k => $v) {
             $recipients[$k] = str_replace(array('[', ']', '"'), array('<', '>', ''), $v);
         }
         $email = new \Email();
         // Get subject and message
         if ($this->format == 'email') {
             $message = $arrSubmitted['message'];
             $email->subject = $arrSubmitted['subject'];
         }
         // Set the admin e-mail as "from" address
         $email->from = $GLOBALS['TL_ADMIN_EMAIL'];
         $email->fromName = $GLOBALS['TL_ADMIN_NAME'];
         // Get the "reply to" address
         if (strlen(\Input::post('email', true))) {
             $replyTo = \Input::post('email', true);
             // Add name
             if (strlen(\Input::post('name'))) {
                 $replyTo = '"' . \Input::post('name') . '" <' . $replyTo . '>';
             }
             $email->replyTo($replyTo);
         }
         // Fallback to default subject
         if (!strlen($email->subject)) {
             $email->subject = $this->replaceInsertTags($this->subject, false);
         }
         // Send copy to sender
         if (strlen($arrSubmitted['cc'])) {
             $email->sendCc(\Input::post('email', true));
             unset($_SESSION['FORM_DATA']['cc']);
         }
         // Attach XML file
         if ($this->format == 'xml') {
             /** @var \FrontendTemplate|object $objTemplate */
             $objTemplate = new \FrontendTemplate('form_xml');
             $objTemplate->fields = $fields;
             $objTemplate->charset = \Config::get('characterSet');
             $email->attachFileFromString($objTemplate->parse(), 'form.xml', 'application/xml');
         }
         // Attach CSV file
         if ($this->format == 'csv') {
             $email->attachFileFromString(\StringUtil::decodeEntities('"' . implode('";"', $keys) . '"' . "\n" . '"' . implode('";"', $values) . '"'), 'form.csv', 'text/comma-separated-values');
         }
         $uploaded = '';
         // Attach uploaded files
         if (!empty($_SESSION['FILES'])) {
             foreach ($_SESSION['FILES'] as $file) {
                 // Add a link to the uploaded file
                 if ($file['uploaded']) {
                     $uploaded .= "\n" . \Environment::get('base') . str_replace(TL_ROOT . '/', '', dirname($file['tmp_name'])) . '/' . rawurlencode($file['name']);
                     continue;
                 }
                 $email->attachFileFromString(file_get_contents($file['tmp_name']), $file['name'], $file['type']);
             }
         }
         $uploaded = strlen(trim($uploaded)) ? "\n\n---\n" . $uploaded : '';
         $email->text = \StringUtil::decodeEntities(trim($message)) . $uploaded . "\n\n";
         // Send the e-mail
         try {
             $email->sendTo($recipients);
         } catch (\Swift_SwiftException $e) {
             $this->log('Form "' . $this->title . '" could not be sent: ' . $e->getMessage(), __METHOD__, TL_ERROR);
         }
     }
     // Store the values in the database
     if ($this->storeValues && $this->targetTable != '') {
         $arrSet = array();
         // Add the timestamp
         if ($this->Database->fieldExists('tstamp', $this->targetTable)) {
             $arrSet['tstamp'] = time();
         }
         // Fields
         foreach ($arrSubmitted as $k => $v) {
             if ($k != 'cc' && $k != 'id') {
                 $arrSet[$k] = $v;
                 // Convert date formats into timestamps (see #6827)
                 if ($arrSet[$k] != '' && in_array($arrFields[$k]->rgxp, array('date', 'time', 'datim'))) {
                     $objDate = new \Date($arrSet[$k], \Date::getFormatFromRgxp($arrFields[$k]->rgxp));
                     $arrSet[$k] = $objDate->tstamp;
                 }
             }
         }
         // Files
         if (!empty($_SESSION['FILES'])) {
             foreach ($_SESSION['FILES'] as $k => $v) {
                 if ($v['uploaded']) {
                     $arrSet[$k] = str_replace(TL_ROOT . '/', '', $v['tmp_name']);
                 }
             }
         }
         // HOOK: store form data callback
         if (isset($GLOBALS['TL_HOOKS']['storeFormData']) && is_array($GLOBALS['TL_HOOKS']['storeFormData'])) {
             foreach ($GLOBALS['TL_HOOKS']['storeFormData'] as $callback) {
                 $this->import($callback[0]);
                 $arrSet = $this->{$callback}[0]->{$callback}[1]($arrSet, $this);
             }
         }
         // Set the correct empty value (see #6284, #6373)
         foreach ($arrSet as $k => $v) {
             if ($v === '') {
                 $arrSet[$k] = \Widget::getEmptyValueByFieldType($GLOBALS['TL_DCA'][$this->targetTable]['fields'][$k]['sql']);
             }
         }
         // Do not use Models here (backwards compatibility)
         $this->Database->prepare("INSERT INTO " . $this->targetTable . " %s")->set($arrSet)->execute();
     }
     // Store all values in the session
     foreach (array_keys($_POST) as $key) {
         $_SESSION['FORM_DATA'][$key] = $this->allowTags ? \Input::postHtml($key, true) : \Input::post($key, true);
     }
     $arrFiles = $_SESSION['FILES'];
     // HOOK: process form data callback
     if (isset($GLOBALS['TL_HOOKS']['processFormData']) && is_array($GLOBALS['TL_HOOKS']['processFormData'])) {
         foreach ($GLOBALS['TL_HOOKS']['processFormData'] as $callback) {
             $this->import($callback[0]);
             $this->{$callback}[0]->{$callback}[1]($arrSubmitted, $this->arrData, $arrFiles, $arrLabels, $this);
         }
     }
     $_SESSION['FILES'] = array();
     // DO NOT CHANGE
     // Add a log entry
     if (FE_USER_LOGGED_IN) {
         $this->import('FrontendUser', 'User');
         $this->log('Form "' . $this->title . '" has been submitted by "' . $this->User->username . '".', __METHOD__, TL_FORMS);
     } else {
         $this->log('Form "' . $this->title . '" has been submitted by ' . \System::anonymizeIp(\Environment::get('ip')) . '.', __METHOD__, TL_FORMS);
     }
     // Check whether there is a jumpTo page
     if (($objJumpTo = $this->objModel->getRelated('jumpTo')) !== null) {
         $this->jumpToOrReload($objJumpTo->row());
     }
     $this->reload();
 }
 protected function compile()
 {
     parent::compile();
     if ($this->page_editor) {
         $memberdata = $this->Template->parse();
         if ($this->page_editor) {
             $this->memberTpl = 'personaldata_default';
         }
         global $objPage;
         $this->import('FrontendUser', 'User');
         $GLOBALS['TL_LANGUAGE'] = $objPage->language;
         $this->loadLanguageFile('tl_member');
         $this->loadLanguageFile('tl_module');
         $this->loadDataContainer('tl_member');
         $this->createDefaultPage();
         $activepage = strlen(\Input::get("activepage")) ? \Input::get("activepage") : 1;
         if (strlen(\Input::post("addPage"))) {
             // add a new page
             $this->addPage();
             $activepage = count(deserialize($this->User->member_pages, TRUE));
             $this->redirect($this->addToUrl("activepage={$activepage}"));
         }
         if (strlen(\Input::post("deletePage"))) {
             // delete a page
             $this->deletePage(\Input::get("activepage"));
             $page = $activepage - 1;
             $this->redirect($this->addToUrl("activepage=" . $page));
         }
         if (strlen(\Input::post("saveContent")) || strlen(\Input::post('FORM_SUBMIT'))) {
             $this->Database->prepare("UPDATE tl_member_pages SET title=?, content=?, is_visible=? WHERE position=? AND id IN (" . implode(",", deserialize($this->User->member_pages, TRUE)) . ")")->execute(\Input::post("pageTitle"), \Input::postHtml("content", TRUE), \Input::post("is_visible"), $activepage);
         }
         // Set template
         if (strlen($this->memberTpl)) {
             $this->Template = new FrontendTemplate($this->memberTpl);
         }
         $arrFields = array();
         $doNotSubmit = false;
         $hasUpload = false;
         $this->Template->fields = '';
         $this->Template->pageaction = ampersand(\Environment::get('request'), ENCODE_AMPERSANDS);
         $pages = array();
         if (is_array($this->User->member_pages) || strlen($this->User->member_pages)) {
             $pageArr = deserialize($this->User->member_pages, TRUE);
             if (count($pageArr)) {
                 $objFrontendPage = $this->Database->prepare("SELECT * FROM tl_member_pages WHERE id IN (" . implode(",", $pageArr) . ") ORDER BY position")->execute();
                 while ($objFrontendPage->next()) {
                     $content = deserialize($objFrontendPage->content);
                     if (!is_array($content)) {
                         $content = specialchars($content);
                     }
                     $pg = array("title" => $objFrontendPage->title, "content" => $content, "position" => $objFrontendPage->position, "is_visible" => $objFrontendPage->is_visible, "href" => $this->addToUrl("activepage=" . $objFrontendPage->position), "type" => $objFrontendPage->pagetype);
                     $pages[] = $pg;
                 }
             }
         }
         if (is_array($GLOBALS['TL_PERSONALDATA_EDITOR'])) {
             if (array_key_exists($pages[$activepage - 1]["type"], $GLOBALS['TL_PERSONALDATA_EDITOR'])) {
                 $this->import($GLOBALS['TL_PERSONALDATA_EDITOR'][$pages[$activepage - 1]["type"]][0]);
                 $this->Template->pageEditorContent = $this->{$GLOBALS}['TL_PERSONALDATA_EDITOR'][$pages[$activepage - 1]["type"]][0]->{$GLOBALS}['TL_PERSONALDATA_EDITOR'][$pages[$activepage - 1]["type"]][1]($pages[$activepage - 1], $this);
             }
         }
         if ($activepage == 1) {
             $this->Template->memberdata = $memberdata;
         }
         $this->Template->page_editor = $this->page_editor ? TRUE : FALSE;
         $this->Template->textVisible = $GLOBALS['TL_LANG']['tl_member']['pageVisible'];
         $this->Template->activepage_position = $activepage;
         $this->Template->pages = $pages;
         $this->Template->activePageArr = $pages[$activepage - 1];
         $this->Template->strDeletePage = $GLOBALS['TL_LANG']['tl_module']['deletePage'];
         $this->Template->showPageHead = $this->showPageHead;
         $this->Template->strAdd = $GLOBALS['TL_LANG']['tl_module']['add'];
         $this->Template->deletePage = $this->addToUrl("deletePage=" . $activepage);
         //$deleteURL;
         $pagetypes = $GLOBALS['TL_LANG']['tl_module']['pagetype'];
         asort($pagetypes);
         $this->Template->pagetypes = $pagetypes;
         $this->Template->save = $GLOBALS['TL_LANG']['MSC']['save'];
         $this->Template->content = $GLOBALS['TL_LANG']['tl_module']['content'];
         $this->Template->pageTitle = $GLOBALS['TL_LANG']['tl_module']['pageTitle'];
         $this->Template->confirmDeletePage = $GLOBALS['TL_LANG']['tl_module']['confirmDeletePage'];
         $this->Template->formId = 'tl_member_' . $this->id;
         $this->Template->slabel = specialchars($GLOBALS['TL_LANG']['MSC']['saveData']);
         $this->Template->action = ampersand(\Environment::get('request'), ENCODE_AMPERSANDS);
         $this->Template->enctype = $hasUpload ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
         $this->Template->rowLast = 'row_' . count($this->editable) . ($i % 2 == 0 ? ' odd' : ' even');
     }
 }