<?php
ini_set('session.use_cookies','0');
session_start();
$appl_names = array();
// KEINE abschliessenden / bei den Verzeichnissen!!!
$path_3rdparty = '/usr/syno/synoman/webman/3rdparty';
$path_phpsrc = '/usr/syno/synoman/phpsrc';
$backup_dir = '/volume1/tmp';
$backup_name = 'backupSynology';
$dh = opendir($path_3rdparty);
while(($res=readdir($dh))!==false){
if($res != '.' && $res != '..' && is_dir($path_3rdparty.'/'.$res)){
$appl_names[] = $res;
}
}
natcasesort($appl_names);
if(isset($_POST['delete']) && isset($_POST['del']) && count($_POST['del']) > 0){
foreach ($_POST['del'] as $wert){
(file_exists($path_3rdparty.'/'.$wert.'/application.cfg') ? $res=file_get_contents($path_3rdparty.'/'.$wert.'/application.cfg') :$res=0);
if($res===0){
continue;
}
preg_match('/path.*?=.*?\/phpsrc(\/.*?)\/.*?\.[^\s]*/i',$res,$ret);
$tt = array();
exec('rm -r '.$path_3rdparty.'/'.$wert,$tt);
if(isset($ret[1]) && is_dir($path_phpsrc.$ret[1])){
exec('rm -r '.$path_phpsrc.$ret[1],$tt);
}
if(count($tt) == 0){
echo 'Erweiterung wurde entfernt';
}else{
echo '<pre>'.implode("\r\n",$tt).'</pre>';
}
exit;
}
}elseif(isset($_POST['senden']) && !empty($_POST['Linktext']) && !empty($_POST['Description']) && !empty($_POST['Dir']) && !
empty($_POST['type']) && !empty($_POST['fname'])){
$att = array();
$strr = '<strong>Please check the data below carefully as after your confirmation application directories will be created with the parameters given<br />*** No operation will be executed <u>before</u> you confirm by clicking below ***</strong>';
$strr .= '<br /><br />First the content of <strong>application.cfg</strong> that will be created in <strong>'.$path_3rdparty.'/'.preg_replace('/\s+|\/+/','',strtolower($_POST['Dir'])).'</strong><br />';
$str = '';
$str .= 'text = '.$_POST['Linktext']."\r\n";
$str .= 'description = '.$_POST['Description']."\r\n";
$str .= 'type = ';
($_POST['type'] == 'embedded') ? $str .= 'embedded' : $str .= 'standalone';
$str .= "\r\n";
$str .= 'path = /phpsrc/'.preg_replace('/\s+|\/+/','',strtolower($_POST['Dir'])).'/'.preg_replace('/\s+|\/+/','',strtolower($_POST['fname']))."\r\n";
$_SESSION['appl.cfg'] = $str;
$_SESSION['appl.dir'] = preg_replace('/\s+|\/+/','',strtolower($_POST['Dir']));
for($i=0;$i<count($_FILES['file']['name']);$i++){
if($_FILES['file']['name'][$i] == ''){
continue;
}
$f = file_get_contents($_FILES['file']['tmp_name'][$i]);
$att[] = array('path'=>$path_phpsrc.'/'.$_SESSION['appl.dir'].'/'.$_FILES['file']['name'][$i],'content'=>$f);
}
if(isset($_POST['file_content']) && trim($_POST['file_content']) != ''){
$att[] = array('path'=>$path_phpsrc.'/'.preg_replace('/\s+|\/+/','',strtolower($_POST['Dir'])).'/'.preg_replace('/\s+|\/+/','',strtolower($_POST['fname'])),'content'=>$_POST['file_content']);
}
$_SESSION['appl.att'] = $att;
echo $strr;
echo '<pre>'.$str.'</pre>';
echo '<br />';
echo 'The following files from your upload will be created:<br /><br />';
foreach($_SESSION['appl.att'] as $wert){
echo '<strong>'.$wert['path'].'</strong><br />';
}
echo '<br />By clicking <a href="'.$_SERVER['PHP_SELF'].'?'.strip_tags(SID).'">this link</a> all the necessary operation will be written to disk!!!<br />You can abort the operation by <a href="https://'.$_SERVER['SERVER_ADDR'].':5001/webman/index.cgi">clicking this link</a>';
}elseif(isset($_SESSION['appl.cfg']) && isset($_SESSION['appl.dir'])){
exec('mkdir '.$path_3rdparty.'/'.$_SESSION['appl.dir']);
exec('mkdir '.$path_phpsrc.'/'.$_SESSION['appl.dir']);
$fp = fopen($path_3rdparty.'/'.$_SESSION['appl.dir'].'/application.cfg','w');
fwrite($fp,$_SESSION['appl.cfg']);
fclose($fp);
foreach($_SESSION['appl.att'] as $wert){
$fp = fopen($wert['path'],'w');
fwrite($fp,$wert['content']);
fclose($fp);
}
unset($_SESSION);
session_destroy();
echo '<a href="https://'.$_SERVER['SERVER_ADDR'].':5001/webman/index.cgi">Operations Successfulls done return</a>';
exit;
}elseif(isset($_POST['backup'])){
if(!is_dir($backup_dir.'/'.$backup_name)){
mkdir($backup_dir.'/'.$backup_name);
}
$t = time();
mkdir($backup_dir.'/'.$backup_name.'/3rdparty_'.$t);
mkdir($backup_dir.'/'.$backup_name.'/phpsrc_'.$t);
foreach($appl_names as $wert){
(file_exists($path_3rdparty.'/'.$wert.'/application.cfg') ? $res=file_get_contents($path_3rdparty.'/'.$wert.'/application.cfg') :$res=0);
if($res===0){
continue;
}
preg_match('/path.*?=.*?\/phpsrc(\/.*?)\/.*?\.[^\s]*/i',$res,$ret);
exec('cp -R '.$path_3rdparty.'/'.$wert.' '.$backup_dir.'/'.$backup_name.'/3rdparty_'.$t);
exec('cp -R '.$path_phpsrc.$ret[1].' '.$backup_dir.'/'.$backup_name.'/phpsrc_'.$t);
}
die('Data seccesfully wrote to backup directory');
}elseif(isset($_POST['restore']) && isset($_POST['time']) && !empty($_POST['time'])){
if(!is_dir($backup_dir.'/'.$backup_name.'/3rdparty_'.$_POST['time']) || !is_dir($backup_dir.'/'.$backup_name.'/phpsrc_'.$_POST['time'])){
die('Data could not be restored because eighter Backup Path was not given OR the subdirectories (3rdparty phpsrc) do not exist in given path');
}
exec('cp -Rf '.$backup_dir.'/'.$backup_name.'/phpsrc_'.$_POST['time'].'/* '.$path_phpsrc);
exec('cp -Rf '.$backup_dir.'/'.$backup_name.'/3rdparty_'.$_POST['time'].'/* '.$path_3rdparty);
die('Data succesfully restored');
}else{
?>
<html>
<head>
<title>3rd Party Application Remover</title>
<style>body,input,textarea,td,option,select{font:11px Verdana}fieldset{border:1px solid #bbb}</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset><legend>installed 3rd-party-applications / remover</legend><table border="0"><tr>
<?php
$ii=0;
foreach ($appl_names as $wert){
echo '<td width="120px"><input type="checkbox" name="del[]" value="'.$wert.'" />'.$wert.'</td><td>';
if (++$ii % 7 == 0) echo '</td></tr><tr>';
}
?>
</tr></table>
<input style="margin-left:150px;margin-top:5px;" type="reset" value="reset form" />
<input style="margin-left:320px" type="submit" name="delete" value="remove 3rd-party-apps" />
</fieldset></form>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
<fieldset><legend>create 3rd-party-application</legend>
<span style="padding-right:53px">text for the link:</span> <input size="90" type="text" name="Linktext" /><br/>
<span style="padding-right:20px">description for tooltip:</span> <input size="90" type="text" name="Description" /><br/>
<span style="padding-right:25px">application directory:</span> <input size="90" type="text" name="Dir" /><br/>
<span style="padding-right:51px">application type:</span> <select name="type">
<option value="embedded" selected="selected">embedded</option>
<option value="popup">popup</option></select><br/>
<span style="padding-right:96px">filename:</span><input input size="90" type="text" name="fname" /><br/>
<span style="padding-right:86px">upload file:</span><input size=89 type="file" name="file[]" /><br/>
<span style="padding-right:86px">upload file:</span><input size=89 type="file" name="file[]" /><br/>
<span style="padding-right:86px">upload file:</span><input size=89 type="file" name="file[]" /><br/>
<span style="padding-right:86px">upload file:</span><input size=89 type="file" name="file[]" /><br/>
<span style="padding-right:86px">upload file:</span><input size=89 type="file" name="file[]" /><br/>
<span style="padding-right:83px">file content:</span><textarea name="file_content" rows="10" cols="87"></textarea><br/>
<input style="margin-left:150px" type="reset" value="reset form" /><input style="margin-left:330px" type="submit" name="senden" value="create 3rd-party-apps" />
</fieldset>
<fieldset><legend>Backup and Restore 3rd Party Applications</legend>
<fieldset><legend>Backup 3rd Party Applications</legend>
<input type="submit" name="backup" value="Create Backup" />
</fieldset>
<fieldset><legend>Restore 3rd Party Applications</legend>
<span style="padding-right:20px">Select Date for restore</span><select name="time">
<?php
$path = $backup_dir.'/'.$backup_name;
$dh = opendir($path);
$arr = array();
while(($res=readdir($dh))!==false){
if($res != '.' && $res != '..' && is_dir($path.'/'.$res)){
$t = substr($res,strpos($res,'_')+1);
$arr[] =intval($t);
}
}
$arr = array_unique($arr);
sort($arr);
foreach($arr as $wert){
if(!empty($wert) && $wert != 0){
echo '<option value="'.$wert.'">'.date('d.m.y H:i:s',$wert).'</option>';
}
}
?>
</select>
<input type="submit" name="restore" value="Restore" />
</fieldset>
</fieldset>
</form>
</body>
</html>
<?php
}
?>