<?

/*		TicketCFC class v1.1 - Chuck Harper
		Based on code originally written for /classes-aeglive/display/TicketURLWrapper.php written by Matt Leonard
		
		This class must be embedded near the top of every page on the site in order to capture the cfc into a
		session variable when the user first lands on the site.
		
		If a CFC is appended to the page url it is stored in a session variable.
		Then anywhere a ticket url is displayed the wrapURL function (used staticly) 
		can encode the url to include the cfc and brand for ticketmaster links.
		If no session stored CFC exists wrapURL uses the default CFC defined in the site's config file.
		
		Version history is at the bottom of the file.
		
		EXAMPLE CODE:
		require_once($_SERVER['DOCUMENT_ROOT'] . '/classes/TicketsCFC.php');
		define('BRAND','aeglive');	// tells ticketmaster to skin the ticket site to match aeglive.com
		define('CFC','AEGLIVE_MAIN');	// this is the default cfc to use if there is no session stored
		$txtTicketBuyLink = TicketsCFC::wrapURL($xml->item->ticket_link,BRAND,CFC);
		
		EXAMPLE URLS:
		http://www.aeglive.com/?cfc=AEGLIVE_BANNERSAMPLE
		http://www.aeglive.com/showdetail.php?id=23670&detail_type=event&cfc=AEGLIVE_BANNERSAMPLE
		
*/

if(!isset($_SESSION)) {
     session_start();
}
// If page had CFC in query string then store in Session variable
if (isset($_REQUEST)) {
	foreach($_REQUEST as $key => $val) {
		if (strtolower($key) == 'cfc') {
			$_SESSION['cfc'] = $val;
			break;
		}
		if (strtolower($key) == 'src') {
			$_SESSION['src'] = $val;
			break;
		}
	}
}

class TicketsCFC {

	private function __construct() {}
	
