} else {
        $collection = get_collection($ref);
        if ($collection === false) {
            exit("Collection not found.");
        }
    }
}
#Check if sharing allowed
if (!$allow_share) {
    $show_error = true;
    $error = $lang["error-permissiondenied"];
}
$user_select_internal = checkperm("noex");
#Check if any resources are not in the active state
foreach ($refArray as $colref) {
    if (!$collection_allow_not_approved_share && !is_collection_approved(trim($colref))) {
        $show_error = true;
        $error = $lang["notapprovedsharecollection"];
    }
}
# Get min access to this collection
foreach ($refArray as $colref) {
    $minaccess = collection_min_access(trim($colref));
}
if ($minaccess >= 1 && !$restricted_share) {
    $show_error = true;
    $error = $lang["restrictedsharecollection"];
}
if (isset($show_error)) {
    ?>
    <script type="text/javascript">
			
			<?php			
			}
		else
			{
			# Access has been selected. Generate a URL.			
			?>
			<p><?php echo $lang["generatethemeurlsexternal"]?></p>
			<p>
			<textarea class="URLDisplay" cols="100" rows="<?php echo count($collectionstoshare)*4+1; ?>" ><?php
			$unapproved_collection=false; 
			foreach($collectionstoshare as $collection){	
				$ref=$collection["ref"];
				
				#Check if any resources are not approved
				if (!is_collection_approved($ref) && !$collection_allow_not_approved_share) {
					echo str_replace("%collectionname%", i18n_get_collection_name($collection), $lang["collection-name"]) . "\r\n" . $lang["notapprovedsharecollection"] . "\r\n\r\n";
					$unapproved_collection=true;
				} else {
					echo str_replace("%collectionname%", i18n_get_collection_name($collection), $lang["collection-name"]) . "\r\n" . $baseurl?>/?c=<?php echo urlencode($ref)?>&k=<?php echo generate_collection_access_key($ref,0,"URL",$access,$expires) . "\r\n" . ($expires!="" ? str_replace("%date%", $expires, $lang["expires-date"]) : str_replace("%date%", $lang["never"], $lang["expires-date"])) . "\r\n\r\n";
				}
			}
			?>
			</textarea>
			<?php if ($unapproved_collection){?><script>alert('<?php echo $lang['notapprovedsharetheme']?>');</script><?php } ?>
			</p>
			<?php
			}
		}
	//Display existing shares for collections in theme
if (!collection_readable($ref)) {exit($lang["no_access_to_collection"]);}

#Check if sharing allowed
if (checkperm("b") || !$allow_share) {
        $show_error=true;
        $error=$lang["error-permissiondenied"];
        }

# Check if editing existing external share
$editaccess=getvalescaped("editaccess","");
($editaccess=="")?$editing=false:$editing=true;

$editexternalurl = (getval("editexternalurl","")!="");
	
#Check if any resources are not active
$collectionstates=is_collection_approved($ref);
if (!$collection_allow_not_approved_share && $collectionstates==false) {
        $show_error=true;
        $error=$lang["notapprovedsharecollection"];
        }
	
if(is_array($collectionstates) && (count($collectionstates)>1 || !in_array(0,$collectionstates)))
	{
	$warningtext=$lang["collection_share_status_warning"];
	foreach($collectionstates as $collectionstate)
		{
		$warningtext.="<br>" . $lang["status" . $collectionstate];
		}
	}

# Get min access to this collection