[xiph-commits] r14749 - in branches/dir.xiph.org: cgi-bin inc templates

balbinus at svn.xiph.org balbinus at svn.xiph.org
Wed Apr 16 10:32:07 PDT 2008


Author: balbinus
Date: 2008-04-16 10:32:05 -0700 (Wed, 16 Apr 2008)
New Revision: 14749

Modified:
   branches/dir.xiph.org/cgi-bin/yp.php
   branches/dir.xiph.org/inc/class.mountpoint.php
   branches/dir.xiph.org/inc/class.server.php
   branches/dir.xiph.org/inc/lib.apilog.php
   branches/dir.xiph.org/templates/yp.xml.tpl
Log:
Modifications batch.

 * cgi-bin/yp.php:
     New clean_string() function (to put into utils...). Use it to clean
     every string coming in. Use the new force_reload parameter of
     retrieveBySID().
 * inc/class.server.php:
     Added a force_reload parameter to retrieveBySID() + retrieveByPk(). Don't
     report removal as failed if only the cache clearing has failed.
 * inc/class.mountpoint.php:
     Don't report removal as failed if only the cache clearing has failed.
 * inc/lib.apilog.php:
     Use MD5 hashes instead of CRC32. Handle our primary keys ourselves.
 * templates/yp.xml.tpl:
     Output "various" as genre if no genre has been given.


Modified: branches/dir.xiph.org/cgi-bin/yp.php
===================================================================
--- branches/dir.xiph.org/cgi-bin/yp.php	2008-04-16 00:00:28 UTC (rev 14748)
+++ branches/dir.xiph.org/cgi-bin/yp.php	2008-04-16 17:32:05 UTC (rev 14749)
@@ -25,6 +25,16 @@
 // Memcache connection
 $memcache = DirXiphOrgMCC::getInstance();
 
+function clean_string($str)
+{
+    $str = mb_ereg_replace('[[:cntrl:]]', '', $str);
+    $str = utils::is_utf8($str) ? $str : utf8_encode($str);
+    $str = mb_convert_encoding($str, 'UTF-8', 'UTF-8,ISO-8859-1,auto');
+    $str = trim($str);
+    
+    return $str;
+}
+
 // Then process it
 switch ($_REQUEST['action'])
 {
@@ -45,10 +55,9 @@
 		    $ip = array_key_exists('REMOTE_ADDR', $_SERVER)
 		            ? $_SERVER['REMOTE_ADDR'] : null;
 		    // Stream name
-		    $stream_name = mb_convert_encoding($_REQUEST['sn'], 'UTF-8',
-                                               'UTF-8,ISO-8859-1,auto');
+		    $stream_name = clean_string($_REQUEST['sn']);
 		    // Media type
-		    $media_type = $_REQUEST['type'];
+		    $media_type = strtolower(clean_string($_REQUEST['type']));
 		    if (array_key_exists('stype', $_REQUEST))
 		    {
 			    if (preg_match('/vorbis/i', $_REQUEST['stype']))
@@ -61,17 +70,16 @@
 			    }
 		    }
 		    // Genre, space-normalized
-		    $genre = mb_convert_encoding($_REQUEST['genre'], 'UTF-8',
-                                         'UTF-8,ISO-8859-1,auto');
+		    $genre = clean_string($_REQUEST['genre']);
 		    $genre = str_replace(array('+', '-', '*', '<', '>', '~', '"', '(', ')', '|', '!', '?', ',', ';', ':', '/'),
 							     array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '),
 							     $genre);
 		    $genre = preg_replace('/\s+/', ' ', $genre);
 		    $genre_list = array_slice(explode(' ', $genre), 0, 10);
 		    // Bitrate
-		    $bitrate = $_REQUEST['b'];
+		    $bitrate = clean_string($_REQUEST['b']);
 		    // Listen URL
