/**
 * OWA authentication filter method
 *
 * This filter function authenticates the user and populates the
 * the current user in OWA with the proper role, and allowed sites list.
 *
 * This method kicks in after all over OWA's built in auth methods have failed
 * in the owa_auth class.
 * 
 * @param 	$auth_status	boolean
 * @return	$auth_status	boolean
 */
function owa_wpAuthUser($auth_status)
{
    $current_user = wp_get_current_user();
    if ($current_user instanceof WP_User) {
        // logged in, authenticated
        $cu = owa_coreAPI::getCurrentUser();
        $cu->setAuthStatus(true);
        if (isset($current_user->user_login)) {
            $cu->setUserData('user_id', $current_user->user_login);
            owa_coreAPI::debug("Wordpress User_id: " . $current_user->user_login);
        }
        if (isset($current_user->user_email)) {
            $cu->setUserData('email_address', $current_user->user_email);
        }
        if (isset($current_user->first_name)) {
            $cu->setUserData('real_name', $current_user->first_name . ' ' . $current_user->last_name);
            $cu->setRole(owa_translate_role($current_user->roles));
        }
        owa_coreAPI::debug("Wordpress User Role: " . print_r($current_user->roles, true));
        owa_coreAPI::debug("Wordpress Translated OWA User Role: " . $cu->getRole());
        // fetch the list of allowed blogs from WP
        $domains = array();
        $allowedBlogs = get_blogs_of_user($current_user->ID);
        foreach ($allowedBlogs as $blog) {
            $domains[] = $blog->siteurl;
        }
        // check to see if we are installing before trying to load sites
        // other wise you run into a race condition as config file
        // might not be created.
        if (!defined('OWA_INSTALLING')) {
            // load assigned sites list by domain
            $cu->loadAssignedSitesByDomain($domains);
        }
        $cu->setInitialized();
        return true;
    } else {
        // not logged in to WP and therefor not authenticated
        return false;
    }
}
/**
 * Singleton Method
 *
 * Returns an instance of OWA
 *
 * @return $owa object
 */
function &owa_getInstance()
{
    static $owa;
    if (empty($owa)) {
        require_once OWA_BASE_CLASSES_DIR . 'owa_wp.php';
        // create owa instance w/ config
        $owa = new owa_wp();
        $owa->setSiteId(md5(get_settings('siteurl')));
        $owa->setSetting('base', 'report_wrapper', 'wrapper_wordpress.tpl');
        $owa->setSetting('base', 'link_template', '%s&%s');
        $owa->setSetting('base', 'main_url', '../wp-admin/index.php?page=owa');
        $owa->setSetting('base', 'main_absolute_url', get_bloginfo('url') . '/wp-admin/index.php?page=owa');
        $owa->setSetting('base', 'action_url', get_bloginfo('url') . '/index.php?owa_specialAction');
        $owa->setSetting('base', 'api_url', get_bloginfo('url') . '/index.php?owa_apiAction');
        $owa->setSetting('base', 'is_embedded', true);
        // needed as old installs might have this turned on by default...
        $owa->setSetting('base', 'delay_first_hit', false);
        // Access WP current user object to check permissions
        $current_user = owa_getCurrentWpUser();
        //print_r($current_user);
        // Set OWA's current user info and mark as authenticated so that
        // downstream controllers don't have to authenticate
        $cu =& owa_coreAPI::getCurrentUser();
        if (isset($current_user->user_login)) {
            $cu->setUserData('user_id', $current_user->user_login);
            owa_coreAPI::debug("Wordpress User_id: " . $current_user->user_login);
        }
        if (isset($current_user->user_email)) {
            $cu->setUserData('email_address', $current_user->user_email);
        }
        if (isset($current_user->first_name)) {
            $cu->setUserData('real_name', $current_user->first_name . ' ' . $current_user->last_name);
            $cu->setRole(owa_translate_role($current_user->roles));
        }
        owa_coreAPI::debug("Wordpress User Role: " . print_r($current_user->roles, true));
        owa_coreAPI::debug("Wordpress Translated OWA User Role: " . $cu->getRole());
        $cu->setAuthStatus(true);
    }
    return $owa;
}
/**
 * Populates OWA's current user object with info about the current mediawiki user.
 * This info is needed by OWA authentication system as well as to add dimensions
 * requests that are logged.
 */
function owa_mwAuthUser($auth_status)
{
    global $wgUser, $wgOwaSiteId;
    if ($wgUser->isLoggedIn()) {
        $cu = owa_coreAPI::getCurrentUser();
        $cu->setAuthStatus(true);
        $cu->setUserData('user_id', $wgUser->getName());
        $cu->setUserData('email_address', $wgUser->getEmail());
        $cu->setUserData('real_name', $wgUser->getRealName());
        $cu->setRole(owa_translate_role($wgUser->getGroups()));
        // set list of allowed sites. In this case it's only this wiki.
        $domains = array($wgOwaSiteId);
        // load assigned sites list by domain
        $cu->loadAssignedSitesByDomain($domains);
        $cu->setInitialized();
        return true;
    } else {
        // not logged in
        return false;
    }
}
/**
 * OWA Singelton
 *
 * Needed to avoid OWA loading for every mediawiki request
 */
function owa_singleton()
{
    static $owa;
    if (empty($owa)) {
        global $wgUser, $wgServer, $wgScriptPath, $wgScript, $wgMainCacheType, $wgMemCachedServers, $wgOwaSiteId, $wgOwaMemCachedServers;
        /* OWA CONFIGURATION OVERRIDES */
        $owa_config = array();
        // check for memcache. these need to be passed into OWA to avoid race condition.
        if ($wgMainCacheType === CACHE_MEMCACHED) {
            $owa_config['cacheType'] = 'memcached';
            $owa_config['memcachedServers'] = $wgMemCachedServers;
        }
        $owa = new owa_mw($owa_config);
        $owa->setSetting('base', 'report_wrapper', 'wrapper_mediawiki.tpl');
        $owa->setSetting('base', 'main_url', $wgScriptPath . '/index.php?title=Special:Owa');
        $owa->setSetting('base', 'main_absolute_url', $wgServer . $owa->getSetting('base', 'main_url'));
        $owa->setSetting('base', 'action_url', $wgServer . $wgScriptPath . '/index.php?action=owa&owa_specialAction');
        $owa->setSetting('base', 'api_url', $wgServer . $wgScriptPath . '/index.php?action=owa&owa_apiAction');
        $owa->setSetting('base', 'link_template', '%s&%s');
        $owa->setSetting('base', 'is_embedded', true);
        $owa->setSetting('base', 'query_string_filters', 'returnto');
        $owa->setSetting('base', 'delay_first_hit', false);
        if (!$wgOwaSiteId) {
            $wgOwaSiteId = md5($wgServer . $wgScriptPath);
        }
        $owa->setSiteId($wgOwaSiteId);
        /**
         * Populates OWA's current user object with info about the current mediawiki user.
         * This info is needed by OWA authentication system as well as to add dimensions
         * requests that are logged.
         */
        $cu =& owa_coreAPI::getCurrentUser();
        $cu->setUserData('user_id', $wgUser->getName());
        $cu->setUserData('email_address', $wgUser->getEmail());
        $cu->setUserData('real_name', $wgUser->getRealName());
        $cu->setRole(owa_translate_role($wgUser->getGroups()));
        $cu->setAuthStatus(true);
    }
    return $owa;
}