Exemple #1
0
function getAuth($room)
{
    if (file_exists('data/auth/' . $room)) {
        $authfile = $room;
    } elseif (file_exists('data/auth/all')) {
        $authfile = 'all';
    } else {
        return false;
    }
    $data = file_get_contents('data/auth/' . $authfile);
    $json = json_decode($data);
    if ($json->token->expires < time()) {
        // token expired, request new
        LogMe("Room {$room} Token expired, request new");
        $token = getNewToken($json->install->oauthId, $json->install->oauthSecret);
        $token->expires = time() + $token->expires_in;
        $json->token = $token;
        LogMe("New Token: " . $json->token->access_token);
        file_put_contents('data/auth/' . $authfile, json_encode($json));
        return $json->token->access_token;
    } else {
        // token valid
        LogMe("Room {$authfile} Token still valid");
        return $json->token->access_token;
    }
}
define('SCOPES', implode(' ', array(Google_Service_Gmail::GMAIL_READONLY)));
if (isset($_GET['action'])) {
    $action = $_GET['action'];
} else {
    $action = "none";
}
if (isset($_COOKIE['MoodleSession'])) {
    $userid = getMoodleIdFromSession($_COOKIE['MoodleSession']);
    //echo "User ID = $userid ";
    if (isStudent($userid)) {
        $userToken = getUserToken($userid);
        if (isset($userToken)) {
            printUnreadMessages($userToken);
        } else {
            if ($action == 'getToken') {
                getNewToken();
            } else {
                echo "0";
            }
        }
    } else {
        echo "null";
    }
}
function printUnreadMessages($userToken)
{
    global $GMAIL;
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfigFile(CLIENT_SECRET_PATH);