<?php 

	/* DO NOT EDIT THIS FILE */
	
	// Render XML into a string
	
	// Open XML tag and gallery tag
	$out = '<?xml version="1.0" encoding="utf-8"?>';
	$out .= "\n<!-- XML Generated by SlideShowPro Director v" . DIR_VERSION . " http://www.slideshowpro.net -->";
	$out .= "\n<gallery";
	
	if (!empty($gallery)) {
		$out .= ' title="' . $director->encodeForXML($gallery['Gallery']['name']) . '" description="' . $director->encodeForXML($gallery['Gallery']['description']) . '"';
	}
	
	$out .= ">";

	if (empty($albums)):
		$out .= "\n" . '<!-- You have not activated any albums yet -->';
	else:
		foreach($albums as $album):
			$cache_path = 'xml' . DS . 'images_album_' . $album['Album']['id'] . $cache_tail . '.cache';
			$cache = cache($cache_path, null, '+1 month');
			if (empty($cache)) {
				$a_out = '';
				// Formulate audio portion of album tag if necessary
				$audio_str = '';
				if (!empty($album['Album']['audioFile']) && $album['Album']['audioFile'] != '0' && $album['Album']['audioFile'] != 'No audio for this album') {
					$audio_str = ' audio="' . $data_host . '/album-audio/' . $album['Album']['audioFile'] . '"';
					if (!empty($album['Album']['audioCap'])) {
						$audio_str .= ' audioCaption="' . $director->encodeForXML($album['Album']['audioCap']) . '"';
					}
				}
			
				// Open album tag
				$a_out .= "\n\t" . '<album';
				$a_out .= ' id="' . 				'album-' . $album['Album']['id'] . '"';
				$a_out .= ' title="' . 			$director->encodeForXML($album['Album']['name']) . '"';
				$a_out .= ' description="' . 		$director->encodeForXML($album['Album']['description'], true) . '"';
				$a_out .= ' lgPath="' .			$data_host . '/p.php?a="';
				$a_out .= ' tnPath="' .			$data_host . '/p.php?a="';
				if (!empty($album['Album']['aTn'])) {
					list($tn_src, $tn_id, $x, $y) = explode(':', $album['Album']['aTn']);
					$tn = __p(array('src' => $tn_src,
					 				'album_id' => $tn_id,
					 				'width' => $specs[10],
					 				'height' => $specs[11],
					 				'square' => $specs[12], 
									'quality' => $specs[8],
									'sharpening' => $specs[9],
									'anchor_x' => $x,
									'anchor_y' => $y));
					if ($relative) {
						$tn = DIR_REL_HOST . str_replace(DIR_HOST, '', $tn);
					}
					$a_out .= ' tn="' . $tn . '"';
				}
				$a_out .= 						$audio_str;
				$a_out .= '>';
		
				// The structure of the images array is a bit different
				// between dynamic galleries and the main feed.
				if ($album['Album']['smart']):
					list($conditions, $order, $limit) = $controller->Gallery->Tag->Album->smartConditions(unserialize($album['Album']['smart_query']));
					$images = $controller->Gallery->Tag->Album->Image->find('all', array('conditions' => $conditions, 'limit' => $limit, 'order' => $order));
					elseif (!isset($album['Image'])):
						if (isset($album['Album']['Image'])) {
							$images = $album['Album']['Image'];
						} else {
							$images = $controller->Gallery->Tag->Album->Image->find('all', array('conditions' => array('aid' => $album['Album']['id']), 'order' => 'seq ASC', 'recursive' => -1));
						}
					else:
					$images = $album['Image'];
				endif;
			
				if (empty($images)):
					$a_out .= "\n\t\t" . '<!-- You have not added any images to this album yet -->';
				else:				
					// Loop through images and create tags
					foreach($images as $image):
						if ($album['Album']['smart']) {
							$original = $image['Album'];
							$image = $image['Image'];
						} else {
							$original = null;
							if (isset($image['Image'])) {
								$image = $image['Image'];
							}
						}
						
						if (is_numeric($album['Album']['watermark_id']) && $album['Album']['watermark_id'] > 0) {
							$watermark_array = array(	'watermark_id' => $album['Album']['watermark_id'],
														'watermark_location' => $watermarks[$album['Album']['watermark_id']]['position'],
														'watermark_opacity' => $watermarks[$album['Album']['watermark_id']]['opacity']);
						} else {
							$watermark_array = array();
						}
					
						if ($image['active']):
							$src = $image['src'];
							if (isNotImg($src)):
								$lg = $data_host . '/' . ALBUM_DIR . '/album-' . $image['aid'] . '/lg/' . $src;
								$not_img = true;
								$bit = substr($src, 0, strrpos($src, '.'));
								if ($image['tn_preview_id'] > 0) {
									$vid_tn_str = $image['tn_preview'];
								} else {
									$vid_tn_str = $image['lg_preview'];
								}
								$thumb = $director->getVidThumb($src, $vid_tn_str, $image['aid'], $specs[5], $specs[6], $specs[7], $specs[8], $specs[9]);
								$vid_preview = $director->getVidThumb($src, $image['lg_preview'], $image['aid'], $specs[0], $specs[1], $specs[2], $specs[3], $specs[4]);
								if ($relative) {
									$vid_preview = DIR_REL_HOST . str_replace(DIR_HOST, '', $vid_preview);
								}
								if (isset($specs[13])):
									$tnsm = $director->getVidThumb($src, $vid_tn_str, $image['aid'], $specs[13], $specs[14], 1, $specs[8], $specs[9]);
									$tnsm = str_replace(DIR_HOST . '/p.php?a=', '', $tnsm);
								endif;
								$tn = str_replace(DIR_HOST . '/p.php?a=', '', $thumb);	
							else:
								$arr = unserialize($image['anchor']);
								$filepath = ALBUMS . DS . 'album-' . $image['aid'] . DS . 'lg' . DS . $src;
								list($w, $h) = getimagesize($filepath);
								if (empty($arr)) {
									$arr['x'] = $arr['y'] = 50;
								}
								$pre_lg_array = array(  'src' => $image['src'],
										 				'album_id' => $image['aid'],
										 				'width' => $specs[0],
										 				'height' => $specs[1],
										 				'square' => $specs[2], 
														'quality' => $specs[3],
														'sharpening' => $specs[4],
														'anchor_x' => $arr['x'],
														'anchor_y' => $arr['y']);
								
								$pre_lg_array = array_merge($pre_lg_array, $watermark_array);

								$lg = str_replace(DIR_HOST . '/p.php?a=', '', __p($pre_lg_array));
								$tn = str_replace(DIR_HOST . '/p.php?a=', '', __p(array('src' => $image['src'],
																						'album_id' => $image['aid'],
																						'width' => $specs[5],
																						'height' => $specs[6],
																						'square' => $specs[7], 
																						'quality' => $specs[8],
																						'sharpening' => $specs[9],
																						'anchor_x' => $arr['x'],
																						'anchor_y' => $arr['y'])));
								if (isset($specs[13])):
									$tnsm = str_replace(DIR_HOST . '/p.php?a=', '', __p(array(  'src' => $image['src'],
																								'album_id' => $image['aid'],
																								'width' => $specs[13],
																								'height' => $specs[14],
																								'quality' => $specs[8],
																								'sharpening' => $specs[9],
																								'anchor_x' => $arr['x'],
																								'anchor_y' => $arr['y'])));
								endif;
							endif;
							// Write tag
							$a_out .= "\n\t\t" . '<img';
							$a_out .= ' id="content-' . $image['id'] . '" src="' . $lg . '" file="' . $src . '"'; 
							if (!empty($tn)):		
								$a_out .= ' tn="' . $tn . '"';
							endif;
							if (isset($tnsm)):		
								$a_out .= ' tnsm="' . $tnsm . '"';
								unset($tnsm);
							endif;
							if (isset($vid_preview)):
								$a_out .= ' vidpreview="' .	$vid_preview . '"';
								unset($vid_preview);
							endif;
							if (isset($w)) {
								$a_out .= ' dims="' . $w . ',' . $h . '" fp="' . $arr['x'] . ',' . $arr['y'] . '"';
							}
							if (!empty($album['Album']['title_template'])) {
								if ($album['Album']['smart'] || empty($image['title'])) {
									$image['title'] = $controller->Director->formTitle($image, $album['Album'], $original);
								}
							}
							$a_out .= !empty($image['title']) ? ' title="' . 	$director->encodeForXML($image['title']) . '"' : '';
							if (!empty($album['Album']['caption_template'])) {
								if ($album['Album']['smart'] || empty($image['caption'])) {
									$image['caption'] = $controller->Director->formCaption($image, $album['Album'], $original);
								}
							}
							$a_out .= !empty($image['caption']) ? ' caption="' . 	$director->encodeForXML($image['caption'], true) . '"' : '';
							if (!empty($album['Album']['link_template'])) {
								if ($album['Album']['smart'] || empty($image['link'])) {
									list($image['link'], $image['target']) = $controller->Director->formLink($image, $album['Album'], $original, $watermark_array);
								}
							}
							if (!empty($image['link'])) {
								$link = $director->encodeForXML($image['link']);
								if ($relative) {
									if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
										$protocol = 'https://';
									} else {
										$protocol = 'http://';
									}
									$link = str_replace($protocol . env('HTTP_HOST'), '', $link);
								}
								$a_out .= ' link="' . $link . '"';
							}
						
							switch((int) $image['target']) {
								case 0:
									$t = '_blank';
									break;
								case 1:
									$t = '_self';
									break;
								case 2:
									$t = '_parent';
									break;
							}
							$a_out .= ' target="' . $t . '"';
							$a_out .= empty($image['pause']) ? '' : " pause=\"{$image['pause']}\"";
							$a_out .= (empty($image['tags']) ? '' : ' tags="' . $director->encodeForXML($image['tags']) . '"') . ' />';
						endif;
					endforeach;
				endif;
		
			// Close album tag
			$a_out .= "\n\t" . '</album>';
			$out .= $a_out;
			cache($cache_path, $a_out);
		} else {
			$out .= '<!-- CACHED -->';
			$out .= $cache;
		}
		endforeach;
	endif;

	// Close gallery tag
	$out .= "\n</gallery>";

	// Try to cache the output to xml_cache
	if (@$controller->Director->setPerms(XML_CACHE)):
		$mask = umask(0);
		$handle = @fopen($path_to_cache, 'w+');
		@fwrite($handle, $out) == false;
		@fclose($handle);
		umask($mask);  
	endif;
	
	// Send to the client
	header('Content-type: text/xml;');
	die($out);
?>