readByGuid($guid); // log $logDao = new \Drive\LogDao($pdo); $logData = new \Drive\LogData(); $logData->setData($_SERVER); $logDao->create($logData); // visitor check include_once 'process/visitors.php'; // check to see if Visitor code setting is enabled $visitorCode = $settingDao->read('site-visitorcode'); if (!checkVisitors($visitorCode, $slugGenerator)) { // need login, stop here return false; } $http = HTTP; $baseDomain = $http . '://' . DOMAINNAME; $domain = [ 'assets' => 'assets/', 'files' => 'files/', 'images' => 'images/', 'pages' => $baseDomain, ]; // set structure to be sent to twig (at bottom of file) // this allows for exceptions to be added into structure $body = array( 'title' => '', 'subTitle' => '', 'content' => '', 'breadcrumb' => [] ); $pageTitle = ''; $pageSubTitle = ''; $pageContent = ''; $title = ''; $metaKey = ''; $metaDesc = ''; $altLang = $alternateLanguages; //always an array (sometimes empty) $altLangLink = array(); // leave blank -> processed below $canonical = ''; $author = ''; $breadcrumb = array(); $menu = array(); $submenu = array(); $routes = array(); //meta data $facebookTitle = ''; $facebookDesc = ''; $facebookImg = ''; $twitterTitle = ''; $twitterDesc = ''; $editNone = $driveContentArray->getEditNoneArray($languageIndex); $editShort = $driveContentArray->getEditShortArray($languageIndex); $editLong = $driveContentArray->getEditLongArray($languageIndex); $editPlain = $driveContentArray->getEditPlainArray($languageIndex); $siteName = $contentDao->read('site-name', $languageIndex); $twig404 = false; //assume we hit a valid page $baseUrl = '/'; if ($driveLanguages->isMultilingual()) { $baseUrl .= $languageIndex . '/'; } //set baseURl for alternate languages foreach ($altLang as $alternateLanguage) { $altLangLink[$alternateLanguage['index']] = '/'; if ($driveLanguages->isMultilingual()) { $altLangLink[$alternateLanguage['index']] = '/' . $alternateLanguage['index'] . '/'; } } //router $router = new \Custom\Router(); //$router->setBasePath('/drive/'); //if we are in a subfolder //add custom date regEx for blog category: e.g. 2018-05 $router->addMatchTypes(array('yearMonth' => '(19|2[0-1])\d{2}-(0[1-9]|1[0-2])')); $router->addMatchTypes(array('cte' => 'no-cte|cte')); //////////////////////////// // ADD ROUTE EXCEPTIONS HERE // http://altorouter.com/usage/mapping-routes.html //$name = 'routeName'; //needs to be unique // //$target = 'controllerName#' . $menuID . '#' . $submenuID; ////leave menu ids blank if necessary, but include the hashtags //controllerName needs to match the name of the controller file in 'controllers' // //$exceptionRoute = '[format:parameterName]' . '/'; //use as many levels/parameters as required // //$route = $baseUrl . $menuRoute . $exceptionRoute; //use menu/submenu routes as required // //$routes[] = array('GET', $route, $target, $name); // add route for sitemap.xml $routes[] = array('GET', '/sitemap.xml', 'sitemap', 'sitemap'); try { //add preview routes when user is logged in if ($admin->isLoggedIn()) { //add blog preview routes if (in_array('blog', $admin->accessIndexes, true)) { $router->addBlogPreviewRoutes($siteMenu, $baseUrl); } } //add menu routes $router->addMenuRoutes($siteMenu, $baseUrl); //add routes outside of the menu structure if necessary $router->addBlogRoutes($siteMenu, $baseUrl); // add event routes $router->addEventItemRoutes($siteMenu, $baseUrl); $router->addEventItemRegistrationRoutes($siteMenu, $baseUrl); // add team routes $router->addTeamRoutes($siteMenu, $baseUrl); // add partner routes $router->addPartnerRoutes($siteMenu, $baseUrl); // add store routes $router->addStoreCategoryRoutes($siteMenu, $baseUrl); $router->addStoreItemRoutes($siteMenu, $baseUrl); //add exceptional routes $router->addRoutes($routes); } catch (Exception $exception) { //TODO: route to 404? Alert user? Log? } $match = $router->match(); // call closure or throw 404 status if (!empty($match)) { //get the controller and menu Ids [$controller, $menuID, $submenuID] = explode('#', $match['target']); //generate the canonical link from the match (to be used in $metaData) try { $canonical = $router->generate($match['name'], $match['params']); } catch (Exception $exception) { $canonical = ''; } include 'controllers/' . $controller . '.php'; } else { //no route was matched $match['target'] = '404'; $twig404 = true; //header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found'); } if (empty($title)) { $title = $pageTitle; } if (empty($templateFilename)) { $templateFilename = $menuTemplateDao->readDefaultTemplate()->name; } //////////////////////////////// // write the content to the page // Country $country = [ 'index' => $settingDao->read('site-country-index'), 'name' => $contentDao->read('site-country-name', $languageIndex), ]; $meta = array( 'theme' => '#e62c3a', 'canonical' => $canonical, 'alternateLanguageLinks' => $altLangLink, 'author' => $author, 'title' => strip_tags($title), 'description' => $metaDesc, 'keywords' => $metaKey, 'opengraph' => [ // Required 'type' => '', 'title' => '', 'image' => '', 'url' => '', // Optional 'site_name' => '', 'description' => '', 'audio' => '', 'video' => '', 'determiner' => '', // a, an, the, auto or "" (blank, the default value) 'locale' => $languageIndex . '_' . $country['index'], 'locale_alternate' => ['fr_CA', 'en_US'], // displayed as locale:alternate ], 'facebook' => [ 'title' => $facebookTitle, 'description' => $facebookDesc, 'image' => $facebookImg ], 'twitter' => [ // Required 'card' => 'summary', 'title' => $twitterTitle, // Optional 'site' => $siteName, 'description' => $twitterDesc, //'image' => 'http://drivemarketing.ca/path-to-image.jpg', //'image_alt' => 'Alt text for this image', //'creator' => '@myname', ], //'google' => [ // 'link' => '', //], ); // body structure declared at top of file // this allows for exceptions $body['title'] = $pageTitle; $body['subTitle'] = $pageSubTitle; $body['content'] = $pageContent; $body['breadcrumb'] = $breadcrumb; // Template: values used everywhere require './controllers/_general.php'; $admin = $adminRepository->readByGuid($guid); //this is the array we send to twig $content = array( 'testMode' => TESTMODE, 'admin' => $admin, 'settings' => [ 'analytics' => $settingDao->readPartial('analytics-%', true), 'breakpoints' => $settingDao->readPartial('breakpoints-%', true), 'country' => $country, 'share' => $settingDao->readPartial('share-%', true), 'site' => $siteName, 'default' => $settingDao->readPartial('default-%', true), 'stripe' => array( 'liveMode' => $settingDao->read('paygate-stripe-useLive'), 'public' => $settingDao->read('paygate-stripe-useLive') ? $settingDao->read('paygate-stripe-live-public') : $settingDao->read('paygate-stripe-test-public'), 'private' => $settingDao->read('paygate-stripe-useLive') ? $settingDao->read('paygate-stripe-live-private') : $settingDao->read('paygate-stripe-test-private'), ), 'paypal' => array( 'liveMode' => $settingDao->read('paygate-paypal-useLive'), 'sandbox' => $settingDao->read('paygate-paypal-sandbox'), 'production' => $settingDao->read('paygate-paypal-production'), ), ], 'login' => [ 'enabled' => $settingDao->read('login-enabled'), ], 'baseUrl' => $baseUrl, 'http' => $http, 'domain' => $domain, 'languageIndex' => $languageIndex, 'languages' => $driveLanguages->getLanguages(), 'country' => $country, 'meta' => $meta, 'selectedMenu' => $selectedMenu ?? '', 'siteMenu' => $siteMenu, 'general' => $general, 'body' => $body, 'editNone' => $editNone, 'editShort' => $editShort, 'editPlain' => $editPlain, 'editLong' => $editLong, 'multilingual' => $driveLanguages->isMultilingual(), 'stripe' => array( 'liveMode' => $settingDao->read('paygate-stripe-useLive'), 'public' => $settingDao->read('paygate-stripe-useLive') ? $settingDao->read('paygate-stripe-live-public') : $settingDao->read('paygate-stripe-test-public'), 'private' => $settingDao->read('paygate-stripe-useLive') ? $settingDao->read('paygate-stripe-live-private') : $settingDao->read('paygate-stripe-test-private'), ), 'paypal' => array( 'liveMode' => $settingDao->read('paygate-paypal-useLive'), 'sandbox' => $settingDao->read('paygate-paypal-sandbox'), 'production' => $settingDao->read('paygate-paypal-production'), ), ); //we have a $templateFilename and valid page request if (!empty($templateFilename) && !$twig404) { twig($templateFilename, $content, ['tpl'], TESTMODE); //this automatically turns off debug mode for Twig on production servers } else { foreach ($altLang as $alternateLanguage) { $altLangLink[$alternateLanguage['index']] .= '404'; if ($alternateLanguage['default'] === 'Y') { $languageIndex = $languageIndex ?? 'en'; } } $content['meta']['title'] = $contentDao->read('template-short-404-title', $languageIndex); $content['meta']['alternateLanguageLinks'] = $altLangLink; $content['type'] = '404'; $content['message'] = $driveContentArray->getContent('template-short-404-message', $languageIndex); $content['url'] = $http . '://' . DOMAINNAME . $_SERVER['REQUEST_URI']; $content['selectedMenu'] = array( 'image' => 'error.jpg', 'pagetitle' => $contentDao->read('template-short-404-title', $languageIndex), ); http_response_code(404); twig('error.twig', $content, ['tpl'], TESTMODE); } if (TESTMODE) { function rutime($ru, $rus, $index) { return ($ru["ru_$index.tv_sec"] * 1000 + (int)$ru["ru_$index.tv_usec"] / 1000) - ($rus["ru_$index.tv_sec"] * 1000 + (int)$rus["ru_$index.tv_usec"] / 1000); } $ru = getrusage(); $ruReport = date('Y-m-d H:i:s') . "\n"; $ruReport .= 'Request: ' . $_SERVER['REQUEST_URI'] . "\n"; $ruReport .= 'This process used ' . rutime($ru, $ruStart, 'utime') . " ms for its computations\n"; $ruReport .= 'It spent ' . rutime($ru, $ruStart, 'stime') . " ms in system calls\n"; $ruReport .= "\n\n"; file_put_contents('runtime.txt', $ruReport, FILE_APPEND); } ?>