<?php
  
/*
    Copyright (c)2006 David Rudie
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:
    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    SUCH DAMAGE.
  */


  /*
    This is just a small benchmark class that will keep track of how long it
    takes to load the page and all content until the end of the page.
  */
  
class Benchmark {

    var 
$time_start;
    var 
$time_end;

    function 
UTime() {
      
$microtime explode(" "microtime());
      
$usec = (double) $microtime[0];
      
$sec = (double) $microtime[1];
      return (
$sec $usec);
    }

    function 
Start() {
      
$this->TimeStart $this->UTime();
    }

    function 
End() {
      
$this->TimeEnd $this->UTime();
    }

    function 
Result() {
      
$bm $this->TimeEnd $this->TimeStart;
      echo 
"Page loaded in ".substr($bm05)."s.";
    }

  }


  
/*
    This class handles all the database interaction.  Create a file named
    dbinfo.php and place these variables in it.  Load the file after you have
    initialized this class.
  */
  
class Database {
    var 
$DB;

    var 
$Username '';
    var 
$Password '';
    var 
$Hostname '';
    var 
$Database '';

    
/*
      This function connects to the database and sets the encoding to UTF-8 so
      that the results appear correctly.
     */
    
function Connect() {
      
$DB mysql_connect($this->Hostname$this->Username$this->Password);
      
//mysql_query("SET CHARACTER SET 'utf8'", $DB);
      
mysql_select_db($this->Database);
    }

    function 
Query($Query) {
      
$Result mysql_query($Query);
      return 
$Result;
    }
  }


  
/*
    This is the main class that will handle everything else.  This class
    should probably be split up into smaller classes but it will do for now.
  */
  
class Music {

    var 
$GoogleAdsEnabled 'false';
    var 
$RewriteModEnabled 'false';

    
/*
      In order for XHTML v1.1 to be considered proper it needs to have a
      content-type that specifies application/xhtml+xml.  The problem that comes
      out of this is that not all browsers support this;  One of them is
      Internet Explorer.  The solution is to detect the browser and send the
      content-type that works.
    */
    
function CheckBrowser() {
      
/* This is the client's user agent. */
      
$user_agent $_SERVER['HTTP_USER_AGENT'];

      
/* These are the browsers that support this content-type. */
      
$browser[] = 'firefox';
      
$browser[] = 'applewebkit/420+';
      
$browser[] = 'opera/9.00';
      
$browser[] = 'camino/1.0';

      
/* Now we just match it and send the correct content-type. */
      
foreach ($browser as $supported):
        if (
eregi($supported$user_agent)):
          
header("Content-Type: application/xhtml+xml");
        else:
          
header("Content-Type: text/html");
        endif;
      endforeach;
    }


    
/*
      The format the music lengths are in is milliseconds so we use a simple
      function to convert it to a regular mm:ss format.
    */
    
function ConvertTime($ms) {
      
$ms gmdate('G:i:s'$ms 1000);
      
$ms preg_replace("/^0:0/"""$ms);
      
$ms preg_replace("/^0:/"""$ms);

      if (!
preg_match('/:/'$ms)):
        
$ms '0:'.$ms;
      endif;

      return 
$ms;
    }


    
/*
      Convert the file size from bytes into something more readable.
    */
    
function ConvertSize($bytes) {
      
$types = array("B""KB""MB""GB""TB");
      
$current 0;

      while (
$bytes 1024):
        
$current++;
        
$bytes /= 1024;
      endwhile;

      
$size round($bytes2);

      if (!
preg_match('/\./'$size)):
        
$size $size.".00";
      endif;

      if (
preg_match('/^\d*\.\d$/'$size)):
        
$size $size."0";
      endif;

      return 
$size." ".$types[$current];
    }


    
/*
      The builtin file_exists() function seems to operate oddly every now
      and then.  The solution is to simply create our own function with a
      few extra features.
    */
    
function FileExists($file) {
      
$patterns[0] = '/\(/';
      
$patterns[1] = '/\)/';
      
$patterns[2] = '/\&amp;/';
      
$patterns[3] = '/\[/';
      
$patterns[4] = '/\]/';
      
$patterns[5] = '/\+/';
      
$replaces[0] = '\(';
      
$replaces[1] = '\)';
      
$replaces[2] = '&';
      
$replaces[3] = '\[';
      
$replaces[4] = '\]';
      
$replaces[5] = '\+';
      
$file preg_replace($patterns$replaces$file);
      
$pattern "/^$file\$/";
      
$dh opendir("./albumart/");
      while (
$data readdir($dh)):
        if (
preg_match($pattern$data)):
          return 
true;
        endif;
      endwhile;
      
closedir($dh);
      return 
false;
    }


    
/*
      This function converts some characters and strips out the necessary
      characters in order to have valid file name.
    */
    
function ImageString($text) {
      
$AE chr(198);
      
$i  chr(239);
      
$o  chr(246);
      
$y  chr(255);
      
$text preg_replace('/\//',     '-',     $text);
      
$text preg_replace('/\?/',     '',      $text);
      
$text preg_replace('/\:/',     ' -',    $text);
      
$text preg_replace('/\#/',     '',      $text);
      
$text preg_replace('/\\\\\'/''\'',    $text);
      
$text preg_replace("/$AE/",    'AE',    $text);
      
$text preg_replace("/$i/",     'i',     $text);
      
$text preg_replace("/$o/",     'o',     $text);
      
$text preg_replace("/$y/",     'y',     $text);
      return 
$text;
    }


    
/*
      Clean up some HTML entities to pass XHTML 1.1 validation.
    */
    
function CleanString($Text) {
      
$Text htmlentities($TextENT_QUOTES"UTF-8");
      return 
$Text;
    }


    
/*
      This code selects between two different HTML files based on your option
      of whether you have the rewrite mod enabled or not.  The only difference
      is where the links point.
    */
    
function LetterIndex() {
      if (
$this->RewriteModEnabled == 'true'):
        include(
'./links_rw.html');
      else:
        include(
'./links_rg.html');
      endif;
    }


    
/*
      The code here just inserts Google Ads if you have them enabled.
    */
    
function GoogleAds() {
      if (
$this->GoogleAdsEnabled == 'true'):
        include(
'./googleads.html');
      endif;
    }


    
/*
      Converts a number to a letter.
    */
    
function ToLetter($Number) {
      
$LetterArray = array('0''a''b''c''d''e''f''g''h''i',
                           
'j''k''l''m''n''o''p''q''r''s',
                           
't''u''v''w''x''y''z');

      
$Letter $LetterArray[$Number];

      return 
$Letter;
    }

  }


?>