if ($USER_CURRENT) {
                        userLogIn($USER_CURRENT);
                    }
                }
            }
        }
        $USER_CURRENT_LOADED = true;
    }
    return $USER_CURRENT;
}
$app['currentUser'] = userGetCurrent();
$app->before(function () use($app) {
    $app['twig']->addGlobal('currentUser', $app['currentUser']);
    $app['twig']->addFunction(new Twig_SimpleFunction('getCurrentUserPrivateFeedKey', function () {
        $r = new \repositories\UserAccountPrivateFeedKeyRepository();
        return $r->getForUser(userGetCurrent());
    }));
    $app['twig']->addFunction(new Twig_SimpleFunction('getCSFRToken', function () {
        global $WEBSESSION;
        return $WEBSESSION->getCSFRToken();
    }));
    $app['twig']->addFunction(new Twig_SimpleFunction('getAndClearFlashMessages', function () {
        global $FLASHMESSAGES;
        return $FLASHMESSAGES->getAndClearMessages();
    }));
    $app['twig']->addFunction(new Twig_SimpleFunction('getAndClearFlashErrors', function () {
        global $FLASHMESSAGES;
        return $FLASHMESSAGES->getAndClearErrors();
    }));
    # ////////////// 12 or 24 hour clock
    $clock12Hour = true;
     // the v and u passed to this have no effect here - they are just cache busters
     header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 30 * 60));
     $data = array();
     // TODO would like to depreceate httpDomain and get scripts to just use httpDomainIndex & httpDomainSite for clarity
     $data['httpDomain'] = $site->getSlug() . "." . $CONFIG->webSiteDomain;
     $data['httpDomainIndex'] = $CONFIG->webIndexDomain;
     if ($CONFIG->hasSSL) {
         $data['hasSSL'] = true;
         $data['httpsDomain'] = $site->getSlug() . "." . $CONFIG->webSiteDomainSSL;
         $data['httpsDomainIndex'] = $CONFIG->webIndexDomainSSL;
     } else {
         $data['hasSSL'] = false;
     }
     $data['twitter'] = $CONFIG->contactTwitter;
     $data['isSingleSiteMode'] = false;
     $user = userGetCurrent();
     if ($user) {
         $data['currentUser'] = array('username' => $user->getUsername());
     } else {
         $data['currentUser'] = false;
     }
     $removeEditorPermissions = false;
     $userHasNoEditorPermissionsInSiteRepo = new UserHasNoEditorPermissionsInSiteRepository();
     if ($app['currentUser'] && $userHasNoEditorPermissionsInSiteRepo->isUserInSite($app['currentUser'], $site)) {
         $removeEditorPermissions = true;
     }
     $userPermissionsRepo = new \repositories\UserPermissionsRepository($app['extensions']);
     $currentUserPermissions = $userPermissionsRepo->getPermissionsForUserInSite($user, $site, $removeEditorPermissions, true);
     $data['currentUserPermissions'] = $currentUserPermissions->getAsArrayForJSON();
     print "var config = " . json_encode($data);
 }