<?php
	if(isset($_GET["username"])){
		$getUserName = $_GET["username"];
		
		$apoDir = dirname(__FILE__);
		$apoFile = basename(__FILE__);
		require $apoDir."/config/config.php";
		require $apoDir."/modules/bookmarks/functions.inc";
		
		// Instanciation de Apo
		$apo = new Apo($apoDir, $apoFile, $connApo);
		
		// Configuration
		$apo->config = $apoDir."/config/config.ini";
		
		// Recherche du userId
		$apo->db->select($apo->dbPrefix."users", "id", "name='".$getUserName."'");
		$getId = $apo->db->fetch_single();
		if($getId){
			// On vérifie si le XML existe
			if(!file_exists($apoDir."/modules/bookmarks/datas/user".$getId.".xml")){
			
				// Récupération des favoris de l'utilisateur
				$apo->db->select($apo->dbPrefix."bookmarks", "*", "userId=".$getId, null, null, array("name"=>"ASC"));
				$listFolders = array();
				$listLinks = array();
				while($row = $connApo->fetch_array()){
					if($row["type"] == "folder"){
						$listFolders[] = $row;
					}else{
						$listLinks[] = $row;
					}
				}
				
				// Il n'existe pas, donc on le crée
				$xmlContent = bookmarks_generateXml($listFolders, $listLinks);
				
				$handle = fopen($apoDir."/modules/bookmarks/datas/user".$getId.".xml", 'w+');
				fwrite ($handle, $xmlContent);
				fclose($handle);
			}
			// Récupère les préférences de l'utilisateur
			$apo->userPreferences = $apo->getPreferences($getId);
			
			// Génération de l'affichage
			$content = bookmarks_parseXML($apoDir."/modules/bookmarks/datas/user".$getId.".xml");
		}else{
			// On ne trouve rien dans la liste des users
			// On cherche maintenant dans la liste des groupes
			$apo->db->select($apo->dbPrefix."groups", "id", "name='".$getUserName."'");
			$getId = $apo->db->fetch_single();
			if($getId){
				// On vérifie si le XML existe
				if(!file_exists($apoDir."/modules/bookmarks/datas/group".$getId.".xml")){
				
					// Récupération des favoris de l'utilisateur
					$apo->db->select($apo->dbPrefix."bookmarks_groups", "*", "groupId=".$getId, null, null, array("name"=>"ASC"));
					$listFolders = array();
					$listLinks = array();
					while($row = $connApo->fetch_array()){
						if($row["type"] == "folder"){
							$listFolders[] = $row;
						}else{
							$listLinks[] = $row;
						}
					}
					
					// Il n'existe pas, donc on le crée
					$xmlContent = bookmarks_generateXml($listFolders, $listLinks);
					
					$handle = fopen($apoDir."/modules/bookmarks/datas/group".$getId.".xml", 'w+');
					fwrite ($handle, $xmlContent);
					fclose($handle);
				}
				// Génération de l'affichage
				$content = bookmarks_parseXML($apoDir."/modules/bookmarks/datas/group".$getId.".xml");
			}else{
				$content = "Mauvais identifiant.";
			}
		}
	}else{
		$content = "<p>ERREUR</p>";
	}
	
	// Envoi des en-tête HTTP
	$accept_xml = !empty($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') !== false;
	if ($accept_xml) {
		header('Content-Type: application/xhtml+xml');
	} else {
		header('Content-Type: text/html; charset=UTF-8');
	}
	
	// Affichage du HTML
	if ($accept_xml) {
		echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $apo->lang; ?>" lang="<?php echo $apo->lang; ?>">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<link rel="stylesheet" type="text/css" href="<?php echo $apo->webRoot; ?>/themes/<?php echo $apo->theme; ?>/style.css" />
		<title><?php echo $apo->getText("menu_bookmarks"); ?></title>
		<script type="text/javascript" src="<?php echo $apo->webRoot; ?>/js/functions.js"></script>
	</head>
	<body>
		<?php echo $apo->error; ?>
		<div id="content" class="bookmarks"><h1><?php echo $apo->getText("menu_bookmarks"); ?> &gt; <?php echo $getUserName; ?></h1></div>
		<?php echo $content; ?>
	</body>
</html>