@ -23,14 +23,14 @@ define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
require_once INSTALL_PATH . 'program/include/clisetup.php' ;
if ( !function_exists( 'exec' ) ) {
rcube::raise_error( "PHP exec() function is required. Check disable_functions in php.ini." , false, true ) ;
rcube::raise_error( "PHP exec() function is required. Check disable_functions in php.ini." , false, true ) ;
}
$cfgfile = INSTALL_PATH . 'jsdeps.json' ;
$SOURCES = json_decode( file_get_contents( $cfgfile ) , true ) ;
if ( empty( $SOURCES [ 'dependencies' ] ) ) {
rcube::raise_error( " Failed to read dependencies list from $cfgfile " , false, true ) ;
rcube::raise_error( " Failed to read dependencies list from $cfgfile " , false, true ) ;
}
$CURL = trim( ` which curl` ) ;
@ -39,13 +39,13 @@ $UNZIP = trim(`which unzip`);
$FILEINFO = trim( ` which file` ) ;
if ( ( $CACHEDIR = getenv( "CACHEDIR" ) ) && is_writeable( $CACHEDIR ) ) {
// use $CACHEDIR
// use $CACHEDIR
}
else if ( is_writeable( INSTALL_PATH . 'temp/js_cache' ) || @mkdir( INSTALL_PATH . 'temp/js_cache' , 0774, true ) ) {
$CACHEDIR = INSTALL_PATH . 'temp/js_cache' ;
$CACHEDIR = INSTALL_PATH . 'temp/js_cache' ;
}
else {
$CACHEDIR = sys_get_temp_dir( ) ;
$CACHEDIR = sys_get_temp_dir( ) ;
}
@ -110,43 +110,43 @@ EOL;
*/
function fetch_from_source( $package , $useCache = true, & $filetype = null)
{
global $CURL , $WGET ;
global $CURL , $WGET ;
$cache_file = extract_filetype( $package , $filetype ) ;
$cache_file = extract_filetype( $package , $filetype ) ;
if ( !is_readable( $cache_file ) || !$useCache ) {
if ( empty( $CURL ) && empty( $WGET ) ) {
rcube::raise_error( "Required 'wget' or 'curl' program not found." , false, true ) ;
}
if ( !is_readable( $cache_file ) || !$useCache ) {
if ( empty( $CURL ) && empty( $WGET ) ) {
rcube::raise_error( "Required 'wget' or 'curl' program not found." , false, true ) ;
}
$url = str_replace( '$v' , $package [ 'version' ] , $package [ 'url' ] ) ;
$url = str_replace( '$v' , $package [ 'version' ] , $package [ 'url' ] ) ;
echo " Fetching $url \n " ;
echo " Fetching $url \n " ;
if ( $CURL )
exec( sprintf( '%s -L -s %s -o %s' , $CURL , escapeshellarg( $url ) , $cache_file ) , $out , $retval ) ;
else
exec( sprintf( '%s -q %s -O %s' , $WGET , escapeshellarg( $url ) , $cache_file ) , $out , $retval ) ;
if ( $CURL )
exec( sprintf( '%s -L -s %s -o %s' , $CURL , escapeshellarg( $url ) , $cache_file ) , $out , $retval ) ;
else
exec( sprintf( '%s -q %s -O %s' , $WGET , escapeshellarg( $url ) , $cache_file ) , $out , $retval ) ;
// Try Github API as a fallback ( #6248)
if ( $retval != = 0 && $package [ 'api_url' ] ) {
$url = str_replace( '$v' , $package [ 'version' ] , $package [ 'api_url' ] ) ;
$header = 'Accept:application/vnd.github.v3.raw' ;
// Try Github API as a fallback ( #6248)
if ( $retval != = 0 && $package [ 'api_url' ] ) {
$url = str_replace( '$v' , $package [ 'version' ] , $package [ 'api_url' ] ) ;
$header = 'Accept:application/vnd.github.v3.raw' ;
rcube::raise_error( " Fetching failed. Using Github API on $url " ) ;
rcube::raise_error( " Fetching failed. Using Github API on $url " ) ;
if ( $CURL )
exec( sprintf( '%s -L -H %s -s %s -o %s' , $CURL , escapeshellarg( $header ) , escapeshellarg( $url ) , $cache_file ) , $out , $retval ) ;
else
exec( sprintf( '%s --header %s -q %s -O %s' , $WGET , escapeshellarg( $header ) , escapeshellarg( $url ) , $cache_file ) , $out , $retval ) ;
}
if ( $CURL )
exec( sprintf( '%s -L -H %s -s %s -o %s' , $CURL , escapeshellarg( $header ) , escapeshellarg( $url ) , $cache_file ) , $out , $retval ) ;
else
exec( sprintf( '%s --header %s -q %s -O %s' , $WGET , escapeshellarg( $header ) , escapeshellarg( $url ) , $cache_file ) , $out , $retval ) ;
}
if ( $retval != = 0) {
rcube::raise_error( " Failed to download source file from $url " , false, true ) ;
if ( $retval != = 0) {
rcube::raise_error( " Failed to download source file from $url " , false, true ) ;
}
}
}
return $cache_file ;
return $cache_file ;
}
/**
@ -154,22 +154,22 @@ function fetch_from_source($package, $useCache = true, &$filetype = null)
*/
function extract_filetype( $package , & $filetype = null)
{
global $FILEINFO , $CACHEDIR ;
global $FILEINFO , $CACHEDIR ;
$filetype = pathinfo( $package [ 'url' ] , PATHINFO_EXTENSION) ?: 'tmp' ;
$cache_file = $CACHEDIR . '/' . $package [ 'lib' ] . '-' . $package [ 'version' ] . '.' . $filetype ;
$filetype = pathinfo( $package [ 'url' ] , PATHINFO_EXTENSION) ?: 'tmp' ;
$cache_file = $CACHEDIR . '/' . $package [ 'lib' ] . '-' . $package [ 'version' ] . '.' . $filetype ;
if ( empty( $FILEINFO ) ) {
rcube::raise_error( "Required program 'file' not found." , false, true ) ;
}
if ( empty( $FILEINFO ) ) {
rcube::raise_error( "Required program 'file' not found." , false, true ) ;
}
// detect downloaded/cached file type
exec( sprintf( '%s -b %s' , $FILEINFO , $cache_file ) , $out ) ;
if ( stripos( $out [ 0] , 'zip' ) = = = 0) {
$filetype = 'zip' ;
}
// detect downloaded/cached file type
exec( sprintf( '%s -b %s' , $FILEINFO , $cache_file ) , $out ) ;
if ( stripos( $out [ 0] , 'zip' ) = = = 0) {
$filetype = 'zip' ;
}
return $cache_file ;
return $cache_file ;
}
/**
@ -177,38 +177,38 @@ function extract_filetype($package, &$filetype = null)
*/
function compose_destfile( $package , $srcfile )
{
global $LICENSES ;
$header = sprintf( "/**\n * %s - v%s\n *\n" , $package [ 'name' ] , $package [ 'version' ] ) ;
if ( !empty( $package [ 'source' ] ) ) {
$header .= " * @source " . str_replace( '$v' , $package [ 'version' ] , $package [ 'source' ] ) . "\n" ;
$header .= " *\n" ;
}
if ( !empty( $package [ 'license' ] ) && isset( $LICENSES [ $package [ 'license' ] ] ) ) {
$header .= " * @licstart The following is the entire license notice for the\n" ;
$header .= " * JavaScript code in this file.\n" ;
$header .= " *\n" ;
if ( !empty( $package [ 'copyright' ] ) ) {
$header .= " * " . $package [ 'copyright' ] . "\n" ;
$header .= " *\n" ;
global $LICENSES ;
$header = sprintf( "/**\n * %s - v%s\n *\n" , $package [ 'name' ] , $package [ 'version' ] ) ;
if ( !empty( $package [ 'source' ] ) ) {
$header .= " * @source " . str_replace( '$v' , $package [ 'version' ] , $package [ 'source' ] ) . "\n" ;
$header .= " *\n" ;
}
$header .= $LICENSES [ $package [ 'license' ] ] ;
$header .= " *\n" ;
$header .= " * @licend The above is the entire license notice\n" ;
$header .= " * for the JavaScript code in this file.\n" ;
}
if ( !empty( $package [ 'license' ] ) && isset( $LICENSES [ $package [ 'license' ] ] ) ) {
$header .= " * @licstart The following is the entire license notice for the\n" ;
$header .= " * JavaScript code in this file.\n" ;
$header .= " *\n" ;
if ( !empty( $package [ 'copyright' ] ) ) {
$header .= " * " . $package [ 'copyright' ] . "\n" ;
$header .= " *\n" ;
}
$header .= $LICENSES [ $package [ 'license' ] ] ;
$header .= " *\n" ;
$header .= " * @licend The above is the entire license notice\n" ;
$header .= " * for the JavaScript code in this file.\n" ;
}
$header .= " */\n" ;
$header .= " */\n" ;
if ( file_put_contents( INSTALL_PATH . $package [ 'dest' ] , $header . file_get_contents( $srcfile ) ) ) {
echo "Wrote file " . INSTALL_PATH . $package [ 'dest' ] . "\n" ;
}
else {
rcube::raise_error( "Failed to write destination file " . INSTALL_PATH . $package [ 'dest' ] , false, true ) ;
}
if ( file_put_contents( INSTALL_PATH . $package [ 'dest' ] , $header . file_get_contents( $srcfile ) ) ) {
echo "Wrote file " . INSTALL_PATH . $package [ 'dest' ] . "\n" ;
}
else {
rcube::raise_error( "Failed to write destination file " . INSTALL_PATH . $package [ 'dest' ] , false, true ) ;
}
}
/**
@ -216,94 +216,94 @@ function compose_destfile($package, $srcfile)
*/
function extract_zipfile( $package , $srcfile )
{
global $UNZIP , $CACHEDIR ;
if ( empty( $UNZIP ) ) {
rcube::raise_error( "Required 'unzip' program not found." , false, true ) ;
}
$destdir = INSTALL_PATH . $package [ 'dest' ] ;
if ( !is_dir( $destdir ) ) {
mkdir( $destdir , 0775, true ) ;
}
if ( !is_writeable( $destdir ) ) {
rcube::raise_error( " Cannot write to destination directory: $destdir " , false, true ) ;
}
// pick files from zip archive
if ( !empty( $package [ 'pick' ] ) ) {
foreach ( $package [ 'pick' ] as $pattern ) {
echo " Extracting files $pattern into $destdir \n " ;
exec( sprintf( '%s -o %s %s -d %s' , $UNZIP , escapeshellarg( $srcfile ) , escapeshellarg( $pattern ) , $destdir ) , $out , $retval ) ;
if ( $retval != = 0) {
rcube::raise_error( " Failed to unpack $pattern ; " . join( '; ' . $out ) ) ;
}
}
}
// unzip the archive and map source to dest files/directories
else if ( !empty( $package [ 'map' ] ) ) {
$extract = $CACHEDIR . '/' . $package [ 'lib' ] . '-extract' ;
if ( !is_dir( $extract ) ) {
mkdir( $extract , 0774, true ) ;
}
global $UNZIP , $CACHEDIR ;
$zip_command = '%s -' . ( $package [ 'flat' ] ? 'j' : 'o' ) . ' %s -d %s' ;
exec( sprintf( $zip_command , $UNZIP , escapeshellarg( $srcfile ) , $extract ) , $out , $retval ) ;
if ( empty( $UNZIP ) ) {
rcube::raise_error( "Required 'unzip' program not found." , false, true ) ;
}
// get the root folder of the extracted package
$extract_tree = glob( " $extract /* " , GLOB_ONLYDIR) ;
$sourcedir = count( $extract_tree ) ? $extract_tree [ 0] : $extract ;
$destdir = INSTALL_PATH . $package [ 'dest' ] ;
if ( !is_dir( $destdir ) ) {
mkdir( $destdir , 0775, true ) ;
}
foreach ( $package [ 'map' ] as $src = > $dest ) {
echo " Installing $sourcedir / $src into $destdir / $dest \n " ;
if ( !is_writeable( $destdir ) ) {
rcube::raise_error( " Cannot write to destination directory: $destdir " , false, true ) ;
}
// make sure the destination' s parent directory exists
if ( strpos( $dest , '/' ) != = false ) {
$parentdir = dirname( $destdir . '/' . $dest ) ;
if ( !is_dir( $parentdir ) ) {
mkdir( $parentdir , 0775, true ) ;
// pick files from zip archive
if ( !empty( $package [ 'pick' ] ) ) {
foreach ( $package [ 'pick' ] as $pattern ) {
echo " Extracting files $pattern into $destdir \n " ;
exec( sprintf( '%s -o %s %s -d %s' , $UNZIP , escapeshellarg( $srcfile ) , escapeshellarg( $pattern ) , $destdir ) , $out , $retval ) ;
if ( $retval != = 0) {
rcube::raise_error( " Failed to unpack $pattern ; " . join( '; ' . $out ) ) ;
}
}
}
// unzip the archive and map source to dest files/directories
else if ( !empty( $package [ 'map' ] ) ) {
$extract = $CACHEDIR . '/' . $package [ 'lib' ] . '-extract' ;
if ( !is_dir( $extract ) ) {
mkdir( $extract , 0774, true ) ;
}
}
// avoid copying source directory as a child into destination
if ( is_dir( $sourcedir . '/' . $src ) && is_dir( $destdir . '/' . $dest ) ) {
exec( sprintf( 'rm -rf %s/%s' , $destdir , $dest ) ) ;
}
$zip_command = '%s -' . ( $package [ 'flat' ] ? 'j' : 'o' ) . ' %s -d %s' ;
exec( sprintf( $zip_command , $UNZIP , escapeshellarg( $srcfile ) , $extract ) , $out , $retval ) ;
// get the root folder of the extracted package
$extract_tree = glob( " $extract /* " , GLOB_ONLYDIR) ;
$sourcedir = count( $extract_tree ) ? $extract_tree [ 0] : $extract ;
foreach ( $package [ 'map' ] as $src = > $dest ) {
echo " Installing $sourcedir / $src into $destdir / $dest \n " ;
// make sure the destination' s parent directory exists
if ( strpos( $dest , '/' ) != = false ) {
$parentdir = dirname( $destdir . '/' . $dest ) ;
if ( !is_dir( $parentdir ) ) {
mkdir( $parentdir , 0775, true ) ;
}
}
// avoid copying source directory as a child into destination
if ( is_dir( $sourcedir . '/' . $src ) && is_dir( $destdir . '/' . $dest ) ) {
exec( sprintf( 'rm -rf %s/%s' , $destdir , $dest ) ) ;
}
exec( sprintf( 'mv -f %s/%s %s/%s' , $sourcedir , $src , $destdir , $dest ) , $out , $retval ) ;
if ( $retval != = 0) {
rcube::raise_error( " Failed to move $src into $destdir / $dest ; " . join( '; ' . $out ) ) ;
}
}
exec( sprintf( 'mv -f %s/%s %s/%s' , $sourcedir , $src , $destdir , $dest ) , $out , $retval ) ;
if ( $retval != = 0) {
rcube::raise_error( " Failed to move $src into $destdir / $dest ; " . join( '; ' . $out ) ) ;
}
// remove temp extraction dir
exec( 'rm -rf ' . $extract ) ;
}
// remove temp extraction dir
exec( 'rm -rf ' . $extract ) ;
}
// extract the archive into the destination directory
else {
echo " Extracting zip archive into $destdir \n " ;
exec( sprintf( '%s -o %s -d %s' , $UNZIP , escapeshellarg( $srcfile ) , $destdir ) , $out , $retval ) ;
if ( $retval != = 0) {
rcube::raise_error( " Failed to unzip $srcfile ; " . join( '; ' . $out ) ) ;
// extract the archive into the destination directory
else {
echo " Extracting zip archive into $destdir \n " ;
exec( sprintf( '%s -o %s -d %s' , $UNZIP , escapeshellarg( $srcfile ) , $destdir ) , $out , $retval ) ;
if ( $retval != = 0) {
rcube::raise_error( " Failed to unzip $srcfile ; " . join( '; ' . $out ) ) ;
}
}
}
// remove some files from the destination
if ( !empty( $package [ 'omit' ] ) ) {
foreach ( ( array) $package [ 'omit' ] as $glob ) {
exec( sprintf( 'rm -rf %s/%s' , $destdir , escapeshellarg( $glob ) ) ) ;
// remove some files from the destination
if ( !empty( $package [ 'omit' ] ) ) {
foreach ( ( array) $package [ 'omit' ] as $glob ) {
exec( sprintf( 'rm -rf %s/%s' , $destdir , escapeshellarg( $glob ) ) ) ;
}
}
}
// prepend license header to extracted files
if ( !empty( $package [ 'addlicense' ] ) ) {
foreach ( ( array) $package [ 'addlicense' ] as $filename ) {
$pkg = $packag e;
$pkg [ 'dest' ] = $package [ 'dest' ] . '/' . $filename ;
compose_destfile( $pkg , $destdir . '/' . $filename ) ;
// prepend license header to extracted files
if ( !empty( $package [ 'addlicense' ] ) ) {
foreach ( ( array) $package [ 'addlicense' ] as $filename ) {
$pkg = $package ;
$pkg [ 'dest' ] = $packag e[ 'dest' ] . '/' . $filenam e;
compose_destfile( $pkg , $destdir . '/' . $filename ) ;
}
}
}
}
/**
@ -311,16 +311,16 @@ function extract_zipfile($package, $srcfile)
*/
function delete_destfile( $package )
{
$destdir = INSTALL_PATH . ( $package [ 'rm' ] ?: $package [ 'dest' ] ) ;
$destdir = INSTALL_PATH . ( $package [ 'rm' ] ?: $package [ 'dest' ] ) ;
if ( file_exists( $destdir ) ) {
if ( PHP_OS = = = 'Windows' ) {
exec( sprintf( "rd /s /q %s" , escapeshellarg( $destdir ) ) ) ;
}
else {
exec( sprintf( "rm -rf %s" , escapeshellarg( $destdir ) ) ) ;
if ( file_exists( $destdir ) ) {
if ( PHP_OS = = = 'Windows' ) {
exec( sprintf( "rd /s /q %s" , escapeshellarg( $destdir ) ) ) ;
}
else {
exec( sprintf( "rm -rf %s" , escapeshellarg( $destdir ) ) ) ;
}
}
}
}
@ -332,44 +332,44 @@ $WHAT = $args[0];
$useCache = !$args [ 'force' ] && !$args [ 'get' ] ;
if ( !$args [ 'get' ] && !$args [ 'extract' ] && !$args [ 'delete' ] ) {
$args [ 'get' ] = $args [ 'extract' ] = 1;
$args [ 'get' ] = $args [ 'extract' ] = 1;
}
foreach ( $SOURCES [ 'dependencies' ] as $package ) {
if ( !isset( $package [ 'name' ] ) ) {
$package [ 'name' ] = $package [ 'lib' ] ;
}
if ( $WHAT && $package [ 'lib' ] != = $WHAT ) {
continue ;
}
if ( $args [ 'delete' ] ) {
delete_destfile( $package ) ;
continue ;
}
if ( $args [ 'get' ] ) {
$srcfile = fetch_from_source( $package , $useCache , $filetype ) ;
}
else {
$srcfile = extract_filetype( $package , $filetype ) ;
}
if ( !empty( $package [ 'sha1' ] ) && ( $sum = sha1_file( $srcfile ) ) != = $package [ 'sha1' ] ) {
rcube::raise_error( " Incorrect sha1 sum of $srcfile . Expected: { $package ['sha1']}, got: $sum " , false, true ) ;
}
if ( $args [ 'extract' ] ) {
echo " Installing { $package ['name']}...\n " ;
if ( $filetype = = = 'zip' ) {
extract_zipfile( $package , $srcfile ) ;
if ( !isset( $package [ 'name' ] ) ) {
$package [ 'name' ] = $package [ 'lib' ] ;
}
if ( $WHAT && $package [ 'lib' ] != = $WHAT ) {
continue ;
}
if ( $args [ 'delete' ] ) {
delete_destfile( $package ) ;
continue ;
}
if ( $args [ 'get' ] ) {
$srcfile = fetch_from_source( $package , $useCache , $filetype ) ;
}
else {
compose_destfile( $package , $srcfile ) ;
$srcfile = extract_filetype( $package , $filetype ) ;
}
if ( !empty( $package [ 'sha1' ] ) && ( $sum = sha1_file( $srcfile ) ) != = $package [ 'sha1' ] ) {
rcube::raise_error( " Incorrect sha1 sum of $srcfile . Expected: { $package ['sha1']}, got: $sum " , false, true ) ;
}
echo "Done.\n" ;
}
if ( $args [ 'extract' ] ) {
echo " Installing { $package ['name']}...\n " ;
if ( $filetype = = = 'zip' ) {
extract_zipfile( $package , $srcfile ) ;
}
else {
compose_destfile( $package , $srcfile ) ;
}
echo "Done.\n" ;
}
}