diff --git a/gitium/functions.php b/gitium/functions.php index 96f8386..865eb5e 100644 --- a/gitium/functions.php +++ b/gitium/functions.php @@ -21,11 +21,6 @@ * @package Gitium */ -function gitium_error_log( $message ) { - if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { return; } - error_log( "gitium_error_log: $message" ); -} - function wp_content_is_versioned() { return file_exists( WP_CONTENT_DIR . '/.git' ); } @@ -234,7 +229,6 @@ function gitium_acquire_merge_lock() { return false; // timeout } } - gitium_error_log( __FUNCTION__ ); return array( $gitium_lock_path, $gitium_lock_handle ); } endif; @@ -242,7 +236,6 @@ function gitium_acquire_merge_lock() { if ( ! function_exists( 'gitium_release_merge_lock' ) ) : function gitium_release_merge_lock( $lock ) { list( $gitium_lock_path, $gitium_lock_handle ) = $lock; - gitium_error_log( __FUNCTION__ ); flock( $gitium_lock_handle, LOCK_UN ); fclose( $gitium_lock_handle ); } @@ -250,22 +243,25 @@ function gitium_release_merge_lock( $lock ) { // Merges the commits with remote and pushes them back function gitium_merge_and_push( $commits ) { - global $git; + global $git; - $lock = gitium_acquire_merge_lock() - or trigger_error( 'Timeout when gitium lock was acquired', E_USER_WARNING ); + $lock = gitium_acquire_merge_lock(); + if ( ! $lock ) { + wp_die( 'Gitium: failed to acquire merge lock. Please try again later.' ); + } - if ( ! $git->fetch_ref() ) { - return false; - } + if ( ! $git->fetch_ref() ) { + return false; + } - $merge_status = $git->merge_with_accept_mine( $commits ); + $merge_status = $git->merge_with_accept_mine( $commits ); - gitium_release_merge_lock( $lock ); + gitium_release_merge_lock( $lock ); - return $git->push() && $merge_status; + return $git->push() && $merge_status; } + function gitium_check_after_event( $plugin, $event = 'activation' ) { global $git; diff --git a/gitium/gitium-webhook.php b/gitium/gitium-webhook.php index 47b82b0..96baaec 100644 --- a/gitium/gitium-webhook.php +++ b/gitium/gitium-webhook.php @@ -21,61 +21,73 @@ * @package Gitium */ -header( 'Content-Type: text/html' ); -define( 'SHORTINIT', true ); - -$current_dir = __DIR__; - -// Define an array of possible WordPress root locations -$try_wp_roots = [ - getenv('DOCUMENT_ROOT'), - filter_input(INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_FULL_SPECIAL_CHARS), - realpath($current_dir . '/../../../../../'), - realpath($current_dir . '/../../../../'), - realpath($current_dir . '/../../../'), // Typical WordPress structure - realpath($current_dir . '/../../'), // Alternative structure - realpath($current_dir . '/../'), // Closer parent directory - $current_dir, // Fallback to current directory -]; - -$wordpress_loader = null; - -foreach ($try_wp_roots as $root) { - if ($root && file_exists($root . '/wp-load.php')) { - $wordpress_loader = $root . '/wp-load.php'; - break; - } -} - -if ($wordpress_loader) { - require_once $wordpress_loader; -} else { - die('Error: Unable to locate wp-load.php. Please verify your WordPress installation.'); -} - -require_once __DIR__ . '/functions.php'; -require_once __DIR__ . '/inc/class-git-wrapper.php'; - -$webhook_key = get_option( 'gitium_webhook_key', '' ); -$get_key = filter_input(INPUT_GET, 'key', FILTER_SANITIZE_FULL_SPECIAL_CHARS); -if ( ! empty ( $webhook_key ) && isset( $get_key ) && $webhook_key == $get_key ) : - ( '1.7' <= substr( $git->get_version(), 0, 3 ) ) or wp_die( 'Gitium plugin require minimum `git version 1.7`!' ); - - list( $git_public_key, $git_private_key ) = gitium_get_keypair(); - if ( ! $git_public_key || ! $git_private_key ) - wp_die('Not ready.', 'Not ready.', array( 'response' => 403 )); - else - $git->set_key( $git_private_key ); - - $commits = array(); - $commitmsg = sprintf( 'Merged changes from %s on %s', $_SERVER['SERVER_NAME'], date( 'm.d.Y' ) ); - - if ( $git->is_dirty() && $git->add() > 0 ) { - $commits[] = $git->commit( $commitmsg ) or trigger_error( 'Could not commit local changes!', E_USER_ERROR ); - } - gitium_merge_and_push( $commits ) or trigger_error( 'Failed merge & push: ' . serialize( $git->get_last_error() ), E_USER_ERROR ); - - wp_die( $commitmsg , 'Pull done!', array( 'response' => 200 ) ); -else : - wp_die( 'Cheating uh?', 'Cheating uh?', array( 'response' => 403 ) ); -endif; + header( 'Content-Type: text/html' ); + define( 'SHORTINIT', true ); + + $current_dir = __DIR__; + + // Define possible WordPress root locations + $try_wp_roots = [ + getenv( 'DOCUMENT_ROOT' ), + filter_input( INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_FULL_SPECIAL_CHARS ), + realpath( $current_dir . '/../../../../../' ), + realpath( $current_dir . '/../../../../' ), + realpath( $current_dir . '/../../../' ), + realpath( $current_dir . '/../../' ), + realpath( $current_dir . '/../' ), + $current_dir, + ]; + + $wordpress_loader = null; + foreach ( $try_wp_roots as $root ) { + if ( $root && file_exists( $root . '/wp-load.php' ) ) { + $wordpress_loader = $root . '/wp-load.php'; + break; + } + } + + if ( $wordpress_loader ) { + require_once $wordpress_loader; + } else { + wp_die( 'Error: Unable to locate wp-load.php. Please verify your WordPress installation.', 'Gitium Error', [ 'response' => 500 ] ); + } + + require_once __DIR__ . '/functions.php'; + require_once __DIR__ . '/inc/class-git-wrapper.php'; + + $webhook_key = get_option( 'gitium_webhook_key', '' ); + $get_key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); + + if ( empty( $webhook_key ) || $get_key !== $webhook_key ) { + wp_die( 'Cheating uh?', 'Gitium Error', [ 'response' => 403 ] ); + } + + if ( version_compare( $git->get_version(), '1.7', '<' ) ) { + wp_die( 'Gitium plugin requires minimum git version 1.7.', 'Gitium Error', [ 'response' => 500 ] ); + } + + // Load keypair + list( $git_public_key, $git_private_key ) = gitium_get_keypair(); + if ( ! $git_public_key || ! $git_private_key ) { + wp_die( 'Gitium is not ready. SSH keys are missing.', 'Gitium Error', [ 'response' => 403 ] ); + } + + $git->set_key( $git_private_key ); + + $commitmsg = sprintf( 'Merged changes from %s on %s', $_SERVER['SERVER_NAME'], date( 'm.d.Y' ) ); + $commits = []; + + if ( $git->is_dirty() && $git->add() > 0 ) { + $commit = $git->commit( $commitmsg ); + if ( ! $commit ) { + wp_die( 'Error: Could not commit local changes.', 'Gitium Error', [ 'response' => 500 ] ); + } + $commits[] = $commit; + } + + if ( ! gitium_merge_and_push( $commits ) ) { + $error = $git->get_last_error(); + wp_die( 'Error: Merge & push failed. ' . ( is_string( $error ) ? $error : '' ), 'Gitium Error', [ 'response' => 500 ] ); + } + + wp_die( esc_html( $commitmsg ), 'Pull done!', [ 'response' => 200 ] ); \ No newline at end of file diff --git a/gitium/gitium.php b/gitium/gitium.php index db0bf32..a7524fb 100644 --- a/gitium/gitium.php +++ b/gitium/gitium.php @@ -196,11 +196,6 @@ function gitium_upgrader_post_install( $res, $hook_extra, $result ) { } } - if ( WP_DEBUG ) { - error_log( __FUNCTION__ . ':hook_extra:' . serialize( $hook_extra ) ); - error_log( __FUNCTION__ . ':action:type:' . $action . ':' . $type ); - } - $git_dir = $result['destination']; $version = ''; diff --git a/gitium/inc/class-git-wrapper.php b/gitium/inc/class-git-wrapper.php index de78d79..a76ae4d 100644 --- a/gitium/inc/class-git-wrapper.php +++ b/gitium/inc/class-git-wrapper.php @@ -120,17 +120,6 @@ function _rrmdir( $dir ) { return rmdir( $dir ); } - function _log(...$args) { - if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { return; } - - $output = ''; - if (isset($args) && $args) foreach ( $args as $arg ) { - $output .= var_export($arg, true).'/n/n'; - } - - if ($output) error_log($output); - } - function _git_temp_key_file() { $key_file = tempnam( sys_get_temp_dir(), 'ssh-git' ); return $key_file; @@ -190,7 +179,6 @@ protected function _call(...$args) { } $return = (int)proc_close( $proc ); } - $this->_log( "$return $cmd", join( "\n", $response ) ); if ( ! defined( 'GIT_KEY_FILE' ) && isset( $env['GIT_KEY_FILE'] ) ) { unlink( $env['GIT_KEY_FILE'] ); } @@ -259,16 +247,11 @@ function is_dot_git_dir( $dir ) { function cleanup() { $dot_git_dir = realpath( $this->repo_dir . '/.git' ); if ( $this->is_dot_git_dir( $dot_git_dir ) && $this->_rrmdir( $dot_git_dir ) ) { - if ( WP_DEBUG ) { - error_log( "Gitium cleanup successfull. Removed '$dot_git_dir'." ); - } - return True; - } - if ( WP_DEBUG ) { - error_log( "Gitium cleanup failed. '$dot_git_dir' is not a .git dir." ); + return true; } - return False; + return false; // Failure silently handled. } + function add_remote_url( $url ) { list( $return, ) = $this->_call( 'remote', 'add', 'origin', $url ); @@ -311,7 +294,6 @@ function fetch_ref() { protected function _resolve_merge_conflicts( $message ) { list( , $changes ) = $this->status( true ); - $this->_log( $changes ); foreach ( $changes as $path => $change ) { if ( in_array( $change, array( 'UD', 'DD' ) ) ) { $this->_call( 'rm', $path );