Aquarionics

/home/a/aquarion/sites/www.aquarionics.com/epistula/include/ext-libs.inc.php

All my code (That is, anything not in the "Others" list on the right) is BSD licenced.

You can also view this page as text/plain or colour-coded source


<?PHP
/*******************************************************************************
    External Libraries
********************************************************************************

    Inside here are libraries that I stole from elsewhere (With - I think -
    permission to do so)

    $Id: ext-libs.inc.php,v 1.1.1.1 2004/05/16 17:39:17 aquarion Exp $

    $log$

*******************************************************************************/


/*    Format plain text
    http://www.textism.com/tools/textile/
    Dean Allen:    dean@textism.com
    
*/
include($_EP['codedir']."/include/textile.php");



/* Differences between two arrays, 
    http://www.holomind.de/phpnet/diff.src.php
    Daniel Unterberger: d.u.phpnet@holomind.de
*/
function arr_diff( $f1, $f2 ,$show_equal=0 )
{

    $c1=0;
    $c2=0;
    $max1=count( $f1 );
    $max2=count( $f2 );
    $outcount=0;
    $hit1="";
    $hit2="";

    while ( $c1<$max1 and $c2<$max2 and ($stop++)<1000 and $outcount<20 )
    {
        if ( trim( $f1[$c1]) == trim ( $f2[$c2])  )
        {
            $out.= ($show_equal==1) ?  formatline ( ($c1) , ($c2), "=", $f1[ $c1 ] ) : "";
            if ( $show_equal==1) { $outcount++; }
            $c1++;
            $c2++;
        }
        else
        {
            #find matching lines on left, or right later in array
            $b="";
            $s1=0;
            $s2=0;
            $found=0;
            $b1="";
            $b2="";
            $fstop=0;

            #fast search in on both sides for next match.
            while( $found==0 and ($c1+$s1<=$max1) and ($c2+$s2<=$max2) and $fstop++<10 )
            {

                #test left
                if ( trim( $f1[$c1+$s1] ) == trim( $f2[$c2])  )
                {
                    $found=1;
                    $s2=0;
                    $c2--;
                    $b=$b1;
                }
                #more
                else
                {
                    if ( $hit1[ ($c1+$s1)."_".($c2) ]!=1)
                    {
                        $b1.= formatline( ($c1+$s1) , ($c2), "-", $f1[ $c1+$s1 ] );
                        $hit1[ ($c1+$s1)."_".$c2 ]=1;
                    }
                }



                #test right
                if ( trim (  $f1[$c1] )  == trim ( $f2[$c2+$s2])  )
                {
                    $found=1;
                    $s1=0;
                    $c1--;
                    $b=$b2;
                }
                else
                {
                    if ( $hit2[ ($c1)."_".($c2+$s2) ]!=1)
                    {
                        $b2.= formatline ( ($c1) , ($c2+$s2), "+", $f2[ $c2+$s2 ] );
                        $hit2[ ($c1)."_".($c2+$s2) ]=1;
                    }

                 }

                #search in bigger distance
                $s1++;
                $s2++;
            }

            #add line as different on both arrays (no match found)
            if ( $found==0)
            {
                $b.= formatline ( ($c1) , ($c2), "-", $f1[ $c1 ] );
                $b.= formatline ( ($c1) , ($c2), "+", $f2[ $c2 ] );
            }
            $out.= $b;
            $outcount++; #

            $c1++;
            $c2++;

        } /*endif*/

    }/*endwhile*/

return $out;
}/*end func*/


/* Differences between two arrays, 
    http://www.holomind.de/phpnet/diff.src.php
    Daniel Unterberger: d.u.phpnet@holomind.de
*/
function formatline( $nr1, $nr2, $stat, &$value )  #change to $value if problems
{
    if ( trim( $value )=="" )
    {
        return "";
    }

    switch ($stat)
    {
        case "=":
            return $nr1. " : $nr2 : = ".htmlentities( $value )  ."<br>";
        break;

        case "+":
            return $nr1. " : $nr2 : + <font color='blue' >".htmlentities( $value )  ."</font><br>";
        break;

        case "-":
            return $nr1. " : $nr2 : - <font color='red' >".htmlentities( $value )  ."</font><br>";
        break;
    }

}


