We are in the process of migrating the Spacial Wiki content to our new Help Center at spacial.com.
Please visit the Help Center for latest Tips and Tricks, Documentation and Troubleshooting.
spacial.com/help-center

Simple Currently Playing Block

From SpacialAudio

Jump to: navigation, search

This is a simple block for PHPNuke that shows the currently playing track and the previous ten tracks.

This ONLY works when used in conjunction with the Shoutcast DNAS server and has only been tested under PHPNuke 6.0 +

To use this you need two files - the actual block file and the scxml.php file.

First, edit the block-Shoutcast.php file and enter your DNAS information (IP/Hostname, Port and Admin pass. Place this file in your Nuke's BLOCKS directory.

Then, edit the scxml.php file and again enter your IP/Hostname, Port and Admin Pass. Place this file in your nuke's INCLUDES directory.

Activate the block in your Nuke admin control panel and away you go!

block-SHOUTcast.php

<?php
  /* Modified from the original */
  /* Mike's SHOUTcast Block     */
  /* v1.7  12/09/02             */
  /* mike@fuckingbrit.com       */
  /* http://www.fuckingbrit.com */
  
  /* SCXML copyright Tom Pepper, tom@nullsoft.com */
   
  if (eregi("block-SHOUTcast.php",$PHP_SELF))
  {
    Header("Location: index.php");
    die();
  }
  
  $servers = array(
                  'servers' => array(
                                      'YOURIP'
                                     ),
                  'ports' => array(
                                    YOURPORT
                                  ),
                  'passwords' => array(
                                        'YOURPASS',
                                      ),
                  'lasttracks' => 10
                  );
  $refreshtime = 60 * 3; // How often to refresh (seconds)
  
  require_once('includes/scxml.php');
  require_once('config.php');
  global $prefix, $dbi;
  
  
  
  /* Function to fetch the info */ 
  function getInfo($servers)
  {
    $server = new SCXML;
    $listeners = 0;
    $nowplaying = 'Data Unavailable';
    $peak = 0;
    $max = 0;
  
    while( $host = each($servers['servers']) )
    {
      $port = each($servers['ports']);
      $password = each($servers['passwords']);
      $server->set_host($host[1]);
    	$server->set_port($port[1]);
    	$server->set_password($password[1]);
    	
    	
      if ($server->retrieveXML())
      {
        $nowplaying = urldecode($server->fetchMatchingTag("SONGTITLE"));
        $listeners += $server->fetchMatchingTag("CURRENTLISTENERS");
        $peak += $server->fetchMatchingTag("PEAKLISTENERS");
        $max += $server->fetchMatchingTag("MAXLISTENERS");
        $lasttracks = $server->fetchMatchingArray("TITLE");
      }
    }
    $content .= "<center><b>Now Playing:</b></center><hr><B><font color=\"CCCCCC\">$nowplaying</font?</B><hr> ";
    $content .= '<center>';
    $content.='</center>';
    if( $servers['lasttracks'] > 0 ) {
      $max = $servers['lasttracks'];
      if( count($lasttracks) < $max ) $max = count($lasttracks);
      $content .= "<br><b>Last $max tracks:</b><br>";
      for( $i=0; $i < $max; $i++ ) {
        $content .= ($i + 1).". $lasttracks[$i]<br>";
      }
    }
    return $content;
  }
  
  /* Function to cache the string $content */
  function cacheInfo($content,$title)
  {
    global $dbi, $prefix;
    $now = time();
    $sql = "UPDATE ".$prefix."_blocks SET content='".$content."', time=".$now
           ." WHERE title='".$title."'";
    sql_query($sql,$dbi);
  }

  /* Get Cached Content: */
  $sql = "SELECT content,time FROM ".$prefix."_blocks WHERE title='".$title."'";
  $result = sql_query($sql,$dbi);
  $roottime = time() - $refreshtime;
  if($result)
  {
    $row = sql_fetch_array($result, $dbi);
    /* Check time! */
    if( $row[1] < $roottime )
    {
      $content = getInfo($servers);
      cacheInfo($content,$title);
    }
    else
    {
      $content = $row[0];
    }
  }
  /* Otherwise, we have to fetch, and cache the details */
  else
  {
    $content = getInfo($servers);
    cacheInfo($content,$title);
  }
  
?>

scxml.php

<?php
/* SCXML object class 0.2.1 t.pepper */

class SCXML {

  var $host="YOURIP";
  var $port=YOURPORT;
  var $password="YOURPASS";

  var $depth = 0;
  var $lastelem= array();
  var $xmlelem = array();
  var $xmldata = array();
  var $stackloc = 0;

  var $parser;

  function set_password($password) {
    $this->password=$password;
  }

  function set_host($host) {
    $this->host=$host;
  }

  function set_port($port) {
    $this->port=$port;
  }

  function startElement($parser, $name, $attrs) {
    $this->stackloc++;
    $this->lastelem[$this->stackloc]=$name;
    $this->depth++;
  }

  function endElement($parser, $name) {
    unset($this->lastelem[$this->stackloc]);
    $this->stackloc--;
  }

  function characterData($parser, $data) {
    $data=trim($data);
    if ($data) { 
      $this->xmlelem[$this->depth]=$this->lastelem[$this->stackloc];
      $this->xmldata[$this->depth].=$data; 
    }
  }

  function retrieveXML() {
    $rval=1;

    $sp=fsockopen($this->host,$this->port,&$errno,&$errstr,10);
    if (!$sp) $rval=0;
    else {
      
      set_socket_blocking($sp,false);
      
      // send request
      
      fputs($sp,"GET /admin.cgi?pass=$this->password&mode=viewxml 
HTTP/1.1\nUser-Agent:Mozilla\n\n");
      
      // fetch response, timeout if it takes > 15s
      
      for($i=0; $i<30; $i++) {
    if(feof($sp)) break; // exit if connection broken
    $sp_data.=fread($sp,31337);
    usleep(500000);
      }
      
      // strip HTTP headers so all we have is XML data
      
      $sp_data=ereg_replace("^.*<!DOCTYPE","<!DOCTYPE",$sp_data);

      /* xml code goes here, here's a plain empty parser */
      
      $this->parser = xml_parser_create();
      xml_set_object($this->parser,&$this);
      xml_set_element_handler($this->parser, "startElement", "endElement");
      xml_set_character_data_handler($this->parser, "characterData");
      
      if (!xml_parse($this->parser, $sp_data, 1)) {
    $rval=-1;
      }
      
      xml_parser_free($this->parser);
      
    }
    return $rval;
  }

  function debugDump(){
    reset($this->xmlelem);
    while (list($key,$val) = each($this->xmlelem)) {
      echo "$key. $val -> ".$this->xmldata[$key]."\n";
    }
    
  }

  function fetchMatchingArray($tag){
    reset($this->xmlelem);
    $rval = array();
    while (list($key,$val) = each($this->xmlelem)) {
      if ($val==$tag) $rval[]=$this->xmldata[$key];
    }
    return $rval;
  }

  function fetchMatchingTag($tag){
    reset($this->xmlelem);
    $rval = "";
    while (list($key,$val) = each($this->xmlelem)) {
      if ($val==$tag) $rval=$this->xmldata[$key];
    }
    return $rval;
  }

}
?>
Personal tools