/** * Validate the form */ private function validateForm() { if ($this->frm->isSubmitted()) { $this->frm->cleanupFields(); // shorten the fields $txtName = $this->frm->getField('name'); $txtEmail = $this->frm->getField('email'); $ddmMethod = $this->frm->getField('method'); $txtSuccessMessage = $this->frm->getField('success_message'); $txtIdentifier = $this->frm->getField('identifier'); $emailAddresses = (array) explode(',', $txtEmail->getValue()); // validate fields $txtName->isFilled(BL::getError('NameIsRequired')); $txtSuccessMessage->isFilled(BL::getError('SuccessMessageIsRequired')); if ($ddmMethod->isFilled(BL::getError('NameIsRequired')) && $ddmMethod->getValue() == 'database_email') { $error = false; // check the addresses foreach ($emailAddresses as $address) { $address = trim($address); if (!\SpoonFilter::isEmail($address)) { $error = true; break; } } // add error if ($error) { $txtEmail->addError(BL::getError('EmailIsInvalid')); } } // identifier if ($txtIdentifier->isFilled()) { // invalid characters if (!\SpoonFilter::isValidAgainstRegexp('/^[a-zA-Z0-9\\.\\_\\-]+$/', $txtIdentifier->getValue())) { $txtIdentifier->setError(BL::getError('InvalidIdentifier')); } elseif (BackendFormBuilderModel::existsIdentifier($txtIdentifier->getValue())) { // unique identifier $txtIdentifier->setError(BL::getError('UniqueIdentifier')); } } if ($this->frm->isCorrect()) { // build array $values['language'] = BL::getWorkingLanguage(); $values['user_id'] = BackendAuthentication::getUser()->getUserId(); $values['name'] = $txtName->getValue(); $values['method'] = $ddmMethod->getValue(); $values['email'] = $ddmMethod->getValue() == 'database_email' ? serialize($emailAddresses) : null; $values['success_message'] = $txtSuccessMessage->getValue(true); $values['identifier'] = $txtIdentifier->isFilled() ? $txtIdentifier->getValue() : BackendFormBuilderModel::createIdentifier(); $values['created_on'] = BackendModel::getUTCDate(); $values['edited_on'] = BackendModel::getUTCDate(); // insert the item $id = BackendFormBuilderModel::insert($values); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_add', array('item' => $values)); // set frontend locale FL::setLocale(BL::getWorkingLanguage(), true); // create submit button $field['form_id'] = $id; $field['type'] = 'submit'; $field['settings'] = serialize(array('values' => \SpoonFilter::ucfirst(FL::getLabel('Send')))); BackendFormBuilderModel::insertField($field); // everything is saved, so redirect to the editform $this->redirect(BackendModel::createURLForAction('Edit') . '&id=' . $id . '&report=added&var=' . rawurlencode($values['name']) . '#tabFields'); } } }
/** * Process the query string */ private function processQueryString() { // store the query string local, so we don't alter it. $queryString = trim($this->request->getPathInfo(), '/'); // split into chunks $chunks = (array) explode('/', $queryString); $hasMultiLanguages = $this->getContainer()->getParameter('site.multilanguage'); // single language if (!$hasMultiLanguages) { // set language id $language = $this->get('fork.settings')->get('Core', 'default_language', SITE_DEFAULT_LANGUAGE); } else { // multiple languages // default value $mustRedirect = false; // get possible languages $possibleLanguages = (array) Language::getActiveLanguages(); $redirectLanguages = (array) Language::getRedirectLanguages(); // the language is present in the URL if (isset($chunks[0]) && in_array($chunks[0], $possibleLanguages)) { // define language $language = (string) $chunks[0]; // try to set a cookie with the language try { // set cookie CommonCookie::set('frontend_language', $language); } catch (\SpoonCookieException $e) { // settings cookies isn't allowed, because this isn't a real problem we ignore the exception } // set sessions \SpoonSession::set('frontend_language', $language); // remove the language part array_shift($chunks); } elseif (CommonCookie::exists('frontend_language') && in_array(CommonCookie::get('frontend_language'), $redirectLanguages)) { // set languageId $language = (string) CommonCookie::get('frontend_language'); // redirect is needed $mustRedirect = true; } else { // default browser language // set languageId & abbreviation $language = Language::getBrowserLanguage(); // try to set a cookie with the language try { // set cookie CommonCookie::set('frontend_language', $language); } catch (\SpoonCookieException $e) { // settings cookies isn't allowed, because this isn't a real problem we ignore the exception } // redirect is needed $mustRedirect = true; } // redirect is required if ($mustRedirect) { // build URL // trim the first / from the query string to prevent double slashes $url = rtrim('/' . $language . '/' . trim($this->getQueryString(), '/'), '/'); // when we are just adding the language to the domain, it's a temporary redirect because // Safari keeps the 301 in cache, so the cookie to switch language doesn't work any more $redirectCode = $url == '/' . $language ? 302 : 301; // set header & redirect throw new RedirectException('Redirect', new RedirectResponse($url, $redirectCode)); } } // define the language defined('FRONTEND_LANGUAGE') || define('FRONTEND_LANGUAGE', $language); defined('LANGUAGE') || define('LANGUAGE', $language); // sets the locale file Language::setLocale($language); // list of pageIds & their full URL $keys = Navigation::getKeys(); // rebuild our URL, but without the language parameter. (it's tripped earlier) $url = implode('/', $chunks); $startURL = $url; // loop until we find the URL in the list of pages while (!in_array($url, $keys)) { // remove the last chunk array_pop($chunks); // redefine the URL $url = implode('/', $chunks); } // remove language from query string if ($hasMultiLanguages) { $queryString = trim(mb_substr($queryString, mb_strlen($language)), '/'); } // if it's the homepage AND parameters were given (not allowed!) if ($url == '' && $queryString != '') { // get 404 URL $url = Navigation::getURL(404); // remove language if ($hasMultiLanguages) { $url = str_replace('/' . $language, '', $url); } } // set pages $url = trim($url, '/'); // currently not in the homepage if ($url != '') { // explode in pages $pages = explode('/', $url); // reset pages $this->setPages($pages); // reset parameters $this->setParameters(array()); } // set parameters $parameters = trim(mb_substr($startURL, mb_strlen($url)), '/'); // has at least one parameter if ($parameters != '') { // parameters will be separated by / $parameters = explode('/', $parameters); // set parameters $this->setParameters($parameters); } // pageId, parentId & depth $pageId = Navigation::getPageId(implode('/', $this->getPages())); $pageInfo = Navigation::getPageInfo($pageId); // invalid page, or parameters but no extra if ($pageInfo === false || !empty($parameters) && !$pageInfo['has_extra']) { // get 404 URL $url = Navigation::getURL(404); // remove language if ($hasMultiLanguages) { $url = str_replace('/' . $language, '', $url); } // remove the first slash $url = trim($url, '/'); // currently not in the homepage if ($url != '') { // explode in pages $pages = explode('/', $url); // reset pages $this->setPages($pages); // reset parameters $this->setParameters(array()); } } // is this an internal redirect? if (isset($pageInfo['redirect_page_id']) && $pageInfo['redirect_page_id'] != '') { // get url for item $newPageURL = Navigation::getURL((int) $pageInfo['redirect_page_id']); $errorURL = Navigation::getURL(404); // not an error? if ($newPageURL != $errorURL) { // redirect throw new RedirectException('Redirect', new RedirectResponse($newPageURL, $pageInfo['redirect_code'])); } } // is this an external redirect? if (isset($pageInfo['redirect_url']) && $pageInfo['redirect_url'] != '') { // redirect throw new RedirectException('Redirect', new RedirectResponse($pageInfo['redirect_url'], $pageInfo['redirect_code'])); } }
/** * Set the language * * @param string $value The (interface-)language, will be used to parse labels. * * @throws Exception */ public function setLanguage($value) { // get the possible languages $possibleLanguages = Language::getActiveLanguages(); // validate if (!in_array($value, $possibleLanguages)) { // only 1 active language? if (!Model::getContainer()->getParameter('site.multilanguage') && count($possibleLanguages) == 1) { $this->language = array_shift($possibleLanguages); } else { // multiple languages available but none selected throw new Exception('Language invalid.'); } } else { // language is valid: set property $this->language = (string) $value; } // define constant defined('FRONTEND_LANGUAGE') || define('FRONTEND_LANGUAGE', $this->language); defined('LANGUAGE') || define('LANGUAGE', $this->language); // set the locale (we need this for the labels) Language::setLocale($this->language); }
/** * Get an unique URL for a page * * @param string $url The URL to base on. * @param int $id The id to ignore. * @param int $parentId The parent for the page to create an url for. * @param bool $isAction Is this page an action. * * @return string */ public static function getURL($url, $id = null, $parentId = 0, $isAction = false) { $url = (string) $url; $parentIds = array((int) $parentId); // 0, 1, 2, 3, 4 are all top levels, so we should place them on the same level if ($parentId == 0 || $parentId == 1 || $parentId == 2 || $parentId == 3 || $parentId == 4) { $parentIds = array(0, 1, 2, 3, 4); } // get db $db = BackendModel::getContainer()->get('database'); // no specific id if ($id === null) { // no items? if ((bool) $db->getVar('SELECT 1 FROM pages AS i INNER JOIN meta AS m ON i.meta_id = m.id WHERE i.parent_id IN(' . implode(',', $parentIds) . ') AND i.status = ? AND m.url = ? AND i.language = ? LIMIT 1', array('active', $url, BL::getWorkingLanguage()))) { // add a number $url = BackendModel::addNumber($url); // recall this method, but with a new URL return self::getURL($url, null, $parentId, $isAction); } } else { // one item should be ignored // there are items so, call this method again. if ((bool) $db->getVar('SELECT 1 FROM pages AS i INNER JOIN meta AS m ON i.meta_id = m.id WHERE i.parent_id IN(' . implode(',', $parentIds) . ') AND i.status = ? AND m.url = ? AND i.id != ? AND i.language = ? LIMIT 1', array('active', $url, $id, BL::getWorkingLanguage()))) { // add a number $url = BackendModel::addNumber($url); // recall this method, but with a new URL return self::getURL($url, $id, $parentId, $isAction); } } // get full URL $fullURL = self::getFullURL($parentId) . '/' . $url; // get info about parent page $parentPageInfo = self::get($parentId, null, BL::getWorkingLanguage()); // does the parent have extras? if ($parentPageInfo['has_extra'] == 'Y' && !$isAction) { // set locale FrontendLanguage::setLocale(BL::getWorkingLanguage(), true); // get all on-site action $actions = FrontendLanguage::getActions(); // if the new URL conflicts with an action we should rebuild the URL if (in_array($url, $actions)) { // add a number $url = BackendModel::addNumber($url); // recall this method, but with a new URL return self::getURL($url, $id, $parentId, $isAction); } } // check if folder exists if (is_dir(PATH_WWW . '/' . $fullURL) || is_file(PATH_WWW . '/' . $fullURL)) { // add a number $url = BackendModel::addNumber($url); // recall this method, but with a new URL return self::getURL($url, $id, $parentId, $isAction); } // check if it is an application if (array_key_exists(trim($fullURL, '/'), \ApplicationRouting::getRoutes())) { // add a number $url = BackendModel::addNumber($url); // recall this method, but with a new URL return self::getURL($url, $id, $parentId, $isAction); } // return the unique URL! return $url; }
/** * Parse the default error messages */ private function parseErrorMessages() { // set frontend locale FL::setLocale(BL::getWorkingLanguage(), true); // assign error messages $this->tpl->assign('errors', BackendFormBuilderModel::getErrors()); }
/** * Set locale * * @param string $language The language to load, if not provided we will load the language based on the URL. * @param bool $force Force the language, so don't check if the language is active. * * @throws Exception */ public static function setLocale($language = null, $force = false) { trigger_error('Frontend\\Core\\Engine\\Language is deprecated. It has been moved to Frontend\\Core\\Language\\Language', E_USER_DEPRECATED); return parent::setLocale($language, $force); }
/** * Get the URL for a give module & action combination * * @param string $module The module wherefore the URL should be build. * @param string $action The specific action wherefore the URL should be build. * @param string $language The language wherein the URL should be retrieved, * if not provided we will load the language that was provided in the URL. * @param array $data An array with keys and values that partially or fully match the data of the block. * If it matches multiple versions of that block it will just return the first match. * * @return string */ public static function getURLForBlock($module, $action = null, $language = null, array $data = null) { $module = (string) $module; $action = $action !== null ? (string) $action : null; $language = $language !== null ? (string) $language : BackendLanguage::getWorkingLanguage(); $pageIdForURL = null; $navigation = self::getNavigation($language); $dataMatch = false; // loop types foreach ($navigation as $level) { // loop level foreach ($level as $pages) { // loop pages foreach ($pages as $pageId => $properties) { // only process pages with extra_blocks that are visible if (!isset($properties['extra_blocks']) || $properties['hidden']) { continue; } // loop extras foreach ($properties['extra_blocks'] as $extra) { // direct link? if ($extra['module'] == $module && $extra['action'] == $action && $extra['action'] !== null) { // if there is data check if all the requested data matches the extra data if (isset($extra['data']) && $data !== null && array_intersect_assoc($data, (array) $extra['data']) !== $data) { // It is the correct action but has the wrong data continue; } // exact page was found, so return return self::getURL($properties['page_id'], $language); } if ($extra['module'] == $module && $extra['action'] == null) { // if there is data check if all the requested data matches the extra data if (isset($extra['data']) && $data !== null) { if (array_intersect_assoc($data, (array) $extra['data']) !== $data) { // It is the correct module but has the wrong data continue; } $pageIdForURL = (int) $pageId; $dataMatch = true; } if ($extra['data'] === null && $data === null) { $pageIdForURL = (int) $pageId; $dataMatch = true; } if (!$dataMatch) { $pageIdForURL = (int) $pageId; } } } } } } // still no page id? if ($pageIdForURL === null) { return self::getURL(404, $language); } $url = self::getURL($pageIdForURL, $language); // set locale with force FrontendLanguage::setLocale($language, true); // append action if ($action !== null) { $url .= '/' . urldecode(FrontendLanguage::act(\SpoonFilter::toCamelCase($action))); } // return the unique URL! return $url; }