$tagifylist = <<<EOD
"AOL" acronym title "America Online" style "color: red;"
"API" acronym title "Application Programming Interface"
"AFP" acronym title "Alt Fan Pratchett"
"AFE" acronym title "Alt Fan Eddings"
"ASR" acronym title "Alt Sysadmin Recovery"
"CGI" acronym title "Common Gateway Interface"
"CMS" acronym title "Content Management System"
"CSS" acronym title "Cascading Style Sheets/Content Scrambling System"
"DNS" acronym title "Domain Name System"
"DOM" acronym title "Document Object Model"
"DTD" acronym title "Document Type Definition"
"DVD" acronym title "Digital [Video|Versitle] Disc"
"FAQ" acronym title "Frequently Asked Questions"
"FOAF" acronym title "Friend of a Friend vocabulary"
"FCVO" acronym title "For certian values of..."
"FSF" acronym title "Free Software Foundation"
"GFDL" acronym title "GNU Free Documentation License"
"GPL" acronym title "GNU General Public License"
"IE" acronym title "Internet Explorer"
"IIRC" acronym title "if I remember correctly"
"IIS" acronym title "Internet Infomation Server"
"IXR" acronym title "Incutio XML-RPC library"
"JSP" acronym title "Java Server Pages"
"KB" acronym title "kilobytes"
"KDE" acronym title "K Desktop Environment"
"HTML" acronym title "HyperText Markup Language"
"LGPL" acronym title "GNU Lesser General Public License"
"MB" acronym title "megabyte"
"MSDN" acronym title "Microsoft Developer Network"
"MSIE" acronym title "Microsoft Internet Explorer"
"MSN" acronym title "Microsoft Network"
"OPML" acronym title "Outline Processor Markup Language"
"P2P" acronym title "Peer To Peer"
"PBS" acronym title "Public Broadcasting System"
"PDF" acronym title "Portable Document Format"
"PHP" acronym title "PHP Hypertext Processor"
"RDF" acronym title "Resource Description Framework"
"RPC" acronym title "Remote Procedure Call"
"RSS" acronym title "Rich Site Summary"
"SOAP" acronym title "Simple Object Access Protocol"
"SSN" acronym title "Social Security Number"
"URL" acronym title "Uniform Resource Locator"
"VNC" acronym title "Virtual Network Computing"
"W3C" acronym title "World Wide Web Consortium"
"WCAG" acronym title "Web Content Accessibility Guidelines"
"WAI" acronym title "Web Accessibility Inititive"
"[E]2" acronym title "Epistula 2"
"WYSIWYG" acronym title "what you see is what you get"
"XHTML" acronym title "eXtensible HyperText Markup Language"
"XML" acronym title "eXtensible Markup Language"
"XSL" acronym title "eXtensible Stylesheet Language"
"XSLT" acronym title "eXtensible Stylesheet Language Transformation"
EOD;

function tagifythis($text) {
    global $tagifylist, $cache_tagifylist;

    /* let's build an array of searches and replaces from
       $tagifylist so that we don't have to regex match
       everything every time we call this function */
    if (empty($cache_tagifylist)) {
        $tagifylist = explode("\n", $tagifylist);
        foreach ($tagifylist as $tag_item) {
            preg_match('#^"(.+?)"\s+(\w+)(.*)#', $tag_item, $blah);
            $tag_match = $blah[1];
            $tag_tag = $blah[2];
            $tag_item = $blah[3];
            preg_match_all('#\s(\w+)\s+"(.+?)"#', $tag_item, $blah, PREG_SET_ORDER);
            $tag_attributes = '';
            foreach($blah as $attribute) {
                $tag_attributes .= ' '.$attribute[1].'="'.$attribute[2].'"';
            }
            $match = '#>([^<]{0,}[^a-zA-Z0-9]{0,})('
            .preg_quote($tag_match)
            .')([^a-zA-Z0-9]{0,}[^>]{0,})<#';
            $replace = '>$1<'.$tag_tag.$tag_attributes.'>$2</'.$tag_tag.'>$3<';
            if (strlen($match)) {
                $cache_tagifylist[] = array($match, $replace);
            }
        }
    }

    /* let's dance! */
    foreach ($cache_tagifylist as $tag_item) {
        $match = $tag_item[0];
        $replace = $tag_item[1];
        $text = '> '.$text.' <';
        $text = preg_replace($match, $replace, $text, 1);
        $text = substr($text, 1, -1);
    }

    return($text);
}


/**
 *
 * Currently Hearing Example PHP Implementation
 * 
 * How to use this file on your site:
 *
 * First, include this file from another.
 * include('dir/song.php');
 *
 * Then run the function
 * $song_info = showSong('dir/song.txt', '-', 'Nothing', '3', '0', '1');
 *
 * Then simply echo the $song_info variable at any time.
 * 
 **/

function getAlbumDL($nexttrack){
    $pos = strpos ($nexttrack, " - ");
    if ($pos){
        $nextstat = "<dt>".substr ($nexttrack, 0 , $pos)."</dt>";
        $nextstat .= "<dd>".substr ($nexttrack, $pos+3)."</dd>";
    } else {
        $nextstat = "<dt>".$nexttrack."</dt><dd></dd>";
    }

    return $nextstat;
}

