<?php
// WARNING: This script is for educational purposes only. Use of it in any form comes with your express knowledge that you are personally responsible for your actions.
//if (!isset($_GET['dev'])) die("DEVELOPING");

/* USAGE:
 * ./itunes-iart.php?param=value&param2=value2
 * @param term        If used, 'artist', 'album' and 'song' will be ignored. Is a general keyword search.
 * @param artist    Artist to search by
 * @param album        Album to search by
 * @param song        Song to search by
 * @param sep        String to separate results by, default "|"
*/

require_once("ciTMS.h.php");

if (
$_GET['artist'] == "" && $_GET['album'] == "" && $_GET['song'] == "" && $_GET['term'] == "" && $_GET['pid'] == "")
{
    exit;
}
if (isset(
$_GET['pid']))
{
    
$type 'pid';
    
$pid=$_GET['pid'];
}
elseif (isset(
$_GET['term']))
{
    
$type 'basic';
    
$term $_GET['term'];
    
$attr $_GET['attr'];
}
else 
{
    
$type 'advanced';
    
$artist $_GET['artist'];
    
$album $_GET['album'];
    
$song $_GET['song'];
}

if (!isset(
$_GET['sep'])) { $sep='|'; }
else { 
$sep=$_GET['sep']; }
if (!isset(
$_GET['brk'])) { $brk="<br />\n"; }
else { 
$brk=$_GET['brk']; }

if (isset(
$error)) error($error);

$itms = new ciTMS;
if (isset(
$_GET['sid'])) { $itms->store $_GET['sid']; }

if (
$type!='pid')
{
    if (
$type=='basic')
        
$songs $itms->BasicSearch($term,$attr);
    if (
$type=='advanced')
        
$songs $itms->Search($artist,$album,$song);

    if (!
is_array($songs))
    {
        
error('Your search returned no results');
    }

    
$results=array();

    foreach (
$songs as $key=>$val)
    {
        if (!isset(
$results[$val['playlistId']])) $results[$val['playlistId']] = array();
        
$results[$val['playlistId']][$val['trackNumber']] = $val;
    }

    
$final=array();

    foreach (
$results as $key=>$val)
    {
        
$temp=array();
        foreach (
$val as $key2=>$val2)
        {
            if (
$temp['artist'] != 'Various Artists')
            {
                if (!isset(
$temp['artist'])) { $temp['artist'] = $val2['artistName']; }
                elseif (
$temp['artist'] != $val2['artistName']) { $temp['artist'] = 'Various Artists'; }
                if (!isset(
$temp['store'])) { $temp['store'] = $val2['s']; }
                if (!isset(
$temp['album'])) { $temp['album'] = $val2['playlistName']; }
                if (!isset(
$temp['pid'])) { $temp['pid'] = $key; }
            }
        }
        
array_push($final,$temp);
    }
    foreach (
$final as $key=>$val)
    {
        echo 
$val['artist'].$sep.$val['album'].$sep.$val['pid'].$sep.$val['store'].$brk;
    }
} else {
    
$results=$itms->TrackList($pid,$itms->store);
    
$temp=array();
    foreach (
$results as $key=>$val) {
        if (
$temp['artist'] != 'Various Artists')
        {
            if (!isset(
$temp['artist'])) { $temp['artist'] = $val['artistName']; }
            elseif (
$temp['artist'] != $val['artistName']) { $temp['artist'] = 'Various Artists'; }
            if (!isset(
$temp['album'])) { $temp['album'] = $val['playlistName']; }
        }
        
$disc[$key]  = $val['discNumber'];
        
$track[$key] = $val['trackNumber'];
    }
    
array_multisort($discSORT_ASC$trackSORT_ASC$results);
    
$artwork $itms->GetArt($pid,$itms->store,true);
    echo 
$temp['artist'].$sep.$temp['album'].$sep.$artwork.$sep.str_replace('600x600-100','170x170-99',$artwork).$sep.str_replace('600x600-100','100x100-99',$artwork).$brk;
    foreach (
$results as $key=>$val)
    {
        echo 
$val['discNumber'].$sep.$val['trackNumber'].$sep.$val['artistName'].$sep.$val['songName'].$brk;
    }
}

function 
error($error)
{
    echo 
$error;
    exit;
}

?>