static function wfPrefHook($user, &$preferences)
 {
     global $wgUser, $wgRequest, $wgOut;
     $user_id = "";
     $acc_id = "";
     $app_id = "";
     $secret = "";
     $error_msg = "";
     $pairResponse = null;
     # If app_id, secret, user_id and the account_id are already in the DB, we take them
     SpecialLatch::accDB_appsecret($app_id, $secret);
     SpecialLatch::accDB_useraccid($wgUser->getId(), $user_id, $acc_id);
     # We create a new Latch object from the Latch SDK
     $api = new Latch($app_id, $secret);
     # We print the Latch preferences
     SpecialLatch::drawUserPreferences($acc_id, $wgUser, $preferences);
     # If the Pair button is pressed, we try to pair the account
     if ($wgRequest->getCheck('latchTokBot')) {
         # CSRF protection
         if (!$wgUser->matchEditToken($wgRequest->getVal('hiddToken'))) {
             return;
         } else {
             $pair_token = $wgRequest->getText('latchTok');
             # Not empty or extrange characters
             if (empty($pair_token) || preg_match('/\\.([^\\.]*$)/', $pair_token)) {
                 throw new DBExpectedError(null, wfMsg('latch-error-pair'));
             } else {
                 $pairResponse = $api->pair($pair_token);
                 $responseData = $pairResponse->getData();
                 if (!empty($responseData)) {
                     $accountId = $responseData->{"accountId"};
                 }
                 # If everything is OK, we insert the data in the DB
                 if (!empty($accountId)) {
                     SpecialLatch::insDB_useraccid($wgUser, $accountId);
                 } elseif ($pairResponse->getError() == NULL) {
                     throw new DBExpectedError(null, wfMsg('default-error-pair'));
                 } else {
                     switch ($pairResponse->getError()->getCode()) {
                         case 205:
                             $error_msg = wfMsg('205-pair');
                             break;
                         case 206:
                             $error_msg = wfMsg('206-pair');
                             break;
                         case 401:
                             $error_msg = wfMsg('error-401');
                             break;
                         default:
                             $error_msg = wfMsg('default-error-pair');
                             break;
                     }
                     throw new DBExpectedError(null, $pairResponse->getError()->getCode() . " - " . $error_msg);
                 }
             }
         }
     }
     # If the Unpair button is pressed, we try to unpair the account
     if ($wgRequest->getCheck('latchUnpair')) {
         SpecialLatch::accDB_useraccid($wgUser->getId(), $user_id, $acc_id);
         # CSRF protection
         if (!$wgUser->matchEditToken($wgRequest->getVal('hiddToken'))) {
             return;
         } else {
             $pairResponse = $api->unpair($acc_id);
             # If Account ID is empty and no error fields are found, there are problems with the connection to the server
             if ($pairResponse->getError() == NULL) {
                 SpecialLatch::delDB_useraccid($wgUser);
             } else {
                 switch ($pairResponse->getError()->getCode()) {
                     case 201:
                         $error_msg = wfMsg('201-unpair');
                         break;
                     case 401:
                         $error_msg = wfMsg('error-401');
                         break;
                     default:
                         $error_msg = wfMsg('error-unpair');
                         break;
                 }
                 throw new DBExpectedError(null, $pairResponse->getError()->getCode() . " - " . $error_msg);
             }
         }
     }
     # We print the Latch preferences again to make sure that nothing strange happens
     SpecialLatch::drawUserPreferences($acc_id, $wgUser, $preferences);
     # Required return value of a hook function.
     return true;
 }