Skip to content

Commit

Permalink
generic php MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
froger-me committed Jan 9, 2024
1 parent c36aa3f commit bf536e6
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 27 deletions.
22 changes: 15 additions & 7 deletions integration/dummy-generic/dummy-generic.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#!/usr/bin/env php
<?php

require_once __DIR__ . '/wppus-api.php';

### MAIN ###

( function () {
global $argv;

$command = $argv[1] ?? '';
$license = $argv[2] ?? '';

if ( function_exists( 'dummy_generic_' . $command ) ) {
$command = 'dummy_generic_' . $command;

WPPUS_API::init();

if ( 'install' === $command ) {
if ( 'dummy_generic_install' === $command ) {
$command( $license );
} else {
$command();
Expand Down Expand Up @@ -65,11 +71,11 @@ function dummy_generic_uninstall() {

### ACTIVATING THE LICENSE ###

function dummy_generic_activate( $license_key ) {
function dummy_generic_activate() {
// If the command is "activate", the script is installed, and the license key is not empty
if ( WPPUS_API::is_installed() && ! empty( $license_key ) ) {
if ( WPPUS_API::is_installed() ) {
// Activate the license
WPPUS_API::activate( $license_key );
WPPUS_API::activate();

echo "Activated\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
Expand Down Expand Up @@ -97,7 +103,7 @@ function dummy_generic_get_update_info() {
// If the command is "get_update_info" and the script is installed
if ( WPPUS_API::is_installed() ) {
// Get the update information
$info = WPPUS_API::get_update_info();
$info = json_decode( WPPUS_API::get_update_info(), true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions.json_encode_json_encode
// Get the current version
$version = WPPUS_API::get_version();
// Get the remote version
Expand Down Expand Up @@ -131,7 +137,7 @@ function dummy_generic_update() {
// If the command is "update" and the script is installed
if ( WPPUS_API::is_installed() ) {
// Get the update information
WPPUS_API::check_for_updates();
WPPUS_API::update();

echo "Updated\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
Expand All @@ -144,12 +150,14 @@ function dummy_generic_update() {
### USAGE ###

function dummy_generic_usage() {
echo "Usage: bash \"$(dirname \"$0\")/wppus-api.sh\" [command] [arguments]\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo "Usage: ./dummy-generic.php [command] [arguments]\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo "Commands:\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo " install [license] - install the package\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo " uninstall - uninstall the package\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo " activate - activate the license\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo " deactivate - deactivate the license\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo " get_update_info - output information about the remote package update\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo " update - update the package if available\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo " status - output the package status\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo "Note: this package assumes it needs a license.\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
150 changes: 130 additions & 20 deletions integration/dummy-generic/wppus-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@
# It is just a collection of basic functions and snippets, and they do not
# perform the necessary checks to ensure data integrity ; they assume that all
# the requests are successful, and do not check paths or permissions.
# They also assume that the package necessitates a license key, stored in an
# environment variable WPPUS_GENERIC_PACKAGE_LICENSE
# They also assume that the package necessitates a license key.

# replace https://server.domain.tld/ with the URL of the server where
# WP Packages Update Server is installed in wppus.json

class WPPUS_API {

private static $config;
private static $url;
private static $package_name;
private static $package_script;
private static $script_name;
private static $zip_name;
private static $version;
private static $license_key;
private static $license_signature;
private static $domain;

private function __construct() {
public static function init() {
# load the configuration file
self::$config = json_decode( file_get_contents( __DIR__ . '/wppus.json' ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
# define the url of the server
Expand All @@ -34,16 +31,14 @@ private function __construct() {
self::$package_name = basename( __DIR__ );
# define the package script
self::$package_script = __DIR__ . '/' . basename( __DIR__ ) . '.php';
# define the current script name
self::$script_name = basename( self::$package_script );
# 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
self::$license_key = isset( $config['licenseKey'] ) ? $config['licenseKey'] : '';
self::$license_key = isset( self::$config['licenseKey'] ) ? self::$config['licenseKey'] : '';
# define license_signature from the wppus.json file
self::$license_signature = isset( $config['licenseSignature'] ) ? $config['licenseSignature'] : '';
self::$license_signature = isset( self::$config['licenseSignature'] ) ? self::$config['licenseSignature'] : '';

# define the domain
if ( 'Darwin' === PHP_OS ) {
Expand Down Expand Up @@ -85,7 +80,7 @@ public static function is_installed() {
return file_exists( __DIR__ . '/.installed' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_exists
}

### SENDNG AN API REQUEST ###
### SENDING AN API REQUEST ###

private static function send_api_request( $action, $args = array() ) {
# build the request url
Expand All @@ -110,37 +105,152 @@ private static function send_api_request( $action, $args = array() ) {
return $response;
}


### CHECKING FOR UPDATES ###

public static function check_for_updates() {
// do something
private static function check_for_updates() {
# build the request url
$endpoint = 'wppus-update-api';
$args = array(
'action' => 'get_metadata',
'package_id' => rawurlencode( self::$package_name ),
'installed_version' => rawurlencode( self::$version ),
'license_key' => rawurlencode( self::$license_key ),
'license_signature' => rawurlencode( self::$license_signature ),
'update_type' => 'Generic',
);
# make the request
$response = self::send_api_request( $endpoint, $args );
# return the response
return $response;
}

### ACTIVATING A LICENSE ###

public static function activate( $license_key ) {
// do something
public static function activate() {
$endpoint = 'wppus-license-api';
$args = array(
'action' => 'activate',
'license_key' => rawurlencode( self::$license_key ),
'allowed_domains' => rawurlencode( self::$domain ),
'package_slug' => rawurlencode( self::$package_name ),
);
# make the request
$response = self::send_api_request( $endpoint, $args );
# get the signature from the response
$signature = rawurldecode( json_decode( $response, true )['license_signature'] );
# add the license signature to wppus.json
self::$config['licenseSignature'] = $signature;
file_put_contents( __DIR__ . '/wppus.json', json_encode( self::$config, JSON_PRETTY_PRINT ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents, WordPress.WP.AlternativeFunctions.json_encode_json_encode
self::$license_signature = $signature;
}

### DEACTIVATING A LICENSE ###

public static function deactivate() {
// do something
# build the request url
$endpoint = 'wppus-license-api';
$args = array(
'action' => 'deactivate',
'license_key' => rawurlencode( self::$license_key ),
'allowed_domains' => rawurlencode( self::$domain ),
'package_slug' => rawurlencode( self::$package_name ),
);
# make the request
self::send_api_request( $endpoint, $args );
# remove the license signature from wppus.json
unset( self::$config['licenseSignature'] );
file_put_contents( __DIR__ . '/wppus.json', json_encode( self::$config, JSON_PRETTY_PRINT ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents, WordPress.WP.AlternativeFunctions.json_encode_json_encode
self::$license_signature = '';
}

### DOWNLOADING THE PACKAGE ###

private static function download_update( $response ) {
# get the download url from the response
$url = rawurldecode( json_decode( $response, true )['download_url'] );
# set the path to the downloaded file
$output_file = '/tmp/' . self::$zip_name;
# 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

### GETTING THE PACKAGE INFO ###
$response = curl_exec( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec

public static function get_update_info() {
// do something
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;
}

### GETTING THE PACKAGE VERSION ###

public static function get_version() {
return '';
# return the current version of the package
return self::$version;
}

### UPDATING THE PACKAGE ###

public static function update() {
# check for updates
$response = self::check_for_updates();
# get the version from the response
$new_version = json_decode( $response, true )['version'];

if ( version_compare( $new_version, self::$version, '>' ) ) {
# download the update
$output_file = self::download_update( $response );

# extract the zip in /tmp/$(package_name)
$zip = new ZipArchive();

$zip->open( $output_file );
$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

# move the updated main script to the current directory ;
# the updated main script is in charge of overriding the update
# scripts by moving files around after update
foreach ( glob( '/tmp/' . self::$package_name . '/*' ) as $file ) {

if ( ! is_dir( $file ) && pathinfo( $file, PATHINFO_FILENAME ) !== pathinfo( self::$package_script, PATHINFO_FILENAME ) ) {
rename( $file, dirname( self::$package_script ) . '/' . basename( $file ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename
}
}

# 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 ) ); // 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;
file_put_contents( __DIR__ . '/wppus.json', json_encode( self::$config, JSON_PRETTY_PRINT ) ); // 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

}
}

### GETTING THE PACKAGE INFO ###

public static function get_update_info() {
# get the update information
return self::check_for_updates();
}
}

0 comments on commit bf536e6

Please sign in to comment.