/**
  * 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');
 }