[xiph-commits] r14709 - in branches/dir.xiph.org: cgi-bin cronjobs inc

balbinus at svn.xiph.org balbinus at svn.xiph.org
Fri Apr 11 04:23:08 PDT 2008


Author: balbinus
Date: 2008-04-11 04:23:07 -0700 (Fri, 11 Apr 2008)
New Revision: 14709

Modified:
   branches/dir.xiph.org/cgi-bin/yp.php
   branches/dir.xiph.org/cronjobs/check_servers.php
   branches/dir.xiph.org/cronjobs/prune_mountpoints.php
   branches/dir.xiph.org/inc/lib.apilog.php
   branches/dir.xiph.org/inc/lib.errors.php
   branches/dir.xiph.org/inc/prepend.php
Log:
Modifications batch.

Small tweakings on the logging and server checks.

 * cronjobs/check_servers.php:
     Added a try/catch in order not to receive tons of SQLNoResultException
     error mails :) Corrected a possible FD leak (FD wasn't closed until script
     end if the write failed (thanks to Karl!). Do not send mails on
     fsockopen() errors... it is designed to fail.
 * cronjobs/prune_mountpoints.php:
     Switched to prepend.php.
 * cgi-bin/yp.php, inc/prepend.php, inc/lib.apilog.php, 
     More logging on server refusal.
 * inc/lib.errors.php:
     Don't kill my mailbox.


Modified: branches/dir.xiph.org/cgi-bin/yp.php
===================================================================
--- branches/dir.xiph.org/cgi-bin/yp.php	2008-04-11 09:15:31 UTC (rev 14708)
+++ branches/dir.xiph.org/cgi-bin/yp.php	2008-04-11 11:23:07 UTC (rev 14709)
@@ -10,6 +10,10 @@
 define('REQUEST_FAILED', 0);
 define('REQUEST_REFUSED', -1);
 
+define('SERVER_REFUSED_MISSING_ARG', 0);
+define('SERVER_REFUSED_PARSE_ERROR', 1);
+define('SERVER_REFUSED_ILLEGAL_URL', 2);
+
 // Do we have enough data?
 if (!array_key_exists('action', $_REQUEST))
 {
@@ -33,7 +37,7 @@
 		    {
 		        if (!array_key_exists($a, $_REQUEST))
 		        {
-			        throw new ServerRefusedAPIException('Not enough arguments.');
+			        throw new ServerRefusedAPIException('Not enough arguments.', SERVER_REFUSED_MISSING_ARG);
 		        }
 		    }
 		    // Remote IP
@@ -71,14 +75,14 @@
             $url = @parse_url($listen_url);
             if (!$url)
             {
-                throw new ServerRefusedAPIException('Could not parse listen_url.');
+                throw new ServerRefusedAPIException('Could not parse listen_url.', SERVER_REFUSED_PARSE_ERROR, $listen_url);
             }
             if (empty($url['scheme']) || $url['scheme'] != 'http'
                 || !array_key_exists('host', $url)
                 || !preg_match('/^.*[A-Za-z0-9\-]+\.[A-Za-z0-9]+$/', $url['host'])
                 || preg_match('/^(10\.|192\.168\.|127\.)/', $url['host']))
             {
-                throw new ServerRefusedAPIException('Illegal listen_url.');
+                throw new ServerRefusedAPIException('Illegal listen_url.', SERVER_REFUSED_ILLEGAL_URL, $listen_url);
             }
 		
 		    // Cluster password
@@ -231,6 +235,7 @@
 		    header("SID: -1");
 		    
             // Log stuff
+            APILog::serverRefused($e->getCode(), $e->getListenUrl());
             APILog::request(REQUEST_ADD, REQUEST_REFUSED, $listen_url,
                             $server_id !== false ? $server_id : null,
                             $mp_id !== false ? $mp_id : null);

Modified: branches/dir.xiph.org/cronjobs/check_servers.php
===================================================================
--- branches/dir.xiph.org/cronjobs/check_servers.php	2008-04-11 09:15:31 UTC (rev 14708)
+++ branches/dir.xiph.org/cronjobs/check_servers.php	2008-04-11 11:23:07 UTC (rev 14709)
@@ -12,119 +12,104 @@
 $memcache = DirXiphOrgMCC::getInstance();
 
 // Old stuff that "timeouted"
-$res = $db->selectQuery('SELECT `id`, `listen_url` FROM `server` WHERE `checked` = 0;');
-while (!$res->endOf())
+try
 {
-    try
+    $res = $db->selectQuery('SELECT `id`, `listen_url` FROM `server` WHERE `checked` = 0;');
+    while (!$res->endOf())
     {
-        // Get the URL
-        $url = @parse_url($res->current('listen_url'));
-        if (!$url)
+        try
         {
-            throw new ToDeleteException();
-        }
-        
-        // Now, verify!
-        if (empty($url['scheme']) || $url['scheme'] != 'http'
-            || !array_key_exists('host', $url)
-            || !preg_match('/^.*[A-Za-z0-9\-]+\.[A-Za-z0-9]+$/', $url['host'])
-            || preg_match('/^(10\.|192\.168\.|127\.)/', $url['host']))
-        {
-            throw new ToDeleteException();
-        }
-        
-        // Try to open a connection to the server
-        $ok = false;
-        $count = 0;
-        while ($count < 3 && !$ok)
-        {
-            $fp = fsockopen($url['host'],
-                            array_key_exists('port', $url) ? $url['port'] : 80, // as per HTTP RFC
-                            $errno, $errstr, 5);
-            if (!$fp)
+            printf("Processing %s...\n", $res->current('listen_url'));
+            // Get the URL
+            $url = @parse_url($res->current('listen_url'));
+            if (!$url)
             {
-                $count++;
-                continue;
+                throw new ToDeleteException();
             }
             
-            // Now send a request
-            $req = sprintf("GET %s HTTP/1.0\r\n\r\n", $url['path']);
-            $r = fwrite($fp, $req);
-            if (!$r || $r != strlen($req))
+            // Now, verify!
+            if (empty($url['scheme']) || $url['scheme'] != 'http'
+                || !array_key_exists('host', $url)
+                || !preg_match('/^.*[A-Za-z0-9\-]+\.[A-Za-z0-9]+$/', $url['host'])
+                || preg_match('/^(10\.|192\.168\.|127\.)/', $url['host']))
             {
-                $count++;
-                continue;
+                throw new ToDeleteException();
             }
-            $r = 0;
-            $headers = array();
-            do
+            
+            // Try to open a connection to the server
+            $ok = false;
+            $count = 0;
+            while ($count < 3 && !$ok)
             {
-                $data = fgets($fp);
-                if (trim($data) != '')
+                $fp = @fsockopen($url['host'],
+                                 array_key_exists('port', $url) ? $url['port'] : 80, // as per HTTP RFC
+                                 $errno, $errstr, 5);
+                if (!$fp)
                 {
-                    list($header, $value) = explode(':', $data);
-                    $headers[strtolower(trim($header))] = trim($value);
+                    $count++;
+                    continue;
                 }
-                $r++;
+                
+                // Now send a request
+                $req = sprintf("GET %s HTTP/1.0\r\n\r\n", $url['path']);
+                $r = fwrite($fp, $req);
+                if (!$r || $r != strlen($req))
+                {
+                    fclose($fp);
+                    $count++;
+                    continue;
+                }
+                $r = 0;
+                $headers = array();
+                do
+                {
+                    $data = fgets($fp);
+                    if (trim($data) != '')
+                    {
+                        list($header, $value) = explode(':', $data);
+                        $headers[strtolower(trim($header))] = trim($value);
+                    }
+                    $r++;
+                }
+                while (trim($data) != '' && $r < 10);
+                
+                // Extremely dangerous, desactivated.
+/*              if (!array_key_exists('server', $headers)
+                    || !stristr($headers['server'], 'icecast'))
+                {
+                    throw new ToDeleteException();
+                }*/
+                fclose($fp);
+                
+                $count++;
+                $ok = true;
             }
-            while (trim($data) != '' && $r < 10);
-            
-            // Extremely dangerous, desactivated.
-/*            if (!array_key_exists('server', $headers)
-                || !stristr($headers['server'], 'icecast'))
+            if (!$ok)
             {
                 throw new ToDeleteException();
-            }*/
-            fclose($fp);
+            }
             
-            $count++;
-            $ok = true;
-        }
-        if (!$ok)
+            // If we're here, everything's ok.
+            $sql = 'UPDATE `server` SET `checked` = 1, `checked_at` = NOW() WHERE `id` = %d;';
+            $sql = sprintf($sql, $res->current('id'));
+            $db->noReturnQuery($sql);
+        } 
+        catch (ToDeleteException $e)
         {
-            throw new ToDeleteException();
+            // TODO: remove the stream
+            echo("Delete it! ".$res->current('listen_url')."\n");
+            
+            $sql = 'UPDATE `server` SET `checked` = 2, `checked_at` = NOW() WHERE `id` = %d;';
+            $sql = sprintf($sql, $res->current('id'));
+            $db->noReturnQuery($sql);
         }
         
-        // If we're here, everything's ok.
-        $sql = 'UPDATE `server` SET `checked` = 1, `checked_at` = NOW() WHERE `id` = %d;';
-        $sql = sprintf($sql, $res->current('id'));
-        $db->noReturnQuery($sql);
-    } 
-    catch (ToDeleteException $e)
-    {
-        // TODO: remove the stream
-        echo("Delete it! ".$res->current('listen_url')."\n");
-        
-        $sql = 'UPDATE `server` SET `checked` = 1, `checked_at` = NOW() WHERE `id` = %d;';
-        $sql = sprintf($sql, $res->current('id'));
-        $db->noReturnQuery($sql);
+        $res->next();
     }
-    
-    $res->next();
 }
