Jump to content

Change Domain Contact Details in PreRegistrarRegisterDomain


Znuff

Recommended Posts

Hello,

I have a relatively simple issue with ResellerClub not accepting Unicode chars in the State/City field (and probably others).

I'm trying to change the contact details with the help of PreRegistrarRegisterDomain, but so far I'm not having much luck. My code seems to be working fine, it changes the array from what I can tell, but the Registrar is still replying with Error.

This is my code (transliteration function shamelessly stolen from another module posted on this community, I forget which):

<?php
function remove_unicode_before_registrar($string){

    $nonLatinChars = array(
        '/ä|æ|ǽ/' => 'ae',
        '/ö|œ/' => 'oe',
        '/ü/' => 'ue',
        '/Ä/' => 'Ae',
        '/Ü/' => 'Ue',
        '/Ö/' => 'Oe',
        '/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ|Α|Ά|Ả|Ạ|Ầ|Ẫ|Ẩ|Ậ|Ằ|Ắ|Ẵ|Ẳ|Ặ|А/' => 'A',
        '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|α|ά|ả|ạ|ầ|ấ|ẫ|ẩ|ậ|ằ|ắ|ẵ|ẳ|ặ|а/' => 'a',
        '/Б/' => 'B',
        '/б/' => 'b',
        '/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
        '/ç|ć|ĉ|ċ|č/' => 'c',
        '/Д/' => 'D',
        '/д/' => 'd',
        '/Ð|Ď|Đ|Δ/' => 'Dj',
        '/ð|ď|đ|δ/' => 'dj',
        '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě|Ε|Έ|Ẽ|Ẻ|Ẹ|Ề|Ế|Ễ|Ể|Ệ|Е|Ё|Э/' => 'E',
        '/è|é|ê|ë|ē|ĕ|ė|ę|ě|έ|ε|ẽ|ẻ|ẹ|ề|ế|ễ|ể|ệ|е|ё|э/' => 'e',
        '/Ф/' => 'F',
        '/ф/' => 'f',
        '/Ĝ|Ğ|Ġ|Ģ|Γ|Г/' => 'G',
        '/ĝ|ğ|ġ|ģ|γ|г/' => 'g',
        '/Ĥ|Ħ/' => 'H',
        '/ĥ|ħ/' => 'h',
        '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|Η|Ή|Ί|Ι|Ϊ|Ỉ|Ị|И|Й/' => 'I',
        '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı|η|ή|ί|ι|ϊ|ỉ|ị|и|й/' => 'i',
        '/Ĵ/' => 'J',
        '/ĵ/' => 'j',
        '/Ķ|Κ|К/' => 'K',
        '/ķ|κ|к/' => 'k',
        '/Ĺ|Ļ|Ľ|Ŀ|Ł|Λ|Л/' => 'L',
        '/ĺ|ļ|ľ|ŀ|ł|λ|л/' => 'l',
        '/М/' => 'M',
        '/м/' => 'm',
        '/Ñ|Ń|Ņ|Ň|Ν|Н/' => 'N',
        '/ñ|ń|ņ|ň|ʼn|ν|н/' => 'n',
        '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ|Ο|Ό|Ω|Ώ|Ỏ|Ọ|Ồ|Ố|Ỗ|Ổ|Ộ|Ờ|Ớ|Ỡ|Ở|Ợ|О/' => 'O',
        '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º|ο|ό|ω|ώ|ỏ|ọ|ồ|ố|ỗ|ổ|ộ|ờ|ớ|ỡ|ở|ợ|о/' => 'o',
        '/П/' => 'P',
        '/п/' => 'p',
        '/Ŕ|Ŗ|Ř|Ρ|Р/' => 'R',
        '/ŕ|ŗ|ř|ρ|р/' => 'r',
        '/Ś|Ŝ|Ş|Ș|Š|Σ|С/' => 'S',
        '/ś|ŝ|ş|ș|š|ſ|σ|ς|с/' => 's',
        '/Ț|Ţ|Ť|Ŧ|τ|Т/' => 'T',
        '/ț|ţ|ť|ŧ|т/' => 't',
        '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ|Ũ|Ủ|Ụ|Ừ|Ứ|Ữ|Ử|Ự|У/' => 'U',
        '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ|υ|ύ|ϋ|ủ|ụ|ừ|ứ|ữ|ử|ự|у/' => 'u',
        '/Ý|Ÿ|Ŷ|Υ|Ύ|Ϋ|Ỳ|Ỹ|Ỷ|Ỵ/' => 'Y',
        '/ý|ÿ|ŷ|ỳ|ỹ|ỷ|ỵ/' => 'y',
        '/В/' => 'V',
        '/в/' => 'v',
        '/Ŵ/' => 'W',
        '/ŵ/' => 'w',
        '/Ź|Ż|Ž|Ζ|З/' => 'Z',
        '/ź|ż|ž|ζ|з/' => 'z',
        '/Æ|Ǽ/' => 'AE',
        '/ß/'=> 'ss',
        '/IJ/' => 'IJ',
        '/ij/' => 'ij',
        '/Œ/' => 'OE',
        '/ƒ/' => 'f',
        '/ξ/' => 'ks',
        '/π/' => 'p',
        '/β/' => 'v',
        '/μ/' => 'm',
        '/ψ/' => 'ps',
        '/Ж/' => 'Zh',
        '/ж/' => 'zh',
        '/Х/' => 'Kh',
        '/х/' => 'kh',
        '/Ц/' => 'Tc',
        '/ц/' => 'tc',
        '/Ч/' => 'Ch',
        '/ч/' => 'ch',
        '/Ы/' => 'Y',
        '/ы/' => 'y',
        '/Ш/' => 'Sh',
        '/ш/' => 'sh',
        '/Щ/' => 'Shch',
        '/щ/' => 'shch',
        '/Ю/' => 'Iu',
        '/ю/' => 'iu',
        '/ю/' => 'iu',
        '/Я/' => 'Ia',
        '/я/' => 'ia'
    );

    $nonLatinFrom = array_keys($nonLatinChars);
    $nonLatinTo = array_values($nonLatinChars);

    $separator = '-';
    $pregSeparator = preg_quote($separator, '#');

    $string = trim($string);
    $string = strip_tags($string);
    $string = preg_replace($nonLatinFrom, $nonLatinTo, $string);

    $string = trim(trim($string, $separator));

return $string;
}

