Skip to content

Commit

Permalink
refactor all, fix php output, mvp sh, udpate process needs test (php …
Browse files Browse the repository at this point in the history
…-> js -> py)
  • Loading branch information
froger-me committed Jan 10, 2024
1 parent 8e2fb9d commit fa6d030
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 144 deletions.
2 changes: 1 addition & 1 deletion integration/dummy-generic/dummy-generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function dummy_generic_update() {

echo "Updated\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo json_encode( WPPUS_API::get_update_info() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions.json_encode_json_encode
echo json_encode( json_decode( WPPUS_API::get_update_info(), true ), JSON_PRETTY_PRINT ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions.json_encode_json_encode
} else {
echo "The package is not installed\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
Expand Down
85 changes: 52 additions & 33 deletions integration/dummy-generic/wppus-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ async function main() {
let package_script = __filename;
// define the current script name
let script_name = path.basename(__filename);
// define the update zip archive name
let zip_name = package_name + '.zip';
// define the current version of the package from the wppus.json file
let version = config.packageData.Version;
// define license_key from the wppus.json file
Expand Down Expand Up @@ -202,7 +200,7 @@ async function main() {
// get the download url from the response
let url = decodeURIComponent(response.download_url);
// set the path to the downloaded file
let output_file = path.join(os.tmpdir(), zip_name);
let output_file = path.join(os.tmpdir(), package_name + '.zip');
// make the request
return new Promise((resolve, reject) => {
let file = fs.createWriteStream(output_file);
Expand Down Expand Up @@ -245,53 +243,74 @@ async function main() {

zip.extractAllTo('/tmp/' + package_name);

// get the permissions of the old file
let octal_mode = fs.statSync(package_script).mode.toString(8).slice(-4);
// set the permissions of the new file to the permissions of the old file
fs.chmodSync('/tmp/' + package_name + '/' + script_name, octal_mode);
if (fs.existsSync('/tmp/' + package_name)) {
// get the permissions of the current script
let octal_mode = fs.statSync(package_script).mode.toString(8).slice(-4);

// delete all files in the current directory, except for update scripts
let files = fs.readdirSync(__dirname);
// set the permissions of the new main scripts to the permissions of the
// current script
let files = fs.readdirSync('/tmp/' + package_name);

for (let i = 0; i < files.length; i++) {
let file = files[i];
for (let i = 0; i < files.length; i++) {
let file = files[i];

// check if the file does not start with `wppus`, or is .json
if (!file.startsWith("wppus") || file.endsWith(".json")) {
let deletePath = path.join(__dirname, file);
// check if the file starts with the package name
if (file.startsWith(package_name)) {
fs.chmodSync('/tmp/' + package_name + '/' + file, octal_mode);
}
}

// delete all files in the current directory, except for update scripts
files = fs.readdirSync(__dirname);

if (fs.existsSync(deletePath)) {
for (let i = 0; i < files.length; i++) {
let file = files[i];

if (parseInt(process.version.slice(1).split('.')[0]) >= 14) {
fs.rmSync(deletePath, { recursive: true, force: true });
} else {
// check if the file does not start with `wppus`, or is .json
if (!file.startsWith("wppus") || file.endsWith(".json")) {
let deletePath = path.join(__dirname, file);

if (fs.lstatSync(deletePath).isDirectory()) {
fs.rmdirSync(deletePath, { recursive: true });
if (fs.existsSync(deletePath)) {

if (parseInt(process.version.slice(1).split('.')[0]) >= 14) {
fs.rmSync(deletePath, { recursive: true, force: true });
} else {
fs.unlinkSync(deletePath);

if (fs.lstatSync(deletePath).isDirectory()) {
fs.rmdirSync(deletePath, { recursive: true });
} else {
fs.unlinkSync(deletePath);
}
}
}
}
}
}

// move the updated package files to the current directory ;
// the updated package is in charge of overriding the update script
// with new ones after update (may be contained in a subdirectory)
files = fs.readdirSync('/tmp/' + package_name);
// move the updated package files to the current directory ; the
// updated package is in charge of overriding the update scripts
// with new ones after update (may be contained in a subdirectory)
files = fs.readdirSync('/tmp/' + package_name);

for (let i = 0; i < files.length; i++) {
let file = files[i];
for (let i = 0; i < files.length; i++) {
let file = files[i];

// check if the file does not start with `wppus`, or is .json
if (!file.startsWith("wppus") || file.endsWith(".json")) {
fs.renameSync('/tmp/' + package_name + '/' + file, path.join(__dirname, file));
// check if the file does not start with `wppus`, or is .json
if (!file.startsWith("wppus") || file.endsWith(".json")) {
fs.renameSync('/tmp/' + package_name + '/' + file, path.join(__dirname, file));
}
}

// remove the directory
fs.rmdirSync('/tmp/' + package_name);
}

// remove the directory
fs.rmdirSync('/tmp/' + package_name);
// add the license key to wppus.json
config.licenseKey = license_key;
// add the license signature to wppus.json
config.licenseSignature = license_signature;
// write the new wppus.json file
fs.writeFileSync(path.join(__dirname, 'wppus.json'), JSON.stringify(config, null, 4));

// remove the zip
fs.unlinkSync(output_file);
}
Expand Down
93 changes: 53 additions & 40 deletions integration/dummy-generic/wppus-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public static function init() {
self::$package_name = basename( __DIR__ );
# define the package script
self::$package_script = __DIR__ . '/' . basename( __DIR__ ) . '.php';
# define the update zip archive name
self::$zip_name = self::$package_name . '.zip';
# define the current version of the package from the wppus.json file
self::$version = self::$config['packageData']['Version'];
# define license_key from the wppus.json file
Expand Down Expand Up @@ -86,6 +84,8 @@ private static function send_api_request( $action, $args = array() ) {
# build the request url
$full_url = self::$url . '/' . $action . '/?' . http_build_query( $args );

echo $full_url . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

# initialize cURL
$ch = curl_init(); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_init

Expand Down Expand Up @@ -166,22 +166,27 @@ public static function deactivate() {
### DOWNLOADING THE PACKAGE ###

private static function download_update( $response ) {
$response = json_decode( $response, true );
# get the download url from the response
$url = rawurldecode( json_decode( $response, true )['download_url'] );
$url = isset( $response['download_url'] ) ? rawurldecode( $response['download_url'] ) : '';
# set the path to the downloaded file
$output_file = '/tmp/' . self::$zip_name;
$output_file = '/tmp/' . self::$package_name . '.zip';

# make the request
$ch = curl_init(); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_init
curl_setopt( $ch, CURLOPT_URL, $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt
curl_setopt( $ch, CURLOPT_TIMEOUT, 20 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt
if ( filter_var( $url, FILTER_VALIDATE_URL ) ) {
$ch = curl_init(); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_init

$response = curl_exec( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec
curl_setopt( $ch, CURLOPT_URL, $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt
curl_setopt( $ch, CURLOPT_TIMEOUT, 20 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt

curl_close( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close
file_put_contents( $output_file, $response ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
$response = curl_exec( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec

curl_close( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close
file_put_contents( $output_file, $response ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
}

# return the path to the downloaded file
return $output_file;
Expand Down Expand Up @@ -213,49 +218,57 @@ public static function update() {
$zip->extractTo( '/tmp/' );
$zip->close();

# get the permissions of the old file
$octal_mode = substr( sprintf( '%o', fileperms( self::$package_script ) ), -4 );
# set the permissions of the new file to the permissions of the old file
chmod( $octal_mode, '/tmp/' . self::$package_name . '/' . self::$package_name . '.php' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod
if ( is_dir( '/tmp/' . self::$package_name ) ) {
$t_ext = PATHINFO_EXTENSION;
# get the permissions of the current script
$octal_mode = substr( sprintf( '%o', fileperms( self::$package_script ) ), -4 );

$t_ext = PATHINFO_EXTENSION;
# set the permissions of the new main scripts to the permissions of the
# current script
foreach ( glob( '/tmp/' . self::$package_name . '/*' ) as $file ) {

# delete all files in the current directory, except for update scripts
foreach ( glob( __DIR__ . '/*' ) as $file ) {
# check if the file starts with the package name
if ( substr( basename( $file ), 0, strlen( self::$package_name ) + 1 ) === self::$package_name . '.' ) {
chmod( $octal_mode, $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod
}
}

# check if the file does not start with `wppus`, or is .json
if ( 'wppus' !== substr( basename( $file ), 0, 5 ) && 'json' !== pathinfo( $file, $t_ext ) ) {
unlink( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
# delete all files in the current directory, except for update scripts
foreach ( glob( __DIR__ . '/*' ) as $file ) {

# check if the file does not start with `wppus`, or is .json
if ( 'wppus' !== substr( basename( $file ), 0, 5 ) && 'json' !== pathinfo( $file, $t_ext ) ) {
unlink( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
}
}
}

# move the updated package files to the current directory ;
# the updated package is in charge of overriding the update script
# with new ones after update (may be contained in a subdirectory)
foreach ( glob( '/tmp/' . self::$package_name . '/*' ) as $file ) {
# move the updated package files to the current directory ; the
# updated package is in charge of overriding the update scripts
# with new ones after update (may be contained in a subdirectory)
foreach ( glob( '/tmp/' . self::$package_name . '/*' ) as $file ) {

# check if the file does not start with `wppus`, or is .json
if ( 'wppus' !== substr( basename( $file ), 0, 5 ) && 'json' !== pathinfo( $file, $t_ext ) ) {
rename( $file, dirname( self::$package_script ) . '/' . basename( $file ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename
}
}

# check if the file does not start with `wppus`, or is .json
if ( 'wppus' !== substr( basename( $file ), 0, 5 ) && 'json' !== pathinfo( $file, $t_ext ) ) {
rename( $file, dirname( self::$package_script ) . '/' . basename( $file ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename
# remove the directory
foreach ( glob( '/tmp/' . self::$package_name . '/*' ) as $file ) {
unlink( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
}

rmdir( '/tmp/' . self::$package_name ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir
}

# add the license key to wppus.json
self::$config['licenseKey'] = self::$license_key;
file_put_contents( __DIR__ . '/wppus.json', json_encode( self::$config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents, WordPress.WP.AlternativeFunctions.json_encode_json_encode
# add the license signature to wppus.json
self::$config['licenseSignature'] = self::$license_signature;
# write the new version to wppus.json
file_put_contents( __DIR__ . '/wppus.json', json_encode( self::$config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents, WordPress.WP.AlternativeFunctions.json_encode_json_encode

# remove the directory
foreach ( glob( '/tmp/' . self::$package_name . '/*' ) as $file ) {
unlink( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
}

rmdir( '/tmp/' . self::$package_name ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir
# remove the zip
unlink( $output_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink

}
}

Expand Down
Loading

0 comments on commit fa6d030

Please sign in to comment.