-
-exit();
-// Useless mountpoint
-$toDelete = $db->selectQuery('SELECT m.`id` AS `mountpoint_id`, s.`id` FROM `mountpoint` AS m LEFT OUTER JOIN `server` AS s ON m.`id` = s.`mountpoint_id` HAVING s.`id` IS NULL;');
-while (!$toDelete->endOf())
+catch (SQLNoResultException $e)
 {
-	// Deletion
-	$mp_id = $toDelete->current('mountpoint_id');
-	$sql = 'DELETE FROM `mountpoint` WHERE `id` = %d;';
-	$db->noReturnQuery(sprintf($sql, $mp_id));
-	
-	// Tag cloud update
-	$sql = 'UPDATE `tag_cloud` SET `tag_usage` = `tag_usage` - 1 WHERE `tag_id` IN (SELECT `tag_id` FROM `mountpoints_tags` WHERE `mountpoint_id` = %d);';
-	$db->noReturnQuery(sprintf($sql, $mp_id));
-	
-	// Next!
-	$toDelete->next();
+    echo "OK.\n";
 }
 
-// Now prune the tags
-$sql = 'DELETE FROM `tag` WHERE `id` IN (SELECT `tag_id` FROM `tag_cloud` WHERE `tag_usage` <= 0);';
-$db->noReturnQuery($sql);
-$sql = 'DELETE FROM `tag_cloud` WHERE `tag_usage` <= 0;';
-$db->noReturnQuery($sql);
-
 ?>