-		    $listen_url = $_REQUEST['listenurl'];
+		    $listen_url = clean_string($_REQUEST['listenurl']);
             // Verify the URL
             $url = @parse_url($listen_url);
             if (!$url)
@@ -87,11 +95,17 @@
             }
 		
 		    // Cluster password
-		    $cluster_password = array_key_exists('cpswd', $_REQUEST) ? $_REQUEST['cpswd'] : null;
+		    $cluster_password = array_key_exists('cpswd', $_REQUEST)
+		                            ? clean_string($_REQUEST['cpswd'])
+		                            : null;
 		    // Description
-		    $description = array_key_exists('desc', $_REQUEST) ? $_REQUEST['desc'] : null;
+		    $description = array_key_exists('desc', $_REQUEST)
+	                        ? clean_string($_REQUEST['desc'])
+	                        : null;
 		    // URL
-		    $url = array_key_exists('url', $_REQUEST) ? $_REQUEST['url'] : null;
+		    $url = array_key_exists('url', $_REQUEST)
+		            ? clean_string($_REQUEST['url'])
+		            : null;
 		
 		    // Look for the server (same listen URL)
 		    $server = Server::retrieveByListenUrl($listen_url);
@@ -226,24 +240,23 @@
 		    }
 		    
 		    // SID
-		    $sid = preg_replace('/[^A-F0-9\-]/', '', strtoupper ($_REQUEST['sid']));
+		    $sid = preg_replace('/[^A-F0-9\-]/', '', strtoupper(clean_string($_REQUEST['sid'])));
 		    // Remote IP
 		    $ip = array_key_exists('REMOTE_ADDR', $_SERVER)
 		            ? $_SERVER['REMOTE_ADDR'] : null;
 		    // Song title
 		    $current_song = array_key_exists('st', $_REQUEST)
-		                    ? mb_convert_encoding($_REQUEST['st'], 'UTF-8',
-                                                  'UTF-8,ISO-8859-1,auto')
-                            : null;
+		                        ? clean_string($_REQUEST['st'])
+                                : null;
 		    // Listeners
 		    $listeners = array_key_exists('listeners', $_REQUEST)
-		                 ? intval($_REQUEST['listeners']) : 0;
+    		                 ? intval($_REQUEST['listeners']) : 0;
 		    // Max listeners
 		    $max_listeners = array_key_exists('max_listeners', $_REQUEST)
         		             ? intval($_REQUEST['max_listeners']) : 0;
 		
 		    // Find the server
