<?php // backup.php - export your domains
// ------------------------------------------------------------------------
// Copyright (c) 2005, 2006, 2015 Denis Guinnepain
 
// ------------------------------------------------------------------------
// This file is part of Diamentis

// Diamentis is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.

// Diamentis is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with phpBugTracker; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// ------------------------------------------------------------------------

global $what,$export_type,$use_transaction,$asfile,$filename,$allow_recoding,$output_charset_conversion,$charset,$charset_of_file,$compression,$my_key;
$my_key="les carottes sont crues";
$allow_recoding=true;
$what='sql';
$export_type == 'person';
//$use_transaction=true;
$compression= 'none';
$asfile=true;
$filename="domain";
$output_charset_conversion=false;
$PMA_recoding_engine=PMA_CHARSET_ICONV;
//$PMA_recoding_engine=2;//PMA_CHARSET_LIBICONV;
//$PMA_recoding_engine=3;//PMA_CHARSET_RECODE;
$charset='utf-8'; //charset source
//$charset=$STRING['lang_charset']; //charset source
$charset_of_file='iso8859-1';  //charset target


function do_export($project_id = 0) {
        global  $db_cpy,$me, $_pv, $STRING, $t,$crlf,$err_url, $dump_buffer, $asfile;
	$listed_user_id=array();
	$listed_group_id=array();
// define domain table
        $table_project = TBL_PREFIX . "file_prj_id_" . $project_id;
        $str_table_project = 'TBL_PREFIX' . "file_prj_id_" . $project_id;
// define parent domains
	$project_id_list="$project_id";
	$parent=$project_id;
	while ($parent!=PUBLIC_PROJECT_ID)
	{
		$parent=$db_cpy->getOne("SELECT parent_project_id FROM " . TBL_PROJECT . " WHERE project_id=$parent");
		$project_id_list.=", $parent";
	}
	
// define groups in domain
	$rs = $db_cpy->query("select distinct(ug.group_id) from " . TBL_USER_GROUP . " ug, " . TBL_PROJECT_GROUP . " pg where ug.group_id = pg.group_id and pg.project_id=$project_id ");
	while ($rs->fetchInto($row)) 
	{
		$listed_group_id[]=$row['group_id'];
	}
// define users in group domain
	$rs = $db_cpy->query("select distinct(u.user_id), login from " . TBL_AUTH_USER . " u, " . TBL_USER_GROUP . " ug, " . TBL_PROJECT_GROUP . " pg where u.active > 0 and u.user_id = ug.user_id and ug.group_id = pg.group_id and pg.project_id=$project_id order by u.login");
	while ($rs->fetchInto($row)) 
	{
		$listed_user_id[]=$row['user_id'];
	}
// add other users in domain but not in group domain
        if (count($listed_user_id)) 
	{
	    $sql="select distinct(u.user_id), login from " . TBL_AUTH_USER . " u, " . TBL_PROJECT_PERM . " pp where u.user_id not in (".delimit_list(',', $listed_user_id) .") and u.active > 0 and u.user_id = pp.user_id and pp.project_id=$project_id order by u.login";
	}
	else
	{
	    $sql="select distinct(u.user_id), login from " . TBL_AUTH_USER . " u, " . TBL_PROJECT_PERM . " pp where u.active > 0 and u.user_id = pp.user_id and pp.project_id=$project_id order by u.login";
	}
	$rs = $db_cpy->query($sql);
	while ($rs->fetchInto($row)) 
	{
		$listed_user_id[]=$row['user_id'];
	} 
if ($project_id!=PUBLIC_PROJECT_ID){
// add other users in comments
        if (count($listed_user_id)) 
	{
	    $sql="select distinct(c.created_by) from " . TBL_COMMENT . " c where c.created_by not in (0,".delimit_list(',', $listed_user_id) .") ";
	}
	else
	{
	    $sql="select distinct(created_by) from " . TBL_COMMENT. " WHERE created_by<>0";
	}
	$rs = $db_cpy->query($sql);
	while ($rs->fetchInto($row)) 
	{
		$listed_user_id[]=$row['created_by'];
	} 
// add other users in file creation
/* problem at least when no user
 * 
*///    if (count($listed_user_id)) 
// 	{
// 	    $sql="select distinct(tp.created_by) from " . $table_project . " tp where tp.created_by not in ( 0, ".delimit_list(',', $listed_user_id) .") ";
// 	}
// 	else
// 	{
// 	    $sql="select distinct(created_by) from " . $table_project . " WHERE created_by<>0";
// 	}
// 	$rs = $db_cpy->query($sql);
// 	while ($rs->fetchInto($row)) 
// 	{
// 		$listed_user_id[]=$row['created_by'];
// 	} 

// // add other users in file modification
//         if (count($listed_user_id)) 
// 	{
// 	    $sql="select distinct(tp.last_modified_by) from " . $table_project . " tp where tp.last_modified_by not in (0,".delimit_list(',', $listed_user_id) .") ";
// 	}
// 	else
// 	{
// 	    $sql="select distinct(last_modified_by) from " . $table_project . " WHERE last_modified_by<>0";
// 	}
// 	$rs = $db_cpy->query($sql);
// 	while ($rs->fetchInto($row)) 
// 	{
// 		$listed_user_id[]=$row['last_modified_by'];
// 	} 
}
//flat the list
        if (count($listed_user_id)) 
	{
	    $listed_user_id_string=" ".delimit_list(',', $listed_user_id) ." ";
	}
	else
	{
		$listed_user_id_string=" NULL ";
	}
       if (count($listed_group_id)) 
	{
	    $listed_group_id_string=" ".delimit_list(',', $listed_group_id) ." ";
	}
	else
	{
		$listed_group_id_string=" NULL ";
	}



/*	$tab_export=array('assignation'=> array('table'=>'TBL_ASSIGNATION',
						'clean_query'=>"DELETE a.* FROM " . 'TBL_ASSIGNATION' . " a, " . $str_table_project . " tp  WHERE a.file_id=tp.bug_id ;",
						'query'=>"SELECT a.* FROM " . TBL_ASSIGNATION . " a, " . $table_project . " tp  WHERE a.file_id=tp.bug_id ",
						'structure'=>false,
						'sequence'=>false,
						'visibility'=>'only_private'),
			  'attachment'=> array('table'=>'TBL_ATTACHMENT',
						'clean_query'=>"DELETE a.* FROM " . 'TBL_ATTACHMENT' . " a, " . $str_table_project . " tp WHERE a.bug_id=tp.bug_id ;",
						'query'=>"SELECT a.* FROM " . TBL_ATTACHMENT . " a, " . $table_project . " tp WHERE a.bug_id=tp.bug_id ",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"attachment_id",
						'visibility'=>'only_private'),
			  'auth_group'=> array('table'=>'TBL_AUTH_GROUP',
						'clean_query'=>"---group are not erased",
						'query'=>"SELECT ag.* FROM " . TBL_AUTH_GROUP . " ag  WHERE ag.group_id IN  ($listed_group_id_string)",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"group_id"),
			  'auth_user'=> array('table'=>'TBL_AUTH_USER',
						'clean_query'=>"---users are not erased",
						'query'=>"SELECT * FROM " . TBL_AUTH_USER . " au  WHERE au.user_id IN  ($listed_user_id_string)",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"user_id"),
			  'bug_dependency'=> array('table'=>'TBL_BUG_DEPENDENCY',
						'clean_query'=>"DELETE bd.* FROM " . 'TBL_BUG_DEPENDENCY' . " bd, " . $str_table_project . " tp WHERE bd.bug_id=tp.bug_id ;",
						'query'=>"SELECT bd.* FROM " . TBL_BUG_DEPENDENCY . " bd, " . $table_project . " tp WHERE bd.bug_id=tp.bug_id ",
						'structure'=>false,
						'sequence'=>false,
						'visibility'=>'only_private'),
			  'comment'=> array('table'=>'TBL_COMMENT',
						'clean_query'=>"DELETE c.* FROM " . 'TBL_COMMENT' . " c, " . $str_table_project . " tp WHERE c.bug_id=tp.bug_id ;",
						'query'=>"SELECT c.* FROM " . TBL_COMMENT . " c, " . $table_project . " tp WHERE c.bug_id=tp.bug_id ",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"comment_id",
						'visibility'=>'only_private'),
			  'configuration'=> array('table'=>'TBL_CONFIGURATION',
						'clean_query'=>"DELETE c.* FROM " . 'TBL_CONFIGURATION' . " c WHERE c.varname<>\"" . 'INSTALL_URL' . "\";",
						'query'=>"SELECT c.* FROM " . TBL_CONFIGURATION .  " c WHERE c.varname<>\"" . 'INSTALL_URL' . "\"",
						'structure'=>false,
						'sequence'=>false,
						'visibility'=>'only_public'),
			  'contact'=> array('table'=>'TBL_BUG_CC',
						'clean_query'=>"DELETE c.* FROM " . 'TBL_BUG_CC' . " c, " . $str_table_project . " tp WHERE c.bug_id=tp.bug_id ;",
						'query'=>"SELECT c.* FROM " . TBL_BUG_CC . " c, " . $table_project . " tp WHERE c.bug_id=tp.bug_id ",
						'structure'=>false,
						'sequence'=>false,
						'visibility'=>'only_private'),
			  'field_conf'=> array('table'=>'TBL_FIELD_CONF',
						'clean_query'=>"DELETE fc.* FROM " . 'TBL_FIELD_CONF' . " fc WHERE fc.project_id=$project_id ;",
						'query'=>"SELECT fc.* FROM " . TBL_FIELD_CONF . " fc WHERE fc.project_id IN ($project_id_list) ",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"field_conf_id"),
			  'history'=> array('table'=>'TBL_FILE_HISTORY',
						'clean_query'=>"DELETE h.* FROM " . 'TBL_FILE_HISTORY' . " h, " . $str_table_project . " tp WHERE h.bug_id=tp.bug_id ;",
						'query'=>"SELECT h.* FROM " . TBL_FILE_HISTORY . " h, " . $table_project . " tp WHERE h.bug_id=tp.bug_id ",
						'structure'=>false,
						'sequence'=>false,
						'visibility'=>'only_private'),
			  'file_index'=> array('table'=>'TBL_FILE_INDEX',
						'clean_query'=>"DELETE fi.* FROM " . 'TBL_FILE_INDEX' . " fi WHERE fi.project_id=$project_id ;",
						'query'=>"SELECT fi.* FROM " . TBL_FILE_INDEX . " fi WHERE fi.project_id=$project_id ",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"file_id",
						'visibility'=>'only_private'),
			  'table_project_id'=> array('table'=>$table_project,
						'clean_query'=>"DROP TABLE IF EXISTS " . $str_table_project . " ;",
						'query'=>"SELECT * FROM " . $table_project,
						'structure'=>true,
						'sequence'=>false,
						'visibility'=>'only_private'),
			  'project'=> array('table'=>'TBL_PROJECT',
						'clean_query'=>"DELETE * FROM " . 'TBL_PROJECT' . " WHERE project_id=$project_id ;",
						'query'=>"SELECT * FROM " . TBL_PROJECT . " WHERE project_id IN ($project_id_list)",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"project_id"),
			  'project_group'=> array('table'=>'TBL_PROJECT_GROUP',
						'clean_query'=>"DELETE * FROM " . 'TBL_PROJECT_GROUP' . " WHERE project_id=$project_id ;",
						'query'=>"SELECT * FROM " . TBL_PROJECT_GROUP . " WHERE project_id=$project_id",
						'structure'=>false,
						'sequence'=>false),
			  'project_perm'=> array('table'=>'TBL_PROJECT_PERM',
						'clean_query'=>"DELETE * FROM " . 'TBL_PROJECT_PERM' . " WHERE project_id=$project_id ;",
						'query'=>"SELECT * FROM " . TBL_PROJECT_PERM . " WHERE project_id=$project_id",
						'structure'=>false,
						'sequence'=>false),
			  'saved_query'=> array('table'=>'TBL_SAVED_QUERY',
						'clean_query'=>"---no erase for queries",
						'query'=>"SELECT * FROM " . TBL_SAVED_QUERY . " sq  WHERE sq.user_id IN  ($listed_user_id_string)",
						'structure'=>false,
						'sequence'=>false),
			  'selectbox_value'=> array('table'=>'TBL_SELECTBOX_VALUE',
						'clean_query'=>"DELETE sbv.* FROM " . 'TBL_FIELD_CONF' . " fc, " . 'TBL_SELECTBOX_VALUE' ."  sbv WHERE sbv.field_conf_id=fc.field_conf_id and fc.project_id=$project_id ",
						'query'=>"SELECT sbv.* FROM " . TBL_FIELD_CONF . " fc, " . TBL_SELECTBOX_VALUE ."  sbv WHERE sbv.field_conf_id=fc.field_conf_id and fc.project_id IN ($project_id_list) ",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"sv_id"),
			  'user_group'=> array('table'=>'TBL_USER_GROUP',
						'clean_query'=>"-- user_groups remain the same",
						'query'=>"SELECT * FROM " . TBL_USER_GROUP . " WHERE group_id IN ($listed_group_id_string)",
						'structure'=>false,
						'sequence'=>false),
			  'user_pref'=> array('table'=>'TBL_USER_PREF',
						'clean_query'=>"-- user_pref remain the same",
						'query'=>"SELECT * FROM " . TBL_USER_PREF . " up  WHERE up.user_id IN  ($listed_user_id_string)",
						'structure'=>false,
						'sequence'=>false)
);*/

	$tab_export=array('field_conf'=> array('table'=>'TBL_FIELD_CONF',
					'query'=>"SELECT fc.* FROM " . TBL_FIELD_CONF . " fc WHERE fc.project_id IN ($project_id_list) ",
					'structure'=>false,
					'sequence'=>false,
					'key'=>"field_conf_id"),
			  'selectbox_value'=> array('table'=>'TBL_SELECTBOX_VALUE',
						'query'=>"SELECT sbv.* FROM " . TBL_FIELD_CONF . " fc, " . TBL_SELECTBOX_VALUE ."  sbv WHERE sbv.field_conf_id=fc.field_conf_id and fc.project_id IN ($project_id_list) ",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"sv_id"),
			  'project'=> array('table'=>'TBL_PROJECT',
						'query'=>"SELECT * FROM " . TBL_PROJECT . " WHERE project_id IN ($project_id_list)",
						'structure'=>false,
						'sequence'=>true,
						'key'=>"project_id"),
			  'table_project_id'=> array('table'=>$table_project,
						'query'=>"SELECT * FROM " . $table_project . " where 1=2",
						'structure'=>true,
						'sequence'=>false,
						'visibility'=>'only_private'
						)
					);
	
	
	
//	echo "\nSET CHARACTER SET utf-8; \n"; not functionnal
error_reporting(-1);
	foreach ($tab_export as $k => $v)
	{	
		if (($v['visibility']=='only_private')&&($project_id==PUBLIC_PROJECT_ID)) continue;
		if (isset($v['visibility']) &&($v['visibility']=='only_public')&&($project_id!=PUBLIC_PROJECT_ID)) continue;
	/*	if (isset($asfile)and $asfile==true)
		{
			$dump_buffer.= "\n". $v['clean_query'] . "\n";
		}
		else
		{
			echo  "\n". $v['clean_query'] . "\n";
		}
		*/	
		if ($v['structure'])
		{
		        if (!PMA_exportStructure(DB_DATABASE, $v['table'], $crlf, $err_url, $do_relation=false, $do_comments=false, $do_mime=false, $do_dates=false))
		            break 3;			
		}
	        if (!PMA_exportData(DB_DATABASE, $v['table'], $crlf, $err_url, $v['query']))
	            break 3;
		if ($v['sequence'])
		{
			if (isset($asfile)and $asfile==true)
			{
				$dump_buffer.= "\nUPDATE " . $v['table'] . "_seq tbs SET tbs.id = (SELECT MAX(tb." . $v['key'] . ") FROM " . $v['table'] . " tb );  \n";
			}
			else
			{
				echo  "\nUPDATE " . $v['table'] . "_seq tbs SET tbs.id = (SELECT MAX(tb." . $v['key'] . ") FROM " . $v['table'] . " tb );  \n";
			}
		}
	} 

//        $local_query  = 'SELECT * FROM ' . $table;
//        if (!PMA_exportStructure(DB_DATABASE, $table, $crlf, $err_url, $do_relation=false, $do_comments=false, $do_mime=false, $do_dates=false))
//            break 3;
//        if (!PMA_exportData($current_db=NULL, $table, $crlf, $err_url, $local_query))
//            break 3;
//	echo "le project_id vaut ". $project_id;

//	$t->wrap('admin/sitelist.html', 'site');
}  //end of function do_export


$DiaBackup="fee";
?>
