2014-09-19 10:21:18 -04:00
< ? php
2017-08-14 16:13:46 -04:00
use ProcessMaker\Core\System ;
2014-09-19 10:21:18 -04:00
ini_set ( " max_execution_time " , 0 );
if ( ! defined ( " PM_VERSION " )) {
if ( file_exists ( PATH_METHODS . " login/version-pmos.php " )) {
include ( PATH_METHODS . " login/version-pmos.php " );
} else {
define ( " PM_VERSION " , " 2.0.0 " );
}
}
if ( ! defined ( " BUFSIZE " )) {
define ( " BUFSIZE " , 16384 );
}
function install ( $file )
{
2017-02-15 16:26:02 +00:00
2014-09-19 10:21:18 -04:00
$result = array ();
$status = 1 ;
try {
//Extract
$tar = new Archive_Tar ( $file );
$swTar = $tar -> extract ( PATH_OUTTRUNK ); //true on success, false on error. //directory for extract
//$swTar = $tar->extract(PATH_PLUGINS);
if ( ! $swTar ) {
throw ( new Exception ( " Could not extract file. " ));
}
//Upgrade
$option = array (
" http " => array (
" method " => " POST "
)
);
// Proxy settings
2017-08-14 16:13:46 -04:00
$sysConf = System :: getSystemConfiguration ();
2014-09-19 10:21:18 -04:00
if ( isset ( $sysConf [ 'proxy_host' ])) {
if ( $sysConf [ 'proxy_host' ] != '' ) {
if ( ! is_array ( $option [ 'http' ])) {
$option [ 'http' ] = array ();
}
$option [ 'http' ][ 'request_fulluri' ] = true ;
$option [ 'http' ][ 'proxy' ] = 'tcp://' . $sysConf [ 'proxy_host' ] . ( $sysConf [ 'proxy_port' ] != '' ? ':' . $sysConf [ 'proxy_port' ] : '' );
if ( $sysConf [ 'proxy_user' ] != '' ) {
if ( ! isset ( $option [ 'http' ][ 'header' ])) {
$option [ 'http' ][ 'header' ] = '' ;
}
$option [ 'http' ][ 'header' ] .= 'Proxy-Authorization: Basic ' . base64_encode ( $sysConf [ 'proxy_user' ] . ( $sysConf [ 'proxy_pass' ] != '' ? ':' . $sysConf [ 'proxy_pass' ] : '' ));
}
}
}
$context = stream_context_create ( $option );
///////
2017-10-10 12:33:25 -04:00
$fileData = @ fopen ( EnterpriseUtils :: getUrlServerName () . " /sys " . config ( " system.workspace " ) . " / " . SYS_LANG . " / " . SYS_SKIN . " /enterprise/services/processMakerUpgrade " , " rb " , false , $context );
2014-09-19 10:21:18 -04:00
if ( $fileData === false ) {
throw ( new Exception ( " Could not open services url. " ));
}
$resultAux = G :: json_decode ( stream_get_contents ( $fileData ));
if ( $resultAux -> status == " OK " ) {
$result [ " status " ] = $resultAux -> status ; //OK
$result [ " message " ] = $resultAux -> message ;
} else {
throw ( new Exception ( $resultAux -> message ));
}
} catch ( Exception $e ) {
$result [ " message " ] = $e -> getMessage ();
$status = 0 ;
}
if ( $status == 0 ) {
$result [ " status " ] = " ERROR " ;
}
return $result ;
}
$option = ( isset ( $_POST [ " option " ])) ? $_POST [ " option " ] : null ;
switch ( $option ) {
case " install " :
$uid = $_POST [ " uid " ];
$version = $_POST [ " version " ];
$versionName = $_POST [ " versionName " ];
$processMakerVersion = $_POST [ " processMakerVersion " ];
$response = array ();
$status = 1 ;
try {
if ( EnterpriseUtils :: getInternetConnection () == 0 ) {
throw ( new Exception ( " Enterprise Plugins Manager no connected to internet. " ));
}
///////
$versionName = EnterpriseUtils :: pmVersion ( $versionName );
$processMakerVersion = EnterpriseUtils :: pmVersion ( $processMakerVersion );
if ( ! version_compare ( $processMakerVersion . " " , $versionName . " " , " < " )) {
throw ( new Exception ( " The system can't be upgraded to a previous version. " ));
}
///////
2017-08-11 16:24:12 -04:00
$licenseManager = & PmLicenseManager :: getSingleton ();
2014-09-19 10:21:18 -04:00
$server = isset ( $licenseManager -> server ) ? $licenseManager -> server : '' ;
2015-05-10 16:32:01 -04:00
$workspace = ( isset ( $licenseManager -> workspace )) ? $licenseManager -> workspace : 'pmLicenseSrv' ;
2014-09-19 10:21:18 -04:00
2015-05-10 16:32:01 -04:00
$url = " http:// $server /sys " . $workspace . " /en/green/services/rest " ;
2014-09-19 10:21:18 -04:00
if ( EnterpriseUtils :: checkConnectivity ( $url ) == false ) {
throw ( new Exception ( " Server ' $server ' not available. " ));
}
///////
2015-03-26 11:22:37 -04:00
$boundary = " --------------------- " . substr ( G :: encryptOld ( rand ( 0 , 32000 )), 0 , 10 );
2014-09-19 10:21:18 -04:00
$data = null ;
$data = $data . " -- $boundary\n " ;
$data = $data . " Content-Disposition: form-data; name= \" action \" \n \n " . " getPM " . " \n " ;
$data = $data . " -- $boundary\n " ;
$data = $data . " Content-Disposition: form-data; name= \" OBJ_UID \" \n \n " . $uid . " \n " ;
$data = $data . " -- $boundary\n " ;
$data = $data . " Content-Disposition: form-data; name= \" OBJ_VERSION \" \n \n " . $version . " \n " ;
$data = $data . " -- $boundary\n " ;
$option = array (
" http " => array (
" method " => " POST " ,
" header " => " Content-Type: multipart/form-data; boundary= " . $boundary ,
" content " => $data
)
);
// Proxy settings
2017-08-14 16:13:46 -04:00
$sysConf = System :: getSystemConfiguration ();
2014-09-19 10:21:18 -04:00
if ( isset ( $sysConf [ 'proxy_host' ])) {
if ( $sysConf [ 'proxy_host' ] != '' ) {
if ( ! is_array ( $option [ 'http' ])) {
$option [ 'http' ] = array ();
}
$option [ 'http' ][ 'request_fulluri' ] = true ;
$option [ 'http' ][ 'proxy' ] = 'tcp://' . $sysConf [ 'proxy_host' ] . ( $sysConf [ 'proxy_port' ] != '' ? ':' . $sysConf [ 'proxy_port' ] : '' );
if ( $sysConf [ 'proxy_user' ] != '' ) {
if ( ! isset ( $option [ 'http' ][ 'header' ])) {
$option [ 'http' ][ 'header' ] = '' ;
}
$option [ 'http' ][ 'header' ] .= 'Proxy-Authorization: Basic ' . base64_encode ( $sysConf [ 'proxy_user' ] . ( $sysConf [ 'proxy_pass' ] != '' ? ':' . $sysConf [ 'proxy_pass' ] : '' ));
}
}
}
$context = stream_context_create ( $option );
///////
$fileData = @ fopen ( $url , " rb " , false , $context );
if ( $fileData === false ) {
throw ( new Exception ( " Could not open download url. " ));
}
//Try to get the download size and filename (ok if it fails)
$meta = stream_get_meta_data ( $fileData );
$fileName = null ;
$fileContentType = null ;
$fileLength = 0 ;
if ( $meta [ " wrapper_type " ] == " http " ) {
foreach ( $meta [ " wrapper_data " ] as $info ) {
$info = explode ( " : " , $info );
$infoVariable = ( isset ( $info [ 0 ])) ? trim ( $info [ 0 ]) : null ;
$infoValue = ( isset ( $info [ 1 ])) ? trim ( $info [ 1 ]) : null ;
if ( preg_match ( " /^.*Content-Type.* $ / " , $infoVariable )) {
$fileContentType = $infoValue ;
}
if ( strcasecmp ( $infoVariable , " Content-Length " ) == 0 ) {
$fileLength = intval ( $infoValue );
}
if ( strcasecmp ( $infoVariable , " Content-Disposition " ) == 0 ) {
foreach ( explode ( " ; " , $infoValue ) as $params ) {
$params = explode ( " = " , $params );
if ( count ( $params ) <= 1 ) {
continue ;
}
if ( strcasecmp ( trim ( $params [ 0 ]), " filename " ) == 0 ) {
$fileName = trim ( $params [ 1 ], " \" " );
}
}
}
}
}
if ( preg_match ( " /^.*json.* $ /i " , $fileContentType )) {
$r = G :: json_decode ( stream_get_contents ( $fileData ));
if ( $r -> status == " ERROR " ) {
throw ( new Exception ( $r -> message ));
}
}
///////
$dir = PATH_DATA . " upgrade " . PATH_SEP . " processmaker " ;
G :: rm_dir ( $dir );
G :: mk_dir ( $dir );
if ( ! file_exists ( $dir )) {
throw ( new Exception ( " Could not create destination directory. " ));
}
///////
$fileName = $dir . PATH_SEP . $fileName ;
$file = @ fopen ( $fileName , " wb " );
if ( $file === false ) {
throw ( new Exception ( " Could not open destination file. " ));
}
while ( ! feof ( $fileData )) {
$data = fread ( $fileData , BUFSIZE );
//Just to be safe, check all error conditions
if ( $data === " " || $data === false ) {
break ;
}
if ( fwrite ( $file , $data ) === false ) {
break ;
}
}
fclose ( $file );
fclose ( $fileData );
///////
$path = PATH_TRUNK ;
//$path = PATH_OUTTRUNK;
if ( EnterpriseUtils :: checkFolderPermissions ( $path , true ) == false ) {
$str = $path . " " . " directory, its sub-directories or file is not writable. Read the wiki for <a href= \" http://wiki.processmaker.com/index.php/Upgrading_ProcessMaker \" onclick= \" window.open(this.href, \ '_blank \ '); return (false); \" >Upgrading ProcessMaker</a>.<br /> The file is downloaded in " . $fileName . " <br /> " ;
throw ( new Exception ( $str ));
}
///////
$result = install ( $fileName );
if ( $result [ " status " ] == " OK " ) {
$response [ " status " ] = $result [ " status " ]; //OK
$response [ " message " ] = $result [ " message " ];
2014-10-10 12:19:34 -04:00
G :: auditLog ( " InstallPlugin " , " Plugin Name: " . $file );
2014-09-19 10:21:18 -04:00
} else {
throw ( new Exception ( $result [ " message " ]));
}
} catch ( Exception $e ) {
$response [ " message " ] = $e -> getMessage ();
$status = 0 ;
}
if ( $status == 0 ) {
$response [ " status " ] = " ERROR " ;
}
echo G :: json_encode ( $response );
break ;
case " list " :
$status = 1 ;
2014-10-23 12:31:40 -04:00
$response = new stdclass ();
$response -> status = 'OK' ;
2014-09-19 10:21:18 -04:00
try {
if ( EnterpriseUtils :: getInternetConnection () == 0 ) {
throw ( new Exception ( " Enterprise Plugins Manager no connected to internet. " ));
}
///////
2017-08-11 16:24:12 -04:00
$licenseManager = & PmLicenseManager :: getSingleton ();
2014-09-19 10:21:18 -04:00
$server = ( isset ( $licenseManager -> server )) ? $licenseManager -> server : '' ;
2015-05-10 16:32:01 -04:00
$workspace = ( isset ( $licenseManager -> workspace )) ? $licenseManager -> workspace : 'pmLicenseSrv' ;
2014-09-19 10:21:18 -04:00
2015-05-10 16:32:01 -04:00
$url = " http:// $server /sys " . $workspace . " /en/green/services/rest " ;
2014-09-19 10:21:18 -04:00
if ( EnterpriseUtils :: checkConnectivity ( $url ) == false ) {
throw ( new Exception ( " Server ' $server ' not available. " ));
}
///////
$option = array (
" http " => array (
" method " => " POST " ,
" header " => " Content-type: application/x-www-form-urlencoded \r \n " ,
" content " => http_build_query (
array (
" action " => " getPMList "
)
)
)
);
// Proxy settings
2017-08-14 16:13:46 -04:00
$sysConf = System :: getSystemConfiguration ();
2014-09-19 10:21:18 -04:00
if ( isset ( $sysConf [ 'proxy_host' ])) {
if ( $sysConf [ 'proxy_host' ] != '' ) {
if ( ! is_array ( $option [ 'http' ])) {
$option [ 'http' ] = array ();
}
$option [ 'http' ][ 'request_fulluri' ] = true ;
$option [ 'http' ][ 'proxy' ] = 'tcp://' . $sysConf [ 'proxy_host' ] . ( $sysConf [ 'proxy_port' ] != '' ? ':' . $sysConf [ 'proxy_port' ] : '' );
if ( $sysConf [ 'proxy_user' ] != '' ) {
if ( ! isset ( $option [ 'http' ][ 'header' ])) {
$option [ 'http' ][ 'header' ] = '' ;
}
$option [ 'http' ][ 'header' ] .= 'Proxy-Authorization: Basic ' . base64_encode ( $sysConf [ 'proxy_user' ] . ( $sysConf [ 'proxy_pass' ] != '' ? ':' . $sysConf [ 'proxy_pass' ] : '' ));
}
}
}
$context = stream_context_create ( $option );
$results = file_get_contents ( $url , false , $context );
$results = G :: json_decode ( $results );
2015-05-10 16:32:01 -04:00
$results = isset ( $results [ 0 ]) ? $results [ 0 ] : array ();
2014-09-19 10:21:18 -04:00
$pmVersion = EnterpriseUtils :: pmVersion ( PM_VERSION );
$versions = array ();
foreach ( $results as $key => $result ) {
$version = EnterpriseUtils :: pmVersion ( $result -> OBJ_VERSION_NAME );
if ( version_compare ( $pmVersion . " " , $version . " " , " < " )) {
$versions [] = $result ;
}
}
if ( isset ( $results [ 0 ])) {
$results [ 0 ] -> OBJ_VERSION_NAME .= " (Stable) " ;
}
$response -> status = " OK " ;
$response -> results = $versions ;
} catch ( Exception $e ) {
$response -> message = $e -> getMessage ();
$status = 0 ;
}
if ( $status == 0 ) {
$response -> status = " ERROR " ;
}
echo G :: json_encode ( $response );
break ;
}