From b6bad0232e01f23b4b89963d2fdf09bd8c72f276 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 27 Apr 2011 14:48:26 -0700 Subject: [PATCH] Move common domain-to-network mapping to the plugin module --- .../DomainStatusNetworkPlugin.php | 94 +++++++++++++++++++ .../scripts/proposesite.php | 58 +----------- .../scripts/sitefordomain.php | 21 ++--- 3 files changed, 103 insertions(+), 70 deletions(-) diff --git a/plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php b/plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php index 0152849a05..024afd4c29 100644 --- a/plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php +++ b/plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php @@ -34,6 +34,11 @@ if (!defined('STATUSNET')) { exit(1); } +$_dir = dirname(__FILE__); + +require_once $_dir . '/extlib/effectiveTLDs.inc.php'; +require_once $_dir . '/extlib/regDomain.inc.php'; + /** * Tools to map one status_network to one email domain in a multi-site * installation. @@ -48,8 +53,20 @@ if (!defined('STATUSNET')) { class DomainStatusNetworkPlugin extends Plugin { + static $_thetree = null; + function initialize() { + // For various reasons this gets squished + + global $tldTree; + + if (empty($tldTree)) { + if (!empty(self::$_thetree)) { + $tldTree = self::$_thetree; + } + } + $nickname = StatusNet::currentSite(); if (empty($nickname)) { @@ -73,6 +90,78 @@ class DomainStatusNetworkPlugin extends Plugin } } + static function toDomain($raw) + { + $parts = explode('@', $raw); + + if (count($parts) == 1) { + $domain = $parts[0]; + } else { + $domain = $parts[1]; + } + + $domain = strtolower(trim($domain)); + + return $domain; + } + + static function registeredDomain($domain) + { + return getRegisteredDomain($domain); + } + + static function nicknameAvailable($nickname) + { + $sn = Status_network::staticGet('nickname', $nickname); + return empty($sn); + } + + static function nicknameForDomain($domain) + { + $registered = self::registeredDomain($domain); + + $parts = explode('.', $registered); + + $base = $parts[0]; + + if (self::nicknameAvailable($base)) { + return $base; + } + + $domainish = str_replace('.', '-', $registered); + + if (self::nicknameAvailable($domainish)) { + return $domainish; + } + + $i = 1; + + // We don't need to keep doing this forever + + while ($i < 1024) { + $candidate = $domainish.'-'.$i; + if (self::nicknameAvailable($candidate)) { + return $candidate; + } + $i++; + } + + return null; + } + + static function siteForDomain($domain) + { + $snt = Status_network_tag::withTag('domain='.$domain); + + while ($snt->fetch()) { + $sn = Status_network::staticGet('site_id', $snt->site_id); + if (!empty($sn)) { + return $sn; + } + } + return null; + } + function onPluginVersion(&$versions) { $versions[] = array('name' => 'DomainStatusNetwork', @@ -84,3 +173,8 @@ class DomainStatusNetworkPlugin extends Plugin return true; } } + +// The way addPlugin() works, this global variable gets disappeared. +// So, we re-appear it. + +DomainStatusNetworkPlugin::$_thetree = $tldTree; diff --git a/plugins/DomainStatusNetwork/scripts/proposesite.php b/plugins/DomainStatusNetwork/scripts/proposesite.php index 345128d20c..f6efecec23 100644 --- a/plugins/DomainStatusNetwork/scripts/proposesite.php +++ b/plugins/DomainStatusNetwork/scripts/proposesite.php @@ -29,65 +29,13 @@ 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)); +$domain = DomainStatusNetworkPlugin::toDomain($args[0]); -$nickname = nicknameForDomain($domain); +$nickname = DomainStatusNetworkPlugin::nicknameForDomain($domain); if (empty($nickname)) { - throw ClientException("No candidate found."); + throw new ClientException("No candidate found."); } else { print $nickname; print "\n"; diff --git a/plugins/DomainStatusNetwork/scripts/sitefordomain.php b/plugins/DomainStatusNetwork/scripts/sitefordomain.php index 80fa6375e0..b870c486ab 100644 --- a/plugins/DomainStatusNetwork/scripts/sitefordomain.php +++ b/plugins/DomainStatusNetwork/scripts/sitefordomain.php @@ -30,23 +30,14 @@ END_OF_SITEFORDOMAIN_HELP; require_once INSTALLDIR.'/scripts/commandline.inc'; -$raw = $args[0]; +$domain = DomainStatusNetworkPlugin::toDomain($args[0]); -$parts = explode('@', $raw); +$sn = DomainStatusNetworkPlugin::siteForDomain($domain); -if (count($parts) == 1) { - $domain = $parts[0]; -} else { - $domain = $parts[1]; +if (empty($sn)) { + exit(1); } -$domain = strtolower(trim($domain)); +print $sn->nickname."\n"; +exit(0); -$snt = Status_network_tag::withTag('domain='.$domain); - -while ($snt->fetch()) { - $sn = Status_network::staticGet('site_id', $snt->site_id); - if (!empty($sn)) { - print $sn->nickname."\n"; - } -} -- 2.39.5