	public function wrapUrl($URL = '', $brand = 'aeglive', $CFC = null) {
		// verify URL is ticketmaster
		// note that although many ticketmaster links are wrapped the same way, some are not (UK) and
		// many may change in the future as the AEG & Ticketmaster relationship matures internationally
		// so this code looks for all kinds of things so dealing with them will be easier in the future
		// ticketmaster sites found as of 9/30/08:
		/*
		www.ticketmaster.com
		www.ticketmaster.co.uk
		www.ticketmaster.ie
		www.ticketmaster.ca
		www.ticketmaster.com.au
		www.ticketmaster.com.br
		www.ticketmaster.cl
		www.ticketmaster.com.mx
		www.ticketmaster.nl
		www.ticketmaster.co.nz
		www.ticketmaster.no/
		web.ticktackticket.com
		www1.ticnet.se
		www.biletix.com
		www.emma.cn
		www.billetnet.dk
		www.lippupalvelu.fi
		www.kartenhaus.de
		 */
		// we're going to ignore the ones that lack the word "ticketmaster" for now
		//
		// .com is "US style" and the majority of what we'll find - ideally, it should be of the "event" type and wrapped to look like:
		// http://www.ticketmaster.com/cgi/outsider.plx?CAMEFROM=CFC&GOTO=http://www.ticketmaster.com/event/EVENTNUM?brand=aeglive
		// but some can't because they're "artist" or "venue" links or just plain weird
		//
		// .ca should also be wrapped US style
		//
		// .co.uk and .ie are "UK style" and should eventually look like:
		// http://www.ticketmaster.co.uk/event/EVENTNUM?brand=uk_aeg_online&camefrom=CFC_UK_AEGLive_WMAIN000000WEB
		// NOTE: 2/17/09 until skins for ticketmaster.co.uk are made for aeg & gv .co.uk, no brand in database, but there is a CFC for both
		// so for now, link should look like:
		// http://www.ticketmaster.co.uk/event/EVENTNUM?camefrom=CFC_UK_AEGLive_WMAIN000000WEB
		//
		// all other ticketmaster.* sites will just be pushed through because we don't have branding and CFCs working
		// with them yet - but they will be identified so putting in branding/CFC later should be easier
		//
		// here's a sample of some links found in the "link" table (with link_type_id=11) (the code must gracefully handle all of these plus more)
		/*
		http://www.ticketmaster.nl/html/error.htmI?l=NL&err=107
		http://www.ticketmaster.nl/html/evinfo.htmI?CNTX=3724406&l=NL&C=
		http://www.ticketmaster.nl/html/connect.htmI?STAGE=1&C=&l=EN&EVNT=TIN2103
		http://www.ticketmaster.nl/html/evinfo.htmI?CNTX=7058615&l=EN&C=
		http://www.ticketmaster.nl/html/venue.htmI?l=NL&venue=ARENA&sys=107

		http://www.ticketmaster.com.au/event/13003F67BBC15AF4

		http://www.ticketmaster.com/event/0000413087414647
		http://www.ticketmaster.com/event/0C00411B9DA2BE8A?
		http://www.ticketmaster.com/event/09004137DBE4902F=goldenvoice
		http://www.ticketmaster.com/event/0900410DD0B459F8?brand=goldenvoice
		http://www.ticketmaster.com/event/1C004129F23157B9?artistid=768058&majorcatid=10001&minorcatid=3
		http://www.ticketmaster.com/cgi/outsider.plx?CAMEFROM=AEGNDFW_WAPFSDFW110108DFW001&GOTO=http://www.ticketmaster.com/event/0C00411B9DA2BE8A?
		http://www.ticketmaster.com/venue/245900/?search_redirect=bluebird%20theater&tm_link=tm_homeA_header_search
		http://www.ticketmaster.com/venue/73901?brand=goldenvoice
		http://www.ticketmaster.com/WaMu-Theater-tickets-Seattle/venue/123666
		http://www.ticketmaster.com/artist/70506
		http://www.ticketmaster.com/artist/717900/
		http://www.ticketmaster.com/artist/977063/?
		http://www.ticketmaster.com/ACDC-tickets/artist/1170951
		http://www.ticketmaster.com/Warhttp://www.ticketmaster.com/event/0C00410C916E6089

		http://www.ticketmaster.co.uk/event/1800412390532D0C
		http://www.ticketmaster.co.uk/event/1F0040F88F033BCD?brand=uk_aegonline
		http://www.ticketmaster.co.uk/artist/736358
		http://www.ticketmaster.ie/event/18004121CAFD3A13
		http://www.ticketmaster.ie/event/1700406DD98A9DD2?artistid=733217&majorcatid=10001&minorcatid=1
		http://www.ticketmaster.ie/venue/197033

		http://www.ticketmaster.ca/event/10004129D3A47BEE
		http://www.ticketmaster.ca/event/1000407F9E605BD9?artistid=807171&majorcatid=10001&minorcatid=202
		http://www.ticketmaster.ca/artist/734977
		http://www.ticketmaster.ca/event/1100407FDA8E9D19?
		http://www.ticketmaster.ca/event/10004067CD87BF67?brand=goldenvoice
		http://www.ticketmaster.ca/The-Venue-tickets-Hammond/venue/57749
		http://www.ticketmaster.ca/Des-Moines-Civic-Center-tickets-Des-Moines/venue/49258?start=1&rpp=20&searchtype=
		http://www.ticketmaster.ca/Tina-Turner-tickets/artist/1249652

		http://www.ticketmaster.com.au/event/13003F68E7128B6B  	

		http://www.rosequarter.com
		*/

		if (strpos($URL, 'ticketmaster') !== false) {
			// we've got a ticketmaster link
			
			$wrappedUrl = '';
			$wrapLink = '';

			$USStyle = false;
			$UKStyle = false;
			$CAStyle = false;
			$AUStyle = false;
			$BRStyle = false;
			$CLStyle = false;
			$MXStyle = false;
			$MLStyle = false;
			$NZStyle = false;
			$NOStyle = false;
			
			if (isset($_SESSION) && isset($_SESSION['cfc'])) {
				$CFC = $_SESSION['cfc'];
			}
			// the preferred ticketmaster link format is: http://www.ticketmaster.com/event/0000413087414647 
			if (preg_match('/http:\/\/www\.ticketmaster\.com\/event\/[0-9a-zA-Z]{16}/', $URL, $matches)) {
				// UNTIL WE HEAR FROM TICKETMASTER, USING [0-9a-zA-Z], BUT [0-9a-fA-F] (hexadecimal) IS THE MOST LIKELY FORMAT FOR THE STRING, 
				// the link is "ticketmaster.com/event/###" style - give CFC and brand from db
				$wrapLink = $matches[0];

				$USStyle = true;

			} elseif (strpos($URL, '.co.uk') !== false || strpos($URL, 'ticketmaster.ie') !== false) {
				// UK (including Ireland .ie) wrapping is different - deal with below
				$UKStyle = true;

				if ((preg_match('/http:\/\/www.ticketmaster.co.uk\/event\/[0-9a-zA-Z]{16}/', $URL, $matches)) ||
					(preg_match('/http:\/\/www.ticketmaster.ie\/event\/[0-9a-zA-Z]{16}/', $URL, $matches))) {
					// this is the basic "UK event" url
					$wrapLink = $matches[0];
				} elseif (strpos($URL, 'artist') !== false || strpos($URL, 'venue') !== false) {
					// a UK "artist" or "venue" link, push through cleaned up version for UK style wrapping
					$wrapLink = TicketsCFC::cleanUrl($URL);
				} else {
					// don't recognize this beyond UK, just push cleaned up version through
					$wrapLink = TicketsCFC::cleanUrl($URL);
				}

			} elseif (strpos($URL, 'ticketmaster.ca') !== false) {
				// Canada wrapping is currently US style
				$CAStyle = true;

				if (preg_match('/http:\/\/www.ticketmaster.ca\/event\/[0-9a-zA-Z]{16}/', $URL, $matches)) {
					// this is the basic "CA event" url
					$wrapLink = $matches[0];
				} elseif (strpos($URL, 'artist') !== false || strpos($URL, 'venue') !== false) {
					// a CA "artist" or "venue" link, push through cleaned up version for CA style wrapping
					$wrapLink = TicketsCFC::cleanUrl($URL);
				} else {
					// don't recognize this beyond CA, just push cleaned up version through
					$wrapLink = TicketsCFC::cleanUrl($URL);
				}

				// since the CA site isn't in the display table, can't grab the brand out of it
				$brand .= 'aeglive';

			} elseif (strpos($URL, 'ticketmaster.com.au') !== false) {
				// Australia - push it through
				// chop non-alphanumeric characters from the end of the URL
				$wrapLink = TicketsCFC::cleanUrl($URL);

			} elseif (strpos($URL, 'ticketmaster.com.br') !== false) {
				// Brazil - push it through - no wrapping
				// chop non-alphanumeric characters from the end of the URL
				$wrapLink = TicketsCFC::cleanUrl($URL);

			} elseif (strpos($URL, 'ticketmaster.cl') !== false) {
				// Chile - push it through - no wrapping
				// chop non-alphanumeric characters from the end of the URL
				$wrapLink = TicketsCFC::cleanUrl($URL);

			} elseif (strpos($URL, 'ticketmaster.com.mx') !== false) {
				// Mexico - push it through - no wrapping
				// chop non-alphanumeric characters from the end of the URL
				$wrapLink = TicketsCFC::cleanUrl($URL);

			} elseif (strpos($URL, 'ticketmaster.nl') !== false) {
				// Netherlands - push it through - no wrapping
				// chop non-alphanumeric characters from the end of the URL
				$wrapLink = TicketsCFC::cleanUrl($URL);

			} elseif (strpos($URL, 'ticketmaster.co.nz') !== false) {
				// New Zealand - push it through - no wrapping
				// chop non-alphanumeric characters from the end of the URL
				$wrapLink = TicketsCFC::cleanUrl($URL);

			} elseif (strpos($URL, 'ticketmaster.no') !== false) {
				// Norway - push it through - no wrapping
				// chop non-alphanumeric characters from the end of the URL
				$wrapLink = TicketsCFC::cleanUrl($URL);

			} elseif (strpos($URL, 'artist') !== false || strpos($URL, 'venue') !== false) {
				// is it of the "artist" or "venue" persuasion? - this URL possibly broken, but might work
				// at least chop non-alphanumeric characters from the end of the URL
				$wrapLink = TicketsCFC::cleanUrl($URL);

				$USStyle = true;
			} else {
				// don't recognize this, just push it through, but first chop non-alphanumeric characters from the end of the URL
				$wrapLink = TicketsCFC::cleanUrl($URL);

				$USStyle = true;
			} // else

			if ($USStyle) {
				// do the US style
				// final URL looks like:
				// http://www.ticketmaster.com/cgi/outsider.plx?CAMEFROM=CFC&GOTO=http://www.ticketmaster.com/event/040040A177BB18B9?brand=aeglive
				
				$wrappedUrl = "http://www.ticketmaster.com/cgi/outsider.plx?GOTO=$wrapLink";
				if (strlen($CFC) > 0) {
					$wrappedUrl .= "&CAMEFROM=$CFC";
				}
				if (strlen($brand) > 0) {
					$wrappedUrl .= "&brand=$brand";
				}
				if (strlen($wrappedUrl) ==  0) $wrappedUrl = $wrapLink;
			} elseif ($UKStyle) {
				// UK stylee
				// final URL will eventually look like:
				// http://www.ticketmaster.co.uk/event/120040D7993B45AF?brand=uk_aegonline&camefrom=CFC_UK_AEGLive_WMAIN000000WEB
				// but since there's no brand, for now (2/17/09) looks like
				// http://www.ticketmaster.co.uk/event/120040D7993B45AF?camefrom=CFC_UK_AEGLive_WMAIN000000WEB

				// some links contain /search?...&q=string - they need stuff appended with an "&" not a "?"
				$delim = '?';
				if ((strpos($URL, '/search') !== false) && (strpos($URL, '&q=') !== false)) {
					$delim = '&';
				}

				if (strlen($brand) > 0) {
					$wrappedUrl = "$wrapLink" . $delim . "brand=$brand";
					if (strlen($CFC) > 0) {
						$wrappedUrl .= "&CAMEFROM=$CFC";
					}
				} elseif (strlen($CFC) > 0) {
					$wrappedUrl .= "$wrapLink" . $delim . "CAMEFROM=$CFC";
				}
				if (strlen($wrappedUrl) ==  0) $wrappedUrl = $wrapLink;
			} elseif ($CAStyle) {
				// do the US style for Canada (until told otherwise)
				// final URL looks like:
				// http://www.ticketmaster.com/cgi/outsider.plx?CAMEFROM=CFC&GOTO=http://www.ticketmaster.com/event/040040A177BB18B9?brand=aeglive
				
				$wrappedUrl = "http://www.ticketmaster.com/cgi/outsider.plx?GOTO=$wrapLink";
				if (strlen($CFC) > 0) {
					$wrappedUrl .= "&CAMEFROM=$CFC";
				}
				if (strlen($brand) > 0) {
					$wrappedUrl .= "&brand=$brand";
				}
				if (strlen($wrappedUrl) ==  0) $wrappedUrl = $wrapLink;
			} else {
				// just pass the cleaned URL back - no wrapping for now
				return $wrapLink;
			}
			
			return $wrappedUrl;

		} else {
			// this URL doesn't contain "ticketmaster" - currently not wrapping
			return $URL;
		} // else
		
	} // wrapUrl

