<?php
/* Ajout d'une boite dans le gabarit */
function add_function_template($file,$div,$chaine_add){
	$chaine_add=trim($chaine_add);
	if (eregi("^/.*",trim($chaine_add))) $chaine_add=substr($chaine_add,1,strlen($chaine_add));
	$chaine_add = (!eregi("inc/",$chaine_add)) ? "\n<?php include('include/box/".$chaine_add."'); ?>" : "\n<?php include('".$chaine_add."'); ?>" ;
	print $chaine_add."<br />";
	 $h=fopen($file, "r+");
	 $provisoire=tempnam(getcwd(),"PRO");
	$pr=fopen($provisoire, "w");
	if (!$pr)die ("pbs ouverture fichier");
	while(! feof($h)){
		$l = fgets($h,250);
		if (eregi("^(.*)(<div id=.(".$div.").>)(.*)$",$l, $reg))  {
			fwrite ($pr, $reg[1].$reg[2].$chaine_add.$reg[4]);
		} else {
		fwrite ($pr, "$l");
		}
	}
	fclose($h);
	fclose($pr);
	rename ($provisoire, $file);
	chmod ($file, 0666);
	print "<p class='ok'>".UPDATE_OK."</p>";
}
/* supp d'une boite dans le gabarit */
function supp_function_template($file,$chaine_supp){
	$h=fopen($file, "r+");
	$provisoire=tempnam(getcwd(),"PRO");
	$pr=fopen($provisoire, "w");
	if (!$pr)return "pbs ouverture fichier";
	while(! feof($h)){
		$l = fgets($h,4096);
		if (eregi("^(.*)(<\?php(.*".$chaine_supp.".*)\?>)(.*)$",$l, $reg))  {
			fwrite ($pr, $reg[1].$reg[4]."");
			$ok=true;
		} else {
		fwrite ($pr, "$l");
		}
	}
	fclose($h);
	fclose($pr);
	if ($ok ==true){
	rename ($provisoire, $file);
	chmod ($file, 0666);
	print "<p class='ok'>".UPDATE_OK."</p>";
	} else print "<p class='erreur'>".UPDATE_NOT_OK."</p>";

}
/* creat new file template */
function new_gab_copy($name, $new_name){
	if (file_exists(trim($name))) {
		print copy (trim($name), ADD_PATH_ADMIN."template/".NOM_DU_THEME."/".trim($new_name).".php");
		print "<p class='ok'>".UPDATE_OK."</p>";
	} else print "<p class='erreur'>".ERREUR."</p>";
}
/* creat new file box */
function new_box_copy($name, $new_name, $rep){
	if (file_exists(trim($name))) {
		$cp= trim($rep).trim($new_name).'.inc';
		print copy (trim($name), ADD_PATH_ADMIN.$cp);
		print "<p class='ok'>".UPDATE_OK."</p>";
	} else print "<p class='erreur'>".ERREUR."</p>";
}
// construction de l'appel ajax inline avec confirmation js Et suppression de l'id appelant
function ajax_droit_confirme($txt_confirm,$lib_dest, $funct_dest,$var_dest,$dest_ret,$txt_ret,$id){
$format_ajax_inline="upload_ajax('%1\$s','%2\$s','%3\$s','%4\$s');";

$s = 	"{ if (confirm(' ".addslashes($txt_confirm)." ')){ "
	.sprintf($format_ajax_inline,"gauche_content",$lib_dest,$funct_dest, $var_dest).";"
	."Element.remove('".$id."');"
	."return false; "
	." } "
	." else  return false;"
	." } ";
return $s;

}
// function destinée à lire le contenu des box en commentaire detaillant le contenu
function detail_box($file){
	$raw=file_get_contents($file);
	if ($raw){
		if (eregi("/[*](.*)[*]/",$raw,$rawitems)){
			return addslashes(eregi_replace("(/|[*]|>|<|\n|\r|\n\r)","",$rawitems[1]));
		}
	}
}
/* affichage de sortie pour les  boite dispo*/
function box_dispo_sortie($js){
	print '<div class="r" style="width:190px">';
	$rep_function = array();
	$nom=NewFct('cstr_liste',ADD_PATH_ADMIN."template/".NOM_DU_THEME."/inc/",'inc',true) ;
	$rep_function=array_merge($rep_function,$nom);
	while ($row =each($rep_function) ){
		$dir= eregi_replace(ADD_PATH_ADMIN."template/".NOM_DU_THEME,'',$row['value']);
		if (!@is_dir($row['value'])) {
			print '<p id="add_'.$dir.'"  class="draggable" '.Toolip(detail_box(ADD_PATH_ADMIN."template/".NOM_DU_THEME."".$dir)).'>'.$dir.'</p>';
			array_push($js, "new Draggable('add_".$dir."',{revert:true});");
		}
	}
	print '</div>';
	print '<div class="l"  style="width:190px">';
	$rep_function = array();
	$nom=NewFct('liste_Dir',ADD_PATH_ADMIN."include/box/");
	$rep_function=array_merge($rep_function,$nom);
	while ($row =each($rep_function) ){
		$dir=eregi_replace(ADD_PATH_ADMIN."include/box/",'',$row['value']);
		if (!is_dir($row['value'])) {
			print '<p id="add_'.$dir.'"  class="draggable" '.Toolip( detail_box(ADD_PATH_ADMIN."include/box/".$dir)).'>'.$dir.'</p>';
			array_push($js, "new Draggable('add_".$dir."',{revert:true});");
		}
	}
	print '</div>';
	return $js;
}
/* Analyse des information plugin/theme */
function xmlParser($attr,$content,&$res,$active_only=true){
	# V�rification du nom
	if (preg_match('/name="(.+)"/msU',$attr,$name))
	{
		# Nom
		$res['name'] = trim($name[1]);


		# Version
		if (preg_match('/version="(.*)"/msU',$attr,$version)) {
			$res['version'] = trim($version[1]);
		}

		# Auteur
		if (preg_match('/<author>(.+)<\/author>/msU',$content,$author)) {
			$res['author'] = trim($author[1]);
		}

    # Label
		if (preg_match('/<label>(.+)<\/label>/msU',$content,$label)) {
			$res['label'] = trim($label[1]);
		}

		# Description
		if (preg_match('/<desc>(.+)<\/desc>/msU',$content,$description)) {
			$res['desc'] = trim($description[1]);
		}

	}
	return $res;
}
## functio d'affiachge du block du choix et catalog de theme
function choise_template(){
	$img=(file_exists(ADD_PATH_ADMIN.'template/'.NOM_DU_THEME.'/screenshot.png')) ? URL_DU_SITE.'template/'.NOM_DU_THEME.'/screenshot.png' : URL_ADMIN.'template/'.TEMPLATE_ADMIN.'/img/screenshot_defaut.jpg';
	print '<h2>'.CHOIX_THEME.'</h2><br />'
		.'<h3>'.THEME_ACTUEL.'</h3>'
		.'<div id="theme_selected">'
		.'<img src="'.$img.'" />';
		$raw=file_get_contents(ADD_PATH_ADMIN.REP_TEMPLATE.NOM_DU_THEME.'/desc.xml');
		 $root="theme";
		if (preg_match('/<'.$root.'(.*)>(.*)<\/'.$root.'>/msU',$raw,$matches)){
			$e=xmlParser($matches[1],$matches[2],@$tmp[@$entry],true);
			  print '<ul>'
				.'<li>'.THEME.' <strong>'.$e['name'].'</strong> '.THEME_VERSION.' '.$e['version'].'</li>'
				.'<li>'.BY.' <strong>'.$e['author'].' </strong> '.LABEL.' '.$e['label'].'</li>'
				.'<li>'.$e['desc'].'</li>'
				.'</ul>';
			}
		print '</div><br /><br /><br />';

	print  '<div id="theme_select">'
		.'<h3>'.THEME_SELECT.'</h3>';
	$nom=NewFct('liste_Dir',ADD_PATH_ADMIN.REP_TEMPLATE);
	while ($y=each($nom)){
		if ( ($raw=@file_get_contents($y['value'].'/desc.xml') ) ==false) verbose(ERROR_OPEN_DESC_TEMP,'error');
		 $root="theme";
		if (preg_match('/<'.$root.'(.*)>(.*)<\/'.$root.'>/msU',$raw,$matches)){
			$e=xmlParser($matches[1],$matches[2],@$tmp[@$entry],true);
			}
			$img=(file_exists(ADD_PATH_ADMIN.'template/'.eregi_replace("(.*/)*",'',$y['value']).'/screenshot.png')) ? URL_DU_SITE.'template/'.eregi_replace("(.*/)*",'',$y['value']).'/screenshot.png' : URL_ADMIN.'template/'.TEMPLATE_ADMIN.'/img/screenshot_defaut.jpg';
			print '<ul>'
				.'<li><a href="#" onclick="'.ajax_action ('include/lib/lib.edit_template.php', 'up_new_theme',"'".$e['name']."'",'edit_template.php').'">'
					.'<img src="'.$img.'" />'
					.'<p>'.$e['name'].' '.$e['version'].'</p>'
				.'</li></a>'
			.'</ul>';
	}
	print	'<br style="clear:both;" /></div>';
}

