<?php

//
// The following block of code that sets a few global variables should be included 
// in every client facing file. Files that should not be called directly should abort
// if $globalsSet is false.
//
// This file should be included in every dsn based nft. It sets the root directory
// path for both including php files and javascript files.
//
// The idea is what we want an 'theRootDir' that can be used by require_once when
// building locations to where source files exist.
//
if( !isset($globalsSet)) {
// For php, if $theRootDir doesn't exist, we create it.
if( !isset($theRootDir) ) {
	if( $_SERVER["SERVER_NAME"] == "localhost") {
		$theRootDir = $_SERVER["DOCUMENT_ROOT"].'/'.'amorstyle';
	} else {
		$theRootDir = $_SERVER["DOCUMENT_ROOT"];
	}
	//echo "base: theRootDir: ".$theRootDir."<br>";
}
// For javascript:
if( !isset($theRootURL) ) {
	$theRootURL = $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["HTTP_HOST"];
	if( $_SERVER["HTTP_HOST"] == 'localhost') {
		$theRootURL = $theRootURL.'/amorstyle';
	}
	//echo "base: theRootURL: ".$theRootURL."<br>";
}
// for project files
if( !isset($theRequestDirURL) ) {
	$theRequestDirURL = $theRootURL.pathinfo($_SERVER["PHP_SELF"],PATHINFO_DIRNAME);
	//echo 'theRequestDirURL: '.$theRequestDirURL.'<br>';
}
$globalsSet = true;
}

if( !isset($globalMobile)) {
// Are we on a mobile system or desktop pc?
$globalMobile = false;
if( isset($_SERVER['HTTP_USER_AGENT']) ) {
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'android')) {
	$globalMobile = true;
} 
}
//$globalMobile = true;
}
// for testing
if( isset($_GET['display']) && 'mobile' == $_GET['display'])
{
$globalMobile = true;
}


// our local metadata file.
require_once($theRootDir.'/nfts/drmaudiov1/common.php');

require_once($theRootDir.'/dsn/url.php');
require_once($theRootDir.'/dsn/theta.php');
require_once($theRootDir.'/dsn/dsncommon.php');
require_once($theRootDir. '/dsn/dsn.php');



class CCard_Metadata {

	private $cd; // common data class

	function __construct($_c_url) {
		$this->cd = new CCardData($_c_url);
	}


	// This function builds all the stuff that is common for all tokens in the collection.
	function main($_c_url) {

		$md['error']=false; // if we hit an error, errorMsg will have a hint.

		if( $_c_url->exists('p') ) {

			if( 1 == $_c_url->value('p')) {
				$md=$this->cd->project();
			} else if( 'hash' == $_c_url->value('p')) {
				$md=$this->cd->hashMD(true);
			} else if( 'hashval' == $_c_url->value('p')) {
				$data=$this->cd->hashMD(true);
				$md['URI']=$data['siteURL'];
				$md['Project']=$data['siteName'];
				$str=json_encode($data);
				$md['hash']= hash('sha1',$str);;
			}

		} else if( $_c_url->exists('t') ) {
			$md=$this->cd->token($_c_url->value('t'));

		} else if( $_c_url->exists('series') && $_c_url->exists('spot') ) {

			$md=$this->cd->seriesSpot($_c_url->value('series'),$_c_url->value('spot'));
		}

		return $md;
	}
}

//
// This code will be called like t=N. It is going to build the metadata needed
// to represent the token. 
//
function run_the_code() {

	$c_url = new C_URL($_GET);
	$common = new CHelp_Common();
	$card = new CCard_Metadata($c_url);
	
	$arrData=array();
	if($common->needHelp($c_url)) {
		$arrData = $common->help($c_url);
	} else {
		$arrData = $card->main($c_url);
	}

	if( null != $arrData ) {
		$output = json_encode($arrData,JSON_UNESCAPED_SLASHES);
		echo $output;
	}
}
run_the_code();