Modified: branches/dir.xiph.org/cronjobs/prune_mountpoints.php
===================================================================
--- branches/dir.xiph.org/cronjobs/prune_mountpoints.php	2008-04-11 09:15:31 UTC (rev 14708)
+++ branches/dir.xiph.org/cronjobs/prune_mountpoints.php	2008-04-11 11:23:07 UTC (rev 14709)
@@ -1,12 +1,7 @@
 <?php
 
 // Inclusions
-include_once(dirname(__FILE__).'/../inc/class.db.php');
-include_once(dirname(__FILE__).'/../inc/class.mc.php');
-include_once(dirname(__FILE__).'/../inc/class.izterator.php');
-include_once(dirname(__FILE__).'/../inc/class.izteratorbuilder.php');
-include_once(dirname(__FILE__).'/../inc/inc.db.php');
-include_once(dirname(__FILE__).'/../inc/inc.mc.php');
+include_once(dirname(__FILE__).'/../inc/prepend.php');
 
 // Database connection
 $db = DirXiphOrgDBC::getInstance();

Modified: branches/dir.xiph.org/inc/lib.apilog.php
===================================================================
--- branches/dir.xiph.org/inc/lib.apilog.php	2008-04-11 09:15:31 UTC (rev 14708)
+++ branches/dir.xiph.org/inc/lib.apilog.php	2008-04-11 11:23:07 UTC (rev 14709)
@@ -51,6 +51,19 @@
         }
         self::log($message, $listen_url, $server_id, $mountpoint_id);
     }