add_hook('PreRegistrarRegisterDomain', 1, function($vars) {
  array_walk($vars['params'], function(&$value) {
    $value = remove_unicode_before_registrar($value); }
  );

  return $vars;
});

The code seems to be working fine when I test it externally: https://3v4l.org/Sbojv - you can see it transliterates Constanța to Constanta just fine.

This also seems to be working fine in WHMCS when I var_dump($vars) in the code, but the registrar is still being sent the non-transliterated values, so I still end up with 

{state=State is invalid., stateid=State is invalid, city=City is invalid.}

What am I doing wrong here, and why is the registrar module not picking up the transliterated strings?

Link to comment
Share on other sites

  • 2 months later...

Answering this for posterity.

While the hook does indeed seem to be NOT functioning correctly, as per #CORE-13757, the best way to do fix my initial issue would be to use this hook:

<?php

function hook_transliterate($string) {
  $string = transliterator_transliterate('Any-Latin; Latin-ASCII;', $string);
  return $string;
}

Notice how we are using transliterator_transliterate(), which is a built-in function of PHP, instead of having a huge array of characters to swap. 

The WHMCS documentation could be a little bit clearer on this functionality, as in, we initially thought that the client data would be stored in WHMCS already transliterated (which we didn't want, at all), but it does not seem to be the case.

Link to comment
Share on other sites

  • 2 months later...
On 9/18/2019 at 11:08 PM, Znuff said:

Answering this for posterity.

While the hook does indeed seem to be NOT functioning correctly, as per #CORE-13757, the best way to do fix my initial issue would be to use this hook:


<?php

function hook_transliterate($string) {
  $string = transliterator_transliterate('Any-Latin; Latin-ASCII;', $string);
  return $string;
}

Notice how we are using transliterator_transliterate(), which is a built-in function of PHP, instead of having a huge array of characters to swap. 

The WHMCS documentation could be a little bit clearer on this functionality, as in, we initially thought that the client data would be stored in WHMCS already transliterated (which we didn't want, at all), but it does not seem to be the case.

Thanks for sharing this... Two questions though:

1) Do we only need this one, or do we need the first one (in your original post) as well?
2) How does the system know when to get it triggered? Shouldn't we add any hook for PreDomainRegister for example?

Link to comment
Share on other sites

1) no, you only need this

2) https://docs.whmcs.com/Custom_Transliteration:

 

Quote

A default function included in WHMCS will automatically take care of and replace the most common special characters that cause problems for users but if you need to tailor it to your specific needs, then you can do so by defining a hook_transliterate function within a file in the /includes/hooks/ directory as follows to override the default function, and specify your own custom character replacement process within it.

 

Link to comment
Share on other sites

It should be fine if you trigger transliteration on the following events:

Moreover I would use the standard PHP function:

<?php

// Transliterate everything
$firstname = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0100-\u7fff] Remove;', $firstname);
$lastname = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0100-\u7fff] Remove;', $lastname);
$companyname = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0100-\u7fff] Remove;', $companyname);
$address1 = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0100-\u7fff] Remove;', $address1);
$address2 = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0100-\u7fff] Remove;', $address2);
$city = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0100-\u7fff] Remove;', $city);
$state = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0100-\u7fff] Remove;', $state);
$postcode = transliterator_transliterate('Any-Latin; Latin-ASCII; [\u0100-\u7fff] Remove;', $postcode);
// Probably you also need to transliterate Client Custom Fields

// UPDATE tblclients SET firstname = $firstname, lastname = $lastname [...] WHERE id = $id
// OR
// UPDATE tblcontacts SET firstname = $firstname, lastname = $lastname [...] WHERE id = $id

I suspect that you can't use hooks like PreDomainRegister since they don't allow you to "override" client details on the fly but I could be wrong.

Edited by Kian
Link to comment
Share on other sites

  • 4 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated