]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Move common domain-to-network mapping to the plugin module
authorEvan Prodromou <evan@status.net>
Wed, 27 Apr 2011 21:48:26 +0000 (14:48 -0700)
committerEvan Prodromou <evan@status.net>
Wed, 27 Apr 2011 21:48:26 +0000 (14:48 -0700)
plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php
plugins/DomainStatusNetwork/scripts/proposesite.php
plugins/DomainStatusNetwork/scripts/sitefordomain.php

index 0152849a05f2903b992c098c38b8b20bcb91852a..024afd4c29da58272bb3d31910a1c3656c86e46b 100644 (file)
@@ -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;
index 345128d20cdd0f7427b60bb2be605ecabf18bbde..f6efecec23361a55e0fa20db512551aa4002f54a 100644 (file)
@@ -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";
index 80fa6375e0fae36019e18bb70b10ceecdc2b4159..b870c486ab253a1023c7f854f4189bd7f231da2c 100644 (file)
@@ -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";
-    }
-}