+    
+    public static function serverRefused($reason, $listen_url = false)
+    {
+        $db = DirXiphOrgDBC::getInstance();
+        $sql = 'INSERT INTO `refused_log` (`reason`, `remote_ip`, `listen_url`, `listen_url_hash`) '
+              .'VALUES (%d, INET_ATON("%s"), "%s", %u);';
+        $sql = sprintf($sql, intval($reason),
+                             array_key_exists('REMOTE_ADDR', $_SERVER)
+                                ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1',
+                             $listen_url != false ? mysql_real_escape_string($listen_url) : '',
+                             $listen_url != false ? sprintf('%u', crc32($listen_url)) : 0);
+        $db->noReturnQuery($sql);
+    }
 }
 
 ?>

Modified: branches/dir.xiph.org/inc/lib.errors.php
===================================================================
--- branches/dir.xiph.org/inc/lib.errors.php	2008-04-11 09:15:31 UTC (rev 14708)
+++ branches/dir.xiph.org/inc/lib.errors.php	2008-04-11 11:23:07 UTC (rev 14709)
@@ -1,6 +1,6 @@
 <?php
 
-define('ERROR_RECIPIENT', 'balbinus at orbus.fr');
+define('ERROR_RECIPIENT', 'annie.dupont1137 at gmail.com');
 define('ERROR_SENDER', 'bug');
 
 function mail_error($mail_to, $mail_from,

Modified: branches/dir.xiph.org/inc/prepend.php
===================================================================
--- branches/dir.xiph.org/inc/prepend.php	2008-04-11 09:15:31 UTC (rev 14708)
+++ branches/dir.xiph.org/inc/prepend.php	2008-04-11 11:23:07 UTC (rev 14709)
@@ -17,7 +17,30 @@
 class EnvironmentUndefinedException extends DXOException { }
 
 class APIException extends DXOException { }
-class ServerRefusedAPIException extends APIException { }
+class ServerRefusedAPIException extends APIException
+{
+    protected $listen_url = false;
+    
+    public function __construct($errstr, $errno, $listen_url = null)
+    {
+        if ($listen_url !== null)
+        {
+            $this->listen_url = $listen_url;
+        }
+        
+        parent::__construct($errstr, $errno);
+    }
+    
+    public function getListenUrl()
+    {
+        return $this->listen_url;
+    }
+    
+    public function setListenUrl($url)
+    {
+        $this->listen_url = $url;
+    }
+}
 class NoSuchSIDAPIException extends APIException { }
 
 if (getenv('ENVIRONMENT') !== false)



More information about the commits mailing list