-		    $server = Server::retrieveBySID($sid);
+		    $server = Server::retrieveBySID($sid, true);
 		    if (!($server instanceOf Server))
 		    {
 		        throw new NoSuchSIDAPIException();

Modified: branches/dir.xiph.org/inc/class.mountpoint.php
===================================================================
--- branches/dir.xiph.org/inc/class.mountpoint.php	2008-04-16 00:00:28 UTC (rev 14748)
+++ branches/dir.xiph.org/inc/class.mountpoint.php	2008-04-16 17:32:05 UTC (rev 14749)
@@ -123,9 +123,9 @@
     public function remove()
     {
         $res0 = $this->removeFromDb();
-        $res1 = $this->removeFromCache();
+        $this->removeFromCache();
         
-        return $res0 && $res1;
+        return $res0;
     }
     
     /**

Modified: branches/dir.xiph.org/inc/class.server.php
===================================================================
--- branches/dir.xiph.org/inc/class.server.php	2008-04-16 00:00:28 UTC (rev 14748)
+++ branches/dir.xiph.org/inc/class.server.php	2008-04-16 17:32:05 UTC (rev 14749)
@@ -58,9 +58,9 @@
      * 
      * @return Server or false if an error occured.
      */
-    public static function retrieveByPk($pk)
+    public static function retrieveByPk($pk, $force_reload = false)
     {
-        $s = new Server($pk);
+        $s = new Server($pk, $force_reload);
         
         return ($s->loaded ? $s : false);
     }
@@ -70,7 +70,7 @@
      * 
      * @return Server or false if an error occured.
      */
-    public static function retrieveBySID($sid)
+    public static function retrieveBySID($sid, $force_reload = false)
     {
         // MySQL Connection
 		$db = DirXiphOrgDBC::getInstance();
@@ -86,7 +86,7 @@
 		    return false;
 		}
 		
-		return self::retrieveByPk(intval($res->current('id')));
+		return self::retrieveByPk(intval($res->current('id')), $force_reload);
     }
     
     /**
@@ -162,9 +162,9 @@
     public function remove()
     {
         $res0 = $this->removeFromDb();
-        $res1 = $this->removeFromCache();
+        $this->removeFromCache();
         
-        return $res0 && $res1;
+        return $res0;
     }
     
     /**

Modified: branches/dir.xiph.org/inc/lib.apilog.php
===================================================================
--- branches/dir.xiph.org/inc/lib.apilog.php	2008-04-16 00:00:28 UTC (rev 14748)
+++ branches/dir.xiph.org/inc/lib.apilog.php	2008-04-16 17:32:05 UTC (rev 14749)
@@ -6,14 +6,28 @@
                                $server_id = null, $mountpoint_id = null)
     {
         $db = DirXiphOrgDBC::getInstance();
-        $sql = 'INSERT INTO `api_log` (`message`, `remote_ip`, `listen_url_hash`, `server_id`, `mountpoint_id`) '
-              .'VALUES ("%s", INET_ATON("%s"), %u, %d, %d);';
-        $sql = sprintf($sql, mysql_real_escape_string($result),
-                             array_key_exists('REMOTE_ADDR', $_SERVER)
-                                ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1',
-                             $listen_url !== null ? sprintf('%u', crc32($listen_url)) : 0,
-                             $server_id, $mountpoint_id);
-        $db->noReturnQuery($sql);
+        
+        try
+        {
+            $db->noReturnQuery('SELECT 0 INTO @prev_id;');
+            $db->noReturnQuery('UPDATE api_log_cpt SET `id_log` = ((`id_log` MOD 10000) + 1) WHERE @prev_id := `id_log;`');
+            $res = $db->singleQuery('SELECT @prev_id AS id;');
+            $id = $res->current('id');
+            
+            $sql = 'REPLACE INTO `api_log` (`id`, `message`, `remote_ip`, `listen_url_hash`, `server_id`, `mountpoint_id`) '
+                  .'VALUES (%d, "%s", INET_ATON("%s"), "%s", %d, %d);';
+            $sql = sprintf($sql, $id,
+                                 mysql_real_escape_string($result),
+                                 array_key_exists('REMOTE_ADDR', $_SERVER)
+                                    ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1',
+                                 $listen_url !== null ? md5($listen_url) : 0,
+                                 $server_id, $mountpoint_id);
+            $db->noReturnQuery($sql);
+        }
+        catch (SQLNoResultException $e)
+        {
+            throw new APIException("Unable to get a new api_log id.");
+        }
     }
     
     public static function serverAdded($ok, $server_id, $mountpoint_id,

Modified: branches/dir.xiph.org/templates/yp.xml.tpl
===================================================================
--- branches/dir.xiph.org/templates/yp.xml.tpl	2008-04-16 00:00:28 UTC (rev 14748)
+++ branches/dir.xiph.org/templates/yp.xml.tpl	2008-04-16 17:32:05 UTC (rev 14749)
@@ -8,7 +8,11 @@
         <bitrate>{$stream.bitrate|escape}</bitrate>
         <channels>{$stream.channels|intval}</channels>
         <samplerate>{$stream.samplerate|intval}</samplerate>
+{if !empty($stream.genre)}
         <genre>{$stream.genre|force_utf8|escape}</genre>
+{else}
+        <genre>various</genre>
+{/if}
         <current_song>{$stream.current_song|force_utf8|escape}</current_song>
     </entry>
 {/foreach}



More information about the commits mailing list