	public function wrapOutboxURL($URL = '', $brand = 'aeglive', $CFC = null, $skin = 'aeglive', $src = null, $fbShareURL = '') {
		//$txtTicketBuyLink = TicketsCFC::wrapOutboxURL($xml->item->ticket_link,BRAND,CFC,SKIN,SRC,'http://bluebirdtheater.net/eventdetail.php?id=' . $id . '&skin=bluebird');
		$wrappedUrl = '';
		if (strpos($URL, 'axs.com') !== false) {
			$url_array = parse_url($URL);
			// convert query values(if any) into an array
			$url_array['query_array'] = array();
			if (strpos($url_array['query'],'=')) {
				foreach(explode('&', $url_array['query']) as $couple) {
					list($key, $val) = explode('=', $couple);
					$key = strtolower($key);
					if ($key == 'fbshareurl') $key = 'fbShareURL';
					$url_array['query_array'][$key] = $val;
				}
			}
			if (isset($_SESSION) && isset($_SESSION['src'])) {
				$url_array['query_array']['src'] = $_SESSION['src'];
			} else {
				if (!is_null($src)) {
					$url_array['query_array']['src'] = $src;
			}
			}
			if (strlen($skin) > 0) {
				$url_array['query_array']['skin'] = $skin;
			}
			if (strlen($fbShareURL) > 0) {
				if (stristr($fbShareURL,'://')) $fbShareURL = rawurlencode($fbShareURL);
				$url_array['query_array']['fbShareURL'] = $fbShareURL;
			}
			
			if (count($url_array['query_array'] > 0)) {
				$url_array['query'] = http_build_query($url_array['query_array']);
			}
			
			$wrappedUrl   	.= isset($url_array['scheme']) ? $url_array['scheme'] . '://' : '';
			$wrappedUrl     .= isset($url_array['host']) ? $url_array['host'] : '';
			$wrappedUrl     .= isset($url_array['port']) ? ':' . $url_array['port'] : '';
			$wrappedUrl     .= isset($url_array['path']) ? $url_array['path'] : '';
			$wrappedUrl    	.= isset($url_array['query']) ? '?' . $url_array['query'] : '';
			$wrappedUrl 	.= isset($url_array['fragment']) ? '#' . $url_array['fragment'] : '';
			
			return $wrappedUrl;
		} elseif (strpos($URL, 'ticketmaster') !== false) {
			return self::wrapURL($URL, $brand, $CFC);
		} else {
			// this URL doesn't contain "axs.com" or "ticketmaster" - currently not wrapping
			return $URL;
		}
	} // wrapOutboxUrl

	private function cleanUrl($str) {
		// this function is called on URLs that are almost ready for wrapping, just need a bit of cleanup
		
		if (stristr($str,'outsider.plx')) {
			// remove any existing goto wrappers
			$pieces = explode('&',$str);
			foreach($pieces as $key=>$piece) {
				if (stristr($piece,'ticketmaster.com')) {
					$url_pieces = explode('GOTO=',$piece);
					$str = $url_pieces[1];
				}
			}
		} else {			
			// the URL should not have a "brand=XXX" string, so remove it if it's there
			$str = trim(preg_replace('/[&]*brand=[\w]*$/', '', $str));
		}
		// whack trailing non-alphanumerics
		return trim(preg_replace('/\W+$/', '', $str));
	} // cleanUrl

}

/*
	VERSION HISTORY
	1.1 - 	Fixed bug with clearURL using $this-> which doesn't work staticly. 
			Also added code to the clearURL function to strip existing CFC wrapper.

*/

?>
