1064 lines
32 KiB
PHP
Executable File
1064 lines
32 KiB
PHP
Executable File
<?php
|
|
error_reporting(0);
|
|
//error_reporting(E_ALL);
|
|
//ini_set("display_errors", 1);
|
|
define('APPDIR', 'IDrive');
|
|
define('SERVICE', 'idriveBackup');
|
|
define('SYNOLOGY','synology');
|
|
define('QNAP','qnap');
|
|
define('NETGEAR','netgear');
|
|
define('TWOTIER','twotier');
|
|
define('ASUSTOR','Asustor');
|
|
|
|
# need to confirm about PHP7.3
|
|
$phpArr = array("PHP7.3", "PHP7.2","PHP7.0", "PHP5.6");
|
|
$phpStopped = "";
|
|
$phpVer = "";
|
|
|
|
if(!isset($argv[1]) || !trim($argv[1])){
|
|
exit('Enter machine name');
|
|
}
|
|
|
|
$machine_type = $argv[1];
|
|
$webDirPath = "";
|
|
|
|
if($machine_type == SYNOLOGY){
|
|
$ver = getSynoDSMVersion();
|
|
$wdir = "web" . (($ver >= 7)? '_packages' : '');
|
|
$webDirPath = "/var/services/$wdir/";
|
|
$perlPath = "/usr/bin/perl";
|
|
$phpPath = "/usr/bin/php";
|
|
$cronPath = "/etc/crontab";
|
|
}
|
|
else if($machine_type == QNAP){
|
|
/*$getWebPath = `/sbin/getcfg SHARE_DEF defWeb -d Qweb -f /etc/config/def_share.info`;
|
|
$getWebPath = trim($getWebPath);
|
|
$webDirPath = "/share/".$getWebPath."/";*/
|
|
$webDirPath = "/share/Web/";
|
|
if(!file_exists($webDirPath))
|
|
$webDirPath = "/share/Qweb/";
|
|
$perlPath = "/opt/bin/perl";
|
|
$phpPath = "/mnt/ext/opt/apache/bin/php";
|
|
$cronPath = "/etc/config/crontab";
|
|
$binaryPath = $webDirPath.APPDIR;
|
|
createLogoutTrigger($binaryPath);
|
|
migrationProcess();
|
|
copyPingForInternalUsageQNAP();
|
|
}
|
|
else if($machine_type == NETGEAR){
|
|
$webDirPath = "/apps/idrive/web/";
|
|
$perlPath = "/usr/bin/perl";
|
|
$phpPath = "/usr/bin/php";
|
|
$cronPath = "/etc/crontab";
|
|
$binaryPath = $webDirPath.APPDIR;
|
|
doCleanup();
|
|
}
|
|
else if($machine_type == TWOTIER){
|
|
$webDirPath = "/usr/local/www/";
|
|
$perlPath = "/usr/pbi/idrive-amd64/bin/perl";
|
|
$phpPath = "/usr/pbi/idrive-amd64/bin/php";
|
|
$cronPath = "/etc/crontab";
|
|
}
|
|
else if($machine_type == ASUSTOR){
|
|
$webDirPath = "/usr/local/www/";
|
|
$perlPath = "/usr/bin/perl";
|
|
$phpPath = "/usr/builtin/bin/php";
|
|
$cronPath = "/var/spool/cron/crontabs/root";
|
|
$binaryPath = $webDirPath.APPDIR;
|
|
cronPerltoPhpUpdate();
|
|
doCleanup();
|
|
}
|
|
|
|
if ($machine_type != TWOTIER){
|
|
$command = "which php";
|
|
$phpPathVal=shell_exec($command);
|
|
$phpPathVal = trim($phpPathVal);
|
|
|
|
if($phpPathVal != ''){
|
|
$phpPath = $phpPathVal;
|
|
}
|
|
|
|
$command = "which perl";
|
|
$perlPathVal=shell_exec($command);
|
|
$perlPathVal = trim($perlPathVal);
|
|
|
|
if($perlPathVal != ''){
|
|
$perlPath = $perlPathVal;
|
|
}
|
|
}
|
|
$binaryPath = $webDirPath.APPDIR;
|
|
cronPerltoPhpUpdate();
|
|
|
|
if(($machine_type == NETGEAR) || ($machine_type == QNAP)){
|
|
|
|
validateIdevsutil();
|
|
if($machine_type == QNAP){
|
|
$suPath = $binaryPath."/.config/su";
|
|
$copiedSu = $binaryPath."/su";
|
|
if(file_exists($copiedSu))
|
|
`mv $copiedSu $suPath`;
|
|
|
|
if(!validateSu($suPath) || !file_exists($suPath)){
|
|
system("sh $binaryPath/.scripts/suBinaryChk.sh --path=$binaryPath/.config/ --machine=$machine_type");
|
|
}
|
|
`chmod 04755 $suPath`;
|
|
|
|
$downloads = $binaryPath."/Downloads";
|
|
if (!is_dir($downloads)){
|
|
`mkdir $downloads`;
|
|
`chmod 777 $downloads`;
|
|
}
|
|
}
|
|
}
|
|
else if($machine_type == SYNOLOGY) {
|
|
if(getSynoDSMVersion() < 7) {
|
|
$unameOut = `uname -m`;
|
|
$pwd = `pwd`;
|
|
$orgpath = "";
|
|
$unameOut = trim($unameOut);
|
|
$orgLibDir = $webDirPath.APPDIR."/.config";
|
|
$orgLib = "libpopt.so.0.0.0";
|
|
$libPath = "/usr/lib";
|
|
$libLinkPath = "libpopt.so.0";
|
|
if("x86_64" == $unameOut) {
|
|
$libPath = "/usr/lib32";
|
|
}
|
|
|
|
chdir($libPath);
|
|
if(is_link($libLinkPath)) {
|
|
if(!file_exists($libLinkPath)) {
|
|
if(!file_exists($orgLib)) {
|
|
# if libpopt.so.0.0.0 not exists then create link to our path
|
|
$orgpath = getCompatibleLibpopt($unameOut,$orgLibDir,$libPath);
|
|
|
|
#unlink libpopt.so.0 and recreate
|
|
unlink($libLinkPath);
|
|
`ln -s $orgpath $libLinkPath`;
|
|
}
|
|
else {
|
|
# if libpopt.so.0.0.0 exists then check path
|
|
$readLink = trim(readlink($libLinkPath));
|
|
if($readLink != "$orgLibDir/$orgLib"){
|
|
unlink($libLinkPath);
|
|
`ln -s $orgLib $libLinkPath`;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
else if(!file_exists($libLinkPath)) {
|
|
|
|
$orgpath = getCompatibleLibpopt($unameOut, $orgLibDir, $libPath);
|
|
`ln -s $orgpath $libLinkPath`;
|
|
}
|
|
}
|
|
|
|
$pwd = trim($pwd);
|
|
chdir($pwd);
|
|
$dsmVersion6 = isDSM6();
|
|
$orgSerPath = "/var/packages/IDrive/target/etc/";
|
|
|
|
$appServiceDir = $webDirPath.SERVICE;
|
|
if(!is_link($appServiceDir)) {
|
|
`ln -s $orgSerPath $appServiceDir`;
|
|
}
|
|
|
|
`chmod -R 0777 $appServiceDir`;
|
|
|
|
#create user profile directory for new service path
|
|
$userProfilePath = $appServiceDir . "/user_profile";
|
|
if(!file_exists($userProfilePath)){
|
|
`mkdir "$userProfilePath"`;
|
|
`chmod 0777 "$userProfilePath"`;
|
|
}
|
|
doCleanup($dsmVersion6);
|
|
}
|
|
|
|
$orgSerPath = "/var/packages/IDrive/target/etc/";
|
|
$appServiceDir = $webDirPath.SERVICE;
|
|
|
|
if(!is_link($appServiceDir)) {
|
|
`ln -s $orgSerPath $appServiceDir`;
|
|
}
|
|
|
|
`chmod -R 0777 $appServiceDir`;
|
|
|
|
/*********************************************************************************************************
|
|
* Subroutine Name : getSynoDSMVersion
|
|
* Objective : Finds the Synology DSM version
|
|
* Added By : Sabin Cheruvattil
|
|
**********************************************************************************************************/
|
|
function getSynoDSMVersion()
|
|
{
|
|
$verarr = explode("\n", `cat /etc.defaults/VERSION`);
|
|
array_walk($verarr, 'trim');
|
|
|
|
$kvp = preg_split('/=/', $verarr[0]);
|
|
$ver = (int)trim($kvp[1], '"');
|
|
|
|
return $ver;
|
|
}
|
|
|
|
/*********************************************************************************************************
|
|
* Subroutine Name : isDSM6
|
|
* Objective : To find, is this DSM 6
|
|
* Added By : Sabin Cheruvattil
|
|
**********************************************************************************************************/
|
|
function isDSM6()
|
|
{
|
|
$ver = getSynoDSMVersion();
|
|
return ($ver >= 6 && $ver < 7)? TRUE : FALSE;
|
|
}
|
|
|
|
/*********************************************************************************************************
|
|
* Subroutine Name : isDSM7
|
|
* Objective : To find, is this DSM 6
|
|
* Added By : Sabin Cheruvattil | 2016-11-25
|
|
**********************************************************************************************************/
|
|
function isDSM7()
|
|
{
|
|
$ver = getSynoDSMVersion();
|
|
return ($ver >= 7 && $ver < 8)? TRUE : FALSE;
|
|
}
|
|
|
|
/*********************************************************************************************************
|
|
* Subroutine Name : doCleanup
|
|
* Objective : Restart php-fpm & httpd servers.
|
|
* Added By : Sabin Cheruvattil | 2016-11-28
|
|
**********************************************************************************************************/
|
|
function doCleanup($isDSM6=0)
|
|
{
|
|
GLOBAL $phpFpmConfPath, $phpNameArray, $machine_type, $webDirPath;
|
|
# This is still a guess, we will try to find the actual path to it.
|
|
getPHPConfdPath($isDSM6);
|
|
|
|
foreach($phpFpmConfPath as $additionalConfPath){
|
|
$additionalConfPath = trim($additionalConfPath);
|
|
if($additionalConfPath == '') {
|
|
continue;
|
|
}
|
|
if($machine_type == ASUSTOR){
|
|
$orgIni = "/usr/local/AppCentral/".APPDIR."/www/.scripts/".APPDIR.".ini";
|
|
} else if($machine_type == SYNOLOGY){
|
|
$orgIni = "/var/packages/".APPDIR."/target/".APPDIR.".ini";
|
|
} else if($machine_type == NETGEAR){
|
|
$orgIni = $webDirPath.APPDIR."/.scripts/".APPDIR.".ini";
|
|
}
|
|
$newIni = $additionalConfPath."/".APPDIR.".ini";
|
|
if(is_link($newIni)){
|
|
unlink($newIni);
|
|
}
|
|
`ln -s $orgIni $newIni`;
|
|
}
|
|
|
|
|
|
if($machine_type == ASUSTOR){
|
|
`/usr/builtin/sbin/apache2 -k restart`;
|
|
} else if ($isDSM6) {
|
|
|
|
#need to create conf file as per the machine configuratuion.
|
|
//$phpVer = getPHPVersion();
|
|
// $phpVer = fetchPHPVersion();
|
|
// createConfFile($phpVer);
|
|
/*$resNginx = system("/usr/syno/sbin/synoservicectl --restart nginx");
|
|
if($resNginx == "") {
|
|
system("synoservice --restart nginx");
|
|
}
|
|
*/
|
|
/*foreach($phpNameArray as $phpversion) {
|
|
if ($phpversion == "php") {
|
|
`initctl restart $phpversion-fpm`;
|
|
}
|
|
else {
|
|
`initctl restart pkg-$phpversion-fpm`;
|
|
}
|
|
}
|
|
`/usr/sbin/initctl restart pkg-apache22`;
|
|
`/usr/sbin/initctl restart pkg-apache24`;*/
|
|
/*$resApa22 = system("/usr/syno/sbin/synoservicectl --restart pkgctl-Apache2.2");
|
|
if($resApa22 == "") {
|
|
system("synoservice --stop pkgctl-Apache2.2");
|
|
system("synoservice --start pkgctl-Apache2.2");
|
|
}
|
|
|
|
$resApa24 = system("/usr/syno/sbin/synoservicectl --restart pkgctl-Apache2.4");
|
|
if($resApa24 == "") {
|
|
system("synoservice --stop pkgctl-Apache2.4");
|
|
system("synoservice --start pkgctl-Apache2.4");
|
|
}*/
|
|
|
|
//`/usr/syno/sbin/synoservicectl --restart nginx`;
|
|
/*$resNginx = system("/usr/syno/sbin/synoservicectl --restart nginx");
|
|
if($resNginx == "") {
|
|
system("synoservice --restart nginx");
|
|
}*/
|
|
//`/usr/syno/sbin/synoservicectl --restart nginx`;
|
|
//`/usr/sbin/initctl restart nginx`;
|
|
|
|
}
|
|
else if($machine_type == SYNOLOGY and getSynoDSMVersion() < 6){
|
|
`/sbin/initctl stop httpd-user`;
|
|
`/sbin/initctl start httpd-user`;
|
|
}
|
|
else if($machine_type == NETGEAR) {
|
|
$dpkgOutput = trim(shell_exec('dpkg -l php5-sqlite 2>&1'));
|
|
if(preg_match('/no packages found/', $dpkgOutput)) {
|
|
# install php5-sqlite
|
|
`apt-get install php5-sqlite`;
|
|
}
|
|
|
|
$dpkgOutput = trim(shell_exec('dpkg -l libsqlite3-0 2>&1'));
|
|
if(preg_match('/no packages found/', $dpkgOutput)) {
|
|
# install libsqlite3-0
|
|
`apt-get install libsqlite3-0`;
|
|
}
|
|
|
|
$dpkgOutput = trim(shell_exec('dpkg -l sqlite 2>&1'));
|
|
if(preg_match('/no packages found/', $dpkgOutput)) {
|
|
# install sqlite
|
|
`apt-get install sqlite`;
|
|
}
|
|
|
|
system("systemctl restart apache2.service");
|
|
}
|
|
// added by anil to trigger logout after update based on this file check.
|
|
createLogoutTrigger($webDirPath.APPDIR);
|
|
copyPingForInternalUsage();
|
|
migrationProcess();
|
|
makeServiceDirAsLinkIfExist();
|
|
validateIdevsutil();
|
|
exit;
|
|
}
|
|
|
|
|
|
/*************************************************************************************************************************
|
|
* Subroutine Name : validateIdevsutil
|
|
* Objective : validate idevs util binary file. If it is invalid, downloads the appropriate one from server
|
|
**************************************************************************************************************************/
|
|
function validateIdevsutil($val="idevsutil_IDrive"){
|
|
|
|
GLOBAL $binaryPath, $machine_type;
|
|
#$lines = 0;
|
|
$helpData = '';
|
|
#Checking the validity of idevsutil binary
|
|
$idevsutilPath = $binaryPath."/idevsutil_IDrive";
|
|
$idevsutil = $binaryPath."/".$val;
|
|
if(file_exists($idevsutil)){
|
|
$helpData = `$idevsutil -h 1>/dev/null 2>/dev/null; echo $?`;
|
|
}else{
|
|
system("sh $binaryPath/.scripts/binaryChk.sh --path=$binaryPath/ --machine=$machine_type 2>/dev/null 1>2");
|
|
}
|
|
/*if($lines < 50){*/
|
|
if(trim($helpData) != 0){
|
|
system("sh $binaryPath/.scripts/binaryChk.sh --path=$binaryPath/ --machine=$machine_type 2>/dev/null 1>2");
|
|
}else if($val=="idevsutil_IDrive")
|
|
{
|
|
validateIdevsutil("idevsutil_IDrive_Dedup");
|
|
}
|
|
}
|
|
|
|
|
|
/*********************************************************************************************************
|
|
* Subroutine Name : getPHPConfdPath
|
|
* Objective : To get the php conf.d path
|
|
* Added By : Sabin Cheruvattil | 2016-11-25
|
|
**********************************************************************************************************/
|
|
function getPHPConfdPath($isDSM6=0)
|
|
{
|
|
GLOBAL $phpFpmConfPath, $phpNameArray, $machine_type;
|
|
$phpFpmConfPath = array();
|
|
|
|
if($machine_type == SYNOLOGY && isDSM7()) {
|
|
return 0;
|
|
}
|
|
|
|
$psPath = "ps -wwwww";
|
|
|
|
if($isDSM6 or $machine_type == ASUSTOR) {
|
|
$psPath = "ps -elf";
|
|
}
|
|
|
|
$phpFpmPath = `$psPath | grep 'php-fpm' | grep 'master process'`;
|
|
preg_match_all("/\/(php.*)\/fpm.conf/", $phpFpmPath, $match);
|
|
|
|
$phpNameArray = $match[1];
|
|
if(!in_array('php', $phpNameArray)) {
|
|
array_push($phpNameArray, 'php');
|
|
}
|
|
|
|
# DSM 6 + Web Station Beta
|
|
if($isDSM6) {
|
|
$phpFpmPath = `$psPath | grep 'php-fpm' | grep 'master process'`;
|
|
//preg_match_all("/\/(php(\d*))\/php-fpm.conf/", $phpFpmPath, $secmatch);
|
|
preg_match_all("/\/target\/misc\/(php.*)_fpm.conf/", $phpFpmPath, $secmatch);
|
|
foreach($secmatch[1] as $phppack) {
|
|
array_push($phpNameArray, $phppack);
|
|
}
|
|
}
|
|
|
|
foreach($phpNameArray as $phpName){
|
|
$pathToPhpBinary = trim(`which $phpName`);
|
|
if($pathToPhpBinary != '') {
|
|
$phpIniInfo = `$pathToPhpBinary --ini`;
|
|
$output = explode("\n", $phpIniInfo);
|
|
foreach($output as $value) {
|
|
if(preg_match("/^Scan for additional .ini files in:/", $value)) {
|
|
$iniDirPath = trim(preg_replace("/Scan for additional .ini files in:/", '', $value));
|
|
if(strstr($iniDirPath,'/'))
|
|
array_push($phpFpmConfPath,$iniDirPath);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if($isDSM6) {
|
|
$phpFpmPath = `$psPath | grep 'php-fpm' | grep 'master process' | grep 'php_profile'`;
|
|
preg_match_all("/\((.*)\/fpm\.conf/", $phpFpmPath, $match);
|
|
|
|
foreach($match[1] as $confdir) {
|
|
array_push($phpFpmConfPath, $confdir . '/conf.d');
|
|
}
|
|
}
|
|
|
|
if($machine_type == NETGEAR){
|
|
$iniDirPath = "/etc/php5/apache2/conf.d";
|
|
if(is_dir($iniDirPath))
|
|
array_push($phpFpmConfPath,$iniDirPath);
|
|
}
|
|
}
|
|
|
|
/***********************************************************************************************
|
|
* Subroutine Name : getCompatibleLibpopt
|
|
* Objective : get the appropriate libpopt file based on machine arch
|
|
***********************************************************************************************/
|
|
function getCompatibleLibpopt($arch="",$basePath="",$libPath)
|
|
{
|
|
if(file_exists($basePath."/libpopt")){
|
|
$libpoptUnivPath = $basePath."/libpopt/libpopt.so.0.0.0";
|
|
|
|
if(preg_match("/armv/i",$arch)){
|
|
$cmd = `uname -a `;
|
|
if(preg_match("/alpine/i", $cmd))
|
|
$archBaseLibpoptPath = $libpoptUnivPath."_alpine";
|
|
else
|
|
$archBaseLibpoptPath = $libpoptUnivPath."_arm";
|
|
}
|
|
else if(preg_match("/aarch64/i",$arch)){
|
|
$archBaseLibpoptPath = $libpoptUnivPath."_aarch64";
|
|
}
|
|
else{
|
|
$archBaseLibpoptPath = $libpoptUnivPath."_X86";
|
|
}
|
|
|
|
|
|
if(file_exists($archBaseLibpoptPath)){
|
|
if(file_exists($libpoptPath))
|
|
unlink($libpoptPath);
|
|
|
|
`mv $archBaseLibpoptPath $libpoptPath`; # it is for universal case and we are moving from libpopt dir to .config in our folder.
|
|
`chown root:root $libpoptPath`;
|
|
`chmod 00755 $libpoptPath`;
|
|
}
|
|
}
|
|
|
|
$libpoptPath = $basePath."/libpopt.so.0.0.0";
|
|
$liborgpath = $libPath."/libpopt.so.0.0.0";
|
|
|
|
if(is_link($liborgpath)){
|
|
if(file_exists($liborgpath)){
|
|
$readLink = trim(readlink($liborgpath));
|
|
return $readLink;
|
|
}
|
|
}
|
|
else if(file_exists($liborgpath))
|
|
return $liborgpath;
|
|
|
|
if(file_exists($libpoptPath)){
|
|
`ln -s $libpoptPath $liborgpath`;
|
|
`chown root:root $liborgpath`;
|
|
`chmod 00755 $liborgpath`;
|
|
}
|
|
|
|
return $libpoptPath;
|
|
}
|
|
|
|
/********************************************************************************************
|
|
* Subroutine Name : validateSu
|
|
* Objective : validates su binary file
|
|
*********************************************************************************************/
|
|
function validateSu($suPath=""){
|
|
$outfile = "/tmp/out.txt";
|
|
$suHelpRes = `$suPath --help 1>$outfile 2>>$outfile`;
|
|
$suLines=`wc -l /tmp/out.txt | awk -F " " '{print $1}'`;
|
|
if(file_exists($outfile)){
|
|
unlink($outfile);
|
|
}
|
|
if($suLines < 5){
|
|
return FALSE;
|
|
}
|
|
/*echo "$suPath --help; echo $?;";
|
|
echo $suHelpRes = `$suPath --help; echo $?;`;
|
|
if(trim($suHelpRes) != "0"){
|
|
echo "dfsaf";
|
|
return FALSE;
|
|
}*/
|
|
return TRUE;
|
|
}
|
|
/*************************************************************************************************************
|
|
# Subroutine Name : cronPerltoPhpUpdate
|
|
# Objective : updating old cron perl files path to new php path
|
|
# Added By : Vijay Vinoth C
|
|
#**************************************************************************************************************/
|
|
function cronPerltoPhpUpdate(){
|
|
GLOBAL $cronPath, $perlPath, $phpPath;
|
|
$count = 0;
|
|
$linesCrontabConfigFile = file($cronPath);
|
|
$linesCrontabExLines = array();
|
|
foreach($linesCrontabConfigFile as $cronTabLine) {
|
|
$cronTabLine = trim($cronTabLine);
|
|
if($cronTabLine == "")
|
|
continue;
|
|
$perlCount = 0;
|
|
if((strpos($cronTabLine,SERVICE.'/user_profile') !== false) && (strpos($cronTabLine,$perlPath) !== false)){
|
|
if(strpos($cronTabLine,'.scripts/Restore_Script_IDrive.pl') !== false)
|
|
$cronTabLine = str_replace(".scripts/Restore_Script_IDrive.pl", ".scripts/Restore_Script_IDrive.php", $cronTabLine );
|
|
elseif(strpos($cronTabLine,'.scripts/Backup_Script_IDrive.pl') !== false)
|
|
$cronTabLine = str_replace(".scripts/Backup_Script_IDrive.pl", ".scripts/Backup_Script_IDrive.php", $cronTabLine);
|
|
elseif(strpos($cronTabLine,'.scripts/Job_Termination_Script.pl') !== false)
|
|
$cronTabLine = str_replace(".scripts/Job_Termination_Script.pl", ".scripts/Job_Termination_Script.php", $cronTabLine);
|
|
|
|
$cronTabLine = str_replace("; ".$perlPath." /", "; ".$phpPath." -d open_basedir=NULL /", $cronTabLine, $perlCount);
|
|
if($perlCount == 0){
|
|
$cronTabLineArr = explode(" ",$cronTabLine);
|
|
$cronTabLineArrVal = array_map("convertPerlToPhpFallBack",$cronTabLineArr);
|
|
$cronTabLine = implode(" ",$cronTabLineArrVal);
|
|
}
|
|
$linesCrontabExLines[] = $cronTabLine."\n";
|
|
$count = $count+1;
|
|
}
|
|
else
|
|
$linesCrontabExLines[] = $cronTabLine."\n";
|
|
}
|
|
|
|
if($count > 0)
|
|
{
|
|
file_put_contents($cronPath,$linesCrontabExLines);
|
|
configureCron();
|
|
}
|
|
}
|
|
|
|
/*************************************************************************************************************
|
|
# Subroutine Name : configureCron
|
|
# Objective :
|
|
# Added By : Vijay Vinoth C
|
|
#**************************************************************************************************************/
|
|
function configureCron()
|
|
{
|
|
GLOBAL $machine_type;
|
|
|
|
$retVal = 1;
|
|
if($machine_type == SYNOLOGY){
|
|
$retVal = system("/usr/syno/sbin/synoservicectl --restart crond");
|
|
# $? perl special variable to collect the error returned by system call
|
|
if($retVal == ""){
|
|
system("synoservice --restart crond");
|
|
$retVal = 0;
|
|
}
|
|
else
|
|
$retVal = 1;
|
|
}
|
|
else if($machine_type == ASUSTOR){
|
|
system("./etc/init.d/S41crond stop");
|
|
system("./etc/init.d/S41crond start");
|
|
}
|
|
else if($machine_type == NETGEAR){
|
|
if(file_exists("./etc/init.d/cron"))
|
|
system("./etc/init.d/cron restart");
|
|
else
|
|
system("service cron restart");
|
|
}
|
|
else {
|
|
$retVal = system("/etc/init.d/crond.sh restart");
|
|
if($retVal == "")
|
|
$retVal = 0;
|
|
else
|
|
$retVal = 1;
|
|
}
|
|
//$retVal = $?;
|
|
//$retVal = $retVal >> 8;
|
|
return $retVal;
|
|
|
|
}
|
|
|
|
function convertPerlToPhpFallBack($str)
|
|
{
|
|
GLOBAL $perlPath, $phpPath;
|
|
if(strpos($str,$perlPath) !== false || strpos($perlPath,$str) !== false)
|
|
$str = $phpPath." -d open_basedir=NULL";
|
|
return($str);
|
|
}
|
|
|
|
/****************************************************************************************************
|
|
* Function Name : createLogoutTrigger
|
|
* Objective : This method is used to dump a file once app got updated which is used to trigger logout.
|
|
* Added By : Anil Kumar
|
|
******************************************************************************************************/
|
|
function createLogoutTrigger($binaryPath)
|
|
{
|
|
//added by anil to trigger logout after update based on this file check.
|
|
$filePath = "$binaryPath/logout.txt";
|
|
if(file_put_contents($filePath,"freshInstall"))
|
|
chmod($filePath,0777);
|
|
}
|
|
|
|
/****************************************************************************************************
|
|
* Function Name : moveUserToNASDir
|
|
* Objective : This method is used to move the the existing users data to appr. NAS user dir.
|
|
* Added By : Anil Kumar
|
|
******************************************************************************************************/
|
|
function moveUserToNASDir()
|
|
{
|
|
GLOBAL $webDirPath, $machine_type, $cronPath;
|
|
|
|
$appServiceDir = $webDirPath.SERVICE;
|
|
$oldServiceDir = $webDirPath."idriveIt";
|
|
|
|
$nasUserName = exec('whoami');
|
|
$nasUserName = trim($nasUserName,"\n");
|
|
|
|
if($nasUserName == '')
|
|
$nasUserName = "admin";
|
|
else if($nasUserName == "root")
|
|
$nasUserName = "admin";
|
|
|
|
#create user profile directory for new service path
|
|
$userProfilePath = $appServiceDir."/user_profile";
|
|
if(!file_exists($userProfilePath)){
|
|
`mkdir "$userProfilePath"`;
|
|
`chmod 0777 "$userProfilePath"`;
|
|
}
|
|
|
|
$oldUserProfilePath = $oldServiceDir."/user_profile/";
|
|
$dh = opendir($oldUserProfilePath);
|
|
|
|
if(!$dh){
|
|
print "Unable to open directory $oldUserProfilePath\n";
|
|
return;
|
|
}
|
|
|
|
while(($userName = readdir($dh)) !== false) {
|
|
#Avoid unnecessary directories
|
|
if(($userName === ".") || ($userName === "..") || (strpos($userName, ".trace") !== FALSE))
|
|
continue;
|
|
|
|
$nasUserNameToUpdate = $nasUserName;
|
|
|
|
#Find nas user name
|
|
$jobs = array("Backup", "Restore");
|
|
foreach ($jobs as $jobType){
|
|
$path = $oldUserProfilePath.$userName."/".$jobType."/+ManualTask/LOGS";
|
|
if(file_exists($path)){
|
|
$cmd = "stat -c %U $path 2>/dev/null";
|
|
$commandToGetOwn = shell_exec($cmd);
|
|
$nasUserNameToUpdate = trim($commandToGetOwn,"\n");
|
|
if(empty($nasUserNameToUpdate)){
|
|
$userInfo = posix_getpwuid(fileowner($path));
|
|
if(($userInfo['name'] == "") || ($userInfo['name'] == "root"))
|
|
$nasUserNameToUpdate = $nasUserName;
|
|
else
|
|
$nasUserNameToUpdate = $userInfo['name'];
|
|
}
|
|
else if($nasUserNameToUpdate == "root")
|
|
$nasUserNameToUpdate = "admin";
|
|
break;
|
|
}
|
|
}
|
|
|
|
# Create NAS user directory if does not exist
|
|
$nasUserPath = $userProfilePath."/".$nasUserNameToUpdate."/";
|
|
if(!file_exists($nasUserPath)){
|
|
`mkdir "$nasUserPath"`;
|
|
`chmod 0777 "$nasUserPath"`;
|
|
}
|
|
|
|
$userPath = $oldUserProfilePath.$userName."/";
|
|
|
|
$cp_cmd = "cp -rfp \"$userPath\" \"$nasUserPath/\"";
|
|
$test = `$cp_cmd`;
|
|
|
|
#Update cron file with new service path name
|
|
$from = "idriveIt/user_profile/".$userName."/";
|
|
$to = "idriveBackup/user_profile/".$nasUserNameToUpdate."/".$userName."/";
|
|
$from = str_replace( '/', '\/', $from );
|
|
$to = str_replace( '/', '\/', $to );
|
|
|
|
$updateCronEntry = "sed -i 's/$from/$to/g' $cronPath 1>/dev/null 2>/dev/null ";
|
|
`$updateCronEntry`;
|
|
|
|
#Commit the changes for cron
|
|
configureCron();
|
|
|
|
#Update restore loction as per new service path name
|
|
$restoreLocFile = $nasUserPath.$userName."/restore_location.txt";
|
|
if(file_exists($restoreLocFile)) {
|
|
$updateRestoreLoc = "sed -i 's/$from/$to/g' \"$restoreLocFile\"";
|
|
`$updateRestoreLoc`;
|
|
}
|
|
|
|
#Update full path exclude to skip latest service path
|
|
$fullPathExclude = $nasUserPath.$userName."/ExcludeFullPath.txt";
|
|
if(file_exists($fullPathExclude)) {
|
|
$fullPathExcludeContent=file_get_contents($fullPathExclude);
|
|
$fullPathExcludeContent=str_replace("/idriveIt\n", "/idriveBackup\n" ,$fullPathExcludeContent);
|
|
file_put_contents($fullPathExclude, $fullPathExcludeContent);
|
|
}
|
|
|
|
#Update client info to attach NAS user name with all records
|
|
$checkClientInfo = $appServiceDir."/.cache/clientInfo.txt";
|
|
$contentsArr = file($checkClientInfo);
|
|
$str = file_get_contents($checkClientInfo);
|
|
|
|
for($i = 1; $i < count($contentsArr); $i++)
|
|
{
|
|
if(trim($contentsArr[$i],"\n") == ("/".trim($userName,"\n")."/"))
|
|
{
|
|
$userInfo = "$nasUserNameToUpdate/$userName\n";
|
|
$str = str_replace("/".$userName."/\n", $userInfo ,$str);
|
|
file_put_contents($checkClientInfo, $str);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
closedir($dh);
|
|
}
|
|
|
|
/****************************************************************************************************
|
|
* Function Name : migrationProcess
|
|
* Objective : This method is used to initiate migration process.
|
|
* Added By : Anil Kumar
|
|
******************************************************************************************************/
|
|
function migrationProcess()
|
|
{
|
|
GLOBAL $webDirPath;
|
|
$appServiceDir = $webDirPath.SERVICE;
|
|
$oldServiceDir = $webDirPath."idriveIt";
|
|
|
|
if(!file_exists($oldServiceDir))
|
|
return;
|
|
|
|
# creating new service directory and providing permissions
|
|
`mkdir $appServiceDir`;
|
|
`chmod -R 0777 $appServiceDir`;
|
|
|
|
#migrate cache dir
|
|
if(file_exists($oldServiceDir."/.cache")){
|
|
$copyCMD = "cp -rfp \"$oldServiceDir/.cache\" \"$appServiceDir/\"";
|
|
`$copyCMD`;
|
|
}
|
|
|
|
#migrate htaccess file
|
|
if(file_exists($oldServiceDir."/.htaccess")){
|
|
$copyCMD = "cp -fp \"$oldServiceDir/.htaccess\" \"$appServiceDir\"";
|
|
`$copyCMD`;
|
|
}
|
|
|
|
#migrate user profile
|
|
moveUserToNASDir();
|
|
|
|
#Move old service path inside new service path as backup
|
|
$move_cmd = "mv -f '".$oldServiceDir."' '".$appServiceDir."/'";
|
|
$test1 = `$move_cmd`;
|
|
|
|
}
|
|
|
|
/****************************************************************************************************
|
|
* Function Name : getPHPVersion
|
|
* Objective : This method is used to descide which PHP is available based on the DSM version.
|
|
* Added By : Anil Kumar
|
|
******************************************************************************************************/
|
|
function getPHPVersion()
|
|
{
|
|
$phpVer = "";
|
|
$phpArr = array("php72", "php70","php56");
|
|
//$php_info = array("php72_min"=>"6.2.23793","php70_min"=>"6.0.2.8575", "php56_min"=>"6.0.1.7393");
|
|
$phpInfo = array("php72"=>"6.2.23739","php70"=>"6.0.2.8575", "php56"=>"6.0.1.7393");
|
|
|
|
$dsmVersionInfo = array();
|
|
$versionData = file("/etc.defaults/VERSION");
|
|
foreach($versionData as $line){
|
|
$keyValuePair = preg_split('/=/', $line);
|
|
$dsmVersionInfo[trim($keyValuePair[0])] = trim($keyValuePair[1]);
|
|
}
|
|
|
|
foreach($phpInfo as $php => $minVerSup){
|
|
$requiredVer = explode(".", $minVerSup);
|
|
for($i = 0; $i<=count($requiredVer); $i++){
|
|
$dsmVersionInfo["majorversion"] =(int)trim($dsmVersionInfo["majorversion"],'"');
|
|
$dsmVersionInfo["minorversion"] =(int)trim($dsmVersionInfo["minorversion"],'"');
|
|
$dsmVersionInfo["smallfixnumber"] =(int)trim($dsmVersionInfo["smallfixnumber"],'"');
|
|
$dsmVersionInfo["buildnumber"] =(int)trim($dsmVersionInfo["buildnumber"],'"');
|
|
|
|
if($requiredVer[$i] > $dsmVersionInfo["majorversion"])
|
|
break;
|
|
|
|
if($requiredVer[$i+1] > $dsmVersionInfo["minorversion"])
|
|
break;
|
|
|
|
$buildnumber = 0;
|
|
if((count($requiredVer) > 3 ) && ($requiredVer[$i+2] != 0) && ($requiredVer[$i+2] <= $dsmVersionInfo["smallfixnumber"])) {
|
|
$phpVer = $php;
|
|
return $phpVer;
|
|
}
|
|
elseif((count($requiredVer) > 3))
|
|
$buildnumber = $requiredVer[$i+3];
|
|
else
|
|
$buildnumber = $requiredVer[$i+2];
|
|
|
|
if($buildnumber > $dsmVersionInfo["buildnumber"])
|
|
break;
|
|
|
|
$phpVer = $php;
|
|
return $phpVer;
|
|
}
|
|
}
|
|
return $phpVer;
|
|
}
|
|
|
|
|
|
function fetchPHPVersion()
|
|
{
|
|
GLOBAL $phpArr, $phpVer, $phpStopped;
|
|
#To check PHP version installed.
|
|
$installedPHPVersion = checkPHPExistsOrNot();
|
|
if($installedPHPVersion != "")
|
|
{
|
|
//echo "\n\n PHP VERSION INSTALLED: ".$installedPHPVersion."\n";
|
|
return $installedPHPVersion;
|
|
//exit;
|
|
}
|
|
|
|
if($phpStopped != "") {
|
|
if(startPHP()) {
|
|
echo "\n\n PHP VERSION STARTED: ".$phpVer."\n";
|
|
return $phpVer;
|
|
//exit;
|
|
}
|
|
}
|
|
|
|
$installPHP = installPHP();
|
|
if($installPHP != "") {
|
|
echo "\n\n Installed PHP VERSION IS: ".$installPHP."\n";
|
|
return $installPHP;
|
|
}
|
|
//echo "\n\n Installed PHP VERSION IS: ".$installPHP."\n";
|
|
}
|
|
|
|
|
|
|
|
|
|
# function to start PHP
|
|
function startPHP() {
|
|
GLOBAL $phpStopped, $phpVer ;
|
|
|
|
$commmand = "synopkg start ".$phpStopped;
|
|
$result = `$commmand`;
|
|
if(strstr($result,"start successfully")){
|
|
$phpVer = $phpStopped;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/*****************************************************************************************************
|
|
# Function : checkPHPExistsOrNot
|
|
# In Param : ---
|
|
# Out Param : ---
|
|
# Objective : This function is to to install latest PHP available for the DSM
|
|
# Added By : Anil Kumar [05-01-2020]
|
|
# Modified By : <name> [Date]
|
|
#****************************************************************************************************/
|
|
function installPHP() {
|
|
GLOBAL $phpArr, $phpVer, $phpStopped ;
|
|
|
|
foreach($phpArr as $php){
|
|
$commmand = "synopkg install_from_server ".$php;
|
|
echo $commmand;
|
|
|
|
$result = trim(system($commmand));
|
|
echo $result;
|
|
|
|
if($result == "")
|
|
{
|
|
$phpVer = $php;
|
|
break;
|
|
}
|
|
/*if(strstr($result,"Failed to query package list from server") || strstr($result," already installed")){
|
|
$phpVer = $php;
|
|
return true;
|
|
}*/
|
|
}
|
|
return $phpVer;
|
|
}
|
|
|
|
/*****************************************************************************************************
|
|
# Function : checkPHPExistsOrNot
|
|
# In Param : ---
|
|
# Out Param : ---
|
|
# Objective : This function is to check which PHP Exist
|
|
# Added By : Anil Kumar [05-01-2020]
|
|
# Modified By : <name> [Date]
|
|
#****************************************************************************************************/
|
|
function checkPHPExistsOrNot() {
|
|
GLOBAL $phpArr, $phpVer, $phpStopped ;
|
|
foreach($phpArr as $php){
|
|
|
|
$commmand = "synopkg status ".$php;
|
|
$result = `$commmand`;
|
|
|
|
if(strstr($result,"package is started")){
|
|
$phpVer = $php;
|
|
break;
|
|
}else if(strstr($result,"package is stopped")){
|
|
$phpStopped = $php;
|
|
break;
|
|
}
|
|
$phpVer = "";
|
|
}
|
|
return $phpVer;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************************
|
|
* Function Name : createConfFile
|
|
* Objective : This method is used to create link for IDrive conf and php fpm.
|
|
* Added By : Anil Kumar
|
|
******************************************************************************************************/
|
|
|
|
function createConfFile($phpVer){
|
|
$val1 = strtolower($phpVer);
|
|
$val2 = str_replace(".","",$val1);
|
|
$confPath = "/var/packages/IDrive/target/nginx_conf";
|
|
$header = 'location ^~ /IDrive/ {
|
|
fastcgi_read_timeout 3600;
|
|
root /var/services/web;
|
|
include /usr/syno/etc/packages/IDrive/nginx/*.conf;
|
|
location ~ ^/IDrive/(.*)/\. {
|
|
deny all;
|
|
}
|
|
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
|
|
expires max;
|
|
log_not_found off;
|
|
}
|
|
location ~ \.php$ {
|
|
include fastcgi.conf;
|
|
fastcgi_pass unix:/run/php-fpm/';
|
|
|
|
$footer = '
|
|
}
|
|
location = /IDrive/doc/html/ {
|
|
return 301 https://docs.phpmyadmin.net/en/latest/;
|
|
}
|
|
location ~ ^/IDrive/doc/html/.*\.html$ {
|
|
rewrite ^/IDrive/doc/html/(.*) https://docs.phpmyadmin.net/en/latest/$1 permanent;
|
|
}
|
|
}
|
|
|
|
location = /IDrive {
|
|
fastcgi_read_timeout 300;
|
|
root /var/services/web;
|
|
include /usr/syno/etc/packages/IDrive/nginx/*.conf;
|
|
try_files $uri/ /IDrive/index.php$is_args$args;
|
|
}
|
|
';
|
|
|
|
$content = $header.$val2."-fpm.sock;".$footer;
|
|
|
|
#Create conf file
|
|
|
|
if (is_dir($confPath))
|
|
`rm -rf $confPath`;
|
|
|
|
`mkdir $confPath`;
|
|
`chmod 777 $confPath`;
|
|
|
|
file_put_contents($confPath."/www.IDrive.enable.conf", $content);
|
|
`chmod 644 "/var/packages/IDrive/target/nginx_conf/www.IDrive.enable.conf"`;
|
|
|
|
`ln -sf "/var/packages/IDrive/target/nginx_conf/www.IDrive.enable.conf" "/usr/local/etc/nginx/conf.d/"`;
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************************************
|
|
* Function Name : copyPingForInternalUsage
|
|
* Objective : This method is used to recreate ping in IDrive path to make backup to wait untill internet connects for all the nas users.
|
|
* Added By : Anil Kumar
|
|
******************************************************************************************************/
|
|
function copyPingForInternalUsage()
|
|
{
|
|
GLOBAL $webDirPath;
|
|
|
|
$pingPath = `which ping`;
|
|
if(empty($pingPath))
|
|
return;
|
|
$copyCmd = "cp -rfp ".trim($pingPath,"\n")." ".$webDirPath.APPDIR;
|
|
`$copyCmd`;
|
|
$newPingPath = $webDirPath.APPDIR."/ping";
|
|
`chmod 04755 $newPingPath`;
|
|
|
|
}
|
|
|
|
/****************************************************************************************************
|
|
* Function Name : copyPingForInternalUsageQNAP
|
|
* Objective : This method is used to recreate busybox and ping in IDrive path to make backup to wait untill internet connects for all the nas users.
|
|
* Added By : Anil Kumar
|
|
******************************************************************************************************/
|
|
function copyPingForInternalUsageQNAP()
|
|
{
|
|
GLOBAL $webDirPath;
|
|
|
|
$pingPath = `which ping`;
|
|
if(empty($pingPath))
|
|
return;
|
|
$copyCmd = "cp -rfpL ".trim($pingPath,"\n")." ".$webDirPath.APPDIR."/ping";
|
|
`$copyCmd`;
|
|
$newPingPath = $webDirPath.APPDIR."/ping";
|
|
`chmod 04755 $newPingPath`;
|
|
}
|
|
|
|
/*****************************************************************************************************
|
|
# Function : makeServiceDirAsLinkIfExist
|
|
# In Param : ---
|
|
# Out Param : ---
|
|
# Objective : This function is to migrate serive directory from folder to link for existing users.
|
|
# Added By : Anil Kumar [05-01-2020]
|
|
# Modified By : <name> [Date]
|
|
#****************************************************************************************************/
|
|
function makeServiceDirAsLinkIfExist() {
|
|
GLOBAL $webDirPath;
|
|
|
|
$orgSerPath = "/var/packages/IDrive/target/etc/";
|
|
$oldServiceDir = $webDirPath.SERVICE;
|
|
//$oldServiceDir = $webDirPath."idriveIt";
|
|
|
|
$appServiceDir = $webDirPath.SERVICE;
|
|
if(file_exists($oldServiceDir) && !is_link($oldServiceDir)){
|
|
$move_cmd = "mv -f '".$oldServiceDir."/"."'* '".$orgSerPath."'";
|
|
|
|
`$move_cmd`;
|
|
`rm -rf $oldServiceDir`;
|
|
`ln -s $orgSerPath $appServiceDir`;
|
|
`chmod -R 0777 $appServiceDir`;
|
|
}
|
|
|
|
`chmod -R 0777 $orgSerPath`;
|
|
}
|
|
|
|
|
|
|