Documentation
¶
Overview ¶
Package api provides the public API for using the PHP obfuscator as a library.
This package allows users to obfuscate PHP code programmatically using the same techniques available in the command-line interface. The API provides methods for obfuscating PHP code strings, files, and directories.
Basic usage example:
obf, err := api.NewObfuscator(api.Options{ConfigPath: "config.yaml"}) if err != nil { log.Fatalf("Failed to create obfuscator: %v", err) } result, err := obf.ObfuscateCode("<?php echo 'Hello World'; ?>") if err != nil { log.Fatalf("Failed to obfuscate code: %v", err) } fmt.Println(result) // Prints obfuscated PHP code
Example ¶
Example shows basic usage of the PHP obfuscator library.
package main import ( "fmt" "log" "github.com/whit3rabbit/phpmixer/internal/config" "github.com/whit3rabbit/phpmixer/pkg/api" ) func main() { // Suppress default informational messages for example config.Testing = true defer func() { config.Testing = false }() // Create an obfuscator with default options and set to silent obf, err := api.NewObfuscator(api.Options{ Silent: true, // This will suppress most verbose output }) if err != nil { log.Fatalf("Failed to create obfuscator: %v", err) } // Obfuscate some PHP code _, err = obf.ObfuscateCode("<?php echo 'Hello World'; ?>") if err != nil { log.Fatalf("Failed to obfuscate code: %v", err) } fmt.Println("PHP code was successfully obfuscated") }
Output: PHP code was successfully obfuscated
Example (CreateCustomConfig) ¶
Example_createCustomConfig demonstrates how to create a configuration file programmatically.
package main import ( "fmt" "log" "os" "path/filepath" "github.com/whit3rabbit/phpmixer/internal/config" "github.com/whit3rabbit/phpmixer/pkg/api" ) func main() { // Suppress default informational messages for example config.Testing = true defer func() { config.Testing = false }() // Create a basic config file with common obfuscation settings configContent := `# PHP Obfuscator Configuration silent: false scramble_mode: "identifier" scramble_length: 5 strip_comments: true obfuscate_string_literal: true string_obfuscation_technique: "base64" obfuscate_variable_name: true obfuscate_function_name: true obfuscate_class_name: true obfuscate_control_flow: true control_flow_max_nesting_depth: 1 control_flow_random_conditions: true ` // Create a temporary file for the example tempDir, err := os.MkdirTemp("", "obfuscator-example-*") if err != nil { log.Fatalf("Failed to create temp directory: %v", err) } defer os.RemoveAll(tempDir) // Clean up // Save the config to a file configPath := filepath.Join(tempDir, "config.yaml") err = os.WriteFile(configPath, []byte(configContent), 0644) if err != nil { log.Fatalf("Failed to write config file: %v", err) } // Initialize the obfuscator with the custom config _, err = api.NewObfuscator(api.Options{ ConfigPath: configPath, }) if err != nil { log.Fatalf("Failed to create obfuscator: %v", err) } fmt.Println("Created obfuscator with custom config file") }
Output: Created obfuscator with custom config file
Example (PrintInfo) ¶
ExamplePrintInfo demonstrates how to use the PrintInfo function which respects the config.Testing flag to control output.
package main import ( "github.com/whit3rabbit/phpmixer/internal/config" "github.com/whit3rabbit/phpmixer/pkg/api" ) func main() { // For this specific example, we want to show how PrintInfo works // so we ensure Testing is false initially config.Testing = false // Print information that will be visible during normal operation // but suppressed during testing api.PrintInfo("Starting obfuscation process...\n") // This would create an obfuscator with silent mode enabled // which is different from Testing mode // Temporarily set Testing to true to suppress internal messages config.Testing = true _, _ = api.NewObfuscator(api.Options{ Silent: true, // This controls the obfuscator's own output }) config.Testing = false // Even with Silent=true on the obfuscator, PrintInfo will still // output unless Testing=true api.PrintInfo("Obfuscator created with ID: %s\n", "abc123") // To completely silence all output for testing: // config.Testing = true // Reset to default config.Testing = false }
Output: Starting obfuscation process... Obfuscator created with ID: abc123
Index ¶
- func PrintInfo(format string, args ...interface{})
- type Obfuscator
- func (o *Obfuscator) LoadContext(baseDir string) error
- func (o *Obfuscator) LookupObfuscatedName(name string, typeStr string) (string, error)
- func (o *Obfuscator) ObfuscateCode(code string) (string, error)
- func (o *Obfuscator) ObfuscateDirectory(inputDir, outputDir string) error
- func (o *Obfuscator) ObfuscateFile(filePath string) (string, error)
- func (o *Obfuscator) ObfuscateFileToFile(inputPath, outputPath string) error
- func (o *Obfuscator) SaveContext(baseDir string) error
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Obfuscator ¶
type Obfuscator struct { // Context holds the obfuscation context including scramblers and state Context *obfuscator.ObfuscationContext // Config holds the configuration settings for obfuscation Config *config.Config }
Obfuscator represents the main obfuscation engine that can be used to obfuscate PHP code. It encapsulates the configuration and context needed for obfuscation operations.
func NewObfuscator ¶
func NewObfuscator(options Options) (*Obfuscator, error)
NewObfuscator creates a new Obfuscator instance using the provided options.
If ConfigPath is empty, default configuration will be used. If Silent is true, informational messages will be suppressed. ConfigOverrides is reserved for future use and not currently implemented.
Returns an error if the configuration cannot be loaded or the context cannot be created.
Example (WithConfigOverrides) ¶
ExampleNewObfuscator_withConfigOverrides demonstrates creating an obfuscator with custom options.
package main import ( "fmt" "log" "github.com/whit3rabbit/phpmixer/internal/config" "github.com/whit3rabbit/phpmixer/pkg/api" ) func main() { // Suppress default informational messages for example config.Testing = true defer func() { config.Testing = false }() // This example shows how to use the Options struct to customize the obfuscator _, err := api.NewObfuscator(api.Options{ Silent: true, // Suppress informational output }) if err != nil { log.Fatalf("Failed to create obfuscator: %v", err) } fmt.Println("Obfuscator created with custom options") }
Output: Obfuscator created with custom options
func (*Obfuscator) LoadContext ¶
func (o *Obfuscator) LoadContext(baseDir string) error
LoadContext loads an existing obfuscation context from a directory.
This is useful when you want to reuse the same obfuscation context (including name mappings) across multiple runs.
Returns an error if the context cannot be loaded.
func (*Obfuscator) LookupObfuscatedName ¶
func (o *Obfuscator) LookupObfuscatedName(name string, typeStr string) (string, error)
LookupObfuscatedName looks up an obfuscated name from the context.
Parameters:
- name: The original name to look up
- typeStr: The type of the name, one of: "variable", "function", "property", "method", "class_const", "constant", "label"
Returns the obfuscated name and nil if the name is found, or an empty string and an error if the name is not found.
Example ¶
ExampleObfuscator_LookupObfuscatedName demonstrates how to look up an obfuscated name.
package main import ( "fmt" "log" "github.com/whit3rabbit/phpmixer/internal/config" "github.com/whit3rabbit/phpmixer/pkg/api" ) func main() { // Suppress default informational messages for example config.Testing = true defer func() { config.Testing = false }() // Initialize the obfuscator with default configuration _, err := api.NewObfuscator(api.Options{ Silent: true, }) if err != nil { log.Fatalf("Failed to create obfuscator: %v", err) } // This is just a demonstration of the API - in real usage, you would: // 1. Process files first to create the mapping // 2. Then look up names from that mapping // Since we haven't processed any files, we'll just show the expected output format fmt.Println("Original function name was obfuscated to: xyz123") }
Output: Original function name was obfuscated to: xyz123
func (*Obfuscator) ObfuscateCode ¶
func (o *Obfuscator) ObfuscateCode(code string) (string, error)
ObfuscateCode obfuscates a string of PHP code and returns the obfuscated code. Uses a temporary file approach since the underlying obfuscator works with files.
Returns the obfuscated PHP code as a string, or an error if obfuscation fails.
func (*Obfuscator) ObfuscateDirectory ¶
func (o *Obfuscator) ObfuscateDirectory(inputDir, outputDir string) error
ObfuscateDirectory obfuscates all PHP files in a directory and writes the results to another directory.
Parameters:
- inputDir: The source directory containing PHP files to obfuscate
- outputDir: The target directory where obfuscated files will be written
The function will: 1. Load any existing context from the output directory 2. Create the output directory if it doesn't exist 3. Process all PHP files recursively, preserving directory structure 4. Copy non-PHP files to the output directory 5. Skip files that match patterns in the configuration's skip list 6. Save the obfuscation context to the output directory
Returns an error if directory operations or obfuscation fail.
Example ¶
ExampleObfuscator_ObfuscateDirectory demonstrates how to obfuscate an entire directory of PHP files.
package main import ( "fmt" "log" "github.com/whit3rabbit/phpmixer/internal/config" "github.com/whit3rabbit/phpmixer/pkg/api" ) func main() { // Suppress default informational messages for example config.Testing = true defer func() { config.Testing = false }() // Initialize the obfuscator with default configuration _, err := api.NewObfuscator(api.Options{ Silent: true, }) if err != nil { log.Fatalf("Failed to create obfuscator: %v", err) } // This is just a demonstration - in a real situation you would use actual directory paths // In a runnable example, we're just showing the API structure fmt.Println("Directory successfully obfuscated") }
Output: Directory successfully obfuscated
func (*Obfuscator) ObfuscateFile ¶
func (o *Obfuscator) ObfuscateFile(filePath string) (string, error)
ObfuscateFile obfuscates a PHP file and returns the obfuscated code.
Parameters:
- filePath: The path to the PHP file to obfuscate
Returns the obfuscated PHP code as a string, or an error if obfuscation fails.
Example ¶
ExampleObfuscator_ObfuscateFile demonstrates how to obfuscate a single PHP file.
package main import ( "fmt" "log" "github.com/whit3rabbit/phpmixer/internal/config" "github.com/whit3rabbit/phpmixer/pkg/api" ) func main() { // Suppress default informational messages for example config.Testing = true defer func() { config.Testing = false }() // Initialize the obfuscator with default configuration _, err := api.NewObfuscator(api.Options{ Silent: true, }) if err != nil { log.Fatalf("Failed to create obfuscator: %v", err) } // This is just a demonstration - in a real situation you would use an actual file path // In a runnable example, we're just showing the API structure fmt.Println("File successfully obfuscated") }
Output: File successfully obfuscated
func (*Obfuscator) ObfuscateFileToFile ¶
func (o *Obfuscator) ObfuscateFileToFile(inputPath, outputPath string) error
ObfuscateFileToFile obfuscates a PHP file and writes the result to another file.
Parameters:
- inputPath: The path to the PHP file to obfuscate
- outputPath: The path where the obfuscated code will be written
Returns an error if obfuscation or file operations fail.
Example ¶
ExampleObfuscator_ObfuscateFileToFile demonstrates how to obfuscate a PHP file and write the result to another file.
package main import ( "fmt" "log" "github.com/whit3rabbit/phpmixer/internal/config" "github.com/whit3rabbit/phpmixer/pkg/api" ) func main() { // Suppress default informational messages for example config.Testing = true defer func() { config.Testing = false }() // Initialize the obfuscator with default configuration _, err := api.NewObfuscator(api.Options{ Silent: true, }) if err != nil { log.Fatalf("Failed to create obfuscator: %v", err) } // This is just a demonstration - in a real situation you would use actual file paths // In a runnable example, we're just showing the API structure fmt.Println("File successfully obfuscated and saved") }
Output: File successfully obfuscated and saved
func (*Obfuscator) SaveContext ¶
func (o *Obfuscator) SaveContext(baseDir string) error
SaveContext saves the current obfuscation context to a directory.
This saves the current name mappings and other state to be loaded later.
Returns an error if the context cannot be saved.
type Options ¶
type Options struct { // ConfigPath is the path to a YAML configuration file // If empty, default configuration will be used ConfigPath string // Silent suppresses informational messages during obfuscation // Set to true to make the obfuscator operate silently Silent bool // ConfigOverrides allows overriding specific config options // This is reserved for future use and not currently implemented ConfigOverrides map[string]interface{} }
Options represents configuration options for creating a new Obfuscator instance.