function showSong($filename, $separator, $default, $history='0', $next='0', $track='0') {
    /**
     * filename: The textfile containing the song info that was uploaded by CurrentlyHearing
     * separator: The character you would like placed between Artist and Title (ie: 'Test - This', separator is -)
     * default: What to output on error or when nothing is playing (ie: Nothing)
     * history: Number of songs to display as previously heard (this number should be either 0, 3, 5 or 10 depending on your plugin setting)
     * next: Show next track? 0 = No, 1 = Yes
     * track: Show track number out of total? 0 = No, 1 = Yes
     *
     * Note: filename might need to contain directory information if this file is a different dir than the text file
     *
     **/

    /**
     * Open the file, read it, save its contents for use
     *
     **/
    $file = $filename; // The file
    $open = @fopen($file, 'r'); // Open the file (read-only)
    $read = @fread($open, @filesize($file)); // Read the file
    @fclose($open); // Close the file

    /**
     *
     * Now begin the song extraction from the file contents
     *
     **/

    $song_info = ($read == '') ? ($song_info = $default) : ($song_info = $read); // If the was an error reading file, pretend your listening to nothing
    $additional = $song_info;
    if ($song_info != $default) { // As long as there wasn't an error, begin formatting
        $exp1 = explode('SONG:', $song_info); // Get rid of everything before the song info
        $exp2 = explode(':SONG', $exp1[1]); // Get rid of everything after the song info
        $song_info = getAlbumDL($exp2[0]); // The song
        if ($exp2[0] != 'Nothing') { // If there's a song (ie: Winamp is not off)
            #$exp3 = explode(' - ', $song_info); // Get rid of the separator so we can use ours
            #$artist = $exp3[0]; // The artist
            #$title = $exp3[1]; // The title
            #$song_info = $title . $separator . ' ' . $artist; // Put it together with our separator in between
            
            if ($history != '0') {
                if (($history == '3') || ($history == '5') || ($history == '10')) {
                    // History (if enabled)
                    // Customize this output if you wish
                    $prevstat = '<br />' . 'Previous ' . $history . ':<br />'; 
                    for ($i=1; $i<=$history; $i++) {
                        $prevtrack = explode('P' . $i . ':', $additional);
                        $prevtrack = explode(':P' . $i, $prevtrack[1]);
                        $prevtrack = $prevtrack[0];
                        if ($i == '1') {
                            if ($prevtrack == '') {
                                $prevstat .= 'None';
                                break;
                            } else {
                                $prevstat .= $prevtrack . '<br />';
                            }
                        } else {
                            if ($prevtrack != '') {
                                $prevstat .= $prevtrack . '<br />';
                            }
                        }
                    }
                }
            }

            if ($next == '1') {
                // Next Track (if shuffle isn't on only)
                $nexttrack = explode('NS:', $additional);
                $nexttrack = explode(':NS', $nexttrack[1]);
                $nexttrack = $nexttrack[0];
                if ($nexttrack == '') { $nexttrack = 'Nothing'; }
                // Customize this output if you wish
                $nextstat = getAlbumDL("Next: ".$nexttrack);
                #$nextstat = '<br />' . 'Next: ' . $nexttrack;
            }

            if ($track == '1') {
                // Track Number (out of) Total Playlist Count
                $trackno = explode('S:', $additional);
                $trackno = explode(':S', $trackno[1]);
                $trackno = $trackno[0];
                $totalno = explode('T:', $additional);
                $totalno = explode(':T', $totalno[1]);
                $totalno = $totalno[0];
                // Customize this output if you wish
                $trackstat = 'Track ' . $trackno . ' of ' . $totalno;
            }
            
            // Put together all the bling-bling
            $song_info = $song_info . $nextstat . $trackstat . $prevstat;
        } else {
            $song_info = $default; // There was no song playing
        }
    }

    /**
     *
     * Return the song info -- all done!
     *
     **/
    return "<dl>".$song_info."</dl>";
}

?>


Using a heavily customised version of Tom's PHPCode2ValidXHTML Thing

Nicholas 'Aquarion' Avenell is a web developer in London, you can find out more about him or how to get in touch.

There are more Articles, Projects, Journal Entries, Photographs and things that defy description here, too.

If you're looking for something specific, there are Calendar & Category -based lists of everything.

And if you want to follow stuff that appears here, try a Syndication Feed, or the generic Feed of everything.


Aquarion's last Twitter was: [updating]
Twitter last updated


© 2000 to 2008 inclusive Nicholas Avenell
All comments are the property of their creators, published with permission
(Unless otherwise indicated, the opinions and sentiments expressed on this site are those of the author and not of any organisation of which he is an affiliate, including his employer. Caveat Lector, E&OE. sigh)
0.675 seconds, 1 queries, 2.78Mb on Fri, 29 Aug 2008 21:13:40 +0000, Not cached
Generated by Epistula Version 2.0.3