public function getActiveSite()
    {
        $controller = Controller::curr();
        if ($controller instanceof CMSPageEditController) {

            // requests to admin/pages/edit/EditorToolbar/viewfile?ID=XX
            // are not reliable because $controller->currentPage()
            // will return an incorrect page based on the ID $_GET parameter
            if ($controller->getRequest()->param('ID') != 'viewfile') {
                $page = $controller->currentPage();

                if (!$page) {
                    // fixes fatal error when duplicating page
                    // TODO find the root of the problem...
                    return Site::get()->first();
                }

                if ($page instanceof Site) {
                    $this->setActiveSite($page);
                    return $page;
                }

                $site = $page->Site();

                if ($site->ID) {
                    $this->setActiveSite($site);
                    return $site;
                }
            }
        } else if (is_subclass_of($controller, ModelAdmin::class)) {
            // if we are in a model admin that isn't using the global active_site_session,
            // return it's ActiveSite. This is important for cases where a multisite aware
            // data object is being saved for the first time in a model admin, we need to
            // know what site to save it to
            if (!$controller->config()->use_active_site_session) {
                return $controller->getActiveSite();
            }
        }

        if ($id = $controller->getRequest()->getSession()->get('Multisites_ActiveSite')) {
            return Site::get()->byID($id);
        }

        // if($id = Session::get('MultisitesModelAdmin_SiteID')) { // legacy
        // 	return Site::get()->byID($id);
        // }

        return $this->getCurrentSite();
    }