<?php
/* +----------------------------------------------------------------------+
| Netautor Professional Application Server |
+----------------------------------------------------------------------+
| Copyright (C) 1998-2005 digiconcept GmbH. <www.digiconcept.net> |
+----------------------------------------------------------------------+
| This file is subject to license, that is bundled with this package |
| in the file LICENSE.TXT, and is available at through the |
| world-wide-web at http://www.netautor.com/license/ |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| hide@address.com so we can mail you a copy. |
+----------------------------------------------------------------------+
| Authors: Stefan Rottensteiner <hide@address.com> |
| Marek Kadziela <hide@address.com> |
| Gregor Wollner |
| Christian Unger |
| Helli Kleinhans |
+----------------------------------------------------------------------+*/
/**
* Executes export-request
*
* @version $Revision: 1.18 $
*/
require_once('../../include/init.inc');
if(!$USER->check_feature('Admin') && !$USER->check_feature('ac_export'))
{
$USER->login_call();
}
if(empty($appl_selection)) $appl_selection = 'MyApplication';
// --- include necessary functions
includeNaPro( 'array,file,xml' );
$LOCALE->textdomain('exchange');
if (!isset($layout_selection)) $layout_selection = array();
if (!isset($db_selection)) $db_selection = array();
if (!isset($appl_selection)) $appl_selection = array();
if (!isset($doc_selection)) $doc_selection = '';
if (!isset($doc_operator)) $doc_operator = 'IN';
if (!isset($doc_replace)) $doc_replace = '';
$include_dbelements = (empty($include_dbelements) ? '' : $include_dbelements);
$include_layouts = (empty($include_layouts) ? '' : $include_layouts);
$include_documents = (empty($include_documents) ? '' : $include_documents);
$include_users = (empty($include_users) ? '' : $include_users);
$include_groups = (empty($include_groups) ? '' : $include_groups);
if (!empty($button_save) && !empty($include_documents))
{
if(!empty($doc_selection))
{
$doc_selection = explode(',',$doc_selection);
$doc_selection = array_unique($doc_selection);
$temp_doc_selection = array();
foreach($doc_selection as $idx => $val)
{
$val = trim($val);
if(empty($val))
{
unset($doc_selection[ $idx ]);
}
else
{
$temp_doc_selection[] = $val;
}
}
$doc_selection = $temp_doc_selection;
if(count($doc_selection) == 0) $doc_selection = '';
}
if(!empty($doc_replace))
{
$doc_replace = array_unique($doc_replace);
$temp_doc_replace = array();
foreach($doc_replace as $idx => $val)
{
$val = trim($val);
$temp_array = explode(',',$val);
if(count($temp_array) == 3)
{
$replace_from = '<'.$temp_array[0].'>'.$temp_array[1].'</'.$temp_array[0].'>';
$replace_to = '<'.$temp_array[0].'>'.$temp_array[2].'</'.$temp_array[0].'>';
$temp_doc_replace['mdoc_content']['like'][ $replace_from ] = $replace_to;
}
}
$doc_replace = $temp_doc_replace;
if(count($doc_replace) == 0) $doc_replace = '';
}
}
function createSelection(&$DBList,&$LayoutList,$selected_dbs)
{
$LayoutList = array();
$sql = $GLOBALS[ 'sql_world' ];
$erg = $sql->select('SELECT mn_name, mn_naid FROM mnames ORDER BY mn_name',0,0,2);
if (!empty($erg))
{
$max_erg = count($erg);
for ($i=0;$i<$max_erg;$i++)
{
$DBList[$i]['name']= $erg[$i]['mn_name'];
$DBList[$i]['id'] =$erg[$i]['mn_naid'];
}
}
@set_time_limit(0);
if (!empty($selected_dbs))
{
$leer=',';
$leer2='';
$query = 'SELECT mlay_name,mlay_id FROM mlayouts WHERE mlay_mn_naid IN('.deepImplode($selected_dbs,$leer,$leer2).') ORDER BY mlay_name';
$erg = $sql->select($query,0,0,2);
if (!empty($erg))
{
@set_time_limit(0);
$max_erg= count($erg);
for ($i=0;$i<$max_erg;$i++)
{
$LayoutList[$i]['name'] =$erg[$i]['mlay_name'];
$LayoutList[$i]['id'] =$erg[$i]['mlay_id'];
}
}
}
}
// Writes the header
function writeHeader($file,$Meta,$Dependency="")
{
$x ='';
$Meta['VERSION'] ='2.0';
$Meta['TIME'] =Date('Y-m-d H:i:s');
$Meta['TABLEDEF']['dummy'] ='';
XML_fromArray($Meta,$x,'METADATA',false,2);
$y=' ';
if(!empty($Dependency) && is_array($Dependency))
{
for ($i=count($Dependency)-1;$i>=0;$i--)
{
$yy=' ';
XML_fromArray($Dependency[$i],$yy,'DEPENDENCY',false,4);
$y.=$yy;
}
}
$start = strpos($x,'<dummy></dummy>');
$end = $start+strlen('<dummy></dummy>');
$x = "<XML_MAGIC>\n".substr($x,0,$start).$y.substr($x,$end)."\n";
return fwrite($file,$x,strlen($x));
}
// Replace values in export array
function replaceContent(&$value,$ReplaceValue)
{
if(!empty($ReplaceValue) && count($ReplaceValue) > 0)
{
foreach($value as $idx => $val)
{
foreach($val as $val_key => $val_val)
{
if(!empty($ReplaceValue[ $val_key ]['exact']) && !empty($ReplaceValue[ $val_key ]['exact']['from'][ $val_val ]))
{
$value[ $idx ][ $val_key ] = $ReplaceValue[ $val_key ]['exact']['from'][ $val_val ];
}
if(!empty($ReplaceValue[ $val_key ]['like']))
{
$value[ $idx ][ $val_key ] = strtr($value[ $idx ][ $val_key ],$ReplaceValue[ $val_key ]['like']);
}
}
}
}
}
// Writes the array as XML-File
function writeContent($FileName,&$value,$ContentName,$Meta='',$Dependency='',$ReplaceValue='')
{
if(!empty($ReplaceValue))
{
replaceContent($value,$ReplaceValue);
}
$file=fopen($FileName,'wb');
if (!$file) return false;
if (is_array($Meta)) writeHeader($file,$Meta,$Dependency);
for ($index=count($value)-1;$index>=0;$index--)
{
@set_time_limit(15);
$result='';
XML_fromArray(clearArray($value[$index]),$result,$ContentName,false,2);
fwrite($file,$result,strlen($result));
}
fwrite($file,'</XML_MAGIC>',strlen('</XML_MAGIC>'));
fclose($file);
return ($index<0?count($value):$index);
}
// exports a table
function export_multiple(&$sql_world,$FilePath,$selection,$Meta,$Dependency,$Table,$ID_Name,$Foreign_ID_Name="",$Add_ID_Name="",$Add_Selection="",$Add_Operator="",$ReplaceValue="")
{
$leer1=',';
$leer2='';
$where_add = '';
$order_add = '';
if(!empty($Add_Selection) && !empty($Add_ID_Name))
{
switch($Add_Operator)
{
case'IN':
default:
$where_add = " AND $Add_ID_Name IN (".deepImplode($Add_Selection,$leer1,$leer2).") ";
$order_add = " , $Add_ID_Name ASC ";
break;
case'NOT IN':
$where_add = " AND $Add_ID_Name NOT IN (".deepImplode($Add_Selection,$leer1,$leer2).") ";
$order_add = " , $Add_ID_Name ASC ";
break;
case'BETWEEN':
$where_add = " AND $Add_ID_Name BETWEEN {$Add_Selection[0]} AND {$Add_Selection[1]} ";
$order_add = " , $Add_ID_Name ASC ";
break;
case'NOT BETWEEN':
$where_add = " AND $Add_ID_Name NOT BETWEEN {$Add_Selection[0]} AND {$Add_Selection[1]} ";
$order_add = " , $Add_ID_Name ASC ";
break;
}
}
$query = "SELECT * FROM $Table ".( empty($selection) ? '' : ' WHERE '.$ID_Name.' IN ('.deepImplode($selection,$leer1,$leer2).')').$where_add.' ORDER BY '.( $Foreign_ID_Name != '' ? $Foreign_ID_Name : $ID_Name ).' ASC'.$order_add;
$values= $sql_world->select($query,0,0,2);
//speziel für die me_settings
if($Table == 'melements')
{
$ids='';
for($i=0;$i<count($values);$i++)
{
$ids.=','.$values[$i]['me_id'];
}
$ids=substr($ids,1);
//alle me_settings auslesen
$sub_values = $sql_world->select("SELECT * FROM me_settings WHERE mes_me_id IN ($ids) ORDER BY mes_me_id",0,0,2);
if(!empty($sub_values))
{
//die settings durchgehen und nach me_id zusammenfassen und den elementen zuordnen
for($i=0;$i<count($sub_values);$i++)
{
$me_settings[ $sub_values[$i]['mes_me_id'] ]['NR'.$i]=$sub_values[$i];
}
for($i=0;$i<count($values);$i++)
{
if(isset($me_settings[ $values[$i]['me_id'] ] ))
{
$values[$i]['me_settings']=$me_settings[ $values[$i]['me_id'] ];
}
}
}
}
if( $sql_world->errnr>0 ) return false;
if( empty($values) ) return 0;
if( $Foreign_ID_Name == '' ) $Foreign_ID_Name = $ID_Name;
$old_one=$values[count($values)-1][$Foreign_ID_Name];
$index=0;
for ($index=count($values)-1;$index>=-1;$index--)
{
set_time_limit(15);
if( $index==-1 || $old_one != $values[$index][$Foreign_ID_Name] )
{
$Meta['TABLEDEF']['NAME']=$Table;
$Meta['TABLEDEF']['ROWS']=count($toBeWritten);
if (!writeContent($FilePath.$Table.'_'.$old_one.'.xml',$toBeWritten,'ROW',$Meta,$Dependency,$ReplaceValue))
{
return count($values)-$index;
}
unset($toBeWritten);
$old_one=($index!=-1 ? $values[$index][$Foreign_ID_Name] : '');
}
$toBeWritten[]=(empty($values[$index])? '' :$values[$index]);
}
$bringback = ( $index <= -1 ? count($values) : count($valus)-$index );
return $bringback; // // Variable zurücksenden
}
function export_single(&$sql_world,$FilePath,$selection,$Meta,$Dependency,$Table,$ID_Name)
{
$leer1 =',';
$leer2 ='';
$values = $sql_world->select("SELECT * FROM $Table ".(empty($selection)?'':" WHERE $ID_Name IN (".deepImplode($selection,$leer1,$leer2).")")." ORDER BY $ID_Name ASC",0,0);
$Meta['TABLEDEF']['NAME']=$Table;
$Meta['TABLEDEF']['ROWS']=count($values);
return writeContent($FilePath.$Table.'.xml',$values,'ROW',$Meta,$Dependency,$ReplaceValue);
}
/**
* Die gewünschten Zeile speichern
*/
function save_appl($appl_selection,$db_selection,$layout_selection,$doc_selection,$doc_operator,$doc_replace,$server_path,$inc_documents,$inc_dbelements,$inc_dbs,$inc_layouts,$inc_users,$inc_groups)
{
global $sql_world;
$Start_Time=time();
$DC_ENV = $GLOBALS['DC_ENV'];
$Meta['APPLICATION']=$appl_selection;
$filecontent='';
// Slashes eventuell korrigieren und $storage zusammenbauen
$server_path=str_replace("\\",'/',$server_path);
// $storage= str_replace("//", "/", ((!strpos($server_path,":") ? $DC_ENV->installpath."/../export/":"").$server_path."/"));
// $storage= str_replace("//", "/", ( ( !strpos($server_path,":") ? $DC_ENV->installpath."/../export/":"").$server_path."/".$Meta['APPLICATION']."/"));
$storage = $server_path.$Meta['APPLICATION']."/";
if ((!is_dir($storage)) && (!File_MkDirs($storage)))
{
$error_report[]=$GLOBALS['LOCALE']->gettext('msg_invalid_target_dir',array($storage));
return $error_report;
}
$error_report[]=$GLOBALS['LOCALE']->gettext('msg_valid_target_dir',array($storage));
//------------------------------------------------------------------- Schemata exportieren
if(($inc_dbs) && (!empty($db_selection)))
{
$Meta['TABLEDEF']['PRIMARY']='mn_id';
$count=export_single($sql_world,$storage,$db_selection,$Meta,'','mnames','mn_naid');
$error_report[] = ( $count>0 ? $GLOBALS['LOCALE']->gettext('msg_exported_table',array($count)) : $GLOBALS['LOCALE']->gettext('msg_exported_table_err') );
}
//------------------------------------------------------------------- Elemente exportieren
if(($inc_dbelements) && (!empty($db_selection)))
{
$Meta['TABLEDEF']['PRIMARY']='me_id';
$Depend[0]['TABLE' ] = 'melements';
$Depend[0]['FROM' ] = 'me_me_id';
$Depend[0]['TO' ] = 'me_id';
$Depend[1]['TABLE' ] = 'mnames';
$Depend[1]['FROM' ] = 'me_mn_naid';
$Depend[1]['TO' ] = 'mn_naid';
$count=export_multiple($sql_world,$storage,$db_selection,$Meta,$Depend,"melements","me_mn_naid","","","","");
$error_report[] = ( $count>0 ? $GLOBALS['LOCALE']->gettext('msg_exported_element',array($count)) : $GLOBALS['LOCALE']->gettext('msg_exported_element_err') );
unset($Depend);
}
//------------------------------------------------------------------- Layouts exportieren
if(($inc_layouts) && (!empty($layout_selection)))
{
$Meta['TABLEDEF']['PRIMARY']='mlay_id';
$Depend[0]['TABLE'] ='mnames';
$Depend[0]['FROM'] ='mlay_mn_id';
$Depend[0]['TO'] ='mn_id';
$count=export_multiple($sql_world,$storage,$layout_selection,$Meta,$Depend,'mlayouts','mlay_id','mlay_mn_naid','','','','');
$error_report[] = ( $count>0 ? $GLOBALS['LOCALE']->gettext('msg_exported_layout',array($count)) : $GLOBALS['LOCALE']->gettext('msg_exported_layout_err') );
unset($Depend);
}
//------------------------------------------------------------------- Dokumente exportieren
if(($inc_documents)&& (!empty($db_selection)))
{
$Add_ID_Name = (!empty($doc_selection) && is_array($doc_selection) && count($doc_selection) > 0 ? 'mdoc_id' : '');
$Add_Selection = (!empty($doc_selection) && is_array($doc_selection) && count($doc_selection) > 0 ? $doc_selection : '');
$ReplaceValue = (!empty($doc_replace) && is_array($doc_replace) && count($doc_replace) > 0 ? $doc_replace : '');
$Add_Operator = (!empty($doc_operator) ? $doc_operator : 'IN');
$Meta['TABLEDEF']['PRIMARY']='mdoc_id';
$count=export_multiple($sql_world,$storage,$db_selection,$Meta,'','mdocs','mdoc_mn_naid','mdoc_mn_naid',$Add_ID_Name,$Add_Selection,$Add_Operator,$ReplaceValue);
$error_report[] = ( $count>0 ? $GLOBALS['LOCALE']->gettext('msg_exported_document',array($count)) : $GLOBALS['LOCALE']->gettext('msg_exported_document_err') );
}
//------------------------------------------------------------------- Gruppen exportieren
if($inc_users )
{
$selection = array();
$result = $sql_world->select('SELECT user_id FROM users WHERE user_group=0',0,0,SQL_FETCH_ALPHA);
if (!empty($result)) foreach ($result as $index => $data) $selection[]= $data['user_id'];
$Meta['TABLEDEF']['PRIMARY']='user_id';
$count = export_single($sql_world,$storage,$selection,$Meta,'','users','user_id');
$error_report[] = ( $count>0 ? $GLOBALS['LOCALE']->gettext('msg_exported_user',array($count)) : $GLOBALS['LOCALE']->gettext('msg_exported_user_err') );
}
//------------------------------------------------------------------- Benutzer exportieren
if($inc_groups )
{
$selection = array();
$result = $sql_world->select('SELECT user_id FROM users WHERE user_group=1',0,0,SQL_FETCH_ALPHA);
if (!empty($result)) foreach ($result as $index => $data) $selection[]= $data['user_id'];
$Meta['TABLEDEF']['PRIMARY']='user_id';
$count = export_single($sql_world,$storage.'group_',$selection,$Meta,'','users','user_id');
$error_report[] = ( $count>0 ? $GLOBALS['LOCALE']->gettext('msg_exported_group',array($count)) : $GLOBALS['LOCALE']->gettext('msg_exported_group_err') );
}
//------------------------------------------------------------------- Features exportieren
if($inc_groups || $inc_users)
{
$id_column = 'fto_id';
$selection = array(); // ID's der Werte
$feature_id = array(); // ID's der verwendeten Features
if ($inc_groups && $inc_users)
{
// Keine weitere Einschränkung wenn Benutzer UND Gruppen exportiert werden
$query_add = '';
}
else
{ // Einschränkung auf Benutzer oder Gruppen - je nach Bedarf
$query_add = ( $inc_groups ? ' AND user_group=1' : ' AND user_group=0' );
}
$result = $sql_world->select('SELECT '.$id_column.',fto_ft_id FROM fto,users WHERE fto_user_id=user_id'.$query_add,0,0,SQL_FETCH_ALPHA);
if (!empty($result)) foreach ($result as $index => $data)
{
if (!empty($data[ $id_column ]))
{
$selection[$data[ $id_column ] ] = $data[ $id_column ];
$feature_id[ $data[ 'fto_ft_id' ] ] = $data[ 'fto_ft_id' ];
}
}
if (count($selection)==0)
{
$error_report[] = $GLOBALS['LOCALE']->gettext('msg_exported_fto_empty');
}
else
{
$Meta['TABLEDEF']['PRIMARY']= $id_column ;
$count = export_single($sql_world,$storage,$selection,$Meta,'','fto',$id_column);
$error_report[] = ( $count>0 ? $GLOBALS['LOCALE']->gettext('msg_exported_fto',array($count)) : $GLOBALS['LOCALE']->gettext('msg_exported_fto_err') );
$Meta['TABLEDEF']['PRIMARY']= 'ft_id' ;
$count = export_single($sql_world,$storage,array(),$Meta,'','feature','ft_id');
//$error_report[] = ( $count>0 ? $GLOBALS['LOCALE']->gettext('msg_exported_feature',array($count)) : $GLOBALS['LOCALE']->gettext('msg_exported_feature_err') );
}
}
//------------------------------------------------------------------- Benutzer-Gruppen-Zuordnung exportieren
// ---- Automatischer Export wenn Bentuzer UND Gruppen exportiert werden
if($inc_groups && $inc_users)
{
$id_column = 'gto_id';
$selection = array();
$result = $sql_world->select('SELECT '.$id_column.' FROM gto',0,0,SQL_FETCH_ALPHA);
if (!empty($result)) foreach ($result as $index => $data) if (!empty($data[ $id_column ])) $selection[]= $data[ $id_column ];
if (count($selection)==0)
{
$error_report[] = $GLOBALS['LOCALE']->gettext('msg_exported_gto_empty');
}
else
{
$Meta['TABLEDEF']['PRIMARY']= $id_column ;
$count = export_single($sql_world,$storage,$selection,$Meta,'','gto',$id_column);
$error_report[] = ( $count>0 ? $GLOBALS['LOCALE']->gettext('msg_exported_gto',array($count)) : $GLOBALS['LOCALE']->gettext('msg_exported_gto_err') );
}
}
$error_report[]= $GLOBALS['LOCALE']->gettext('msg_export_time',array( (time()-$Start_Time) )) ;
return $error_report;
}
/**
* Die gewünschten Zeile als Download anbieten.
*/
function send_appl()
{
$error_report[] ="Function 'Send to client' not yet implemented !";
return $error_report;
}
function is_in($Value,$SearchArray)
{
if(empty($SearchArray) || empty($Value)) return false;
for($index=count($SearchArray)-1;$index>=0;$index--) {if($Value==$SearchArray[$index]) return true;}
return false;
}
if(!empty($button_save))
{ print_r();
$error_report=save_appl($appl_selection,$db_selection,$layout_selection,$doc_selection,$doc_operator,$doc_replace,$destinationpath,$include_documents,$include_dbelements,$include_dbs,$include_layouts,$include_users,$include_groups);
}
if (!empty($button_send))
{
$error_report=send_appl();
}
if(!empty($appl_selection))
{
if (empty($db_selection)) $db_selection="";
createSelection($DBList,$LayoutList,$db_selection);
}
if(empty($destinationpath))
{
$destinationpath= $DC_ENV->startpath.'export/';
}
if(empty($msk)) $msk = 'appl_export';
include ($msk.'.msk');
?>