<?php
/*

___Canvas XML Functions_________________________________________

Imports function name and variable definitions from an XML file.
Contains the XML and directory scanning functions.

________________________________________________________________

*/


function canvas_get_plugin($filename, $modTitle) {
	global $plugin, $current_function, $current_variable, $current_tag, $current_option;
	if (!($xmlparser = xml_parser_create())) echo("ERROR: Cannot create parser for ".$filename);
	xml_set_element_handler($xmlparser, "startElement", "endElement");

	$current_function = trim($modTitle);
	$plugin = new importedFunction();
	$plugin->functionName = $current_function;

	xml_set_character_data_handler($xmlparser, "canvas_import_XML_content");

	if (!($fp = fopen($filename, "r"))) echo("ERROR: Can't open ".$filename);
	while ($data = fread($fp, 4096)) {
		$data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
        $data=eregi_replace(">"."[[:space:]]+",">",$data);
        $data=eregi_replace("[[:space:]]+"."<","<",$data);
		
		if (!xml_parse($xmlparser, $data, feof($fp))) {
			echo(sprintf("XML error: %s at line %d of".$filename, 
				xml_error_string(xml_get_error_code($xml_parser)), 
				xml_get_current_line_number($xml_parser)));
		}
	}
	xml_parser_free($xmlparser);
	
	return $plugin;
}

function canvas_import_XML_content($parser, $data) {
	global $plugin, $current_function, $current_variable, $current_option, $current_tag;
	
	$functionNameKey = "^CANVAS^FUNCTION^NAME";
	$functionUBIKey = "^CANVAS^FUNCTION^UBI";
	$functionPathKey = "^CANVAS^FUNCTION^PATH";
	$functionAuthorKey = "^CANVAS^FUNCTION^AUTHOR";
	$functionUriKey = "^CANVAS^FUNCTION^URI";
	$functionDescKey = "^CANVAS^FUNCTION^DESCRIPTION";
	$functionGroupKey = "^CANVAS^FUNCTION^GROUP";
	$functionNumvarsKey = "^CANVAS^FUNCTION^NUMVARS";
	$variablesKey = "^CANVAS^FUNCTION^VARIABLES";
	$variableKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE";
	$variableNameKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^NAME";
	$variableDescriptionKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^DESCRIPTION";
	$variableTypeKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^TYPE";
	$variableDirectoryKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^DIRECTORY";
	$variableDefaultKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^DEFAULT";
    	$variableNumoptsKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^NUMOPTIONS";
	$optionsKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^OPTIONS";
	$optionKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^OPTIONS^OPTION";
    	$optionTextKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^OPTIONS^OPTION^DISPLAYTEXT";
    	$optionValueKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^OPTIONS^OPTION^VALUE";
	$optionParamsKey = "^CANVAS^FUNCTION^VARIABLES^VARIABLE^OPTIONS^OPTION^PARAMS";	

	if($current_tag == $functionNameKey) {
		$current_function = trim($data);
		$plugin = new importedFunction();
		$plugin->functionName = $current_function;
	} elseif($current_tag == $functionUBIKey) {
		$plugin->functionUBI = trim($data);
	} elseif($current_tag == $functionPathKey) {
		$plugin->functionPath = trim($data);
	} elseif($current_tag == $functionAuthorKey) {
		$plugin->functionAuthor = trim($data);
	} elseif($current_tag == $functionUriKey) {
		$plugin->functionUri = trim($data);
	} elseif($current_tag == $functionDescKey) {
		$plugin->functionDesc = trim($data);
	} elseif($current_tag == $functionGroupKey) {
		if(trim($data) == '') $data = 'Other';
		$plugin->functionGroup = $data;
	} elseif($current_tag == $variableNameKey) {
		$current_variable = trim($data);
		$plugin->variables[$current_variable]['name'] = $current_variable;
	} elseif($current_tag == $variableDescriptionKey) {
		$plugin->variables[$current_variable]['description'] = trim($data);
	} elseif($current_tag == $variableTypeKey) {
		$plugin->variables[$current_variable]['type'] = trim($data);
	} elseif($current_tag == $variableDirectoryKey) {
		$plugin->variables[$current_variable]['option']['gallery']['name'] = 'Image Directory URI';
		$plugin->variables[$current_variable]['option']['gallery']['value'] = trim($data);
	} elseif($current_tag == $variableDefaultKey) {
		$plugin->variables[$current_variable]['default'] = trim($data);
	} elseif($current_tag == $optionTextKey) {
        	$current_option = trim($data);
	    	$plugin->variables[$current_variable]['option'][$current_option]['name'] = $current_option;
    	} elseif($current_tag == $optionValueKey) {
	    $plugin->variables[$current_variable]['option'][$current_option]['value'] = trim($data);
    	} elseif($current_tag == $optionParamsKey) {
	    $plugin->variables[$current_variable]['option'][$current_option]['params'] = trim($data);
    	}


}

