From 57d1a3dcc0fb58eb13e2a47254ba3f2b656dd3f3 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 26 Apr 2011 16:17:24 -0400 Subject: [PATCH] get a free site nickname by email or domain --- .../EmailRegistration/scripts/proposesite.php | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 plugins/EmailRegistration/scripts/proposesite.php diff --git a/plugins/EmailRegistration/scripts/proposesite.php b/plugins/EmailRegistration/scripts/proposesite.php new file mode 100644 index 0000000000..345128d20c --- /dev/null +++ b/plugins/EmailRegistration/scripts/proposesite.php @@ -0,0 +1,94 @@ +#!/usr/bin/env php +. + */ + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..')); + +$helptext = << +Prints site information for the domain given + +END_OF_SITEFORDOMAIN_HELP; + +require_once INSTALLDIR.'/scripts/commandline.inc'; +require_once INSTALLDIR.'/plugins/EmailRegistration/extlib/effectiveTLDs.inc.php'; +require_once INSTALLDIR.'/plugins/EmailRegistration/extlib/regDomain.inc.php'; + +function nicknameAvailable($nickname) +{ + $sn = Status_network::staticGet('nickname', $nickname); + return !empty($sn); +} + +function nicknameForDomain($domain) +{ + global $tldTree; + + $registered = getRegisteredDomain($domain, $tldTree); + + $parts = explode('.', $registered); + + $base = $parts[0]; + + if (nicknameAvailable($base)) { + return $base; + } + + $domainish = str_replace('.', '-', $registered); + + if (nicknameAvailable($domainish)) { + return $domainish; + } + + $i = 1; + + // We don't need to keep doing this forever + + while ($i < 1024) { + $candidate = $domainish.'-'.$i; + if (nicknameAvailable($candidate)) { + return $candidate; + } + } + + return null; +} + +$raw = $args[0]; + +$parts = explode('@', $raw); + +if (count($parts) == 1) { + $domain = $parts[0]; +} else { + $domain = $parts[1]; +} + +$domain = strtolower(trim($domain)); + +$nickname = nicknameForDomain($domain); + +if (empty($nickname)) { + throw ClientException("No candidate found."); +} else { + print $nickname; + print "\n"; +} -- 2.39.5