/**
  * Updates the main email of the current user
  *
  * @param Request $r
  */
 public static function apiUpdateMainEmail(Request $r)
 {
     self::authenticateRequest($r);
     Validators::isEmail($r['email'], 'email');
     try {
         // Update email
         $email = EmailsDAO::getByPK($r['current_user']->getMainEmailId());
         $email->setEmail($r['email']);
         EmailsDAO::save($email);
         // Add verification_id if not there
         if ($r['current_user']->getVerified() == '0') {
             self::$log->info('User not verified.');
             if ($r['current_user']->getVerificationId() == null) {
                 self::$log->info('User does not have verification id. Generating.');
                 try {
                     $r['current_user']->setVerificationId(self::randomString(50));
                     UsersDAO::save($r['current_user']);
                 } catch (Exception $e) {
                     // best effort, eat exception
                 }
             }
         }
     } catch (Exception $e) {
         // If duplicate in DB
         if (strpos($e->getMessage(), '1062') !== false) {
             throw new DuplicatedEntryInDatabaseException('mailInUse');
         } else {
             throw new InvalidDatabaseOperationException($e);
         }
     }
     // Delete profile cache
     Cache::deleteFromCache(Cache::USER_PROFILE, $r['current_user']->getUsername());
     // Send verification email
     $r['user'] = $r['current_user'];
     self::sendVerificationEmail($r);
     return array('status' => 'ok');
 }
 public static function getCurrentSession(Request $r)
 {
     $authToken = $r['auth_token'];
     if (is_null($authToken)) {
         return array('valid' => false, 'id' => null, 'name' => null, 'username' => null, 'email' => null, 'email_md5' => null, 'auth_token' => null, 'is_admin' => false, 'login_url' => '/login/');
     }
     $vo_CurrentUser = AuthTokensDAO::getUserByToken($authToken);
     if (is_null($vo_CurrentUser)) {
         // Means user has auth token, but at
         // does not exist in DB
         return array('valid' => false, 'id' => null, 'name' => null, 'username' => null, 'email' => null, 'email_md5' => null, 'auth_token' => null, 'is_admin' => false, 'login_url' => '/login/');
     }
     // Get email via his id
     $vo_Email = EmailsDAO::getByPK($vo_CurrentUser->getMainEmailId());
     $_SESSION['omegaup_user'] = array('name' => $vo_CurrentUser->getUsername(), 'email' => !is_null($vo_Email) ? $vo_Email->getEmail() : '');
     return array('valid' => true, 'id' => $vo_CurrentUser->getUserId(), 'name' => $vo_CurrentUser->getName(), 'email' => !is_null($vo_Email) ? $vo_Email->getEmail() : '', 'email_md5' => !is_null($vo_Email) ? md5($vo_Email->getEmail()) : '', 'user' => $vo_CurrentUser, 'username' => $vo_CurrentUser->getUsername(), 'auth_token' => $authToken, 'is_email_verified' => $vo_CurrentUser->getVerified(), 'is_admin' => Authorization::IsSystemAdmin($vo_CurrentUser->getUserId()), 'private_contests_count' => ContestsDAO::getPrivateContestsCount($vo_CurrentUser), 'private_problems_count' => ProblemsDAO::getPrivateCount($vo_CurrentUser), 'needs_basic_info' => $vo_CurrentUser->getPassword() == null);
 }