public function __construct() { // because some cronjobs will be run on the command line we should pass parameters if (isset($_SERVER['argv'])) { // init var $first = true; // loop all passes arguments foreach ($_SERVER['argv'] as $parameter) { // ignore first, because this is the scripts name. if ($first) { // reset $first = false; // skip continue; } // split into chunks $chunks = explode('=', $parameter, 2); // valid paramters? if (count($chunks) == 2) { // build key and value $key = trim($chunks[0], '--'); $value = $chunks[1]; // set in GET if ($key != '' && $value != '') { $_GET[$key] = $value; } } } } // define the Named Application if (!defined('NAMED_APPLICATION')) { define('NAMED_APPLICATION', 'backend'); } // set the module $this->setModule(SpoonFilter::getGetValue('module', null, '')); // set the requested file $this->setAction(SpoonFilter::getGetValue('action', null, '')); // set the language $this->setLanguage(SpoonFilter::getGetValue('language', FrontendLanguage::getActiveLanguages(), SITE_DEFAULT_LANGUAGE)); // mark cronjob as run $cronjobs = (array) BackendModel::getModuleSetting('core', 'cronjobs'); $cronjobs[] = $this->getModule() . '.' . $this->getAction(); BackendModel::setModuleSetting('core', 'cronjobs', array_unique($cronjobs)); // create new action $action = new BackendCronjobAction($this->getAction(), $this->getModule()); $action->execute(); }
/** * Process the querystring * * @return void */ private function processQueryString() { // store the querystring local, so we don't alter it. $queryString = $this->getQueryString(); // fix GET-parameters $getChunks = explode('?', $queryString); // are there GET-parameters if (isset($getChunks[1])) { // get key-value pairs $get = explode('&', $getChunks[1]); // remove from querystring $queryString = str_replace('?' . $getChunks[1], '', $this->getQueryString()); // loop pairs foreach ($get as $getItem) { // get key and value $getChunks = explode('=', $getItem, 2); // key available? if (isset($getChunks[0])) { // reset in $_GET $_GET[$getChunks[0]] = isset($getChunks[1]) ? (string) $getChunks[1] : ''; // add into parameters if (isset($getChunks[1])) { $this->parameters[(string) $getChunks[0]] = (string) $getChunks[1]; } } } } // split into chunks $chunks = (array) explode('/', $queryString); // single language if (!SITE_MULTILANGUAGE) { // set language id $language = FrontendModel::getModuleSetting('core', 'default_language', SITE_DEFAULT_LANGUAGE); } else { // default value $mustRedirect = false; // get possible languages $possibleLanguages = (array) FrontendLanguage::getActiveLanguages(); $redirectLanguages = (array) FrontendLanguage::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 SpoonCookie::set('frontend_language', $language, 7 * 24 * 60 * 60, '/', '.' . $this->getDomain()); } 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 (SpoonCookie::exists('frontend_language') && in_array(SpoonCookie::get('frontend_language'), $redirectLanguages)) { // set languageId $language = (string) SpoonCookie::get('frontend_language'); // redirect is needed $mustRedirect = true; } else { // set languageId & abbreviation $language = FrontendLanguage::getBrowserLanguage(); // try to set a cookie with the language try { // set cookie SpoonCookie::set('frontend_language', $language, 7 * 24 * 60 * 60, '/', '.' . $this->getDomain()); } 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 $URL = rtrim('/' . $language . '/' . $this->getQueryString(), '/'); // set header & redirect SpoonHTTP::redirect($URL, 301); } } // define the language define('FRONTEND_LANGUAGE', $language); // sets the localefile FrontendLanguage::setLocale($language); // list of pageIds & their full URL $keys = FrontendNavigation::getKeys(); // full URL $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 querystring if (SITE_MULTILANGUAGE) { $queryString = trim(substr($queryString, strlen($language)), '/'); } // if it's the homepage AND parameters were given (not allowed!) if ($URL == '' && $queryString != '') { // get 404 URL $URL = FrontendNavigation::getURL(404); // remove language if (SITE_MULTILANGUAGE) { $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(substr($startURL, 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 = FrontendNavigation::getPageId(implode('/', $this->getPages())); $pageInfo = FrontendNavigation::getPageInfo($pageId); // invalid page, or parameters but no extra if ($pageInfo === false || !empty($parameters) && !$pageInfo['has_extra']) { // get 404 URL $URL = FrontendNavigation::getURL(404); // remove language if (SITE_MULTILANGUAGE) { $URL = trim(str_replace('/' . $language, '', $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 = FrontendNavigation::getURL((int) $pageInfo['redirect_page_id']); $errorURL = FrontendNavigation::getURL(404); // not an error? if ($newPageURL != $errorURL) { // redirect SpoonHTTP::redirect($newPageURL, $pageInfo['redirect_code']); } } // is this an external redirect? if (isset($pageInfo['redirect_url']) && $pageInfo['redirect_url'] != '') { // redirect SpoonHTTP::redirect($pageInfo['redirect_url'], $pageInfo['redirect_code']); } }
/** * Parse the languages */ protected function parseLanguages() { // just execute if the site is multi-language if (SITE_MULTILANGUAGE) { // get languages $activeLanguages = FrontendLanguage::getActiveLanguages(); // init var $languages = array(); // loop active languages foreach ($activeLanguages as $language) { // build temp array $temp = array(); $temp['url'] = '/' . $language; $temp['label'] = $language; $temp['name'] = FL::msg(strtoupper($language)); $temp['current'] = (bool) ($language == FRONTEND_LANGUAGE); // add $languages[] = $temp; } // assign if (count($languages) > 1) { $this->tpl->assign('languages', $languages); } } }
/** * Get the locale that is used in the frontend but doesn't exists. * * @param string $language The language to check. * @return array */ public static function getNonExistingFrontendLocale($language) { // get files to process $tree = self::getTree(FRONTEND_PATH); $used = array(); // loop files foreach ($tree as $file) { // grab content $content = SpoonFile::getContent($file); // process the file based on extension switch (SpoonFile::getExtension($file)) { // javascript file case 'js': $matches = array(); // get matches preg_match_all('/\\{\\$(act|err|lbl|msg)(.*)(\\|.*)?\\}/iU', $content, $matches); // any matches? if (isset($matches[2])) { // loop matches foreach ($matches[2] as $key => $match) { // set type $type = $matches[1][$key]; // init if needed if (!isset($used[$match])) { $used[$type][$match] = array('files' => array()); } // add file if (!in_array($file, $used[$type][$match]['files'])) { $used[$type][$match]['files'][] = $file; } } } break; // PHP file // PHP file case 'php': $matches = array(); // get matches preg_match_all('/(FrontendLanguage|FL)::(get(Action|Label|Error|Message)|act|lbl|err|msg)\\(\'(.*)\'\\)/iU', $content, $matches); // any matches? if (!empty($matches[4])) { // loop matches foreach ($matches[4] as $key => $match) { $type = 'lbl'; if ($matches[3][$key] == 'Action') { $type = 'act'; } if ($matches[2][$key] == 'act') { $type = 'act'; } if ($matches[3][$key] == 'Error') { $type = 'err'; } if ($matches[2][$key] == 'err') { $type = 'err'; } if ($matches[3][$key] == 'Message') { $type = 'msg'; } if ($matches[2][$key] == 'msg') { $type = 'msg'; } // init if needed if (!isset($used[$type][$match])) { $used[$type][$match] = array('files' => array()); } // add file if (!in_array($file, $used[$type][$match]['files'])) { $used[$type][$match]['files'][] = $file; } } } break; // template file // template file case 'tpl': $matches = array(); // get matches preg_match_all('/\\{\\$(act|err|lbl|msg)([a-z-_]*)(\\|.*)?\\}/iU', $content, $matches); // any matches? if (isset($matches[2])) { // loop matches foreach ($matches[2] as $key => $match) { // set type $type = $matches[1][$key]; // init if needed if (!isset($used[$type][$match])) { $used[$type][$match] = array('files' => array()); } // add file if (!in_array($file, $used[$type][$match]['files'])) { $used[$type][$match]['files'][] = $file; } } } break; } } // init var $nonExisting = array(); // set language FrontendLanguage::setLocale($language); // check if the locale is present in the current language foreach ($used as $type => $items) { // loop items foreach ($items as $key => $data) { // process based on type switch ($type) { case 'act': // if the action isn't available add it to the list if (FL::act($key) == '{$' . $type . $key . '}') { $nonExisting['frontend' . $key . $type] = array('language' => $language, 'application' => 'frontend', 'module' => 'core', 'type' => $type, 'name' => $key, 'used_in' => serialize($data['files'])); } break; case 'err': // if the error isn't available add it to the list if (FL::err($key) == '{$' . $type . $key . '}') { $nonExisting['frontend' . $key . $type] = array('language' => $language, 'application' => 'frontend', 'module' => 'core', 'type' => $type, 'name' => $key, 'used_in' => serialize($data['files'])); } break; case 'lbl': // if the label isn't available add it to the list if (FL::lbl($key) == '{$' . $type . $key . '}') { $nonExisting['frontend' . $key . $type] = array('language' => $language, 'application' => 'frontend', 'module' => 'core', 'type' => $type, 'name' => $key, 'used_in' => serialize($data['files'])); } break; case 'msg': // if the message isn't available add it to the list if (FL::msg($key) == '{$' . $type . $key . '}') { $nonExisting['frontend' . $key . $type] = array('language' => $language, 'application' => 'frontend', 'module' => 'core', 'type' => $type, 'name' => $key, 'used_in' => serialize($data['files'])); } break; } } } ksort($nonExisting); return $nonExisting; }
/** * Set the language * * @return void * @param string $value The (interface-)language, will be used to parse labels. */ public function setLanguage($value) { // get the possible languages $possibleLanguages = FrontendLanguage::getActiveLanguages(); // validate if (!in_array($value, $possibleLanguages)) { // only 1 active language? if (!SITE_MULTILANGUAGE && count($possibleLanguages) == 1) { $this->language = array_shift($possibleLanguages); } else { // create fake action $fakeAction = new FrontendBaseAJAXAction('', ''); // output error $fakeAction->output(FrontendBaseAJAXAction::BAD_REQUEST, null, 'Language not provided.'); } } else { $this->language = (string) $value; } // define constant define('FRONTEND_LANGUAGE', $this->language); // set the locale (we need this for the labels) FrontendLanguage::setLocale($this->language); }
/** * Assign the labels */ private function parseLabels() { $actions = FrontendLanguage::getActions(); $errors = FrontendLanguage::getErrors(); $labels = FrontendLanguage::getLabels(); $messages = FrontendLanguage::getMessages(); // execute addslashes on the values for the locale, will be used in JS if ($this->addSlashes) { foreach ($actions as &$value) { if (!is_array($value)) { $value = addslashes($value); } } foreach ($errors as &$value) { if (!is_array($value)) { $value = addslashes($value); } } foreach ($labels as &$value) { if (!is_array($value)) { $value = addslashes($value); } } foreach ($messages as &$value) { if (!is_array($value)) { $value = addslashes($value); } } } // assign actions $this->assignArray($actions, 'act'); // assign errors $this->assignArray($errors, 'err'); // assign labels $this->assignArray($labels, 'lbl'); // assign messages $this->assignArray($messages, 'msg'); }
/** * Set language * * @param string $value The language. */ private function setLanguage($value) { // set property $this->language = (string) $value; // define constant define('FRONTEND_LANGUAGE', $this->language); // set the locale (we need this for the labels) FrontendLanguage::setLocale($this->language); }
/** * Get an unique URL for a page * * @param string $URL The URL to base on. * @param int[optional] $id The id to ignore. * @param int[optional] $parentId The parent for the page to create an url for. * @param bool[optional] $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 toplevels, 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::getDB(); // 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 { // 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 extra's? if ($parentPageInfo['has_extra'] == 'Y' && !$isAction) { // set locale FrontendLanguage::setLocale(BackendLanguage::getWorkingLanguage(), true); // get all onsite 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 (SpoonDirectory::exists(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 appliation if (in_array(trim($fullURL, '/'), array_keys(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; }
/** * Get the URL for a give module & action combination * * @return string * @param string $module The module to get the URL for. * @param string[optional] $action The action to get the URL for. * @param string[optional] $language The language to use, if not provided we will use the working language. */ public static function getURLForBlock($module, $action = null, $language = null) { // redefine $module = (string) $module; $action = $action !== null ? (string) $action : null; $language = $language !== null ? (string) $language : BackendLanguage::getWorkingLanguage(); // init var $pageIdForURL = null; // get the menuItems $navigation = self::getNavigation($language); // 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 if (isset($properties['extra_blocks'])) { // loop extras foreach ($properties['extra_blocks'] as $extra) { // direct link? if ($extra['module'] == $module && $extra['action'] == $action) { // exacte page was found, so return return self::getURL($properties['page_id'], $language); } elseif ($extra['module'] == $module && $extra['action'] == null) { // store pageId $pageIdForURL = (int) $pageId; } } } } } } // pageId stored? if ($pageIdForURL !== null) { // build URL $URL = self::getURL($pageIdForURL, $language); // set locale FrontendLanguage::setLocale($language); // append action $URL .= '/' . FL::act(SpoonFilter::toCamelCase($action)); // return the unique URL! return $URL; } // fallback return self::getURL(404); }
/** * Assign the labels * * @return void */ private function parseLabels() { // assign actions $this->assignArray(FrontendLanguage::getActions(), 'act'); // assign errors $this->assignArray(FrontendLanguage::getErrors(), 'err'); // assign labels $this->assignArray(FrontendLanguage::getLabels(), 'lbl'); // assign messages $this->assignArray(FrontendLanguage::getMessages(), 'msg'); }
/** * Get a message from the language-file * * @param string $key The key to get. * @return string */ public static function msg($key) { return FrontendLanguage::getMessage($key); }