<?PHP
class phpBBSync extends Plugin
{
    function Register()
    {
        $this->Name = "phpBB Login Synchronization";
        $this->Version = "1.0.0";
        $this->Description = "Allows users to sign in to an associated "
            ."phpBB installation using their CWIS login.  An included companion "
            ."module (<i>auth_cwis.php</i>) must be loaded into phpBB "
            ."for this plugin to work.";
        $this->Author = "Internet Scout";
        $this->Url = "http://scout.wisc.edu/cwis/";
        $this->Email = "scout@scout.wisc.edu";
        $this->Requires = array(
            "CWISCore" => "2.0.0"
            );
    }

    function Install()
    {
        $DB = new Database();
        $DB->Query("CREATE TABLE phpBBLoginData ( "
                   ."UserName TEXT, "
                   ."RemIp TEXT,"
                   ."Token INT );");
    }

    function HookEvents()
    {
        return array(
            "EVENT_USER_LOGIN"            => "phpBBMirrorUserLogin",
            "EVENT_USER_LOGOUT"           => "phpBBMirrorUserLogout",
            "EVENT_USER_DELETED"          => "phpBBMirrorUserDel"
            );
    }

    /*
      The phpBB auth_cwis module looks for a login token set by CWIS
      to determine if a user should be logged in or out. To guard
      against cookie forgery, there's a random number embedded in the
      cookie and stored in the CWIS database which phpBB will validate
      before completing a login/logout.

      Logging a user in to phpBB means setting the cookie and DB
      token.

      A user is logged out of phpBB when that cookie/token is deleted.
     */

    function phpBBMirrorUserLogin($UserId, $Password)
    {
        $DB = new Database();
        $User = new SPTUser($UserId);

        // Generate a login token
        mt_srand((double)microtime() * 1000000);
        $Token = mt_rand(0, 2147483647);

        // Insert data into the database
        $DB->Query("INSERT INTO phpBBLoginData "
                   ."(UserName, RemIP, Token) VALUES ("
                   ."'".addslashes($User->Name())."', "
                   ."'".$_SERVER["REMOTE_ADDR"]."', "
                   .$Token.")");

        // And set a cookie for phpBB to find
        $CookieName = "phpBBLoginToken";
        $CookieData = array(
            "Version" => 1,
            "UserName" => $User->Name(),
            "Token"    => $Token);
        $EncData = urlencode(
            base64_encode(gzcompress(serialize($CookieData))));
        setcookie($CookieName, $EncData, time()+3600*24, "/");

    }

    function phpBBMirrorUserLogout($UserId)
    {
        $DB = new Database();
        $User = new SPTUser($UserId);

        // Nuke the DB entry
        $DB->Query("DELETE FROM phpBBLoginData WHERE "
                   ."UserName='".addslashes($User->Name())."'");

        // Nuke the cookie
        $CookieName = "phpBBLoginToken";
        setcookie($CookieName, "", time() - 3600, "/");
    }

    function phpBBMirrorUserDel($UserId)
    {
        $DB = new Database();
        $User = new SPTUser($UserId);

        $DB->Query("INSERT INTO phpBBLoginData (UserName,RemIp) "
                   ."VALUES ('".$User->Name()."','GoAwayMrUser')");
    }

};
?>