function canvas_import_layout_XML($parser, $data) {
	global $imported_theme, $imported_blocks, $imported_variables, $id, $variable, $current_tag;
	
	$themeTitle = "^CANVAS^EXPORT^META^TITLE";
	$themeAuthor = "^CANVAS^EXPORT^META^AUTHOR";
	$themeURI = "^CANVAS^EXPORT^META^URI";
	$theme = "^CANVAS^EXPORT^META^THEME";
	$blockName = "^CANVAS^EXPORT^LAYOUT^BLOCK^NAME";
	$blockType = "^CANVAS^EXPORT^LAYOUT^BLOCK^TYPE";
	$blockZone = "^CANVAS^EXPORT^LAYOUT^BLOCK^ZONE";
	$blockPosition = "^CANVAS^EXPORT^LAYOUT^BLOCK^POSITION";
	$blockAuthor = "^CANVAS^EXPORT^LAYOUT^BLOCK^AUTHOR";
	$blockDesc = "^CANVAS^EXPORT^LAYOUT^BLOCK^DESCRIPTION";
	$blockGroup = "^CANVAS^EXPORT^LAYOUT^BLOCK^GROUP";
	$blockURI = "^CANVAS^EXPORT^LAYOUT^BLOCK^URI";
	$blockPath = "^CANVAS^EXPORT^LAYOUT^BLOCK^PATH";
	$blockUBI = "^CANVAS^EXPORT^LAYOUT^BLOCK^UBI";
	$blockID = "^CANVAS^EXPORT^LAYOUT^BLOCK^ID";
	$variableName = "^CANVAS^EXPORT^LAYOUT^BLOCK^VARIABLES^VARIABLE^NAME";
	$variableValue = "^CANVAS^EXPORT^LAYOUT^BLOCK^VARIABLES^VARIABLE^VALUE";

	if($current_tag == $theme) {
		$imported_theme['theme'] = trim($data);
	} elseif($current_tag == $themeTitle) {
		$imported_theme['title'] = trim($data);
	} elseif($current_tag == $themeAuthor) {
		$imported_theme['author'] = trim($data);
	} elseif($current_tag == $themeURI) {
		$imported_theme['uri'] = trim($data);
	} elseif($current_tag == $blockID) {
		$id = trim($data);
	} elseif($current_tag == $blockUBI) {
		$imported_blocks[$id]['ubi'] = trim($data);
	} elseif($current_tag == $blockName) {
		$imported_blocks[$id]['name'] = trim($data);
	} elseif($current_tag == $blockType) {
		$imported_blocks[$id]['type'] = trim($data);
	} elseif($current_tag == $blockZone) {
		$imported_blocks[$id]['zone'] = trim($data);
	} elseif($current_tag == $blockPosition) {
		$imported_blocks[$id]['position'] = trim($data);
	} elseif($current_tag == $blockAuthor) {
		$imported_blocks[$id]['author'] = trim($data);
	} elseif($current_tag == $blockDesc) {
		$imported_blocks[$id]['description'] = trim($data);
	} elseif($current_tag == $blockGroup) {
		$imported_blocks[$id]['group'] = trim($data);
	} elseif($current_tag == $blockURI) {
		$imported_blocks[$id]['uri'] = trim($data);
	} elseif($current_tag == $blockPath) {
		$imported_blocks[$id]['path'] = trim($data);
	} elseif($current_tag == $variableName) {
		$variable = trim($data);
		$imported_variables[$id][$variable]['name'] = trim($data);
	} elseif($current_tag == $variableValue) {
		$imported_variables[$id][$variable]['value'] = trim($data);
	}
}

function endElement($parser, $name) {
	global $current_tag;
	$caret_pos = strrpos($current_tag,'^');
	$current_tag = substr($current_tag,0,$caret_pos);
}

function startElement($parser, $name, $attrs) {
	global $current_tag;
	$current_tag .= "^$name";
}

// Scans the directory (included for PHP 4 users)
if(!function_exists(scandirectory)) {
	function scandirectory($dir = './', $sort = 0) {
		$dir_open = @opendir($dir);
		if (!$dir_open) return false;
		while(($dir_content = readdir($dir_open)) !== false) {
			$files[] = $dir_content;
		}
		if ($sort == 1) rsort($files, SORT_STRING);
		else sort($files, SORT_STRING);
		return $files;
	}
}

?>