From f8c98952aa5a8a30a50160db864217d6bf942a8f Mon Sep 17 00:00:00 2001 From: Karl 'vollmerk' Vollmer Date: Tue, 31 Jul 2007 07:22:18 +0000 Subject: fixed now playing, hopefully once and for all, added title text to the now playing username that displays the agent they are using to play said song, added distinct sessions and session lengths for each stream instance --- lib/class/stream.class.php | 85 +++++++++++++++++++++++++++++++++++++++++----- lib/class/update.class.php | 41 ++++++++++++++++++++++ 2 files changed, 118 insertions(+), 8 deletions(-) (limited to 'lib/class') diff --git a/lib/class/stream.class.php b/lib/class/stream.class.php index 6fca492c..fe40de79 100644 --- a/lib/class/stream.class.php +++ b/lib/class/stream.class.php @@ -45,13 +45,14 @@ class Stream { $this->type = $type; $this->songs = $song_ids; $this->web_path = Config::get('web_path'); + $this->user_id = $GLOBALS['user']->id; if (Config::get('force_http_play')) { $this->web_path = preg_replace("/https/", "http",$this->web_path); } - $this->sess = session_id(); - $this->user_id = $GLOBALS['user']->id; + // Generate the session ID + $this->session = md5(uniqid(rand(), true));; } // Constructor @@ -67,6 +68,12 @@ class Stream { return false; } + // We're starting insert the session into session_stream + if (!$this->insert_session()) { + debug_event('stream','Session Insertion failure, aboring','3'); + return false; + } + $methods = get_class_methods('Stream'); $create_function = "create_" . $this->type; @@ -93,6 +100,68 @@ class Stream { } // manual_url_add + /** + * insert_session + * This inserts a row into the session_stream table + */ + private function insert_session() { + + $expire = time() + Config::get('stream_length'); + + $sql = "INSERT INTO `session_stream` (`id`,`expire`,`user`) " . + "VALUES('$this->session','$expire','$this->user_id')"; + $db_results = Dba::query($sql); + + if (!$db_results) { return false; } + + return true; + + } // insert_session + + /** + * session_exists + * This checks to see if the passed stream session exists and is valid + */ + public static function session_exists($sid) { + + $sid = Dba::escape($sid); + $time = time(); + + $sql = "SELECT * FROM `session_stream` WHERE `id`='$sid' AND `expire` > '$time'"; + $db_results = Dba::query($sql); + + if ($row = Dba::fetch_assoc($db_results)) { + return true; + } + + return false; + + } // session_exists + + /** + * extend_session + * This takes the passed sid and does a replace into also setting the user + * agent and IP also do a little GC in this function + */ + public static function extend_session($sid,$uid) { + + $expire = time() + Config::get('stream_length'); + $sid = Dba::escape($sid); + $agent = Dba::escape($_SERVER['HTTP_USER_AGENT']); + $ip = ip2int($_SERVER['REMOTE_ADDR']); + $uid = Dba::escape($uid); + + $sql = "UPDATE `session_stream` SET `expire`='$expire', `agent`='$agent', `ip`='$ip' " . + "WHERE `id`='$sid'"; + $db_results = Dba::query($sql); + + $sql = "DELETE FROM `session_stream` WHERE `ip`='$ip' AND `agent`='$agent' AND `user`='$uid' AND `id` != '$sid'"; + $db_results = Dba::query($sql); + + return true; + + } // extend_session + /** * create_simplem3u * this creates a simple m3u without any of the extended information @@ -118,7 +187,7 @@ class Stream { if ($GLOBALS['user']->prefs['play_type'] == 'downsample') { $ds = $GLOBALS['user']->prefs['sample_rate']; } - echo "$this->web_path/play/index.php?song=$song_id&uid=$this->user_id&sid=$this->sess&ds=$ds&stupidwinamp=." . $song->type . "\n"; + echo $song->get_url($this->session); } // end foreach /* Foreach the additional URLs */ @@ -155,7 +224,7 @@ class Stream { $song->format(); echo "#EXTINF:$song->time," . $song->f_artist_full . " - " . $song->title . "\n"; - echo $song->get_url() . "\n"; + echo $song->get_url($this->session) . "\n"; } // end foreach /* Foreach URLS */ @@ -186,7 +255,7 @@ class Stream { $song = new Song($song_id); $song->format(); $song_name = $song->f_artist_full . " - " . $song->title . "." . $song->type; - $song_url = $song->get_url(); + $song_url = $song->get_url($this->session); echo "File" . $i . "=$song_url\n"; echo "Title" . $i . "=$song_name\n"; echo "Length" . $i . "=$song->time\n"; @@ -220,7 +289,7 @@ class Stream { foreach ($this->songs as $song_id) { $song = new Song($song_id); $song->format(); - $url = $song->get_url(); + $url = $song->get_url($this->session); $song_name = $song->f_artist_full . " - " . $song->title . "." . $song->type; echo "\n"; @@ -264,7 +333,7 @@ class Stream { $song->format(); $xml = array(); - $xml['track']['location'] = $song->get_url() . $flash_hack; + $xml['track']['location'] = $song->get_url($this->session) . $flash_hack; $xml['track']['identifier'] = $xml['track']['location']; $xml['track']['title'] = $song->title; $xml['track']['creator'] = $song->f_artist_full; @@ -392,7 +461,7 @@ class Stream { header("Content-Type: audio/x-pn-realaudio ram;"); foreach ($this->songs as $song_id) { $song = new Song($song_id); - echo "$this->web_path/play/index.php?song=$song_id&uid=$this->user_id&sid=$this->sess&stupidwinamp=." . $song->type . "\n"; + echo $song->get_url($this->session); } // foreach songs } // create_ram diff --git a/lib/class/update.class.php b/lib/class/update.class.php index bd1be7af..2af04264 100644 --- a/lib/class/update.class.php +++ b/lib/class/update.class.php @@ -215,6 +215,11 @@ class Update { $version[] = array('version' => '340006','description' => $update_string); + $update_string = '- Added new session_stream table for sessions tied directly to stream instances.
' . + '- Altered the session table, making value a LONGTEXT.
'; + + $version[] = array('version' => '340007','description' => $update_string); + return $version; } // populate_version @@ -807,5 +812,41 @@ class Update { } // update_340006 + /** + * update_340007 + * This update converts the session.value to a longtext + * and adds a session_stream table + */ + public static function update_340007() { + + // Tweak the session table to handle larger session vars for my page-a-nation hotness + $sql = "ALTER TABLE `session` CHANGE `value` `value` LONGTEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL"; + $db_results = Dba::query($sql); + + // Create the new stream table where we will store stream SIDs + $sql = "CREATE TABLE `session_stream` ( " . + "`id` VARCHAR( 64 ) NOT NULL , " . + "`user` INT( 11 ) UNSIGNED NOT NULL , " . + "`agent` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL , " . + "`expire` INT( 11 ) UNSIGNED NOT NULL , " . + "`ip` INT( 11 ) UNSIGNED NULL , " . + "PRIMARY KEY ( `id` ) " . + ") ENGINE = MYISAM"; + $db_results = Dba::query($sql); + + // Change the now playing to use stream session ids for its ID + $sql = "ALTER TABLE `now_playing` CHANGE `id` `id` VARCHAR( 64 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL"; + $db_results = Dba::query($sql); + + // Now longer needed because of the new hotness + $sql = "ALTER TABLE `now_playing` DROP `session`"; + $db_results = Dba::query($sql); + + self::set_version('db_version','340007'); + + return true; + + } // update_340007 + } // end update class ?> -- cgit