/* Boite ajax */
/* affiche formulaire pour dupliquer un gabarit */
function dble_gabarit($files,$file_conf){
?>
	<div id='new_gab' style='display:none;'>
	<p id='new_gab_help' style=""><?php print BOX_NEW_GAB_HELP_D ?></p>
	<?php print tep_form ('edit_template','','gab=new',"post","POST",false,"  Onsubmit=\"  ".ajax_droit_confirm(TITRE_CONFIRM,"include/lib/lib.edit_template.php", "new_gab_copy"," \' ' + document.getElementById('gab').value + ' \',\' '+  document.getElementById('new_gabarit').value +' \'" ,"edit_template.php?file=".$files,RETOUR)."; \"" ); ?>
		Nom :<input id='new_gabarit' type='txt' name='new_gabarit' />
		<nobr>copy de : <select id='gab' name='gab'>
			<?php while($l=each($file_conf)){
			      eregi(".*/([a-z0-9_-]*)(\.[a-z0-9]{3})",$l['value'],$r);
			      if (!empty($r[1])) print "<option value='".$l['value']."'>".$r[1]."</option>";
			      $r='';
			      } ?></select></nobr>
		<input type='submit' value='<?php echo SUBMIT_AJOUTER; ?>' name='add_gab' />
	</form><hr /><br />
	</div>
<?php
}
/* affiche formulaire pour dupliquer un box */
function dble_box($files,$file_conf){
?>
	<div id='new_box' style='display:none;'>
	<p id='new_box_help' style=""><?php print BOX_NEW_BOX_HELP_D ?></p>
	<?php print tep_form ('edit_template','','box=new',"post","POST",false,"  Onsubmit=\"  ".ajax_droit_confirm(TITRE_CONFIRM,"include/lib/lib.edit_template.php", "new_box_copy"," \' ' + document.getElementById('box').value + ' \',\' '+  document.getElementById('new_box_name').value +' \',\'template/".NOM_DU_THEME."/inc/\'" ,"edit_template.php?file=".$files,RETOUR)."; \"" ); ?>
		Nom :<input id='new_box_name' type='txt' name='new_box_name' />
		<nobr>copy de : <select id='box' name='box'>
			<?php while($l=each($file_conf)){
			      eregi(".*/([a-z0-9_-]*)(\.[a-z0-9]{3})",$l['value'],$r);
			      if (!empty($r[1])) print "<option value='".$l['value']."'>".$r[1]."</option>";
			      $r='';
			      } ?></select></nobr>
		<input type='submit' value='<?php echo SUBMIT_AJOUTER; ?>' name='add_box' />
	</form><hr /><br />
	</div>
<?php
}

## modif du them  dans le sys config. verif presence rep
function up_new_theme($nom_du_theme){
	if ( (file_exists(ADD_PATH_ADMIN.REP_TEMPLATE.$nom_du_theme))AND(is_dir(ADD_PATH_ADMIN.REP_TEMPLATE.$nom_du_theme)) ){
		NewFct('up_var_table' , "nom_du_theme",$nom_du_theme, ADD_PATH_ADMIN.TBL_SYS_CONFIG_INC,"_PX_config");
		print '<p class="ok" style="width:80%;">'.TX_CHARG_NEW_TH.'</p> <br /><p style="text-align:center;width:80%;"><a href="edit_template.php">'.NEXT.'</a></p>';
	} else print '<p class="erreur" style="width:80%; height">'.TX_ERR_CHARG_NEW_TH.'</p>';
}
?>